diff --git a/packages/marimekko/src/hooks.ts b/packages/marimekko/src/hooks.ts index 94fc8af0f..245acbed5 100644 --- a/packages/marimekko/src/hooks.ts +++ b/packages/marimekko/src/hooks.ts @@ -146,7 +146,7 @@ export const useThicknessScale = ({ export const useComputedData = ({ data, stacked, - dimensionIds, + rawDimensions, valueFormat, thicknessScale, dimensionsScale, @@ -157,7 +157,7 @@ export const useComputedData = ({ }: { data: NormalizedDatum[] stacked: Series[] - dimensionIds: string[] + rawDimensions: DataProps['dimensions'] valueFormat: DataProps['valueFormat'] thicknessScale: ScaleLinear dimensionsScale: ScaleLinear @@ -193,12 +193,13 @@ export const useComputedData = ({ position += thickness + innerPadding - dimensionIds.forEach(dimensionId => { - const dimension = stacked.find(stack => stack.key === dimensionId) + rawDimensions.forEach(rawDimension => { + const dimension = stacked.find(stack => stack.key === rawDimension.id) if (dimension) { const dimensionPoint = dimension[datum.index] const dimensionDatum: DimensionDatum = { - id: dimensionId, + id: rawDimension.id, + dimension: rawDimension, datum: computedDatum, value: dimensionPoint[1] - dimensionPoint[0], formattedValue: formatValue(dimensionPoint[1] - dimensionPoint[0]), @@ -252,7 +253,7 @@ export const useComputedData = ({ }, [ data, stacked, - dimensionIds, + rawDimensions, thicknessScale, dimensionsScale, layout, @@ -335,7 +336,7 @@ export const useMarimekko = ({ const computedData = useComputedData({ data: normalizedData, stacked, - dimensionIds, + rawDimensions, valueFormat, thicknessScale, dimensionsScale, diff --git a/packages/marimekko/src/types.ts b/packages/marimekko/src/types.ts index 57a4b93f1..e3d02c918 100644 --- a/packages/marimekko/src/types.ts +++ b/packages/marimekko/src/types.ts @@ -18,14 +18,16 @@ export type DatumFormattedValue = string | number export type DatumPropertyAccessor = (datum: RawDatum) => T +export type RawDimensionDatum = { + id: string + value: string | number | DatumPropertyAccessor +} + export interface DataProps { data: readonly RawDatum[] id: string | number | DatumPropertyAccessor value: string | number | DatumPropertyAccessor - dimensions: readonly { - id: string - value: string | number | DatumPropertyAccessor - }[] + dimensions: readonly RawDimensionDatum[] valueFormat?: ValueFormat } @@ -45,6 +47,7 @@ export interface DimensionDatum { y: number width: number height: number + dimension: RawDimensionDatum datum: ComputedDatum } diff --git a/storybook/stories/marimekko/Marimekko.stories.tsx b/storybook/stories/marimekko/Marimekko.stories.tsx index d83f99558..8bef81e11 100644 --- a/storybook/stories/marimekko/Marimekko.stories.tsx +++ b/storybook/stories/marimekko/Marimekko.stories.tsx @@ -118,6 +118,41 @@ export const Diverging: Story = { ) }, } +export const CustomColors: Story = { + render: () => { + const data = generateData() + const dimensions = [ + { + id: 'disagree strongly', + value: 'stronglyDisagree', + color: '#d63a3a', + }, + { + id: 'disagree', + value: 'disagree', + color: '#d6883a', + }, + { + id: 'agree', + value: 'agree', + color: '#ecce00', + }, + { + id: 'agree strongly', + value: 'stronglyAgree', + color: '#007c3e', + }, + ] + return ( + + ) + }, +} const ShadowsLayer = ({ data }) => ( <>