sejm-calculator/src/index.ts

66 wiersze
1.9 KiB
TypeScript
Czysty Zwykły widok Historia

2019-09-24 09:57:02 +00:00
import Chartist, {ChartistStatic} from 'chartist';
2019-09-07 21:11:03 +00:00
import {committees} from './data';
import {calculateMandates} from './mandates';
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']) => {
bar.detach();
document.getElementById('support-bar-chart')!.innerHTML = '';
pie.detach();
document.getElementById('division-pie-chart')!.innerHTML = '';
document.querySelectorAll<HTMLTableDataCellElement>(`tr td:last-child`).forEach(td => {
td.innerHTML = '';
});
}
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);
inputs.forEach(input => input.addEventListener('input', () => {
clearResults(bar, pie);
}))
2019-09-07 18:30:44 +00:00
}
const bindActions = () => {
document
.querySelector('#calculate-button')!
.addEventListener('click', handleCalculateButtonClick);
}
document.addEventListener('DOMContentLoaded', () => {
bindActions();
});