Skip to content

Commit e18733e

Browse files
authoredSep 8, 2022
feat(choropleth): add support for defs and fill to the choropleth (#2072)
* enable defs and fill in choropleth map * update website example * add gradient example * lint and format
1 parent 9bc03db commit e18733e

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed
 

‎packages/geo/src/Choropleth.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99
import { memo, Fragment, useCallback } from 'react'
10-
import { SvgWrapper, withContainer, useDimensions, useTheme } from '@nivo/core'
10+
import { SvgWrapper, withContainer, useDimensions, useTheme, bindDefs } from '@nivo/core'
1111
import { BoxLegendSvg } from '@nivo/legends'
1212
import { useTooltip } from '@nivo/tooltip'
1313
import { ChoroplethPropTypes, ChoroplethDefaultProps } from './props'
@@ -44,6 +44,8 @@ const Choropleth = memo(
4444
onClick,
4545
tooltip: Tooltip,
4646
role,
47+
defs = ChoroplethDefaultProps.defs,
48+
fill = ChoroplethDefaultProps.fill,
4749
}) => {
4850
const { margin, outerWidth, outerHeight } = useDimensions(width, height, partialMargin)
4951
const { graticule, path, getBorderWidth, getBorderColor } = useGeoMap({
@@ -71,6 +73,11 @@ const Choropleth = memo(
7173

7274
const theme = useTheme()
7375

76+
const boundDefs = bindDefs(defs, boundFeatures, fill, {
77+
dataKey: 'data',
78+
targetKey: 'fill',
79+
})
80+
7481
const { showTooltipFromEvent, hideTooltip } = useTooltip()
7582
const handleClick = useCallback(
7683
(feature, event) => isInteractive && onClick && onClick(feature, event),
@@ -101,6 +108,7 @@ const Choropleth = memo(
101108
height={outerHeight}
102109
margin={margin}
103110
theme={theme}
111+
defs={boundDefs}
104112
role={role}
105113
>
106114
{layers.map((layer, i) => {

‎packages/geo/src/GeoMapFeature.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const GeoMapFeature = memo(
2424
return (
2525
<path
2626
key={feature.id}
27-
fill={fillColor}
27+
fill={feature?.fill ?? fillColor}
2828
strokeWidth={borderWidth}
2929
stroke={borderColor}
3030
strokeLinejoin="bevel"

‎packages/geo/src/props.js

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ const commonDefaultProps = {
104104

105105
layers: ['graticule', 'features'],
106106
legends: [],
107+
108+
fill: [],
109+
defs: [],
107110
}
108111

109112
export const GeoMapDefaultProps = {

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

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import omit from 'lodash/omit'
3+
import { patternDotsDef, patternLinesDef, linearGradientDef } from '@nivo/core'
34
import { ResponsiveChoropleth, ChoroplethDefaultProps } from '@nivo/geo'
45
import { ComponentTemplate } from '../../components/components/ComponentTemplate'
56
import meta from '../../data/components/choropleth/meta.yml'
@@ -45,6 +46,33 @@ const initialProperties = {
4546
'custom tooltip example': false,
4647
tooltip: null,
4748

49+
defs: [
50+
patternDotsDef('dots', {
51+
background: 'inherit',
52+
color: '#38bcb2',
53+
size: 4,
54+
padding: 1,
55+
stagger: true,
56+
}),
57+
patternLinesDef('lines', {
58+
background: 'inherit',
59+
color: '#eed312',
60+
rotation: -45,
61+
lineWidth: 6,
62+
spacing: 10,
63+
}),
64+
linearGradientDef('gradient', [
65+
{ offset: 0, color: '#000' },
66+
{ offset: 100, color: 'inherit' },
67+
]),
68+
],
69+
70+
fill: [
71+
{ match: { id: 'CAN' }, id: 'dots' },
72+
{ match: { id: 'CHN' }, id: 'lines' },
73+
{ match: { id: 'ATA' }, id: 'gradient' },
74+
],
75+
4876
legends: [
4977
{
5078
anchor: 'bottom-left',

0 commit comments

Comments
 (0)
Please sign in to comment.