@oliasoft-open-source/charts-library 2.5.12 → 2.5.14

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.12",
3
+ "version": "2.5.14",
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.14
4
+
5
+ - Sync axis labels in chart Controls with props
6
+
7
+ ## 2.5.13
8
+
9
+ - fixed custom range inputs reset, multi-axis range placeholder values missing
10
+
3
11
  ## 2.5.12
4
12
 
5
13
  - change thousand separators in axis label from comma to space
@@ -15,6 +15,7 @@ import { RiRuler2Line } from 'react-icons/ri';
15
15
  const AxesOptionsPopover = ({
16
16
  onResetAxes,
17
17
  axes,
18
+ controlsAxesLabels,
18
19
  onSetAxisValue,
19
20
  panEnabled,
20
21
  scalesMaxMin,
@@ -31,8 +32,13 @@ const AxesOptionsPopover = ({
31
32
  {axes.map((axis, i) => {
32
33
  // TODO: Translate strings
33
34
  if (!axis.min || !axis.max) return null;
35
+
36
+ const axisLabel = controlsAxesLabels.find(
37
+ (el) => el.id === axis.id,
38
+ )?.label;
39
+
34
40
  return (
35
- <Field key={i} label={axis.label || axis.id || ''}>
41
+ <Field key={i} label={axisLabel || axis.id || ''}>
36
42
  <InputGroup small>
37
43
  <Input
38
44
  name="min"
@@ -113,6 +119,7 @@ const AxesOptionsPopover = ({
113
119
  export const AxesOptions = ({
114
120
  onResetAxes,
115
121
  axes,
122
+ controlsAxesLabels,
116
123
  onSetAxisValue,
117
124
  panEnabled,
118
125
  scalesMaxMin,
@@ -126,6 +133,7 @@ export const AxesOptions = ({
126
133
  <AxesOptionsPopover
127
134
  onResetAxes={onResetAxes}
128
135
  axes={axes}
136
+ controlsAxesLabels={controlsAxesLabels}
129
137
  onSetAxisValue={onSetAxisValue}
130
138
  panEnabled={panEnabled}
131
139
  scalesMaxMin={scalesMaxMin}
@@ -13,6 +13,7 @@ import styles from './controls.module.less';
13
13
 
14
14
  const Controls = ({
15
15
  axes,
16
+ controlsAxesLabels,
16
17
  chart,
17
18
  headerComponent,
18
19
  legendEnabled,
@@ -45,6 +46,7 @@ const Controls = ({
45
46
  <AxesOptions
46
47
  onResetAxes={onResetAxes}
47
48
  axes={axes}
49
+ controlsAxesLabels={controlsAxesLabels}
48
50
  onSetAxisValue={onSetAxisValue}
49
51
  panEnabled={panEnabled}
50
52
  scalesMaxMin={scalesMaxMin}
@@ -3,14 +3,18 @@ import {
3
3
  getAxisPosition,
4
4
  } from '../../helpers/chart-utils';
5
5
  import { COLORS, LOGARITHMIC_STEPS } from '../../helpers/chart-consts';
6
- import { truncateDecimals, validNumber } from './line-chart-utils';
6
+ import {
7
+ generateAxisId,
8
+ truncateDecimals,
9
+ validNumber,
10
+ } from './line-chart-utils';
7
11
  import { AxisType, ScaleType } from '../../helpers/enums';
8
12
 
9
13
  /**
10
14
  * @param {import('./line-chart.interface').ILineChartOptions} options - line chart options object
11
15
  * @param {'x'|'y'} axisType
12
16
  * @param {import('../../helpers/chart-interface').IInitialState} state - chart state object controlled by useReducer or similar
13
- * @param {import('./line-chart.interface').ILineChartAxes[]} [currentScales]
17
+ * @param {import('./line-chart.interface').ILineChartAxis[]} [currentScales]
14
18
  * @param {number} [i]
15
19
  */
16
20
  const getLineChartAxis = (options, axisType, state, currentScales, i = 0) => {
@@ -94,7 +98,8 @@ const getLineChartAxes = (options, axisType, state) => {
94
98
  axisData.position = curr.position || getAxisPosition(axisType, i);
95
99
 
96
100
  const axis = getLineChartAxis(options, axisType, state, axisData, i);
97
- return { ...acc, [axisType + (i + 1)]: axis };
101
+ const axisId = generateAxisId(axisType, i, true);
102
+ return { ...acc, [axisId]: axis };
98
103
  }, {});
99
104
  return axes;
100
105
  };
@@ -0,0 +1,12 @@
1
+ export const getScalesMinMax = (chartRef) => {
2
+ const { scales = {} } = chartRef.current || {};
3
+ return Object.keys(scales).reduce((acc, key) => {
4
+ return {
5
+ ...acc,
6
+ [key]: {
7
+ min: scales[key]?.min,
8
+ max: scales[key]?.max,
9
+ },
10
+ };
11
+ }, {});
12
+ };
@@ -169,3 +169,15 @@ export const truncateDecimals = (number, digits) => {
169
169
 
170
170
  return truncatedNum / multiplier;
171
171
  };
172
+
173
+ /**
174
+ *
175
+ * @param {'x'|'y'} axisType
176
+ * @param {number} [index] - axis index; optional, 0 by default
177
+ * @param {boolean} [hasMultiAxes] - optional, false by default
178
+ * @return {string} - e.g. x if chart has singular axes; x1, x2 - in case of multiple axes
179
+ */
180
+ export const generateAxisId = (axisType, index = 0, hasMultiAxes = false) => {
181
+ const i = hasMultiAxes ? index + 1 : '';
182
+ return `${axisType}${i}`;
183
+ };
@@ -45,18 +45,20 @@ export interface ILineChartAdditionalAxesOptions {
45
45
  };
46
46
  }
47
47
 
48
- export interface ILineChartAxes<PositionType> {
48
+ export interface ILineChartAxis<PositionType> {
49
49
  label: string;
50
50
  position: PositionType;
51
51
  color: string;
52
52
  }
53
53
 
54
+ export interface ILineChartAxes {
55
+ x: ILineChartAxis<'top' | 'bottom'>[];
56
+ y: ILineChartAxis<'left' | 'right'>[];
57
+ }
58
+
54
59
  export interface ILineChartOptions {
55
60
  title: string | string[];
56
- axes: {
57
- x: ILineChartAxes<'top' | 'bottom'>[];
58
- y: ILineChartAxes<'left' | 'right'>[];
59
- };
61
+ axes: ILineChartAxes;
60
62
  additionalAxesOptions: ILineChartAdditionalAxesOptions;
61
63
  chartStyling: IChartStyling;
62
64
  tooltip: ILineChartTooltip;
@@ -21,6 +21,7 @@ import styles from './line-chart.module.less';
21
21
  import { reducer } from './state/line-chart-reducer';
22
22
  import initialState from './state/initial-state';
23
23
  import {
24
+ SET_AXIS_MIN_MAX,
24
25
  SET_AXIS_VALUE,
25
26
  SET_POINTS_ZOOM_DEFAULTS,
26
27
  TOGGLE_ANNOTATION,
@@ -70,6 +71,8 @@ import {
70
71
  PanZoomMode,
71
72
  PointStyle,
72
73
  } from '../../helpers/enums';
74
+ import { getScalesMinMax } from './get-scales-min-max';
75
+ import { generateAxisId } from './line-chart-utils';
73
76
 
74
77
  ChartJS.register(
75
78
  LinearScale,
@@ -131,6 +134,9 @@ const LineChart = (props) => {
131
134
  if (chartOptions.enablePan !== true) {
132
135
  dispatch({ type: TOGGLE_PAN });
133
136
  }
137
+ if (chartRef) {
138
+ dispatch({ type: SET_AXIS_MIN_MAX, payload: getScalesMinMax(chartRef) });
139
+ }
134
140
  }, []);
135
141
 
136
142
  useEffect(() => {
@@ -338,50 +344,61 @@ const LineChart = (props) => {
338
344
  };
339
345
  });
340
346
  };
341
- const controlsAxes = getControlsAxes();
347
+ const controlsAxes = getControlsAxes(axes);
348
+
349
+ const getControlsAxesLabels = (propsAxes) => {
350
+ if (!Object.keys(propsAxes)?.length) {
351
+ return [];
352
+ }
353
+
354
+ const getAxesLabels = (axes, axisType) => {
355
+ if (!axes[axisType] || !axes[axisType]?.length) {
356
+ return [];
357
+ } else {
358
+ return axes[axisType].map((axisObj, index) => {
359
+ return {
360
+ id: generateAxisId(axisType, index, axes[axisType].length > 1),
361
+ label: axisObj?.label || '',
362
+ };
363
+ });
364
+ }
365
+ };
366
+
367
+ const axesLabels = [
368
+ ...getAxesLabels(propsAxes, AxisType.X),
369
+ ...getAxesLabels(propsAxes, AxisType.Y),
370
+ ];
371
+ return axesLabels;
372
+ };
373
+
374
+ const controlsAxesLabels = getControlsAxesLabels(props.chart.options.axes);
342
375
 
343
376
  const setAxisValuesInSettings = (chart) => {
377
+ const { scales = {} } = chart || {};
378
+ const getPayload = (scales) => {
379
+ return Object.keys(scales).reduce((acc, key) => {
380
+ return [
381
+ ...acc,
382
+ {
383
+ name: 'min',
384
+ value: scales[key]?.min ? scales[key].min : 0,
385
+ id: key,
386
+ },
387
+ {
388
+ name: 'max',
389
+ value: scales[key]?.max ? scales[key].max : 0,
390
+ id: key,
391
+ },
392
+ ];
393
+ }, []);
394
+ };
395
+
344
396
  dispatch({
345
397
  type: SET_AXIS_VALUE,
346
- payload: [
347
- {
348
- name: 'min',
349
- value: chart.scales?.x?.min ? chart.scales.x.min : 0,
350
- id: 'x',
351
- },
352
- {
353
- name: 'max',
354
- value: chart.scales?.x?.max ? chart.scales.x.max : 0,
355
- id: 'x',
356
- },
357
- {
358
- name: 'min',
359
- value: chart.scales?.y?.min ? chart.scales.y.min : 0,
360
- id: 'y',
361
- },
362
- {
363
- name: 'max',
364
- value: chart.scales?.y?.max ? chart.scales.y.max : 0,
365
- id: 'y',
366
- },
367
- ],
398
+ payload: getPayload(scales),
368
399
  });
369
400
  };
370
401
 
371
- const getScalesMaxMin = () => {
372
- const chart = chartRef.current;
373
- return {
374
- x: {
375
- min: chart?.scales?.x?.min,
376
- max: chart?.scales?.x?.max,
377
- },
378
- y: {
379
- min: chart?.scales?.y?.min,
380
- max: chart?.scales?.y?.max,
381
- },
382
- };
383
- };
384
-
385
402
  return (
386
403
  <div
387
404
  className={getClassName(chartStyling, styles)}
@@ -396,6 +413,7 @@ const LineChart = (props) => {
396
413
  >
397
414
  <Controls
398
415
  axes={controlsAxes}
416
+ controlsAxesLabels={controlsAxesLabels}
399
417
  chart={chart}
400
418
  headerComponent={headerComponent}
401
419
  legendEnabled={state.legendEnabled}
@@ -415,7 +433,7 @@ const LineChart = (props) => {
415
433
  onToggleZoom={() => dispatch({ type: TOGGLE_ZOOM })}
416
434
  panEnabled={state.panEnabled}
417
435
  pointsEnabled={state.pointsEnabled}
418
- scalesMaxMin={getScalesMaxMin()}
436
+ scalesMaxMin={state.scalesMinMax}
419
437
  showTable={state.showTable}
420
438
  subheaderComponent={subheaderComponent}
421
439
  table={table}
@@ -8,3 +8,4 @@ export const SET_AXIS_VALUE = 'SET_AXIS_VALUE';
8
8
  export const SET_POINTS_ZOOM_DEFAULTS = 'SET_POINTS_ZOOM_DEFAULTS';
9
9
  export const TOGGLE_ANNOTATION = 'TOGGLE_ANNOTATION';
10
10
  export const TOGGLE_TABLE = 'TOGGLE_TABLE';
11
+ export const SET_AXIS_MIN_MAX = 'SET_AXIS_MIN_MAX';
@@ -1,5 +1,6 @@
1
1
  import { AxisType } from '../../../helpers/enums';
2
2
  import { setAnnotations } from '../../../helpers/chart-utils';
3
+ import { generateAxisId } from '../line-chart-utils';
3
4
 
4
5
  const initialState = ({
5
6
  axes,
@@ -11,23 +12,25 @@ const initialState = ({
11
12
  annotationsData,
12
13
  }) => {
13
14
  /**
14
- * getAxis
15
+ * getStateAxesByType
15
16
  * @param {'x'|'y'} axisType
16
- * @return {{id: string, label: string}[] | []} returns array of objects describing all chart axis or empty array
17
+ * @return {{id: string}[] | []} returns array of objects describing all chart axis or empty array
17
18
  */
18
19
  const getStateAxesByType = (axisType) => {
19
- if (axes[axisType].length > 1) {
20
+ if (!axes[axisType]) {
21
+ return [];
22
+ }
23
+
24
+ if (axes[axisType]?.length > 1) {
20
25
  return axes[axisType].map((axisObj, index) => {
21
26
  return {
22
- id: `${axisType}${index}`,
23
- label: axisObj.label,
27
+ id: generateAxisId(axisType, index, axes[axisType].length > 1),
24
28
  };
25
29
  });
26
30
  } else {
27
31
  return [
28
32
  {
29
- id: axisType,
30
- label: axes[axisType][0].label,
33
+ id: generateAxisId(axisType),
31
34
  },
32
35
  ];
33
36
  }
@@ -16,6 +16,7 @@ import {
16
16
  SET_POINTS_ZOOM_DEFAULTS,
17
17
  TOGGLE_ANNOTATION,
18
18
  TOGGLE_TABLE,
19
+ SET_AXIS_MIN_MAX,
19
20
  } from './action-types';
20
21
  import { getAxisValue } from '../../../helpers/chart-utils';
21
22
 
@@ -137,6 +138,11 @@ export const reducer = (state, action) => {
137
138
  ...newState,
138
139
  showAnnotationLineIndex: updatedIndexes,
139
140
  };
141
+ case SET_AXIS_MIN_MAX:
142
+ return {
143
+ ...newState,
144
+ scalesMinMax: action.payload,
145
+ };
140
146
  default: {
141
147
  return newState;
142
148
  }