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 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
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