This is an automated email from the ASF dual-hosted git repository.
maximebeauchemin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git
The following commit(s) were added to refs/heads/master by this push:
new 8051818 [pie] allow formating nubmers on pie chart (#6628)
8051818 is described below
commit 8051818693672e2d03e77d270d66b6b7b52cc505
Author: Maxime Beauchemin <[email protected]>
AuthorDate: Fri Jan 11 08:28:58 2019 -0800
[pie] allow formating nubmers on pie chart (#6628)
---
superset/assets/src/explore/controlPanels/Pie.js | 8 +++++++-
superset/assets/src/visualizations/nvd3/NVD3Vis.js | 19 +++++++++----------
.../assets/src/visualizations/nvd3/transformProps.js | 2 ++
3 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/superset/assets/src/explore/controlPanels/Pie.js
b/superset/assets/src/explore/controlPanels/Pie.js
index b5eab4d..eb80963 100644
--- a/superset/assets/src/explore/controlPanels/Pie.js
+++ b/superset/assets/src/explore/controlPanels/Pie.js
@@ -16,7 +16,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
- ['pie_label_type'],
+ ['pie_label_type', 'number_format'],
['donut', 'show_legend'],
['show_labels', 'labels_outside'],
['color_scheme'],
@@ -27,5 +27,11 @@ export default {
row_limit: {
default: 25,
},
+ number_format: {
+ description: (
+ t('D3 format syntax: https://github.com/d3/d3-format') + ' ' +
+ t('Only applies when the "Label Type" is not set to a percentage.')
+ ),
+ },
},
};
diff --git a/superset/assets/src/visualizations/nvd3/NVD3Vis.js
b/superset/assets/src/visualizations/nvd3/NVD3Vis.js
index 0b8fb2e..dcaf26a 100644
--- a/superset/assets/src/visualizations/nvd3/NVD3Vis.js
+++ b/superset/assets/src/visualizations/nvd3/NVD3Vis.js
@@ -6,7 +6,7 @@ import moment from 'moment';
import PropTypes from 'prop-types';
import { t } from '@superset-ui/translation';
import { CategoricalColorNamespace } from '@superset-ui/color';
-import { getNumberFormatter, formatNumber, NumberFormats } from
'@superset-ui/number-format';
+import { getNumberFormatter, NumberFormats } from '@superset-ui/number-format';
import { getTimeFormatter, smartDateVerboseFormatter } from
'@superset-ui/time-format';
import 'nvd3/build/nv.d3.min.css';
@@ -127,6 +127,7 @@ const propTypes = {
'dual_line',
]),
xAxisFormat: PropTypes.string,
+ numberFormat: PropTypes.string,
xAxisLabel: PropTypes.string,
xAxisShowMinMax: PropTypes.bool,
xIsLogScale: PropTypes.bool,
@@ -213,6 +214,7 @@ function nvd3Vis(element, props) {
useRichTooltip,
vizType,
xAxisFormat,
+ numberFormat,
xAxisLabel,
xAxisShowMinMax = false,
xField,
@@ -264,6 +266,7 @@ function nvd3Vis(element, props) {
isTruthy(showBrush) ||
(showBrush === 'auto' && maxHeight >= MIN_HEIGHT_FOR_BRUSH &&
xTicksLayout !== '45°')
);
+ const numberFormatter = getNumberFormatter(numberFormat);
switch (vizType) {
case 'line':
@@ -331,7 +334,7 @@ function nvd3Vis(element, props) {
case 'pie':
chart = nv.models.pieChart();
colorKey = 'x';
- chart.valueFormat(formatter);
+ chart.valueFormat(numberFormatter);
if (isDonut) {
chart.donut(true);
}
@@ -341,18 +344,14 @@ function nvd3Vis(element, props) {
chart.labelThreshold(0.05);
chart.cornerRadius(true);
- if (pieLabelType !== 'key_percent' && pieLabelType !== 'key_value') {
+ if (['key', 'value', 'percent'].indexOf(pieLabelType) >= 0) {
chart.labelType(pieLabelType);
} else if (pieLabelType === 'key_value') {
- chart.labelType(d => `${d.data.x}: ${formatNumber(NumberFormats.SI,
d.data.y)}`);
- }
-
- if (pieLabelType === 'percent' || pieLabelType === 'key_percent') {
+ chart.labelType(d => `${d.data.x}: ${numberFormatter(d.data.y)}`);
+ } else if (pieLabelType === 'key_percent') {
const total = d3.sum(data, d => d.y);
chart.tooltip.valueFormatter(d => `${((d / total) *
100).toFixed()}%`);
- if (pieLabelType === 'key_percent') {
- chart.labelType(d => `${d.data.x}: ${((d.data.y / total) *
100).toFixed()}%`);
- }
+ chart.labelType(d => `${d.data.x}: ${((d.data.y / total) *
100).toFixed()}%`);
}
// Pie chart does not need top margin
chart.margin({ top: 0 });
diff --git a/superset/assets/src/visualizations/nvd3/transformProps.js
b/superset/assets/src/visualizations/nvd3/transformProps.js
index 9126837..1ddeffd 100644
--- a/superset/assets/src/visualizations/nvd3/transformProps.js
+++ b/superset/assets/src/visualizations/nvd3/transformProps.js
@@ -45,6 +45,7 @@ export default function transformProps(chartProps) {
xAxisFormat,
xAxisLabel,
xAxisShowminmax,
+ numberFormat,
xLogScale,
xTicksLayout,
y,
@@ -83,6 +84,7 @@ export default function transformProps(chartProps) {
leftMargin,
lineInterpolation,
maxBubbleSize: parseInt(maxBubbleSize, 10),
+ numberFormat,
onBrushEnd: isTruthy(sendTimeRange) ? ((timeRange) => {
onAddFilter('__time_range', timeRange, false, true);
}) : undefined,