Skip to content

Commit

Permalink
fix(scatter): fix legend and labels (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored and zhaoyongjie committed Nov 30, 2021
1 parent 55b3c79 commit c3e5edf
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ function getCategories(fd, data) {
}

const propTypes = {
datasource: PropTypes.object.isRequired,
formData: PropTypes.object.isRequired,
mapboxApiKey: PropTypes.string.isRequired,
setControlValue: PropTypes.func.isRequired,
viewport: PropTypes.object.isRequired,
getLayer: PropTypes.func.isRequired,
getPoints: PropTypes.func.isRequired,
payload: PropTypes.object.isRequired,
height: PropTypes.number.isRequired,
mapboxApiKey: PropTypes.string.isRequired,
onAddFilter: PropTypes.func,
payload: PropTypes.object.isRequired,
setControlValue: PropTypes.func.isRequired,
viewport: PropTypes.object.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
};

export default class CategoricalDeckGLContainer extends React.PureComponent {
Expand Down Expand Up @@ -179,7 +180,7 @@ export default class CategoricalDeckGLContainer extends React.PureComponent {
data: { ...payload.data, features },
};

return [getLayer(fd, filteredPayload, onAddFilter, this.setTooltip)];
return [getLayer(fd, filteredPayload, onAddFilter, this.setTooltip, this.props.datasource)];
}

// eslint-disable-next-line class-methods-use-this
Expand Down Expand Up @@ -255,11 +256,12 @@ export default class CategoricalDeckGLContainer extends React.PureComponent {
height={this.props.height}
>
<Legend
forceCategorical
categories={this.state.categories}
toggleCategory={this.toggleCategory}
showSingleCategory={this.showSingleCategory}
position={this.props.formData.legend_position}
format={this.props.formData.legend_format}
position={this.props.formData.legend_position}
showSingleCategory={this.showSingleCategory}
toggleCategory={this.toggleCategory}
/>
</AnimatableDeckGLContainer>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@ const categoryDelimiter = ' - ';

const propTypes = {
categories: PropTypes.object,
toggleCategory: PropTypes.func,
showSingleCategory: PropTypes.func,
forceCategorical: PropTypes.bool,
format: PropTypes.string,
position: PropTypes.oneOf([null, 'tl', 'tr', 'bl', 'br']),
showSingleCategory: PropTypes.func,
toggleCategory: PropTypes.func,
};

const defaultProps = {
categories: {},
toggleCategory: () => {},
showSingleCategory: () => {},
forceCategorical: false,
format: null,
position: 'tr',
showSingleCategory: () => {},
toggleCategory: () => {},
};

export default class Legend extends React.PureComponent {
format(value) {
if (!this.props.format) {
if (!this.props.format || this.props.forceCategorical) {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import CategoricalDeckGLContainer from './CategoricalDeckGLContainer';
import fitViewport from './utils/fitViewport';

const propTypes = {
datasource: PropTypes.object.isRequired,
formData: PropTypes.object.isRequired,
height: PropTypes.number.isRequired,
onAddFilter: PropTypes.func,
payload: PropTypes.object.isRequired,
setControlValue: PropTypes.func.isRequired,
viewport: PropTypes.object.isRequired,
onAddFilter: PropTypes.func,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
};
const defaultProps = {
onAddFilter() {},
Expand Down Expand Up @@ -120,10 +121,11 @@ export function createDeckGLComponent(getLayer, getPoints) {

export function createCategoricalDeckGLComponent(getLayer, getPoints) {
function Component(props) {
const { formData, payload, setControlValue, viewport, width, height } = props;
const { datasource, formData, height, payload, setControlValue, viewport, width } = props;

return (
<CategoricalDeckGLContainer
datasource={datasource}
formData={formData}
mapboxApiKey={payload.data.mapboxApiKey}
setControlValue={setControlValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import { ScatterplotLayer } from 'deck.gl';
import React from 'react';
import { t } from '@superset-ui/core';
import { getMetricLabel, t } from '@superset-ui/core';
import { commonLayerProps } from '../common';
import { createCategoricalDeckGLComponent } from '../../factory';
import TooltipRow from '../../TooltipRow';
Expand All @@ -28,27 +28,27 @@ function getPoints(data) {
return data.map(d => d.position);
}

function setTooltipContent(formData) {
return o => (
<div className="deckgl-tooltip">
<TooltipRow
label={`${t('Longitude and Latitude')}: `}
value={`${o.object.position[0]}, ${o.object.position[1]}`}
/>
{o.object.cat_color && (
<TooltipRow label={`${t('Category')}: `} value={`${o.object.cat_color}`} />
)}
{o.object.metric && (
function setTooltipContent(formData, verboseMap) {
return o => {
const label =
verboseMap?.[formData.point_radius_fixed.value] ||
getMetricLabel(formData.point_radius_fixed?.value);
return (
<div className="deckgl-tooltip">
<TooltipRow
label={`${formData.point_radius_fixed.value.label}: `}
value={`${o.object.metric}`}
label={`${t('Longitude and Latitude')}: `}
value={`${o.object.position[0]}, ${o.object.position[1]}`}
/>
)}
</div>
);
{o.object.cat_color && (
<TooltipRow label={`${t('Category')}: `} value={`${o.object.cat_color}`} />
)}
{o.object.metric && <TooltipRow label={`${label}: `} value={`${o.object.metric}`} />}
</div>
);
};
}

export function getLayer(formData, payload, onAddFilter, setTooltip) {
export function getLayer(formData, payload, onAddFilter, setTooltip, datasource) {
const fd = formData;
const dataWithRadius = payload.data.features.map(d => {
let radius = unitToRadius(fd.point_unit, d.radius) || 10;
Expand All @@ -73,7 +73,7 @@ export function getLayer(formData, payload, onAddFilter, setTooltip) {
radiusMinPixels: fd.min_radius || null,
radiusMaxPixels: fd.max_radius || null,
stroked: false,
...commonLayerProps(fd, setTooltip, setTooltipContent(fd)),
...commonLayerProps(fd, setTooltip, setTooltipContent(fd, datasource?.verboseMap)),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export default {
{
label: t('Point Size'),
controlSetRows: [
[pointRadiusFixed],
[
pointRadiusFixed,
{
name: 'point_unit',
config: {
Expand Down Expand Up @@ -125,17 +125,17 @@ export default {
{
label: t('Point Color'),
controlSetRows: [
['color_picker', legendPosition],
[null, legendFormat],
['color_picker'],
[legendPosition],
[legendFormat],
[
{
...dimension,
label: t('Categorical Color'),
description: t('Pick a dimension from which categorical colors are defined'),
},
'color_scheme',
'label_colors',
],
['color_scheme'],
],
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
const NOOP = () => {};

export default function transformProps(chartProps) {
const { width, height, rawFormData, queriesData, hooks } = chartProps;
const { datasource, height, hooks, queriesData, rawFormData, width } = chartProps;
const { onAddFilter = NOOP, setControlValue = NOOP } = hooks;

return {
datasource,
formData: rawFormData,
height,
onAddFilter,
Expand Down

0 comments on commit c3e5edf

Please sign in to comment.