demographicTableTwo = async (demographic = "age", lang = "en") => {
const censusDemographics = IllinoisStatewideDemographics[0].Race;
let data = IllinoisCountyCovidData.demographics[demographic];
if (demographic === "race") {
data = data.sort((a, b) => b.count - a.count);
}
const totalCount = d3.sum(data, d => d.count);
const totalDeaths = d3.sum(data, d => d.deaths);
data = data.map(d => {
d.count_pct = d.count / totalCount;
d.death_pct = d.deaths / totalDeaths;
return d;
});
const countScale = d3
.scaleLinear()
.domain([0, d3.max(data, d => d.count_pct)])
.range(["#fff5eb", "#e25609"]);
const deathScale = d3
.scaleLinear()
.domain([0, d3.max(data, d => d.death_pct)])
.range(["#f1eff6", "#7363ac"]);
const popScale = d3
.scaleLinear()
.domain([0, d3.max(Object.values(censusDemographics), d => d.PE)])
.range(["#e3eef9", "#2f7ebc"]);
const legendScale = (label, color, data, selector) =>
legend({
title: `% of ${label}`,
color: color,
tickFormat: '%',
tickValues: [0, d3.max(data, d => d[selector])],
width: 60,
marginLeft: 5
});
const tableLabelColor = hex => {
const color = d3.hsl(d3.color(hex));
return color.l < .7 ? "#ffffff" : "#000000";
};
return html`
<table class="demographic-table">
<thead>
<tr>
<th>${messageFormatter(
"table.labels." + demographic,
lang
).format()}</th>
<th>${messageFormatter("table.labels.cases", lang).format()}</th>
<th>${messageFormatter("table.labels.deaths", lang).format()}</th>
${
demographic === "race"
? html`<th>${messageFormatter(
"table.labels.population_long",
lang
).format()}</th>`
: ''
}
</tr>
</thead>
<tbody>
${data.map(d => {
const demoValue =
d[demographicKeyLabelMap.get(demographic).descColumn];
let demoLabel = '';
if ('race' === demographic) {
demoLabel = html`${messageFormatter(
"table.labels.races." + demoValue,
lang
).format()}`;
if (demoValue === "Left Blank")
demoLabel = html`<em>${demoLabel}</em>`;
} else {
demoLabel = html`${demoValue}`;
//if (demoValue === "Unknown") demoLabel = html`<em>${messageFormatter("table.labels.unknown",lang).format()}</em>`;
if (demoValue === "Unknown") return html``;
}
return html`
<tr>
<td class="description">
<span class="${
demoValue === "Left Blank" || demoValue === "Unknown"
? "blank"
: ""
}">
${demoLabel}
</span>
</td>
<td class="number text-bold border-bottom-dotted">
<span class="align-right" style="background-color: ${countScale(
d.count_pct
)}; color: ${tableLabelColor(
countScale(d.count_pct)
)};">${d.count.toLocaleString(lang)}</span>
</td>
<td class="number text-bold border-bottom-dotted">
<span class="align-right" style="background-color: ${deathScale(
d.death_pct
)}; color: ${tableLabelColor(
deathScale(d.death_pct)
)};">${d.deaths.toLocaleString(lang)}</span>
</td>
${
demographic === "race"
? html`<td class="number">
${
censusDemographics[d.description]
? censusDemographics[d.description].PE < .01
? html`<span class="too-low"><${(.01).toLocaleString(
lang,
{ style: "percent" }
)}</span>`
: html`<span style="
background-color: ${popScale(
censusDemographics[d.description].PE
)};
color: ${tableLabelColor(
popScale(censusDemographics[d.description].PE)
)};">
${censusDemographics[
d.description
].E.toLocaleString(lang, {})}
</span>`
: html`<span class="na">n/a</span>`
}
</td>`
: ''
}
</tr>`;
})}
<tr class="legend-row">
<td></td>
<td class="legend">
${legendScale(
messageFormatter("table.labels.cases", lang).format(),
countScale,
data,
'count_pct'
)}
</td>
<td class="legend">
${legendScale(
messageFormatter("table.labels.deaths", lang).format(),
deathScale,
data,
'death_pct'
)}
</td>
${
demographic === "race"
? html`<td class="legend">
${legendScale(
messageFormatter("table.labels.population", lang).format(),
popScale,
Object.values(censusDemographics),
'PE'
)}
</td>`
: ''
}
</tr>
</tbody>
</table>
`;
}