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 missing legend in Bar chart #362

Merged
merged 2 commits into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 11 additions & 9 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,23 @@ const Bar = props => {
targetKey: 'data.fill',
})

const legendDataForKeys = result.bars
.filter(bar => bar.data.index === 0)
const legendDataForKeys = uniqBy(result.bars
Copy link
Contributor Author

@AhyoungRyu AhyoungRyu Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't wrap this \w uniqBy then duplicated legends will be generated

.map(bar => ({
id: bar.data.id,
label: bar.data.id,
color: bar.color,
fill: bar.data.fill,
}))
.reverse()
.reverse(), (({id}) => id))

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,
}))
const legendDataForIndexes = uniqBy(result.bars
.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
12 changes: 6 additions & 6 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,22 @@ class BarCanvas extends Component {
})

this.ctx.strokeStyle = '#dddddd'
const legendDataForKeys = result.bars
.filter(bar => bar.data.index === 0)

const legendDataForKeys = uniqBy(result.bars
.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])
.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