@oliasoft-open-source/charts-library 4.7.8 → 4.7.9

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
@@ -9,7 +9,7 @@ import { produce } from "immer";
9
9
  import { round as round$2, displayNumber, isCloseTo, roundByMagnitude } from "@oliasoft-open-source/units";
10
10
  import cx from "classnames";
11
11
  import { Portal, Icon, Menu, Tooltip as Tooltip$2, Button, Popover, Field, InputGroup, NumberInput, InputGroupAddon, Select, ButtonGroup, Spacer, Flex, Text } from "@oliasoft-open-source/react-ui-library";
12
- import { isEmpty, isArray as isArray$2, some, has, cloneDeep, defaultTo, findIndex, set as set$2, debounce as debounce$3, isNil, map as map$3, find, get as get$1, noop as noop$2 } from "lodash";
12
+ import { isEmpty, isArray as isArray$2, some, has, isNil, cloneDeep, defaultTo, findIndex, set as set$2, debounce as debounce$3, map as map$3, find, get as get$1, noop as noop$2 } from "lodash";
13
13
  /*!
14
14
  * @kurkle/color v0.3.2
15
15
  * https://github.com/kurkle/color#readme
@@ -25172,6 +25172,29 @@ const generateLineChartDatasets = (datasets, state, options, { label }) => {
25172
25172
  };
25173
25173
  });
25174
25174
  };
25175
+ const getAnnotationsData = (data) => {
25176
+ return data == null ? void 0 : data.reduce((acc, obj) => {
25177
+ return {
25178
+ ...acc,
25179
+ [obj.annotationAxis]: [...acc[obj.annotationAxis] || [], +obj.value]
25180
+ };
25181
+ }, {});
25182
+ };
25183
+ const getAxisRangeByType = (chartRef, axesType, annotationsData) => {
25184
+ if (!chartRef || !chartRef.current) return null;
25185
+ const metasets = chartRef.current.getSortedVisibleDatasetMetas();
25186
+ const annotDataByType = getAnnotationsData(annotationsData)[axesType] ?? [];
25187
+ let allData = [];
25188
+ metasets.forEach((metaset) => {
25189
+ const data = metaset._parsed.map((parsedData) => parsedData[axesType]).filter((value) => value != null && !isNaN(value));
25190
+ allData = allData.concat(data);
25191
+ });
25192
+ allData = allData.concat(annotDataByType);
25193
+ if (allData.length === 0) return null;
25194
+ const min = Math.min(...allData);
25195
+ const max = Math.max(...allData);
25196
+ return { min, max };
25197
+ };
25175
25198
  const calculateDelta$1 = (tickValue, ticks) => {
25176
25199
  let delta = ticks.length > 3 ? ticks[2].value - ticks[1].value : ticks[1].value - ticks[0].value;
25177
25200
  if (Math.abs(delta) >= 1 && tickValue !== Math.floor(Number(tickValue))) {
@@ -25365,14 +25388,6 @@ const getSuggestedAxisRange = ({
25365
25388
  max: round$2(maxAxisValue, DECIMAL_POINT_TOLERANCE)
25366
25389
  };
25367
25390
  };
25368
- const getAnnotationsData = (data) => {
25369
- return data == null ? void 0 : data.reduce((acc, obj) => {
25370
- return {
25371
- ...acc,
25372
- [obj.annotationAxis]: [...acc[obj.annotationAxis] || [], +obj.value]
25373
- };
25374
- }, {});
25375
- };
25376
25391
  const getAxesDataFromMetasets = (chartRef, scalesKeys, annotationsData) => {
25377
25392
  var _a2;
25378
25393
  if (!chartRef || !scalesKeys) return {};
@@ -25386,6 +25401,7 @@ const getAxesDataFromMetasets = (chartRef, scalesKeys, annotationsData) => {
25386
25401
  };
25387
25402
  }, {}));
25388
25403
  };
25404
+ const shouldCalculate = (min, max) => !isNil(min) || !isNil(max);
25389
25405
  const autoScale = (options, state, generatedDatasets, chartRef) => {
25390
25406
  const {
25391
25407
  additionalAxesOptions,
@@ -25405,7 +25421,8 @@ const autoScale = (options, state, generatedDatasets, chartRef) => {
25405
25421
  const adjustedScales = data && (scalesKeys == null ? void 0 : scalesKeys.reduce((acc, key) => {
25406
25422
  const scale = scales[key];
25407
25423
  const { min: propMin = void 0, max: propMax = void 0 } = scale;
25408
- const { min, max } = getSuggestedAxisRange({
25424
+ const { min: calcMin, max: calcMax } = getAxisRangeByType(chartRef, key, annotationsData) ?? {};
25425
+ const { min: suggestedMin, max: suggestedMax } = getSuggestedAxisRange({
25409
25426
  data: data[key],
25410
25427
  beginAtZero: additionalAxesOptions == null ? void 0 : additionalAxesOptions.beginAtZero,
25411
25428
  autoAxisPadding: additionalAxesOptions == null ? void 0 : additionalAxesOptions.autoAxisPadding
@@ -25413,8 +25430,8 @@ const autoScale = (options, state, generatedDatasets, chartRef) => {
25413
25430
  const res = {
25414
25431
  [key]: {
25415
25432
  ...scale,
25416
- min: propMin ?? min,
25417
- max: propMax ?? max
25433
+ min: propMin ?? (shouldCalculate(propMin, propMax) ? calcMin : suggestedMin),
25434
+ max: propMax ?? (shouldCalculate(propMin, propMax) ? calcMax : suggestedMax)
25418
25435
  }
25419
25436
  };
25420
25437
  return { ...acc, ...res };
@@ -26089,35 +26106,6 @@ const useChartPlugins = ({ options, resetZoom: resetZoom2 }) => {
26089
26106
  ];
26090
26107
  }, [handleDoubleClick]);
26091
26108
  };
26092
- const getAxesRangesFromChart = (chartRef, axes) => {
26093
- const { scales = {} } = chartRef.current || {};
26094
- return Object.entries(scales).map(([key, { min, max }], i2) => {
26095
- var _a2;
26096
- const axesArray = [...axes.x, ...axes.y];
26097
- const unit = (_a2 = axesArray == null ? void 0 : axesArray[i2]) == null ? void 0 : _a2.unit;
26098
- return {
26099
- id: key,
26100
- min,
26101
- max,
26102
- ...unit ? { unit } : {}
26103
- };
26104
- });
26105
- };
26106
- const getAxisRangeByType = (chartRef, axesType, annotationsData) => {
26107
- if (!chartRef || !chartRef.current) return null;
26108
- const metasets = chartRef.current.getSortedVisibleDatasetMetas();
26109
- const annotDataByType = getAnnotationsData(annotationsData)[axesType] ?? [];
26110
- let allData = [];
26111
- metasets.forEach((metaset) => {
26112
- const data = metaset._parsed.map((parsedData) => parsedData[axesType]).filter((value) => value != null && !isNaN(value));
26113
- allData = allData.concat(data);
26114
- });
26115
- allData = allData.concat(annotDataByType);
26116
- if (allData.length === 0) return null;
26117
- const min = Math.min(...allData);
26118
- const max = Math.max(...allData);
26119
- return { min, max };
26120
- };
26121
26109
  const useToggleCustomLegendVisibility = (memoState, memoOptions) => {
26122
26110
  var _a2, _b2;
26123
26111
  useEffect(() => {
@@ -26179,18 +26167,27 @@ const useUpdateAxesRanges = ({
26179
26167
  }
26180
26168
  }, [range]);
26181
26169
  };
26182
- const useSaveInitialAxesRanges = (chartRef, axes, dispatch, generatedDatasets) => {
26170
+ const useSaveInitialAxesRanges = (chartRef, dispatch, generatedDatasets) => {
26171
+ var _a2;
26183
26172
  const prevDatasets = useRef(null);
26184
26173
  useEffect(() => {
26185
- if (chartRef.current && !isEqual(prevDatasets.current, generatedDatasets)) {
26186
- const initialAxesRanges = getAxesRangesFromChart(chartRef, axes);
26174
+ var _a3, _b2;
26175
+ if (((_a3 = chartRef == null ? void 0 : chartRef.current) == null ? void 0 : _a3.scales) && !isEqual(prevDatasets.current, generatedDatasets)) {
26176
+ const scales = ((_b2 = chartRef == null ? void 0 : chartRef.current) == null ? void 0 : _b2.scales) ?? {};
26177
+ const initialAxesRanges = Object.entries(scales).map(
26178
+ ([id, { min, max, _range: range }]) => ({
26179
+ id,
26180
+ min: (range == null ? void 0 : range.min) ?? min,
26181
+ max: (range == null ? void 0 : range.max) ?? max
26182
+ })
26183
+ );
26187
26184
  dispatch({
26188
26185
  type: SAVE_INITIAL_AXES_RANGES,
26189
26186
  payload: { initialAxesRanges }
26190
26187
  });
26191
26188
  prevDatasets.current = generatedDatasets;
26192
26189
  }
26193
- }, [generatedDatasets]);
26190
+ }, [(_a2 = chartRef == null ? void 0 : chartRef.current) == null ? void 0 : _a2.scales, generatedDatasets]);
26194
26191
  };
26195
26192
  const useChartState = ({
26196
26193
  chartRef,
@@ -26212,8 +26209,7 @@ const useChartState = ({
26212
26209
  const memoOptions = useMemo(() => options, [options]);
26213
26210
  const {
26214
26211
  additionalAxesOptions: { range = void 0 },
26215
- annotations: { annotationsData = [] } = {},
26216
- axes
26212
+ annotations: { annotationsData = [] } = {}
26217
26213
  } = memoOptions;
26218
26214
  useStoreChartStateInStorage(memoState, persistenceId);
26219
26215
  useToggleCustomLegendVisibility(memoState, memoOptions);
@@ -26224,7 +26220,7 @@ const useChartState = ({
26224
26220
  chartRef,
26225
26221
  annotationsData
26226
26222
  });
26227
- useSaveInitialAxesRanges(chartRef, axes, dispatch, generatedDatasets);
26223
+ useSaveInitialAxesRanges(chartRef, dispatch, generatedDatasets);
26228
26224
  };
26229
26225
  const WORD_SEPARATOR = " ";
26230
26226
  const SEMI_TRANSPARENT = "rgba(0, 0, 0, 0.5)";