sejm-calculator/src/index.ts

46 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

2019-09-07 18:30:44 +00:00
import Chartist 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-07 18:30:44 +00:00
const handleCalculateButtonClick = (event: Event) => {
event.preventDefault();
2019-09-21 18:39:32 +00:00
// clear charts
2019-09-07 18:30:44 +00:00
const support = Array
.from(document.querySelectorAll<HTMLInputElement>('#support-form input'))
.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);
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,
};
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,
};
new Chartist.Pie('#division-pie-chart', pieChartData, pieChartOptions);
}
const bindActions = () => {
document
.querySelector('#calculate-button')!
.addEventListener('click', handleCalculateButtonClick);
}
document.addEventListener('DOMContentLoaded', () => {
bindActions();
});