sejm-calculator/src/index.ts

90 wiersze
2.6 KiB
TypeScript
Czysty Zwykły widok Historia

2019-09-24 09:57:02 +00:00
import Chartist, {ChartistStatic} from 'chartist';
2019-09-24 17:21:02 +00:00
import {committees, constituencies} from './data';
2019-09-07 21:11:03 +00:00
import {calculateMandates} from './mandates';
2019-09-24 17:21:02 +00:00
import template from './constituency.pug';
2019-08-29 17:07:09 +00:00
import './styles.css';
2019-09-24 09:57:02 +00:00
const clearResults = (bar: ChartistStatic['Bar'], pie: ChartistStatic['Pie']) => {
2019-09-24 17:21:02 +00:00
document.querySelectorAll<HTMLTableDataCellElement>(`tr td:last-child`).forEach(td => {
td.innerHTML = '';
});
2019-09-24 09:57:02 +00:00
bar.detach();
pie.detach();
2019-09-24 17:21:02 +00:00
document.getElementById('support-bar-chart')!.innerHTML = '';
2019-09-24 09:57:02 +00:00
document.getElementById('division-pie-chart')!.innerHTML = '';
2019-09-24 17:21:02 +00:00
document.getElementById('constituency-results')!.innerHTML = '';
}
const displayConstituencyResults = () => {
const container = document.getElementById('constituency-results');
constituencies.forEach((constituency, index) => {
const data = (constituency.mandates && constituency.support)
? constituency.mandates.map((mandates, index) => ({
committee: committees[index].name,
support: (constituency.support as number[])[index],
mandates,
}))
: [];
data.sort((a, b) => b.support - a.support)
container!.insertAdjacentHTML('beforeend', template({
number: index + 1,
name: constituency.name,
size: constituency.size,
data,
}));
2019-09-24 09:57:02 +00:00
});
}
2019-09-07 18:30:44 +00:00
const handleCalculateButtonClick = (event: Event) => {
event.preventDefault();
2019-09-24 09:57:02 +00:00
const inputs = document.querySelectorAll<HTMLInputElement>('#support-form input');
2019-09-07 18:30:44 +00:00
const support = Array
2019-09-24 09:57:02 +00:00
.from(inputs)
2019-09-07 18:30:44 +00:00
.map(input => parseFloat(input.value));
2019-09-21 18:39:32 +00:00
if (!document.querySelector<HTMLFormElement>('#support-form')!.checkValidity()) return;
2019-09-07 18:30:44 +00:00
const mandates = calculateMandates(support);
2019-09-24 09:57:02 +00:00
mandates.forEach((value, index) => {
const committeeId = committees[index].id;
const td = document.querySelector<HTMLTableDataCellElement>(`tr.${committeeId} td:last-child`);
if (td) td.textContent = value.toString();
});
2019-09-07 18:30:44 +00:00
const barChartData = {
2019-09-21 18:39:32 +00:00
labels: committees.map(committee => committee.shortName).slice(0, -1),
series: support,
2019-09-07 18:30:44 +00:00
};
2019-09-21 18:39:32 +00:00
const barChartOptions = {
distributeSeries: true,
};
2019-09-24 09:57:02 +00:00
const bar = new Chartist.Bar('#support-bar-chart', barChartData, barChartOptions);
2019-09-07 18:30:44 +00:00
const pieChartData = {
series: mandates,
};
const pieChartOptions = {
donut: true,
donutWidth: 60,
startAngle: 270,
total: 460 * 2,
};
2019-09-24 09:57:02 +00:00
const pie = new Chartist.Pie('#division-pie-chart', pieChartData, pieChartOptions);
2019-09-24 17:21:02 +00:00
displayConstituencyResults();
2019-09-24 09:57:02 +00:00
inputs.forEach(input => input.addEventListener('input', () => {
clearResults(bar, pie);
2019-09-24 17:21:02 +00:00
}));
2019-09-07 18:30:44 +00:00
}
const bindActions = () => {
document
.querySelector('#calculate-button')!
.addEventListener('click', handleCalculateButtonClick);
}
document.addEventListener('DOMContentLoaded', () => {
bindActions();
});