@oliasoft-open-source/charts-library 3.6.5 → 3.6.6-beta-1

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/dist/index.js CHANGED
@@ -23998,48 +23998,9 @@ const useChartFunctions = ({
23998
23998
  generatedDatasets
23999
23999
  }) => {
24000
24000
  const {
24001
- annotations,
24002
- interactions: {
24003
- onLegendClick,
24004
- onHover: onPointHover,
24005
- onUnhover: onPointUnhover
24006
- },
24001
+ interactions: { onHover: onPointHover, onUnhover: onPointUnhover },
24007
24002
  axes
24008
24003
  } = options;
24009
- const legendClick = useCallback(
24010
- (_e2, legendItem2) => {
24011
- const { datasetIndex } = legendItem2;
24012
- const chartInstance = chartRef == null ? void 0 : chartRef.current;
24013
- const { datasets } = (chartInstance == null ? void 0 : chartInstance.data) || {};
24014
- const dataset = datasets == null ? void 0 : datasets[datasetIndex];
24015
- const meta = chartInstance == null ? void 0 : chartInstance.getDatasetMeta(datasetIndex);
24016
- if (meta) {
24017
- if (meta.hidden === null) {
24018
- meta.hidden = !dataset.hidden;
24019
- } else {
24020
- meta.hidden = null;
24021
- }
24022
- }
24023
- if (annotations.controlAnnotation && (dataset == null ? void 0 : dataset.isAnnotation)) {
24024
- const { annotationIndex } = dataset;
24025
- dispatch({ type: "TOGGLE_ANNOTATION", payload: { annotationIndex } });
24026
- }
24027
- if (dataset.displayGroup) {
24028
- datasets == null ? void 0 : datasets.forEach((ds, ix) => {
24029
- if (ds.displayGroup !== dataset.displayGroup)
24030
- return;
24031
- if (chartInstance && meta) {
24032
- chartInstance.getDatasetMeta(ix).hidden = meta.hidden;
24033
- }
24034
- });
24035
- }
24036
- if (onLegendClick) {
24037
- onLegendClick(legendItem2 == null ? void 0 : legendItem2.text, legendItem2.hidden);
24038
- }
24039
- chartInstance == null ? void 0 : chartInstance.update();
24040
- },
24041
- [onLegendClick, annotations]
24042
- );
24043
24004
  const resetZoom2 = useCallback(() => {
24044
24005
  const chartInstance = chartRef == null ? void 0 : chartRef.current;
24045
24006
  chartInstance == null ? void 0 : chartInstance.resetZoom();
@@ -24161,7 +24122,6 @@ const useChartFunctions = ({
24161
24122
  dispatch({ type: UPDATE_AXES_RANGES, payload: { axes: axes2 } });
24162
24123
  }, []);
24163
24124
  return {
24164
- legendClick,
24165
24125
  resetZoom: resetZoom2,
24166
24126
  onHover,
24167
24127
  handleDownload,
@@ -24669,10 +24629,6 @@ function useMeasure() {
24669
24629
  const useMeasure$1 = isBrowser && typeof window.ResizeObserver !== "undefined" ? useMeasure : function() {
24670
24630
  return [noop, defaultState];
24671
24631
  };
24672
- const getGeneratedLabels = (chart2) => {
24673
- var _a2, _b2, _c2, _d2, _e2;
24674
- return ((_e2 = (_d2 = (_c2 = (_b2 = (_a2 = chart2 == null ? void 0 : chart2.options) == null ? void 0 : _a2.plugins) == null ? void 0 : _b2.legend) == null ? void 0 : _c2.labels) == null ? void 0 : _d2.generateLabels) == null ? void 0 : _e2.call(_d2, chart2)) ?? [];
24675
- };
24676
24632
  const legend = "_legend_wpro0_1";
24677
24633
  const isDragging = "_isDragging_wpro0_8";
24678
24634
  const legendItems = "_legendItems_wpro0_11";
@@ -24806,7 +24762,95 @@ const LegendItem = ({ hidden, dataset, handleClick }) => {
24806
24762
  }
24807
24763
  );
24808
24764
  };
24765
+ const useLegendState = ({
24766
+ chartRef,
24767
+ options,
24768
+ state,
24769
+ dispatch
24770
+ }) => {
24771
+ const {
24772
+ annotations,
24773
+ interactions: { onLegendClick },
24774
+ legend: { customLegend }
24775
+ } = options;
24776
+ const { legendEnabled } = state;
24777
+ const [hiddenStates, setHiddenStates] = useState({});
24778
+ const legendClick = useCallback(
24779
+ (_e2, legendItem2) => {
24780
+ const { datasetIndex } = legendItem2;
24781
+ const chartInstance = chartRef == null ? void 0 : chartRef.current;
24782
+ const { datasets } = (chartInstance == null ? void 0 : chartInstance.data) || {};
24783
+ const dataset = datasets == null ? void 0 : datasets[datasetIndex];
24784
+ const meta = chartInstance == null ? void 0 : chartInstance.getDatasetMeta(datasetIndex);
24785
+ const newHiddenState = !hiddenStates[datasetIndex];
24786
+ setHiddenStates((prevHiddenStates) => ({
24787
+ ...prevHiddenStates,
24788
+ [datasetIndex]: newHiddenState
24789
+ }));
24790
+ if (meta) {
24791
+ if (meta.hidden === null) {
24792
+ meta.hidden = !(dataset == null ? void 0 : dataset.hidden);
24793
+ } else {
24794
+ meta.hidden = null;
24795
+ }
24796
+ }
24797
+ if (annotations.controlAnnotation && (dataset == null ? void 0 : dataset.isAnnotation)) {
24798
+ const { annotationIndex } = dataset;
24799
+ dispatch({ type: "TOGGLE_ANNOTATION", payload: { annotationIndex } });
24800
+ }
24801
+ if (dataset.displayGroup) {
24802
+ datasets == null ? void 0 : datasets.forEach((ds, ix) => {
24803
+ if (ds.displayGroup !== dataset.displayGroup)
24804
+ return;
24805
+ if (chartInstance && meta) {
24806
+ chartInstance.getDatasetMeta(ix).hidden = meta.hidden;
24807
+ }
24808
+ });
24809
+ }
24810
+ if (onLegendClick) {
24811
+ onLegendClick(legendItem2 == null ? void 0 : legendItem2.text, legendItem2.hidden);
24812
+ }
24813
+ chartInstance == null ? void 0 : chartInstance.update();
24814
+ },
24815
+ [chartRef, annotations, onLegendClick, hiddenStates]
24816
+ );
24817
+ const legend2 = useMemo(
24818
+ () => getLegend(options, legendClick),
24819
+ [legendEnabled]
24820
+ );
24821
+ const customLegendPlugin = useMemo(
24822
+ () => ({
24823
+ [CUSTOM_LEGEND_PLUGIN_NAME]: (customLegend == null ? void 0 : customLegend.customLegendPlugin) && {
24824
+ containerID: customLegend == null ? void 0 : customLegend.customLegendContainerID
24825
+ }
24826
+ }),
24827
+ [customLegend]
24828
+ );
24829
+ return {
24830
+ legendClick,
24831
+ hiddenStates,
24832
+ legend: legend2,
24833
+ customLegendPlugin
24834
+ };
24835
+ };
24809
24836
  const LEGEND_MARGIN = 4;
24837
+ const createStyleObject = (legendPosition, chart2) => {
24838
+ const { height = 0, width = 0, chartArea } = chart2;
24839
+ const { top: top2, left: left2, bottom: bottom2, right: right2 } = chartArea;
24840
+ return {
24841
+ left: legendPosition.includes("left") ? left2 : void 0,
24842
+ right: legendPosition.includes("right") ? width - right2 : void 0,
24843
+ top: legendPosition.includes("top") ? top2 : void 0,
24844
+ bottom: legendPosition.includes("bottom") ? height - bottom2 : void 0,
24845
+ maxHeight: 0.5 * (bottom2 - top2 - LEGEND_MARGIN * 2),
24846
+ maxWidth: 0.5 * (right2 - left2 - LEGEND_MARGIN * 2),
24847
+ margin: LEGEND_MARGIN
24848
+ };
24849
+ };
24850
+ const getGeneratedLabels = (chart2) => {
24851
+ var _a2, _b2, _c2, _d2, _e2;
24852
+ return ((_e2 = (_d2 = (_c2 = (_b2 = (_a2 = chart2 == null ? void 0 : chart2.options) == null ? void 0 : _a2.plugins) == null ? void 0 : _b2.legend) == null ? void 0 : _c2.labels) == null ? void 0 : _d2.generateLabels) == null ? void 0 : _e2.call(_d2, chart2)) ?? [];
24853
+ };
24810
24854
  const LegendPanel = forwardRef((props, ref) => {
24811
24855
  const {
24812
24856
  legendPosition,
@@ -24817,32 +24861,31 @@ const LegendPanel = forwardRef((props, ref) => {
24817
24861
  generatedDatasets,
24818
24862
  isDragging: isDragging2
24819
24863
  } = props;
24820
- const [refresh, setRefresh] = useState(false);
24821
24864
  const { legendEnabled } = state;
24822
- const { legendClick } = useChartFunctions({
24865
+ const chart2 = chartRef.current;
24866
+ if (!chart2)
24867
+ return null;
24868
+ const {
24869
+ data: { datasets }
24870
+ } = chart2;
24871
+ const { legendClick, hiddenStates } = useLegendState({
24823
24872
  chartRef,
24824
- state,
24825
24873
  options,
24826
- dispatch,
24827
- generatedDatasets
24874
+ state,
24875
+ dispatch
24828
24876
  });
24829
24877
  const { onToggleLegend } = useToggleHandlers(dispatch);
24830
- const chart2 = chartRef.current;
24831
- if (!chart2)
24832
- return null;
24833
- const { data, chartArea, height = 0, width = 0 } = chart2;
24834
- const { datasets } = data;
24835
- const { top: top2, left: left2, bottom: bottom2, right: right2 } = chartArea;
24836
- const items = getGeneratedLabels(chart2);
24837
- const style = {
24838
- left: legendPosition.includes("left") ? left2 : void 0,
24839
- right: legendPosition.includes("right") ? width - right2 : void 0,
24840
- top: legendPosition.includes("top") ? top2 : void 0,
24841
- bottom: legendPosition.includes("bottom") ? height - bottom2 : void 0,
24842
- maxHeight: 0.5 * (bottom2 - top2 - LEGEND_MARGIN * 2),
24843
- maxWidth: 0.5 * (right2 - left2 - LEGEND_MARGIN * 2),
24844
- margin: LEGEND_MARGIN
24845
- };
24878
+ const [items, setItems] = useState([]);
24879
+ useEffect(() => {
24880
+ if (!chart2)
24881
+ return;
24882
+ const newItems = getGeneratedLabels(chart2);
24883
+ setItems(newItems);
24884
+ }, [generatedDatasets, chart2]);
24885
+ const style = useMemo(
24886
+ () => createStyleObject(legendPosition, chart2),
24887
+ [chart2, legendPosition]
24888
+ );
24846
24889
  return /* @__PURE__ */ jsxs(
24847
24890
  "div",
24848
24891
  {
@@ -24863,15 +24906,12 @@ const LegendPanel = forwardRef((props, ref) => {
24863
24906
  icon: legendEnabled ? /* @__PURE__ */ jsx(TbX, {}) : /* @__PURE__ */ jsx(TbList, {})
24864
24907
  }
24865
24908
  ) }),
24866
- /* @__PURE__ */ jsx("div", { className: styles$2.legendItems, children: items.map((item) => /* @__PURE__ */ jsx(
24909
+ /* @__PURE__ */ jsx("div", { className: styles$2.legendItems, children: items.map((item, idx) => /* @__PURE__ */ jsx(
24867
24910
  LegendItem,
24868
24911
  {
24869
- hidden: item.hidden,
24912
+ hidden: hiddenStates[idx],
24870
24913
  dataset: datasets[item.datasetIndex],
24871
- handleClick: (event) => {
24872
- legendClick(event, item);
24873
- setRefresh(!refresh);
24874
- }
24914
+ handleClick: (event) => legendClick(event, item)
24875
24915
  },
24876
24916
  `${item.datasetIndex}-hidden-${item.hidden}`
24877
24917
  )) })
@@ -25712,7 +25752,6 @@ const useChartOptions = ({
25712
25752
  dispatch,
25713
25753
  generatedDatasets
25714
25754
  }) => {
25715
- var _a2, _b2;
25716
25755
  const {
25717
25756
  interactions: { onAnimationComplete },
25718
25757
  annotations: {
@@ -25734,29 +25773,28 @@ const useChartOptions = ({
25734
25773
  zoomEnabled,
25735
25774
  panEnabled,
25736
25775
  lineEnabled,
25737
- showAnnotationLineIndex,
25738
- legendEnabled
25776
+ showAnnotationLineIndex
25739
25777
  } = state;
25740
25778
  const [hoveredPoint, setHoveredPoint] = useState(null);
25741
- const { legendClick, updateAxesRangesFromChart, onHover } = useChartFunctions(
25742
- {
25743
- chartRef,
25744
- state,
25745
- options,
25746
- dispatch,
25747
- generatedDatasets
25748
- }
25749
- );
25779
+ const { updateAxesRangesFromChart, onHover } = useChartFunctions({
25780
+ chartRef,
25781
+ state,
25782
+ options,
25783
+ dispatch,
25784
+ generatedDatasets
25785
+ });
25786
+ const { legendClick, legend: legend2, customLegendPlugin } = useLegendState({
25787
+ chartRef,
25788
+ options,
25789
+ state,
25790
+ dispatch
25791
+ });
25750
25792
  const datalabels = getLineChartDataLabels(options);
25751
25793
  const tooltip = getLineChartToolTips(options);
25752
25794
  const annotation2 = useMemo(
25753
25795
  () => getAnnotation(options, state),
25754
25796
  [showAnnotationLineIndex, legendClick]
25755
25797
  );
25756
- const legend2 = useMemo(
25757
- () => getLegend(options, legendClick),
25758
- [legendEnabled]
25759
- );
25760
25798
  const scales = useMemo(
25761
25799
  () => autoScale(options, state, generatedDatasets, chartRef),
25762
25800
  [options, state, generatedDatasets, chartRef]
@@ -25798,9 +25836,7 @@ const useChartOptions = ({
25798
25836
  tooltip,
25799
25837
  legend: { ...legend2, display: false },
25800
25838
  // hide default legend for line chart only
25801
- [CUSTOM_LEGEND_PLUGIN_NAME]: ((_a2 = options.legend.customLegend) == null ? void 0 : _a2.customLegendPlugin) && {
25802
- containerID: (_b2 = options.legend.customLegend) == null ? void 0 : _b2.customLegendContainerID
25803
- },
25839
+ customLegendPlugin,
25804
25840
  chartAreaBorder: {
25805
25841
  borderColor: BORDER_COLOR
25806
25842
  },