diff --git a/packages/waffle/src/Waffle.js b/packages/waffle/src/Waffle.js index 92af4bec9..fa5126245 100644 --- a/packages/waffle/src/Waffle.js +++ b/packages/waffle/src/Waffle.js @@ -79,6 +79,7 @@ export class Waffle extends Component { cellSize, origin, computedData, + legendData, legends, } = this.props @@ -87,12 +88,6 @@ export class Waffle extends Component { cell.color = emptyColor }) - const legendData = computedData.map(datum => ({ - id: datum.id, - label: datum.id, - fill: datum.fill, - })) - return ( {({ showTooltip, hideTooltip }) => { diff --git a/packages/waffle/src/enhance.js b/packages/waffle/src/enhance.js index e04bf2f15..d257f2419 100644 --- a/packages/waffle/src/enhance.js +++ b/packages/waffle/src/enhance.js @@ -54,6 +54,13 @@ const commonEnhancers = [ }), } }), + withPropsOnChange(['computedData'], ({ computedData }) => ({ + legendData: computedData.map(datum => ({ + id: datum.id, + label: datum.id, + fill: datum.fill || datum.color, + })), + })), ] export default Component => { diff --git a/website/src/components/charts/bar/Bar.js b/website/src/components/charts/bar/Bar.js index 42d6e5776..a2d9db875 100644 --- a/website/src/components/charts/bar/Bar.js +++ b/website/src/components/charts/bar/Bar.js @@ -153,8 +153,8 @@ export default class Bar extends Component { itemDirection: 'left-to-right', itemOpacity: 0.85, symbolSize: 20, - onClick: d => { - alert(JSON.stringify(d, null, ' ')) + onClick: data => { + alert(JSON.stringify(data, null, ' ')) }, effects: [ { diff --git a/website/src/components/charts/bar/props.js b/website/src/components/charts/bar/props.js index bab9cf723..f402eeca1 100644 --- a/website/src/components/charts/bar/props.js +++ b/website/src/components/charts/bar/props.js @@ -475,10 +475,5 @@ export default [ }, }, }, - /*—————————————————————————————————————————————————————————————————————————— - - Motion - - ————————————————————————————————————————————————————————————————————————————*/ ...motionProperties(['Bar'], defaults), ] diff --git a/website/src/components/charts/waffle/Waffle.js b/website/src/components/charts/waffle/Waffle.js index 4de1cf901..b4cef99ac 100644 --- a/website/src/components/charts/waffle/Waffle.js +++ b/website/src/components/charts/waffle/Waffle.js @@ -58,11 +58,10 @@ export default class Waffle extends Component { top: 10, right: 10, bottom: 10, - left: 10, + left: 120, }, // styling - theme: nivoTheme, cellComponent: 'default', emptyColor: '#cccccc', emptyOpacity: 1, @@ -83,6 +82,37 @@ export default class Waffle extends Component { isInteractive: true, 'custom tooltip example': false, tooltip: null, + + legends: [ + { + anchor: 'top-left', + direction: 'column', + justify: false, + translateX: -100, + translateY: 0, + itemsSpacing: 4, + itemWidth: 100, + itemHeight: 20, + itemDirection: 'left-to-right', + itemOpacity: 1, + itemTextColor: '#777', + symbolSize: 20, + onClick: data => { + alert(JSON.stringify(data, null, ' ')) + }, + effects: [ + { + on: 'hover', + style: { + itemTextColor: '#000', + itemBackground: '#f7fafb', + }, + }, + ], + }, + ], + + theme: nivoTheme, }, } diff --git a/website/src/components/charts/waffle/props.js b/website/src/components/charts/waffle/props.js index 3109f126f..c20606361 100644 --- a/website/src/components/charts/waffle/props.js +++ b/website/src/components/charts/waffle/props.js @@ -11,6 +11,7 @@ import { marginProperties, motionProperties, defsProperties, + getLegendsProps, } from '../../../lib/componentProperties' import { WaffleDefaultProps } from '@nivo/waffle' import dedent from 'dedent-js' @@ -143,11 +144,6 @@ export default [ max: 2, }, }, - /*—————————————————————————————————————————————————————————————————————————— - - Styling - - ————————————————————————————————————————————————————————————————————————————*/ { key: 'cellComponent', scopes: ['Waffle', 'WaffleHtml'], @@ -310,5 +306,44 @@ export default [ ), }, + { + key: 'legends', + scopes: ['Waffle'], + type: '{Array}', + description: `Optional chart's legends.`, + controlGroup: 'Legends', + controlType: 'array', + controlOptions: { + props: getLegendsProps(), + shouldCreate: true, + addLabel: 'add legend', + shouldRemove: true, + defaults: { + anchor: 'left', + direction: 'column', + justify: false, + translateX: -100, + translateY: 0, + itemWidth: 100, + itemHeight: 20, + itemsSpacing: 4, + symbolSize: 20, + itemDirection: 'left-to-right', + itemTextColor: '#777', + onClick: data => { + alert(JSON.stringify(data, null, ' ')) + }, + effects: [ + { + on: 'hover', + style: { + itemTextColor: '#000', + itemBackground: '#f7fafb', + }, + }, + ], + }, + }, + }, ...motionProperties(['Waffle', 'WaffleHtml'], defaults), ]