@oliasoft-open-source/charts-library 9.1.0-beta-2 → 9.1.0-beta-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/dist/index.js CHANGED
@@ -33200,7 +33200,17 @@ var getMinorTickPositions = (majorTickPositions, scale) => {
33200
33200
  return parseFloat(minorTickPosition?.toFixed(1));
33201
33201
  })?.filter((position) => isValidPosition(position, sortedMajorTickPositions, scale));
33202
33202
  };
33203
- var drawMinorTicksForScale = (scale) => {
33203
+ var getEqualScaleMinorTicks = (scale, majorPositions, step) => {
33204
+ const positions = [];
33205
+ const first = Math.ceil(scale.min / step);
33206
+ const last = Math.floor(scale.max / step);
33207
+ for (let index = first; index <= last; index++) {
33208
+ const position = scale.getPixelForValue(index * step);
33209
+ if (isValidPosition(position, majorPositions, scale) && !majorPositions.some((major) => Math.abs(major - position) < .1)) positions.push(position);
33210
+ }
33211
+ return positions;
33212
+ };
33213
+ var drawMinorTicksForScale = (scale, sharedStep) => {
33204
33214
  const { chart, ctx } = scale ?? {};
33205
33215
  if (!ctx || ctx?.canvas === null) return;
33206
33216
  const config = chart?.config;
@@ -33208,7 +33218,7 @@ var drawMinorTicksForScale = (scale) => {
33208
33218
  if (config?.options?.indexAxis === scale?.axis) return;
33209
33219
  const isHorizontal = scale?.axis === AxisType.X;
33210
33220
  const majorTickPositions = scale?.ticks?.map((_, index) => scale?.getPixelForTick(index))?.sort((a, b) => a - b);
33211
- const minorTickPositions = getMinorTickPositions(majorTickPositions, scale);
33221
+ const minorTickPositions = sharedStep ? getEqualScaleMinorTicks(scale, majorTickPositions, sharedStep) : getMinorTickPositions(majorTickPositions, scale);
33212
33222
  ctx?.save?.();
33213
33223
  ctx.strokeStyle = config?._config?.options?.scales?.[scale.id]?.grid?.color || "rgba(0,0,0,0.06)";
33214
33224
  ctx.lineWidth = .5;
@@ -33229,9 +33239,16 @@ var drawMinorTicksForScale = (scale) => {
33229
33239
  };
33230
33240
  var chartMinorGridlinesPlugin = {
33231
33241
  id: "minorGridlines",
33232
- beforeDatasetsDraw: (chart) => {
33233
- const { scales } = chart ?? {};
33234
- Object.keys(scales)?.forEach((id) => drawMinorTicksForScale(scales?.[id]));
33242
+ beforeDatasetsDraw: (chart, _args, { equalScale }) => {
33243
+ const { scales } = chart;
33244
+ const { x, y } = scales;
33245
+ let sharedStep;
33246
+ if (equalScale && Object.keys(scales).length === 2 && x?.type === "linear" && y?.type === "linear") {
33247
+ const majorSteps = [x, y].map((scale) => getLargestMajorTickWidth(scale.ticks.map(({ value }) => value)));
33248
+ const step = Math.min(...majorSteps) / MINOR_TICKS_PER_MAJOR;
33249
+ if (Number.isFinite(step) && step > 0) sharedStep = step;
33250
+ }
33251
+ Object.values(scales).forEach((scale) => drawMinorTicksForScale(scale, sharedStep));
33235
33252
  }
33236
33253
  };
33237
33254
  // src/components/common/helpers/chart-consts.ts
@@ -33633,9 +33650,10 @@ var storeChartStateInStorage = (state, persistenceId) => {
33633
33650
  Initial chart state for the line chart.
33634
33651
  */
33635
33652
  var initialState = ({ options, persistenceId }) => {
33636
- const { chartOptions: { enableZoom, enablePan, showPoints, showLine, enableDragAnnotation }, legend: { display }, dragData } = options;
33653
+ const { chartOptions: { enableZoom, enablePan, showPoints, showLine, enableDragAnnotation }, legend: { display }, dragData, additionalAxesOptions } = options;
33637
33654
  const { zoomEnabled, panEnabled, pointsEnabled, lineEnabled, legendEnabled, enableDragPoints, enableDragAnnotation: enableDragAnnotationStorage } = getChartStateFromStorage(persistenceId) || {};
33638
33655
  return {
33656
+ equalScaleEnabled: !!additionalAxesOptions.allowEqualScale && !!additionalAxesOptions.defaultEqualScaleEnabled,
33639
33657
  zoomEnabled: enableDragAnnotation ?? enableDragAnnotationStorage ? false : zoomEnabled ?? enableZoom,
33640
33658
  panEnabled: panEnabled ?? enablePan,
33641
33659
  pointsEnabled: pointsEnabled ?? showPoints,
@@ -34566,7 +34584,8 @@ var defaultAxes$2 = (axes) => {
34566
34584
  };
34567
34585
  };
34568
34586
  var defaultAdditionalAxesOptions$2 = (options) => ({
34569
- allowEqualScale: options?.allowEqualScale ?? false,
34587
+ allowEqualScale: options?.allowEqualScale ?? true,
34588
+ defaultEqualScaleEnabled: options?.defaultEqualScaleEnabled ?? false,
34570
34589
  chartScaleType: options?.chartScaleType || "linear",
34571
34590
  reverse: options?.reverse || false,
34572
34591
  beginAtZero: options?.beginAtZero ?? false,
@@ -36544,6 +36563,7 @@ var useChartOptions = ({ chartRef, state, options, dispatch, generatedDatasets,
36544
36563
  const lineMarkersAnnotation = options?.annotations?.lineMarkersAnnotation ?? {};
36545
36564
  const lineMarkersPluginEnabled = Boolean(options?.plugins?.lineMarkersPlugin?.enabled ?? lineMarkersAnnotation?.enabled);
36546
36565
  const plugins = useMemo(() => ({
36566
+ minorGridlines: { equalScale: !!options.additionalAxesOptions.allowEqualScale && !!state.equalScaleEnabled },
36547
36567
  equalAxisScale: { enabled: !!options.additionalAxesOptions.allowEqualScale && !!state.equalScaleEnabled },
36548
36568
  datalabels,
36549
36569
  annotationDraggerPlugin: { enabled: state?.enableDragAnnotation },
@@ -36673,6 +36693,9 @@ var createEqualScalePlugin = (onAxesUpdate) => {
36673
36693
  let fitting = false;
36674
36694
  return {
36675
36695
  id: "equalAxisScale",
36696
+ beforeRender() {
36697
+ if (fitting) return false;
36698
+ },
36676
36699
  afterUpdate(chart, _args, { enabled }) {
36677
36700
  if (fitting) return;
36678
36701
  const { x, y } = chart.scales;
@@ -67,11 +67,13 @@ export interface ILineRange {
67
67
  /** LineChart scale defaults and per-axis range overrides. */
68
68
  export interface ILineChartAdditionalAxesOptions {
69
69
  /**
70
- * Offers a toggle for equal units per pixel on one pair of linear X/Y axes. Defaults to false.
70
+ * Offers a toggle for equal units per pixel on one pair of linear X/Y axes. Defaults to true.
71
71
  * Both axes must use the same physical units; no unit conversion is performed.
72
- * The toggle starts off and expands the requested viewport without changing the canvas shape.
72
+ * Equal scaling expands the requested viewport without changing the canvas shape.
73
73
  */
74
74
  allowEqualScale?: boolean;
75
+ /** Initial toggle value when allowEqualScale is true. Defaults to false; later changes do not reset user choice. */
76
+ defaultEqualScaleEnabled?: boolean;
75
77
  /** Scale family used when constructing axes. */
76
78
  chartScaleType?: 'linear' | 'logarithmic' | 'time' | 'timeseries';
77
79
  /** Reverses the Y scale direction. Does not change tooltip axis selection. */
@@ -1,8 +1,7 @@
1
- import { Chart, Scale } from 'chart.js';
1
+ import { Scale, Plugin, ChartType } from 'chart.js';
2
2
  export declare const getLargestMajorTickWidth: (majorTickPositions: number[]) => number;
3
3
  export declare const isValidPosition: (minorTickPosition: number, majorTickPositions: number[], scale: Scale) => boolean;
4
4
  export declare const getMinorTickPositions: (majorTickPositions: number[], scale: Scale) => number[];
5
- export declare const chartMinorGridlinesPlugin: {
6
- id: string;
7
- beforeDatasetsDraw: (chart: Chart) => void;
8
- };
5
+ export declare const chartMinorGridlinesPlugin: Plugin<ChartType, {
6
+ equalScale?: boolean;
7
+ }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/charts-library",
3
- "version": "9.1.0-beta-2",
3
+ "version": "9.1.0-beta-4",
4
4
  "description": "React Chart Library (based on Chart.js and react-chart-js-2)",
5
5
  "homepage": "https://gitlab.com/oliasoft-open-source/charts-library",
6
6
  "bugs": {