Skip to content

Commit

Permalink
fix(bar): fix missing legend in Bar chart #362
Browse files Browse the repository at this point in the history
* Remove unnecessary filter from legend data generation in Bar/ BarCanvas component

* Fix fmt: Bar & BarCanvas
  • Loading branch information
AhyoungRyu authored and Raphaël Benitte committed Dec 2, 2018
1 parent 8358014 commit aa12d9d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
34 changes: 20 additions & 14 deletions packages/bar/src/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/
import React, { Fragment } from 'react'
import uniqBy from 'lodash/uniqBy'
import { TransitionMotion, spring } from 'react-motion'
import { bindDefs, Container, SvgWrapper, Grid, CartesianMarkers } from '@nivo/core'
import { Axes } from '@nivo/axes'
Expand Down Expand Up @@ -152,22 +153,27 @@ const Bar = props => {
targetKey: 'data.fill',
})

const legendDataForKeys = result.bars
.filter(bar => bar.data.index === 0)
.map(bar => ({
id: bar.data.id,
label: bar.data.id,
const legendDataForKeys = uniqBy(
result.bars
.map(bar => ({
id: bar.data.id,
label: bar.data.id,
color: bar.color,
fill: bar.data.fill,
}))
.reverse(),
({ id }) => id
)

const legendDataForIndexes = uniqBy(
result.bars.map(bar => ({
id: bar.data.indexValue,
label: bar.data.indexValue,
color: bar.color,
fill: bar.data.fill,
}))
.reverse()

const legendDataForIndexes = result.bars.filter(bar => bar.data.id === keys[0]).map(bar => ({
id: bar.data.indexValue,
label: bar.data.indexValue,
color: bar.color,
fill: bar.data.fill,
}))
})),
({ id }) => id
)

return (
<Container isInteractive={isInteractive} theme={theme}>
Expand Down
31 changes: 18 additions & 13 deletions packages/bar/src/BarCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/
import React, { Component } from 'react'
import uniqBy from 'lodash/uniqBy'
import setDisplayName from 'recompose/setDisplayName'
import {
renderAxesToCanvas,
Expand Down Expand Up @@ -132,23 +133,27 @@ class BarCanvas extends Component {
})

this.ctx.strokeStyle = '#dddddd'
const legendDataForKeys = result.bars
.filter(bar => bar.data.index === 0)
.map(bar => ({
id: bar.data.id,
label: bar.data.id,
color: bar.color,
fill: bar.data.fill,
}))
.reverse()
const legendDataForIndexes = result.bars
.filter(bar => bar.data.id === keys[0])
.map(bar => ({

const legendDataForKeys = uniqBy(
result.bars
.map(bar => ({
id: bar.data.id,
label: bar.data.id,
color: bar.color,
fill: bar.data.fill,
}))
.reverse(),
({ id }) => id
)
const legendDataForIndexes = uniqBy(
result.bars.map(bar => ({
id: bar.data.indexValue,
label: bar.data.indexValue,
color: bar.color,
fill: bar.data.fill,
}))
})),
({ id }) => id
)

legends.forEach(legend => {
let legendData
Expand Down

0 comments on commit aa12d9d

Please sign in to comment.