@oliasoft-open-source/charts-library 2.17.5 → 2.18.0-beta-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 (84) hide show
  1. package/package.json +28 -36
  2. package/index.js +0 -14
  3. package/release-notes.md +0 -305
  4. package/src/assets/icons/line-and-point.svg +0 -1
  5. package/src/assets/icons/line-only.svg +0 -1
  6. package/src/assets/icons/list-hide.svg +0 -1
  7. package/src/assets/icons/point-only.svg +0 -1
  8. package/src/components/bar-chart/bar-chart-prop-types.js +0 -209
  9. package/src/components/bar-chart/bar-chart.interface.ts +0 -91
  10. package/src/components/bar-chart/bar-chart.jsx +0 -247
  11. package/src/components/bar-chart/bar-chart.module.less +0 -61
  12. package/src/components/bar-chart/get-bar-chart-data-labels.js +0 -42
  13. package/src/components/bar-chart/get-bar-chart-scales.js +0 -123
  14. package/src/components/bar-chart/get-bar-chart-tooltips.js +0 -102
  15. package/src/components/controls/axes-options/axes-options.jsx +0 -270
  16. package/src/components/line-chart/constants/default-translations.js +0 -24
  17. package/src/components/line-chart/constants/line-chart-consts.js +0 -12
  18. package/src/components/line-chart/controls/axes-options/action-types.js +0 -5
  19. package/src/components/line-chart/controls/axes-options/axes-options-form-state.js +0 -97
  20. package/src/components/line-chart/controls/controls.jsx +0 -174
  21. package/src/components/line-chart/controls/controls.module.less +0 -12
  22. package/src/components/line-chart/controls/drag-options.jsx +0 -112
  23. package/src/components/line-chart/controls/legend-options.jsx +0 -36
  24. package/src/components/line-chart/controls/line-options.jsx +0 -64
  25. package/src/components/line-chart/hooks/use-chart-functions.js +0 -257
  26. package/src/components/line-chart/hooks/use-chart-options.js +0 -181
  27. package/src/components/line-chart/hooks/use-chart-plugins.js +0 -35
  28. package/src/components/line-chart/hooks/use-toggle-handler.js +0 -33
  29. package/src/components/line-chart/initialize/config.js +0 -23
  30. package/src/components/line-chart/initialize/initialize-line-chart.js +0 -25
  31. package/src/components/line-chart/line-chart-prop-types.js +0 -292
  32. package/src/components/line-chart/line-chart.interface.ts +0 -121
  33. package/src/components/line-chart/line-chart.jsx +0 -161
  34. package/src/components/line-chart/line-chart.module.less +0 -77
  35. package/src/components/line-chart/plugins/chart-area-text-plugin.js +0 -246
  36. package/src/components/line-chart/plugins/line-chart.minor-gridlines-plugin.js +0 -88
  37. package/src/components/line-chart/plugins/plugin-constants.js +0 -11
  38. package/src/components/line-chart/state/action-types.js +0 -12
  39. package/src/components/line-chart/state/initial-state.js +0 -89
  40. package/src/components/line-chart/state/line-chart-reducer.js +0 -100
  41. package/src/components/line-chart/state/manage-state-in-local-storage.js +0 -86
  42. package/src/components/line-chart/state/use-chart-state.js +0 -141
  43. package/src/components/line-chart/utils/axis-scales/axis-scales.js +0 -165
  44. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-condition.js +0 -13
  45. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-data.js +0 -20
  46. package/src/components/line-chart/utils/datalabels-alignment/get-datalabels-position.js +0 -25
  47. package/src/components/line-chart/utils/generate-line-chart-datasets.js +0 -114
  48. package/src/components/line-chart/utils/get-axes-ranges-from-chart.js +0 -13
  49. package/src/components/line-chart/utils/get-line-chart-data-labels.js +0 -21
  50. package/src/components/line-chart/utils/get-line-chart-scales.js +0 -120
  51. package/src/components/line-chart/utils/get-line-chart-tooltips.js +0 -94
  52. package/src/components/line-chart/utils/line-chart-utils.js +0 -77
  53. package/src/components/line-chart/utils/translations/get-translations.js +0 -17
  54. package/src/components/pie-chart/pie-chart-prop-types.js +0 -111
  55. package/src/components/pie-chart/pie-chart-utils.js +0 -32
  56. package/src/components/pie-chart/pie-chart.interface.ts +0 -61
  57. package/src/components/pie-chart/pie-chart.jsx +0 -450
  58. package/src/components/pie-chart/pie-chart.module.less +0 -61
  59. package/src/components/scatter-chart/scatter-chart.intefrace.ts +0 -33
  60. package/src/components/scatter-chart/scatter-chart.jsx +0 -21
  61. package/src/components/scatter-chart/scatter-chart.module.less +0 -4
  62. package/src/helpers/chart-border-plugin.js +0 -19
  63. package/src/helpers/chart-consts.js +0 -64
  64. package/src/helpers/chart-interface.ts +0 -94
  65. package/src/helpers/chart-utils.js +0 -182
  66. package/src/helpers/container.jsx +0 -60
  67. package/src/helpers/disabled-context.js +0 -8
  68. package/src/helpers/enums.js +0 -96
  69. package/src/helpers/get-chart-annotation.js +0 -106
  70. package/src/helpers/get-custom-legend-plugin-example.js +0 -80
  71. package/src/helpers/get-draggableData.js +0 -32
  72. package/src/helpers/range/estimate-data-series-have-close-values.js +0 -54
  73. package/src/helpers/range/range.js +0 -100
  74. package/src/helpers/text.js +0 -6
  75. package/src/style/external.less +0 -4
  76. package/src/style/fonts/lato/Lato-Bold.woff2 +0 -0
  77. package/src/style/fonts/lato/Lato-BoldItalic.woff2 +0 -0
  78. package/src/style/fonts/lato/Lato-Italic.woff2 +0 -0
  79. package/src/style/fonts/lato/Lato-Regular.woff2 +0 -0
  80. package/src/style/fonts.less +0 -27
  81. package/src/style/global.less +0 -43
  82. package/src/style/reset/reset.less +0 -28
  83. package/src/style/shared.less +0 -11
  84. package/src/style/variables.less +0 -91
@@ -1,91 +0,0 @@
1
- import {
2
- IChartAnnotations,
3
- IChartDraggableData,
4
- IChartInteractions,
5
- IChartLegend,
6
- IChartStyling
7
- } from "../../helpers/chart-interface";
8
-
9
- export interface IChartOptions {
10
- enableZoom: boolean;
11
- enablePan: boolean;
12
- }
13
-
14
- export interface IBarChartGraph {
15
- showDataLabels: boolean;
16
- showMinorGridlines: boolean;
17
- }
18
-
19
- export interface IBarChartTooltip {
20
- tooltips: boolean;
21
- showLabelsInTooltips: boolean;
22
- }
23
-
24
- export interface IBarChartAdditionalAxesOptions {
25
- chartScaleType: 'linear' | 'logarithmic';
26
- reverse: boolean;
27
- beginAtZero: boolean;
28
- stepSize: number;
29
- stacked: boolean;
30
- suggestedMin: number;
31
- suggestedMax:number;
32
- min: number;
33
- max: number;
34
- }
35
-
36
- export interface IBarChartAxes<PositionType> {
37
- label: string;
38
- position: PositionType;
39
- color: string;
40
- unit: string;
41
- }
42
-
43
- export interface IBarChartOptions {
44
- title: string | string[];
45
- direction: 'vertical' | 'horizontal';
46
- axes: {
47
- x: IBarChartAxes<'top' | 'bottom'>[];
48
- y: IBarChartAxes<'left' | 'right'>[];
49
- };
50
- additionalAxesOptions: IBarChartAdditionalAxesOptions;
51
- chartStyling: IChartStyling;
52
- tooltip: IBarChartTooltip;
53
- graph: IBarChartGraph;
54
- annotations: IChartAnnotations;
55
- legend: IChartLegend;
56
- chartOptions: IChartOptions;
57
- interactions: IChartInteractions;
58
- draggableData :IChartDraggableData;
59
- }
60
-
61
- export interface IBarDataPoint {
62
- x: number;
63
- y: number;
64
- label?: string;
65
- }
66
-
67
- export interface IBarChartDataset {
68
- label?: string;
69
- data: number[] | number[][] | IBarDataPoint[];
70
- borderSkipped?: string;
71
- backgroundColor?: string[];
72
- borderColor?: string[];
73
- borderRadius?: number;
74
- borderWidth?: number;
75
- yAxisID?: string;
76
- stack?: number;
77
- }
78
-
79
- export interface IBarChartData {
80
- //TODO: revisit data interface definition after project is more stable
81
- testId: string | null;
82
- data: {
83
- labels?: string[],
84
- datasets: IBarChartDataset[]
85
- } | any;
86
- options: IBarChartOptions;
87
- }
88
-
89
- export interface IBarChartProps {
90
- chart: IBarChartData;
91
- }
@@ -1,247 +0,0 @@
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 };
@@ -1,61 +0,0 @@
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
- }
@@ -1,42 +0,0 @@
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;
@@ -1,123 +0,0 @@
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
-
9
- /**
10
- * @param {import('./bar-chart.interface').IBarChartData} chart - chart data
11
- * @param {'x'|'y'} axisType
12
- * @param {import('./bar-chart.interface').IBarChartAxes} [currentScales]
13
- */
14
- const getBarChartAxis = (chart, axisType, currentScales) => {
15
- const isDirectionVertical = isVertical(chart.options.direction);
16
-
17
- const axisData = currentScales || chart.options.axes[axisType][0];
18
- const isDirectionCompatibleWithAxisType =
19
- (isDirectionVertical && axisType === AxisType.Y) ||
20
- (!isDirectionVertical && axisType === AxisType.X);
21
-
22
- const grid = axisData?.gridLines || {};
23
-
24
- const getScaleType = () => {
25
- const scaleType = chart.data.labels ? ScaleType.Category : ScaleType.Linear;
26
- const axisWithScale = isDirectionVertical ? AxisType.X : AxisType.Y;
27
- return axisType === axisWithScale
28
- ? scaleType
29
- : chart.options.additionalAxesOptions.chartScaleType;
30
- };
31
-
32
- const getReverse = () => {
33
- const axisWithReverse = isDirectionVertical ? AxisType.Y : AxisType.X;
34
- return axisType === axisWithReverse
35
- ? chart.options.additionalAxesOptions.reverse
36
- : false;
37
- };
38
-
39
- const getTicks = () => {
40
- const areLogarithmicTicks =
41
- isDirectionCompatibleWithAxisType &&
42
- chart.options.additionalAxesOptions.chartScaleType ===
43
- ScaleType.Logarithmic;
44
-
45
- if (areLogarithmicTicks) {
46
- return {
47
- callback: (tick) => {
48
- return LOGARITHMIC_STEPS.includes(tick) ? tick.toLocaleString() : '';
49
- },
50
- };
51
- } else {
52
- return {
53
- stepSize:
54
- axisData.stepSize || chart.options.additionalAxesOptions.stepSize,
55
- };
56
- }
57
- };
58
-
59
- return {
60
- type: getScaleType(),
61
- position: axisData.position,
62
- beginAtZero: chart.options.additionalAxesOptions.beginAtZero,
63
- reverse: getReverse(),
64
- suggestedMax: chart.options?.additionalAxesOptions?.suggestedMax,
65
- suggestedMin: chart.options?.additionalAxesOptions?.suggestedMin,
66
- min: isDirectionCompatibleWithAxisType
67
- ? chart.options?.additionalAxesOptions?.min
68
- : undefined,
69
- max: isDirectionCompatibleWithAxisType
70
- ? chart.options?.additionalAxesOptions?.max
71
- : undefined,
72
- title: {
73
- display: axisData.label?.length || axisData.unit?.length,
74
- text: axisData.label || axisData.unit,
75
- padding: 0,
76
- },
77
- ticks: getTicks(),
78
- grid: {
79
- ...grid,
80
- },
81
- stacked: chart.options.additionalAxesOptions.stacked,
82
- };
83
- };
84
-
85
- /**
86
- * @param {import('./bar-chart.interface').IBarChartData} chart - chart data
87
- * @param {'x'|'y'} axisType
88
- */
89
- const getBarChartAxes = (chart, axisType) => {
90
- const axesData = chart.options.axes[axisType];
91
- const axes = axesData.reduce((acc, curr, i) => {
92
- const axisData = curr;
93
- const color = curr.color || COLORS[i] || generateRandomColor(COLORS);
94
- axisData.color = color;
95
- axisData.position = curr.position || getAxisPosition(axisType, i);
96
-
97
- const axis = getBarChartAxis(chart, axisType, axisData);
98
- return { ...acc, [axisType + (i + 1)]: axis };
99
- }, {});
100
- return axes;
101
- };
102
-
103
- /**
104
- * @param {import('./bar-chart.interface').IBarChartData} chart - chart data
105
- */
106
- const getBarChartScales = (chart) => {
107
- const hasMultipleXAxes = chart.options.axes.x?.length > 1;
108
- const hasMultipleYAxes = chart.options.axes.y?.length > 1;
109
-
110
- const xAxesScales = hasMultipleXAxes
111
- ? getBarChartAxes(chart, AxisType.X)
112
- : { x: getBarChartAxis(chart, AxisType.X) };
113
- const yAxesScales = hasMultipleYAxes
114
- ? getBarChartAxes(chart, AxisType.Y)
115
- : { y: getBarChartAxis(chart, AxisType.Y) };
116
-
117
- return {
118
- ...xAxesScales,
119
- ...yAxesScales,
120
- };
121
- };
122
-
123
- export default getBarChartScales;
@@ -1,102 +0,0 @@
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;