Skip to content

Commit

Permalink
chore(react-chart): min/max applying for scale processing (DevExpress…
Browse files Browse the repository at this point in the history
  • Loading branch information
Krijovnick authored May 15, 2018
1 parent 34259eb commit 2fb2ed6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { HORIZONTAL, VERTICAL, BAND } from '../../constants';

const isDefined = item => item !== undefined;

const getAxesDomains = axes =>
const collectAxesTypes = axes =>
axes.reduce(
(domains, {
name, min, max, type,
name, type,
}) => ({
...domains,
[name]: {
domain: [min, max].filter(isDefined),
type,
},
}),
Expand All @@ -29,7 +28,7 @@ const calculateDomainField = (field1, field2, data, domain = [], type) => {
]);
};

const calculateDomain = (series, data, axesDomains, argumentAxisName) =>
const calculateDomain = (series, data, axesTypes, argumentAxisName) =>
series.reduce(
(domains, {
valueField, argumentField, axisName, name,
Expand Down Expand Up @@ -58,10 +57,31 @@ const calculateDomain = (series, data, axesDomains, argumentAxisName) =>
type: domains[argumentAxisName] && domains[argumentAxisName].type,
},
}),
axesDomains,
axesTypes,
);

const adjustDomains = (axes, calculatedDomains) => axes.reduce(
(domains, {
name, min, max, type,
}) => {
const currentDomain = domains[name];
return {
...domains,
[name]: {
domain: type !== BAND ? [
isDefined(min) ? min : currentDomain.domain[0],
isDefined(max) ? max : currentDomain.domain[1],
] : currentDomain.domain,
type: currentDomain.type,
orientation: currentDomain.orientation,
},
};
},
calculatedDomains,
);

export const domains = (axes, series, data, argumentAxisName) => {
const axesDomains = getAxesDomains(axes);
return calculateDomain(series, data, axesDomains, argumentAxisName);
const axesTypes = collectAxesTypes(axes);
const calculatedDomains = calculateDomain(series, data, axesTypes, argumentAxisName);
return adjustDomains(axes, calculatedDomains);
};
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,58 @@ describe('calculateDomain', () => {
},
});
});

it('should be computed from data and min/max of axis', () => {
const calculatedDomains = domains(
[argumentAxis, { ...valueAxis, min: 3, max: 7 }],
[{
axisName: 'valueAxis', argumentField: 'arg', valueField: 'val', name: 'name',
}],
[{ arg: 1, val: 9, 'val-name-end': 9 }, { arg: 4, val: 1, 'val-name-end': 1 }],
'argumentAxis',
);

expect(calculatedDomains).toEqual({
argumentAxis: { domain: [1, 4], orientation: 'horizontal' },
valueAxis: { domain: [3, 7], orientation: 'vertical' },
});
});

it('should be computed from data and max of axis', () => {
const calculatedDomains = domains(
[argumentAxis, { ...valueAxis, max: 7 }],
[{
axisName: 'valueAxis', argumentField: 'arg', valueField: 'val', name: 'name',
}],
[{ arg: 1, val: 9, 'val-name-end': 9 }, { arg: 4, val: 1, 'val-name-end': 1 }],
'argumentAxis',
);

expect(calculatedDomains).toEqual({
argumentAxis: { domain: [1, 4], orientation: 'horizontal' },
valueAxis: { domain: [1, 7], orientation: 'vertical' },
});
});

it('should be computed from data, type is band and axis min/max is ignore', () => {
const calculatedDomains = domains(
[{
...argumentAxis, min: 1, max: 7, type: 'band',
},
valueAxis,
],
[{
axisName: 'valueAxis', argumentField: 'arg', valueField: 'val', name: 'name',
}],
[{ arg: 'one', val: 9, 'val-name-end': 9 },
{ arg: 'two', val: 1, 'val-name-end': 1 },
{ arg: 'three', val: 1, 'val-name-end': 1 }],
'argumentAxis',
);

expect(calculatedDomains).toEqual({
argumentAxis: { domain: ['one', 'two', 'three'], orientation: 'horizontal', type: 'band' },
valueAxis: { domain: [1, 9], orientation: 'vertical' },
});
});
});

0 comments on commit 2fb2ed6

Please sign in to comment.