Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

marimekko: all dimension input data is propagated #2681

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/marimekko/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const useThicknessScale = <RawDatum>({
export const useComputedData = <RawDatum>({
data,
stacked,
dimensionIds,
rawDimensions,
valueFormat,
thicknessScale,
dimensionsScale,
Expand All @@ -157,7 +157,7 @@ export const useComputedData = <RawDatum>({
}: {
data: NormalizedDatum<RawDatum>[]
stacked: Series<RawDatum, string>[]
dimensionIds: string[]
rawDimensions: DataProps<RawDatum>['dimensions']
valueFormat: DataProps<RawDatum>['valueFormat']
thicknessScale: ScaleLinear<number>
dimensionsScale: ScaleLinear<number>
Expand Down Expand Up @@ -193,12 +193,13 @@ export const useComputedData = <RawDatum>({

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<RawDatum> = {
id: dimensionId,
id: rawDimension.id,
dimension: rawDimension,
datum: computedDatum,
value: dimensionPoint[1] - dimensionPoint[0],
formattedValue: formatValue(dimensionPoint[1] - dimensionPoint[0]),
Expand Down Expand Up @@ -252,7 +253,7 @@ export const useComputedData = <RawDatum>({
}, [
data,
stacked,
dimensionIds,
rawDimensions,
thicknessScale,
dimensionsScale,
layout,
Expand Down Expand Up @@ -335,7 +336,7 @@ export const useMarimekko = <RawDatum>({
const computedData = useComputedData<RawDatum>({
data: normalizedData,
stacked,
dimensionIds,
rawDimensions,
valueFormat,
thicknessScale,
dimensionsScale,
Expand Down
11 changes: 7 additions & 4 deletions packages/marimekko/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ export type DatumFormattedValue = string | number

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

export type RawDimensionDatum<RawDatum> = {
id: string
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
}

export interface DataProps<RawDatum> {
data: readonly RawDatum[]
id: string | number | DatumPropertyAccessor<RawDatum, DatumId>
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
dimensions: readonly {
id: string
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
}[]
dimensions: readonly RawDimensionDatum<RawDatum>[]
valueFormat?: ValueFormat<number>
}

Expand All @@ -45,6 +47,7 @@ export interface DimensionDatum<RawDatum> {
y: number
width: number
height: number
dimension: RawDimensionDatum<RawDatum>
datum: ComputedDatum<RawDatum>
}

Expand Down
35 changes: 35 additions & 0 deletions storybook/stories/marimekko/Marimekko.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Marimekko
{...commonProps}
data={data}
dimensions={dimensions}
colors={{ datum: 'dimension.color' }}
/>
)
},
}

const ShadowsLayer = ({ data }) => (
<>
Expand Down
Loading