@oliasoft-open-source/charts-library 3.3.6-beta-5 → 3.3.6
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.
- package/dist/index.js +39230 -0
- package/dist/index.js.map +1 -0
- package/package.json +6 -1
- package/index.js +0 -14
- package/release-notes.md +0 -378
- package/src/assets/circle.svg +0 -1
- package/src/assets/icons/line-and-point.svg +0 -1
- package/src/assets/icons/line-only.svg +0 -1
- package/src/assets/icons/list-hide.svg +0 -1
- package/src/assets/icons/point-only.svg +0 -1
- package/src/assets/rect.svg +0 -1
- package/src/assets/rectRot.svg +0 -1
- package/src/assets/triangle.svg +0 -1
- package/src/components/bar-chart/bar-chart-prop-types.js +0 -209
- package/src/components/bar-chart/bar-chart.interface.ts +0 -98
- package/src/components/bar-chart/bar-chart.jsx +0 -247
- package/src/components/bar-chart/bar-chart.module.less +0 -61
- package/src/components/bar-chart/get-bar-chart-data-labels.js +0 -42
- package/src/components/bar-chart/get-bar-chart-scales.js +0 -138
- package/src/components/bar-chart/get-bar-chart-tooltips.js +0 -102
- package/src/components/controls/axes-options/axes-options.jsx +0 -271
- package/src/components/line-chart/constants/default-translations.js +0 -24
- package/src/components/line-chart/constants/line-chart-consts.js +0 -12
- package/src/components/line-chart/controls/axes-options/action-types.js +0 -5
- package/src/components/line-chart/controls/axes-options/axes-options-form-state.js +0 -97
- package/src/components/line-chart/controls/controls-portal.jsx +0 -12
- package/src/components/line-chart/controls/controls.jsx +0 -176
- package/src/components/line-chart/controls/controls.module.less +0 -12
- package/src/components/line-chart/controls/drag-options.jsx +0 -124
- package/src/components/line-chart/controls/line-options.jsx +0 -64
- package/src/components/line-chart/hooks/use-chart-functions.js +0 -261
- package/src/components/line-chart/hooks/use-chart-options.js +0 -181
- package/src/components/line-chart/hooks/use-chart-plugins.js +0 -35
- package/src/components/line-chart/hooks/use-toggle-handler.js +0 -33
- package/src/components/line-chart/initialize/config.js +0 -23
- package/src/components/line-chart/initialize/initialize-line-chart.js +0 -25
- package/src/components/line-chart/legend/legend-constants.js +0 -1
- package/src/components/line-chart/legend/legend-dropzone.jsx +0 -74
- package/src/components/line-chart/legend/legend-item.jsx +0 -105
- package/src/components/line-chart/legend/legend-panel.jsx +0 -86
- package/src/components/line-chart/legend/legend.jsx +0 -54
- package/src/components/line-chart/legend/legend.module.less +0 -192
- package/src/components/line-chart/line-chart-prop-types.js +0 -298
- package/src/components/line-chart/line-chart.interface.ts +0 -128
- package/src/components/line-chart/line-chart.jsx +0 -180
- package/src/components/line-chart/line-chart.module.less +0 -78
- package/src/components/line-chart/plugins/chart-area-text-plugin.js +0 -246
- package/src/components/line-chart/plugins/line-chart.minor-gridlines-plugin.js +0 -88
- package/src/components/line-chart/plugins/plugin-constants.js +0 -11
- package/src/components/line-chart/state/action-types.js +0 -12
- package/src/components/line-chart/state/initial-state.js +0 -89
- package/src/components/line-chart/state/line-chart-reducer.js +0 -101
- package/src/components/line-chart/state/manage-state-in-local-storage.js +0 -86
- package/src/components/line-chart/state/use-chart-state.js +0 -141
- package/src/components/line-chart/utils/axis-formatting/axis-formatting.js +0 -69
- package/src/components/line-chart/utils/axis-scales/axis-scales.js +0 -165
- package/src/components/line-chart/utils/datalabels-alignment/get-alignment-condition.js +0 -13
- package/src/components/line-chart/utils/datalabels-alignment/get-alignment-data.js +0 -20
- package/src/components/line-chart/utils/datalabels-alignment/get-datalabels-position.js +0 -25
- package/src/components/line-chart/utils/generate-line-chart-datasets.js +0 -122
- package/src/components/line-chart/utils/get-axes-ranges-from-chart.js +0 -13
- package/src/components/line-chart/utils/get-line-chart-data-labels.js +0 -21
- package/src/components/line-chart/utils/get-line-chart-scales.js +0 -117
- package/src/components/line-chart/utils/get-line-chart-tooltips.js +0 -94
- package/src/components/line-chart/utils/line-chart-utils.js +0 -62
- package/src/components/line-chart/utils/translations/get-translations.js +0 -17
- package/src/components/pie-chart/pie-chart-prop-types.js +0 -111
- package/src/components/pie-chart/pie-chart-utils.js +0 -32
- package/src/components/pie-chart/pie-chart.interface.ts +0 -61
- package/src/components/pie-chart/pie-chart.jsx +0 -458
- package/src/components/pie-chart/pie-chart.module.less +0 -61
- package/src/components/scatter-chart/scatter-chart.intefrace.ts +0 -33
- package/src/components/scatter-chart/scatter-chart.jsx +0 -21
- package/src/components/scatter-chart/scatter-chart.module.less +0 -4
- package/src/helpers/chart-border-plugin.js +0 -19
- package/src/helpers/chart-consts.js +0 -64
- package/src/helpers/chart-interface.ts +0 -94
- package/src/helpers/chart-utils.js +0 -178
- package/src/helpers/container.jsx +0 -60
- package/src/helpers/disabled-context.js +0 -8
- package/src/helpers/enums.js +0 -107
- package/src/helpers/get-chart-annotation.js +0 -102
- package/src/helpers/get-custom-legend-plugin-example.js +0 -80
- package/src/helpers/get-draggableData.js +0 -32
- package/src/helpers/range/estimate-data-series-have-close-values.js +0 -54
- package/src/helpers/range/range.js +0 -100
- package/src/helpers/text.js +0 -6
- package/src/style/external.less +0 -4
- package/src/style/fonts/lato/Lato-Bold.woff2 +0 -0
- package/src/style/fonts/lato/Lato-BoldItalic.woff2 +0 -0
- package/src/style/fonts/lato/Lato-Italic.woff2 +0 -0
- package/src/style/fonts/lato/Lato-Regular.woff2 +0 -0
- package/src/style/fonts.less +0 -27
- package/src/style/global.less +0 -43
- package/src/style/reset/reset.less +0 -28
- package/src/style/shared.less +0 -11
- package/src/style/variables.less +0 -91
- package/vite.config.mjs +0 -49
- package/vitest.config.mjs +0 -8
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
2
|
-
import { isEqual } from 'lodash';
|
|
3
|
-
import { storeChartStateInStorage } from './manage-state-in-local-storage';
|
|
4
|
-
import { SAVE_INITIAL_AXES_RANGES, UPDATE_AXES_RANGES } from './action-types';
|
|
5
|
-
import { getAxesRangesFromChart } from '../utils/get-axes-ranges-from-chart';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Hook for toggling the visibility of the custom legend.
|
|
9
|
-
*
|
|
10
|
-
* @param memoState - The memoized state object.
|
|
11
|
-
* @param memoOptions - The memoized options object.
|
|
12
|
-
*/
|
|
13
|
-
const useToggleCustomLegendVisibility = (memoState, memoOptions) => {
|
|
14
|
-
useEffect(() => {
|
|
15
|
-
if (memoOptions.legend.customLegend.customLegendPlugin) {
|
|
16
|
-
// Get the parent element of the custom legend container and toggle its visibility based on the state.
|
|
17
|
-
const parent = document.getElementById(
|
|
18
|
-
memoOptions.legend.customLegend.customLegendContainerID,
|
|
19
|
-
);
|
|
20
|
-
parent.style.visibility = memoState.legendEnabled ? 'visible' : 'hidden';
|
|
21
|
-
}
|
|
22
|
-
}, [
|
|
23
|
-
memoOptions.legend.customLegend.customLegendPlugin,
|
|
24
|
-
memoState.legendEnabled,
|
|
25
|
-
]);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Hook for storing the chart state in local storage.
|
|
30
|
-
*
|
|
31
|
-
* @param memoState - The memoized state object.
|
|
32
|
-
* @param persistenceId - The chart persistenceId.
|
|
33
|
-
*/
|
|
34
|
-
const useStoreChartStateInStorage = (memoState, persistenceId) => {
|
|
35
|
-
useEffect(() => {
|
|
36
|
-
// Store the chart state in local storage.
|
|
37
|
-
storeChartStateInStorage(memoState, persistenceId);
|
|
38
|
-
}, [
|
|
39
|
-
memoState.panEnabled,
|
|
40
|
-
memoState.lineEnabled,
|
|
41
|
-
memoState.pointsEnabled,
|
|
42
|
-
memoState.legendEnabled,
|
|
43
|
-
memoState.enableDragPoints,
|
|
44
|
-
memoState.zoomEnabled,
|
|
45
|
-
]);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Hook for updating the axes ranges in the chart state.
|
|
50
|
-
*
|
|
51
|
-
* @param range - The range to the chart.
|
|
52
|
-
* @param dispatch - The dispatch function for state management.
|
|
53
|
-
*/
|
|
54
|
-
const useUpdateAxesRanges = (range, dispatch) => {
|
|
55
|
-
const prevRange = useRef();
|
|
56
|
-
|
|
57
|
-
const updateAxesRanges = useCallback(() => {
|
|
58
|
-
if (range && range.x && range.y && !isEqual(range, prevRange.current)) {
|
|
59
|
-
const axes = Object.entries(range).map(([key, { min, max }]) => ({
|
|
60
|
-
id: key,
|
|
61
|
-
min: min ?? 0,
|
|
62
|
-
max: max ?? 0,
|
|
63
|
-
}));
|
|
64
|
-
dispatch({
|
|
65
|
-
type: UPDATE_AXES_RANGES,
|
|
66
|
-
payload: { axes },
|
|
67
|
-
});
|
|
68
|
-
prevRange.current = range;
|
|
69
|
-
}
|
|
70
|
-
}, [range]);
|
|
71
|
-
|
|
72
|
-
useEffect(() => {
|
|
73
|
-
updateAxesRanges();
|
|
74
|
-
}, [updateAxesRanges]);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* A custom hook to save the initial axes ranges of a chart when the generated datasets change.
|
|
79
|
-
*
|
|
80
|
-
* @param {Object} chartRef - A reference to the chart object.
|
|
81
|
-
* @param {Array} axes - An array of axes configurations.
|
|
82
|
-
* @param {Array} generatedDatasets - An array of generated datasets for the chart.
|
|
83
|
-
* @param {Function} dispatch - A dispatch function from the useContext hook for managing the state.
|
|
84
|
-
*/
|
|
85
|
-
const useSaveInitialAxesRanges = (
|
|
86
|
-
chartRef,
|
|
87
|
-
axes,
|
|
88
|
-
generatedDatasets,
|
|
89
|
-
dispatch,
|
|
90
|
-
) => {
|
|
91
|
-
const prevGeneratedDatasets = useRef();
|
|
92
|
-
|
|
93
|
-
const saveInitialAxesRanges = useCallback(() => {
|
|
94
|
-
if (
|
|
95
|
-
chartRef &&
|
|
96
|
-
!isEqual(generatedDatasets, prevGeneratedDatasets.current)
|
|
97
|
-
) {
|
|
98
|
-
const initialAxesRanges = getAxesRangesFromChart(chartRef, axes);
|
|
99
|
-
dispatch({
|
|
100
|
-
type: SAVE_INITIAL_AXES_RANGES,
|
|
101
|
-
payload: { initialAxesRanges },
|
|
102
|
-
});
|
|
103
|
-
prevGeneratedDatasets.current = generatedDatasets;
|
|
104
|
-
}
|
|
105
|
-
}, [chartRef, generatedDatasets]);
|
|
106
|
-
|
|
107
|
-
useEffect(() => {
|
|
108
|
-
saveInitialAxesRanges();
|
|
109
|
-
}, [saveInitialAxesRanges, generatedDatasets]);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Hook for managing the state of the chart.
|
|
114
|
-
*
|
|
115
|
-
* @param chartRef - The reference to the chart.
|
|
116
|
-
* @param options - The chart options.
|
|
117
|
-
* @param state - The chart state.
|
|
118
|
-
* @param {Array} generatedDatasets - An array of generated datasets for the chart.
|
|
119
|
-
* @param dispatch - The dispatch function for state management.
|
|
120
|
-
* @param persistenceId - The chart persistenceId.
|
|
121
|
-
*/
|
|
122
|
-
export const useChartState = ({
|
|
123
|
-
chartRef,
|
|
124
|
-
options,
|
|
125
|
-
state,
|
|
126
|
-
generatedDatasets,
|
|
127
|
-
dispatch,
|
|
128
|
-
persistenceId,
|
|
129
|
-
}) => {
|
|
130
|
-
const memoState = useMemo(() => state, [state]);
|
|
131
|
-
const memoOptions = useMemo(() => options, [options]);
|
|
132
|
-
const {
|
|
133
|
-
additionalAxesOptions: { range },
|
|
134
|
-
axes,
|
|
135
|
-
} = memoOptions;
|
|
136
|
-
|
|
137
|
-
useStoreChartStateInStorage(memoState, persistenceId);
|
|
138
|
-
useToggleCustomLegendVisibility(memoState, memoOptions);
|
|
139
|
-
useUpdateAxesRanges(range, dispatch);
|
|
140
|
-
useSaveInitialAxesRanges(chartRef, axes, generatedDatasets, dispatch);
|
|
141
|
-
};
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { displayNumber, round } from '@oliasoft-open-source/units';
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
formatAxisLabelNumbers is cloned from the internal logic of chart.js
|
|
5
|
-
We needed to override how it formats thousands separators, while retaining
|
|
6
|
-
its other formatting decisions to avoid any breaking changes. We can change
|
|
7
|
-
this more or deviate further from the chart.js built-in implementation in the
|
|
8
|
-
future, depending on our requirements.
|
|
9
|
-
|
|
10
|
-
The function comes from
|
|
11
|
-
- https://github.com/chartjs/Chart.js/blob/master/src/helpers/helpers.intl.ts
|
|
12
|
-
- https://github.com/chartjs/Chart.js/blob/master/src/core/core.ticks.js#L28
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const calculateDelta = (tickValue, ticks) => {
|
|
16
|
-
// Figure out how many digits to show
|
|
17
|
-
// The space between the first two ticks might be smaller than normal spacing
|
|
18
|
-
let delta =
|
|
19
|
-
ticks.length > 3
|
|
20
|
-
? ticks[2].value - ticks[1].value
|
|
21
|
-
: ticks[1].value - ticks[0].value;
|
|
22
|
-
|
|
23
|
-
// If we have a number like 2.5 as the delta, figure out how many decimal places we need
|
|
24
|
-
if (Math.abs(delta) >= 1 && tickValue !== Math.floor(tickValue)) {
|
|
25
|
-
// not an integer
|
|
26
|
-
delta = tickValue - Math.floor(tickValue);
|
|
27
|
-
}
|
|
28
|
-
return delta;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const formatAxisLabelNumbers = (tickValue, ticks, locale = 'en-gb') => {
|
|
32
|
-
if (tickValue === 0 || tickValue === null) {
|
|
33
|
-
return '0'; // never show decimal places for 0
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let notation;
|
|
37
|
-
let delta = tickValue; // This is used when there are less than 2 ticks as the tick interval.
|
|
38
|
-
|
|
39
|
-
if (ticks.length > 1) {
|
|
40
|
-
// all ticks are small or there huge numbers; use scientific notation
|
|
41
|
-
const maxTick = Math.max(
|
|
42
|
-
Math.abs(ticks[0].value),
|
|
43
|
-
Math.abs(ticks[ticks.length - 1].value),
|
|
44
|
-
);
|
|
45
|
-
if (maxTick < 1e-4 || maxTick > 1e7) {
|
|
46
|
-
notation = 'scientific';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
delta = calculateDelta(tickValue, ticks);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const logDelta = Math.log10(Math.abs(delta));
|
|
53
|
-
|
|
54
|
-
// When datasets have values approaching Number.MAX_VALUE, the tick calculations might result in
|
|
55
|
-
// infinity and eventually NaN. Passing NaN for minimumFractionDigits or maximumFractionDigits
|
|
56
|
-
// will make the number formatter throw. So instead we check for isNaN and use a fallback value.
|
|
57
|
-
//
|
|
58
|
-
// toFixed has a max of 20 decimal places
|
|
59
|
-
const numDecimal = isNaN(logDelta)
|
|
60
|
-
? 1
|
|
61
|
-
: Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0);
|
|
62
|
-
// upwards adjust rounding of scientific coefficient to minimum of 1 (stops all axis ticks having same value)
|
|
63
|
-
const roundScientificCoefficient = numDecimal === 0 ? 1 : numDecimal;
|
|
64
|
-
|
|
65
|
-
return displayNumber(round(tickValue, numDecimal), {
|
|
66
|
-
scientific: notation === 'scientific',
|
|
67
|
-
roundScientificCoefficient,
|
|
68
|
-
});
|
|
69
|
-
};
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import getLineChartScales from '../get-line-chart-scales';
|
|
2
|
-
import { AxisType } from '../../../../helpers/enums';
|
|
3
|
-
import { getAxisTypeFromKey } from '../line-chart-utils';
|
|
4
|
-
import { estimateDataSeriesHaveCloseValues } from '../../../../helpers/range/estimate-data-series-have-close-values';
|
|
5
|
-
import { getSuggestedAxisRange } from '../../../../helpers/range/range';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
Function that counts the number of occurences of "x" and "y" in an array and returns the one with the highest count.
|
|
9
|
-
@param {Array} scalesKeys - An array of strings that may contain "x" or "y".
|
|
10
|
-
@return {string} - Returns "x" if "x" occurs more times, "y" if "y" occurs more times, or null if they occur the same number of times.
|
|
11
|
-
*/
|
|
12
|
-
const checkMultiAxis = (scalesKeys) => {
|
|
13
|
-
let counts = {
|
|
14
|
-
x: 0,
|
|
15
|
-
y: 0,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
scalesKeys.forEach((axeKey) => {
|
|
19
|
-
const key = getAxisTypeFromKey(axeKey);
|
|
20
|
-
counts[key]++;
|
|
21
|
-
});
|
|
22
|
-
const res =
|
|
23
|
-
(counts.x > counts.y && AxisType.X) ||
|
|
24
|
-
(counts.y > counts.x && AxisType.Y) ||
|
|
25
|
-
(counts.x === counts.y && null);
|
|
26
|
-
|
|
27
|
-
return res;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
Function that returns an object containing all the values for a specific key in an array of objects,
|
|
32
|
-
grouped by the unique values of another key in the same objects.
|
|
33
|
-
@param {Array} data - An array of objects to search for the keys.
|
|
34
|
-
@return {Object} - Returns an object with keys representing the unique values for the annotationAxis key in the data array,
|
|
35
|
-
and values representing an array of all values for the value key in the data array.
|
|
36
|
-
*/
|
|
37
|
-
const getAnnotationsData = (data) => {
|
|
38
|
-
return data.reduce((acc, obj) => {
|
|
39
|
-
return {
|
|
40
|
-
...acc,
|
|
41
|
-
[obj.annotationAxis]: [...(acc[obj.annotationAxis] || []), obj.value],
|
|
42
|
-
};
|
|
43
|
-
}, {});
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* return data for each axes
|
|
48
|
-
* @function getAxesData
|
|
49
|
-
* @return {object}
|
|
50
|
-
*/
|
|
51
|
-
const getAxesData = (scalesKeys, datasets, annotationsData) => {
|
|
52
|
-
const allData =
|
|
53
|
-
datasets?.reduce((acc, item) => [...acc, ...item.data], []) ?? [];
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* return data by key and depends on axes count
|
|
57
|
-
* @function getData
|
|
58
|
-
* @return {object}
|
|
59
|
-
*/
|
|
60
|
-
const getData = () => {
|
|
61
|
-
return scalesKeys.reduce((acc, axeKey) => {
|
|
62
|
-
const key = getAxisTypeFromKey(axeKey);
|
|
63
|
-
const data = getAnnotationsData(annotationsData);
|
|
64
|
-
const formData = allData
|
|
65
|
-
?.map((val) => val?.[key])
|
|
66
|
-
.concat(data[key] || []);
|
|
67
|
-
|
|
68
|
-
return {
|
|
69
|
-
...acc,
|
|
70
|
-
[axeKey]: [...new Set(formData)],
|
|
71
|
-
};
|
|
72
|
-
}, {});
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
Function that returns an object containing the unique values for a multi axis key in an array of objects.
|
|
77
|
-
@return {Object} - Returns an object with key representing the found key in the scalesKeys array,
|
|
78
|
-
and values representing an array of unique values for that key in the datasets array. If no key is found, an empty object is returned.
|
|
79
|
-
*/
|
|
80
|
-
const getDataForMultiAxes = () => {
|
|
81
|
-
const multiAxesKey = checkMultiAxis(scalesKeys);
|
|
82
|
-
const axes = scalesKeys?.filter((key) => key?.includes(multiAxesKey)) ?? [];
|
|
83
|
-
const data = getAnnotationsData(annotationsData);
|
|
84
|
-
const [first, second] = axes;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Reduces an array of objects and returns an array of values from the specified key
|
|
88
|
-
* @param {Array} arr - The array of objects to be reduced
|
|
89
|
-
* @param {string} key - The key to extract the values from
|
|
90
|
-
* @returns {Array} - An array of values from the specified key
|
|
91
|
-
*/
|
|
92
|
-
const reduceData = (arr, key) =>
|
|
93
|
-
arr.reduce((acc, { [key]: value }) => [...acc, value], []);
|
|
94
|
-
|
|
95
|
-
return datasets.reduce(
|
|
96
|
-
(acc, obj) => {
|
|
97
|
-
const include = Object.values(obj).some((val) => axes.includes(val));
|
|
98
|
-
const key = include ? second : first;
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
...acc,
|
|
102
|
-
[key]: [
|
|
103
|
-
...new Set([
|
|
104
|
-
...acc[key],
|
|
105
|
-
...reduceData(obj?.data, multiAxesKey),
|
|
106
|
-
...((!include && data?.[multiAxesKey]) || []),
|
|
107
|
-
]),
|
|
108
|
-
],
|
|
109
|
-
};
|
|
110
|
-
},
|
|
111
|
-
{ [first]: [], [second]: [] },
|
|
112
|
-
);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
...getData(),
|
|
117
|
-
...getDataForMultiAxes(),
|
|
118
|
-
};
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Auto scales axis ranges (mix, max) if no explicit range is set
|
|
123
|
-
* - overrides some edge cases not handled well by default chart.js
|
|
124
|
-
* - supports optional padding when `autoAxisPadding` is set
|
|
125
|
-
* - otherwise does not set min/max (falls back to chart.js default)
|
|
126
|
-
*
|
|
127
|
-
* @function autoScale
|
|
128
|
-
* @return {object} scales
|
|
129
|
-
*/
|
|
130
|
-
export const autoScale = (options, state, generatedDatasets) => {
|
|
131
|
-
const { additionalAxesOptions = {}, annotations = {} } = options || {};
|
|
132
|
-
const { annotationsData = [] } = annotations || {};
|
|
133
|
-
const scales = getLineChartScales(options, state) || {};
|
|
134
|
-
|
|
135
|
-
if (
|
|
136
|
-
!additionalAxesOptions?.autoAxisPadding &&
|
|
137
|
-
!estimateDataSeriesHaveCloseValues(generatedDatasets)
|
|
138
|
-
) {
|
|
139
|
-
return scales;
|
|
140
|
-
}
|
|
141
|
-
const scalesKeys = Object.keys(scales) ?? [];
|
|
142
|
-
const data =
|
|
143
|
-
getAxesData(scalesKeys, generatedDatasets, annotationsData) ?? {};
|
|
144
|
-
|
|
145
|
-
const adjustedScales = scalesKeys?.reduce((acc, key) => {
|
|
146
|
-
const { min: propMin = undefined, max: propMax = undefined } = scales[key];
|
|
147
|
-
const { min, max } = getSuggestedAxisRange({
|
|
148
|
-
data: data[key],
|
|
149
|
-
beginAtZero: additionalAxesOptions?.beginAtZero,
|
|
150
|
-
autoAxisPadding: additionalAxesOptions?.autoAxisPadding,
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
const res = {
|
|
154
|
-
[key]: {
|
|
155
|
-
...scales[key],
|
|
156
|
-
min: propMin ?? min,
|
|
157
|
-
max: propMax ?? max,
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
return { ...acc, ...res };
|
|
162
|
-
}, {});
|
|
163
|
-
|
|
164
|
-
return adjustedScales ?? scales;
|
|
165
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/** returning boolean condition depends on label position and chart area
|
|
2
|
-
*
|
|
3
|
-
* @return {object} - returning object with boolean props
|
|
4
|
-
*/
|
|
5
|
-
export const getCondition = (x, y, left, right, bottom) => {
|
|
6
|
-
const threshold = 100;
|
|
7
|
-
const overLeftSide = x - threshold <= left;
|
|
8
|
-
const overRightSide = x + threshold >= right;
|
|
9
|
-
const overBottomSide =
|
|
10
|
-
x + threshold >= left && x + threshold < right && y + threshold >= bottom;
|
|
11
|
-
|
|
12
|
-
return { overLeftSide, overRightSide, overBottomSide };
|
|
13
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/** returning destructured data from context
|
|
2
|
-
*
|
|
3
|
-
* @return {object}
|
|
4
|
-
*/
|
|
5
|
-
export const getAlignmentData = (context) => {
|
|
6
|
-
const { chart = {}, dataIndex = 0, datasetIndex = 0 } = context || {};
|
|
7
|
-
const { chartArea = {} } = chart;
|
|
8
|
-
const { left = null, right = null, bottom = null } = chartArea;
|
|
9
|
-
|
|
10
|
-
const meta = chart.getDatasetMeta(datasetIndex);
|
|
11
|
-
const { x = null, y = null } = meta?.data?.[dataIndex] || {};
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
x,
|
|
15
|
-
y,
|
|
16
|
-
left,
|
|
17
|
-
right,
|
|
18
|
-
bottom,
|
|
19
|
-
};
|
|
20
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { getCondition } from './get-alignment-condition';
|
|
2
|
-
import { getAlignmentData } from './get-alignment-data';
|
|
3
|
-
import { AlignOptions } from '../../../../helpers/enums';
|
|
4
|
-
|
|
5
|
-
/** returning position depends on condition
|
|
6
|
-
*
|
|
7
|
-
* @return {string} - position
|
|
8
|
-
*/
|
|
9
|
-
export const getPosition = () => {
|
|
10
|
-
return (context) => {
|
|
11
|
-
const { x, y, left, right, bottom } = getAlignmentData(context);
|
|
12
|
-
const {
|
|
13
|
-
overLeftSide = false,
|
|
14
|
-
overRightSide = false,
|
|
15
|
-
overBottomSide = false,
|
|
16
|
-
} = getCondition(x, y, left, right, bottom);
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
(overLeftSide && AlignOptions.Right) ||
|
|
20
|
-
(overRightSide && AlignOptions.Left) ||
|
|
21
|
-
(overBottomSide && AlignOptions.End) ||
|
|
22
|
-
AlignOptions.Start
|
|
23
|
-
);
|
|
24
|
-
};
|
|
25
|
-
};
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ANNOTATION_DASH,
|
|
3
|
-
BORDER_WIDTH,
|
|
4
|
-
COLORS,
|
|
5
|
-
} from '../../../helpers/chart-consts';
|
|
6
|
-
import {
|
|
7
|
-
BORDER_JOIN_STYLE,
|
|
8
|
-
DEFAULT_BACKGROUND_COLOR,
|
|
9
|
-
DEFAULT_BORDER_WIDTH,
|
|
10
|
-
DEFAULT_HOVER_RADIUS,
|
|
11
|
-
DEFAULT_LINE_POINT_RADIUS,
|
|
12
|
-
DEFAULT_POINT_RADIUS,
|
|
13
|
-
} from '../constants/line-chart-consts';
|
|
14
|
-
import { generateRandomColor } from '../../../helpers/chart-utils';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Generates line chart datasets based on the provided datasets, state, and options.
|
|
18
|
-
*
|
|
19
|
-
* @param {Array} datasets - The initial datasets for the line chart.
|
|
20
|
-
* @param {Object} state - The state object containing chart settings (e.g., lineEnabled, pointsEnabled, axes).
|
|
21
|
-
* @param {Object} options - The options object containing additional settings (e.g., annotations, graph).
|
|
22
|
-
* @param {Object} translations - The getTranslations object with the label property
|
|
23
|
-
* @returns {Array} - The generated line chart datasets with applied settings and configurations.
|
|
24
|
-
*/
|
|
25
|
-
export const generateLineChartDatasets = (
|
|
26
|
-
datasets,
|
|
27
|
-
state,
|
|
28
|
-
options,
|
|
29
|
-
{ label },
|
|
30
|
-
) => {
|
|
31
|
-
const copyDataset = [...datasets];
|
|
32
|
-
const { annotations, graph } = options;
|
|
33
|
-
const {
|
|
34
|
-
controlAnnotation,
|
|
35
|
-
showAnnotations,
|
|
36
|
-
annotationsData = [],
|
|
37
|
-
} = annotations;
|
|
38
|
-
// Add annotations to dataset to have them appear in legend.
|
|
39
|
-
if (controlAnnotation && showAnnotations) {
|
|
40
|
-
const annotationDatasets = annotationsData.map((annotation, index) => {
|
|
41
|
-
const color = annotation.color ?? COLORS[index];
|
|
42
|
-
const borderDash = ANNOTATION_DASH;
|
|
43
|
-
const borderWidth = BORDER_WIDTH.INITIAL;
|
|
44
|
-
// Create an annotation dataset
|
|
45
|
-
return {
|
|
46
|
-
isAnnotation: true,
|
|
47
|
-
annotationType: annotation.type,
|
|
48
|
-
label: annotation.label,
|
|
49
|
-
annotationIndex: index,
|
|
50
|
-
backgroundColor: color,
|
|
51
|
-
pointBackgroundColor: color,
|
|
52
|
-
borderColor: color,
|
|
53
|
-
borderDash,
|
|
54
|
-
borderWidth,
|
|
55
|
-
data: [{}],
|
|
56
|
-
};
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Add annotation datasets to the copyDataset array
|
|
60
|
-
copyDataset.push(...annotationDatasets);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Map over the copyDataset array and generate line chart datasets with applied settings and configurations
|
|
64
|
-
return copyDataset.map((line, i) => {
|
|
65
|
-
// Destructure properties from the line, state, and graph objects
|
|
66
|
-
const {
|
|
67
|
-
formation,
|
|
68
|
-
data = [],
|
|
69
|
-
pointRadius,
|
|
70
|
-
pointHoverRadius,
|
|
71
|
-
borderWidth,
|
|
72
|
-
borderColor,
|
|
73
|
-
backgroundColor,
|
|
74
|
-
pointBackgroundColor,
|
|
75
|
-
borderDash,
|
|
76
|
-
} = line ?? {};
|
|
77
|
-
const { lineEnabled, pointsEnabled, axes = [] } = state ?? {};
|
|
78
|
-
const { lineTension, spanGaps } = graph ?? {};
|
|
79
|
-
|
|
80
|
-
// Adjust the start and end points of the line if it has a formation flag
|
|
81
|
-
if (formation) {
|
|
82
|
-
const axesMin = axes[0]?.min;
|
|
83
|
-
const axesMax = axes[0]?.max;
|
|
84
|
-
const [startPoint, midPointWithLabel, endPoint] = data;
|
|
85
|
-
|
|
86
|
-
if (axesMin && startPoint?.x) {
|
|
87
|
-
data[0].x = Math.max(axesMin, startPoint.x);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (axesMax && endPoint?.x) {
|
|
91
|
-
data[2].x = Math.min(axesMax, endPoint.x);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Remove invalid falsy data points (OW-9855)
|
|
96
|
-
const filteredData = data.filter(Boolean) || [];
|
|
97
|
-
|
|
98
|
-
const linePointRadius = parseFloat(pointRadius) || DEFAULT_POINT_RADIUS;
|
|
99
|
-
const hoverRadius = parseFloat(pointHoverRadius) || DEFAULT_HOVER_RADIUS;
|
|
100
|
-
const indexedColor = COLORS[i];
|
|
101
|
-
|
|
102
|
-
// Return the dataset with applied settings and configurations
|
|
103
|
-
return {
|
|
104
|
-
...line,
|
|
105
|
-
label: line.label || `${label} ${i + 1}`,
|
|
106
|
-
data: filteredData,
|
|
107
|
-
showLine: lineEnabled,
|
|
108
|
-
lineTension,
|
|
109
|
-
spanGaps,
|
|
110
|
-
borderWidth: parseFloat(borderWidth) || DEFAULT_BORDER_WIDTH,
|
|
111
|
-
borderDash: borderDash || [],
|
|
112
|
-
borderJoinStyle: BORDER_JOIN_STYLE,
|
|
113
|
-
borderColor: borderColor ?? indexedColor ?? generateRandomColor(COLORS),
|
|
114
|
-
backgroundColor: backgroundColor ?? DEFAULT_BACKGROUND_COLOR,
|
|
115
|
-
pointBackgroundColor:
|
|
116
|
-
pointBackgroundColor ?? indexedColor ?? generateRandomColor(COLORS),
|
|
117
|
-
pointRadius: pointsEnabled ? linePointRadius : DEFAULT_LINE_POINT_RADIUS,
|
|
118
|
-
pointHoverRadius: hoverRadius,
|
|
119
|
-
pointHitRadius: line.pointHitRadius ?? hoverRadius,
|
|
120
|
-
};
|
|
121
|
-
});
|
|
122
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export const getAxesRangesFromChart = (chartRef, axes) => {
|
|
2
|
-
const { scales = {} } = chartRef.current || {};
|
|
3
|
-
return Object.entries(scales).map(([key, { min, max }], i) => {
|
|
4
|
-
const axesArray = axes.x.concat(axes.y);
|
|
5
|
-
const unit = axesArray?.[i]?.unit;
|
|
6
|
-
return {
|
|
7
|
-
id: key,
|
|
8
|
-
min,
|
|
9
|
-
max,
|
|
10
|
-
...(unit ? { unit } : {}),
|
|
11
|
-
};
|
|
12
|
-
});
|
|
13
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { AUTO } from '../../../helpers/chart-consts';
|
|
2
|
-
import { getPosition } from './datalabels-alignment/get-datalabels-position';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* adjusts the position of the label depends on chart area
|
|
6
|
-
*
|
|
7
|
-
* @param {import('../line-chart.interface').ILineChartOptions} options - line chart options object
|
|
8
|
-
* @return {object} - returning position, if label exist in datasets item
|
|
9
|
-
*/
|
|
10
|
-
const getLineChartDataLabels = (options) => {
|
|
11
|
-
return options.graph.showDataLabels
|
|
12
|
-
? {
|
|
13
|
-
display: AUTO,
|
|
14
|
-
align: getPosition(),
|
|
15
|
-
formatter: (value, context) =>
|
|
16
|
-
context.dataset.data[context.dataIndex].label || '',
|
|
17
|
-
}
|
|
18
|
-
: { display: false };
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export default getLineChartDataLabels;
|