Skip to content

Commit

Permalink
feat(marimekko): add support for legends
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Nov 16, 2020
1 parent 8560b6a commit f39f12a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/marimekko/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@nivo/axes": "0.64.0",
"@nivo/colors": "0.64.0",
"@nivo/legends": "0.64.0",
"d3-scale": "^3.0.0",
"d3-shape": "^1.3.5",
"lodash": "^4.17.11",
Expand Down
26 changes: 24 additions & 2 deletions packages/marimekko/src/Marimekko.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
} from '@nivo/core'
import { Grid, Axes } from '@nivo/axes'
import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors'
import { BoxLegendSvg } from '@nivo/legends'
import { SvgProps, LayerId, DimensionDatum } from './types'
import { defaultProps } from './props'
import { useMarimekko, useLayerContext } from './hooks'
import { useMarimekko, useLayerContext, useLegendData } from './hooks'
import { Bars } from './Bars'
import { BarTooltip } from './BarTooltip'

Expand Down Expand Up @@ -49,6 +50,7 @@ const InnerMarimekko = <RawDatum,>({
onMouseEnter,
onMouseMove,
onMouseLeave,
legends = [],
role,
}: SvgProps<RawDatum>) => {
const { outerWidth, outerHeight, margin, innerWidth, innerHeight } = useDimensions(
Expand All @@ -57,7 +59,9 @@ const InnerMarimekko = <RawDatum,>({
partialMargin
)

const { computedData, bars, thicknessScale, dimensionsScale } = useMarimekko<RawDatum>({
const { computedData, bars, thicknessScale, dimensionsScale, dimensionIds } = useMarimekko<
RawDatum
>({
data,
id,
value,
Expand Down Expand Up @@ -131,6 +135,24 @@ const InnerMarimekko = <RawDatum,>({
)
}

const legendData = useLegendData<RawDatum>(dimensionIds, bars)

if (layers.includes('legends')) {
layerById.legends = (
<g key="legends">
{legends.map((legend, i) => (
<BoxLegendSvg
key={i}
{...legend}
containerWidth={innerWidth}
containerHeight={innerHeight}
data={legendData}
/>
))}
</g>
)
}

const layerContext = useLayerContext<RawDatum>({
data: computedData,
bars,
Expand Down
24 changes: 24 additions & 0 deletions packages/marimekko/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export const useMarimekko = <RawDatum>({
bars,
thicknessScale,
dimensionsScale,
dimensionIds,
}
}

Expand All @@ -383,3 +384,26 @@ export const useLayerContext = <RawDatum>({
}),
[data, bars, thicknessScale, dimensionsScale]
)

export const useLegendData = <RawDatum>(dimensionIds: string[], bars: BarDatum<RawDatum>[]) => {
const legendData: {
id: string
label: string
color: string
fill?: string
}[] = []

dimensionIds.forEach(dimensionId => {
const bar = bars.find(bar => bar.id === dimensionId)
if (bar) {
legendData.push({
id: dimensionId,
label: dimensionId,
color: bar.color,
fill: bar.fill,
})
}
})

return legendData
}
10 changes: 5 additions & 5 deletions website/src/pages/marimekko/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const initialProperties = {
margin: {
top: 40,
right: 80,
bottom: 80,
bottom: 100,
left: 80,
},

Expand Down Expand Up @@ -119,15 +119,15 @@ const initialProperties = {
direction: 'row',
justify: false,
translateX: 0,
translateY: 56,
translateY: 80,
itemsSpacing: 0,
itemWidth: 100,
itemWidth: 140,
itemHeight: 18,
itemTextColor: '#999',
itemDirection: 'left-to-right',
itemDirection: 'right-to-left',
itemOpacity: 1,
symbolSize: 18,
symbolShape: 'circle',
symbolShape: 'square',
effects: [
{
on: 'hover',
Expand Down

0 comments on commit f39f12a

Please sign in to comment.