Skip to content

Commit

Permalink
feat(radar): add support for legends on Radar component
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Dec 7, 2017
1 parent b6a4595 commit 415ac59
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 14 deletions.
10 changes: 6 additions & 4 deletions packages/nivo-line/src/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ const Line = ({
/>
)}
{legends.map((legend, i) => {
const legendData = lines.map(line => ({
label: line.id,
fill: line.color
})).reverse()
const legendData = lines
.map(line => ({
label: line.id,
fill: line.color,
}))
.reverse()

return (
<BoxLegendSvg
Expand Down
4 changes: 1 addition & 3 deletions packages/nivo-line/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ export const LinePropTypes = {
enableStackTooltip: PropTypes.bool.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),

legends: PropTypes.arrayOf(
PropTypes.shape(LegendPropShape)
).isRequired,
legends: PropTypes.arrayOf(PropTypes.shape(LegendPropShape)).isRequired,
}

export const LineDefaultProps = {
Expand Down
1 change: 1 addition & 0 deletions packages/nivo-radar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"jsnext:main": "es/index.js",
"dependencies": {
"@nivo/core": "0.32.0",
"@nivo/legends": "0.32.0",
"d3-format": "^1.2.0",
"d3-scale": "^1.0.6",
"d3-shape": "^1.2.0",
Expand Down
25 changes: 24 additions & 1 deletion packages/nivo-radar/src/Radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import compose from 'recompose/compose'
import pure from 'recompose/pure'
import withPropsOnChange from 'recompose/withPropsOnChange'
import defaultProps from 'recompose/defaultProps'
import { scaleLinear } from 'd3-scale'
import { closedCurvePropType } from '@nivo/core'
import { withTheme, withColors, withCurve, withDimensions, withMotion } from '@nivo/core'
import { scaleLinear } from 'd3-scale'
import { getAccessorFor } from '@nivo/core'
import { Container, SvgWrapper } from '@nivo/core'
import { LegendPropShape, BoxLegendSvg } from '@nivo/legends'
import RadarShapes from './RadarShapes'
import RadarGrid from './RadarGrid'
import RadarTooltip from './RadarTooltip'
Expand All @@ -39,6 +40,8 @@ const Radar = ({
centerX,
centerY,
margin,
width,
height,
outerWidth,
outerHeight,

Expand Down Expand Up @@ -76,13 +79,20 @@ const Radar = ({
// interactivity
isInteractive,
tooltipFormat,

legends,
}) => {
const motionProps = {
animate,
motionDamping,
motionStiffness,
}

const legendData = keys.map(key => ({
label: key,
fill: colorByKey[key],
}))

return (
<Container isInteractive={isInteractive} theme={theme}>
{({ showTooltip, hideTooltip }) => (
Expand Down Expand Up @@ -146,6 +156,15 @@ const Radar = ({
/>
)}
</g>
{legends.map((legend, i) => (
<BoxLegendSvg
key={i}
{...legend}
containerWidth={width}
containerHeight={height}
data={legendData}
/>
))}
</SvgWrapper>
)}
</Container>
Expand Down Expand Up @@ -193,6 +212,8 @@ Radar.propTypes = {
// interactivity
isInteractive: PropTypes.bool.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),

legends: PropTypes.arrayOf(PropTypes.shape(LegendPropShape)).isRequired,
}

export const RadarDefaultProps = {
Expand All @@ -215,6 +236,8 @@ export const RadarDefaultProps = {

// interactivity
isInteractive: true,

legends: [],
}

const enhance = compose(
Expand Down
12 changes: 8 additions & 4 deletions website/src/components/charts/line/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export default class Line extends Component {
itemWidth: 80,
itemHeight: 20,
symbolSize: 12,
symbolShape: 'circle'
}
]
symbolShape: 'circle',
},
],
},
}

Expand All @@ -52,7 +52,7 @@ export default class Line extends Component {

const code = generateCode('Line', mappedSettings, {
pkg: '@nivo/line',
defaults: LineDefaultProps
defaults: LineDefaultProps,
})

const header = (
Expand Down Expand Up @@ -100,6 +100,10 @@ export default class Line extends Component {
the storybook
</a>.
</p>
<p className="description">
See the <Link to="/guides/legends">dedicated guide</Link> on how to setup
legends for this component.
</p>
</div>
)

Expand Down
2 changes: 1 addition & 1 deletion website/src/components/charts/line/LinePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, { Component } from 'react'
import Helmet from 'react-helmet'
import { generateDrinkStats } from '@nivo/generators'

const generateData = () => generateDrinkStats(12)
const generateData = () => generateDrinkStats(9)

class LinePage extends Component {
state = {
Expand Down
15 changes: 14 additions & 1 deletion website/src/components/charts/radar/Radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import MediaQuery from 'react-responsive'
import { Link } from 'react-router-dom'
import { ResponsiveRadar } from '@nivo/radar'
import ChartHeader from '../../ChartHeader'
import ChartTabs from '../../ChartTabs'
import RadarControls from './RadarControls'
import { ResponsiveRadar } from '@nivo/radar'
import generateCode from '../../../lib/generateChartCode'
import ComponentPropsDocumentation from '../../properties/ComponentPropsDocumentation'
import properties from './props'
Expand Down Expand Up @@ -67,6 +67,19 @@ export default class Radar extends Component {
motionDamping: 15,

isInteractive: true,

legends: [
{
anchor: 'top-left',
direction: 'column',
translateX: -50,
translateY: -40,
itemWidth: 80,
itemHeight: 20,
symbolSize: 12,
symbolShape: 'circle',
},
],
},
}

Expand Down

0 comments on commit 415ac59

Please sign in to comment.