Commit dc2b749b authored by Rodrigo Tapia-McClung's avatar Rodrigo Tapia-McClung

Improve area chart creation

parent 25d53dee
This diff is collapsed.
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* January 2021 * January 2021
*/ */
/* globals omnivore, Promise, makeBaseMap, am4core, makeRadialChart, makeLineChart */ /* globals omnivore, Promise, makeBaseMap, am4core, makeRadialChart, makeLineChart, makeAreaChart */
/* exported. userFiles, userDates, timeParse, layerControl, yearList */ /* exported. userFiles, userDates, timeParse, layerControl, yearList */
let timeParse, let timeParse,
...@@ -323,7 +323,7 @@ const populateMap = async(mapData) => { ...@@ -323,7 +323,7 @@ const populateMap = async(mapData) => {
makeRadialChart(chartData, monthArray, yearList); makeRadialChart(chartData, monthArray, yearList);
}); });
//LINE CHART // LINE 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 linesChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "linesChart"); //let linesChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "linesChart");
...@@ -337,7 +337,6 @@ const populateMap = async(mapData) => { ...@@ -337,7 +337,6 @@ const populateMap = async(mapData) => {
return new Promise(resolve => { return new Promise(resolve => {
resolve(queryData); resolve(queryData);
}); });
}); });
// 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_ls).then( data => { Promise.all(queries_ls).then( data => {
...@@ -361,45 +360,45 @@ const populateMap = async(mapData) => { ...@@ -361,45 +360,45 @@ const populateMap = async(mapData) => {
chartData.sort((a, b) => (a.date > b.date) ? 1 : -1); chartData.sort((a, b) => (a.date > b.date) ? 1 : -1);
makeLineChart(chartData); makeLineChart(chartData);
}); });
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ STACKED AREAS CHART // STACKED AREA CHART
let data_sa = []; // get am chart - may need this to update chart on date change
let stackedAreaChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "stackedAreaChart"); //let stackedAreaChart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "stackedAreaChart");
// async queries for each date that has been loaded // async queries for each date that has been loaded
// TODO: this is the exact same call as in LINE CHART => merge into one
const queries_sa = userFiles.map(async(mes, i) => { const queries_sa = userFiles.map(async(mes, i) => {
let query_sa = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 area, descrip&group=descrip`; let query_sa = `${baseUrl}/query/${mes}?columns=sum(areacpo)/10000 area, descrip&group=descrip`;
const queryData = await d3.json(query_sa); const queryData = await d3.json(query_sa);
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_sa.push(queryData); //data_sa.push(queryData);
return new Promise(resolve => {
}); resolve(queryData);
// wait for all queries to complete });
const dataQueries_sa = await Promise.all(queries_sa); });
// and then set chart data // wait for all queries to complete and then set chart data
//lineStackedChart.data = data_ls; Promise.all(queries_sa).then( data => {
var merged_1 = [].concat.apply([], data_sa); // TODO: maybe make this a function so it can be used to update chart when changing dates
//console.log(data_sa); let merged = [].concat.apply([], data);
let aguaPermanente = merged.filter(d => d.descrip == "Agua permanente" );
let aguaPermanente_1 = merged_1.filter(function(d) { return d.descrip == "Agua permanente" }); let aguaTemporal = merged.filter(d => d.descrip == "Áreas temporalmente inundadas" );
let aguaTemporal_1 = merged_1.filter(function(d) { return d.descrip == "Áreas temporalmente inundadas" }); let aguaVegetacion = merged.filter(d => d.descrip == "Suelos húmedos-vegetación acuática" );
let aguaVegetacion_1 = merged_1.filter(function(d) { return d.descrip == "Suelos húmedos-vegetación acuática" });
let chartData = [];
let j; aguaPermanente.forEach( (wb, i) => {
for (j = 0; j < aguaPermanente_1.length; j++) { let element = {
aguaPermanente_1[j].permanente = aguaPermanente_1[j].area; permanente: aguaPermanente[i].area,
aguaPermanente_1[j].temporal = aguaTemporal_1[j].area; temporal: aguaTemporal[i].area,
aguaPermanente_1[j].vegetacion = aguaVegetacion_1[j].area; vegetacion: aguaVegetacion[i].area,
delete aguaPermanente_1[j].descrip; date: aguaPermanente[i].date
delete aguaPermanente_1[j].area;
} }
chartData.push(element);
});
let grijalva_bodies_1 = aguaPermanente_1; chartData.sort((a, b) => (a.date > b.date) ? 1 : -1);
grijalva_bodies_1.sort((a, b) => (a.date > b.date) ? 1 : -1) makeAreaChart(chartData);
stackedAreaChart.data = grijalva_bodies_1; });
// populate bars // populate bars
......
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