@gravity-ui/charts 1.11.4 → 1.12.0

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 (85) hide show
  1. package/dist/cjs/components/ChartInner/useChartInnerProps.js +12 -6
  2. package/dist/cjs/components/Tooltip/ChartTooltipContent.d.ts +1 -0
  3. package/dist/cjs/components/Tooltip/ChartTooltipContent.js +3 -3
  4. package/dist/cjs/components/Tooltip/DefaultTooltipContent/Row.d.ts +9 -0
  5. package/dist/cjs/components/Tooltip/DefaultTooltipContent/Row.js +10 -0
  6. package/dist/cjs/components/Tooltip/DefaultTooltipContent/RowTotals.d.ts +9 -0
  7. package/dist/cjs/components/Tooltip/DefaultTooltipContent/RowTotals.js +23 -0
  8. package/dist/cjs/components/Tooltip/DefaultTooltipContent/index.d.ts +11 -0
  9. package/dist/cjs/components/Tooltip/DefaultTooltipContent/index.js +102 -0
  10. package/dist/cjs/components/Tooltip/DefaultTooltipContent/utils.d.ts +30 -0
  11. package/dist/cjs/components/Tooltip/DefaultTooltipContent/utils.js +126 -0
  12. package/dist/cjs/components/Tooltip/index.js +1 -1
  13. package/dist/cjs/components/Tooltip/styles.css +14 -2
  14. package/dist/cjs/components/Tooltip/utils.d.ts +30 -0
  15. package/dist/cjs/components/Tooltip/utils.js +126 -0
  16. package/dist/cjs/constants/axis.d.ts +6 -0
  17. package/dist/cjs/constants/axis.js +6 -0
  18. package/dist/cjs/constants/index.d.ts +6 -4
  19. package/dist/cjs/constants/index.js +6 -4
  20. package/dist/cjs/constants/tooltip.d.ts +3 -0
  21. package/dist/cjs/constants/tooltip.js +3 -0
  22. package/dist/cjs/hooks/useAxisScales/index.d.ts +14 -3
  23. package/dist/cjs/hooks/useAxisScales/index.js +73 -22
  24. package/dist/cjs/hooks/useChartOptions/x-axis.js +1 -1
  25. package/dist/cjs/hooks/useChartOptions/y-axis.d.ts +4 -2
  26. package/dist/cjs/hooks/useChartOptions/y-axis.js +9 -3
  27. package/dist/cjs/hooks/useShapes/bar-x/prepare-data.js +1 -1
  28. package/dist/cjs/hooks/useShapes/bar-y/prepare-data.js +17 -62
  29. package/dist/cjs/hooks/useShapes/waterfall/prepare-data.js +1 -1
  30. package/dist/cjs/hooks/utils/bar-y.d.ts +27 -0
  31. package/dist/cjs/hooks/utils/bar-y.js +69 -0
  32. package/dist/cjs/hooks/utils/index.d.ts +1 -0
  33. package/dist/cjs/hooks/utils/index.js +1 -0
  34. package/dist/cjs/i18n/keysets/en.json +7 -1
  35. package/dist/cjs/i18n/keysets/ru.json +7 -1
  36. package/dist/cjs/types/chart/axis.d.ts +2 -2
  37. package/dist/cjs/types/chart/tooltip.d.ts +21 -0
  38. package/dist/cjs/validation/index.js +55 -1
  39. package/dist/esm/components/ChartInner/useChartInnerProps.js +12 -6
  40. package/dist/esm/components/Tooltip/ChartTooltipContent.d.ts +1 -0
  41. package/dist/esm/components/Tooltip/ChartTooltipContent.js +3 -3
  42. package/dist/esm/components/Tooltip/DefaultTooltipContent/Row.d.ts +9 -0
  43. package/dist/esm/components/Tooltip/DefaultTooltipContent/Row.js +10 -0
  44. package/dist/esm/components/Tooltip/DefaultTooltipContent/RowTotals.d.ts +9 -0
  45. package/dist/esm/components/Tooltip/DefaultTooltipContent/RowTotals.js +23 -0
  46. package/dist/esm/components/Tooltip/DefaultTooltipContent/index.d.ts +11 -0
  47. package/dist/esm/components/Tooltip/DefaultTooltipContent/index.js +102 -0
  48. package/dist/esm/components/Tooltip/DefaultTooltipContent/utils.d.ts +30 -0
  49. package/dist/esm/components/Tooltip/DefaultTooltipContent/utils.js +126 -0
  50. package/dist/esm/components/Tooltip/index.js +1 -1
  51. package/dist/esm/components/Tooltip/styles.css +14 -2
  52. package/dist/esm/components/Tooltip/utils.d.ts +30 -0
  53. package/dist/esm/components/Tooltip/utils.js +126 -0
  54. package/dist/esm/constants/axis.d.ts +6 -0
  55. package/dist/esm/constants/axis.js +6 -0
  56. package/dist/esm/constants/index.d.ts +6 -4
  57. package/dist/esm/constants/index.js +6 -4
  58. package/dist/esm/constants/tooltip.d.ts +3 -0
  59. package/dist/esm/constants/tooltip.js +3 -0
  60. package/dist/esm/hooks/useAxisScales/index.d.ts +14 -3
  61. package/dist/esm/hooks/useAxisScales/index.js +73 -22
  62. package/dist/esm/hooks/useChartOptions/x-axis.js +1 -1
  63. package/dist/esm/hooks/useChartOptions/y-axis.d.ts +4 -2
  64. package/dist/esm/hooks/useChartOptions/y-axis.js +9 -3
  65. package/dist/esm/hooks/useShapes/bar-x/prepare-data.js +1 -1
  66. package/dist/esm/hooks/useShapes/bar-y/prepare-data.js +17 -62
  67. package/dist/esm/hooks/useShapes/waterfall/prepare-data.js +1 -1
  68. package/dist/esm/hooks/utils/bar-y.d.ts +27 -0
  69. package/dist/esm/hooks/utils/bar-y.js +69 -0
  70. package/dist/esm/hooks/utils/index.d.ts +1 -0
  71. package/dist/esm/hooks/utils/index.js +1 -0
  72. package/dist/esm/i18n/keysets/en.json +7 -1
  73. package/dist/esm/i18n/keysets/ru.json +7 -1
  74. package/dist/esm/types/chart/axis.d.ts +2 -2
  75. package/dist/esm/types/chart/tooltip.d.ts +21 -0
  76. package/dist/esm/validation/index.js +55 -1
  77. package/package.json +1 -1
  78. package/dist/cjs/components/Tooltip/DefaultContent.d.ts +0 -10
  79. package/dist/cjs/components/Tooltip/DefaultContent.js +0 -187
  80. package/dist/esm/components/Tooltip/DefaultContent.d.ts +0 -10
  81. package/dist/esm/components/Tooltip/DefaultContent.js +0 -187
  82. /package/dist/cjs/hooks/{useShapes/constants.d.ts → constants.d.ts} +0 -0
  83. /package/dist/cjs/hooks/{useShapes/constants.js → constants.js} +0 -0
  84. /package/dist/esm/hooks/{useShapes/constants.d.ts → constants.d.ts} +0 -0
  85. /package/dist/esm/hooks/{useShapes/constants.js → constants.js} +0 -0
@@ -21,6 +21,9 @@ export function useChartInnerProps(props) {
21
21
  title: data.title,
22
22
  tooltip: data.tooltip,
23
23
  });
24
+ const preparedSeriesOptions = React.useMemo(() => {
25
+ return getPreparedOptions(data.series.options);
26
+ }, [data.series.options]);
24
27
  const [zoomState, setZoomState] = React.useState({});
25
28
  const sortedSeriesData = React.useMemo(() => {
26
29
  return getSortedSeriesData(data.series.data);
@@ -41,11 +44,13 @@ export function useChartInnerProps(props) {
41
44
  const [yAxis, setYAxis] = React.useState([]);
42
45
  React.useEffect(() => {
43
46
  setYAxis([]);
44
- getPreparedYAxis({ yAxis: data.yAxis, height, seriesData: zoomedSeriesData }).then((val) => setYAxis(val));
45
- }, [data.yAxis, height, zoomedSeriesData]);
46
- const preparedSeriesOptions = React.useMemo(() => {
47
- return getPreparedOptions(data.series.options);
48
- }, [data.series.options]);
47
+ getPreparedYAxis({
48
+ height,
49
+ seriesData: zoomedSeriesData,
50
+ seriesOptions: preparedSeriesOptions,
51
+ yAxis: data.yAxis,
52
+ }).then((val) => setYAxis(val));
53
+ }, [data.yAxis, height, preparedSeriesOptions, zoomedSeriesData]);
49
54
  const { preparedSeries, preparedLegend, handleLegendItemClick } = useSeries({
50
55
  colors,
51
56
  legend: data.legend,
@@ -89,9 +94,10 @@ export function useChartInnerProps(props) {
89
94
  hasZoomX: Boolean(zoomState.x),
90
95
  hasZoomY: Boolean(zoomState.y),
91
96
  series: preparedSeries,
97
+ seriesOptions: preparedSeriesOptions,
98
+ split: preparedSplit,
92
99
  xAxis,
93
100
  yAxis,
94
- split: preparedSplit,
95
101
  });
96
102
  const isOutsideBounds = React.useCallback((x, y) => {
97
103
  return x < 0 || x > boundsWidth || y < 0 || y > boundsHeight;
@@ -6,5 +6,6 @@ export interface ChartTooltipContentProps {
6
6
  yAxis?: ChartYAxis;
7
7
  renderer?: ChartTooltip['renderer'];
8
8
  valueFormat?: ChartTooltip['valueFormat'];
9
+ totals?: ChartTooltip['totals'];
9
10
  }
10
11
  export declare const ChartTooltipContent: (props: ChartTooltipContentProps) => React.JSX.Element | null;
@@ -1,11 +1,11 @@
1
1
  import React from 'react';
2
2
  import isNil from 'lodash/isNil';
3
- import { DefaultContent } from './DefaultContent';
3
+ import { DefaultTooltipContent } from './DefaultTooltipContent';
4
4
  export const ChartTooltipContent = (props) => {
5
- const { hovered, xAxis, yAxis, renderer, valueFormat } = props;
5
+ const { hovered, xAxis, yAxis, renderer, valueFormat, totals } = 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, valueFormat: valueFormat })) : (customTooltip);
10
+ return isNil(customTooltip) ? (React.createElement(DefaultTooltipContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis, valueFormat: valueFormat, totals: totals })) : (customTooltip);
11
11
  };
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ export declare function Row(props: {
3
+ label: React.ReactNode;
4
+ value: React.ReactNode;
5
+ active?: boolean;
6
+ className?: string;
7
+ color?: string;
8
+ striped?: boolean;
9
+ }): React.JSX.Element;
@@ -0,0 +1,10 @@
1
+ import React from 'react';
2
+ import { block } from '../../../utils';
3
+ const b = block('tooltip');
4
+ export function Row(props) {
5
+ const { label, value, active, color, className, striped } = props;
6
+ return (React.createElement("div", { className: b('content-row', { active, striped }, className) },
7
+ color && React.createElement("div", { className: b('content-row-color'), style: { backgroundColor: color } }),
8
+ label,
9
+ React.createElement("span", { className: b('content-row-value') }, value)));
10
+ }
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+ import type { ChartTooltipTotalsAggregationValue, ChartTooltipTotalsBuiltInAggregation, ValueFormat } from '../../../types';
3
+ import type { HoveredValue } from './utils';
4
+ export declare function RowTotals(props: {
5
+ aggregation: ChartTooltipTotalsBuiltInAggregation | (() => ChartTooltipTotalsAggregationValue);
6
+ values: HoveredValue[];
7
+ label?: string;
8
+ valueFormat?: ValueFormat;
9
+ }): React.JSX.Element;
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ import { block } from '../../../utils';
3
+ import { getFormattedValue } from '../../../utils/chart/format';
4
+ import { Row } from './Row';
5
+ import { getBuiltInAggregatedValue, getBuiltInAggregationLabel } from './utils';
6
+ const b = block('tooltip');
7
+ export function RowTotals(props) {
8
+ const { aggregation, label, valueFormat, values } = props;
9
+ let resultLabel = label;
10
+ if (!resultLabel && typeof aggregation === 'string') {
11
+ resultLabel = getBuiltInAggregationLabel({ aggregation });
12
+ }
13
+ const resultValue = typeof aggregation === 'function'
14
+ ? aggregation()
15
+ : getBuiltInAggregatedValue({ aggregation, values });
16
+ const formattedResultValue = typeof resultValue === 'number'
17
+ ? getFormattedValue({
18
+ value: resultValue,
19
+ format: valueFormat || { type: 'number' },
20
+ })
21
+ : resultValue;
22
+ return (React.createElement(Row, { className: b('content-row-totals'), label: resultLabel, value: formattedResultValue }));
23
+ }
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ import type { ChartTooltip, ChartXAxis, ChartYAxis, TooltipDataChunk, ValueFormat } from '../../../types';
3
+ type Props = {
4
+ hovered: TooltipDataChunk[];
5
+ totals?: ChartTooltip['totals'];
6
+ valueFormat?: ValueFormat;
7
+ xAxis?: ChartXAxis | null;
8
+ yAxis?: ChartYAxis;
9
+ };
10
+ export declare const DefaultTooltipContent: ({ hovered, xAxis, yAxis, valueFormat, totals }: Props) => React.JSX.Element;
11
+ export {};
@@ -0,0 +1,102 @@
1
+ import React from 'react';
2
+ import { Divider } from '@gravity-ui/uikit';
3
+ import get from 'lodash/get';
4
+ import { block } from '../../../utils';
5
+ import { getFormattedValue } from '../../../utils/chart/format';
6
+ import { Row } from './Row';
7
+ import { RowTotals } from './RowTotals';
8
+ import { getDefaultValueFormat, getHoveredValues, getMeasureValue, getPreparedAggregation, getXRowData, } from './utils';
9
+ const b = block('tooltip');
10
+ export const DefaultTooltipContent = ({ hovered, xAxis, yAxis, valueFormat, totals }) => {
11
+ const measureValue = getMeasureValue({ data: hovered, xAxis, yAxis });
12
+ const hoveredValues = getHoveredValues({ hovered, xAxis, yAxis });
13
+ return (React.createElement("div", { className: b('content') },
14
+ measureValue && React.createElement("div", { className: b('series-name') }, measureValue),
15
+ // eslint-disable-next-line complexity
16
+ hovered.map((seriesItem, i) => {
17
+ var _a;
18
+ const { data, series, closest } = seriesItem;
19
+ const id = `${get(series, 'id')}_${i}`;
20
+ const color = get(series, 'color');
21
+ const active = closest && hovered.length > 1;
22
+ const striped = (i + 1) % 2 === 0;
23
+ switch (series.type) {
24
+ case 'scatter':
25
+ case 'line':
26
+ case 'area':
27
+ case 'bar-x': {
28
+ const format = valueFormat || getDefaultValueFormat({ axis: yAxis });
29
+ const formattedValue = getFormattedValue({
30
+ value: hoveredValues[i],
31
+ format,
32
+ });
33
+ return (React.createElement(Row, { key: id, active: active, color: color, label: series.name, striped: striped, value: formattedValue }));
34
+ }
35
+ case 'waterfall': {
36
+ const isTotal = get(data, 'total', false);
37
+ const subTotalValue = (_a = seriesItem.subTotal) !== null && _a !== void 0 ? _a : 0;
38
+ const format = valueFormat || getDefaultValueFormat({ axis: yAxis });
39
+ const subTotal = getFormattedValue({
40
+ value: subTotalValue,
41
+ format,
42
+ });
43
+ const formattedValue = getFormattedValue({
44
+ value: hoveredValues[i],
45
+ format,
46
+ });
47
+ return (React.createElement(React.Fragment, { key: id },
48
+ !isTotal && (React.createElement(React.Fragment, null,
49
+ React.createElement("div", { className: b('series-name') }, getXRowData(data, xAxis)),
50
+ React.createElement(Row, { label: series.name, value: formattedValue }))),
51
+ React.createElement(Row, { label: isTotal ? 'Total' : 'Subtotal', value: subTotal })));
52
+ }
53
+ case 'bar-y': {
54
+ const format = valueFormat || getDefaultValueFormat({ axis: xAxis });
55
+ const formattedValue = getFormattedValue({
56
+ value: hoveredValues[i],
57
+ format,
58
+ });
59
+ return (React.createElement(Row, { key: id, active: active, color: color, label: series.name, striped: striped, value: formattedValue }));
60
+ }
61
+ case 'pie':
62
+ case 'treemap': {
63
+ const seriesData = data;
64
+ const formattedValue = getFormattedValue({
65
+ value: hoveredValues[i],
66
+ format: valueFormat || { type: 'number' },
67
+ });
68
+ return (React.createElement(Row, { key: id, color: color, label: React.createElement("span", { dangerouslySetInnerHTML: {
69
+ __html: [seriesData.name || seriesData.id]
70
+ .flat()
71
+ .join('\n'),
72
+ } }), value: formattedValue }));
73
+ }
74
+ case 'sankey': {
75
+ const { target, data: source } = seriesItem;
76
+ const formattedValue = getFormattedValue({
77
+ value: hoveredValues[i],
78
+ format: valueFormat || { type: 'number' },
79
+ });
80
+ return (React.createElement(Row, { key: id, color: source.color, label: React.createElement("span", null,
81
+ source.name,
82
+ " \u2192 ", target === null || target === void 0 ? void 0 :
83
+ target.name,
84
+ ":"), value: formattedValue }));
85
+ }
86
+ case 'radar': {
87
+ const radarSeries = series;
88
+ const formattedValue = getFormattedValue({
89
+ value: hoveredValues[i],
90
+ format: valueFormat || { type: 'number' },
91
+ });
92
+ return (React.createElement(Row, { key: id, active: active, color: color, label: radarSeries.name || radarSeries.id, value: formattedValue }));
93
+ }
94
+ default: {
95
+ return null;
96
+ }
97
+ }
98
+ }),
99
+ (totals === null || totals === void 0 ? void 0 : totals.enabled) && hovered.length > 1 && (React.createElement(React.Fragment, null,
100
+ React.createElement(Divider, { className: b('content-row-totals-divider') }),
101
+ React.createElement(RowTotals, { aggregation: getPreparedAggregation({ hovered, totals, xAxis, yAxis }), label: totals.label, values: hoveredValues, valueFormat: valueFormat })))));
102
+ };
@@ -0,0 +1,30 @@
1
+ import type { ChartSeriesData, ChartTooltip, ChartTooltipTotalsAggregationValue, ChartTooltipTotalsBuiltInAggregation, ChartXAxis, ChartYAxis, TooltipDataChunk, ValueFormat } from '../../../types';
2
+ export type HoveredValue = string | number | null | undefined;
3
+ export declare function getXRowData(data: ChartSeriesData, xAxis?: ChartXAxis | null): string | number | undefined;
4
+ export declare function getDefaultValueFormat({ axis, }: {
5
+ axis?: ChartXAxis | ChartYAxis | null;
6
+ }): ValueFormat | undefined;
7
+ export declare const getMeasureValue: ({ data, xAxis, yAxis, valueFormat, }: {
8
+ data: TooltipDataChunk[];
9
+ xAxis?: ChartXAxis | null;
10
+ yAxis?: ChartYAxis;
11
+ valueFormat?: ValueFormat;
12
+ }) => string | null;
13
+ export declare function getHoveredValues(args: {
14
+ hovered: TooltipDataChunk[];
15
+ xAxis?: ChartXAxis | null;
16
+ yAxis?: ChartYAxis;
17
+ }): HoveredValue[];
18
+ export declare function getBuiltInAggregatedValue(args: {
19
+ aggregation: ChartTooltipTotalsBuiltInAggregation;
20
+ values: HoveredValue[];
21
+ }): number | undefined;
22
+ export declare function getBuiltInAggregationLabel(args: {
23
+ aggregation: ChartTooltipTotalsBuiltInAggregation;
24
+ }): string;
25
+ export declare function getPreparedAggregation(args: {
26
+ hovered: TooltipDataChunk[];
27
+ totals?: ChartTooltip['totals'];
28
+ xAxis?: ChartXAxis | null;
29
+ yAxis?: ChartYAxis;
30
+ }): ChartTooltipTotalsBuiltInAggregation | (() => ChartTooltipTotalsAggregationValue);
@@ -0,0 +1,126 @@
1
+ import get from 'lodash/get';
2
+ import { i18n } from '../../../i18n';
3
+ import { getDataCategoryValue } from '../../../utils';
4
+ import { getFormattedValue } from '../../../utils/chart/format';
5
+ const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
6
+ function getRowData(fieldName, data, axis) {
7
+ switch (axis === null || axis === void 0 ? void 0 : axis.type) {
8
+ case 'category': {
9
+ const categories = get(axis, 'categories', []);
10
+ return getDataCategoryValue({ axisDirection: fieldName, categories, data });
11
+ }
12
+ default: {
13
+ return get(data, fieldName);
14
+ }
15
+ }
16
+ }
17
+ export function getXRowData(data, xAxis) {
18
+ return getRowData('x', data, xAxis);
19
+ }
20
+ function getYRowData(data, yAxis) {
21
+ return getRowData('y', data, yAxis);
22
+ }
23
+ export function getDefaultValueFormat({ axis, }) {
24
+ switch (axis === null || axis === void 0 ? void 0 : axis.type) {
25
+ case 'linear':
26
+ case 'logarithmic': {
27
+ return {
28
+ type: 'number',
29
+ };
30
+ }
31
+ case 'datetime': {
32
+ return {
33
+ type: 'date',
34
+ format: DEFAULT_DATE_FORMAT,
35
+ };
36
+ }
37
+ default:
38
+ return undefined;
39
+ }
40
+ }
41
+ export const getMeasureValue = ({ data, xAxis, yAxis, valueFormat, }) => {
42
+ var _a, _b, _c, _d;
43
+ if (data.every((item) => ['pie', 'treemap', 'waterfall', 'sankey'].includes(item.series.type))) {
44
+ return null;
45
+ }
46
+ if (data.some((item) => item.series.type === 'radar')) {
47
+ return (_b = (_a = data[0].category) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : null;
48
+ }
49
+ if (data.some((item) => item.series.type === 'bar-y')) {
50
+ const format = valueFormat !== null && valueFormat !== void 0 ? valueFormat : getDefaultValueFormat({ axis: yAxis });
51
+ return getFormattedValue({
52
+ value: getYRowData((_c = data[0]) === null || _c === void 0 ? void 0 : _c.data, yAxis),
53
+ format,
54
+ });
55
+ }
56
+ const format = valueFormat !== null && valueFormat !== void 0 ? valueFormat : getDefaultValueFormat({ axis: xAxis });
57
+ return getFormattedValue({
58
+ value: getXRowData((_d = data[0]) === null || _d === void 0 ? void 0 : _d.data, xAxis),
59
+ format,
60
+ });
61
+ };
62
+ export function getHoveredValues(args) {
63
+ const { hovered, xAxis, yAxis } = args;
64
+ return hovered.map((seriesItem) => {
65
+ var _a;
66
+ const { data, series } = seriesItem;
67
+ switch (series.type) {
68
+ case 'area':
69
+ case 'line':
70
+ case 'bar-x':
71
+ case 'scatter': {
72
+ return getYRowData(data, yAxis);
73
+ }
74
+ case 'bar-y': {
75
+ return getXRowData(data, xAxis);
76
+ }
77
+ case 'pie':
78
+ case 'radar':
79
+ case 'treemap': {
80
+ const seriesData = data;
81
+ return seriesData.value;
82
+ }
83
+ case 'sankey': {
84
+ const { target, data: source } = seriesItem;
85
+ return (_a = source.links.find((d) => d.name === (target === null || target === void 0 ? void 0 : target.name))) === null || _a === void 0 ? void 0 : _a.value;
86
+ }
87
+ case 'waterfall': {
88
+ return getYRowData(data, yAxis);
89
+ }
90
+ default: {
91
+ return undefined;
92
+ }
93
+ }
94
+ });
95
+ }
96
+ export function getBuiltInAggregatedValue(args) {
97
+ const { aggregation, values } = args;
98
+ switch (aggregation) {
99
+ case 'sum':
100
+ return values.reduce((acc, value) => {
101
+ return acc + (typeof value === 'number' ? value : 0);
102
+ }, 0);
103
+ default:
104
+ return undefined;
105
+ }
106
+ }
107
+ export function getBuiltInAggregationLabel(args) {
108
+ const { aggregation } = args;
109
+ switch (aggregation) {
110
+ case 'sum':
111
+ return i18n('tooltip', 'label_totals_sum');
112
+ default:
113
+ return '';
114
+ }
115
+ }
116
+ export function getPreparedAggregation(args) {
117
+ const { hovered, totals, xAxis, yAxis } = args;
118
+ const aggregation = totals === null || totals === void 0 ? void 0 : totals.aggregation;
119
+ if (typeof aggregation === 'string') {
120
+ return aggregation;
121
+ }
122
+ if (typeof aggregation === 'function') {
123
+ return () => aggregation({ hovered, xAxis, yAxis });
124
+ }
125
+ return 'sum';
126
+ }
@@ -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('popup-content') },
26
- React.createElement(ChartTooltipContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis, renderer: tooltip.renderer, valueFormat: tooltip.valueFormat })))) : null;
26
+ React.createElement(ChartTooltipContent, { hovered: hovered, xAxis: xAxis, yAxis: yAxis, renderer: tooltip.renderer, valueFormat: tooltip.valueFormat, totals: tooltip.totals })))) : null;
27
27
  };
@@ -17,18 +17,30 @@
17
17
  .gcharts-tooltip__content-row {
18
18
  display: flex;
19
19
  align-items: center;
20
+ column-gap: 8px;
20
21
  padding: 2px 14px;
21
22
  font-size: 12px;
22
23
  }
24
+ .gcharts-tooltip__content-row_striped {
25
+ background-color: var(--g-color-base-generic);
26
+ }
23
27
  .gcharts-tooltip__content-row_active {
24
28
  font-weight: 600;
25
29
  background-color: var(--g-color-base-info-medium);
26
30
  }
27
- .gcharts-tooltip__color {
31
+ .gcharts-tooltip__content-row-color {
28
32
  display: inline-block;
29
33
  width: 16px;
30
34
  height: 8px;
31
- margin-inline-end: 8px;
32
35
  border-radius: 2px;
33
36
  background-color: #dddddd;
37
+ }
38
+ .gcharts-tooltip__content-row-value {
39
+ margin-inline-start: auto;
40
+ }
41
+ .gcharts-tooltip__content-row-totals {
42
+ color: var(--g-color-text-complementary);
43
+ }
44
+ .gcharts-tooltip__content-row-totals-divider {
45
+ margin-block: 5px 5px;
34
46
  }
@@ -0,0 +1,30 @@
1
+ import type { ChartSeriesData, ChartTooltip, ChartTooltipTotalsAggregationValue, ChartTooltipTotalsBuiltInAggregation, ChartXAxis, ChartYAxis, TooltipDataChunk, ValueFormat } from '../../types';
2
+ export type HoveredValue = string | number | null | undefined;
3
+ export declare function getXRowData(data: ChartSeriesData, xAxis?: ChartXAxis | null): string | number | undefined;
4
+ export declare function getDefaultValueFormat({ axis, }: {
5
+ axis?: ChartXAxis | ChartYAxis | null;
6
+ }): ValueFormat | undefined;
7
+ export declare const getMeasureValue: ({ data, xAxis, yAxis, valueFormat, }: {
8
+ data: TooltipDataChunk[];
9
+ xAxis?: ChartXAxis | null;
10
+ yAxis?: ChartYAxis;
11
+ valueFormat?: ValueFormat;
12
+ }) => string | null;
13
+ export declare function getHoveredValues(args: {
14
+ hovered: TooltipDataChunk[];
15
+ xAxis?: ChartXAxis | null;
16
+ yAxis?: ChartYAxis;
17
+ }): HoveredValue[];
18
+ export declare function getBuiltInAggregatedValue(args: {
19
+ aggregation: ChartTooltipTotalsBuiltInAggregation;
20
+ values: HoveredValue[];
21
+ }): number | undefined;
22
+ export declare function getBuiltInAggregationLabel(args: {
23
+ aggregation: ChartTooltipTotalsBuiltInAggregation;
24
+ }): string;
25
+ export declare function getPreparedAggregation(args: {
26
+ hovered: TooltipDataChunk[];
27
+ totals?: ChartTooltip['totals'];
28
+ xAxis?: ChartXAxis | null;
29
+ yAxis?: ChartYAxis;
30
+ }): ChartTooltipTotalsBuiltInAggregation | (() => ChartTooltipTotalsAggregationValue);
@@ -0,0 +1,126 @@
1
+ import get from 'lodash/get';
2
+ import { i18n } from '../../i18n';
3
+ import { getDataCategoryValue } from '../../utils';
4
+ import { getFormattedValue } from '../../utils/chart/format';
5
+ const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
6
+ function getRowData(fieldName, data, axis) {
7
+ switch (axis === null || axis === void 0 ? void 0 : axis.type) {
8
+ case 'category': {
9
+ const categories = get(axis, 'categories', []);
10
+ return getDataCategoryValue({ axisDirection: fieldName, categories, data });
11
+ }
12
+ default: {
13
+ return get(data, fieldName);
14
+ }
15
+ }
16
+ }
17
+ export function getXRowData(data, xAxis) {
18
+ return getRowData('x', data, xAxis);
19
+ }
20
+ function getYRowData(data, yAxis) {
21
+ return getRowData('y', data, yAxis);
22
+ }
23
+ export function getDefaultValueFormat({ axis, }) {
24
+ switch (axis === null || axis === void 0 ? void 0 : axis.type) {
25
+ case 'linear':
26
+ case 'logarithmic': {
27
+ return {
28
+ type: 'number',
29
+ };
30
+ }
31
+ case 'datetime': {
32
+ return {
33
+ type: 'date',
34
+ format: DEFAULT_DATE_FORMAT,
35
+ };
36
+ }
37
+ default:
38
+ return undefined;
39
+ }
40
+ }
41
+ export const getMeasureValue = ({ data, xAxis, yAxis, valueFormat, }) => {
42
+ var _a, _b, _c, _d;
43
+ if (data.every((item) => ['pie', 'treemap', 'waterfall', 'sankey'].includes(item.series.type))) {
44
+ return null;
45
+ }
46
+ if (data.some((item) => item.series.type === 'radar')) {
47
+ return (_b = (_a = data[0].category) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : null;
48
+ }
49
+ if (data.some((item) => item.series.type === 'bar-y')) {
50
+ const format = valueFormat !== null && valueFormat !== void 0 ? valueFormat : getDefaultValueFormat({ axis: yAxis });
51
+ return getFormattedValue({
52
+ value: getYRowData((_c = data[0]) === null || _c === void 0 ? void 0 : _c.data, yAxis),
53
+ format,
54
+ });
55
+ }
56
+ const format = valueFormat !== null && valueFormat !== void 0 ? valueFormat : getDefaultValueFormat({ axis: xAxis });
57
+ return getFormattedValue({
58
+ value: getXRowData((_d = data[0]) === null || _d === void 0 ? void 0 : _d.data, xAxis),
59
+ format,
60
+ });
61
+ };
62
+ export function getHoveredValues(args) {
63
+ const { hovered, xAxis, yAxis } = args;
64
+ return hovered.map((seriesItem) => {
65
+ var _a;
66
+ const { data, series } = seriesItem;
67
+ switch (series.type) {
68
+ case 'area':
69
+ case 'line':
70
+ case 'bar-x':
71
+ case 'scatter': {
72
+ return getYRowData(data, yAxis);
73
+ }
74
+ case 'bar-y': {
75
+ return getXRowData(data, xAxis);
76
+ }
77
+ case 'pie':
78
+ case 'radar':
79
+ case 'treemap': {
80
+ const seriesData = data;
81
+ return seriesData.value;
82
+ }
83
+ case 'sankey': {
84
+ const { target, data: source } = seriesItem;
85
+ return (_a = source.links.find((d) => d.name === (target === null || target === void 0 ? void 0 : target.name))) === null || _a === void 0 ? void 0 : _a.value;
86
+ }
87
+ case 'waterfall': {
88
+ return getYRowData(data, yAxis);
89
+ }
90
+ default: {
91
+ return undefined;
92
+ }
93
+ }
94
+ });
95
+ }
96
+ export function getBuiltInAggregatedValue(args) {
97
+ const { aggregation, values } = args;
98
+ switch (aggregation) {
99
+ case 'sum':
100
+ return values.reduce((acc, value) => {
101
+ return acc + (typeof value === 'number' ? value : 0);
102
+ }, 0);
103
+ default:
104
+ return undefined;
105
+ }
106
+ }
107
+ export function getBuiltInAggregationLabel(args) {
108
+ const { aggregation } = args;
109
+ switch (aggregation) {
110
+ case 'sum':
111
+ return i18n('tooltip', 'label_totals_sum');
112
+ default:
113
+ return '';
114
+ }
115
+ }
116
+ export function getPreparedAggregation(args) {
117
+ const { hovered, totals, xAxis, yAxis } = args;
118
+ const aggregation = totals === null || totals === void 0 ? void 0 : totals.aggregation;
119
+ if (typeof aggregation === 'string') {
120
+ return aggregation;
121
+ }
122
+ if (typeof aggregation === 'function') {
123
+ return () => aggregation({ hovered, xAxis, yAxis });
124
+ }
125
+ return 'sum';
126
+ }
@@ -0,0 +1,6 @@
1
+ export declare const AXIS_TYPE: {
2
+ readonly CATEGORY: "category";
3
+ readonly DATETIME: "datetime";
4
+ readonly LINEAR: "linear";
5
+ readonly LOGARITHMIC: "logarithmic";
6
+ };
@@ -0,0 +1,6 @@
1
+ export const AXIS_TYPE = {
2
+ CATEGORY: 'category',
3
+ DATETIME: 'datetime',
4
+ LINEAR: 'linear',
5
+ LOGARITHMIC: 'logarithmic',
6
+ };
@@ -1,8 +1,10 @@
1
- export * from './defaults';
2
- export * from './misc';
1
+ export * from './axis';
3
2
  export * from './chart-types';
4
- export * from './line-styles';
5
- export * from './symbol-types';
3
+ export * from './defaults';
6
4
  export * from './layout-algorithms';
5
+ export * from './line-styles';
6
+ export * from './misc';
7
7
  export * from './palette';
8
+ export * from './symbol-types';
9
+ export * from './tooltip';
8
10
  export * from './typography';
@@ -1,8 +1,10 @@
1
- export * from './defaults';
2
- export * from './misc';
1
+ export * from './axis';
3
2
  export * from './chart-types';
4
- export * from './line-styles';
5
- export * from './symbol-types';
3
+ export * from './defaults';
6
4
  export * from './layout-algorithms';
5
+ export * from './line-styles';
6
+ export * from './misc';
7
7
  export * from './palette';
8
+ export * from './symbol-types';
9
+ export * from './tooltip';
8
10
  export * from './typography';
@@ -0,0 +1,3 @@
1
+ export declare const TOOLTIP_TOTALS_BUILT_IN_AGGREGATION: {
2
+ readonly SUM: "sum";
3
+ };
@@ -0,0 +1,3 @@
1
+ export const TOOLTIP_TOTALS_BUILT_IN_AGGREGATION = {
2
+ SUM: 'sum',
3
+ };