Skip to content

Commit

Permalink
fix(bar): improve grouped bar performance (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
gallotamas committed Apr 21, 2021
1 parent 644faf4 commit f37d066
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/bar/src/compute/common.js
Expand Up @@ -28,9 +28,17 @@ export const getIndexScale = (data, getIndex, range, padding, indexScale) => {

export const normalizeData = (data, keys) =>
data.map(item => ({
...keys.reduce((acc, key) => ({ ...acc, [key]: null }), {}),
...keys.reduce((acc, key) => {
acc[key] = null
return acc
}, {}),
...item,
}))

export const filterNullValues = data =>
Object.keys(data).reduce((acc, key) => (data[key] ? { ...acc, [key]: data[key] } : acc), {})
Object.keys(data).reduce((acc, key) => {
if (data[key]) {
acc[key] = data[key]
}
return acc
}, {})
6 changes: 4 additions & 2 deletions packages/bar/src/compute/grouped.js
Expand Up @@ -42,6 +42,7 @@ const generateVerticalGroupedBars = (
const compare = reverse ? lt : gt
const getY = d => (compare(d, 0) ? yScale(d) : yRef)
const getHeight = (d, y) => (compare(d, 0) ? yRef - y : yScale(d) - yRef)
const cleanedData = data.map(filterNullValues)

const bars = flatten(
keys.map((key, i) =>
Expand All @@ -54,7 +55,7 @@ const generateVerticalGroupedBars = (
value: data[index][key],
index,
indexValue: getIndex(data[index]),
data: filterNullValues(data[index]),
data: cleanedData[index],
}

return {
Expand Down Expand Up @@ -98,6 +99,7 @@ const generateHorizontalGroupedBars = (
const compare = reverse ? lt : gt
const getX = d => (compare(d, 0) ? xRef : xScale(d))
const getWidth = (d, x) => (compare(d, 0) ? xScale(d) - xRef : xRef - x)
const cleanedData = data.map(filterNullValues)

const bars = flatten(
keys.map((key, i) =>
Expand All @@ -110,7 +112,7 @@ const generateHorizontalGroupedBars = (
value: data[index][key],
index,
indexValue: getIndex(data[index]),
data: filterNullValues(data[index]),
data: cleanedData[index],
}

return {
Expand Down

0 comments on commit f37d066

Please sign in to comment.