Skip to content

Commit 2dde8c5

Browse files
committed
wip
1 parent d6f2cff commit 2dde8c5

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

__mocks__/dh-core.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,8 @@ class Axis {
17891789
formatPattern = null,
17901790
log = false,
17911791
businessCalendar = null,
1792+
minRange = undefined,
1793+
maxRange = undefined,
17921794
} = {}) {
17931795
this.label = label;
17941796
this.type = type;
@@ -1797,6 +1799,8 @@ class Axis {
17971799
this.formatPattern = formatPattern;
17981800
this.log = log;
17991801
this.businessCalendar = businessCalendar;
1802+
this.minRange = minRange;
1803+
this.maxRange = maxRange;
18001804
}
18011805

18021806
range() {}

packages/chart/src/ChartTestUtils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ class ChartTestUtils {
2424
formatType = undefined,
2525
formatPattern = '###,###0.00',
2626
log = false,
27+
minRange = undefined,
28+
maxRange = undefined,
2729
}: {
2830
label?: string;
2931
type?: DhType.plot.AxisType;
3032
position?: DhType.plot.AxisPosition;
3133
formatType?: DhType.plot.AxisFormatType;
3234
formatPattern?: string;
3335
log?: boolean;
36+
minRange?: number;
37+
maxRange?: number;
3438
} = {}): DhType.plot.Axis {
3539
const { dh } = this;
3640
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -41,6 +45,8 @@ class ChartTestUtils {
4145
formatType: formatType ?? dh.plot.AxisFormatType.NUMBER,
4246
formatPattern,
4347
log,
48+
minRange,
49+
maxRange,
4450
});
4551
}
4652

packages/chart/src/ChartUtils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,22 @@ class ChartUtils {
15391539
);
15401540
layoutAxis.domain = [bottom, top];
15411541
}
1542+
1543+
// autorangeoptions exists in plotly.js but not in the types
1544+
// axis.range only applies to the initial axis range, can't handle specifying min or max independently,
1545+
// and cannot be returned to if the user changes the range via interaction
1546+
// minallowed and maxallowed can be set independently and can be returned to if the user changes
1547+
// the range via interaction
1548+
const { minRange, maxRange, log } = axis;
1549+
if (!Number.isNaN(minRange) || !Number.isNaN(maxRange)) {
1550+
(layoutAxis as any).autorangeoptions = {};
1551+
if (!Number.isNaN(minRange)) {
1552+
(layoutAxis as any).autorangeoptions.minallowed = log ? Math.log10(minRange) : minRange;
1553+
}
1554+
if (!Number.isNaN(maxRange)) {
1555+
(layoutAxis as any).autorangeoptions.maxallowed = log ? Math.log10(maxRange) : maxRange;;
1556+
}
1557+
}
15421558
}
15431559

15441560
/**

packages/chart/src/FigureChartModel.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,77 @@ describe('legend visibility', () => {
533533
]);
534534
});
535535
});
536+
537+
it('handles axes min and max properly', () => {
538+
const xaxis = chartTestUtils.makeAxis({
539+
label: 'x1',
540+
type: dh.plot.AxisType.X,
541+
position: dh.plot.AxisPosition.BOTTOM,
542+
minRange: 0,
543+
maxRange: 100,
544+
});
545+
546+
const xaxis2 = chartTestUtils.makeAxis({
547+
label: 'x1',
548+
type: dh.plot.AxisType.X,
549+
position: dh.plot.AxisPosition.BOTTOM,
550+
log: true,
551+
minRange: 100,
552+
maxRange: 1000,
553+
});
554+
555+
const yaxis1 = chartTestUtils.makeAxis({
556+
label: 'y1',
557+
type: dh.plot.AxisType.Y,
558+
position: dh.plot.AxisPosition.LEFT,
559+
maxRange: 200,
560+
});
561+
562+
const yaxis2 = chartTestUtils.makeAxis({
563+
label: 'y2',
564+
type: dh.plot.AxisType.Y,
565+
position: dh.plot.AxisPosition.RIGHT,
566+
minRange: -10,
567+
});
568+
569+
const axes = [xaxis, xaxis2, yaxis1, yaxis2];
570+
571+
const chart = chartTestUtils.makeChart({ axes });
572+
const figure = chartTestUtils.makeFigure({ charts: [chart] });
573+
const model = new FigureChartModel(dh, figure);
574+
575+
const layout = model.getLayout();
576+
577+
expect((layout.xaxis as any).autorangeoptions.minallowed).toEqual(
578+
0
579+
);
580+
581+
expect((layout.xaxis as any).autorangeoptions.maxallowed).toEqual(
582+
100
583+
);
584+
585+
expect((layout.xaxis2 as any).autorangeoptions.minallowed).toEqual(
586+
2
587+
);
588+
589+
expect((layout.xaxis2 as any).autorangeoptions.maxallowed).toEqual(
590+
3
591+
);
592+
593+
expect((layout.yaxis as any).autorangeoptions.minallowed).toEqual(
594+
undefined
595+
);
596+
597+
expect((layout.yaxis as any).autorangeoptions.maxallowed).toEqual(
598+
200
599+
);
600+
601+
expect((layout.yaxis2 as any).autorangeoptions.minallowed).toEqual(
602+
-10
603+
);
604+
605+
expect((layout.yaxis2 as any).autorangeoptions.maxallowed).toEqual(
606+
undefined
607+
);
608+
609+
});

0 commit comments

Comments
 (0)