@oliasoft-open-source/charts-library 4.8.0-beta-6 → 4.8.0-beta-8

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
@@ -22856,6 +22856,18 @@ const setDefaultTheme = () => {
22856
22856
  defaults$2.color = DEFAULT_COLOR;
22857
22857
  defaults$2.borderColor = BORDER_COLOR;
22858
22858
  };
22859
+ const isNilOrEmpty = (value) => {
22860
+ if (value === null || value === void 0 || value === "") {
22861
+ return true;
22862
+ }
22863
+ if (Array.isArray(value) && value.length === 0) {
22864
+ return true;
22865
+ }
22866
+ if (typeof value === "object" && Object.keys(value).length === 0) {
22867
+ return true;
22868
+ }
22869
+ return false;
22870
+ };
22859
22871
  const isLessThanMax = (value, max) => {
22860
22872
  return value === void 0 || max === void 0 || Number(value) < Number(max);
22861
22873
  };
@@ -22875,6 +22887,10 @@ const generateKey = (values) => {
22875
22887
  return key;
22876
22888
  };
22877
22889
  const isPrimitiveValue = (value) => typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null;
22890
+ const isRangeValid = (r2) => {
22891
+ if (!r2) return false;
22892
+ return Object.values(r2).some((value) => !isNilOrEmpty(value));
22893
+ };
22878
22894
  const getChartStateFromStorage = (persistenceId) => {
22879
22895
  if (!persistenceId) return null;
22880
22896
  const chartStateKey = `line-chart-state-${persistenceId}`;
@@ -22885,6 +22901,37 @@ const getChartStateFromStorage = (persistenceId) => {
22885
22901
  }
22886
22902
  return null;
22887
22903
  };
22904
+ const removeExpiredChartStates = (maxAgeInHours = 72) => {
22905
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
22906
+ const maxAgeInMilliseconds = maxAgeInHours * 60 * 60 * 1e3;
22907
+ for (let i2 = 0; i2 < localStorage.length; i2++) {
22908
+ const key = localStorage.key(i2);
22909
+ if (key == null ? void 0 : key.includes("line-chart-state-")) {
22910
+ const chartStateObjectJSON = localStorage.getItem(key);
22911
+ if (chartStateObjectJSON) {
22912
+ const chartStateObject = JSON.parse(chartStateObjectJSON);
22913
+ const storedTime = chartStateObject.timestamp;
22914
+ if (storedTime) {
22915
+ const ageInMilliseconds = currentTime - storedTime;
22916
+ if (ageInMilliseconds > maxAgeInMilliseconds) {
22917
+ localStorage.removeItem(key);
22918
+ }
22919
+ }
22920
+ }
22921
+ }
22922
+ }
22923
+ };
22924
+ const storeChartStateInStorage = (state, persistenceId) => {
22925
+ if (!persistenceId) return;
22926
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
22927
+ const chartStateKey = `line-chart-state-${persistenceId}`;
22928
+ const chartStateObject = {
22929
+ state,
22930
+ timestamp: currentTime
22931
+ };
22932
+ localStorage.setItem(chartStateKey, JSON.stringify(chartStateObject));
22933
+ removeExpiredChartStates();
22934
+ };
22888
22935
  const initialState = ({ options, persistenceId }) => {
22889
22936
  const {
22890
22937
  additionalAxesOptions: { range: customAxesRange = {} },
@@ -26064,6 +26111,122 @@ const useChartPlugins = ({ options, resetZoom: resetZoom2 }) => {
26064
26111
  ];
26065
26112
  }, [handleDoubleClick]);
26066
26113
  };
26114
+ const useToggleCustomLegendVisibility = (memoState, memoOptions) => {
26115
+ var _a2, _b2;
26116
+ useEffect(() => {
26117
+ var _a3, _b3;
26118
+ if ((_b3 = (_a3 = memoOptions == null ? void 0 : memoOptions.legend) == null ? void 0 : _a3.customLegend) == null ? void 0 : _b3.customLegendPlugin) {
26119
+ const parent = document.getElementById(
26120
+ memoOptions.legend.customLegend.customLegendContainerID
26121
+ );
26122
+ if (parent !== null) {
26123
+ parent.style.visibility = memoState.legendEnabled ? "visible" : "hidden";
26124
+ }
26125
+ }
26126
+ }, [
26127
+ (_b2 = (_a2 = memoOptions == null ? void 0 : memoOptions.legend) == null ? void 0 : _a2.customLegend) == null ? void 0 : _b2.customLegendPlugin,
26128
+ memoState.legendEnabled
26129
+ ]);
26130
+ };
26131
+ const useStoreChartStateInStorage = (memoState, persistenceId) => {
26132
+ useEffect(() => {
26133
+ storeChartStateInStorage(memoState, persistenceId);
26134
+ }, [
26135
+ memoState.panEnabled,
26136
+ memoState.lineEnabled,
26137
+ memoState.pointsEnabled,
26138
+ memoState.legendEnabled,
26139
+ memoState.enableDragPoints,
26140
+ memoState.zoomEnabled
26141
+ ]);
26142
+ };
26143
+ const useUpdateAxesRanges = ({
26144
+ chartRef,
26145
+ range,
26146
+ state,
26147
+ dispatch,
26148
+ annotationsData
26149
+ }) => {
26150
+ const prevAxes = useRef(state.axes);
26151
+ useEffect(() => {
26152
+ if (range && isRangeValid(range)) {
26153
+ const newAxes = Object.entries(range).map(([key, { min, max }]) => {
26154
+ const { min: minFromData = 0, max: maxFromData = 0 } = getAxisRangeByType(chartRef, key, annotationsData) ?? {};
26155
+ return {
26156
+ id: key,
26157
+ min: min ?? minFromData,
26158
+ max: max ?? maxFromData
26159
+ };
26160
+ });
26161
+ const mergedAxes = [...prevAxes.current ?? []].map((axis) => {
26162
+ const newAxis = newAxes.find((a2) => a2.id === axis.id);
26163
+ return newAxis ? { ...axis, ...newAxis } : axis;
26164
+ });
26165
+ if (!isEqual(mergedAxes, prevAxes.current)) {
26166
+ dispatch({
26167
+ type: UPDATE_AXES_RANGES,
26168
+ payload: { axes: mergedAxes }
26169
+ });
26170
+ prevAxes.current = mergedAxes;
26171
+ }
26172
+ }
26173
+ }, [range]);
26174
+ };
26175
+ const useSaveInitialAxesRanges = (chartRef, dispatch, generatedDatasets) => {
26176
+ var _a2;
26177
+ const prevDatasets = useRef(null);
26178
+ useEffect(() => {
26179
+ var _a3, _b2;
26180
+ if (((_a3 = chartRef == null ? void 0 : chartRef.current) == null ? void 0 : _a3.scales) && !isEqual(prevDatasets.current, generatedDatasets)) {
26181
+ const scales = ((_b2 = chartRef == null ? void 0 : chartRef.current) == null ? void 0 : _b2.scales) ?? {};
26182
+ const initialAxesRanges = Object.entries(scales).map(
26183
+ ([id, { min, max, _range: range }]) => ({
26184
+ id,
26185
+ min: (range == null ? void 0 : range.min) ?? min,
26186
+ max: (range == null ? void 0 : range.max) ?? max
26187
+ })
26188
+ );
26189
+ dispatch({
26190
+ type: SAVE_INITIAL_AXES_RANGES,
26191
+ payload: { initialAxesRanges }
26192
+ });
26193
+ prevDatasets.current = generatedDatasets;
26194
+ }
26195
+ }, [(_a2 = chartRef == null ? void 0 : chartRef.current) == null ? void 0 : _a2.scales, generatedDatasets]);
26196
+ };
26197
+ const useChartState = ({
26198
+ chartRef,
26199
+ options,
26200
+ state,
26201
+ dispatch,
26202
+ persistenceId,
26203
+ generatedDatasets
26204
+ }) => {
26205
+ useEffect(() => {
26206
+ return () => {
26207
+ if (chartRef == null ? void 0 : chartRef.current) {
26208
+ const chart2 = chartRef == null ? void 0 : chartRef.current;
26209
+ chart2 == null ? void 0 : chart2.destroy();
26210
+ }
26211
+ };
26212
+ }, []);
26213
+ const memoState = useMemo(() => state, [state]);
26214
+ const memoOptions = useMemo(() => options, [options]);
26215
+ const {
26216
+ additionalAxesOptions: { range = void 0 },
26217
+ annotations: { annotationsData = [] } = {}
26218
+ } = memoOptions;
26219
+ useStoreChartStateInStorage(memoState, persistenceId);
26220
+ useToggleCustomLegendVisibility(memoState, memoOptions);
26221
+ useUpdateAxesRanges({
26222
+ range,
26223
+ state: memoState,
26224
+ dispatch,
26225
+ chartRef,
26226
+ annotationsData
26227
+ });
26228
+ useSaveInitialAxesRanges(chartRef, dispatch, generatedDatasets);
26229
+ };
26067
26230
  const WORD_SEPARATOR = " ";
26068
26231
  const SEMI_TRANSPARENT = "rgba(0, 0, 0, 0.5)";
26069
26232
  const getWords = (text) => {
@@ -27145,19 +27308,16 @@ const LineChart = (props) => {
27145
27308
  initialState
27146
27309
  );
27147
27310
  const generatedDatasets = useMemo(() => {
27148
- const startTimeStamp = performance.now();
27149
- const result = generateLineChartDatasets(
27150
- datasets,
27151
- state,
27152
- options,
27153
- translations
27154
- );
27155
- const endTimeStamp = performance.now();
27156
- console.log(
27157
- `generateLineChartDatasets execution time: ${(endTimeStamp - startTimeStamp).toFixed(5)} ms`
27158
- );
27159
- return result;
27311
+ return generateLineChartDatasets(datasets, state, options, translations);
27160
27312
  }, [state.lineEnabled, state.pointsEnabled, axes, annotations, graph]);
27313
+ useChartState({
27314
+ chartRef,
27315
+ options,
27316
+ state,
27317
+ dispatch,
27318
+ persistenceId,
27319
+ generatedDatasets
27320
+ });
27161
27321
  const { resetZoom: resetZoom2, handleKeyDown, handleKeyUp } = useChartFunctions({
27162
27322
  chartRef,
27163
27323
  state,