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

Optimizations for preformatted data #1212

Merged
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
4 changes: 4 additions & 0 deletions packages/victory-area/src/helper-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const getDataWithBaseline = (props, scale) => {
const minY = Math.min(...domainY) > 0 ? Math.min(...domainY) : defaultMin;

return data.map((datum) => {
if ((datum._y1 !== undefined || datum._y !== undefined) && datum._y0 !== undefined) {
return datum;
}

const _y1 = datum._y1 !== undefined ? datum._y1 : datum._y;
const _y0 = datum._y0 !== undefined ? datum._y0 : minY;
return assign({}, datum, { _y0, _y1 });
Expand Down
58 changes: 34 additions & 24 deletions packages/victory-core/src/victory-util/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
property,
orderBy,
isEmpty,
isEqual,
includes
} from "lodash";
import Helpers from "./helpers";
Expand Down Expand Up @@ -166,7 +167,8 @@ function formatData(dataset, props, expectedKeys) {
return [];
}

expectedKeys = Array.isArray(expectedKeys) ? expectedKeys : ["x", "y", "y0"];
const defaultKeys = ["x", "y", "y0"];
expectedKeys = Array.isArray(expectedKeys) ? expectedKeys : defaultKeys;

const stringMap = {
x: expectedKeys.indexOf("x") !== -1 ? createStringMap(props, "x") : undefined,
Expand All @@ -183,31 +185,39 @@ function formatData(dataset, props, expectedKeys) {
return memo;
}, {});

const data = dataset.reduce((dataArr, datum, index) => {
// eslint-disable-line complexity
datum = parseDatum(datum);
const fallbackValues = { x: index, y: datum };
const processedValues = expectedKeys.reduce((memo, type) => {
const processedValue = accessor[type](datum);
const value = processedValue !== undefined ? processedValue : fallbackValues[type];
if (value !== undefined) {
if (typeof value === "string" && stringMap[type]) {
memo[`${type}Name`] = value;
memo[`_${type}`] = stringMap[type][value];
} else {
memo[`_${type}`] = value;
const preformattedData =
isEqual(expectedKeys, defaultKeys) &&
props.x === "_x" &&
props.y === "_y" &&
props.y0 === "_y0";

const data = preformattedData
? dataset
: dataset.reduce((dataArr, datum, index) => {
// eslint-disable-line complexity
datum = parseDatum(datum);
const fallbackValues = { x: index, y: datum };
const processedValues = expectedKeys.reduce((memo, type) => {
const processedValue = accessor[type](datum);
const value = processedValue !== undefined ? processedValue : fallbackValues[type];
if (value !== undefined) {
if (typeof value === "string" && stringMap[type]) {
memo[`${type}Name`] = value;
memo[`_${type}`] = stringMap[type][value];
} else {
memo[`_${type}`] = value;
}
}
return memo;
}, {});

const formattedDatum = assign({}, processedValues, datum);
if (!isEmpty(formattedDatum)) {
dataArr.push(formattedDatum);
}
}
return memo;
}, {});

const formattedDatum = assign({}, processedValues, datum);
if (!isEmpty(formattedDatum)) {
dataArr.push(formattedDatum);
}

return dataArr;
}, []);
return dataArr;
}, []);

const sortedData = sortData(data, props.sortKey, props.sortOrder);
const cleanedData = cleanData(sortedData, props);
Expand Down