Skip to content

Commit

Permalink
feat(marimekko): add support for value formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Nov 16, 2020
1 parent 87e5edb commit c5429db
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/marimekko/src/BarTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BarDatum } from './types'
export const BarTooltip = <RawDatum,>({ bar }: { bar: BarDatum<RawDatum> }) => (
<BasicTooltip
id={`${bar.datum.id} - ${bar.id}`}
value={bar.value}
value={bar.formattedValue}
enableChip={true}
color={bar.color}
/>
Expand Down
2 changes: 2 additions & 0 deletions packages/marimekko/src/Marimekko.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const InnerMarimekko = <RawDatum,>({
data,
id,
value,
valueFormat,
dimensions,
width,
height,
Expand Down Expand Up @@ -61,6 +62,7 @@ const InnerMarimekko = <RawDatum,>({
id,
value,
dimensions,
valueFormat,
layout,
offset,
outerPadding,
Expand Down
15 changes: 14 additions & 1 deletion packages/marimekko/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { useMemo } from 'react'
import { get } from 'lodash'
import { stack as d3Stack, Stack, Series } from 'd3-shape'
import { ScaleLinear, scaleLinear } from 'd3-scale'
import { useTheme } from '@nivo/core'
import {
// @ts-ignore
useValueFormatter,
useTheme,
} from '@nivo/core'
import { InheritedColorConfig, useInheritedColor, useOrdinalColorScale } from '@nivo/colors'
import {
NormalizedDatum,
Expand Down Expand Up @@ -154,6 +158,7 @@ export const useComputedData = <RawDatum>({
data,
stacked,
dimensionIds,
valueFormat,
thicknessScale,
dimensionsScale,
colors,
Expand All @@ -164,6 +169,7 @@ export const useComputedData = <RawDatum>({
data: NormalizedDatum<RawDatum>[]
stacked: Series<RawDatum, string>[]
dimensionIds: string[]
valueFormat: DataProps<RawDatum>['valueFormat']
thicknessScale: ScaleLinear<number, number>
dimensionsScale: ScaleLinear<number, number>
colors: CommonProps<RawDatum>['colors']
Expand All @@ -173,6 +179,8 @@ export const useComputedData = <RawDatum>({
}) => {
const getColor = useOrdinalColorScale<Omit<DimensionDatum<RawDatum>, 'color'>>(colors, 'id')

const formatValue = useValueFormatter(valueFormat)

return useMemo(() => {
const computedData: ComputedDatum<RawDatum>[] = []

Expand Down Expand Up @@ -203,6 +211,7 @@ export const useComputedData = <RawDatum>({
id: dimensionId,
datum: computedDatum,
value: dimensionPoint[1] - dimensionPoint[0],
formattedValue: formatValue(dimensionPoint[1] - dimensionPoint[0]),
color: 'rgba(0, 0, 0, 0)',
x: 0,
y: 0,
Expand Down Expand Up @@ -258,6 +267,7 @@ export const useComputedData = <RawDatum>({
outerPadding,
innerPadding,
getColor,
formatValue,
])
}

Expand Down Expand Up @@ -290,6 +300,7 @@ export const useMarimekko = <RawDatum>({
data,
id,
value,
valueFormat,
dimensions: rawDimensions,
layout,
offset,
Expand All @@ -304,6 +315,7 @@ export const useMarimekko = <RawDatum>({
data: DataProps<RawDatum>['data']
id: DataProps<RawDatum>['id']
value: DataProps<RawDatum>['value']
valueFormat: DataProps<RawDatum>['valueFormat']
dimensions: DataProps<RawDatum>['dimensions']
layout: Layout
offset: OffsetId
Expand Down Expand Up @@ -332,6 +344,7 @@ export const useMarimekko = <RawDatum>({
data: normalizedData,
stacked,
dimensionIds,
valueFormat,
thicknessScale,
dimensionsScale,
colors,
Expand Down
14 changes: 10 additions & 4 deletions packages/marimekko/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ import {
stackOffsetSilhouette,
stackOffsetWiggle,
} from 'd3-shape'
import { Box, Dimensions, Theme, SvgDefsAndFill, ModernMotionProps } from '@nivo/core'
import {
Box,
Dimensions,
Theme,
SvgDefsAndFill,
ModernMotionProps,
DataFormatter,
} from '@nivo/core'
import { AxisProps } from '@nivo/axes'
import { OrdinalColorScaleConfig, InheritedColorConfig } from '@nivo/colors'
import { LegendProps } from '@nivo/legends'

export type DatumId = string | number
export type DatumValue = number
export type DatumFormattedValue = string | number
export type ValueFormatter = (value: number) => DatumFormattedValue

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

Expand All @@ -26,6 +32,7 @@ export interface DataProps<RawDatum> {
id: string
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
}[]
valueFormat?: string | DataFormatter
}

export interface NormalizedDatum<RawDatum> {
Expand All @@ -38,6 +45,7 @@ export interface NormalizedDatum<RawDatum> {
export interface DimensionDatum<RawDatum> {
id: string
value: number
formattedValue: string | number
color: string
x: number
y: number
Expand Down Expand Up @@ -103,8 +111,6 @@ export const offsetById = {
export type OffsetId = keyof typeof offsetById

export type CommonProps<RawDatum> = {
valueFormat?: string | ValueFormatter

margin: Box
layout: Layout
offset: OffsetId
Expand Down
2 changes: 1 addition & 1 deletion packages/marimekko/stories/marimekko.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ stories.add('diverging', () => {
data={data}
layout="horizontal"
offset="diverging"
axisLeft={{
axisBottom={{
format: (v: number) => Math.abs(v),
}}
/>
Expand Down

0 comments on commit c5429db

Please sign in to comment.