Skip to content

Commit

Permalink
feat: allow axis domain to accept a callback (recharts#2770)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommilligan authored Feb 10, 2022
1 parent f32431e commit e125658
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/util/ChartUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,11 @@ export type D3Scale<T> = D3ScaleContinuousNumeric<T, number>;

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 {
Expand Down
14 changes: 11 additions & 3 deletions test/specs/util/ChartUtilsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand All @@ -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', () => {
Expand Down

0 comments on commit e125658

Please sign in to comment.