Skip to content

Commit

Permalink
feat(choropleth): add support for defs and fill to the choropleth (pl…
Browse files Browse the repository at this point in the history
…ouc#2072)

* enable defs and fill in choropleth map

* update website example

* add gradient example

* lint and format
  • Loading branch information
francescocretti authored Sep 8, 2022
1 parent 9bc03db commit e18733e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/geo/src/Choropleth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/
import { memo, Fragment, useCallback } from 'react'
import { SvgWrapper, withContainer, useDimensions, useTheme } from '@nivo/core'
import { SvgWrapper, withContainer, useDimensions, useTheme, bindDefs } from '@nivo/core'
import { BoxLegendSvg } from '@nivo/legends'
import { useTooltip } from '@nivo/tooltip'
import { ChoroplethPropTypes, ChoroplethDefaultProps } from './props'
Expand Down Expand Up @@ -44,6 +44,8 @@ const Choropleth = memo(
onClick,
tooltip: Tooltip,
role,
defs = ChoroplethDefaultProps.defs,
fill = ChoroplethDefaultProps.fill,
}) => {
const { margin, outerWidth, outerHeight } = useDimensions(width, height, partialMargin)
const { graticule, path, getBorderWidth, getBorderColor } = useGeoMap({
Expand Down Expand Up @@ -71,6 +73,11 @@ const Choropleth = memo(

const theme = useTheme()

const boundDefs = bindDefs(defs, boundFeatures, fill, {
dataKey: 'data',
targetKey: 'fill',
})

const { showTooltipFromEvent, hideTooltip } = useTooltip()
const handleClick = useCallback(
(feature, event) => isInteractive && onClick && onClick(feature, event),
Expand Down Expand Up @@ -101,6 +108,7 @@ const Choropleth = memo(
height={outerHeight}
margin={margin}
theme={theme}
defs={boundDefs}
role={role}
>
{layers.map((layer, i) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/geo/src/GeoMapFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const GeoMapFeature = memo(
return (
<path
key={feature.id}
fill={fillColor}
fill={feature?.fill ?? fillColor}
strokeWidth={borderWidth}
stroke={borderColor}
strokeLinejoin="bevel"
Expand Down
3 changes: 3 additions & 0 deletions packages/geo/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const commonDefaultProps = {

layers: ['graticule', 'features'],
legends: [],

fill: [],
defs: [],
}

export const GeoMapDefaultProps = {
Expand Down
28 changes: 28 additions & 0 deletions website/src/pages/choropleth/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import omit from 'lodash/omit'
import { patternDotsDef, patternLinesDef, linearGradientDef } from '@nivo/core'
import { ResponsiveChoropleth, ChoroplethDefaultProps } from '@nivo/geo'
import { ComponentTemplate } from '../../components/components/ComponentTemplate'
import meta from '../../data/components/choropleth/meta.yml'
Expand Down Expand Up @@ -45,6 +46,33 @@ const initialProperties = {
'custom tooltip example': false,
tooltip: null,

defs: [
patternDotsDef('dots', {
background: 'inherit',
color: '#38bcb2',
size: 4,
padding: 1,
stagger: true,
}),
patternLinesDef('lines', {
background: 'inherit',
color: '#eed312',
rotation: -45,
lineWidth: 6,
spacing: 10,
}),
linearGradientDef('gradient', [
{ offset: 0, color: '#000' },
{ offset: 100, color: 'inherit' },
]),
],

fill: [
{ match: { id: 'CAN' }, id: 'dots' },
{ match: { id: 'CHN' }, id: 'lines' },
{ match: { id: 'ATA' }, id: 'gradient' },
],

legends: [
{
anchor: 'bottom-left',
Expand Down

0 comments on commit e18733e

Please sign in to comment.