@gravity-ui/charts 1.11.4 → 1.13.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 (135) hide show
  1. package/dist/cjs/components/Axis/AxisX.js +62 -36
  2. package/dist/cjs/components/Axis/AxisY.js +67 -31
  3. package/dist/cjs/components/ChartInner/styles.css +1 -0
  4. package/dist/cjs/components/ChartInner/useChartInnerProps.js +15 -9
  5. package/dist/cjs/components/Tooltip/ChartTooltipContent.d.ts +1 -0
  6. package/dist/cjs/components/Tooltip/ChartTooltipContent.js +3 -3
  7. package/dist/cjs/components/Tooltip/DefaultTooltipContent/Row.d.ts +9 -0
  8. package/dist/cjs/components/Tooltip/DefaultTooltipContent/Row.js +10 -0
  9. package/dist/cjs/components/Tooltip/DefaultTooltipContent/RowTotals.d.ts +9 -0
  10. package/dist/cjs/components/Tooltip/DefaultTooltipContent/RowTotals.js +23 -0
  11. package/dist/cjs/components/Tooltip/DefaultTooltipContent/index.d.ts +11 -0
  12. package/dist/cjs/components/Tooltip/DefaultTooltipContent/index.js +102 -0
  13. package/dist/cjs/components/Tooltip/DefaultTooltipContent/utils.d.ts +30 -0
  14. package/dist/cjs/components/Tooltip/DefaultTooltipContent/utils.js +126 -0
  15. package/dist/cjs/components/Tooltip/index.js +1 -1
  16. package/dist/cjs/components/Tooltip/styles.css +14 -2
  17. package/dist/cjs/components/Tooltip/utils.d.ts +30 -0
  18. package/dist/cjs/components/Tooltip/utils.js +126 -0
  19. package/dist/cjs/constants/axis.d.ts +6 -0
  20. package/dist/cjs/constants/axis.js +6 -0
  21. package/dist/cjs/constants/index.d.ts +6 -4
  22. package/dist/cjs/constants/index.js +6 -4
  23. package/dist/cjs/constants/tooltip.d.ts +3 -0
  24. package/dist/cjs/constants/tooltip.js +3 -0
  25. package/dist/cjs/hooks/useAxisScales/index.d.ts +14 -3
  26. package/dist/cjs/hooks/useAxisScales/index.js +86 -22
  27. package/dist/cjs/hooks/useChartOptions/types.d.ts +5 -0
  28. package/dist/cjs/hooks/useChartOptions/utils.d.ts +11 -0
  29. package/dist/cjs/hooks/useChartOptions/utils.js +27 -0
  30. package/dist/cjs/hooks/useChartOptions/x-axis.js +6 -2
  31. package/dist/cjs/hooks/useChartOptions/y-axis.d.ts +4 -2
  32. package/dist/cjs/hooks/useChartOptions/y-axis.js +14 -4
  33. package/dist/cjs/hooks/useSeries/prepare-area.d.ts +1 -1
  34. package/dist/cjs/hooks/useSeries/prepare-bar-y.d.ts +3 -0
  35. package/dist/cjs/hooks/useSeries/prepare-bar-y.js +5 -2
  36. package/dist/cjs/hooks/useSeries/prepare-line.d.ts +1 -1
  37. package/dist/cjs/hooks/useSeries/prepare-radar.d.ts +1 -1
  38. package/dist/cjs/hooks/useSeries/types.d.ts +3 -0
  39. package/dist/cjs/hooks/useShapes/bar-x/prepare-data.js +16 -13
  40. package/dist/cjs/hooks/useShapes/bar-y/index.d.ts +2 -2
  41. package/dist/cjs/hooks/useShapes/bar-y/index.js +5 -9
  42. package/dist/cjs/hooks/useShapes/bar-y/prepare-data.d.ts +2 -2
  43. package/dist/cjs/hooks/useShapes/bar-y/prepare-data.js +80 -107
  44. package/dist/cjs/hooks/useShapes/bar-y/types.d.ts +7 -2
  45. package/dist/cjs/hooks/useShapes/index.js +1 -1
  46. package/dist/cjs/hooks/useShapes/waterfall/prepare-data.js +1 -1
  47. package/dist/cjs/hooks/utils/bar-y.d.ts +27 -0
  48. package/dist/cjs/hooks/utils/bar-y.js +55 -0
  49. package/dist/cjs/hooks/utils/index.d.ts +1 -0
  50. package/dist/cjs/hooks/utils/index.js +1 -0
  51. package/dist/cjs/i18n/keysets/en.json +7 -1
  52. package/dist/cjs/i18n/keysets/ru.json +7 -1
  53. package/dist/cjs/types/chart/axis.d.ts +15 -3
  54. package/dist/cjs/types/chart/bar-y.d.ts +10 -0
  55. package/dist/cjs/types/chart/series.d.ts +10 -0
  56. package/dist/cjs/types/chart/tooltip.d.ts +21 -0
  57. package/dist/cjs/utils/chart/axis-generators/bottom.js +26 -13
  58. package/dist/cjs/utils/chart/get-closest-data.js +13 -12
  59. package/dist/cjs/utils/chart/index.js +1 -1
  60. package/dist/cjs/utils/chart/series/sorting.d.ts +6 -2
  61. package/dist/cjs/utils/chart/series/sorting.js +29 -4
  62. package/dist/cjs/utils/chart/zoom.js +2 -1
  63. package/dist/cjs/validation/index.js +55 -1
  64. package/dist/esm/components/Axis/AxisX.js +62 -36
  65. package/dist/esm/components/Axis/AxisY.js +67 -31
  66. package/dist/esm/components/ChartInner/styles.css +1 -0
  67. package/dist/esm/components/ChartInner/useChartInnerProps.js +15 -9
  68. package/dist/esm/components/Tooltip/ChartTooltipContent.d.ts +1 -0
  69. package/dist/esm/components/Tooltip/ChartTooltipContent.js +3 -3
  70. package/dist/esm/components/Tooltip/DefaultTooltipContent/Row.d.ts +9 -0
  71. package/dist/esm/components/Tooltip/DefaultTooltipContent/Row.js +10 -0
  72. package/dist/esm/components/Tooltip/DefaultTooltipContent/RowTotals.d.ts +9 -0
  73. package/dist/esm/components/Tooltip/DefaultTooltipContent/RowTotals.js +23 -0
  74. package/dist/esm/components/Tooltip/DefaultTooltipContent/index.d.ts +11 -0
  75. package/dist/esm/components/Tooltip/DefaultTooltipContent/index.js +102 -0
  76. package/dist/esm/components/Tooltip/DefaultTooltipContent/utils.d.ts +30 -0
  77. package/dist/esm/components/Tooltip/DefaultTooltipContent/utils.js +126 -0
  78. package/dist/esm/components/Tooltip/index.js +1 -1
  79. package/dist/esm/components/Tooltip/styles.css +14 -2
  80. package/dist/esm/components/Tooltip/utils.d.ts +30 -0
  81. package/dist/esm/components/Tooltip/utils.js +126 -0
  82. package/dist/esm/constants/axis.d.ts +6 -0
  83. package/dist/esm/constants/axis.js +6 -0
  84. package/dist/esm/constants/index.d.ts +6 -4
  85. package/dist/esm/constants/index.js +6 -4
  86. package/dist/esm/constants/tooltip.d.ts +3 -0
  87. package/dist/esm/constants/tooltip.js +3 -0
  88. package/dist/esm/hooks/useAxisScales/index.d.ts +14 -3
  89. package/dist/esm/hooks/useAxisScales/index.js +86 -22
  90. package/dist/esm/hooks/useChartOptions/types.d.ts +5 -0
  91. package/dist/esm/hooks/useChartOptions/utils.d.ts +11 -0
  92. package/dist/esm/hooks/useChartOptions/utils.js +27 -0
  93. package/dist/esm/hooks/useChartOptions/x-axis.js +6 -2
  94. package/dist/esm/hooks/useChartOptions/y-axis.d.ts +4 -2
  95. package/dist/esm/hooks/useChartOptions/y-axis.js +14 -4
  96. package/dist/esm/hooks/useSeries/prepare-area.d.ts +1 -1
  97. package/dist/esm/hooks/useSeries/prepare-bar-y.d.ts +3 -0
  98. package/dist/esm/hooks/useSeries/prepare-bar-y.js +5 -2
  99. package/dist/esm/hooks/useSeries/prepare-line.d.ts +1 -1
  100. package/dist/esm/hooks/useSeries/prepare-radar.d.ts +1 -1
  101. package/dist/esm/hooks/useSeries/types.d.ts +3 -0
  102. package/dist/esm/hooks/useShapes/bar-x/prepare-data.js +16 -13
  103. package/dist/esm/hooks/useShapes/bar-y/index.d.ts +2 -2
  104. package/dist/esm/hooks/useShapes/bar-y/index.js +5 -9
  105. package/dist/esm/hooks/useShapes/bar-y/prepare-data.d.ts +2 -2
  106. package/dist/esm/hooks/useShapes/bar-y/prepare-data.js +80 -107
  107. package/dist/esm/hooks/useShapes/bar-y/types.d.ts +7 -2
  108. package/dist/esm/hooks/useShapes/index.js +1 -1
  109. package/dist/esm/hooks/useShapes/waterfall/prepare-data.js +1 -1
  110. package/dist/esm/hooks/utils/bar-y.d.ts +27 -0
  111. package/dist/esm/hooks/utils/bar-y.js +55 -0
  112. package/dist/esm/hooks/utils/index.d.ts +1 -0
  113. package/dist/esm/hooks/utils/index.js +1 -0
  114. package/dist/esm/i18n/keysets/en.json +7 -1
  115. package/dist/esm/i18n/keysets/ru.json +7 -1
  116. package/dist/esm/types/chart/axis.d.ts +15 -3
  117. package/dist/esm/types/chart/bar-y.d.ts +10 -0
  118. package/dist/esm/types/chart/series.d.ts +10 -0
  119. package/dist/esm/types/chart/tooltip.d.ts +21 -0
  120. package/dist/esm/utils/chart/axis-generators/bottom.js +26 -13
  121. package/dist/esm/utils/chart/get-closest-data.js +13 -12
  122. package/dist/esm/utils/chart/index.js +1 -1
  123. package/dist/esm/utils/chart/series/sorting.d.ts +6 -2
  124. package/dist/esm/utils/chart/series/sorting.js +29 -4
  125. package/dist/esm/utils/chart/zoom.js +2 -1
  126. package/dist/esm/validation/index.js +55 -1
  127. package/package.json +1 -1
  128. package/dist/cjs/components/Tooltip/DefaultContent.d.ts +0 -10
  129. package/dist/cjs/components/Tooltip/DefaultContent.js +0 -187
  130. package/dist/esm/components/Tooltip/DefaultContent.d.ts +0 -10
  131. package/dist/esm/components/Tooltip/DefaultContent.js +0 -187
  132. /package/dist/cjs/hooks/{useShapes/constants.d.ts → constants.d.ts} +0 -0
  133. /package/dist/cjs/hooks/{useShapes/constants.js → constants.js} +0 -0
  134. /package/dist/esm/hooks/{useShapes/constants.d.ts → constants.d.ts} +0 -0
  135. /package/dist/esm/hooks/{useShapes/constants.js → constants.js} +0 -0
@@ -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
+ };
@@ -1,13 +1,14 @@
1
1
  import type { ScaleBand, ScaleLinear, ScaleTime } from 'd3';
2
2
  import type { ChartAxis, ChartSeries } from '../../types';
3
3
  import type { PreparedAxis } from '../useChartOptions/types';
4
- import type { PreparedSeries } from '../useSeries/types';
4
+ import type { PreparedSeries, PreparedSeriesOptions } from '../useSeries/types';
5
5
  import type { PreparedSplit } from '../useSplit/types';
6
6
  export type ChartScale = ScaleLinear<number, number> | ScaleBand<string> | ScaleTime<number, number>;
7
7
  type Args = {
8
8
  boundsWidth: number;
9
9
  boundsHeight: number;
10
10
  series: PreparedSeries[];
11
+ seriesOptions: PreparedSeriesOptions;
11
12
  xAxis: PreparedAxis | null;
12
13
  yAxis: PreparedAxis[];
13
14
  split: PreparedSplit;
@@ -18,8 +19,18 @@ type ReturnValue = {
18
19
  xScale?: ChartScale;
19
20
  yScale?: ChartScale[];
20
21
  };
21
- export declare function createYScale(axis: PreparedAxis, series: PreparedSeries[], boundsHeight: number): ScaleBand<string> | ScaleLinear<number, number, never> | ScaleTime<number, number, never>;
22
- export declare function createXScale(axis: PreparedAxis | ChartAxis, series: (PreparedSeries | ChartSeries)[], boundsWidth: number, hasZoomX?: boolean): ScaleBand<string> | ScaleLinear<number, number, never> | ScaleTime<number, number, never>;
22
+ export declare function createYScale(args: {
23
+ axis: PreparedAxis;
24
+ boundsHeight: number;
25
+ series: (PreparedSeries | ChartSeries)[];
26
+ seriesOptions: PreparedSeriesOptions;
27
+ }): ScaleBand<string> | ScaleLinear<number, number, never> | ScaleTime<number, number, never>;
28
+ export declare function createXScale(args: {
29
+ axis: PreparedAxis | ChartAxis;
30
+ boundsWidth: number;
31
+ series: (PreparedSeries | ChartSeries)[];
32
+ hasZoomX?: boolean;
33
+ }): ScaleBand<string> | ScaleLinear<number, number, never> | ScaleTime<number, number, never>;
23
34
  /**
24
35
  * Uses to create scales for axis related series
25
36
  */
@@ -1,13 +1,14 @@
1
1
  import React from 'react';
2
2
  import { extent, scaleBand, scaleLinear, scaleLog, scaleUtc } from 'd3';
3
3
  import get from 'lodash/get';
4
- import { DEFAULT_AXIS_TYPE } from '../../constants';
4
+ import { DEFAULT_AXIS_TYPE, SeriesType } from '../../constants';
5
5
  import { CHART_SERIES_WITH_VOLUME_ON_Y_AXIS, getAxisHeight, getDataCategoryValue, getDefaultMaxXAxisValue, getDefaultMinXAxisValue, getDomainDataXBySeries, getDomainDataYBySeries, getOnlyVisibleSeries, isAxisRelatedSeries, isSeriesWithCategoryValues, } from '../../utils';
6
+ import { getBarYLayoutForNumericScale, groupBarYDataByYValue } from '../utils';
6
7
  const X_AXIS_ZOOM_PADDING = 0.02;
7
- const isNumericalArrayData = (data) => {
8
+ function isNumericalArrayData(data) {
8
9
  return data.every((d) => typeof d === 'number' || d === null);
9
- };
10
- const filterCategoriesByVisibleSeries = (args) => {
10
+ }
11
+ function filterCategoriesByVisibleSeries(args) {
11
12
  const { axisDirection, categories, series } = args;
12
13
  const visibleCategories = new Set();
13
14
  series.forEach((s) => {
@@ -18,18 +19,67 @@ const filterCategoriesByVisibleSeries = (args) => {
18
19
  }
19
20
  });
20
21
  return categories.filter((c) => visibleCategories.has(c));
21
- };
22
- export function createYScale(axis, series, boundsHeight) {
23
- const yType = get(axis, 'type', DEFAULT_AXIS_TYPE);
22
+ }
23
+ // axis is validated in `validation/index.ts`, so the value of `axis.type` is definitely valid.
24
+ // eslint-disable-next-line consistent-return
25
+ function getYScaleRange(args) {
26
+ const { axis, boundsHeight, series, seriesOptions } = args;
27
+ switch (axis.type) {
28
+ case 'datetime':
29
+ case 'linear':
30
+ case 'logarithmic': {
31
+ let range = [boundsHeight, boundsHeight * axis.maxPadding];
32
+ switch (axis.order) {
33
+ case 'sortDesc':
34
+ case 'reverse': {
35
+ range.reverse();
36
+ }
37
+ }
38
+ const barYSeries = series.filter((s) => s.type === SeriesType.BarY);
39
+ if (barYSeries.length) {
40
+ const groupedData = groupBarYDataByYValue(barYSeries, [axis]);
41
+ const { barSize, dataLength } = getBarYLayoutForNumericScale({
42
+ plotHeight: boundsHeight - boundsHeight * axis.maxPadding,
43
+ groupedData,
44
+ seriesOptions: seriesOptions,
45
+ });
46
+ if (dataLength > 1) {
47
+ const alreadyCountedStackingIds = new Set();
48
+ const offsetMultiplier = barYSeries.reduce((acc, s) => {
49
+ let count = 0;
50
+ if (s.stackId) {
51
+ if (!alreadyCountedStackingIds.has(s.stackId)) {
52
+ alreadyCountedStackingIds.add(s.stackId);
53
+ count = 1;
54
+ }
55
+ }
56
+ else {
57
+ count = 1;
58
+ }
59
+ return acc + count;
60
+ }, 0);
61
+ const offset = (barSize * Math.max(offsetMultiplier, 1)) / 2;
62
+ range = [range[0] - offset, range[1] + offset];
63
+ }
64
+ }
65
+ return range;
66
+ }
67
+ case 'category': {
68
+ return [boundsHeight, 0];
69
+ }
70
+ }
71
+ }
72
+ export function createYScale(args) {
73
+ const { axis, boundsHeight, series, seriesOptions } = args;
24
74
  const yMinProps = get(axis, 'min');
25
75
  const yMaxProps = get(axis, 'max');
26
76
  const yCategories = get(axis, 'categories');
27
77
  const yTimestamps = get(axis, 'timestamps');
28
- switch (yType) {
78
+ const range = getYScaleRange({ axis, boundsHeight, series, seriesOptions });
79
+ switch (axis.type) {
29
80
  case 'linear':
30
81
  case 'logarithmic': {
31
82
  const domain = getDomainDataYBySeries(series);
32
- const range = [boundsHeight, boundsHeight * axis.maxPadding];
33
83
  if (isNumericalArrayData(domain)) {
34
84
  const [yMinDomain, yMaxDomain] = extent(domain);
35
85
  const yMin = typeof yMinProps === 'number' ? yMinProps : yMinDomain;
@@ -41,7 +91,7 @@ export function createYScale(axis, series, boundsHeight) {
41
91
  const hasSeriesWithVolumeOnYAxis = series.some((s) => CHART_SERIES_WITH_VOLUME_ON_Y_AXIS.includes(s.type));
42
92
  yMax = hasSeriesWithVolumeOnYAxis ? Math.max(yMaxDomain, 0) : yMaxDomain;
43
93
  }
44
- const scaleFn = yType === 'logarithmic' ? scaleLog : scaleLinear;
94
+ const scaleFn = axis.type === 'logarithmic' ? scaleLog : scaleLinear;
45
95
  return scaleFn().domain([yMin, yMax]).range(range).nice();
46
96
  }
47
97
  break;
@@ -53,12 +103,11 @@ export function createYScale(axis, series, boundsHeight) {
53
103
  categories: yCategories,
54
104
  series: series,
55
105
  });
56
- return scaleBand().domain(filteredCategories).range([boundsHeight, 0]);
106
+ return scaleBand().domain(filteredCategories).range(range);
57
107
  }
58
108
  break;
59
109
  }
60
110
  case 'datetime': {
61
- const range = [boundsHeight, boundsHeight * axis.maxPadding];
62
111
  if (yTimestamps) {
63
112
  const [yMinTimestamp, yMaxTimestamp] = extent(yTimestamps);
64
113
  const yMin = typeof yMinProps === 'number' ? yMinProps : yMinTimestamp;
@@ -74,7 +123,6 @@ export function createYScale(axis, series, boundsHeight) {
74
123
  return scaleUtc().domain([yMin, yMax]).range(range).nice();
75
124
  }
76
125
  }
77
- break;
78
126
  }
79
127
  }
80
128
  throw new Error('Failed to create yScale');
@@ -97,7 +145,8 @@ function calculateXAxisPadding(series) {
97
145
  return result;
98
146
  }
99
147
  // eslint-disable-next-line complexity
100
- export function createXScale(axis, series, boundsWidth, hasZoomX) {
148
+ export function createXScale(args) {
149
+ const { axis, boundsWidth, series, hasZoomX } = args;
101
150
  const xMinProps = get(axis, 'min');
102
151
  const xMaxProps = get(axis, 'max');
103
152
  const xType = get(axis, 'type', DEFAULT_AXIS_TYPE);
@@ -108,6 +157,13 @@ export function createXScale(axis, series, boundsWidth, hasZoomX) {
108
157
  const xAxisZoomPadding = boundsWidth * X_AXIS_ZOOM_PADDING;
109
158
  const xRange = [0, boundsWidth - xAxisMaxPadding];
110
159
  const xRangeZoom = [0 + xAxisZoomPadding, boundsWidth - xAxisZoomPadding];
160
+ switch (axis.order) {
161
+ case 'sortDesc':
162
+ case 'reverse': {
163
+ xRange.reverse();
164
+ xRangeZoom.reverse();
165
+ }
166
+ }
111
167
  switch (xType) {
112
168
  case 'linear':
113
169
  case 'logarithmic': {
@@ -193,13 +249,15 @@ export function createXScale(axis, series, boundsWidth, hasZoomX) {
193
249
  throw new Error('Failed to create xScale');
194
250
  }
195
251
  const createScales = (args) => {
196
- const { boundsWidth, boundsHeight, series, xAxis, yAxis, split, hasZoomX } = args;
252
+ const { boundsWidth, boundsHeight, hasZoomX, series, seriesOptions, split, xAxis, yAxis } = args;
197
253
  let visibleSeries = getOnlyVisibleSeries(series);
198
254
  // Reassign to all series in case of all series unselected,
199
255
  // otherwise we will get an empty space without grid
200
256
  visibleSeries = visibleSeries.length === 0 ? series : visibleSeries;
201
257
  return {
202
- xScale: xAxis ? createXScale(xAxis, visibleSeries, boundsWidth, hasZoomX) : undefined,
258
+ xScale: xAxis
259
+ ? createXScale({ axis: xAxis, boundsWidth, series: visibleSeries, hasZoomX })
260
+ : undefined,
203
261
  yScale: yAxis.map((axis, index) => {
204
262
  const axisSeries = series.filter((s) => {
205
263
  const seriesAxisIndex = get(s, 'yAxis', 0);
@@ -207,7 +265,12 @@ const createScales = (args) => {
207
265
  });
208
266
  const visibleAxisSeries = getOnlyVisibleSeries(axisSeries);
209
267
  const axisHeight = getAxisHeight({ boundsHeight, split });
210
- return createYScale(axis, visibleAxisSeries.length ? visibleAxisSeries : axisSeries, axisHeight);
268
+ return createYScale({
269
+ axis,
270
+ boundsHeight: axisHeight,
271
+ series: visibleAxisSeries.length ? visibleAxisSeries : axisSeries,
272
+ seriesOptions,
273
+ });
211
274
  }),
212
275
  };
213
276
  };
@@ -215,7 +278,7 @@ const createScales = (args) => {
215
278
  * Uses to create scales for axis related series
216
279
  */
217
280
  export const useAxisScales = (args) => {
218
- const { boundsWidth, boundsHeight, series, xAxis, yAxis, split, hasZoomX, hasZoomY } = args;
281
+ const { boundsWidth, boundsHeight, hasZoomX, hasZoomY, series, seriesOptions, split, xAxis, yAxis, } = args;
219
282
  return React.useMemo(() => {
220
283
  let xScale;
221
284
  let yScale;
@@ -224,14 +287,15 @@ export const useAxisScales = (args) => {
224
287
  ({ xScale, yScale } = createScales({
225
288
  boundsWidth,
226
289
  boundsHeight,
290
+ hasZoomX,
291
+ hasZoomY,
227
292
  series,
293
+ seriesOptions,
294
+ split,
228
295
  xAxis,
229
296
  yAxis,
230
- split,
231
- hasZoomX,
232
- hasZoomY,
233
297
  }));
234
298
  }
235
299
  return { xScale, yScale };
236
- }, [boundsWidth, boundsHeight, series, xAxis, yAxis, split, hasZoomX, hasZoomY]);
300
+ }, [boundsWidth, boundsHeight, hasZoomX, hasZoomY, series, seriesOptions, split, xAxis, yAxis]);
237
301
  };
@@ -22,6 +22,11 @@ export type PreparedAxisPlotLine = {
22
22
  dashStyle: DashStyle;
23
23
  opacity: number;
24
24
  layerPlacement: PlotLayerPlacement;
25
+ label: {
26
+ text: string;
27
+ style: BaseTextStyle;
28
+ padding: number;
29
+ };
25
30
  };
26
31
  export type PreparedAxis = Omit<ChartAxis, 'type' | 'labels' | 'plotLines' | 'plotBands'> & {
27
32
  type: ChartAxisType;
@@ -0,0 +1,11 @@
1
+ import type { AxisPlot, ChartYAxis } from '../../types';
2
+ export declare function prepareAxisPlotLabel(d: AxisPlot): {
3
+ text: string;
4
+ style: {
5
+ fontSize: string;
6
+ fontWeight?: string;
7
+ fontColor: string;
8
+ };
9
+ padding: number;
10
+ };
11
+ export declare function getAxisCategories(axis?: ChartYAxis): string[] | undefined;