@oliasoft-open-source/charts-library 3.6.6 → 3.6.8-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
|
@@ -24774,7 +24774,28 @@ const useLegendState = ({
|
|
|
24774
24774
|
legend: { customLegend }
|
|
24775
24775
|
} = options;
|
|
24776
24776
|
const { legendEnabled } = state;
|
|
24777
|
-
const [hiddenStates, setHiddenStates] = useState(
|
|
24777
|
+
const [hiddenStates, setHiddenStates] = useState([]);
|
|
24778
|
+
const upsertHiddenState = ({ label, datasetIndex, hidden }) => {
|
|
24779
|
+
setHiddenStates((prevStates) => {
|
|
24780
|
+
const existingIndex = prevStates.findIndex(
|
|
24781
|
+
(state2) => state2.label === label
|
|
24782
|
+
);
|
|
24783
|
+
const newStates = [...prevStates];
|
|
24784
|
+
const newState = { label, datasetIndex, hidden };
|
|
24785
|
+
if (existingIndex !== -1) {
|
|
24786
|
+
newStates[existingIndex] = newState;
|
|
24787
|
+
} else {
|
|
24788
|
+
newStates.push(newState);
|
|
24789
|
+
}
|
|
24790
|
+
return newStates;
|
|
24791
|
+
});
|
|
24792
|
+
};
|
|
24793
|
+
const clearNonExistentDatasets = (datasets) => {
|
|
24794
|
+
const existingLabels = datasets.map((ds) => ds.label);
|
|
24795
|
+
setHiddenStates(
|
|
24796
|
+
(prevStates) => prevStates.filter((state2) => existingLabels.includes(state2.label))
|
|
24797
|
+
);
|
|
24798
|
+
};
|
|
24778
24799
|
const legendClick = useCallback(
|
|
24779
24800
|
(_e2, legendItem2) => {
|
|
24780
24801
|
const { datasetIndex } = legendItem2;
|
|
@@ -24782,21 +24803,16 @@ const useLegendState = ({
|
|
|
24782
24803
|
const { datasets } = (chartInstance == null ? void 0 : chartInstance.data) || {};
|
|
24783
24804
|
const dataset = datasets == null ? void 0 : datasets[datasetIndex];
|
|
24784
24805
|
const meta = chartInstance == null ? void 0 : chartInstance.getDatasetMeta(datasetIndex);
|
|
24785
|
-
const
|
|
24786
|
-
|
|
24787
|
-
|
|
24788
|
-
|
|
24789
|
-
|
|
24790
|
-
|
|
24791
|
-
|
|
24792
|
-
meta.hidden = !(dataset == null ? void 0 : dataset.hidden);
|
|
24793
|
-
} else {
|
|
24794
|
-
meta.hidden = null;
|
|
24795
|
-
}
|
|
24796
|
-
}
|
|
24806
|
+
const isDatasetVisible = (chartInstance == null ? void 0 : chartInstance.isDatasetVisible(datasetIndex)) ?? true;
|
|
24807
|
+
upsertHiddenState({
|
|
24808
|
+
label: dataset.label,
|
|
24809
|
+
datasetIndex,
|
|
24810
|
+
hidden: isDatasetVisible
|
|
24811
|
+
});
|
|
24812
|
+
chartInstance == null ? void 0 : chartInstance.setDatasetVisibility(datasetIndex, !isDatasetVisible);
|
|
24797
24813
|
if (annotations.controlAnnotation && (dataset == null ? void 0 : dataset.isAnnotation)) {
|
|
24798
24814
|
const { annotationIndex } = dataset;
|
|
24799
|
-
dispatch({ type:
|
|
24815
|
+
dispatch({ type: TOGGLE_ANNOTATION, payload: { annotationIndex } });
|
|
24800
24816
|
}
|
|
24801
24817
|
if (dataset.displayGroup) {
|
|
24802
24818
|
datasets == null ? void 0 : datasets.forEach((ds, ix) => {
|
|
@@ -24830,7 +24846,9 @@ const useLegendState = ({
|
|
|
24830
24846
|
legendClick,
|
|
24831
24847
|
hiddenStates,
|
|
24832
24848
|
legend: legend2,
|
|
24833
|
-
customLegendPlugin
|
|
24849
|
+
customLegendPlugin,
|
|
24850
|
+
upsertHiddenState,
|
|
24851
|
+
clearNonExistentDatasets
|
|
24834
24852
|
};
|
|
24835
24853
|
};
|
|
24836
24854
|
const LEGEND_MARGIN = 4;
|
|
@@ -24851,6 +24869,35 @@ const getGeneratedLabels = (chart2) => {
|
|
|
24851
24869
|
var _a2, _b2, _c2, _d2, _e2;
|
|
24852
24870
|
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
24871
|
};
|
|
24872
|
+
const useGeneratedLabels = (chartRef, generatedDatasets) => {
|
|
24873
|
+
const [items, setItems] = useState([]);
|
|
24874
|
+
useEffect(() => {
|
|
24875
|
+
const chart2 = chartRef.current;
|
|
24876
|
+
if (!chart2)
|
|
24877
|
+
return;
|
|
24878
|
+
const newItems = getGeneratedLabels(chart2);
|
|
24879
|
+
setItems(newItems);
|
|
24880
|
+
}, [generatedDatasets, chartRef]);
|
|
24881
|
+
return items;
|
|
24882
|
+
};
|
|
24883
|
+
const LegendItems = ({
|
|
24884
|
+
items,
|
|
24885
|
+
hiddenStates,
|
|
24886
|
+
datasets,
|
|
24887
|
+
legendClick
|
|
24888
|
+
}) => /* @__PURE__ */ jsx("div", { className: styles$2.legendItems, children: items.map((item) => {
|
|
24889
|
+
var _a2;
|
|
24890
|
+
const hiddenState = ((_a2 = hiddenStates.find((state) => state.label === item.text)) == null ? void 0 : _a2.hidden) ?? false;
|
|
24891
|
+
return /* @__PURE__ */ jsx(
|
|
24892
|
+
LegendItem,
|
|
24893
|
+
{
|
|
24894
|
+
hidden: hiddenState,
|
|
24895
|
+
dataset: datasets[item.datasetIndex],
|
|
24896
|
+
handleClick: (event) => legendClick(event, item)
|
|
24897
|
+
},
|
|
24898
|
+
`${item.datasetIndex}-hidden-${item.hidden}`
|
|
24899
|
+
);
|
|
24900
|
+
}) });
|
|
24854
24901
|
const LegendPanel = forwardRef((props, ref) => {
|
|
24855
24902
|
const {
|
|
24856
24903
|
legendPosition,
|
|
@@ -24862,27 +24909,24 @@ const LegendPanel = forwardRef((props, ref) => {
|
|
|
24862
24909
|
isDragging: isDragging2
|
|
24863
24910
|
} = props;
|
|
24864
24911
|
const { legendEnabled } = state;
|
|
24865
|
-
const chart2 = chartRef.current;
|
|
24912
|
+
const chart2 = chartRef == null ? void 0 : chartRef.current;
|
|
24866
24913
|
if (!chart2)
|
|
24867
24914
|
return null;
|
|
24868
24915
|
const {
|
|
24869
24916
|
data: { datasets }
|
|
24870
24917
|
} = chart2;
|
|
24871
|
-
const { legendClick, hiddenStates } = useLegendState({
|
|
24918
|
+
const { legendClick, hiddenStates, clearNonExistentDatasets } = useLegendState({
|
|
24872
24919
|
chartRef,
|
|
24873
24920
|
options,
|
|
24874
24921
|
state,
|
|
24875
24922
|
dispatch
|
|
24876
24923
|
});
|
|
24877
24924
|
const { onToggleLegend } = useToggleHandlers(dispatch);
|
|
24878
|
-
const
|
|
24879
|
-
useEffect(() => {
|
|
24880
|
-
if (!chart2)
|
|
24881
|
-
return;
|
|
24882
|
-
const newItems = getGeneratedLabels(chart2);
|
|
24883
|
-
setItems(newItems);
|
|
24884
|
-
}, [generatedDatasets, chart2]);
|
|
24925
|
+
const items = useGeneratedLabels(chartRef, generatedDatasets);
|
|
24885
24926
|
const style = createLegendStyle(legendPosition, chart2);
|
|
24927
|
+
useEffect(() => {
|
|
24928
|
+
clearNonExistentDatasets(generatedDatasets);
|
|
24929
|
+
}, [generatedDatasets]);
|
|
24886
24930
|
return /* @__PURE__ */ jsxs(
|
|
24887
24931
|
"div",
|
|
24888
24932
|
{
|
|
@@ -24903,15 +24947,15 @@ const LegendPanel = forwardRef((props, ref) => {
|
|
|
24903
24947
|
icon: legendEnabled ? /* @__PURE__ */ jsx(TbX, {}) : /* @__PURE__ */ jsx(TbList, {})
|
|
24904
24948
|
}
|
|
24905
24949
|
) }),
|
|
24906
|
-
/* @__PURE__ */ jsx(
|
|
24907
|
-
|
|
24950
|
+
/* @__PURE__ */ jsx(
|
|
24951
|
+
LegendItems,
|
|
24908
24952
|
{
|
|
24909
|
-
|
|
24910
|
-
|
|
24911
|
-
|
|
24912
|
-
|
|
24913
|
-
|
|
24914
|
-
)
|
|
24953
|
+
items,
|
|
24954
|
+
hiddenStates,
|
|
24955
|
+
datasets,
|
|
24956
|
+
legendClick
|
|
24957
|
+
}
|
|
24958
|
+
)
|
|
24915
24959
|
]
|
|
24916
24960
|
}
|
|
24917
24961
|
);
|