Skip to content

Commit 6948fea

Browse files
committed
add legends to map
1 parent 3dfe655 commit 6948fea

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

index.html

+28-31
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
<!-- Load plotly.js into the DOM -->
88
<script src="https://cdn.plot.ly/plotly-2.16.1.min.js"></script>
99

10-
<script
11-
src="https://code.jquery.com/jquery-3.6.3.min.js"
12-
integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU="
13-
crossorigin="anonymous"
14-
></script>
10+
<script src="https://code.jquery.com/jquery-3.6.3.min.js"
11+
integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
1512

1613
<body>
1714
<label>Election Type:</label>
@@ -121,18 +118,18 @@
121118
//compatible with president and legialator-at-large
122119
let candidates = !candidateData.columns.includes("candidate")
123120
? Array.from(
124-
new Set(
125-
candidateData.map((d) => [
126-
d["candidate_party"],
127-
d["candidate_party"],
128-
])
129-
)
130-
)
121+
new Set(
122+
candidateData.map((d) => [
123+
d["candidate_party"],
124+
d["candidate_party"],
125+
])
126+
)
127+
)
131128
: Array.from(
132-
new Set(
133-
candidateData.map((d) => [d["candidate"], d["candidate_party"]])
134-
)
135-
);
129+
new Set(
130+
candidateData.map((d) => [d["candidate"], d["candidate_party"]])
131+
)
132+
);
136133
for (let c of candidates) {
137134
candidateInfo[c[0]] = {
138135
party: c[1],
@@ -173,29 +170,29 @@
173170
width: 800,
174171
height: 800,
175172
title: title,
176-
colorway: [
177-
"#f3cec9",
178-
"#e7a4b6",
179-
"#cd7eaf",
180-
181-
],
182173
};
183174

184175
var config = {
185176
mapboxAccessToken:
186177
"pk.eyJ1IjoiYTA1MTI4IiwiYSI6ImNsY2JybmJyYTIyZzEzb2w3dGl3cXpkdXYifQ.aIV8apormocbhm8GpqbySg",
187178
};
188179

189-
//todo: draw legeng in map ?
180+
//legends by adding dummy traces
181+
let data = candidates.map((c) => {
182+
return {
183+
type: "scattermapbox",
184+
lat: [46],
185+
lon: [-74],
186+
z: [0],
187+
name: c[0]==c[1]?c[0]:c[0]+" ("+c[1]+")",
188+
showlegend: true,
189+
color: partyColor(c[1]),
190+
}
191+
});
192+
190193
Plotly.newPlot(
191194
"map",
192-
[
193-
{
194-
type: "scattermapbox",
195-
lat: [46],
196-
lon: [-74],
197-
},
198-
],
195+
data,
199196
layout,
200197
config
201198
);
@@ -248,4 +245,4 @@
248245
}
249246

250247
presidentPlot(files["president"], 2016);
251-
</script>
248+
</script>

0 commit comments

Comments
 (0)