From e1256589a191b839bb4310c4d90d77689be119aa Mon Sep 17 00:00:00 2001 From: Tom Milligan Date: Thu, 10 Feb 2022 14:54:52 +0000 Subject: [PATCH] feat: allow axis domain to accept a callback (#2770) --- src/util/ChartUtils.ts | 4 ++++ src/util/types.ts | 6 +++++- test/specs/util/ChartUtilsSpec.js | 14 +++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/util/ChartUtils.ts b/src/util/ChartUtils.ts index ff8aa168b6..01bf36ccb1 100644 --- a/src/util/ChartUtils.ts +++ b/src/util/ChartUtils.ts @@ -1046,6 +1046,10 @@ export const MIN_VALUE_REG = /^dataMin[\s]*-[\s]*([0-9]+([.]{1}[0-9]+){0,1})$/; export const MAX_VALUE_REG = /^dataMax[\s]*\+[\s]*([0-9]+([.]{1}[0-9]+){0,1})$/; export const parseSpecifiedDomain = (specifiedDomain: any, dataDomain: any, allowDataOverflow: boolean) => { + if (_.isFunction(specifiedDomain)) { + return specifiedDomain(dataDomain, allowDataOverflow); + } + if (!_.isArray(specifiedDomain)) { return dataDomain; } diff --git a/src/util/types.ts b/src/util/types.ts index aab3cc578d..88d83dbab4 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -985,7 +985,11 @@ export type D3Scale = D3ScaleContinuousNumeric; export type AxisDomainItem = string | number | Function | 'auto' | 'dataMin' | 'dataMax'; /** The domain of axis */ -export type AxisDomain = string[] | number[] | [AxisDomainItem, AxisDomainItem]; +export type AxisDomain = + | string[] + | number[] + | [AxisDomainItem, AxisDomainItem] + | (([dataMin, dataMax]: [number, number], allowDataOverflow: boolean) => [number, number]); /** The props definition of base axis */ export interface BaseAxisProps { diff --git a/test/specs/util/ChartUtilsSpec.js b/test/specs/util/ChartUtilsSpec.js index 33c2ecced0..fa97c0327a 100644 --- a/test/specs/util/ChartUtilsSpec.js +++ b/test/specs/util/ChartUtilsSpec.js @@ -44,17 +44,17 @@ describe('parseSpecifiedDomain', () => { expect(parseSpecifiedDomain(1, domain)).to.equal(domain); }); - it('DataUtils.parseSpecifiedDomain(["auto", "auto"], domain) should return null ', () => { + it('DataUtils.parseSpecifiedDomain(["auto", "auto"], domain) should return domain ', () => { const result = parseSpecifiedDomain(['auto', 'auto'], domain); expect(result).to.deep.equal(domain); }); - it('DataUtils.parseSpecifiedDomain([-1, 120], domain) should return null ', () => { + it('DataUtils.parseSpecifiedDomain([-1, 120], domain) should return input value ', () => { const result = parseSpecifiedDomain([-1, 120], domain); expect(result).to.deep.equal([-1, 120]); }); - it('DataUtils.parseSpecifiedDomain(["dataMin - 10", "dataMax + 10"], domain) should return null ', () => { + it('DataUtils.parseSpecifiedDomain(["dataMin - 10", "dataMax + 10"], domain) should return computed value ', () => { const result = parseSpecifiedDomain(['dataMin - 10', 'dataMax + 10'], domain); expect(result).to.deep.equal([10, 110]); }); @@ -66,6 +66,14 @@ describe('parseSpecifiedDomain', () => { ); expect(result).to.deep.equal([-20, 200]); }); + + it('DataUtils.parseSpecifiedDomain(callback, domain) should execute the callback and return computed value ', () => { + const result = parseSpecifiedDomain( + ([dataMin, dataMax], _allowDataOverflow) => [dataMin / 4, dataMax * 4], + domain + ); + expect(result).to.deep.equal([5, 400]); + }); }); describe('parseScale', () => {