From: Evert Timberg Date: Sun, 25 Oct 2020 14:38:41 +0000 (-0400) Subject: Enable suggestedMin and suggestedMax setts for logarithmic axes (#7955) X-Git-Tag: v3.0.0-beta.5~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c428797592cc0847c65b5b7e2c2370a6870a144c;p=thirdparty%2FChart.js.git Enable suggestedMin and suggestedMax setts for logarithmic axes (#7955) --- diff --git a/docs/docs/axes/cartesian/logarithmic.md b/docs/docs/axes/cartesian/logarithmic.md index 60c966edb..07b483387 100644 --- a/docs/docs/axes/cartesian/logarithmic.md +++ b/docs/docs/axes/cartesian/logarithmic.md @@ -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). diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 526ada835..cee47b7f1 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -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; diff --git a/test/specs/scale.logarithmic.tests.js b/test/specs/scale.logarithmic.tests.js index 699881380..905f75677 100644 --- a/test/specs/scale.logarithmic.tests.js +++ b/test/specs/scale.logarithmic.tests.js @@ -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); + }); }); diff --git a/types/scales/index.d.ts b/types/scales/index.d.ts index d37d4b6e5..857be7f64 100644 --- a/types/scales/index.d.ts +++ b/types/scales/index.d.ts @@ -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