Skip to content

Commit 5c541bb

Browse files
committed
Honor label change
1 parent 6384080 commit 5c541bb

File tree

6 files changed

+10064
-15331
lines changed

6 files changed

+10064
-15331
lines changed

docs/SimpleDorling.js

Lines changed: 83 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react"
1+
import React, { useMemo, useState } from "react"
22
import DorlingCartogram from "../src/DorlingCartogram"
33
import ResponsiveDorlingCartogram from "../src/ResponsiveDorlingCartogram"
44
import geoNaturalEarth1 from "d3-geo/src/projection/naturalEarth1"
@@ -97,7 +97,26 @@ const countryData = [
9797
{ id: "US", bbhouseholds: 500, watchers: 500 }
9898
]
9999

100+
const defaultLabelFn = d =>
101+
d.id ? (
102+
<text
103+
fill="white"
104+
textAnchor="middle"
105+
fontWeight={900}
106+
fontSize={`${d.r / 2}px`}
107+
y={d.r / 3.5}
108+
>
109+
{d.id}
110+
</text>
111+
) : (
112+
""
113+
)
114+
115+
const differentLabelFn = d =>
116+
"!!"
117+
100118
const baseGeoStyle = d => {
119+
if (!d) { return {} }
101120
return d.bbhouseholds
102121
? { fill: "purple", stroke: "none" }
103122
: { fill: "orange", stroke: "none" }
@@ -107,89 +126,74 @@ const secondaryGeoStyle = d => {
107126
return !d.bbhouseholds
108127
? { fill: "darkred", stroke: "none" }
109128
: d.bbhouseholds > 100
110-
? { fill: "steelblue", stroke: "purple" }
111-
: { fill: "lightgreen", stroke: "none" }
129+
? { fill: "steelblue", stroke: "purple" }
130+
: { fill: "lightgreen", stroke: "none" }
112131
}
113132

114-
class SimpleDorling extends React.Component {
115-
constructor(props) {
116-
super(props)
117-
this.state = {
118-
carto: false,
119-
sizeByBasic: false,
120-
size: [500, 500],
121-
geoStyle: baseGeoStyle
122-
}
123-
}
133+
export default props => {
124134

125-
render() {
126-
return (
127-
<div>
128-
<button onClick={() => this.setState({ carto: !this.state.carto })}>
129-
Change
135+
const [carto, changeCarto] = useState(false)
136+
const [sizeByBasic, changeSizeBy] = useState(false)
137+
const [size, changeSize] = useState([500, 500])
138+
const [geoStyle, changeStyle] = useState(() => baseGeoStyle)
139+
const [filteredGeodata, changeGeodata] = useState(false)
140+
const [labelFn, changeLabelFn] = useState(() => defaultLabelFn)
141+
142+
return (
143+
<div>
144+
<button onClick={() => changeCarto(!carto)}>
145+
Change Cartogram Mode
130146
</button>
131-
<button
132-
onClick={() =>
133-
this.setState({ sizeByBasic: !this.state.sizeByBasic })
134-
}
135-
>
136-
Change SizeBy
147+
<button
148+
onClick={() =>
149+
changeSizeBy(!sizeByBasic)
150+
}
151+
>
152+
Change SizeBy
137153
</button>
138-
<button
139-
onClick={() =>
140-
this.setState({ filteredGeodata: !this.state.filteredGeodata })
141-
}
142-
>
143-
Change Geodata
154+
<button
155+
onClick={() =>
156+
changeGeodata(!filteredGeodata)
157+
}
158+
>
159+
Change Geodata
144160
</button>
145-
<button
146-
onClick={() =>
147-
this.setState({
148-
size: this.state.size[0] === 500 ? [800, 600] : [500, 500]
149-
})
150-
}
151-
>
152-
Change Size
161+
<button
162+
onClick={() =>
163+
changeSize(size[0] === 500 ? [800, 600] : [500, 500])
164+
}
165+
>
166+
Change Size
153167
</button>
154-
<button onClick={() => this.setState({ geoStyle: secondaryGeoStyle })}>
155-
Change Style Fn
168+
<button onClick={() => changeStyle(() => secondaryGeoStyle)}>
169+
Change Style Fn
156170
</button>
157171

158-
<ResponsiveDorlingCartogram
159-
responsiveWidth
160-
showBorders
161-
cartogram={this.state.carto}
162-
// circleStyle={{ fill: 'red' }}
163-
// customMark={customMark}
164-
geoStyle={this.state.geoStyle}
165-
transitionSeconds={2}
166-
size={this.state.size}
167-
sizeBy={this.state.sizeByBasic ? dynamicSize : staticSize}
168-
projectionType={geoNaturalEarth1}
169-
data={countryData}
170-
mapData={this.state.filteredGeodata ? secondGeoData : geodata}
171-
onHover={d => {
172-
console.info("hover d", d)
173-
}}
174-
circlePadding={5}
175-
label={d =>
176-
d.id ? (
177-
<text
178-
fill="white"
179-
textAnchor="middle"
180-
fontWeight={900}
181-
fontSize={`${d.r / 2}px`}
182-
y={d.r / 3.5}
183-
>
184-
{d.id}
185-
</text>
186-
) : (
187-
""
188-
)
189-
}
190-
/>
191-
<pre>
192-
{`import React from 'react';
172+
<button onClick={() => changeLabelFn(() => differentLabelFn)}>
173+
Change Label Fn
174+
</button>
175+
176+
<ResponsiveDorlingCartogram
177+
responsiveWidth
178+
showBorders
179+
cartogram={carto}
180+
// circleStyle={{ fill: 'red' }}
181+
// customMark={customMark}
182+
geoStyle={geoStyle}
183+
transitionSeconds={2}
184+
size={size}
185+
sizeBy={sizeByBasic ? dynamicSize : staticSize}
186+
projectionType={geoNaturalEarth1}
187+
data={countryData}
188+
mapData={filteredGeodata ? secondGeoData : geodata}
189+
onHover={d => {
190+
console.info("hover d", d)
191+
}}
192+
circlePadding={5}
193+
label={labelFn}
194+
/>
195+
<pre>
196+
{`import React from 'react';
193197
import DorlingCartogram from '../src/DorlingCartogram';
194198
// import { geoAzimuthalEquidistant } from 'd3-geo';
195199
@@ -302,10 +306,8 @@ class SimpleDorling extends React.Component {
302306
}
303307
304308
export default SimpleDorling;`}
305-
</pre>
306-
</div>
307-
)
308-
}
309-
}
309+
</pre>
310+
</div>
311+
)
310312

311-
export default SimpleDorling
313+
}

0 commit comments

Comments
 (0)