Skip to content

Commit c428797

Browse files
authored
Enable suggestedMin and suggestedMax setts for logarithmic axes (#7955)
1 parent f76cd48 commit c428797

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

docs/docs/axes/cartesian/logarithmic.md

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ title: Logarithmic Axis
44

55
The logarithmic scale is used to chart numerical data. It can be placed on either the x or y-axis. As the name suggests, logarithmic interpolation is used to determine where a value lies on the axis.
66

7+
## Configuration Options
8+
9+
These options extend the [common configuration for all cartesian axes](index.md#configuration-options).
10+
11+
| Name | Type | Description
12+
| ---- | ---- | -----------
13+
| `suggestedMax` | `number` | Adjustment used when calculating the maximum data value. [more...](#axis-range-settings)
14+
| `suggestedMin` | `number` | Adjustment used when calculating the minimum data value. [more...](#axis-range-settings)
15+
716
## Tick Configuration Options
817

918
The following options are provided by the logarithmic scale. They are all located in the `ticks` sub-options. These options extend the [common tick configuration](index.md#tick-configuration).

src/scales/scale.logarithmic.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isFinite} from '../helpers/helpers.core';
1+
import {isFinite, isNullOrUndef} from '../helpers/helpers.core';
22
import {_setMinAndMaxByKey, log10} from '../helpers/helpers.math';
33
import Scale from '../core/core.scale';
44
import LinearScaleBase from './scale.linearbase';
@@ -86,11 +86,19 @@ export default class LogarithmicScale extends Scale {
8686

8787
handleTickRangeOptions() {
8888
const me = this;
89+
const {suggestedMax, suggestedMin} = me.options;
8990
const DEFAULT_MIN = 1;
9091
const DEFAULT_MAX = 10;
9192
let min = me.min;
9293
let max = me.max;
9394

95+
if (!isNullOrUndef(suggestedMin)) {
96+
min = Math.min(min, suggestedMin);
97+
}
98+
if (!isNullOrUndef(suggestedMax)) {
99+
max = Math.max(max, suggestedMax);
100+
}
101+
94102
if (min === max) {
95103
if (min <= 0) { // includes null
96104
min = DEFAULT_MIN;

test/specs/scale.logarithmic.tests.js

+25
Original file line numberDiff line numberDiff line change
@@ -1118,4 +1118,29 @@ describe('Logarithmic Scale tests', function() {
11181118
});
11191119
});
11201120

1121+
it('Should correctly determine the max & min when no values provided and suggested minimum and maximum are set', function() {
1122+
var chart = window.acquireChart({
1123+
type: 'bar',
1124+
data: {
1125+
datasets: [{
1126+
yAxisID: 'y',
1127+
data: []
1128+
}],
1129+
labels: ['a', 'b', 'c', 'd', 'e', 'f']
1130+
},
1131+
options: {
1132+
scales: {
1133+
y: {
1134+
type: 'logarithmic',
1135+
suggestedMin: 10,
1136+
suggestedMax: 100
1137+
}
1138+
}
1139+
}
1140+
});
1141+
1142+
expect(chart.scales.y).not.toEqual(undefined); // must construct
1143+
expect(chart.scales.y.min).toBe(10);
1144+
expect(chart.scales.y.max).toBe(100);
1145+
});
11211146
});

types/scales/index.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ export const LinearScale: IChartComponent & {
245245
export type ILogarithmicScaleOptions = ICartesianScaleOptions & {
246246
stacked?: boolean;
247247

248+
/**
249+
* Adjustment used when calculating the maximum data value.
250+
* @see https://www.chartjs.org/docs/next/axes/cartesian/linear#axis-range-settings
251+
*/
252+
suggestedMin?: number;
253+
/**
254+
* Adjustment used when calculating the minimum data value.
255+
* @see https://www.chartjs.org/docs/next/axes/cartesian/linear#axis-range-settings
256+
*/
257+
suggestedMax?: number;
258+
248259
ticks: {
249260
/**
250261
* The Intl.NumberFormat options used by the default label formatter

0 commit comments

Comments
 (0)