@oliasoft-open-source/charts-library 2.5.3 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/charts-library",
3
- "version": "2.5.3",
3
+ "version": "2.5.4",
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,9 @@
1
1
  # Charts Library Release Notes
2
2
 
3
+ ## 2.5.4
4
+
5
+ - Fixed onPointHover event
6
+
3
7
  ## 2.5.3
4
8
 
5
9
  - Fixed datalabels position, set labels position inside chart area
@@ -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,
@@ -94,7 +94,7 @@ ChartJS.register(
94
94
  const LineChart = (props) => {
95
95
  setDefaultTheme();
96
96
  const chartRef = useRef(null);
97
- let pointHover = false;
97
+ const [hoveredPoint, setHoveredPoint] = useState(null);
98
98
  const chart = getDefaultProps(props);
99
99
  const { options, testId } = chart;
100
100
  const { headerComponent, subheaderComponent, table } = props;
@@ -276,18 +276,19 @@ const LineChart = (props) => {
276
276
  };
277
277
 
278
278
  const onHover = (evt, hoveredItems, chartInstance) => {
279
- if (pointHover && !hoveredItems?.length) {
280
- pointHover = false;
281
- if (interactions.onPointUnhover) {
282
- interactions.onPointUnhover(evt);
283
- }
279
+ if (!hoveredItems?.length && interactions.onPointUnhover && hoveredPoint) {
280
+ setHoveredPoint(null);
281
+ interactions.onPointUnhover(evt);
284
282
  }
285
- if (!pointHover && hoveredItems?.length) {
286
- pointHover = true;
287
- if (interactions.onPointHover) {
288
- const { index, datasetIndex } = hoveredItems[0];
289
- const generatedDataset = generatedDatasets;
290
- interactions.onPointHover(evt, datasetIndex, index, generatedDataset);
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);
291
292
  }
292
293
  }
293
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
+ };