Commit 25d53dee authored by Rodrigo Tapia-McClung's avatar Rodrigo Tapia-McClung

Improve line chart creation

parent 4b3219fd
This diff is collapsed.
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* January 2021 * January 2021
*/ */
/* globals omnivore, Promise, makeBaseMap, am4core, makeRadialChart */ /* globals omnivore, Promise, makeBaseMap, am4core, makeRadialChart, makeLineChart */
/* exported. userFiles, userDates, timeParse, layerControl, yearList */ /* exported. userFiles, userDates, timeParse, layerControl, yearList */
let timeParse, let timeParse,
...@@ -288,6 +288,10 @@ let barChartData = [], ...@@ -288,6 +288,10 @@ let barChartData = [],
const populateMap = async(mapData) => { const populateMap = async(mapData) => {
let baseUrl = new URL(`/data`, window.location.href).href; let baseUrl = new URL(`/data`, window.location.href).href;
// create sets of unique months and years
yearList = [... new Set(userDates.map( date => new Date(date).getFullYear() ))];
//let formatter = new Intl.DateTimeFormat('es', { month: 'long' });
// RADIAL CHART // RADIAL CHART
// get am chart - may need this to update chart on date change // get am chart - may need this to update chart on date change
//let radialChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "radial_chart"); //let radialChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "radial_chart");
...@@ -297,20 +301,15 @@ const populateMap = async(mapData) => { ...@@ -297,20 +301,15 @@ const populateMap = async(mapData) => {
//let query = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 value1,sum(perimcpo) value2, sum(dimfrcpo) value3`; //let query = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 value1,sum(perimcpo) value2, sum(dimfrcpo) value3`;
let query = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 area`; let query = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 area`;
const queryData = await d3.json(query); const queryData = await d3.json(query);
queryData[0].date = new Date(userDates[i]) queryData[0].date = new Date(userDates[i]);
return new Promise(resolve => { return new Promise(resolve => {
resolve(queryData[0]); resolve(queryData[0]);
}); });
}); });
// wait for all queries to complete and then set chart data // wait for all queries to complete and then set chart data
Promise.all(queries).then( data => { Promise.all(queries).then( data => {
// create sets of unique months and years
yearList = [... new Set(userDates.map( date => new Date(date).getFullYear() ))];
//let formatter = new Intl.DateTimeFormat('es', { month: 'long' });
// TODO: maybe make this a function so it can be used to update chart when changing dates // TODO: maybe make this a function so it can be used to update chart when changing dates
let chartaData = []; let chartData = [];
monthArray.forEach( (month, index) => { monthArray.forEach( (month, index) => {
let element = {}; let element = {};
yearList.forEach( year => { yearList.forEach( year => {
...@@ -319,51 +318,49 @@ const populateMap = async(mapData) => { ...@@ -319,51 +318,49 @@ const populateMap = async(mapData) => {
let yearData = data.filter(d => new Date(d.date).getMonth() == index && new Date(d.date).getFullYear() == year); let yearData = data.filter(d => new Date(d.date).getMonth() == index && new Date(d.date).getFullYear() == year);
element[year] = yearData.length > 0 ? yearData[0].area : null; element[year] = yearData.length > 0 ? yearData[0].area : null;
}); });
chartaData.push(element); chartData.push(element);
}); });
makeRadialChart(chartaData, monthArray, yearList); makeRadialChart(chartData, monthArray, yearList);
}); });
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ LINE CHART //LINE CHART
let data_ls = []; // get am chart - may need this to update chart on date change
let linesChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "linesChart"); //let linesChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "linesChart");
// async queries for each date that has been loaded // async queries for each date that has been loaded
const queries_ls = userFiles.map(async(mes, i) => { const queries_ls = userFiles.map(async(mes, i) => {
let query = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 area, descrip&group=descrip`; let query = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 area, descrip&group=descrip`;
const queryData = await d3.json(query); const queryData = await d3.json(query);
let date = new Date(dateArray[i]);
//date.setFullYear(2016)
for (const wbody in queryData) { for (const wbody in queryData) {
queryData[wbody].date = date; queryData[wbody].date = new Date(userDates[i]);
} }
data_ls.push(queryData); return new Promise(resolve => {
resolve(queryData);
});
}); });
// wait for all queries to complete // wait for all queries to complete and then set chart data
const dataQueries_ls = await Promise.all(queries_ls); Promise.all(queries_ls).then( data => {
// and then set chart data // TODO: maybe make this a function so it can be used to update chart when changing dates
//lineStackedChart.data = data_ls; let merged = [].concat.apply([], data);
var merged = [].concat.apply([], data_ls); let aguaPermanente = merged.filter(d => d.descrip == "Agua permanente" );
//console.log(merged); let aguaTemporal = merged.filter(d => d.descrip == "Áreas temporalmente inundadas" );
let aguaVegetacion = merged.filter(d => d.descrip == "Suelos húmedos-vegetación acuática" );
let aguaPermanente = merged.filter(function(d) { return d.descrip == "Agua permanente" });
let aguaTemporal = merged.filter(function(d) { return d.descrip == "Áreas temporalmente inundadas" }); let chartData = [];
let aguaVegetacion = merged.filter(function(d) { return d.descrip == "Suelos húmedos-vegetación acuática" }); aguaPermanente.forEach( (wb, i) => {
let element = {
let i; permanente: aguaPermanente[i].area,
for (i = 0; i < aguaPermanente.length; i++) { temporal: aguaTemporal[i].area,
aguaPermanente[i].permanente = aguaPermanente[i].area; vegetacion: aguaVegetacion[i].area,
aguaPermanente[i].temporal = aguaTemporal[i].area; date: aguaPermanente[i].date
aguaPermanente[i].vegetacion = aguaVegetacion[i].area; }
chartData.push(element);
delete aguaPermanente[i].descrip; });
delete aguaPermanente[i].area;
}
let grijalva_bodies = aguaPermanente; chartData.sort((a, b) => (a.date > b.date) ? 1 : -1);
grijalva_bodies.sort((a, b) => (a.date > b.date) ? 1 : -1); makeLineChart(chartData);
linesChart.data = grijalva_bodies; });
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ STACKED AREAS CHART //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ STACKED AREAS CHART
let data_sa = []; let data_sa = [];
let stackedAreaChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "stackedAreaChart"); let stackedAreaChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "stackedAreaChart");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment