@oliasoft-open-source/charts-library 3.3.6-beta-3 → 3.3.6-beta-5

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 (99) hide show
  1. package/index.js +14 -0
  2. package/package.json +1 -6
  3. package/release-notes.md +378 -0
  4. package/src/assets/circle.svg +1 -0
  5. package/src/assets/icons/line-and-point.svg +1 -0
  6. package/src/assets/icons/line-only.svg +1 -0
  7. package/src/assets/icons/list-hide.svg +1 -0
  8. package/src/assets/icons/point-only.svg +1 -0
  9. package/src/assets/rect.svg +1 -0
  10. package/src/assets/rectRot.svg +1 -0
  11. package/src/assets/triangle.svg +1 -0
  12. package/src/components/bar-chart/bar-chart-prop-types.js +209 -0
  13. package/src/components/bar-chart/bar-chart.interface.ts +98 -0
  14. package/src/components/bar-chart/bar-chart.jsx +247 -0
  15. package/src/components/bar-chart/bar-chart.module.less +61 -0
  16. package/src/components/bar-chart/get-bar-chart-data-labels.js +42 -0
  17. package/src/components/bar-chart/get-bar-chart-scales.js +138 -0
  18. package/src/components/bar-chart/get-bar-chart-tooltips.js +102 -0
  19. package/src/components/controls/axes-options/axes-options.jsx +271 -0
  20. package/src/components/line-chart/constants/default-translations.js +24 -0
  21. package/src/components/line-chart/constants/line-chart-consts.js +12 -0
  22. package/src/components/line-chart/controls/axes-options/action-types.js +5 -0
  23. package/src/components/line-chart/controls/axes-options/axes-options-form-state.js +97 -0
  24. package/src/components/line-chart/controls/controls-portal.jsx +12 -0
  25. package/src/components/line-chart/controls/controls.jsx +176 -0
  26. package/src/components/line-chart/controls/controls.module.less +12 -0
  27. package/src/components/line-chart/controls/drag-options.jsx +124 -0
  28. package/src/components/line-chart/controls/line-options.jsx +64 -0
  29. package/src/components/line-chart/hooks/use-chart-functions.js +261 -0
  30. package/src/components/line-chart/hooks/use-chart-options.js +181 -0
  31. package/src/components/line-chart/hooks/use-chart-plugins.js +35 -0
  32. package/src/components/line-chart/hooks/use-toggle-handler.js +33 -0
  33. package/src/components/line-chart/initialize/config.js +23 -0
  34. package/src/components/line-chart/initialize/initialize-line-chart.js +25 -0
  35. package/src/components/line-chart/legend/legend-constants.js +1 -0
  36. package/src/components/line-chart/legend/legend-dropzone.jsx +74 -0
  37. package/src/components/line-chart/legend/legend-item.jsx +105 -0
  38. package/src/components/line-chart/legend/legend-panel.jsx +86 -0
  39. package/src/components/line-chart/legend/legend.jsx +54 -0
  40. package/src/components/line-chart/legend/legend.module.less +192 -0
  41. package/src/components/line-chart/line-chart-prop-types.js +298 -0
  42. package/src/components/line-chart/line-chart.interface.ts +128 -0
  43. package/src/components/line-chart/line-chart.jsx +180 -0
  44. package/src/components/line-chart/line-chart.module.less +78 -0
  45. package/src/components/line-chart/plugins/chart-area-text-plugin.js +246 -0
  46. package/src/components/line-chart/plugins/line-chart.minor-gridlines-plugin.js +88 -0
  47. package/src/components/line-chart/plugins/plugin-constants.js +11 -0
  48. package/src/components/line-chart/state/action-types.js +12 -0
  49. package/src/components/line-chart/state/initial-state.js +89 -0
  50. package/src/components/line-chart/state/line-chart-reducer.js +101 -0
  51. package/src/components/line-chart/state/manage-state-in-local-storage.js +86 -0
  52. package/src/components/line-chart/state/use-chart-state.js +141 -0
  53. package/src/components/line-chart/utils/axis-formatting/axis-formatting.js +69 -0
  54. package/src/components/line-chart/utils/axis-scales/axis-scales.js +165 -0
  55. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-condition.js +13 -0
  56. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-data.js +20 -0
  57. package/src/components/line-chart/utils/datalabels-alignment/get-datalabels-position.js +25 -0
  58. package/src/components/line-chart/utils/generate-line-chart-datasets.js +122 -0
  59. package/src/components/line-chart/utils/get-axes-ranges-from-chart.js +13 -0
  60. package/src/components/line-chart/utils/get-line-chart-data-labels.js +21 -0
  61. package/src/components/line-chart/utils/get-line-chart-scales.js +117 -0
  62. package/src/components/line-chart/utils/get-line-chart-tooltips.js +94 -0
  63. package/src/components/line-chart/utils/line-chart-utils.js +62 -0
  64. package/src/components/line-chart/utils/translations/get-translations.js +17 -0
  65. package/src/components/pie-chart/pie-chart-prop-types.js +111 -0
  66. package/src/components/pie-chart/pie-chart-utils.js +32 -0
  67. package/src/components/pie-chart/pie-chart.interface.ts +61 -0
  68. package/src/components/pie-chart/pie-chart.jsx +458 -0
  69. package/src/components/pie-chart/pie-chart.module.less +61 -0
  70. package/src/components/scatter-chart/scatter-chart.intefrace.ts +33 -0
  71. package/src/components/scatter-chart/scatter-chart.jsx +21 -0
  72. package/src/components/scatter-chart/scatter-chart.module.less +4 -0
  73. package/src/helpers/chart-border-plugin.js +19 -0
  74. package/src/helpers/chart-consts.js +64 -0
  75. package/src/helpers/chart-interface.ts +94 -0
  76. package/src/helpers/chart-utils.js +178 -0
  77. package/src/helpers/container.jsx +60 -0
  78. package/src/helpers/disabled-context.js +8 -0
  79. package/src/helpers/enums.js +107 -0
  80. package/src/helpers/get-chart-annotation.js +102 -0
  81. package/src/helpers/get-custom-legend-plugin-example.js +80 -0
  82. package/src/helpers/get-draggableData.js +32 -0
  83. package/src/helpers/range/estimate-data-series-have-close-values.js +54 -0
  84. package/src/helpers/range/range.js +100 -0
  85. package/src/helpers/text.js +6 -0
  86. package/src/style/external.less +4 -0
  87. package/src/style/fonts/lato/Lato-Bold.woff2 +0 -0
  88. package/src/style/fonts/lato/Lato-BoldItalic.woff2 +0 -0
  89. package/src/style/fonts/lato/Lato-Italic.woff2 +0 -0
  90. package/src/style/fonts/lato/Lato-Regular.woff2 +0 -0
  91. package/src/style/fonts.less +27 -0
  92. package/src/style/global.less +43 -0
  93. package/src/style/reset/reset.less +28 -0
  94. package/src/style/shared.less +11 -0
  95. package/src/style/variables.less +91 -0
  96. package/vite.config.mjs +49 -0
  97. package/vitest.config.mjs +8 -0
  98. package/dist/index.js +0 -39189
  99. package/dist/index.js.map +0 -1
@@ -0,0 +1,98 @@
1
+ import { LinearScaleOptions } from 'chart.js';
2
+ import {
3
+ IChartAnnotations,
4
+ IChartDraggableData,
5
+ IChartInteractions,
6
+ IChartLegend,
7
+ IChartStyling
8
+ } from "../../helpers/chart-interface";
9
+
10
+ export interface IChartOptions {
11
+ enableZoom: boolean;
12
+ enablePan: boolean;
13
+ }
14
+
15
+ export interface IBarChartGraph {
16
+ showDataLabels: boolean;
17
+ showMinorGridlines: boolean;
18
+ }
19
+
20
+ export interface IBarChartTooltip {
21
+ tooltips: boolean;
22
+ showLabelsInTooltips: boolean;
23
+ }
24
+
25
+ export interface IBarChartAdditionalAxesOptions {
26
+ chartScaleType: 'linear' | 'logarithmic';
27
+ reverse: boolean;
28
+ beginAtZero: boolean;
29
+ stepSize: number;
30
+ stacked: boolean;
31
+ suggestedMin: number;
32
+ suggestedMax:number;
33
+ min: number;
34
+ max: number;
35
+ }
36
+
37
+ export interface IBarChartAxes<PositionType> {
38
+ label: string;
39
+ position: PositionType;
40
+ color: string;
41
+ unit: string;
42
+ }
43
+
44
+ export interface IBarScales {
45
+ x: LinearScaleOptions;
46
+ y: LinearScaleOptions;
47
+ }
48
+
49
+ export interface IBarChartOptions {
50
+ title: string | string[];
51
+ direction: 'vertical' | 'horizontal';
52
+ axes: {
53
+ x: IBarChartAxes<'top' | 'bottom'>[];
54
+ y: IBarChartAxes<'left' | 'right'>[];
55
+ };
56
+ scales: IBarScales,
57
+ additionalAxesOptions: IBarChartAdditionalAxesOptions;
58
+ chartStyling: IChartStyling;
59
+ tooltip: IBarChartTooltip;
60
+ graph: IBarChartGraph;
61
+ annotations: IChartAnnotations;
62
+ legend: IChartLegend;
63
+ chartOptions: IChartOptions;
64
+ interactions: IChartInteractions;
65
+ draggableData :IChartDraggableData;
66
+ }
67
+
68
+ export interface IBarDataPoint {
69
+ x: number;
70
+ y: number;
71
+ label?: string;
72
+ }
73
+
74
+ export interface IBarChartDataset {
75
+ label?: string;
76
+ data: number[] | number[][] | IBarDataPoint[];
77
+ borderSkipped?: string;
78
+ backgroundColor?: string[];
79
+ borderColor?: string[];
80
+ borderRadius?: number;
81
+ borderWidth?: number;
82
+ yAxisID?: string;
83
+ stack?: number;
84
+ }
85
+
86
+ export interface IBarChartData {
87
+ //TODO: revisit data interface definition after project is more stable
88
+ testId: string | null;
89
+ data: {
90
+ labels?: string[],
91
+ datasets: IBarChartDataset[]
92
+ } | any;
93
+ options: IBarChartOptions;
94
+ }
95
+
96
+ export interface IBarChartProps {
97
+ chart: IBarChartData;
98
+ }
@@ -0,0 +1,247 @@
1
+ import React, { useRef, useState } from 'react';
2
+ import {
3
+ BarElement,
4
+ CategoryScale,
5
+ Chart as ChartJS,
6
+ Filler,
7
+ Legend,
8
+ LinearScale,
9
+ LogarithmicScale,
10
+ Title,
11
+ Tooltip,
12
+ } from 'chart.js';
13
+ import { Bar } from 'react-chartjs-2';
14
+ import zoomPlugin from 'chartjs-plugin-zoom';
15
+ import dataLabelsPlugin from 'chartjs-plugin-datalabels';
16
+ import annotationPlugin from 'chartjs-plugin-annotation';
17
+ import dragDataPlugin from 'chartjs-plugin-dragdata';
18
+
19
+ import styles from './bar-chart.module.less';
20
+ import { BarChartPropTypes, getDefaultProps } from './bar-chart-prop-types';
21
+ import getBarChartScales from './get-bar-chart-scales';
22
+ import getBarChartToolTips from './get-bar-chart-tooltips';
23
+ import getBarChartDataLabels from './get-bar-chart-data-labels';
24
+ import getAnnotation from '../../helpers/get-chart-annotation';
25
+ import {
26
+ getTitle,
27
+ isVertical,
28
+ setAnnotations,
29
+ generateRandomColor,
30
+ getClassName,
31
+ getPlugins,
32
+ getLegend,
33
+ setDefaultTheme,
34
+ } from '../../helpers/chart-utils';
35
+ import {
36
+ ALPHA_CHANEL,
37
+ ANIMATION_DURATION,
38
+ AUTO,
39
+ COLORS,
40
+ CUSTOM_LEGEND_PLUGIN_NAME,
41
+ BORDER_COLOR,
42
+ } from '../../helpers/chart-consts';
43
+ import {
44
+ AxisType,
45
+ ChartHoverMode,
46
+ ChartType,
47
+ PointStyle,
48
+ } from '../../helpers/enums';
49
+ import getDraggableData from '../../helpers/get-draggableData';
50
+
51
+ ChartJS.register(
52
+ LinearScale,
53
+ CategoryScale,
54
+ LogarithmicScale,
55
+ BarElement,
56
+ Legend,
57
+ Tooltip,
58
+ Title,
59
+ Filler,
60
+ zoomPlugin,
61
+ dataLabelsPlugin,
62
+ annotationPlugin,
63
+ dragDataPlugin,
64
+ );
65
+
66
+ /**
67
+ * this is a bar chart component
68
+ * @param {import('./bar-chart.interface').IBarChartProps} props
69
+ */
70
+ const BarChart = (props) => {
71
+ setDefaultTheme();
72
+ const chartRef = useRef(null);
73
+ const [pointHover, setPointHover] = useState(false);
74
+ const chart = getDefaultProps(props);
75
+ const { options, testId } = chart;
76
+ const { annotations, chartStyling, interactions, graph } = chart.options;
77
+
78
+ const [visibleAnnotationsIndices, setVisibleAnnotationsIndices] = useState(
79
+ setAnnotations(annotations.annotationsData),
80
+ );
81
+
82
+ const generateBarChartDatasets = (datasets) => {
83
+ const barDatasetsCopy = [...datasets];
84
+
85
+ // Add annotations to dataset to have them appear in legend.
86
+ if (
87
+ annotations.controlAnnotation &&
88
+ annotations.showAnnotations &&
89
+ annotations.annotationsData?.length
90
+ ) {
91
+ annotations.annotationsData.forEach((annotation, index) => {
92
+ barDatasetsCopy.push({
93
+ isAnnotation: true,
94
+ label: annotation.label,
95
+ annotationIndex: index,
96
+ backgroundColor: annotation.color || COLORS[index],
97
+ borderColor: annotation.color || COLORS[index],
98
+ data: [],
99
+ type: ChartType.Bar,
100
+ });
101
+ });
102
+ }
103
+
104
+ const generatedDatasets = barDatasetsCopy.map((barDataset, index) => {
105
+ const colorSchema = COLORS;
106
+ const colors = barDataset.data.map((_, i) => {
107
+ const colorSelectionIndex = datasets.length > 1 ? index : i;
108
+ return colorSchema[colorSelectionIndex] || generateRandomColor(COLORS);
109
+ });
110
+
111
+ const backgroundColors =
112
+ barDataset.backgroundColor ||
113
+ colors.map((color) => color + ALPHA_CHANEL);
114
+
115
+ return {
116
+ ...barDataset,
117
+ borderWidth: parseFloat(barDataset.borderWidth) || '1',
118
+ borderColor: barDataset.borderColor || colors,
119
+ backgroundColor: backgroundColors,
120
+ };
121
+ });
122
+ return generatedDatasets;
123
+ };
124
+
125
+ const generatedDatasets = generateBarChartDatasets(chart.data.datasets);
126
+
127
+ const legendClick = (e, legendItem) => {
128
+ const index = legendItem.datasetIndex;
129
+ const chartInstance = chartRef.current;
130
+ const { datasets } = chartInstance.data;
131
+ const dataset = datasets[index];
132
+ const meta = chartInstance.getDatasetMeta(index);
133
+ meta.hidden = meta.hidden === null ? !dataset.hidden : null;
134
+
135
+ if (annotations.controlAnnotation && dataset.isAnnotation) {
136
+ const { annotationIndex } = dataset;
137
+ if (visibleAnnotationsIndices.includes(annotationIndex)) {
138
+ setVisibleAnnotationsIndices(
139
+ visibleAnnotationsIndices.filter((ann) => ann !== annotationIndex),
140
+ );
141
+ } else {
142
+ setVisibleAnnotationsIndices([
143
+ ...visibleAnnotationsIndices,
144
+ annotationIndex,
145
+ ]);
146
+ }
147
+ }
148
+
149
+ // Show/hide entire display group
150
+ if (dataset.displayGroup) {
151
+ datasets.forEach((ds, ix) => {
152
+ if (ds.displayGroup !== dataset.displayGroup) {
153
+ return;
154
+ }
155
+ chartInstance.getDatasetMeta(ix).hidden = meta.hidden;
156
+ });
157
+ }
158
+
159
+ if (interactions.onLegendClick) {
160
+ interactions.onLegendClick(legendItem?.text, legendItem.hidden);
161
+ }
162
+
163
+ chartInstance.update();
164
+ };
165
+
166
+ const onClick = (evt, elements, chartInstance) => {
167
+ chartInstance.resetZoom();
168
+ // TODO: Restore redux-logic for zoom
169
+ };
170
+
171
+ const onHover = (evt, hoveredItems, chartInstance) => {
172
+ if (pointHover && !hoveredItems?.length) {
173
+ setPointHover(false);
174
+ if (interactions.onBarUnhover) {
175
+ interactions.onBarUnhover(evt);
176
+ }
177
+ }
178
+ if (!pointHover && hoveredItems?.length) {
179
+ setPointHover(true);
180
+ if (interactions.onBarHover) {
181
+ const { index, datasetIndex } = hoveredItems[0];
182
+ const generatedDataset = generatedDatasets;
183
+ interactions.onBarHover(evt, datasetIndex, index, generatedDataset);
184
+ }
185
+ }
186
+ };
187
+
188
+ return (
189
+ <div
190
+ className={getClassName(chartStyling, styles)}
191
+ style={{
192
+ width: chartStyling.width || AUTO,
193
+ height: chartStyling.height || AUTO,
194
+ }}
195
+ data-testid={testId}
196
+ >
197
+ <Bar
198
+ ref={chartRef}
199
+ data={{
200
+ labels: chart.data.labels.length ? chart.data.labels : [''],
201
+ datasets: generatedDatasets,
202
+ }}
203
+ options={{
204
+ onClick,
205
+ onHover,
206
+ indexAxis: isVertical(options.direction) ? AxisType.X : AxisType.Y,
207
+ maintainAspectRatio: chartStyling.maintainAspectRatio,
208
+ animation: chartStyling.performanceMode
209
+ ? false
210
+ : {
211
+ duration: ANIMATION_DURATION.FAST,
212
+ },
213
+ hover: {
214
+ mode: ChartHoverMode.Nearest,
215
+ intersect: true,
216
+ },
217
+ elements: {
218
+ bar: { pointStyle: PointStyle.Circle },
219
+ },
220
+ scales: getBarChartScales(chart),
221
+ plugins: {
222
+ title: getTitle(options),
223
+ datalabels: getBarChartDataLabels(options),
224
+ annotation: getAnnotation(options, {
225
+ showAnnotationLineIndex: visibleAnnotationsIndices,
226
+ }),
227
+ tooltip: getBarChartToolTips(options),
228
+ legend: getLegend(options, legendClick),
229
+ [CUSTOM_LEGEND_PLUGIN_NAME]: options.legend.customLegend
230
+ .customLegendPlugin && {
231
+ containerID: options.legend.customLegend.customLegendContainerID,
232
+ },
233
+ chartAreaBorder: {
234
+ borderColor: BORDER_COLOR,
235
+ },
236
+ dragData: getDraggableData(options),
237
+ },
238
+ }}
239
+ plugins={getPlugins(graph, options.legend)}
240
+ />
241
+ </div>
242
+ );
243
+ };
244
+
245
+ BarChart.propTypes = BarChartPropTypes;
246
+
247
+ export { BarChart };
@@ -0,0 +1,61 @@
1
+ html[data-theme='dark'] .chart canvas {
2
+ // Flip chart colors if dark mode enabled
3
+ filter: invert(1) hue-rotate(180deg);
4
+ }
5
+
6
+ .chart {
7
+ border: 1px solid rgba(255, 255, 255, 0);
8
+ padding-top: 10px;
9
+ position: relative;
10
+
11
+ canvas {
12
+ width: 100% !important; // Fix for resizing bug
13
+ height: 100% !important; // Remove if stretched when maintainAspectRatio=true
14
+ }
15
+
16
+ &.fixedHeight {
17
+ display: flex;
18
+ align-items: flex-start;
19
+ justify-content: flex-start;
20
+ height: auto;
21
+ }
22
+
23
+ &.stretchHeight {
24
+ display: flex;
25
+ align-items: stretch;
26
+ justify-content: stretch;
27
+ height: 100%;
28
+ }
29
+
30
+ &:focus {
31
+ border: 1px solid #85b7d9;
32
+ outline: none; // Remove dotted outline on FF
33
+ }
34
+
35
+ &::-moz-focus-inner {
36
+ border: 0; // Remove dotted outline on FF
37
+ }
38
+ }
39
+
40
+ .zoomForm {
41
+ position: absolute;
42
+ display: flex;
43
+ align-items: center;
44
+ justify-content: center;
45
+ top: 0;
46
+ right: 0;
47
+
48
+ .zoomReset {
49
+ margin-left: 10px;
50
+ }
51
+
52
+ .help {
53
+ margin-left: 5px;
54
+ line-height: 0; // Strip whitespace from icon
55
+ }
56
+ }
57
+
58
+ .autoWeight {
59
+ width: auto;
60
+ height: auto;
61
+ }
@@ -0,0 +1,42 @@
1
+ import { AlignOptions } from '../../helpers/enums';
2
+ import { AUTO } from '../../helpers/chart-consts';
3
+
4
+ /**
5
+ * @param {import('./bar-chart.interface').IBarChartOptions} options - bar chart options object
6
+ */
7
+ const getBarChartDataLabels = (options) => {
8
+ const { beginAtZero, reverse } = options.additionalAxesOptions;
9
+
10
+ const formatterCallback = (value, context) => {
11
+ const { dataset, dataIndex } = context;
12
+ const dataElement = dataset.data[dataIndex];
13
+
14
+ let dataLabel = '';
15
+ if (typeof dataElement === 'number') {
16
+ dataLabel = Number.isInteger(dataElement)
17
+ ? dataElement
18
+ : dataElement.toFixed(2);
19
+ }
20
+ if (Array.isArray(dataElement)) {
21
+ dataLabel = dataElement.reduce((acc, curr, i) => {
22
+ return i === 0 ? `${acc} ${curr}` : `${acc}, ${curr}`;
23
+ }, '');
24
+ }
25
+
26
+ return dataLabel;
27
+ };
28
+
29
+ const beginAtNotZeroAlign = reverse ? AlignOptions.End : AlignOptions.Start;
30
+ const beginAtNotZeroAnchor = reverse ? AlignOptions.Start : AlignOptions.End;
31
+
32
+ return options.graph.showDataLabels
33
+ ? {
34
+ display: AUTO,
35
+ align: beginAtZero ? AlignOptions.Center : beginAtNotZeroAlign,
36
+ anchor: beginAtZero ? AlignOptions.Center : beginAtNotZeroAnchor,
37
+ formatter: formatterCallback,
38
+ }
39
+ : { display: false };
40
+ };
41
+
42
+ export default getBarChartDataLabels;
@@ -0,0 +1,138 @@
1
+ import { AxisType, ScaleType } from '../../helpers/enums';
2
+ import {
3
+ getAxisPosition,
4
+ isVertical,
5
+ generateRandomColor,
6
+ } from '../../helpers/chart-utils';
7
+ import { COLORS, LOGARITHMIC_STEPS } from '../../helpers/chart-consts';
8
+ import { formatAxisLabelNumbers } from '../line-chart/utils/axis-formatting/axis-formatting';
9
+
10
+ /**
11
+ * @param {import('./bar-chart.interface').IBarChartData} chart - chart data
12
+ * @param {'x'|'y'} axisType
13
+ * @param {import('./bar-chart.interface').IBarChartAxes} [currentScales]
14
+ */
15
+ const getBarChartAxis = (chart, axisType, currentScales, i) => {
16
+ const isDirectionVertical = isVertical(chart.options.direction);
17
+
18
+ const axisData = currentScales || chart.options.axes[axisType][0];
19
+ const isDirectionCompatibleWithAxisType =
20
+ (isDirectionVertical && axisType === AxisType.Y) ||
21
+ (!isDirectionVertical && axisType === AxisType.X);
22
+
23
+ const grid = axisData?.gridLines || {};
24
+
25
+ const getScaleType = () => {
26
+ const scaleType = chart.data.labels ? ScaleType.Category : ScaleType.Linear;
27
+ const axisWithScale = isDirectionVertical ? AxisType.X : AxisType.Y;
28
+ return axisType === axisWithScale
29
+ ? scaleType
30
+ : chart.options.additionalAxesOptions.chartScaleType;
31
+ };
32
+
33
+ const getReverse = () => {
34
+ const axisWithReverse = isDirectionVertical ? AxisType.Y : AxisType.X;
35
+ return axisType === axisWithReverse
36
+ ? chart.options.additionalAxesOptions.reverse
37
+ : false;
38
+ };
39
+
40
+ const getTicks = () => {
41
+ const additionalAxesOptions = chart?.options?.additionalAxesOptions;
42
+ const ticksConfigFromProps =
43
+ chart?.options?.scales?.[`${i === 0 ? axisType : axisType + i}`]?.ticks;
44
+ const isLogarithmic =
45
+ isDirectionCompatibleWithAxisType &&
46
+ additionalAxesOptions?.chartScaleType === ScaleType.Logarithmic;
47
+ const ticksFormattingCallback = (tick, _index, ticks) => {
48
+ return isLogarithmic
49
+ ? LOGARITHMIC_STEPS.includes(tick)
50
+ ? formatAxisLabelNumbers(tick, ticks)
51
+ : ''
52
+ : formatAxisLabelNumbers(tick, ticks);
53
+ };
54
+ const stepSize = !isLogarithmic
55
+ ? {
56
+ stepSize:
57
+ axisData.stepSize ??
58
+ (axisType === AxisType.Y ? additionalAxesOptions.stepSize : null),
59
+ }
60
+ : {};
61
+ const ticks = {
62
+ ...stepSize,
63
+ callback: ticksFormattingCallback,
64
+ includeBounds: false, //OW-10088 disable irregular axis ticks
65
+ ...ticksConfigFromProps,
66
+ font: {
67
+ size: 14,
68
+ },
69
+ };
70
+ return ticks;
71
+ };
72
+
73
+ return {
74
+ type: getScaleType(),
75
+ position: axisData.position,
76
+ beginAtZero: chart.options.additionalAxesOptions.beginAtZero,
77
+ reverse: getReverse(),
78
+ suggestedMax: chart.options?.additionalAxesOptions?.suggestedMax,
79
+ suggestedMin: chart.options?.additionalAxesOptions?.suggestedMin,
80
+ min: isDirectionCompatibleWithAxisType
81
+ ? chart.options?.additionalAxesOptions?.min
82
+ : undefined,
83
+ max: isDirectionCompatibleWithAxisType
84
+ ? chart.options?.additionalAxesOptions?.max
85
+ : undefined,
86
+ title: {
87
+ display: axisData.label?.length || axisData.unit?.length,
88
+ text: axisData.label || axisData.unit,
89
+ padding: 0,
90
+ },
91
+ ticks: getTicks(),
92
+ grid: {
93
+ ...grid,
94
+ },
95
+ stacked: chart.options.additionalAxesOptions.stacked,
96
+ };
97
+ };
98
+
99
+ /**
100
+ * @param {import('./bar-chart.interface').IBarChartData} chart - chart data
101
+ * @param {'x'|'y'} axisType
102
+ */
103
+ const getBarChartAxes = (chart, axisType) => {
104
+ const axesData = chart.options.axes[axisType];
105
+ const axes = axesData.reduce((acc, curr, i) => {
106
+ const axisData = curr;
107
+ const color = curr.color || COLORS[i] || generateRandomColor(COLORS);
108
+ axisData.color = color;
109
+ axisData.position = curr.position || getAxisPosition(axisType, i);
110
+
111
+ const axis = getBarChartAxis(chart, axisType, axisData);
112
+ const axisId = i === 0 ? axisType : `${axisType}${i + 1}`;
113
+ return { ...acc, [axisId]: axis };
114
+ }, {});
115
+ return axes;
116
+ };
117
+
118
+ /**
119
+ * @param {import('./bar-chart.interface').IBarChartData} chart - chart data
120
+ */
121
+ const getBarChartScales = (chart) => {
122
+ const hasMultipleXAxes = chart.options.axes.x?.length > 1;
123
+ const hasMultipleYAxes = chart.options.axes.y?.length > 1;
124
+
125
+ const xAxesScales = hasMultipleXAxes
126
+ ? getBarChartAxes(chart, AxisType.X)
127
+ : { x: getBarChartAxis(chart, AxisType.X) };
128
+ const yAxesScales = hasMultipleYAxes
129
+ ? getBarChartAxes(chart, AxisType.Y)
130
+ : { y: getBarChartAxis(chart, AxisType.Y) };
131
+
132
+ return {
133
+ ...xAxesScales,
134
+ ...yAxesScales,
135
+ };
136
+ };
137
+
138
+ export default getBarChartScales;
@@ -0,0 +1,102 @@
1
+ import { toNum } from '@oliasoft-open-source/units';
2
+ import {
3
+ afterLabelCallback,
4
+ getTooltipLabel,
5
+ isVertical,
6
+ } from '../../helpers/chart-utils';
7
+ import { ChartHoverMode, TooltipLabel } from '../../helpers/enums';
8
+
9
+ /**
10
+ * @param {import('./bar-chart.interface').IBarChartOptions} options - bar chart options object
11
+ */
12
+ const getBarChartToolTips = (options) => {
13
+ const getTooltipLabels = (dataset) => {
14
+ const isDirectionVertical = isVertical(options.direction);
15
+ const { x, y } = options.axes;
16
+
17
+ const xIndex = dataset.xAxisID?.length > 1 ? dataset.xAxisID[1] - 1 : 0;
18
+ const yIndex = dataset.yAxisID?.length > 1 ? dataset.yAxisID[1] - 1 : 0;
19
+ const xAxis = isDirectionVertical ? x[xIndex] : y[yIndex];
20
+ const yAxis = isDirectionVertical ? y[yIndex] : x[xIndex];
21
+
22
+ return {
23
+ titleAxisLabel: xAxis?.label || '',
24
+ valueAxisLabel: yAxis?.label || '',
25
+ titleLabel: TooltipLabel.X,
26
+ valueLabel: TooltipLabel.Y,
27
+ titleUnit: xAxis?.unit || '',
28
+ valueUnit: yAxis?.unit || '',
29
+ };
30
+ };
31
+
32
+ const titleCallback = (tooltipItem, data) => {
33
+ const barLabel = tooltipItem[0]?.label || '';
34
+ const labels = getTooltipLabels(tooltipItem[0].dataset);
35
+ const { titleAxisLabel, titleUnit } = labels;
36
+ const axisLabel =
37
+ !titleAxisLabel && !titleUnit ? '' : `${titleAxisLabel || titleUnit}: `;
38
+ return axisLabel + barLabel;
39
+ };
40
+
41
+ const labelCallback = (tooltipItem) => {
42
+ const { showLabelsInTooltips } = options.tooltip;
43
+ let label = tooltipItem.dataset?.label || '';
44
+
45
+ const labels = getTooltipLabels(tooltipItem.dataset);
46
+ const { valueUnit, valueAxisLabel } = labels;
47
+
48
+ const getTooltipItemValue = () => {
49
+ const { formattedValue } = tooltipItem;
50
+ const formattedValueWithDotSeparator = formattedValue.replace(',', '.');
51
+ const labelNumber = toNum(formattedValueWithDotSeparator);
52
+
53
+ let labelNumberFormatted;
54
+ if (Math.abs(labelNumber) < 1) {
55
+ labelNumberFormatted = labelNumber.toPrecision(3);
56
+ } else {
57
+ labelNumberFormatted = Number.isInteger(labelNumber)
58
+ ? labelNumber
59
+ : labelNumber.toFixed(3);
60
+ }
61
+
62
+ let tooltipItemValue = '';
63
+ if (typeof tooltipItem.raw === 'number') {
64
+ tooltipItemValue = labelNumberFormatted;
65
+ }
66
+ if (Array.isArray(tooltipItem.raw)) {
67
+ tooltipItemValue = tooltipItem.raw.reduce((acc, curr, i) => {
68
+ return i === 0 ? `${acc} ${curr}` : `${acc}, ${curr}`;
69
+ }, '');
70
+ }
71
+ if (typeof tooltipItem.raw === 'object') {
72
+ tooltipItemValue = formattedValue;
73
+ }
74
+
75
+ return tooltipItemValue;
76
+ };
77
+
78
+ const hideValueAxisLabel =
79
+ label === valueAxisLabel || valueAxisLabel.includes(label);
80
+
81
+ const tooltipItemValue = getTooltipItemValue();
82
+ const unit = valueUnit ? `[${valueUnit}] ` : '';
83
+ const valAxisLabel = hideValueAxisLabel ? '' : valueAxisLabel;
84
+ const tooltipLabel = getTooltipLabel(tooltipItem, showLabelsInTooltips);
85
+
86
+ return `${label}: ${tooltipItemValue} ${unit}${valAxisLabel}${tooltipLabel}`;
87
+ };
88
+
89
+ return {
90
+ enabled: options.tooltip.tooltips,
91
+ mode: ChartHoverMode.Nearest,
92
+ intersect: true,
93
+ padding: 8,
94
+ callbacks: {
95
+ title: titleCallback,
96
+ label: labelCallback,
97
+ afterLabel: afterLabelCallback,
98
+ },
99
+ };
100
+ };
101
+
102
+ export default getBarChartToolTips;