@oliasoft-open-source/charts-library 2.5.2 → 2.5.4
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/package.json +1 -1
- package/release-notes.md +9 -1
- package/src/components/line-chart/datalabels-alignment/__tests__/get-alignment-condition.test.js +36 -0
- package/src/components/line-chart/datalabels-alignment/__tests__/get-alignment-data.test.js +29 -0
- package/src/components/line-chart/datalabels-alignment/__tests__/get-datalabels-position.test.js +21 -0
- package/src/components/line-chart/datalabels-alignment/get-alignment-condition.js +13 -0
- package/src/components/line-chart/datalabels-alignment/get-alignment-data.js +20 -0
- package/src/components/line-chart/datalabels-alignment/get-datalabels-position.js +24 -0
- package/src/components/line-chart/datalabels-alignment/set-chart-line-datalabels-alignment.js +19 -0
- package/src/components/line-chart/line-chart.jsx +16 -13
- package/src/components/line-chart/line-chart.stories.jsx +19 -0
package/package.json
CHANGED
package/release-notes.md
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
# Charts Library Release Notes
|
|
2
2
|
|
|
3
|
-
## 2.5.
|
|
3
|
+
## 2.5.4
|
|
4
|
+
|
|
5
|
+
- Fixed onPointHover event
|
|
6
|
+
|
|
7
|
+
## 2.5.3
|
|
8
|
+
|
|
9
|
+
- Fixed datalabels position, set labels position inside chart area
|
|
10
|
+
|
|
11
|
+
## 2.5.2
|
|
4
12
|
|
|
5
13
|
- Fix crashes when type a non number into the axis range - add validation in getLinechartAxis method
|
|
6
14
|
|
package/src/components/line-chart/datalabels-alignment/__tests__/get-alignment-condition.test.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getCondition } from '../get-alignment-condition';
|
|
2
|
+
|
|
3
|
+
describe('Datalabels alignment condition', () => {
|
|
4
|
+
test('should return overLeftSide = true', () => {
|
|
5
|
+
const result = getCondition(36, 325, 36, 1204, 325);
|
|
6
|
+
expect(result).toStrictEqual({
|
|
7
|
+
overLeftSide: true,
|
|
8
|
+
overRightSide: false,
|
|
9
|
+
overBottomSide: true,
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
test('should return overRightSide = true', () => {
|
|
13
|
+
const result = getCondition(1200, 325, 36, 1204, 325);
|
|
14
|
+
expect(result).toStrictEqual({
|
|
15
|
+
overLeftSide: false,
|
|
16
|
+
overRightSide: true,
|
|
17
|
+
overBottomSide: false,
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
test('should return overBottomSide = true', () => {
|
|
21
|
+
const result = getCondition(457, 250, 36, 1204, 325);
|
|
22
|
+
expect(result).toStrictEqual({
|
|
23
|
+
overLeftSide: false,
|
|
24
|
+
overRightSide: false,
|
|
25
|
+
overBottomSide: true,
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
test('should return all false', () => {
|
|
29
|
+
const result = getCondition(789, 100, 36, 1204, 325);
|
|
30
|
+
expect(result).toStrictEqual({
|
|
31
|
+
overLeftSide: false,
|
|
32
|
+
overRightSide: false,
|
|
33
|
+
overBottomSide: false,
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getAlignmentData } from '../get-alignment-data';
|
|
2
|
+
|
|
3
|
+
const mockedContext = {
|
|
4
|
+
chart: {
|
|
5
|
+
chartArea: {
|
|
6
|
+
left: 36,
|
|
7
|
+
right: 1204,
|
|
8
|
+
bottom: 325,
|
|
9
|
+
},
|
|
10
|
+
getDatasetMeta: jest.fn(),
|
|
11
|
+
},
|
|
12
|
+
dataIndex: 0,
|
|
13
|
+
datasetIndex: 0,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe('Datalabels alignment data', () => {
|
|
17
|
+
test('should return destructured data from context', () => {
|
|
18
|
+
const result = getAlignmentData(mockedContext);
|
|
19
|
+
expect(result).toEqual(
|
|
20
|
+
expect.objectContaining({
|
|
21
|
+
x: null,
|
|
22
|
+
y: null,
|
|
23
|
+
left: 36,
|
|
24
|
+
right: 1204,
|
|
25
|
+
bottom: 325,
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
});
|
package/src/components/line-chart/datalabels-alignment/__tests__/get-datalabels-position.test.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getPosition } from '../get-datalabels-position';
|
|
2
|
+
|
|
3
|
+
const mockedContext = {
|
|
4
|
+
chart: {
|
|
5
|
+
chartArea: {
|
|
6
|
+
left: 36,
|
|
7
|
+
right: 1204,
|
|
8
|
+
bottom: 325,
|
|
9
|
+
},
|
|
10
|
+
getDatasetMeta: jest.fn(),
|
|
11
|
+
},
|
|
12
|
+
dataIndex: 0,
|
|
13
|
+
datasetIndex: 0,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe('Datalabels alignment position', () => {
|
|
17
|
+
test('should return position', () => {
|
|
18
|
+
const result = getPosition()(mockedContext);
|
|
19
|
+
expect(result).toBe('right');
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getCondition } from './get-alignment-condition';
|
|
2
|
+
import { getAlignmentData } from './get-alignment-data';
|
|
3
|
+
|
|
4
|
+
/** returning position depends on condition
|
|
5
|
+
*
|
|
6
|
+
* @return {string} - position
|
|
7
|
+
*/
|
|
8
|
+
export const getPosition = () => {
|
|
9
|
+
return (context) => {
|
|
10
|
+
const { x, y, left, right, bottom } = getAlignmentData(context);
|
|
11
|
+
const {
|
|
12
|
+
overLeftSide = false,
|
|
13
|
+
overRightSide = false,
|
|
14
|
+
overBottomSide = false,
|
|
15
|
+
} = getCondition(x, y, left, right, bottom);
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
(overLeftSide && 'right') ||
|
|
19
|
+
(overRightSide && 'left') ||
|
|
20
|
+
(overBottomSide && 'end') ||
|
|
21
|
+
'start'
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getPosition } from './get-datalabels-position';
|
|
2
|
+
|
|
3
|
+
/** adjusts the position of the label depends on chart area
|
|
4
|
+
*
|
|
5
|
+
* @param {import('../components/bar-chart/bar-chart.interface').IBarChartOptions |
|
|
6
|
+
* import('../components/line-chart/line-chart.interface').ILineChartOptions} options - chart options object
|
|
7
|
+
* @return {object} - returning position, if label exist in datasets item
|
|
8
|
+
*/
|
|
9
|
+
export const setDataLabelsAlignment = (options) => {
|
|
10
|
+
const { graph = {} } = options || {};
|
|
11
|
+
const { showDataLabels = false } = graph;
|
|
12
|
+
|
|
13
|
+
return showDataLabels
|
|
14
|
+
? {
|
|
15
|
+
display: true,
|
|
16
|
+
align: getPosition(),
|
|
17
|
+
}
|
|
18
|
+
: {};
|
|
19
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useEffect, useReducer, useRef } from 'react';
|
|
1
|
+
import React, { useEffect, useReducer, useRef, useState } from 'react';
|
|
2
2
|
import {
|
|
3
3
|
CategoryScale,
|
|
4
4
|
Chart as ChartJS,
|
|
@@ -70,6 +70,7 @@ import {
|
|
|
70
70
|
PanZoomMode,
|
|
71
71
|
PointStyle,
|
|
72
72
|
} from '../../helpers/enums';
|
|
73
|
+
import { setDataLabelsAlignment } from './datalabels-alignment/set-chart-line-datalabels-alignment';
|
|
73
74
|
|
|
74
75
|
ChartJS.register(
|
|
75
76
|
LinearScale,
|
|
@@ -93,7 +94,7 @@ ChartJS.register(
|
|
|
93
94
|
const LineChart = (props) => {
|
|
94
95
|
setDefaultTheme();
|
|
95
96
|
const chartRef = useRef(null);
|
|
96
|
-
|
|
97
|
+
const [hoveredPoint, setHoveredPoint] = useState(null);
|
|
97
98
|
const chart = getDefaultProps(props);
|
|
98
99
|
const { options, testId } = chart;
|
|
99
100
|
const { headerComponent, subheaderComponent, table } = props;
|
|
@@ -231,6 +232,7 @@ const LineChart = (props) => {
|
|
|
231
232
|
: DEFAULT_LINE_POINT_RADIUS,
|
|
232
233
|
pointHoverRadius,
|
|
233
234
|
pointHitRadius: line.pointHitRadius || pointHoverRadius,
|
|
235
|
+
datalabels: setDataLabelsAlignment(options),
|
|
234
236
|
};
|
|
235
237
|
});
|
|
236
238
|
return generatedDatasets;
|
|
@@ -274,18 +276,19 @@ const LineChart = (props) => {
|
|
|
274
276
|
};
|
|
275
277
|
|
|
276
278
|
const onHover = (evt, hoveredItems, chartInstance) => {
|
|
277
|
-
if (
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
interactions.onPointUnhover(evt);
|
|
281
|
-
}
|
|
279
|
+
if (!hoveredItems?.length && interactions.onPointUnhover && hoveredPoint) {
|
|
280
|
+
setHoveredPoint(null);
|
|
281
|
+
interactions.onPointUnhover(evt);
|
|
282
282
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
283
|
+
|
|
284
|
+
if (hoveredItems?.length && interactions.onPointHover) {
|
|
285
|
+
const { index, datasetIndex } = hoveredItems[0];
|
|
286
|
+
const dataset = generatedDatasets[datasetIndex];
|
|
287
|
+
const point = dataset?.data[index];
|
|
288
|
+
|
|
289
|
+
if (point && hoveredPoint !== point) {
|
|
290
|
+
setHoveredPoint(point);
|
|
291
|
+
interactions.onPointHover(evt, datasetIndex, index, generatedDatasets);
|
|
289
292
|
}
|
|
290
293
|
}
|
|
291
294
|
};
|
|
@@ -593,3 +593,22 @@ SquareAspectRatioFillContainer.decorators = [
|
|
|
593
593
|
</div>
|
|
594
594
|
),
|
|
595
595
|
];
|
|
596
|
+
|
|
597
|
+
export const OnPointHover = Template.bind({});
|
|
598
|
+
OnPointHover.args = {
|
|
599
|
+
chart: {
|
|
600
|
+
...basicChart,
|
|
601
|
+
options: {
|
|
602
|
+
...basicChart.options,
|
|
603
|
+
interactions: {
|
|
604
|
+
onPointHover: (evt, datasetIndex, pointIndex, datasets) => {
|
|
605
|
+
console.log({ evt });
|
|
606
|
+
console.log(datasets[datasetIndex].data[pointIndex]);
|
|
607
|
+
},
|
|
608
|
+
onPointUnhover: () => {
|
|
609
|
+
console.log('No point hovered');
|
|
610
|
+
},
|
|
611
|
+
},
|
|
612
|
+
},
|
|
613
|
+
},
|
|
614
|
+
};
|