|
1 | | -import React, { useState } from "react"; |
| 1 | +import React, { Component } from "react"; |
2 | 2 | import { ComposableMap, Geographies, Geography } from "react-simple-maps"; |
3 | 3 | import { scaleQuantile } from "d3-scale"; |
4 | 4 | import ReactTooltip from "react-tooltip"; |
5 | | - |
6 | 5 | import LinearGradient from "./LinearGradient.js"; |
7 | | -// import "./App.css"; |
8 | 6 |
|
9 | 7 | /** |
10 | 8 | * Courtesy: https://rawgit.com/Anujarya300/bubble_maps/master/data/geography-data/india.topo.json |
@@ -51,166 +49,79 @@ const geographyStyle = { |
51 | 49 | }, |
52 | 50 | }; |
53 | 51 |
|
54 | | -// will generate random heatmap data on every call |
55 | | -const getHeatMapData = () => { |
56 | | - return [ |
57 | | - { id: "AP", state: "Andhra Pradesh", value: getRandomInt() }, |
58 | | - { id: "AR", state: "Arunachal Pradesh", value: getRandomInt() }, |
59 | | - { id: "AS", state: "Assam", value: getRandomInt() }, |
60 | | - { id: "BR", state: "Bihar", value: getRandomInt() }, |
61 | | - { id: "CT", state: "Chhattisgarh", value: getRandomInt() }, |
62 | | - { id: "GA", state: "Goa", value: 21 }, |
63 | | - { id: "GJ", state: "Gujarat", value: 22 }, |
64 | | - { id: "HR", state: "Haryana", value: getRandomInt() }, |
65 | | - { id: "JH", state: "Jharkhand", value: 26 }, |
66 | | - { id: "KA", state: "Karnataka", value: 27 }, |
67 | | - { id: "KL", state: "Kerala", value: getRandomInt() }, |
68 | | - { id: "MP", state: "Madhya Pradesh", value: getRandomInt() }, |
69 | | - { id: "MH", state: "Maharashtra", value: getRandomInt() }, |
70 | | - { id: "MN", state: "Manipur", value: getRandomInt() }, |
71 | | - { id: "ML", state: "Meghalaya", value: 59 }, |
72 | | - { id: "MZ", state: "Mizoram", value: getRandomInt() }, |
73 | | - { id: "NL", state: "Nagaland", value: 59 }, |
74 | | - { id: "OR", state: "Odisha", value: 59 }, |
75 | | - { id: "PB", state: "Punjab", value: getRandomInt() }, |
76 | | - { id: "RJ", state: "Rajasthan", value: getRandomInt() }, |
77 | | - { id: "SK", state: "Sikkim", value: getRandomInt() }, |
78 | | - { id: "TN", state: "Tamil Nadu", value: getRandomInt() }, |
79 | | - { id: "TG", state: "Telangana", value: getRandomInt() }, |
80 | | - { id: "TR", state: "Tripura", value: 14 }, |
81 | | - { id: "UT", state: "Uttarakhand", value: getRandomInt() }, |
82 | | - { id: "UP", state: "Uttar Pradesh", value: 15 }, |
83 | | - { id: "WB", state: "West Bengal", value: 17 }, |
84 | | - { id: "WB", state: "West Bengal", value: 17 }, |
85 | | - { id: "AN", state: "Andaman and Nicobar Islands", value: getRandomInt() }, |
86 | | - { id: "CH", state: "Chandigarh", value: getRandomInt() }, |
87 | | - { id: "DN", state: "Dadra and Nagar Haveli", value: 19 }, |
88 | | - { id: "DD", state: "Daman and Diu", value: 20 }, |
89 | | - { id: "DL", state: "Delhi", value: 59 }, |
90 | | - { id: "JK", state: "Jammu and Kashmir", value: 25 }, |
91 | | - { id: "LA", state: "Ladakh", value: getRandomInt() }, |
92 | | - { id: "LD", state: "Lakshadweep", value: getRandomInt() }, |
93 | | - { id: "PY", state: "Puducherry", value: getRandomInt() }, |
94 | | - ]; |
95 | | -}; |
96 | | -// const getHeatMapData = () => { |
97 | | -// return [ |
98 | | -// { id: "AP", state: "Andhra Pradesh", value: getRandomInt() }, |
99 | | -// { id: "AR", state: "Arunachal Pradesh", value: getRandomInt() }, |
100 | | -// { id: "AS", state: "Assam", value: getRandomInt() }, |
101 | | -// { id: "BR", state: "Bihar", value: getRandomInt() }, |
102 | | -// { id: "CT", state: "Chhattisgarh", value: getRandomInt() }, |
103 | | -// { id: "GA", state: "Goa", value: 21 }, |
104 | | -// { id: "GJ", state: "Gujarat", value: 22 }, |
105 | | -// { id: "HR", state: "Haryana", value: getRandomInt() }, |
106 | | -// { id: "HP", state: "Himachal Pradesh", value: 24 }, |
107 | | -// { id: "JH", state: "Jharkhand", value: 26 }, |
108 | | -// { id: "KA", state: "Karnataka", value: 27 }, |
109 | | -// { id: "KL", state: "Kerala", value: getRandomInt() }, |
110 | | -// { id: "MP", state: "Madhya Pradesh", value: getRandomInt() }, |
111 | | -// { id: "MH", state: "Maharashtra", value: getRandomInt() }, |
112 | | -// { id: "MN", state: "Manipur", value: getRandomInt() }, |
113 | | -// { id: "ML", state: "Meghalaya", value: 59 }, |
114 | | -// { id: "MZ", state: "Mizoram", value: getRandomInt() }, |
115 | | -// { id: "NL", state: "Nagaland", value: 59 }, |
116 | | -// { id: "OR", state: "Odisha", value: 59 }, |
117 | | -// { id: "PB", state: "Punjab", value: getRandomInt() }, |
118 | | -// { id: "RJ", state: "Rajasthan", value: getRandomInt() }, |
119 | | -// { id: "SK", state: "Sikkim", value: getRandomInt() }, |
120 | | -// { id: "TN", state: "Tamil Nadu", value: getRandomInt() }, |
121 | | -// { id: "TG", state: "Telangana", value: getRandomInt() }, |
122 | | -// { id: "TR", state: "Tripura", value: 14 }, |
123 | | -// { id: "UT", state: "Uttarakhand", value: getRandomInt() }, |
124 | | -// { id: "UP", state: "Uttar Pradesh", value: 15 }, |
125 | | -// { id: "WB", state: "West Bengal", value: 17 }, |
126 | | -// { id: "WB", state: "West Bengal", value: 17 }, |
127 | | -// { id: "AN", state: "Andaman and Nicobar Islands", value: getRandomInt() }, |
128 | | -// { id: "CH", state: "Chandigarh", value: getRandomInt() }, |
129 | | -// { id: "DN", state: "Dadra and Nagar Haveli", value: 19 }, |
130 | | -// { id: "DD", state: "Daman and Diu", value: 20 }, |
131 | | -// { id: "DL", state: "Delhi", value: 59 }, |
132 | | -// { id: "JK", state: "Jammu and Kashmir", value: 25 }, |
133 | | -// { id: "LA", state: "Ladakh", value: getRandomInt() }, |
134 | | -// { id: "LD", state: "Lakshadweep", value: getRandomInt() }, |
135 | | -// { id: "PY", state: "Puducherry", value: getRandomInt() }, |
136 | | -// ]; |
137 | | -// }; |
138 | | - |
139 | | -function Map(props) { |
140 | | - const [tooltipContent, setTooltipContent] = useState(""); |
141 | | - const [data, setData] = useState(getHeatMapData()); |
142 | | - const completeData = props.completeData; |
143 | | - |
144 | | - const formatData = (completeData) => { |
145 | | - console.log(completeData); |
146 | | - const newArr = completeData.slice(-1).map((data) => data.regional); |
147 | | - const formatedData = newArr.flat().map((region, i) => { |
148 | | - return { |
149 | | - state: region.loc, |
150 | | - value: region.totalConfirmed, |
151 | | - }; |
152 | | - }); |
153 | | - console.log(formatedData); |
154 | | - }; |
155 | | - |
156 | | - formatData(completeData); |
157 | | - |
158 | | - const gradientData = { |
159 | | - fromColor: COLOR_RANGE[0], |
160 | | - toColor: COLOR_RANGE[COLOR_RANGE.length - 1], |
161 | | - min: 0, |
162 | | - max: data.reduce((max, item) => (item.value > max ? item.value : max), 0), |
163 | | - }; |
164 | | - |
165 | | - const colorScale = scaleQuantile() |
166 | | - .domain(data.map((d) => d.value)) |
167 | | - .range(COLOR_RANGE); |
| 52 | +class Map extends Component { |
| 53 | + constructor(props) { |
| 54 | + super(props); |
| 55 | + this.state = { |
| 56 | + tooltipContent: "", |
| 57 | + data: [ |
| 58 | + { id: "AP", state: "Andhra Pradesh", value: getRandomInt() }, |
| 59 | + { id: "AR", state: "Arunachal Pradesh", value: getRandomInt() }, |
| 60 | + ], |
| 61 | + }; |
| 62 | + } |
168 | 63 |
|
169 | | - const onMouseEnter = (geo, current = { value: "NA" }) => { |
| 64 | + onMouseEnter = (geo, current = { value: "NA" }) => { |
170 | 65 | return () => { |
171 | | - setTooltipContent(`${geo.properties.name}: ${current.value}`); |
| 66 | + this.setState({ |
| 67 | + tooltipContent: `${geo.properties.name}: ${current.value}`, |
| 68 | + }); |
172 | 69 | }; |
173 | 70 | }; |
174 | 71 |
|
175 | | - const onMouseLeave = () => { |
176 | | - setTooltipContent(""); |
| 72 | + onMouseLeave = () => { |
| 73 | + this.setState({ tooltipContent: "" }); |
177 | 74 | }; |
178 | 75 |
|
179 | | - const onChangeButtonClick = () => { |
180 | | - setData(getHeatMapData()); |
| 76 | + onChangeButtonClick = () => { |
| 77 | + this.setState({ data: this.formatData(this.props.completeData) }); |
181 | 78 | }; |
182 | 79 |
|
183 | | - return ( |
184 | | - <div className="full-width-height container"> |
185 | | - <ReactTooltip>{tooltipContent}</ReactTooltip> |
186 | | - <ComposableMap |
187 | | - projectionConfig={PROJECTION_CONFIG} |
188 | | - projection="geoMercator" |
189 | | - width={600} |
190 | | - height={220} |
191 | | - data-tip="" |
192 | | - > |
193 | | - <Geographies geography={INDIA_TOPO_JSON}> |
194 | | - {({ geographies }) => |
195 | | - geographies.map((geo) => { |
196 | | - const current = data.find((s) => s.id === geo.id); |
197 | | - return ( |
198 | | - <Geography |
199 | | - key={geo.rsmKey} |
200 | | - geography={geo} |
201 | | - fill={current ? colorScale(current.value) : DEFAULT_COLOR} |
202 | | - style={geographyStyle} |
203 | | - onMouseEnter={onMouseEnter(geo, current)} |
204 | | - onMouseLeave={onMouseLeave} |
205 | | - /> |
206 | | - ); |
207 | | - }) |
208 | | - } |
209 | | - </Geographies> |
210 | | - </ComposableMap> |
211 | | - <LinearGradient data={gradientData} /> |
212 | | - </div> |
213 | | - ); |
| 80 | + render() { |
| 81 | + const data = this.props.mapData; |
| 82 | + const gradientData = { |
| 83 | + fromColor: COLOR_RANGE[0], |
| 84 | + toColor: COLOR_RANGE[COLOR_RANGE.length - 1], |
| 85 | + min: 0, |
| 86 | + max: data.reduce((max, item) => (item.value > max ? item.value : max), 0), |
| 87 | + }; |
| 88 | + |
| 89 | + const colorScale = scaleQuantile() |
| 90 | + .domain(data.map((d) => d.value)) |
| 91 | + .range(COLOR_RANGE); |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="full-width-height container"> |
| 95 | + <ReactTooltip>{this.state.tooltipContent}</ReactTooltip> |
| 96 | + <ComposableMap |
| 97 | + projectionConfig={PROJECTION_CONFIG} |
| 98 | + projection="geoMercator" |
| 99 | + width={600} |
| 100 | + height={220} |
| 101 | + data-tip="" |
| 102 | + > |
| 103 | + <Geographies geography={INDIA_TOPO_JSON}> |
| 104 | + {({ geographies }) => |
| 105 | + geographies.map((geo) => { |
| 106 | + const current = data.find((s) => s.id === geo.id); |
| 107 | + return ( |
| 108 | + <Geography |
| 109 | + key={geo.rsmKey} |
| 110 | + geography={geo} |
| 111 | + fill={current ? colorScale(current.value) : DEFAULT_COLOR} |
| 112 | + style={geographyStyle} |
| 113 | + onMouseEnter={this.onMouseEnter(geo, current)} |
| 114 | + onMouseLeave={this.onMouseLeave} |
| 115 | + /> |
| 116 | + ); |
| 117 | + }) |
| 118 | + } |
| 119 | + </Geographies> |
| 120 | + </ComposableMap> |
| 121 | + <LinearGradient data={gradientData} /> |
| 122 | + </div> |
| 123 | + ); |
| 124 | + } |
214 | 125 | } |
215 | 126 |
|
216 | 127 | export default Map; |
0 commit comments