From 82a51caf345ffe35c35196c5773ffc369b3d332e Mon Sep 17 00:00:00 2001 From: stock Date: Wed, 7 Apr 2021 12:11:58 +0200 Subject: [PATCH 1/5] Fixes #8835 --- src/core/core.scale.autoskip.js | 2 +- src/core/core.ticks.js | 3 ++- src/scales/scale.linearbase.js | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/core/core.scale.autoskip.js b/src/core/core.scale.autoskip.js index 89d0ddfac0a..a1fca3aabbf 100644 --- a/src/core/core.scale.autoskip.js +++ b/src/core/core.scale.autoskip.js @@ -15,7 +15,7 @@ import {_factorize} from '../helpers/helpers.math'; * @private */ export function autoSkip(scale, ticks) { - const tickOpts = scale.options.ticks; + const tickOpts = scale.options.ticks.setContext(scale.getContext()); const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale); const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; const numMajorIndices = majorIndices.length; diff --git a/src/core/core.ticks.js b/src/core/core.ticks.js index 186be7c0f07..efd3d75f682 100644 --- a/src/core/core.ticks.js +++ b/src/core/core.ticks.js @@ -48,7 +48,8 @@ const formatters = { const numDecimal = Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); // toFixed has a max of 20 decimal places const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; - Object.assign(options, this.options.ticks.format); + const tickOpts = this.options.ticks.setContext(this.getContext()); + Object.assign(options, tickOpts.format); return formatNumber(tickValue, locale, options); }, diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index 3a671d269cd..fd7d937722a 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -186,7 +186,7 @@ export default class LinearScaleBase extends Scale { getTickLimit() { const me = this; - const tickOpts = me.options.ticks; + const tickOpts = me.options.ticks.setContext(me.getContext()); // eslint-disable-next-line prefer-const let {maxTicksLimit, stepSize} = tickOpts; let maxTicks; @@ -215,7 +215,7 @@ export default class LinearScaleBase extends Scale { buildTicks() { const me = this; const opts = me.options; - const tickOpts = opts.ticks; + const tickOpts = opts.ticks.setContext(me.getContext()); // Figure out what the max number of ticks we can support it is based on the size of // the axis area. For now, we say that the minimum tick spacing in pixels must be 40 From 4b4415620c2ccaa09ba4b77daed48a42ee6259d9 Mon Sep 17 00:00:00 2001 From: stock Date: Wed, 7 Apr 2021 12:51:47 +0200 Subject: [PATCH 2/5] context as argument because protected --- src/core/core.scale.autoskip.js | 5 +++-- src/core/core.scale.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/core.scale.autoskip.js b/src/core/core.scale.autoskip.js index a1fca3aabbf..39bf40be035 100644 --- a/src/core/core.scale.autoskip.js +++ b/src/core/core.scale.autoskip.js @@ -11,11 +11,12 @@ import {_factorize} from '../helpers/helpers.math'; * Returns a subset of ticks to be plotted to avoid overlapping labels. * @param {import('./core.scale').default} scale * @param {Tick[]} ticks + * @paran {Object} context * @return {Tick[]} * @private */ -export function autoSkip(scale, ticks) { - const tickOpts = scale.options.ticks.setContext(scale.getContext()); +export function autoSkip(scale, ticks, context) { + const tickOpts = scale.options.ticks.setContext(context); const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale); const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; const numMajorIndices = majorIndices.length; diff --git a/src/core/core.scale.js b/src/core/core.scale.js index bdefea4a747..f85f5c904ec 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -429,7 +429,7 @@ export default class Scale extends Element { // Auto-skip if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) { - me.ticks = autoSkip(me, me.ticks); + me.ticks = autoSkip(me, me.ticks, me.getContext()); me._labelSizes = null; } From f54f7ca3d796f0aed75effdd3bf13ccc7c0a5598 Mon Sep 17 00:00:00 2001 From: stock Date: Wed, 7 Apr 2021 13:34:01 +0200 Subject: [PATCH 3/5] fixes test case on formatters --- test/specs/core.ticks.tests.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/specs/core.ticks.tests.js b/test/specs/core.ticks.tests.js index 52857b649aa..59a969db053 100644 --- a/test/specs/core.ticks.tests.js +++ b/test/specs/core.ticks.tests.js @@ -99,7 +99,13 @@ describe('Test tick generators', function() { describe('formatters.numeric', function() { it('should not fail on empty or 1 item array', function() { - const scale = {chart: {options: {locale: 'en'}}, options: {ticks: {format: {}}}}; + const getContext = ()=>{ + return {}; + }; + const setContext = ()=>{ + return {ticks: {format: {}}}; + }; + const scale = {chart: {options: {locale: 'en'}}, options: {ticks: {format: {}, setContext}}, getContext}; expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, []])).toEqual('1'); expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, [{value: 1}]])).toEqual('1'); expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, [{value: 1}, {value: 1.01}]])).toEqual('1.00'); From 0fb9c6dc08fb56799c064d94ba34f803a97a14fa Mon Sep 17 00:00:00 2001 From: stock Date: Wed, 7 Apr 2021 15:40:15 +0200 Subject: [PATCH 4/5] reverts previous commits and sets the scale context in the scale init --- src/core/core.scale.autoskip.js | 5 ++--- src/core/core.scale.js | 4 ++-- src/core/core.ticks.js | 3 +-- src/scales/scale.linearbase.js | 4 ++-- test/specs/core.controller.tests.js | 4 ++-- test/specs/core.ticks.tests.js | 8 +------- 6 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/core/core.scale.autoskip.js b/src/core/core.scale.autoskip.js index 39bf40be035..89d0ddfac0a 100644 --- a/src/core/core.scale.autoskip.js +++ b/src/core/core.scale.autoskip.js @@ -11,12 +11,11 @@ import {_factorize} from '../helpers/helpers.math'; * Returns a subset of ticks to be plotted to avoid overlapping labels. * @param {import('./core.scale').default} scale * @param {Tick[]} ticks - * @paran {Object} context * @return {Tick[]} * @private */ -export function autoSkip(scale, ticks, context) { - const tickOpts = scale.options.ticks.setContext(context); +export function autoSkip(scale, ticks) { + const tickOpts = scale.options.ticks; const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale); const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; const numMajorIndices = majorIndices.length; diff --git a/src/core/core.scale.js b/src/core/core.scale.js index f85f5c904ec..dbdbb18c32a 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -237,7 +237,7 @@ export default class Scale extends Element { */ init(options) { const me = this; - me.options = options; + me.options = options.setContext(me.getContext()); me.axis = options.axis; @@ -429,7 +429,7 @@ export default class Scale extends Element { // Auto-skip if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) { - me.ticks = autoSkip(me, me.ticks, me.getContext()); + me.ticks = autoSkip(me, me.ticks); me._labelSizes = null; } diff --git a/src/core/core.ticks.js b/src/core/core.ticks.js index efd3d75f682..186be7c0f07 100644 --- a/src/core/core.ticks.js +++ b/src/core/core.ticks.js @@ -48,8 +48,7 @@ const formatters = { const numDecimal = Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); // toFixed has a max of 20 decimal places const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; - const tickOpts = this.options.ticks.setContext(this.getContext()); - Object.assign(options, tickOpts.format); + Object.assign(options, this.options.ticks.format); return formatNumber(tickValue, locale, options); }, diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index fd7d937722a..3a671d269cd 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -186,7 +186,7 @@ export default class LinearScaleBase extends Scale { getTickLimit() { const me = this; - const tickOpts = me.options.ticks.setContext(me.getContext()); + const tickOpts = me.options.ticks; // eslint-disable-next-line prefer-const let {maxTicksLimit, stepSize} = tickOpts; let maxTicks; @@ -215,7 +215,7 @@ export default class LinearScaleBase extends Scale { buildTicks() { const me = this; const opts = me.options; - const tickOpts = opts.ticks.setContext(me.getContext()); + const tickOpts = opts.ticks; // Figure out what the max number of ticks we can support it is based on the size of // the axis area. For now, we say that the minimum tick spacing in pixels must be 40 diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index fee6832d62c..9d4e7aa96e3 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -413,7 +413,7 @@ describe('Chart', function() { }); expect(chart.scales.x.type).toBe('logarithmic'); - expect(chart.scales.x.options).toBe(chart.options.scales.x); + expect(chart.scales.x.options).toEqual(chart.options.scales.x); expect(chart.scales.x.options).toEqual( jasmine.objectContaining({ _jasmineCheckA: 'a0', @@ -423,7 +423,7 @@ describe('Chart', function() { })); expect(chart.scales.y.type).toBe('time'); - expect(chart.scales.y.options).toBe(chart.options.scales.y); + expect(chart.scales.y.options).toEqual(chart.options.scales.y); expect(chart.scales.y.options).toEqual( jasmine.objectContaining({ _jasmineCheckA: 'a0', diff --git a/test/specs/core.ticks.tests.js b/test/specs/core.ticks.tests.js index 59a969db053..52857b649aa 100644 --- a/test/specs/core.ticks.tests.js +++ b/test/specs/core.ticks.tests.js @@ -99,13 +99,7 @@ describe('Test tick generators', function() { describe('formatters.numeric', function() { it('should not fail on empty or 1 item array', function() { - const getContext = ()=>{ - return {}; - }; - const setContext = ()=>{ - return {ticks: {format: {}}}; - }; - const scale = {chart: {options: {locale: 'en'}}, options: {ticks: {format: {}, setContext}}, getContext}; + const scale = {chart: {options: {locale: 'en'}}, options: {ticks: {format: {}}}}; expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, []])).toEqual('1'); expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, [{value: 1}]])).toEqual('1'); expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, [{value: 1}, {value: 1.01}]])).toEqual('1.00'); From 3660c43a9d61ed522c3156836690902bdb77366b Mon Sep 17 00:00:00 2001 From: stock Date: Wed, 7 Apr 2021 15:58:34 +0200 Subject: [PATCH 5/5] changes documentation about linear ticks, adding scriptable Yes --- docs/axes/cartesian/linear.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/axes/cartesian/linear.md b/docs/axes/cartesian/linear.md index bf82fc30509..2aaf9c53205 100644 --- a/docs/axes/cartesian/linear.md +++ b/docs/axes/cartesian/linear.md @@ -23,13 +23,13 @@ Namespace: `options.scales[scaleId]` Namespace: `options.scales[scaleId].ticks` -| Name | Type | Default | Description -| ---- | ---- | ------- | ----------- -| `count` | `number` | `undefined` | The number of ticks to generate. If specified, this overrides the automatic generation. -| `format` | `object` | | The [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) options used by the default label formatter -| `maxTicksLimit` | `number` | `11` | Maximum number of ticks and gridlines to show. -| `precision` | `number` | | if defined and `stepSize` is not specified, the step size will be rounded to this many decimal places. -| `stepSize` | `number` | | User-defined fixed step size for the scale. [more...](#step-size) +| Name | Type | Scriptable | Default | Description +| ---- | ---- | ------- | ------- | ----------- +| `count` | `number` | Yes | `undefined` | The number of ticks to generate. If specified, this overrides the automatic generation. +| `format` | `object` | Yes | | The [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) options used by the default label formatter +| `maxTicksLimit` | `number` | Yes | `11` | Maximum number of ticks and gridlines to show. +| `precision` | `number` | Yes | | if defined and `stepSize` is not specified, the step size will be rounded to this many decimal places. +| `stepSize` | `number` | Yes | | User-defined fixed step size for the scale. [more...](#step-size) !!!include(axes/cartesian/_common_ticks.md)!!!