Enable suggestedMin and suggestedMax setts for logarithmic axes (#7955)

This commit is contained in:
Evert Timberg 2020-10-25 10:38:41 -04:00 committed by GitHub
parent f76cd48f35
commit c428797592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 1 deletions

View File

@ -4,6 +4,15 @@ title: Logarithmic Axis
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.
## Configuration Options
These options extend the [common configuration for all cartesian axes](index.md#configuration-options).
| Name | Type | Description
| ---- | ---- | -----------
| `suggestedMax` | `number` | Adjustment used when calculating the maximum data value. [more...](#axis-range-settings)
| `suggestedMin` | `number` | Adjustment used when calculating the minimum data value. [more...](#axis-range-settings)
## Tick Configuration Options
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).

View File

@ -1,4 +1,4 @@
import {isFinite} from '../helpers/helpers.core';
import {isFinite, isNullOrUndef} from '../helpers/helpers.core';
import {_setMinAndMaxByKey, log10} from '../helpers/helpers.math';
import Scale from '../core/core.scale';
import LinearScaleBase from './scale.linearbase';
@ -86,11 +86,19 @@ export default class LogarithmicScale extends Scale {
handleTickRangeOptions() {
const me = this;
const {suggestedMax, suggestedMin} = me.options;
const DEFAULT_MIN = 1;
const DEFAULT_MAX = 10;
let min = me.min;
let max = me.max;
if (!isNullOrUndef(suggestedMin)) {
min = Math.min(min, suggestedMin);
}
if (!isNullOrUndef(suggestedMax)) {
max = Math.max(max, suggestedMax);
}
if (min === max) {
if (min <= 0) { // includes null
min = DEFAULT_MIN;

View File

@ -1118,4 +1118,29 @@ describe('Logarithmic Scale tests', function() {
});
});
it('Should correctly determine the max & min when no values provided and suggested minimum and maximum are set', function() {
var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
yAxisID: 'y',
data: []
}],
labels: ['a', 'b', 'c', 'd', 'e', 'f']
},
options: {
scales: {
y: {
type: 'logarithmic',
suggestedMin: 10,
suggestedMax: 100
}
}
}
});
expect(chart.scales.y).not.toEqual(undefined); // must construct
expect(chart.scales.y.min).toBe(10);
expect(chart.scales.y.max).toBe(100);
});
});

View File

@ -245,6 +245,17 @@ export const LinearScale: IChartComponent & {
export type ILogarithmicScaleOptions = ICartesianScaleOptions & {
stacked?: boolean;
/**
* Adjustment used when calculating the maximum data value.
* @see https://www.chartjs.org/docs/next/axes/cartesian/linear#axis-range-settings
*/
suggestedMin?: number;
/**
* Adjustment used when calculating the minimum data value.
* @see https://www.chartjs.org/docs/next/axes/cartesian/linear#axis-range-settings
*/
suggestedMax?: number;
ticks: {
/**
* The Intl.NumberFormat options used by the default label formatter