Skip to content

Commit bdb231c

Browse files
committedSep 7, 2021
feat(core): add typings for d3 curve factories
1 parent 062ab4e commit bdb231c

File tree

12 files changed

+80
-27
lines changed

12 files changed

+80
-27
lines changed
 

‎packages/core/index.d.ts

+58
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react'
22
import { Interpolation, SpringConfig } from '@react-spring/web'
3+
import { CurveFactory } from 'd3-shape'
34

45
declare module '@nivo/core' {
56
export type DatumValue = string | number | Date
@@ -437,4 +438,61 @@ declare module '@nivo/core' {
437438
props: CartesianMarkersProps<X, Y>
438439
) => JSX.Element
439440
export const CartesianMarkers: CartesianMarkersType
441+
442+
export type CurveFactoryId =
443+
| 'basis'
444+
| 'basisClosed'
445+
| 'basisOpen'
446+
| 'bundle'
447+
| 'cardinal'
448+
| 'cardinalClosed'
449+
| 'cardinalOpen'
450+
| 'catmullRom'
451+
| 'catmullRomClosed'
452+
| 'catmullRomOpen'
453+
| 'linear'
454+
| 'linearClosed'
455+
| 'monotoneX'
456+
| 'monotoneY'
457+
| 'natural'
458+
| 'step'
459+
| 'stepAfter'
460+
| 'stepBefore'
461+
462+
// Curve factories compatible d3 line shape generator
463+
export type LineCurveFactoryId =
464+
| 'basis'
465+
| 'cardinal'
466+
| 'catmullRom'
467+
| 'linear'
468+
| 'monotoneX'
469+
| 'monotoneY'
470+
| 'natural'
471+
| 'step'
472+
| 'stepAfter'
473+
| 'stepBefore'
474+
475+
// Curve factories compatible d3 area shape generator
476+
export type AreaCurveFactoryId =
477+
| 'basis'
478+
| 'cardinal'
479+
| 'catmullRom'
480+
| 'linear'
481+
| 'monotoneX'
482+
| 'monotoneY'
483+
| 'natural'
484+
| 'step'
485+
| 'stepAfter'
486+
| 'stepBefore'
487+
488+
export type ClosedCurveFactoryId =
489+
| 'basisClosed'
490+
| 'cardinalClosed'
491+
| 'catmullRomClosed'
492+
| 'linearClosed'
493+
export const closedCurvePropKeys: ClosedCurveFactoryId[]
494+
495+
export const curveFromProp: (interpolation: CurveFactoryId) => CurveFactory
496+
497+
export const useCurveInterpolation: (interpolation: CurveFactoryId) => CurveFactory
440498
}

‎packages/core/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"resize-observer-polyfill": "^1.5.1"
3535
},
3636
"devDependencies": {
37-
"@nivo/tooltip": "0.73.0"
37+
"@nivo/tooltip": "0.73.0",
38+
"@types/d3-shape": "^2.0.0"
3839
},
3940
"peerDependencies": {
4041
"@nivo/tooltip": "0.73.0",

‎packages/core/src/props/curve.js

-4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export const curvePropType = PropTypes.oneOf(curvePropKeys)
4848

4949
export const closedCurvePropKeys = curvePropKeys.filter(c => c.endsWith('Closed'))
5050

51-
export const closedCurvePropType = PropTypes.oneOf(closedCurvePropKeys)
52-
5351
// Safe curves to be used with d3 area shape generator
5452
export const areaCurvePropKeys = without(
5553
curvePropKeys,
@@ -63,8 +61,6 @@ export const areaCurvePropKeys = without(
6361
'linearClosed'
6462
)
6563

66-
export const areaCurvePropType = PropTypes.oneOf(areaCurvePropKeys)
67-
6864
// Safe curves to be used with d3 line shape generator
6965
export const lineCurvePropKeys = without(
7066
curvePropKeys,

‎packages/radar/src/Radar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const InnerRadar = <D extends Record<string, unknown>>({
6666
centerX,
6767
centerY,
6868
angleStep,
69-
curveInterpolator,
69+
curveFactory,
7070
legendData,
7171
} = useRadar<D>({
7272
data,
@@ -113,7 +113,7 @@ const InnerRadar = <D extends Record<string, unknown>>({
113113
colorByKey={colorByKey}
114114
radiusScale={radiusScale}
115115
angleStep={angleStep}
116-
curveInterpolator={curveInterpolator}
116+
curveFactory={curveFactory}
117117
borderWidth={borderWidth}
118118
borderColor={borderColor}
119119
fillOpacity={fillOpacity}

‎packages/radar/src/RadarShapes.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface RadarShapesProps<D extends Record<string, unknown>> {
1212
colorByKey: Record<string | number, string>
1313
radiusScale: ScaleLinear<number, number>
1414
angleStep: number
15-
curveInterpolator: CurveFactory
15+
curveFactory: CurveFactory
1616
borderWidth: RadarCommonProps<D>['borderWidth']
1717
borderColor: RadarCommonProps<D>['borderColor']
1818
fillOpacity: RadarCommonProps<D>['fillOpacity']
@@ -25,7 +25,7 @@ export const RadarShapes = <D extends Record<string, unknown>>({
2525
colorByKey,
2626
radiusScale,
2727
angleStep,
28-
curveInterpolator,
28+
curveFactory,
2929
borderWidth,
3030
borderColor,
3131
fillOpacity,
@@ -38,8 +38,8 @@ export const RadarShapes = <D extends Record<string, unknown>>({
3838
return lineRadial<number>()
3939
.radius(d => radiusScale(d))
4040
.angle((_, i) => i * angleStep)
41-
.curve(curveInterpolator)
42-
}, [radiusScale, angleStep, curveInterpolator])
41+
.curve(curveFactory)
42+
}, [radiusScale, angleStep, curveFactory])
4343

4444
const { animate, config: springConfig } = useMotionConfig()
4545
const animatedPath = useAnimatedPath(lineGenerator(data.map(d => d[key])))

‎packages/radar/src/RadarTooltipItem.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ export const RadarTooltipItem = <D extends Record<string, unknown>>({
3838
key,
3939
tooltipFormatter(datum[key], key),
4040
])
41+
// sorting won't work if values are formatted to string
4142
rows.sort((a, b) => a[2] - b[2])
4243
rows.reverse()
4344

44-
return <TableTooltip title={<strong>{index}</strong>} rows={rows} theme={theme} />
45+
return <TableTooltip title={<strong>{index}</strong>} rows={rows} />
4546
}, [datum, keys, index, colorByKey, theme, tooltipFormatter])
4647
const showItemTooltip = useCallback(
4748
event => {

‎packages/radar/src/hooks.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { useMemo } from 'react'
22
import { scaleLinear } from 'd3-scale'
3-
import {
4-
// @ts-ignore
5-
useCurveInterpolation,
6-
usePropertyAccessor,
7-
} from '@nivo/core'
3+
import { useCurveInterpolation, usePropertyAccessor } from '@nivo/core'
84
import { useOrdinalColorScale } from '@nivo/colors'
95
import { svgDefaultProps } from './props'
106
import { RadarCommonProps, RadarDataProps } from './types'
@@ -64,7 +60,7 @@ export const useRadar = <D extends Record<string, unknown>>({
6460
}
6561
}, [keys, indexBy, data, maxValue, width, height])
6662

67-
const curveInterpolator = useCurveInterpolation(curve)
63+
const curveFactory = useCurveInterpolation(curve)
6864

6965
const legendData = keys.map(key => ({
7066
id: key,
@@ -81,7 +77,7 @@ export const useRadar = <D extends Record<string, unknown>>({
8177
centerX,
8278
centerY,
8379
angleStep,
84-
curveInterpolator,
80+
curveFactory,
8581
legendData,
8682
}
8783
}

‎packages/radar/src/props.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const svgDefaultProps = {
66

77
maxValue: 'auto' as const,
88

9-
curve: 'linearClosed',
9+
curve: 'linearClosed' as const,
1010

1111
borderWidth: 2,
1212
borderColor: { from: 'color' },

‎packages/radar/src/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ModernMotionProps,
99
PropertyAccessor,
1010
ValueFormat,
11+
ClosedCurveFactoryId,
1112
} from '@nivo/core'
1213
import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors'
1314
import { LegendProps } from '@nivo/legends'
@@ -54,7 +55,7 @@ export interface RadarCommonProps<D extends Record<string, unknown>> {
5455

5556
margin: Box
5657

57-
curve: string // closedCurvePropType.isRequired,
58+
curve: ClosedCurveFactoryId
5859

5960
gridLevels: number
6061
gridShape: 'circular' | 'linear'

‎packages/stream/src/hooks.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
useTheme,
66
usePropertyAccessor,
77
useValueFormatter,
8-
// @ts-ignore
9-
curveFromProp,
8+
useCurveInterpolation,
109
// @ts-ignore
1110
stackOrderFromProp,
1211
// @ts-ignore
@@ -57,14 +56,15 @@ export const useStream = <RawDatum extends StreamDatum>({
5756
dotBorderColor?: StreamCommonProps<RawDatum>['dotBorderColor']
5857
borderColor?: StreamCommonProps<RawDatum>['borderColor']
5958
}) => {
59+
const areaCurveFactory = useCurveInterpolation(curve)
6060
const areaGenerator = useMemo(
6161
() =>
6262
area<StreamLayerDatum>()
6363
.x(({ x }) => x)
6464
.y0(({ y1 }) => y1)
6565
.y1(({ y2 }) => y2)
66-
.curve(curveFromProp(curve)),
67-
[curve]
66+
.curve(areaCurveFactory),
67+
[areaCurveFactory]
6868
)
6969

7070
const stack = useMemo(

‎website/src/data/components/radar/props.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-ignore
21
import { closedCurvePropKeys } from '@nivo/core'
32
import { svgDefaultProps as defaults, RadarDots } from '@nivo/radar'
43
import { themeProperty, motionProperties, groupProperties } from '../../../lib/componentProperties'

‎website/src/pages/radar/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import { generateWinesTastes } from '@nivo/generators'
3-
import { ResponsiveRadar } from '@nivo/radar'
3+
import { ResponsiveRadar, svgDefaultProps } from '@nivo/radar'
44
import { ComponentTemplate } from '../../components/components/ComponentTemplate'
55
import meta from '../../data/components/radar/meta.yml'
66
import mapper from '../../data/components/radar/mapper'
@@ -79,6 +79,7 @@ const Radar = () => (
7979
currentFlavor="svg"
8080
properties={groups}
8181
initialProperties={initialProperties}
82+
defaultProperties={svgDefaultProps}
8283
propertiesMapper={mapper}
8384
codePropertiesMapper={(properties, data) => ({
8485
keys: data.keys,

0 commit comments

Comments
 (0)
Please sign in to comment.