@oliasoft-open-source/charts-library 2.1.5 → 2.1.7

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.1.5",
3
+ "version": "2.1.7",
4
4
  "description": "React Chart Library (based on Chart.js and react-chart-js-2)",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
2
2
 
3
3
  export const BarChartPropTypes = {
4
4
  chart: PropTypes.shape({
5
+ testId: PropTypes.string,
5
6
  data: PropTypes.object.isRequired,
6
7
  options: PropTypes.shape({
7
8
  title: PropTypes.oneOfType([
@@ -106,6 +107,7 @@ export const getDefaultProps = (props) => {
106
107
  props.chart.options.interactions = props.chart.options.interactions || {};
107
108
  // Set defaults for missing properties
108
109
  const chart = {
110
+ testId: props.chart.testId ?? null,
109
111
  data: props.chart.data,
110
112
  options: {
111
113
  title: props.chart.options.title || '',
@@ -71,6 +71,7 @@ export interface IBarChartDataset {
71
71
 
72
72
  export interface IBarChartData {
73
73
  //TODO: revisit data interface definition after project is more stable
74
+ testId: string | null;
74
75
  data: {
75
76
  labels?: string[],
76
77
  datasets: IBarChartDataset[]
@@ -79,7 +79,7 @@ const BarChart = (props) => {
79
79
  const chartRef = useRef(null);
80
80
  const [pointHover, setPointHover] = useState(false);
81
81
  const chart = getDefaultProps(props);
82
- const { options } = chart;
82
+ const { options, testId } = chart;
83
83
  const { annotations, chartStyling, interactions, graph } = chart.options;
84
84
 
85
85
  const [visibleAnnotationsIndices, setVisibleAnnotationsIndices] = useState(
@@ -199,6 +199,7 @@ const BarChart = (props) => {
199
199
  width: chartStyling.width || AUTO,
200
200
  height: chartStyling.height || AUTO,
201
201
  }}
202
+ data-testid={testId}
202
203
  >
203
204
  <Bar
204
205
  ref={chartRef}
@@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
2
2
 
3
3
  export const LineChartPropTypes = {
4
4
  chart: PropTypes.shape({
5
+ testId: PropTypes.string,
5
6
  data: PropTypes.object.isRequired,
6
7
  options: PropTypes.shape({
7
8
  title: PropTypes.oneOfType([
@@ -117,6 +118,7 @@ export const getDefaultProps = (props) => {
117
118
  props.chart.options.interactions = props.chart.options.interactions || {};
118
119
  // Set defaults for missing properties
119
120
  const chart = {
121
+ testId: props.chart.testId ?? null,
120
122
  data: props.chart.data,
121
123
  options: {
122
124
  title: props.chart.options.title || '',
@@ -91,6 +91,7 @@ export interface ILineChartDataset {
91
91
 
92
92
  export interface ILineChartData {
93
93
  //TODO: revisit data interface definition after project is more stable
94
+ testId: string | null;
94
95
  data: {
95
96
  labels?: string[],
96
97
  datasets: ILineChartDataset[]
@@ -105,7 +105,7 @@ const LineChart = (props) => {
105
105
  const chartRef = useRef(null);
106
106
  let pointHover = false;
107
107
  const chart = getDefaultProps(props);
108
- const { options } = chart;
108
+ const { options, testId } = chart;
109
109
 
110
110
  const {
111
111
  additionalAxesOptions,
@@ -409,6 +409,7 @@ const LineChart = (props) => {
409
409
  tabIndex={0} //eslint-disable-line jsx-a11y/no-noninteractive-tabindex
410
410
  onKeyDown={handleKeyDown}
411
411
  onKeyUp={handleKeyUp}
412
+ data-testid={testId}
412
413
  >
413
414
  <div className={styles.zoomForm}>
414
415
  <Controls
@@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
2
2
 
3
3
  export const PieChartPropTypes = {
4
4
  chart: PropTypes.shape({
5
+ testId: PropTypes.string,
5
6
  data: PropTypes.shape({
6
7
  labels: PropTypes.arrayOf(PropTypes.string),
7
8
  datasets: PropTypes.arrayOf(PropTypes.object).isRequired,
@@ -58,6 +59,7 @@ export const getDefaultProps = (props) => {
58
59
  props.chart.options.interactions = props.chart.options.interactions || {};
59
60
  // Set defaults for missing properties
60
61
  const chart = {
62
+ testId: props.chart.testId ?? null,
61
63
  data: props.chart.data,
62
64
  options: {
63
65
  title: props.chart.options.title || '',
@@ -52,6 +52,7 @@ export interface IChartData {
52
52
  }
53
53
 
54
54
  export interface IChartData {
55
+ testId: string | null;
55
56
  data: IChartData;
56
57
  options: IOptions;
57
58
  }
@@ -54,11 +54,12 @@ const PieChart = (props) => {
54
54
  const chart = getDefaultProps(props);
55
55
  const chartRef = useRef(null);
56
56
  const [pointHover, setPointHover] = useState(false);
57
+ const { data, options, testId } = chart;
57
58
 
58
59
  const generateDatasets = (datasets) => {
59
60
  const copyDataset = JSON.parse(JSON.stringify(datasets));
60
61
 
61
- if (chart.options.graph.stacked) {
62
+ if (options.graph.stacked) {
62
63
  const generatedDataset = {
63
64
  label: copyDataset[0].label,
64
65
  backgroundColor: [],
@@ -66,7 +67,7 @@ const PieChart = (props) => {
66
67
  borderWidth: parseFloat(copyDataset[0].borderWidth) || '1',
67
68
  data: [],
68
69
  };
69
- chart.data.labels.map((label, labelIndex) => {
70
+ data.labels.map((label, labelIndex) => {
70
71
  copyDataset.map((arc, arcIndex) => {
71
72
  generatedDataset.data.push(arc.data[labelIndex]);
72
73
  const { backgroundColor } = arc;
@@ -95,7 +96,7 @@ const PieChart = (props) => {
95
96
  const colorDarkmode = pieDataset.data.map(
96
97
  (pie, i) => colorsDarkmode[i] || generateRandomColor(),
97
98
  );
98
- const color = chart.options.chartStyling.darkMode
99
+ const color = options.chartStyling.darkMode
99
100
  ? colorDarkmode
100
101
  : colorLightmode;
101
102
  const borderColor = pieDataset.borderColor || color;
@@ -113,14 +114,14 @@ const PieChart = (props) => {
113
114
  return generatedDatasets;
114
115
  };
115
116
 
116
- const generatedDatasets = generateDatasets(chart.data.datasets);
117
+ const generatedDatasets = generateDatasets(data.datasets);
117
118
 
118
119
  const getTitle = () => {
119
- return chart.options.title !== ''
120
+ return options.title !== ''
120
121
  ? {
121
122
  display: true,
122
- text: chart.options.title,
123
- color: chart.options.chartStyling.darkMode
123
+ text: options.title,
124
+ color: options.chartStyling.darkMode
124
125
  ? defaults.darkModeColor
125
126
  : undefined,
126
127
  }
@@ -132,14 +133,14 @@ const PieChart = (props) => {
132
133
  const { datasets } = chartInstance.data;
133
134
  const { index } = legendItem;
134
135
 
135
- if (chart.options.graph.stacked) {
136
- if (chart.options.legend.useDataset) {
136
+ if (options.graph.stacked) {
137
+ if (options.legend.useDataset) {
137
138
  for (
138
139
  let i = 0;
139
140
  i < chartInstance.config._config.data.datasets[0].data.length;
140
141
  i++
141
142
  ) {
142
- if (i % chart.data.datasets.length === index) {
143
+ if (i % data.datasets.length === index) {
143
144
  const meta = chartInstance.getDatasetMeta(0);
144
145
  const arc = meta.data[i];
145
146
  arc.hidden = !arc.hidden;
@@ -151,7 +152,7 @@ const PieChart = (props) => {
151
152
  i < chartInstance.config._config.data.datasets[0].data.length;
152
153
  i++
153
154
  ) {
154
- if (parseInt(i / chart.data.datasets.length) === index) {
155
+ if (parseInt(i / data.datasets.length) === index) {
155
156
  const meta = chartInstance.getDatasetMeta(0);
156
157
  const arc = meta.data[i];
157
158
  arc.hidden = !arc.hidden;
@@ -159,12 +160,12 @@ const PieChart = (props) => {
159
160
  }
160
161
  }
161
162
  } else {
162
- if (chart.options.legend.useDataset) {
163
+ if (options.legend.useDataset) {
163
164
  const meta = chartInstance.getDatasetMeta(index);
164
165
  meta.hidden = !meta.hidden;
165
166
  } else {
166
167
  if (datasets.length > 1) {
167
- for (let i = 0; i < chart.data.datasets.length; i++) {
168
+ for (let i = 0; i < data.datasets.length; i++) {
168
169
  const meta = chartInstance.getDatasetMeta(i);
169
170
 
170
171
  const arc = meta.data[index];
@@ -178,11 +179,8 @@ const PieChart = (props) => {
178
179
  }
179
180
  }
180
181
 
181
- if (chart.options.interactions.onLegendClick)
182
- chart.options.interactions.onLegendClick(
183
- legendItem?.text,
184
- legendItem.hidden,
185
- );
182
+ if (options.interactions.onLegendClick)
183
+ options.interactions.onLegendClick(legendItem?.text, legendItem.hidden);
186
184
 
187
185
  chartInstance.update();
188
186
  };
@@ -195,16 +193,16 @@ const PieChart = (props) => {
195
193
  const onHover = (evt, hoveredItems) => {
196
194
  if (pointHover && !hoveredItems?.length) {
197
195
  setPointHover(false);
198
- if (chart.options.interactions.onPieUnhover) {
199
- chart.options.interactions.onPieUnhover(evt);
196
+ if (options.interactions.onPieUnhover) {
197
+ options.interactions.onPieUnhover(evt);
200
198
  }
201
199
  }
202
200
  if (!pointHover && hoveredItems?.length) {
203
201
  setPointHover(true);
204
- if (chart.options.interactions.onPieHover) {
202
+ if (options.interactions.onPieHover) {
205
203
  const { index, datasetIndex } = hoveredItems[0];
206
204
  const generatedDataset = generatedDatasets;
207
- chart.options.interactions.onPieHover(
205
+ options.interactions.onPieHover(
208
206
  evt,
209
207
  datasetIndex,
210
208
  index,
@@ -215,7 +213,7 @@ const PieChart = (props) => {
215
213
  };
216
214
 
217
215
  const getDatalabels = () => {
218
- return chart.options.graph.showDataLabels
216
+ return options.graph.showDataLabels
219
217
  ? {
220
218
  display: 'auto',
221
219
  align: 'center',
@@ -223,7 +221,7 @@ const PieChart = (props) => {
223
221
  formatter: (value, context) => {
224
222
  if (
225
223
  context.chart.getDatasetMeta(
226
- chart.options.graph.stacked ? 0 : context.datasetIndex,
224
+ options.graph.stacked ? 0 : context.datasetIndex,
227
225
  ).data[context.dataIndex].hidden
228
226
  )
229
227
  return '';
@@ -246,7 +244,7 @@ const PieChart = (props) => {
246
244
  : label;
247
245
  return dataLabel;
248
246
  },
249
- color: chart.options.chartStyling.darkMode
247
+ color: options.chartStyling.darkMode
250
248
  ? defaults.darkModeColor
251
249
  : undefined,
252
250
  }
@@ -257,23 +255,22 @@ const PieChart = (props) => {
257
255
 
258
256
  const getLegend = () => {
259
257
  return {
260
- position: chart.options.legend.position,
261
- display: chart.options.legend.display,
262
- align: chart.options.legend.align,
258
+ position: options.legend.position,
259
+ display: options.legend.display,
260
+ align: options.legend.align,
263
261
  labels: {
264
262
  generateLabels(chartInstance) {
265
- if (chart.options.graph.stacked) {
263
+ if (options.graph.stacked) {
266
264
  const meta = chartInstance.getDatasetMeta(0);
267
- if (chart.options.legend.useDataset) {
268
- return chart.data.datasets.map((data, i) => {
269
- const { label } =
270
- chart.data.datasets[i % chart.data.datasets.length];
265
+ if (options.legend.useDataset) {
266
+ return data.datasets.map((data, i) => {
267
+ const { label } = data.datasets[i % data.datasets.length];
271
268
  const arc = meta.data[i];
272
269
  const text = arc.hidden
273
270
  ? label.split('').reduce((acc, curr) => acc + '-', '')
274
271
  : label;
275
- const backgroundColor = chart.data.datasets[i]?.backgroundColor;
276
- const borderColor = chart.data.datasets[i]?.borderColor;
272
+ const backgroundColor = data.datasets[i]?.backgroundColor;
273
+ const borderColor = data.datasets[i]?.borderColor;
277
274
  return {
278
275
  text,
279
276
  fillStyle:
@@ -288,14 +285,14 @@ const PieChart = (props) => {
288
285
  };
289
286
  });
290
287
  } else {
291
- return chart.data.labels.map((data, i) => {
292
- const label = chart.data.labels[i];
293
- const arc = meta.data[parseInt(i * chart.data.datasets.length)];
288
+ return data.labels.map((data, i) => {
289
+ const label = data.labels[i];
290
+ const arc = meta.data[parseInt(i * data.datasets.length)];
294
291
  const text = arc.hidden
295
292
  ? label.split('').reduce((acc, curr) => acc + '-', '')
296
293
  : label;
297
- const backgroundColor = chart.data.datasets[0]?.backgroundColor;
298
- const borderColor = chart.data.datasets[0]?.borderColor;
294
+ const backgroundColor = data.datasets[0]?.backgroundColor;
295
+ const borderColor = data.datasets[0]?.borderColor;
299
296
  return {
300
297
  text,
301
298
  fillStyle:
@@ -311,14 +308,14 @@ const PieChart = (props) => {
311
308
  });
312
309
  }
313
310
  }
314
- if (chart.options.legend.useDataset) {
315
- return chart.data.datasets.map((dataset, i) => {
311
+ if (options.legend.useDataset) {
312
+ return data.datasets.map((dataset, i) => {
316
313
  const meta = chartInstance.getDatasetMeta(i);
317
314
  const text = meta.hidden
318
315
  ? dataset.label.split('').reduce((acc, curr) => acc + '-', '')
319
316
  : dataset.label;
320
- const backgroundColor = chart.data.datasets[i]?.backgroundColor;
321
- const borderColor = chart.data.datasets[i]?.borderColor;
317
+ const backgroundColor = data.datasets[i]?.backgroundColor;
318
+ const borderColor = data.datasets[i]?.borderColor;
322
319
  return {
323
320
  text,
324
321
  fillStyle:
@@ -332,14 +329,14 @@ const PieChart = (props) => {
332
329
  };
333
330
  });
334
331
  } else {
335
- return chart.data.labels.map((label, i) => {
332
+ return data.labels.map((label, i) => {
336
333
  const meta = chartInstance.getDatasetMeta(0);
337
334
  const arc = meta.data[i];
338
335
  const text = arc.hidden
339
336
  ? label.split('').reduce((acc, curr) => acc + '-', '')
340
337
  : label;
341
- const backgroundColor = chart.data.datasets[0]?.backgroundColor;
342
- const borderColor = chart.data.datasets[0]?.borderColor;
338
+ const backgroundColor = data.datasets[0]?.backgroundColor;
339
+ const borderColor = data.datasets[0]?.borderColor;
343
340
  return {
344
341
  text,
345
342
  fillStyle:
@@ -358,12 +355,12 @@ const PieChart = (props) => {
358
355
  boxHeight: 6,
359
356
  boxWidth: 6,
360
357
  usePointStyle: true,
361
- color: chart.options.chartStyling.darkMode
358
+ color: options.chartStyling.darkMode
362
359
  ? defaults.darkModeColor
363
360
  : undefined,
364
361
  filter: (item, data) => {
365
- if (!chart.options.legend.useDataset) return true;
366
- return !chart.data.datasets[item.index]?.hideLegend;
362
+ if (!options.legend.useDataset) return true;
363
+ return !data.datasets[item.index]?.hideLegend;
367
364
  },
368
365
  },
369
366
  onClick: legendClick,
@@ -375,31 +372,31 @@ const PieChart = (props) => {
375
372
  callbacks: {
376
373
  title: (tooltipItem) => {
377
374
  const { dataIndex } = tooltipItem[0];
378
- const index = chart.options.graph.stacked
379
- ? parseInt(dataIndex / chart.data.labels.length)
375
+ const index = options.graph.stacked
376
+ ? parseInt(dataIndex / data.labels.length)
380
377
  : dataIndex;
381
- const label = chart.data.labels[index];
378
+ const label = data.labels[index];
382
379
  return `${label}`;
383
380
  },
384
381
  label: (tooltipItem) => {
385
382
  const { dataIndex } = tooltipItem;
386
- const datasetIndex = chart.options.graph.stacked
387
- ? dataIndex % chart.data.datasets.length
383
+ const datasetIndex = options.graph.stacked
384
+ ? dataIndex % data.datasets.length
388
385
  : tooltipItem.datasetIndex;
389
386
  const dataLabel =
390
- chart.data.datasets.length > 1
391
- ? `${chart.data.datasets[datasetIndex]?.label}: `
387
+ data.datasets.length > 1
388
+ ? `${data.datasets[datasetIndex]?.label}: `
392
389
  : '';
393
390
  const value = tooltipItem.dataset.data[dataIndex];
394
391
  const labelValue =
395
392
  typeof value === 'object'
396
393
  ? `${value.value || ''} ${
397
- chart.options.tooltip.showLabelsInTooltips && value.label
394
+ options.tooltip.showLabelsInTooltips && value.label
398
395
  ? '(' + value.label + ')'
399
396
  : ''
400
397
  }`
401
398
  : value;
402
- const unit = chart.data?.yUnit ? `[${chart.data?.yUnit}]` : '';
399
+ const unit = data?.yUnit ? `[${data?.yUnit}]` : '';
403
400
  return `${dataLabel} ${labelValue} ${unit}`;
404
401
  },
405
402
  },
@@ -409,37 +406,36 @@ const PieChart = (props) => {
409
406
  <div
410
407
  className={cx(
411
408
  styles.chart,
412
- !chart.options.chartStyling.width || !chart.options.chartStyling.height
413
- ? chart.options.chartStyling.staticChartHeight
409
+ !options.chartStyling.width || !options.chartStyling.height
410
+ ? options.chartStyling.staticChartHeight
414
411
  ? styles.fixedHeight
415
412
  : styles.stretchHeight
416
413
  : '',
417
414
  )}
418
415
  style={{
419
- width: chart.options.chartStyling.width || 'auto',
420
- height: chart.options.chartStyling.height || 'auto',
416
+ width: options.chartStyling.width || 'auto',
417
+ height: options.chartStyling.height || 'auto',
421
418
  }}
419
+ data-testid={testId}
422
420
  >
423
421
  <Pie
424
422
  ref={chartRef}
425
423
  data={{
426
424
  datasets: generatedDatasets,
427
- labels: chart.options.graph.stacked ? undefined : chart.data.labels,
425
+ labels: options.graph.stacked ? undefined : data.labels,
428
426
  }}
429
427
  options={{
430
- cutout: chart.options.graph.cutout,
428
+ cutout: options.graph.cutout,
431
429
  onClick,
432
430
  responsive: true, // Defaults to true, should be removed
433
- maintainAspectRatio: chart.options.chartStyling.maintainAspectRatio,
431
+ maintainAspectRatio: options.chartStyling.maintainAspectRatio,
434
432
  animation: {
435
- duration: chart.options.chartStyling.performanceMode ? 0 : 1000,
433
+ duration: options.chartStyling.performanceMode ? 0 : 1000,
436
434
  },
437
435
  hover: {
438
436
  mode: 'nearest',
439
437
  intersect: 'true',
440
- animationDuration: chart.options.chartStyling.performanceMode
441
- ? 0
442
- : 400,
438
+ animationDuration: options.chartStyling.performanceMode ? 0 : 400,
443
439
  },
444
440
  onHover,
445
441
  elements: {
@@ -21,6 +21,7 @@ export interface IScatterChartDataset {
21
21
  }
22
22
 
23
23
  export interface IScatterChartData {
24
+ testId: string | null;
24
25
  data: {
25
26
  datasets: IScatterChartDataset[]
26
27
  };
@@ -6,8 +6,8 @@ import { Scatter } from 'react-chartjs-2';
6
6
  * @param {import('./scatter-chart.intefrace').IScatterChartProps} props
7
7
  */
8
8
  const ScatterChart = (props) => {
9
- const { data, options } = props.chart;
10
- return <Scatter options={options} data={data} />;
9
+ const { data, options, testId = null } = props.chart;
10
+ return <Scatter options={options} data={data} data-testid={testId} />;
11
11
  };
12
12
 
13
13
  export default ScatterChart;