add caption
This commit is contained in:
parent
ce65d04cdc
commit
9ea145fdfc
16
index.js
16
index.js
|
@ -17,13 +17,13 @@
|
||||||
const f = response.url.split('/').pop();
|
const f = response.url.split('/').pop();
|
||||||
if (f === "metadata.json") {
|
if (f === "metadata.json") {
|
||||||
const metadata = await response.json();
|
const metadata = await response.json();
|
||||||
states = new Map(metadata.geo.map((c) => [c.uid, c.name]))
|
states = new Map(metadata.geo.map((c) => [c.uid, c.name]));
|
||||||
parties = new Map(metadata.parties.map((c) => [c.code, c.name]));
|
parties = new Map(metadata.parties.map((c) => [c.code, c.name]));
|
||||||
candidates = new Map(metadata.candidates.map((c) => [c.uid, [c.fullName, parties.get(c.candidateRaces[0].majorParty)]]));
|
candidates = new Map(metadata.candidates.map((c) => [c.uid, [c.fullName, parties.get(c.candidateRaces[0].majorParty)]]));
|
||||||
} else if (f === "president.json") {
|
} else if (f === "president.json") {
|
||||||
|
|
||||||
const president = await response.json();
|
const president = await response.json();
|
||||||
let data = president[0]["state_electionTypes"].map((t) => t.state.officeRaces[0].candidateVotes.map((o) => {
|
let data = president[0].state_electionTypes.map((t) => t.state.officeRaces[0].candidateVotes.map((o) => {
|
||||||
return {
|
return {
|
||||||
"name": (candidates) ? candidates.get(o.candidate) : [o.candidate, "?"],
|
"name": (candidates) ? candidates.get(o.candidate) : [o.candidate, "?"],
|
||||||
"votes": o.totalVote
|
"votes": o.totalVote
|
||||||
|
@ -40,7 +40,7 @@
|
||||||
return acc;
|
return acc;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
let stateVotes = president[0]["state_electionTypes"].map((t) => {
|
let stateVotes = president[0].state_electionTypes.map((t) => {
|
||||||
return {
|
return {
|
||||||
"state": (states) ? states.get(t.state.uid) : t.uid,
|
"state": (states) ? states.get(t.state.uid) : t.uid,
|
||||||
"current": t.state.officeRaces[0].officeVotes[0].totalVote,
|
"current": t.state.officeRaces[0].officeVotes[0].totalVote,
|
||||||
|
@ -77,9 +77,15 @@
|
||||||
stateContainer.innerHTML = '';
|
stateContainer.innerHTML = '';
|
||||||
stateVotes.forEach((row) => {
|
stateVotes.forEach((row) => {
|
||||||
const table = document.createElement('table');
|
const table = document.createElement('table');
|
||||||
|
const caption = document.createElement('caption');
|
||||||
|
caption.innerHTML = `
|
||||||
|
<h2>${row.state} (Votes Left: ${row.expected - row.current})</h2>
|
||||||
|
`;
|
||||||
|
table.appendChild(caption);
|
||||||
|
|
||||||
const thead = document.createElement('thead');
|
const thead = document.createElement('thead');
|
||||||
const headerRow = document.createElement('tr');
|
const headerRow = document.createElement('tr');
|
||||||
const headers = [`${row.state}`, 'Party', `Votes Left: ${row.expected - row.current}`];
|
const headers = ['Name', 'Party', 'Votes'];
|
||||||
headers.forEach(header => {
|
headers.forEach(header => {
|
||||||
const th = document.createElement('th');
|
const th = document.createElement('th');
|
||||||
th.textContent = header;
|
th.textContent = header;
|
||||||
|
@ -89,7 +95,7 @@
|
||||||
table.appendChild(thead);
|
table.appendChild(thead);
|
||||||
|
|
||||||
const tbody = document.createElement('tbody');
|
const tbody = document.createElement('tbody');
|
||||||
row.candidates.sort((a, b) => b.votes - a.votes).forEach((candidate, index) => {
|
row.candidates.sort((a, b) => b.votes - a.votes).forEach((candidate) => {
|
||||||
const rowElement = document.createElement('tr');
|
const rowElement = document.createElement('tr');
|
||||||
|
|
||||||
const nameCell = document.createElement('td');
|
const nameCell = document.createElement('td');
|
||||||
|
|
Loading…
Reference in New Issue