Skip to content

Commit 4317795

Browse files
mistercrunchzhaoyongjie
authored andcommittedNov 26, 2021
feat: improve world map colors (apache#711)
* feat: improve world map colors * sqrt * addressing comment * ad d3-color
1 parent 835335d commit 4317795

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed
 

‎superset-frontend/temporary_superset_ui/superset-ui/plugins/legacy-plugin-chart-world-map/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929
},
3030
"dependencies": {
3131
"d3": "^3.5.17",
32+
"d3-array": "^2.4.0",
33+
"d3-color": "^1.4.1",
3234
"datamaps": "^0.5.8",
3335
"prop-types": "^15.6.2"
3436
},
3537
"peerDependencies": {
3638
"@superset-ui/chart": "^0.14.0",
3739
"@superset-ui/chart-controls": "^0.14.0",
40+
"@superset-ui/color": "^0.14.9",
3841
"@superset-ui/number-format": "^0.14.0",
3942
"@superset-ui/style": "^0.14.3",
4043
"@superset-ui/translation": "^0.14.0",

‎superset-frontend/temporary_superset_ui/superset-ui/plugins/legacy-plugin-chart-world-map/src/WorldMap.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
/* eslint-disable react/sort-prop-types */
2020
import d3 from 'd3';
2121
import PropTypes from 'prop-types';
22+
import { extent as d3Extent } from 'd3-array';
23+
import { getSequentialSchemeRegistry } from '@superset-ui/color';
2224
import Datamap from 'datamaps/dist/datamaps.world.min';
2325
import { getNumberFormatter } from '@superset-ui/number-format';
2426

@@ -36,33 +38,34 @@ const propTypes = {
3638
height: PropTypes.number,
3739
maxBubbleSize: PropTypes.number,
3840
showBubbles: PropTypes.bool,
41+
linearColorScheme: PropTypes.string,
42+
color: PropTypes.string,
3943
};
4044

4145
const formatter = getNumberFormatter();
4246

4347
function WorldMap(element, props) {
44-
const { data, width, height, maxBubbleSize, showBubbles } = props;
45-
48+
const { data, width, height, maxBubbleSize, showBubbles, linearColorScheme, color } = props;
4649
const div = d3.select(element);
4750
div.classed('superset-legacy-chart-world-map', true);
4851
div.selectAll('*').remove();
4952

5053
// Ignore XXX's to get better normalization
5154
const filteredData = data.filter(d => d.country && d.country !== 'XXX');
5255

53-
const ext = d3.extent(filteredData, d => d.m1);
54-
const extRadius = d3.extent(filteredData, d => d.m2);
56+
const extRadius = d3.extent(filteredData, d => Math.sqrt(d.m2));
5557
const radiusScale = d3.scale
5658
.linear()
5759
.domain([extRadius[0], extRadius[1]])
5860
.range([1, maxBubbleSize]);
5961

60-
// color gradient based on http://colorbrewer2.org/#type=sequential&scheme=Greens&n=9
61-
const colorScale = d3.scale.linear().domain([ext[0], ext[1]]).range(['#c7e9c0', '#00441b']);
62+
const colorScale = getSequentialSchemeRegistry()
63+
.get(linearColorScheme)
64+
.createLinearScale(d3Extent(filteredData, d => d.m1));
6265

6366
const processedData = filteredData.map(d => ({
6467
...d,
65-
radius: radiusScale(d.m2),
68+
radius: radiusScale(Math.sqrt(d.m2)),
6669
fillColor: colorScale(d.m1),
6770
}));
6871

@@ -85,23 +88,23 @@ function WorldMap(element, props) {
8588
borderWidth: 1,
8689
borderColor: '#feffff',
8790
highlightBorderColor: '#feffff',
88-
highlightFillColor: '#00749A',
91+
highlightFillColor: color,
8992
highlightBorderWidth: 1,
9093
popupTemplate: (geo, d) =>
9194
`<div class="hoverinfo"><strong>${d.name}</strong><br>${formatter(d.m1)}</div>`,
9295
},
9396
bubblesConfig: {
9497
borderWidth: 1,
9598
borderOpacity: 1,
96-
borderColor: '#00749A',
99+
borderColor: color,
97100
popupOnHover: true,
98101
radius: null,
99102
popupTemplate: (geo, d) =>
100103
`<div class="hoverinfo"><strong>${d.name}</strong><br>${formatter(d.m2)}</div>`,
101104
fillOpacity: 0.5,
102105
animate: true,
103106
highlightOnHover: true,
104-
highlightFillColor: '#00749A',
107+
highlightFillColor: color,
105108
highlightBorderColor: 'black',
106109
highlightBorderWidth: 2,
107110
highlightBorderOpacity: 1,
@@ -115,7 +118,7 @@ function WorldMap(element, props) {
115118

116119
if (showBubbles) {
117120
map.bubbles(processedData);
118-
div.selectAll('circle.datamaps-bubble').style('fill', '#00749A');
121+
div.selectAll('circle.datamaps-bubble').style('fill', color).style('stroke', color);
119122
}
120123
}
121124

‎superset-frontend/temporary_superset_ui/superset-ui/plugins/legacy-plugin-chart-world-map/src/controlPanel.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export default {
5252
],
5353
},
5454
{
55-
label: t('Bubbles'),
55+
label: t('Options'),
56+
expanded: true,
5657
controlSetRows: [
5758
[
5859
{
@@ -79,21 +80,29 @@ export default {
7980
},
8081
},
8182
],
83+
['color_picker'],
84+
['linear_color_scheme'],
8285
],
8386
},
8487
],
8588
controlOverrides: {
8689
entity: {
87-
label: t('Country Control'),
90+
label: t('Country Column'),
8891
description: t('3 letter code of the country'),
8992
},
9093
metric: {
91-
label: t('Metric for color'),
94+
label: t('Metric for Color'),
9295
description: t('Metric that defines the color of the country'),
9396
},
9497
secondary_metric: {
95-
label: t('Bubble size'),
98+
label: t('Bubble Size'),
9699
description: t('Metric that defines the size of the bubble'),
97100
},
101+
color_picker: {
102+
label: t('Bubble Color'),
103+
},
104+
linear_color_scheme: {
105+
label: t('Country Color Scheme'),
106+
},
98107
},
99108
};

‎superset-frontend/temporary_superset_ui/superset-ui/plugins/legacy-plugin-chart-world-map/src/transformProps.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
import { rgb } from 'd3-color';
20+
1921
export default function transformProps(chartProps) {
2022
const { width, height, formData, queryData } = chartProps;
21-
const { maxBubbleSize, showBubbles } = formData;
23+
const { maxBubbleSize, showBubbles, linearColorScheme, colorPicker } = formData;
24+
const { r, g, b } = colorPicker;
2225

2326
return {
2427
data: queryData.data,
2528
width,
2629
height,
2730
maxBubbleSize: parseInt(maxBubbleSize, 10),
2831
showBubbles,
32+
linearColorScheme,
33+
color: rgb(r, g, b).hex(),
2934
};
3035
}

0 commit comments

Comments
 (0)