-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from brodly/js
Added toggle to display total requests per year
- Loading branch information
Showing
3 changed files
with
98 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters