Skip to content

Commit d38dcb9

Browse files
committed
fix: Making viz components respect D3 Format from metric (apache-superset#280)
* fix: BigNumber uses metric's d3format value now, when populated. * fix: respecting D3 Format column for a handful of NVD3 charts * fix: treemap respects metric's D3 Format setting * style: Simpler loop syntax using forEach, Prettier cleanup. * style: prettier * style: re-installed modules, re-ran prettier * fix: eslint nits * style: moving grabD3Format outside the transformProps block * fix: allow formData to override metric's D3 Format * style: overwriting yAxisFormat rather than declaring a new var
1 parent 833aac8 commit d38dcb9

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

plugins/superset-ui-plugins/packages/superset-ui-legacy-plugin-chart-treemap/src/transformProps.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@
1919
/* eslint-disable sort-keys */
2020
export default function transformProps(chartProps) {
2121
const { width, height, formData, queryData } = chartProps;
22-
const { colorScheme, numberFormat, treemapRatio } = formData;
22+
const { colorScheme, treemapRatio } = formData;
23+
let { numberFormat } = formData;
24+
25+
if (!numberFormat && chartProps.datasource && chartProps.datasource.metrics) {
26+
chartProps.datasource.metrics.forEach(metric => {
27+
if (metric.metric_name === chartProps.formData.metrics[0] && metric.d3format) {
28+
numberFormat = metric.d3format;
29+
}
30+
});
31+
}
2332

2433
return {
2534
width,

plugins/superset-ui-plugins/packages/superset-ui-legacy-preset-chart-big-number/src/BigNumber/transformProps.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export default function transformProps(chartProps) {
3636
startYAxisAtZero,
3737
subheader = '',
3838
vizType,
39-
yAxisFormat,
4039
} = formData;
40+
let { yAxisFormat } = formData;
4141
const { data } = queryData;
4242

4343
let mainColor;
@@ -68,7 +68,10 @@ export default function transformProps(chartProps) {
6868
}
6969
}
7070
trendLineData = supportAndShowTrendLine
71-
? sortedData.map(point => ({ x: point[TIME_COLUMN], y: point[metricName] }))
71+
? sortedData.map(point => ({
72+
x: point[TIME_COLUMN],
73+
y: point[metricName],
74+
}))
7275
: null;
7376
} else {
7477
bigNumber = data[0][metricName];
@@ -82,6 +85,14 @@ export default function transformProps(chartProps) {
8285
className = 'negative';
8386
}
8487

88+
if (!yAxisFormat && chartProps.datasource && chartProps.datasource.metrics) {
89+
chartProps.datasource.metrics.forEach(metricEntry => {
90+
if (metricEntry.metric_name === metric && metricEntry.d3format) {
91+
yAxisFormat = metricEntry.d3format;
92+
}
93+
});
94+
}
95+
8596
const formatValue = getNumberFormatter(yAxisFormat);
8697

8798
return {

plugins/superset-ui-plugins/packages/superset-ui-legacy-preset-chart-nvd3/src/transformProps.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ import { formatLabel } from './utils';
2222

2323
const NOOP = () => {};
2424

25+
const grabD3Format = (chartProps, targetMetric) => {
26+
let foundFormatter;
27+
chartProps.datasource.metrics.forEach(metric => {
28+
if (metric.d3format && metric.metric_name === targetMetric) {
29+
foundFormatter = metric.d3format;
30+
}
31+
});
32+
33+
return foundFormatter;
34+
};
35+
2536
export default function transformProps(chartProps) {
2637
const { width, height, annotationData, datasource, formData, hooks, queryData } = chartProps;
2738

@@ -59,18 +70,17 @@ export default function transformProps(chartProps) {
5970
xAxisFormat,
6071
xAxisLabel,
6172
xAxisShowminmax,
62-
numberFormat,
6373
xLogScale,
6474
xTicksLayout,
6575
y,
66-
yAxisFormat,
67-
yAxis2Format,
6876
yAxisBounds,
6977
yAxisLabel,
7078
yAxisShowminmax,
7179
yLogScale,
7280
} = formData;
7381

82+
let { numberFormat, yAxisFormat, yAxis2Format } = formData;
83+
7484
const rawData = queryData.data || [];
7585
const data = Array.isArray(rawData)
7686
? rawData.map(row => ({
@@ -79,6 +89,15 @@ export default function transformProps(chartProps) {
7989
}))
8090
: rawData;
8191

92+
if (chartProps.formData.vizType === 'pie') {
93+
numberFormat = numberFormat || grabD3Format(chartProps, chartProps.formData.metric);
94+
} else if (chartProps.formData.vizType === 'dual_line') {
95+
yAxisFormat = yAxisFormat || grabD3Format(chartProps, chartProps.formData.metric);
96+
yAxis2Format = yAxis2Format || grabD3Format(chartProps, chartProps.formData.metric2);
97+
} else if (['line', 'dist_bar', 'bar', 'area'].indexOf(chartProps.formData.vizType) > -1) {
98+
yAxisFormat = yAxisFormat || grabD3Format(chartProps, chartProps.formData.metrics[0]);
99+
}
100+
82101
return {
83102
width,
84103
height,

0 commit comments

Comments
 (0)