]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Enable suggestedMin and suggestedMax setts for logarithmic axes (#7955)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sun, 25 Oct 2020 14:38:41 +0000 (10:38 -0400)
committerGitHub <noreply@github.com>
Sun, 25 Oct 2020 14:38:41 +0000 (10:38 -0400)
docs/docs/axes/cartesian/logarithmic.md
src/scales/scale.logarithmic.js
test/specs/scale.logarithmic.tests.js
types/scales/index.d.ts

index 60c966edbc197662d73bfd4e0750778a56f3bd71..07b48338784d4e3fd3625271cbafb9cd0c711f87 100644 (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).
index 526ada83559b3d6d3345402f7541d5e25fd54c94..cee47b7f1c8b01c6ddd4f3b1101e3a445df351f1 100644 (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;
index 69988138099e81909ef0f65ea22c186fda22121c..905f756771dcada420abd728d24693a5aac40ce4 100644 (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);
+       });
 });
index d37d4b6e5eeebf19c0440ff819cc92de604ad6a4..857be7f64e5f8c802cfa5ff8d33fa54571cd9673 100644 (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