Skip to content

Commit

Permalink
feat(stream): add support for legends on Stream component
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Dec 7, 2017
1 parent df1d308 commit 7939535
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/nivo-stream/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.33.0-0",
"d3-format": "^1.2.0",
"d3-scale": "^1.0.6",
"d3-shape": "^1.2.0",
Expand Down
19 changes: 19 additions & 0 deletions packages/nivo-stream/src/Stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { min, max, range, sortBy } from 'lodash'
import { bindDefs } from '@nivo/core'
import { Container, SvgWrapper } from '@nivo/core'
import { Axes, Grid } from '@nivo/core'
import { BoxLegendSvg } from '@nivo/legends'
import StreamLayers from './StreamLayers'
import StreamSlices from './StreamSlices'
import { StreamPropTypes } from './props'
Expand Down Expand Up @@ -59,6 +60,8 @@ const Stream = ({

// stack tooltip
enableStackTooltip,

legends,
}) => {
const enhancedLayers = layers.map((points, i) => {
const layer = points.map((point, i) => ({
Expand Down Expand Up @@ -148,6 +151,22 @@ const Stream = ({
tooltipFormat={tooltipFormat}
/>
)}
{legends.map((legend, i) => {
const legendData = enhancedLayers.map(l => ({
label: l.id,
fill: l.color,
})).reverse()

return (
<BoxLegendSvg
key={i}
{...legend}
containerWidth={width}
containerHeight={height}
data={legendData}
/>
)
})}
</SvgWrapper>
)}
</Container>
Expand Down
5 changes: 5 additions & 0 deletions packages/nivo-stream/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import PropTypes from 'prop-types'
import { areaCurvePropType, stackOrderPropType, stackOffsetPropType } from '@nivo/core'
import { LegendPropShape } from '@nivo/legends'

export const StreamPropTypes = {
// data
Expand Down Expand Up @@ -55,6 +56,8 @@ export const StreamPropTypes = {
isInteractive: PropTypes.bool,
enableStackTooltip: PropTypes.bool.isRequired,
tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),

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

export const StreamDefaultProps = {
Expand All @@ -81,4 +84,6 @@ export const StreamDefaultProps = {

// stack tooltip
enableStackTooltip: true,

legends: [],
}
20 changes: 19 additions & 1 deletion website/src/components/charts/stream/Stream.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 { Link } from 'react-router-dom'
import MediaQuery from 'react-responsive'
import { ResponsiveStream, StreamDefaultProps } from '@nivo/stream'
import ChartHeader from '../../ChartHeader'
Expand All @@ -23,7 +24,20 @@ import propsMapper from './propsMapper'
export default class Stream extends Component {
state = {
...generateLightDataSet(),
settings: defaultProps,
settings: {
...defaultProps,
legends: [
{
anchor: 'bottom-right',
direction: 'column',
translateX: 100,
itemWidth: 80,
itemHeight: 20,
symbolSize: 12,
symbolShape: 'circle',
},
],
},
}

handleSettingsUpdate = settings => {
Expand Down Expand Up @@ -58,6 +72,10 @@ export default class Stream extends Component {
<p className="description">
The responsive alternative of this component is <code>ResponsiveStream</code>.
</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/stream/defaultProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { patternDotsDef, patternSquaresDef } from '@nivo/core'
export default {
margin: {
top: 50,
right: 60,
right: 110,
bottom: 50,
left: 60,
},
Expand Down

0 comments on commit 7939535

Please sign in to comment.