Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add d3 legend formatting for Arc, Polygon and Scatter deck.gl maps #7951

Merged
merged 3 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions superset/assets/src/explore/controlPanels/DeckArc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default {
['color_picker', 'target_color_picker'],
['dimension', 'color_scheme', 'label_colors'],
['stroke_width', 'legend_position'],
['legend_format', null],
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/DeckPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default {
['linear_color_scheme', 'opacity'],
['num_buckets', 'break_points'],
['table_filter', 'toggle_polygons'],
['legend_position', null],
['legend_position', 'legend_format'],
],
},
{
Expand Down
1 change: 1 addition & 0 deletions superset/assets/src/explore/controlPanels/DeckScatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default {
label: t('Point Color'),
controlSetRows: [
['color_picker', 'legend_position'],
[null, 'legend_format'],
['dimension', 'color_scheme', 'label_colors'],
],
},
Expand Down
10 changes: 10 additions & 0 deletions superset/assets/src/explore/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ export const controls = {
renderTrigger: true,
},

legend_format: {
label: t('Legend Format'),
description: t('Choose the format for legend values'),
type: 'SelectControl',
clearable: false,
default: D3_FORMAT_OPTIONS[0],
choices: D3_FORMAT_OPTIONS,
renderTrigger: true,
},

fill_color_picker: {
label: t('Fill Color'),
description: t(' Set the opacity to 0 if you do not want to override the color specified in the GeoJSON'),
Expand Down
30 changes: 29 additions & 1 deletion superset/assets/src/visualizations/Legend.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,52 @@
*/
import React from 'react';
import PropTypes from 'prop-types';
import { formatNumber } from '@superset-ui/number-format';

import './Legend.css';

const categoryDelimiter = ' - ';

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

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

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

const numValue = parseFloat(value);
return formatNumber(this.props.format, numValue);

}

formatCategoryLabel(k) {
if (!this.props.format) {
return k;
}

if (k.includes(categoryDelimiter)) {
const values = k.split(categoryDelimiter);
return this.format(values[0]) + categoryDelimiter + this.format(values[1]);
}

return this.format(k);
}

render() {
if (Object.keys(this.props.categories).length === 0 || this.props.position === null) {
return null;
Expand All @@ -51,7 +79,7 @@ export default class Legend extends React.PureComponent {
onClick={() => this.props.toggleCategory(k)}
onDoubleClick={() => this.props.showSingleCategory(k)}
>
<span style={style}>{icon}</span> {k}
<span style={style}>{icon}</span> {this.formatCategoryLabel(k)}
</a>
</li>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export default class CategoricalDeckGLContainer extends React.PureComponent {
toggleCategory={this.toggleCategory}
showSingleCategory={this.showSingleCategory}
position={this.props.formData.legend_position}
format={this.props.formData.legend_format}
/>
</AnimatableDeckGLContainer>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class DeckGLPolygon extends React.Component {
<Legend
categories={buckets}
position={formData.legend_position}
format={formData.legend_format}
/>}
</AnimatableDeckGLContainer>
</div>
Expand Down