Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-chart): fix domain calculation with zero values #1383

Merged
merged 3 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 61 additions & 71 deletions packages/dx-chart-core/src/plugins/scale/computeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,54 +42,55 @@ const getCorrectAxisType = (type, data, field) => {
return type || 'linear';
};

const getFieldStack = (index, object) => (object && object[index] ? object[index] : undefined);

const calculateDomain = (series, data, axesOptions, argumentAxisName) => series.reduce(
(domains, {
valueField, argumentField, axisName, name,
}) => {
const valueType = getCorrectAxisType(
domains[axisName] && domains[axisName].type,
data,
valueField,
);
const argumentType = getCorrectAxisType(
domains[argumentAxisName] && domains[argumentAxisName].type,
data,
argumentField,
);
return {
...domains,
[axisName]: {
domain: calculateDomainField(
object => getFieldStack(1, object[`${valueField}-${name}-stack`]),
object => getFieldStack(0, object[`${valueField}-${name}-stack`]),
data,
domains[axisName] && domains[axisName].domain,
valueType,
),
orientation: VERTICAL,
type: valueType,
tickFormat: domains[axisName] && domains[axisName].tickFormat,
},
[argumentAxisName]: {
domain: calculateDomainField(
object => object[argumentField],
null,
data,
domains[argumentAxisName] && domains[argumentAxisName].domain,
argumentType,
),
orientation: HORIZONTAL,
type: argumentType,
tickFormat: domains[argumentAxisName] && domains[argumentAxisName].tickFormat,
},
};
},
axesOptions,
const getFieldStack = (index, object) => (
object && isDefined(object[index]) ? object[index] : undefined
);

const adjustRangeToZero = range => [Math.min(range[0], 0), Math.max(0, range[1])];
const calculateDomain = (series, data, axesOptions, argumentAxisName, startFromZero) => series
.reduce(
(domains, {
valueField, argumentField, axisName, name,
}) => {
const valueType = getCorrectAxisType(
domains[axisName] && domains[axisName].type,
data,
valueField,
);
const argumentType = getCorrectAxisType(
domains[argumentAxisName] && domains[argumentAxisName].type,
data,
argumentField,
);
return {
...domains,
[axisName]: {
domain: calculateDomainField(
object => getFieldStack(1, object[`${valueField}-${name}-stack`]),
startFromZero[axisName] ? object => getFieldStack(0, object[`${valueField}-${name}-stack`]) : undefined,
data,
domains[axisName] && domains[axisName].domain,
valueType,
),
orientation: VERTICAL,
type: valueType,
tickFormat: domains[axisName] && domains[axisName].tickFormat,
},
[argumentAxisName]: {
domain: calculateDomainField(
object => object[argumentField],
null,
data,
domains[argumentAxisName] && domains[argumentAxisName].domain,
argumentType,
),
orientation: HORIZONTAL,
type: argumentType,
tickFormat: domains[argumentAxisName] && domains[argumentAxisName].tickFormat,
},
};
},
axesOptions,
);

const recalculateDomain = (range, currentDomain) => ({
domain: currentDomain.type !== BAND ? range : currentDomain.domain,
Expand All @@ -98,33 +99,21 @@ const recalculateDomain = (range, currentDomain) => ({
tickFormat: currentDomain.tickFormat,
});

const adjustDomains = (axes, calculatedDomains, startFromZero) => {
const adjustedDomainsBySeries = Object.keys(calculatedDomains).reduce((domains, name) => {
const adjustDomains = (axes, calculatedDomains) => axes.reduce(
(domains, {
name, min, max,
}) => {
const currentDomain = domains[name];
const range = startFromZero[name]
? adjustRangeToZero(currentDomain.domain) : currentDomain.domain;
return {
...domains,
[name]: recalculateDomain(range, currentDomain),
[name]: recalculateDomain([
isDefined(min) ? min : currentDomain.domain[0],
isDefined(max) ? max : currentDomain.domain[1],
], currentDomain),
};
}, calculatedDomains);

return axes.reduce(
(domains, {
name, min, max,
}) => {
const currentDomain = domains[name];
return {
...domains,
[name]: recalculateDomain([
isDefined(min) ? min : currentDomain.domain[0],
isDefined(max) ? max : currentDomain.domain[1],
], currentDomain),
};
},
adjustedDomainsBySeries,
);
};
},
calculatedDomains,
);

export const computedExtension = (extension) => {
const defaultExtension = [
Expand All @@ -136,6 +125,7 @@ export const computedExtension = (extension) => {

export const domains = (axes = [], series, data, argumentAxisName, startFromZero) => {
const axesOptions = collectAxesOptions(axes);
const calculatedDomains = calculateDomain(series, data, axesOptions, argumentAxisName);
return adjustDomains(axes, calculatedDomains, startFromZero);
const calculatedDomains = calculateDomain(series, data, axesOptions,
argumentAxisName, startFromZero);
return adjustDomains(axes, calculatedDomains);
};
27 changes: 26 additions & 1 deletion packages/dx-chart-core/src/plugins/scale/computeds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('calculateDomain', () => {
[{
arg: 1, val: 9, 'val-name-stack': [0, 9],
}, {
arg: 2, val: -10, 'val-name-stack': [-10, 0],
arg: 2, val: -10, 'val-name-stack': [0, -10],
}],
'argumentAxis',
{},
Expand All @@ -66,6 +66,31 @@ describe('calculateDomain', () => {
});
});

it('should be computed from data with zero values', () => {
const calculatedDomains = domains(
[argumentAxis, valueAxis],
[{
axisName: 'valueAxis', argumentField: 'arg', valueField: 'val', name: 'name',
}],
[{
arg: 1, val: 0, 'val-name-stack': [0, 0],
}, {
arg: 2, val: 10, 'val-name-stack': [0, 10],
}],
'argumentAxis',
{},
);

expect(calculatedDomains).toEqual({
argumentAxis: {
domain: [1, 2], orientation: 'horizontal', type: 'linear', tickFormat: undefined,
},
valueAxis: {
domain: [0, 10], orientation: 'vertical', type: 'linear', tickFormat: undefined,
},
});
});

it('should be computed from data and series option, startFromZero option set for value axis', () => {
const calculatedDomains = domains(
[argumentAxis, valueAxis],
Expand Down