Skip to content

Commit af22689

Browse files
authored
feat(marimekko): computed dimension now contains the original input dimension (#2681)
1 parent cdf60f7 commit af22689

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

packages/marimekko/src/hooks.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const useThicknessScale = <RawDatum>({
146146
export const useComputedData = <RawDatum>({
147147
data,
148148
stacked,
149-
dimensionIds,
149+
rawDimensions,
150150
valueFormat,
151151
thicknessScale,
152152
dimensionsScale,
@@ -157,7 +157,7 @@ export const useComputedData = <RawDatum>({
157157
}: {
158158
data: NormalizedDatum<RawDatum>[]
159159
stacked: Series<RawDatum, string>[]
160-
dimensionIds: string[]
160+
rawDimensions: DataProps<RawDatum>['dimensions']
161161
valueFormat: DataProps<RawDatum>['valueFormat']
162162
thicknessScale: ScaleLinear<number>
163163
dimensionsScale: ScaleLinear<number>
@@ -193,12 +193,13 @@ export const useComputedData = <RawDatum>({
193193

194194
position += thickness + innerPadding
195195

196-
dimensionIds.forEach(dimensionId => {
197-
const dimension = stacked.find(stack => stack.key === dimensionId)
196+
rawDimensions.forEach(rawDimension => {
197+
const dimension = stacked.find(stack => stack.key === rawDimension.id)
198198
if (dimension) {
199199
const dimensionPoint = dimension[datum.index]
200200
const dimensionDatum: DimensionDatum<RawDatum> = {
201-
id: dimensionId,
201+
id: rawDimension.id,
202+
dimension: rawDimension,
202203
datum: computedDatum,
203204
value: dimensionPoint[1] - dimensionPoint[0],
204205
formattedValue: formatValue(dimensionPoint[1] - dimensionPoint[0]),
@@ -252,7 +253,7 @@ export const useComputedData = <RawDatum>({
252253
}, [
253254
data,
254255
stacked,
255-
dimensionIds,
256+
rawDimensions,
256257
thicknessScale,
257258
dimensionsScale,
258259
layout,
@@ -335,7 +336,7 @@ export const useMarimekko = <RawDatum>({
335336
const computedData = useComputedData<RawDatum>({
336337
data: normalizedData,
337338
stacked,
338-
dimensionIds,
339+
rawDimensions,
339340
valueFormat,
340341
thicknessScale,
341342
dimensionsScale,

packages/marimekko/src/types.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ export type DatumFormattedValue = string | number
1818

1919
export type DatumPropertyAccessor<RawDatum, T> = (datum: RawDatum) => T
2020

21+
export type RawDimensionDatum<RawDatum> = {
22+
id: string
23+
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
24+
}
25+
2126
export interface DataProps<RawDatum> {
2227
data: readonly RawDatum[]
2328
id: string | number | DatumPropertyAccessor<RawDatum, DatumId>
2429
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
25-
dimensions: readonly {
26-
id: string
27-
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
28-
}[]
30+
dimensions: readonly RawDimensionDatum<RawDatum>[]
2931
valueFormat?: ValueFormat<number>
3032
}
3133

@@ -45,6 +47,7 @@ export interface DimensionDatum<RawDatum> {
4547
y: number
4648
width: number
4749
height: number
50+
dimension: RawDimensionDatum<RawDatum>
4851
datum: ComputedDatum<RawDatum>
4952
}
5053

storybook/stories/marimekko/Marimekko.stories.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,41 @@ export const Diverging: Story = {
118118
)
119119
},
120120
}
121+
export const CustomColors: Story = {
122+
render: () => {
123+
const data = generateData()
124+
const dimensions = [
125+
{
126+
id: 'disagree strongly',
127+
value: 'stronglyDisagree',
128+
color: '#d63a3a',
129+
},
130+
{
131+
id: 'disagree',
132+
value: 'disagree',
133+
color: '#d6883a',
134+
},
135+
{
136+
id: 'agree',
137+
value: 'agree',
138+
color: '#ecce00',
139+
},
140+
{
141+
id: 'agree strongly',
142+
value: 'stronglyAgree',
143+
color: '#007c3e',
144+
},
145+
]
146+
return (
147+
<Marimekko
148+
{...commonProps}
149+
data={data}
150+
dimensions={dimensions}
151+
colors={{ datum: 'dimension.color' }}
152+
/>
153+
)
154+
},
155+
}
121156

122157
const ShadowsLayer = ({ data }) => (
123158
<>

0 commit comments

Comments
 (0)