@oliasoft-open-source/charts-library 2.5.1 → 2.5.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/charts-library",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
4
4
  "description": "React Chart Library (based on Chart.js and react-chart-js-2)",
5
5
  "main": "index.js",
6
6
  "files": [
package/release-notes.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Charts Library Release Notes
2
2
 
3
+ ## 2.5.3
4
+
5
+ - Fixed datalabels position, set labels position inside chart area
6
+
7
+ ## 2.5.2
8
+
9
+ - Fix crashes when type a non number into the axis range - add validation in getLinechartAxis method
10
+
3
11
  ## 2.5.1
4
12
 
5
13
  - Added resize delay for performance
@@ -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
+ });
@@ -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
+ };
@@ -57,8 +57,12 @@ const getLineChartAxis = (options, axisType, state, currentScales, i = 0) => {
57
57
  reverse: axisType === AxisType.Y ? additionalAxesOptions.reverse : false,
58
58
  suggestedMax: additionalAxesOptions.suggestedMax,
59
59
  suggestedMin: additionalAxesOptions.suggestedMin,
60
- min: stateAxis.min?.value || additionalAxesOptions?.range?.[axisType]?.min,
61
- max: stateAxis.max?.value || additionalAxesOptions?.range?.[axisType]?.max,
60
+ min: stateAxis.min?.valid
61
+ ? stateAxis.min?.value
62
+ : additionalAxesOptions?.range?.[axisType]?.min,
63
+ max: stateAxis.max?.valid
64
+ ? stateAxis.max?.value
65
+ : additionalAxesOptions?.range?.[axisType]?.max,
62
66
  title: {
63
67
  display: axisData.label?.length,
64
68
  text: axisData.label,
@@ -104,6 +108,7 @@ const getLineChartScales = (options, state) => {
104
108
  const yAxes = hasMultipleYAxes
105
109
  ? getLineChartAxes(options, AxisType.Y, state)
106
110
  : { y: getLineChartAxis(options, AxisType.Y, state) };
111
+
107
112
  return {
108
113
  ...xAxes,
109
114
  ...yAxes,
@@ -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,
@@ -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;