diff --git a/src/javascript/public/app.js b/src/javascript/public/app.js index 7d23f0b78..9d6a10771 100644 --- a/src/javascript/public/app.js +++ b/src/javascript/public/app.js @@ -1,56 +1,83 @@ (function(){ let chartData = []; + let countRequestType = []; - const selectors = document.querySelectorAll('.data-dropdown'); - const chartType = document.querySelector('.chart-type'); - const submitButton = document.querySelector('button'); const chart = c3.generate({ bindto: '#chart', data: { - type: chartType.value, + type: document.querySelector('.chart-type').value, columns: [], }, + axis: { + x: { + show: false, + } + } }); - function displayChart() { - getData(selectors[0].value) - .then(() => { getData(selectors[1].value) - .then(() => { - console.log(chartData); - updateChart(chartData, chartType.value); - }) + function buildChart() { + const requestType = document.querySelector('.request-dropdown').value; + const chartType = document.querySelector('.chart-type').value; + const datasets = document.querySelectorAll('.data-dropdown'); + + getData(datasets[0].value, requestType) + .then(() => { + getTotal(datasets[0].value, requestType) + .then(() => { + getData(datasets[1].value, requestType) + .then(() => { + getTotal(datasets[1].value, requestType) + .then(() => { + renderChart(chartData, countRequestType, chartType); + }) + }) + }) }) - }; + }; - function updateChart(data, type) { + function renderChart(chartData, countRequestType, type) { + const isDisplayTotal = document.querySelector('input[name="total"]').checked; let columns = []; - const newData = data.reduce((acc, cur) => { - const date = cur.createddate.slice(0, 4); - if (acc[date]) acc[date].push(parseInt(cur.zipcode)); - else acc[date] = []; - return acc; - }, {}) - - for (let key in newData) { - newData[key].unshift(key); - columns.push(newData[key]); + + if (!isDisplayTotal) { + const newData = chartData.reduce((acc, cur) => { + const date = cur.createddate.slice(0, 4); + if (acc[date]) acc[date].push(parseInt(cur.zipcode)); + else acc[date] = []; + return acc; + }, {}) + + for (let key in newData) { + newData[key].unshift(key); + columns.push(newData[key]); + } + } else { + columns = countRequestType; } chart.load({ columns, type }); }; - function getData(action) { - return fetch(`/soda/${action}`) + function getData(year, requestType) { + return fetch(`/soda/${year}/${requestType}`) .then(res => res.json()) .then(data => { chartData.push(...data); }) .catch(err => { console.error('Fetch Error :-S', err); }); }; - submitButton.onclick = e => { + function getTotal(year, requestType) { + return fetch(`/soda/${year}/${requestType}/total`) + .then(res => res.json()) + .then(data => { countRequestType.push([year, parseInt(data[0].count_requesttype)]); }) + .catch(err => { console.error('Fetch Error :-S', err); }) + } + + document.querySelector('button').onclick = e => { e.preventDefault(); - // chartData = []; - updateChart(chartData, chartType.value); + chartData = []; + countRequestType = []; + buildChart(); }; - displayChart(); + buildChart(); })(); diff --git a/src/javascript/public/index.html b/src/javascript/public/index.html index e58d4e205..48e1273ff 100644 --- a/src/javascript/public/index.html +++ b/src/javascript/public/index.html @@ -11,6 +11,21 @@