@gravity-ui/charts 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/components/Tooltip/ChartTooltipContent.d.ts +1 -0
  3. package/dist/cjs/components/Tooltip/ChartTooltipContent.js +2 -2
  4. package/dist/cjs/components/Tooltip/DefaultContent.d.ts +3 -2
  5. package/dist/cjs/components/Tooltip/DefaultContent.js +37 -8
  6. package/dist/cjs/components/Tooltip/index.js +1 -1
  7. package/dist/cjs/hooks/useSeries/prepare-area.js +2 -1
  8. package/dist/cjs/hooks/useSeries/prepare-bar-x.js +2 -1
  9. package/dist/cjs/hooks/useSeries/prepare-bar-y.js +7 -3
  10. package/dist/cjs/hooks/useSeries/prepare-line.js +2 -1
  11. package/dist/cjs/hooks/useSeries/prepare-pie.js +5 -4
  12. package/dist/cjs/hooks/useSeries/prepare-radar.js +2 -1
  13. package/dist/cjs/hooks/useSeries/prepare-sankey.js +2 -1
  14. package/dist/cjs/hooks/useSeries/prepare-treemap.js +2 -1
  15. package/dist/cjs/hooks/useSeries/prepare-waterfall.js +2 -1
  16. package/dist/cjs/hooks/useSeries/types.d.ts +10 -1
  17. package/dist/cjs/hooks/useShapes/area/prepare-data.js +2 -1
  18. package/dist/cjs/hooks/useShapes/bar-x/prepare-data.js +2 -1
  19. package/dist/cjs/hooks/useShapes/bar-y/prepare-data.js +2 -1
  20. package/dist/cjs/hooks/useShapes/line/prepare-data.js +2 -1
  21. package/dist/cjs/hooks/useShapes/pie/prepare-data.js +2 -1
  22. package/dist/cjs/hooks/useShapes/radar/prepare-data.js +2 -1
  23. package/dist/cjs/hooks/useShapes/sankey/prepare-data.js +3 -1
  24. package/dist/cjs/hooks/useShapes/treemap/prepare-data.js +8 -6
  25. package/dist/cjs/hooks/useShapes/waterfall/prepare-data.js +2 -1
  26. package/dist/cjs/types/chart/base.d.ts +12 -0
  27. package/dist/cjs/types/chart/tooltip.d.ts +3 -0
  28. package/dist/cjs/utils/chart/axis-generators/bottom.js +4 -3
  29. package/dist/cjs/utils/chart/format.d.ts +5 -0
  30. package/dist/cjs/utils/chart/format.js +17 -0
  31. package/dist/esm/components/Tooltip/ChartTooltipContent.d.ts +1 -0
  32. package/dist/esm/components/Tooltip/ChartTooltipContent.js +2 -2
  33. package/dist/esm/components/Tooltip/DefaultContent.d.ts +3 -2
  34. package/dist/esm/components/Tooltip/DefaultContent.js +37 -8
  35. package/dist/esm/components/Tooltip/index.js +1 -1
  36. package/dist/esm/hooks/useSeries/prepare-area.js +2 -1
  37. package/dist/esm/hooks/useSeries/prepare-bar-x.js +2 -1
  38. package/dist/esm/hooks/useSeries/prepare-bar-y.js +7 -3
  39. package/dist/esm/hooks/useSeries/prepare-line.js +2 -1
  40. package/dist/esm/hooks/useSeries/prepare-pie.js +5 -4
  41. package/dist/esm/hooks/useSeries/prepare-radar.js +2 -1
  42. package/dist/esm/hooks/useSeries/prepare-sankey.js +2 -1
  43. package/dist/esm/hooks/useSeries/prepare-treemap.js +2 -1
  44. package/dist/esm/hooks/useSeries/prepare-waterfall.js +2 -1
  45. package/dist/esm/hooks/useSeries/types.d.ts +10 -1
  46. package/dist/esm/hooks/useShapes/area/prepare-data.js +2 -1
  47. package/dist/esm/hooks/useShapes/bar-x/prepare-data.js +2 -1
  48. package/dist/esm/hooks/useShapes/bar-y/prepare-data.js +2 -1
  49. package/dist/esm/hooks/useShapes/line/prepare-data.js +2 -1
  50. package/dist/esm/hooks/useShapes/pie/prepare-data.js +2 -1
  51. package/dist/esm/hooks/useShapes/radar/prepare-data.js +2 -1
  52. package/dist/esm/hooks/useShapes/sankey/prepare-data.js +3 -1
  53. package/dist/esm/hooks/useShapes/treemap/prepare-data.js +8 -6
  54. package/dist/esm/hooks/useShapes/waterfall/prepare-data.js +2 -1
  55. package/dist/esm/types/chart/base.d.ts +12 -0
  56. package/dist/esm/types/chart/tooltip.d.ts +3 -0
  57. package/dist/esm/utils/chart/axis-generators/bottom.js +4 -3
  58. package/dist/esm/utils/chart/format.d.ts +5 -0
  59. package/dist/esm/utils/chart/format.js +17 -0
  60. package/package.json +1 -1
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Gravity Charts · [![npm package](https://img.shields.io/npm/v/@gravity-ui/charts)](https://www.npmjs.com/package/@gravity-ui/charts) [![CI](https://img.shields.io/github/actions/workflow/status/gravity-ui/charts/.github/workflows/ci.yml?label=CI&logo=github)](https://github.com/gravity-ui/charts/actions/workflows/ci.yml?query=branch:main) [![storybook](https://img.shields.io/badge/Storybook-deployed-ff4685)](https://preview.gravity-ui.com/charts/)
1
+ # Gravity UI Charts · [![npm package](https://img.shields.io/npm/v/@gravity-ui/charts)](https://www.npmjs.com/package/@gravity-ui/charts) [![CI](https://img.shields.io/github/actions/workflow/status/gravity-ui/charts/.github/workflows/ci.yml?label=CI&logo=github)](https://github.com/gravity-ui/charts/actions/workflows/ci.yml?query=branch:main) [![storybook](https://img.shields.io/badge/Storybook-deployed-ff4685)](https://preview.gravity-ui.com/charts/)
2
2
 
3
3
  ## Install
4
4
 
@@ -5,5 +5,6 @@ export interface ChartTooltipContentProps {
5
5
  xAxis?: ChartXAxis;
6
6
  yAxis?: ChartYAxis;
7
7
  renderer?: ChartTooltip['renderer'];
8
+ valueFormat?: ChartTooltip['valueFormat'];
8
9
  }
9
10
  export declare const ChartTooltipContent: (props: ChartTooltipContentProps) => React.JSX.Element | null;
@@ -2,10 +2,10 @@ import React from 'react';
2
2
  import isNil from 'lodash/isNil';
3
3
  import { DefaultContent } from './DefaultContent';
4
4
  export const ChartTooltipContent = (props) => {
5
- const { hovered, xAxis, yAxis, renderer } = props;
5
+ const { hovered, xAxis, yAxis, renderer, valueFormat } = props;
6
6
  if (!hovered) {
7
7
  return null;
8
8
  }
9
9
  const customTooltip = renderer === null || renderer === void 0 ? void 0 : renderer({ hovered, xAxis, yAxis });
10
- return isNil(customTooltip) ? (React.createElement(DefaultContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis })) : (customTooltip);
10
+ return isNil(customTooltip) ? (React.createElement(DefaultContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis, valueFormat: valueFormat })) : (customTooltip);
11
11
  };
@@ -1,9 +1,10 @@
1
1
  import React from 'react';
2
- import type { ChartXAxis, ChartYAxis, TooltipDataChunk } from '../../types';
2
+ import type { ChartXAxis, ChartYAxis, TooltipDataChunk, ValueFormat } from '../../types';
3
3
  type Props = {
4
4
  hovered: TooltipDataChunk[];
5
5
  xAxis?: ChartXAxis;
6
6
  yAxis?: ChartYAxis;
7
+ valueFormat?: ValueFormat;
7
8
  };
8
- export declare const DefaultContent: ({ hovered, xAxis, yAxis }: Props) => React.JSX.Element;
9
+ export declare const DefaultContent: ({ hovered, xAxis, yAxis, valueFormat }: Props) => React.JSX.Element;
9
10
  export {};
@@ -3,6 +3,7 @@ import { dateTime } from '@gravity-ui/date-utils';
3
3
  import get from 'lodash/get';
4
4
  import { formatNumber } from '../../libs';
5
5
  import { block, getDataCategoryValue, getWaterfallPointSubtotal } from '../../utils';
6
+ import { getFormattedValue } from '../../utils/chart/format';
6
7
  const b = block('tooltip');
7
8
  const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
8
9
  const getRowData = (fieldName, data, axis) => {
@@ -40,7 +41,7 @@ const getMeasureValue = (data, xAxis, yAxis) => {
40
41
  }
41
42
  return getXRowData((_d = data[0]) === null || _d === void 0 ? void 0 : _d.data, xAxis);
42
43
  };
43
- export const DefaultContent = ({ hovered, xAxis, yAxis }) => {
44
+ export const DefaultContent = ({ hovered, xAxis, yAxis, valueFormat }) => {
44
45
  const measureValue = getMeasureValue(hovered, xAxis, yAxis);
45
46
  return (React.createElement(React.Fragment, null,
46
47
  measureValue && React.createElement("div", null, measureValue),
@@ -54,17 +55,29 @@ export const DefaultContent = ({ hovered, xAxis, yAxis }) => {
54
55
  case 'line':
55
56
  case 'area':
56
57
  case 'bar-x': {
58
+ const formattedValue = getFormattedValue({
59
+ value: getYRowData(data, yAxis),
60
+ format: valueFormat,
61
+ });
57
62
  const value = (React.createElement(React.Fragment, null,
58
63
  series.name,
59
64
  ": ",
60
- getYRowData(data, yAxis)));
65
+ formattedValue));
61
66
  return (React.createElement("div", { key: id, className: b('content-row') },
62
67
  React.createElement("div", { className: b('color'), style: { backgroundColor: color } }),
63
68
  React.createElement("div", null, closest ? React.createElement("b", null, value) : React.createElement("span", null, value))));
64
69
  }
65
70
  case 'waterfall': {
66
71
  const isTotal = get(data, 'total', false);
67
- const subTotal = getWaterfallPointSubtotal(data, series);
72
+ const subTotalValue = getWaterfallPointSubtotal(data, series);
73
+ const subTotal = getFormattedValue({
74
+ value: subTotalValue,
75
+ format: valueFormat,
76
+ });
77
+ const formattedValue = getFormattedValue({
78
+ value: getYRowData(data, yAxis),
79
+ format: valueFormat,
80
+ });
68
81
  return (React.createElement("div", { key: `${id}_${get(data, 'x')}` },
69
82
  !isTotal && (React.createElement(React.Fragment, null,
70
83
  React.createElement("div", { key: id, className: b('content-row') },
@@ -73,17 +86,21 @@ export const DefaultContent = ({ hovered, xAxis, yAxis }) => {
73
86
  React.createElement("span", null,
74
87
  series.name,
75
88
  "\u00A0"),
76
- React.createElement("span", null, getYRowData(data, yAxis))))),
89
+ React.createElement("span", null, formattedValue)))),
77
90
  React.createElement("div", { key: id, className: b('content-row') },
78
91
  isTotal ? 'Total' : 'Subtotal',
79
92
  ": ",
80
93
  subTotal)));
81
94
  }
82
95
  case 'bar-y': {
96
+ const formattedValue = getFormattedValue({
97
+ value: getXRowData(data, xAxis),
98
+ format: valueFormat,
99
+ });
83
100
  const value = (React.createElement(React.Fragment, null,
84
101
  series.name,
85
102
  ": ",
86
- getXRowData(data, xAxis)));
103
+ formattedValue));
87
104
  return (React.createElement("div", { key: id, className: b('content-row') },
88
105
  React.createElement("div", { className: b('color'), style: { backgroundColor: color } }),
89
106
  React.createElement("div", null, closest ? React.createElement("b", null, value) : React.createElement("span", null, value))));
@@ -91,16 +108,24 @@ export const DefaultContent = ({ hovered, xAxis, yAxis }) => {
91
108
  case 'pie':
92
109
  case 'treemap': {
93
110
  const seriesData = data;
111
+ const formattedValue = getFormattedValue({
112
+ value: seriesData.value,
113
+ format: valueFormat,
114
+ });
94
115
  return (React.createElement("div", { key: id, className: b('content-row') },
95
116
  React.createElement("div", { className: b('color'), style: { backgroundColor: color } }),
96
117
  React.createElement("span", null,
97
118
  seriesData.name || seriesData.id,
98
119
  "\u00A0"),
99
- React.createElement("span", null, seriesData.value)));
120
+ React.createElement("span", null, formattedValue)));
100
121
  }
101
122
  case 'sankey': {
102
123
  const { target, data: source } = seriesItem;
103
124
  const value = (_a = source.links.find((d) => d.name === (target === null || target === void 0 ? void 0 : target.name))) === null || _a === void 0 ? void 0 : _a.value;
125
+ const formattedValue = getFormattedValue({
126
+ value,
127
+ format: valueFormat,
128
+ });
104
129
  return (React.createElement("div", { key: id, className: b('content-row') },
105
130
  React.createElement("div", { className: b('color'), style: { backgroundColor: source.color } }),
106
131
  React.createElement("div", { style: { display: 'flex', gap: 8, verticalAlign: 'center' } },
@@ -110,16 +135,20 @@ export const DefaultContent = ({ hovered, xAxis, yAxis }) => {
110
135
  " ", target === null || target === void 0 ? void 0 :
111
136
  target.name,
112
137
  ": ",
113
- value)));
138
+ formattedValue)));
114
139
  }
115
140
  case 'radar': {
116
141
  const radarSeries = series;
117
142
  const seriesData = data;
143
+ const formattedValue = getFormattedValue({
144
+ value: seriesData.value,
145
+ format: valueFormat,
146
+ });
118
147
  const value = (React.createElement(React.Fragment, null,
119
148
  React.createElement("span", null,
120
149
  radarSeries.name || radarSeries.id,
121
150
  "\u00A0"),
122
- React.createElement("span", null, seriesData.value)));
151
+ React.createElement("span", null, formattedValue)));
123
152
  return (React.createElement("div", { key: id, className: b('content-row') },
124
153
  React.createElement("div", { className: b('color'), style: { backgroundColor: color } }),
125
154
  React.createElement("div", null, closest ? React.createElement("b", null, value) : React.createElement("span", null, value))));
@@ -23,5 +23,5 @@ export const Tooltip = (props) => {
23
23
  }, [left, top]);
24
24
  return (hovered === null || hovered === void 0 ? void 0 : hovered.length) ? (React.createElement(Popup, { anchorElement: anchor, className: b({ pinned: tooltipPinned }), disableTransition: true, floatingStyles: tooltipPinned ? undefined : { pointerEvents: 'none' }, offset: { mainAxis: 20 }, onOpenChange: tooltipPinned ? handleOnOpenChange : undefined, open: true, placement: ['right', 'left', 'top', 'bottom'] },
25
25
  React.createElement("div", { className: b('content') },
26
- React.createElement(ChartTooltipContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis, renderer: tooltip.renderer })))) : null;
26
+ React.createElement(ChartTooltipContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis, renderer: tooltip.renderer, valueFormat: tooltip.valueFormat })))) : null;
27
27
  };
@@ -28,7 +28,7 @@ export function prepareArea(args) {
28
28
  const defaultAreaWidth = get(seriesOptions, 'area.lineWidth', DEFAULT_LINE_WIDTH);
29
29
  const defaultOpacity = get(seriesOptions, 'area.opacity', 0.75);
30
30
  return seriesList.map((series) => {
31
- var _a, _b;
31
+ var _a, _b, _c;
32
32
  const id = getUniqId();
33
33
  const name = series.name || '';
34
34
  const color = series.color || colorScale(name);
@@ -53,6 +53,7 @@ export function prepareArea(args) {
53
53
  padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING),
54
54
  allowOverlap: get(series, 'dataLabels.allowOverlap', false),
55
55
  html: get(series, 'dataLabels.html', false),
56
+ format: (_c = series.dataLabels) === null || _c === void 0 ? void 0 : _c.format,
56
57
  },
57
58
  marker: prepareMarker(series, seriesOptions),
58
59
  cursor: get(series, 'cursor', null),
@@ -5,7 +5,7 @@ import { getSeriesStackId, prepareLegendSymbol } from './utils';
5
5
  export function prepareBarXSeries(args) {
6
6
  const { colorScale, series: seriesList, seriesOptions, legend } = args;
7
7
  return seriesList.map((series) => {
8
- var _a, _b, _c, _d, _e;
8
+ var _a, _b, _c, _d, _e, _f;
9
9
  const name = series.name || '';
10
10
  const color = series.color || colorScale(name);
11
11
  return {
@@ -30,6 +30,7 @@ export function prepareBarXSeries(args) {
30
30
  allowOverlap: ((_e = series.dataLabels) === null || _e === void 0 ? void 0 : _e.allowOverlap) || false,
31
31
  padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING),
32
32
  html: get(series, 'dataLabels.html', false),
33
+ format: (_f = series.dataLabels) === null || _f === void 0 ? void 0 : _f.format,
33
34
  },
34
35
  cursor: get(series, 'cursor', null),
35
36
  yAxis: get(series, 'yAxis', 0),
@@ -1,13 +1,16 @@
1
1
  import get from 'lodash/get';
2
2
  import { getLabelsSize, getUniqId } from '../../utils';
3
+ import { getFormattedValue } from '../../utils/chart/format';
3
4
  import { DEFAULT_DATALABELS_STYLE } from './constants';
4
5
  import { getSeriesStackId, prepareLegendSymbol } from './utils';
5
6
  function prepareDataLabels(series) {
6
- var _a;
7
+ var _a, _b;
7
8
  const enabled = get(series, 'dataLabels.enabled', false);
8
9
  const style = Object.assign({}, DEFAULT_DATALABELS_STYLE, (_a = series.dataLabels) === null || _a === void 0 ? void 0 : _a.style);
9
10
  const html = get(series, 'dataLabels.html', false);
10
- const labels = enabled ? series.data.map((d) => String(d.label || d.x)) : [];
11
+ const labels = enabled
12
+ ? series.data.map((d) => getFormattedValue(Object.assign({ value: d.x || d.label }, series.dataLabels)))
13
+ : [];
11
14
  const { maxHeight = 0, maxWidth = 0 } = getLabelsSize({
12
15
  labels,
13
16
  style,
@@ -21,6 +24,7 @@ function prepareDataLabels(series) {
21
24
  maxHeight,
22
25
  maxWidth,
23
26
  html,
27
+ format: (_b = series.dataLabels) === null || _b === void 0 ? void 0 : _b.format,
24
28
  };
25
29
  }
26
30
  export function prepareBarYSeries(args) {
@@ -39,7 +43,7 @@ export function prepareBarYSeries(args) {
39
43
  enabled: get(series, 'legend.enabled', legend.enabled),
40
44
  symbol: prepareLegendSymbol(series),
41
45
  },
42
- data: series.data,
46
+ data: series.data.filter((d) => d.x !== null),
43
47
  stacking: series.stacking,
44
48
  stackId: getSeriesStackId(series),
45
49
  dataLabels: prepareDataLabels(series),
@@ -46,7 +46,7 @@ export function prepareLineSeries(args) {
46
46
  const defaultLineWidth = get(seriesOptions, 'line.lineWidth', DEFAULT_LINE_WIDTH);
47
47
  const defaultDashStyle = get(seriesOptions, 'line.dashStyle', DEFAULT_DASH_STYLE);
48
48
  return seriesList.map((series) => {
49
- var _a, _b;
49
+ var _a, _b, _c;
50
50
  const id = getUniqId();
51
51
  const name = series.name || '';
52
52
  const color = series.color || colorScale(name);
@@ -69,6 +69,7 @@ export function prepareLineSeries(args) {
69
69
  padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING),
70
70
  allowOverlap: get(series, 'dataLabels.allowOverlap', false),
71
71
  html: get(series, 'dataLabels.html', false),
72
+ format: (_c = series.dataLabels) === null || _c === void 0 ? void 0 : _c.format,
72
73
  },
73
74
  marker: prepareMarker(series, seriesOptions),
74
75
  dashStyle: dashStyle,
@@ -11,7 +11,7 @@ export function preparePieSeries(args) {
11
11
  const stackId = getUniqId();
12
12
  const seriesHoverState = get(seriesOptions, 'pie.states.hover');
13
13
  const preparedSeries = series.data.map((dataItem, i) => {
14
- var _a, _b, _c, _d, _e;
14
+ var _a, _b, _c, _d, _e, _f;
15
15
  const result = {
16
16
  type: 'pie',
17
17
  data: dataItem,
@@ -25,6 +25,7 @@ export function preparePieSeries(args) {
25
25
  distance: get(series, 'dataLabels.distance', 25),
26
26
  connectorCurve: get(series, 'dataLabels.connectorCurve', 'basic'),
27
27
  html: get(series, 'dataLabels.html', false),
28
+ format: (_b = series.dataLabels) === null || _b === void 0 ? void 0 : _b.format,
28
29
  },
29
30
  label: dataItem.label,
30
31
  value: dataItem.value,
@@ -38,9 +39,9 @@ export function preparePieSeries(args) {
38
39
  },
39
40
  center: series.center || ['50%', '50%'],
40
41
  borderColor: series.borderColor || '',
41
- borderRadius: (_b = series.borderRadius) !== null && _b !== void 0 ? _b : 0,
42
- borderWidth: (_c = series.borderWidth) !== null && _c !== void 0 ? _c : 1,
43
- radius: (_e = (_d = dataItem.radius) !== null && _d !== void 0 ? _d : series.radius) !== null && _e !== void 0 ? _e : '100%',
42
+ borderRadius: (_c = series.borderRadius) !== null && _c !== void 0 ? _c : 0,
43
+ borderWidth: (_d = series.borderWidth) !== null && _d !== void 0 ? _d : 1,
44
+ radius: (_f = (_e = dataItem.radius) !== null && _e !== void 0 ? _e : series.radius) !== null && _f !== void 0 ? _f : '100%',
44
45
  innerRadius: series.innerRadius || 0,
45
46
  stackId,
46
47
  states: {
@@ -30,7 +30,7 @@ export function prepareRadarSeries(args) {
30
30
  const colorScale = scaleOrdinal(radarSeries.map((s, index) => { var _a; return (_a = s.name) !== null && _a !== void 0 ? _a : `Series ${index + 1}`; }), DEFAULT_PALETTE);
31
31
  const categories = (_b = (_a = radarSeries.find((s) => s.categories)) === null || _a === void 0 ? void 0 : _a.categories) !== null && _b !== void 0 ? _b : [];
32
32
  return radarSeries.map((series, index) => {
33
- var _a, _b, _c, _d, _e;
33
+ var _a, _b, _c, _d, _e, _f;
34
34
  const name = (_a = series.name) !== null && _a !== void 0 ? _a : `Series ${index + 1}`;
35
35
  const color = (_b = series.color) !== null && _b !== void 0 ? _b : colorScale(name);
36
36
  const preparedSeries = {
@@ -54,6 +54,7 @@ export function prepareRadarSeries(args) {
54
54
  padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING),
55
55
  allowOverlap: get(series, 'dataLabels.allowOverlap', false),
56
56
  html: get(series, 'dataLabels.html', false),
57
+ format: (_f = series.dataLabels) === null || _f === void 0 ? void 0 : _f.format,
57
58
  },
58
59
  cursor: get(series, 'cursor', null),
59
60
  marker: prepareMarker(series, seriesOptions),
@@ -5,7 +5,7 @@ import { prepareLegendSymbol } from './utils';
5
5
  export function prepareSankeySeries(args) {
6
6
  const { colorScale, legend, series } = args;
7
7
  return series.map((s) => {
8
- var _a;
8
+ var _a, _b;
9
9
  const id = getUniqId();
10
10
  const name = s.name || '';
11
11
  const color = colorScale(name);
@@ -22,6 +22,7 @@ export function prepareSankeySeries(args) {
22
22
  dataLabels: {
23
23
  enabled: get(s, 'dataLabels.enabled', true),
24
24
  style: Object.assign({}, DEFAULT_DATALABELS_STYLE, (_a = s.dataLabels) === null || _a === void 0 ? void 0 : _a.style),
25
+ format: (_b = s.dataLabels) === null || _b === void 0 ? void 0 : _b.format,
25
26
  },
26
27
  id,
27
28
  type: s.type,
@@ -6,7 +6,7 @@ import { prepareLegendSymbol } from './utils';
6
6
  export function prepareTreemap(args) {
7
7
  const { colorScale, legend, series } = args;
8
8
  return series.map((s) => {
9
- var _a;
9
+ var _a, _b;
10
10
  const id = getUniqId();
11
11
  const name = s.name || '';
12
12
  const color = s.color || colorScale(name);
@@ -20,6 +20,7 @@ export function prepareTreemap(args) {
20
20
  allowOverlap: get(s, 'dataLabels.allowOverlap', false),
21
21
  html: get(s, 'dataLabels.html', false),
22
22
  align: get(s, 'dataLabels.align', 'left'),
23
+ format: (_b = s.dataLabels) === null || _b === void 0 ? void 0 : _b.format,
23
24
  },
24
25
  id,
25
26
  type: s.type,
@@ -7,7 +7,7 @@ export function prepareWaterfallSeries(args) {
7
7
  const { colorScale, series: seriesList, legend } = args;
8
8
  const [, negativeColor, positiveColor] = DEFAULT_PALETTE;
9
9
  return seriesList.map((series) => {
10
- var _a, _b, _c;
10
+ var _a, _b, _c, _d;
11
11
  const name = series.name || '';
12
12
  const color = series.color || colorScale(name);
13
13
  const prepared = {
@@ -29,6 +29,7 @@ export function prepareWaterfallSeries(args) {
29
29
  allowOverlap: ((_c = series.dataLabels) === null || _c === void 0 ? void 0 : _c.allowOverlap) || false,
30
30
  padding: get(series, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING),
31
31
  html: get(series, 'dataLabels.html', false),
32
+ format: (_d = series.dataLabels) === null || _d === void 0 ? void 0 : _d.format,
32
33
  },
33
34
  cursor: get(series, 'cursor', null),
34
35
  };
@@ -1,5 +1,5 @@
1
1
  import type { DashStyle, LayoutAlgorithm, LineCap, SeriesOptionsDefaults, SymbolType } from '../../constants';
2
- import type { AreaSeries, AreaSeriesData, BarXSeries, BarXSeriesData, BarYSeries, BarYSeriesData, BaseTextStyle, ChartLegend, ConnectorCurve, ConnectorShape, LineSeries, LineSeriesData, PathLegendSymbolOptions, PieSeries, PieSeriesData, RadarSeries, RadarSeriesCategory, RadarSeriesData, RectLegendSymbolOptions, SankeySeries, SankeySeriesData, ScatterSeries, ScatterSeriesData, SymbolLegendSymbolOptions, TreemapSeries, TreemapSeriesData, WaterfallSeries, WaterfallSeriesData } from '../../types';
2
+ import type { AreaSeries, AreaSeriesData, BarXSeries, BarXSeriesData, BarYSeries, BarYSeriesData, BaseTextStyle, ChartLegend, ConnectorCurve, ConnectorShape, LineSeries, LineSeriesData, PathLegendSymbolOptions, PieSeries, PieSeriesData, RadarSeries, RadarSeriesCategory, RadarSeriesData, RectLegendSymbolOptions, SankeySeries, SankeySeriesData, ScatterSeries, ScatterSeriesData, SymbolLegendSymbolOptions, TreemapSeries, TreemapSeriesData, ValueFormat, WaterfallSeries, WaterfallSeriesData } from '../../types';
3
3
  export type RectLegendSymbol = {
4
4
  shape: 'rect';
5
5
  } & Required<RectLegendSymbolOptions>;
@@ -106,6 +106,7 @@ export type PreparedBarXSeries = {
106
106
  allowOverlap: boolean;
107
107
  padding: number;
108
108
  html: boolean;
109
+ format?: ValueFormat;
109
110
  };
110
111
  borderRadius: number;
111
112
  yAxis: number;
@@ -122,6 +123,7 @@ export type PreparedBarYSeries = {
122
123
  maxHeight: number;
123
124
  maxWidth: number;
124
125
  html: boolean;
126
+ format?: ValueFormat;
125
127
  };
126
128
  borderRadius: number;
127
129
  } & BasePreparedSeries;
@@ -147,6 +149,7 @@ export type PreparedPieSeries = {
147
149
  distance: number;
148
150
  connectorCurve: ConnectorCurve;
149
151
  html: boolean;
152
+ format?: ValueFormat;
150
153
  };
151
154
  states: {
152
155
  hover: {
@@ -166,6 +169,7 @@ export type PreparedLineSeries = {
166
169
  padding: number;
167
170
  allowOverlap: boolean;
168
171
  html: boolean;
172
+ format?: ValueFormat;
169
173
  };
170
174
  marker: {
171
175
  states: {
@@ -203,6 +207,7 @@ export type PreparedAreaSeries = {
203
207
  padding: number;
204
208
  allowOverlap: boolean;
205
209
  html: boolean;
210
+ format?: ValueFormat;
206
211
  };
207
212
  marker: {
208
213
  states: {
@@ -234,6 +239,7 @@ export type PreparedTreemapSeries = {
234
239
  allowOverlap: boolean;
235
240
  html: boolean;
236
241
  align: Required<Required<TreemapSeries>['dataLabels']>['align'];
242
+ format?: ValueFormat;
237
243
  };
238
244
  layoutAlgorithm: `${LayoutAlgorithm}`;
239
245
  } & BasePreparedSeries & Omit<TreemapSeries, keyof BasePreparedSeries>;
@@ -246,6 +252,7 @@ export type PreparedWaterfallSeries = {
246
252
  allowOverlap: boolean;
247
253
  padding: number;
248
254
  html: boolean;
255
+ format?: ValueFormat;
249
256
  };
250
257
  positiveColor: string;
251
258
  negativeColor: string;
@@ -256,6 +263,7 @@ export type PreparedSankeySeries = {
256
263
  dataLabels: {
257
264
  enabled: boolean;
258
265
  style: BaseTextStyle;
266
+ format?: ValueFormat;
259
267
  };
260
268
  } & BasePreparedSeries & Omit<SankeySeries, keyof BasePreparedSeries>;
261
269
  export type PreparedRadarSeries = {
@@ -271,6 +279,7 @@ export type PreparedRadarSeries = {
271
279
  padding: number;
272
280
  allowOverlap: boolean;
273
281
  html: boolean;
282
+ format?: ValueFormat;
274
283
  };
275
284
  marker: {
276
285
  states: {
@@ -1,8 +1,9 @@
1
1
  import { group, sort } from 'd3';
2
2
  import { getDataCategoryValue, getLabelsSize, getLeftPosition } from '../../../utils';
3
+ import { getFormattedValue } from '../../../utils/chart/format';
3
4
  import { getXValue, getYValue } from '../utils';
4
5
  function getLabelData(point, series, xMax) {
5
- const text = String(point.data.label || point.data.y);
6
+ const text = getFormattedValue(Object.assign({ value: point.data.label || point.data.y }, series.dataLabels));
6
7
  const style = series.dataLabels.style;
7
8
  const size = getLabelsSize({ labels: [text], style, html: series.dataLabels.html });
8
9
  const labelData = {
@@ -1,12 +1,13 @@
1
1
  import { ascending, descending, max, sort } from 'd3';
2
2
  import get from 'lodash/get';
3
3
  import { getDataCategoryValue, getLabelsSize } from '../../../utils';
4
+ import { getFormattedValue } from '../../../utils/chart/format';
4
5
  import { MIN_BAR_GAP, MIN_BAR_GROUP_GAP, MIN_BAR_WIDTH } from '../constants';
5
6
  function getLabelData(d) {
6
7
  if (!d.series.dataLabels.enabled) {
7
8
  return undefined;
8
9
  }
9
- const text = String(d.data.label || d.data.y);
10
+ const text = getFormattedValue(Object.assign({ value: d.data.label || d.data.y }, d.series.dataLabels));
10
11
  const style = d.series.dataLabels.style;
11
12
  const html = d.series.dataLabels.html;
12
13
  const { maxHeight: height, maxWidth: width } = getLabelsSize({
@@ -1,6 +1,7 @@
1
1
  import { ascending, descending, max, sort } from 'd3';
2
2
  import get from 'lodash/get';
3
3
  import { getDataCategoryValue, getLabelsSize } from '../../../utils';
4
+ import { getFormattedValue } from '../../../utils/chart/format';
4
5
  import { MIN_BAR_GAP, MIN_BAR_GROUP_GAP, MIN_BAR_WIDTH } from '../constants';
5
6
  const DEFAULT_LABEL_PADDING = 7;
6
7
  function groupByYValue(series, yAxis) {
@@ -54,7 +55,7 @@ function setLabel(prepared) {
54
55
  return;
55
56
  }
56
57
  const data = prepared.data;
57
- const content = String(data.label || data.x);
58
+ const content = getFormattedValue(Object.assign({ value: data.label || data.x }, dataLabels));
58
59
  const { maxHeight: height, maxWidth: width } = getLabelsSize({
59
60
  labels: [content],
60
61
  style: dataLabels.style,
@@ -1,7 +1,8 @@
1
1
  import { getLabelsSize, getLeftPosition } from '../../../utils';
2
+ import { getFormattedValue } from '../../../utils/chart/format';
2
3
  import { getXValue, getYValue } from '../utils';
3
4
  function getLabelData(point, series, xMax) {
4
- const text = String(point.data.label || point.data.y);
5
+ const text = getFormattedValue(Object.assign({ value: point.data.label || point.data.y }, series.dataLabels));
5
6
  const style = series.dataLabels.style;
6
7
  const size = getLabelsSize({ labels: [text], style });
7
8
  const labelData = {
@@ -1,5 +1,6 @@
1
1
  import { arc, group, line as lineGenerator } from 'd3';
2
2
  import { calculateNumericProperty, getLabelsSize, getLeftPosition, isLabelsOverlapping, } from '../../../utils';
3
+ import { getFormattedValue } from '../../../utils/chart/format';
3
4
  import { getCurveFactory, pieGenerator } from './utils';
4
5
  const FULL_CIRCLE = Math.PI * 2;
5
6
  const getCenter = (boundsWidth, boundsHeight, center) => {
@@ -100,7 +101,7 @@ export function preparePieData(args) {
100
101
  .outerRadius((d) => d.data.radius + distance + connectorPadding);
101
102
  series.forEach((d, index) => {
102
103
  const prevLabel = labels[labels.length - 1];
103
- const text = String(d.data.label || d.data.value);
104
+ const text = getFormattedValue(Object.assign({ value: d.data.label || d.data.value }, d.dataLabels));
104
105
  const shouldUseHtml = dataLabels.html;
105
106
  const labelSize = getLabelsSize({ labels: [text], style, html: shouldUseHtml });
106
107
  const labelWidth = labelSize.maxWidth;
@@ -1,5 +1,6 @@
1
1
  import { curveLinearClosed, line, range, scaleLinear } from 'd3';
2
2
  import { getLabelsSize } from '../../../utils';
3
+ import { getFormattedValue } from '../../../utils/chart/format';
3
4
  export function prepareRadarData(args) {
4
5
  const { series: preparedSeries, boundsWidth, boundsHeight } = args;
5
6
  const maxRadius = Math.min(boundsWidth, boundsHeight) / 2;
@@ -112,7 +113,7 @@ export function prepareRadarData(args) {
112
113
  const { style } = dataLabels;
113
114
  const shouldUseHtml = dataLabels.html;
114
115
  data.labels = categories.map((category, index) => {
115
- const text = category.key;
116
+ const text = getFormattedValue(Object.assign({ value: category.key }, dataLabels));
116
117
  const labelSize = getLabelsSize({ labels: [text], style });
117
118
  const angle = index * angleStep - Math.PI / 2;
118
119
  // Position label slightly outside the point
@@ -1,4 +1,5 @@
1
1
  import { sankey, sankeyLinkHorizontal } from 'd3-sankey';
2
+ import { getFormattedValue } from '../../../utils/chart/format';
2
3
  export function prepareSankeyData(args) {
3
4
  const { series, width, height } = args;
4
5
  const htmlElements = [];
@@ -58,8 +59,9 @@ export function prepareSankeyData(args) {
58
59
  const x1 = (_b = d.x1) !== null && _b !== void 0 ? _b : 0;
59
60
  const y0 = (_c = d.y0) !== null && _c !== void 0 ? _c : 0;
60
61
  const y1 = (_d = d.y1) !== null && _d !== void 0 ? _d : 0;
62
+ const text = getFormattedValue(Object.assign({ value: d.name }, dataLabels));
61
63
  return {
62
- text: d.name,
64
+ text,
63
65
  x: x0 < width / 2 ? x1 + 6 : x0 - 6,
64
66
  y: (y1 + y0) / 2,
65
67
  textAnchor: x0 < width / 2 ? 'start' : 'end',
@@ -1,14 +1,16 @@
1
1
  import { stratify, treemap, treemapBinary, treemapDice, treemapSlice, treemapSliceDice, treemapSquarify, } from 'd3';
2
2
  import { LayoutAlgorithm } from '../../../constants';
3
3
  import { getLabelsSize } from '../../../utils';
4
+ import { getFormattedValue } from '../../../utils/chart/format';
4
5
  const DEFAULT_PADDING = 1;
5
6
  function getLabels(args) {
6
- const { data, html, padding, align } = args;
7
+ const { data, options: { html, padding, align }, } = args;
7
8
  return data.reduce((acc, d) => {
8
9
  const texts = Array.isArray(d.data.name) ? d.data.name : [d.data.name];
9
10
  texts.forEach((text, index) => {
10
11
  var _a;
11
- const { maxHeight: lineHeight, maxWidth: labelWidth } = (_a = getLabelsSize({ labels: [text], html })) !== null && _a !== void 0 ? _a : {};
12
+ const label = getFormattedValue(Object.assign({ value: text }, args.options));
13
+ const { maxHeight: lineHeight, maxWidth: labelWidth } = (_a = getLabelsSize({ labels: [label], html })) !== null && _a !== void 0 ? _a : {};
12
14
  const left = d.x0 + padding;
13
15
  const right = d.x1 - padding;
14
16
  const width = Math.max(0, right - left);
@@ -30,13 +32,13 @@ function getLabels(args) {
30
32
  }
31
33
  const item = html
32
34
  ? {
33
- content: text,
35
+ content: label,
34
36
  x,
35
37
  y,
36
38
  size: { width, height: lineHeight },
37
39
  }
38
40
  : {
39
- text,
41
+ text: label,
40
42
  x,
41
43
  y,
42
44
  width,
@@ -92,8 +94,8 @@ export function prepareTreemapData(args) {
92
94
  let labelData = [];
93
95
  const htmlElements = [];
94
96
  if ((_a = series.dataLabels) === null || _a === void 0 ? void 0 : _a.enabled) {
95
- const { html, padding, align } = series.dataLabels;
96
- const labels = getLabels({ html, padding, align, data: leaves });
97
+ const { html } = series.dataLabels;
98
+ const labels = getLabels({ data: leaves, options: series.dataLabels });
97
99
  if (html) {
98
100
  htmlElements.push(...labels);
99
101
  }
@@ -1,13 +1,14 @@
1
1
  import get from 'lodash/get';
2
2
  import sortBy from 'lodash/sortBy';
3
3
  import { getLabelsSize } from '../../../utils';
4
+ import { getFormattedValue } from '../../../utils/chart/format';
4
5
  import { MIN_BAR_GAP, MIN_BAR_WIDTH } from '../constants';
5
6
  import { getXValue, getYValue } from '../utils';
6
7
  function getLabelData(d, plotHeight) {
7
8
  if (!d.series.dataLabels.enabled) {
8
9
  return undefined;
9
10
  }
10
- const text = String(d.data.label || d.subTotal);
11
+ const text = getFormattedValue(Object.assign({ value: d.data.label || d.subTotal }, d.series.dataLabels));
11
12
  const style = d.series.dataLabels.style;
12
13
  const { maxHeight: height, maxWidth: width } = getLabelsSize({ labels: [text], style });
13
14
  let y;