Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
fordecyt_2019
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Rodrigo Tapia-McClung
fordecyt_2019
Commits
89c8ea79
Commit
89c8ea79
authored
Jan 27, 2021
by
Rodrigo Tapia-McClung
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update radar chart on date change
parent
c88945e4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
85 deletions
+134
-85
cuerpos_charts.js
public/js/cuerpos_charts.js
+73
-73
cuerpos_functions.js
public/js/cuerpos_functions.js
+61
-12
No files found.
public/js/cuerpos_charts.js
View file @
89c8ea79
...
...
@@ -6,7 +6,7 @@
*/
/* globals updateBarChart, barChartData, am4charts, am4core, am4themes_animated, yearList */
/* export makeRad
ial
Chart, makeLineChart, makeAreaChart, makeBarChart */
/* export makeRad
ar
Chart, makeLineChart, makeAreaChart, makeBarChart */
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function
grijalva_theme
(
target
)
{
...
...
@@ -44,29 +44,29 @@ am4core.useTheme(grijalva_theme);
let
mainTextColor
=
getComputedStyle
(
document
.
body
).
getPropertyValue
(
'--main-text-color'
);
const
makeRad
ial
Series
=
(
chart
,
value
,
category
,
name
,
color
)
=>
{
let
rad
ial
Series
=
chart
.
series
.
push
(
new
am4charts
.
RadarSeries
());
rad
ial
Series
.
dataFields
.
valueY
=
value
;
rad
ial
Series
.
dataFields
.
categoryX
=
category
;
rad
ial
Series
.
strokeWidth
=
1.5
;
rad
ial
Series
.
tooltipText
=
"{valueY}"
;
rad
ial
Series
.
name
=
name
;
//rad
ial
Series.connect = false;
//rad
ial
Series.connectEnds = false;
rad
ial
Series
.
stroke
=
am4core
.
color
(
color
);
// red
rad
ial
Series
.
tooltip
.
getFillFromObject
=
false
;
rad
ial
Series
.
tooltipText
=
"[bold] {name}: [/]{valueY.formatNumber('#,###.0')} Ha[/]"
;
rad
ial
Series
.
tooltip
.
background
.
fill
=
am4core
.
color
(
color
);
const
makeRad
ar
Series
=
(
chart
,
value
,
category
,
name
,
color
)
=>
{
let
rad
ar
Series
=
chart
.
series
.
push
(
new
am4charts
.
RadarSeries
());
rad
ar
Series
.
dataFields
.
valueY
=
value
;
rad
ar
Series
.
dataFields
.
categoryX
=
category
;
rad
ar
Series
.
strokeWidth
=
1.5
;
rad
ar
Series
.
tooltipText
=
"{valueY}"
;
rad
ar
Series
.
name
=
name
;
//rad
ar
Series.connect = false;
//rad
ar
Series.connectEnds = false;
rad
ar
Series
.
stroke
=
am4core
.
color
(
color
);
// red
rad
ar
Series
.
tooltip
.
getFillFromObject
=
false
;
rad
ar
Series
.
tooltipText
=
"[bold] {name}: [/]{valueY.formatNumber('#,###.0')} Ha[/]"
;
rad
ar
Series
.
tooltip
.
background
.
fill
=
am4core
.
color
(
color
);
}
const
makeRad
ial
Range
=
(
axis
,
start
,
end
,
color
,
span
)
=>
{
let
rad
ial
Range
=
axis
.
axisRanges
.
create
();
rad
ial
Range
.
category
=
start
;
rad
ial
Range
.
endcategory
=
end
;
rad
ial
Range
.
axisFill
.
fill
=
am4core
.
color
(
color
);
rad
ial
Range
.
axisFill
.
fillOpacity
=
0.4
;
rad
ial
Range
.
grid
.
strokeOpacity
=
0
;
rad
ial
Range
.
locations
.
endCategory
=
span
?
span
:
""
;
const
makeRad
ar
Range
=
(
axis
,
start
,
end
,
color
,
span
)
=>
{
let
rad
ar
Range
=
axis
.
axisRanges
.
create
();
rad
ar
Range
.
category
=
start
;
rad
ar
Range
.
endcategory
=
end
;
rad
ar
Range
.
axisFill
.
fill
=
am4core
.
color
(
color
);
rad
ar
Range
.
axisFill
.
fillOpacity
=
0.4
;
rad
ar
Range
.
grid
.
strokeOpacity
=
0
;
rad
ar
Range
.
locations
.
endCategory
=
span
?
span
:
""
;
}
const
makeLineAxisAndSeries
=
(
chart
,
field
,
name
,
opposite
,
bullet
)
=>
{
...
...
@@ -145,64 +145,64 @@ const makeBarSeries = (chart, field, name) => {
barValueLabel
.
label
.
fill
=
am4core
.
color
(
"#fff"
);
}
const
makeRad
ial
Chart
=
(
data
,
months
,
years
)
=>
{
let
rad
ial
Chart
=
am4core
.
create
(
"radial_chart"
,
am4charts
.
RadarChart
);
const
makeRad
ar
Chart
=
(
data
,
months
,
years
)
=>
{
let
rad
ar
Chart
=
am4core
.
create
(
"radial_chart"
,
am4charts
.
RadarChart
);
let
rad
ialTitle
=
radial
Chart
.
titles
.
create
();
rad
ial
Title
.
text
=
"Superficie total por año"
;
rad
ial
Title
.
fontSize
=
10
;
rad
ial
Title
.
marginBottom
=
10
;
rad
ial
Title
.
fill
=
am4core
.
color
(
mainTextColor
);
let
rad
arTitle
=
radar
Chart
.
titles
.
create
();
rad
ar
Title
.
text
=
"Superficie total por año"
;
rad
ar
Title
.
fontSize
=
10
;
rad
ar
Title
.
marginBottom
=
10
;
rad
ar
Title
.
fill
=
am4core
.
color
(
mainTextColor
);
rad
ial
Chart
.
data
=
data
;
rad
ar
Chart
.
data
=
data
;
/* Create axes */
let
rad
ialCategoryAxis
=
radial
Chart
.
xAxes
.
push
(
new
am4charts
.
CategoryAxis
());
rad
ial
CategoryAxis
.
dataFields
.
category
=
"month"
;
rad
ial
CategoryAxis
.
fontSize
=
9
;
rad
ial
CategoryAxis
.
renderer
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
rad
ial
CategoryAxis
.
renderer
.
grid
.
template
.
stroke
=
am4core
.
color
(
mainTextColor
);
let
rad
ialValueAxis
=
radial
Chart
.
yAxes
.
push
(
new
am4charts
.
ValueAxis
());
rad
ial
ValueAxis
.
renderer
.
grid
.
template
.
stroke
=
am4core
.
color
(
mainTextColor
);
rad
ial
ValueAxis
.
extraMin
=
0.2
;
rad
ial
ValueAxis
.
extraMax
=
0.2
;
rad
ial
ValueAxis
.
tooltip
.
disabled
=
true
;
rad
ial
ValueAxis
.
fontSize
=
9
;
rad
ial
ValueAxis
.
renderer
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
rad
ial
ValueAxis
.
renderer
.
grid
.
template
.
stroke
=
am4core
.
color
(
mainTextColor
);
//rad
ial
ValueAxis.renderer.labels.template.fontSize = 15;
let
rad
arCategoryAxis
=
radar
Chart
.
xAxes
.
push
(
new
am4charts
.
CategoryAxis
());
rad
ar
CategoryAxis
.
dataFields
.
category
=
"month"
;
rad
ar
CategoryAxis
.
fontSize
=
9
;
rad
ar
CategoryAxis
.
renderer
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
rad
ar
CategoryAxis
.
renderer
.
grid
.
template
.
stroke
=
am4core
.
color
(
mainTextColor
);
let
rad
arValueAxis
=
radar
Chart
.
yAxes
.
push
(
new
am4charts
.
ValueAxis
());
rad
ar
ValueAxis
.
renderer
.
grid
.
template
.
stroke
=
am4core
.
color
(
mainTextColor
);
rad
ar
ValueAxis
.
extraMin
=
0.2
;
rad
ar
ValueAxis
.
extraMax
=
0.2
;
rad
ar
ValueAxis
.
tooltip
.
disabled
=
true
;
rad
ar
ValueAxis
.
fontSize
=
9
;
rad
ar
ValueAxis
.
renderer
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
rad
ar
ValueAxis
.
renderer
.
grid
.
template
.
stroke
=
am4core
.
color
(
mainTextColor
);
//rad
ar
ValueAxis.renderer.labels.template.fontSize = 15;
/* Create and configure series */
let
seriesColor
=
[
"#F98293"
,
"#2AC2BB"
,
"#FDA259"
];
years
.
forEach
(
(
year
,
i
)
=>
{
makeRad
ial
Series
(
radialChart
,
year
,
"month"
,
year
,
seriesColor
[
i
]);
makeRad
ar
Series
(
radialChart
,
year
,
"month"
,
year
,
seriesColor
[
i
]);
});
rad
ial
Chart
.
scrollbarX
=
new
am4core
.
Scrollbar
();
rad
ial
Chart
.
scrollbarX
.
minHeight
=
1
;
rad
ialChart
.
scrollbarX
.
parent
=
radial
Chart
.
bottomAxesContainer
;
rad
ial
Chart
.
scrollbarY
=
new
am4core
.
Scrollbar
();
rad
ial
Chart
.
scrollbarY
.
minWidth
=
1
;
customizeGrip
(
rad
ial
Chart
.
scrollbarX
.
startGrip
);
customizeGrip
(
rad
ial
Chart
.
scrollbarX
.
endGrip
);
customizeGrip
(
rad
ial
Chart
.
scrollbarY
.
startGrip
);
customizeGrip
(
rad
ial
Chart
.
scrollbarY
.
endGrip
);
let
rad
ial
RangeColors
=
[
"#E1DBC8"
,
"#595952"
,
"#838278"
,
"#B1AE9F"
];
makeRad
ialRange
(
radialCategoryAxis
,
"enero"
,
"marzo"
,
radial
RangeColors
[
0
],
2
);
makeRad
ialRange
(
radialCategoryAxis
,
"marzo"
,
"mayo"
,
radial
RangeColors
[
1
],
2
);
makeRad
ialRange
(
radialCategoryAxis
,
"mayo"
,
"septiembre"
,
radial
RangeColors
[
2
],
4
);
makeRad
ialRange
(
radialCategoryAxis
,
"septiembre"
,
"diciembre"
,
radial
RangeColors
[
3
],
3
);
makeRad
ialRange
(
radialCategoryAxis
,
"diciembre"
,
"diciembre"
,
radial
RangeColors
[
0
],
1
);
rad
ial
Chart
.
cursor
=
new
am4charts
.
RadarCursor
();
rad
ar
Chart
.
scrollbarX
=
new
am4core
.
Scrollbar
();
rad
ar
Chart
.
scrollbarX
.
minHeight
=
1
;
rad
arChart
.
scrollbarX
.
parent
=
radar
Chart
.
bottomAxesContainer
;
rad
ar
Chart
.
scrollbarY
=
new
am4core
.
Scrollbar
();
rad
ar
Chart
.
scrollbarY
.
minWidth
=
1
;
customizeGrip
(
rad
ar
Chart
.
scrollbarX
.
startGrip
);
customizeGrip
(
rad
ar
Chart
.
scrollbarX
.
endGrip
);
customizeGrip
(
rad
ar
Chart
.
scrollbarY
.
startGrip
);
customizeGrip
(
rad
ar
Chart
.
scrollbarY
.
endGrip
);
let
rad
ar
RangeColors
=
[
"#E1DBC8"
,
"#595952"
,
"#838278"
,
"#B1AE9F"
];
makeRad
arRange
(
radarCategoryAxis
,
"enero"
,
"marzo"
,
radar
RangeColors
[
0
],
2
);
makeRad
arRange
(
radarCategoryAxis
,
"marzo"
,
"mayo"
,
radar
RangeColors
[
1
],
2
);
makeRad
arRange
(
radarCategoryAxis
,
"mayo"
,
"septiembre"
,
radar
RangeColors
[
2
],
4
);
makeRad
arRange
(
radarCategoryAxis
,
"septiembre"
,
"diciembre"
,
radar
RangeColors
[
3
],
3
);
makeRad
arRange
(
radarCategoryAxis
,
"diciembre"
,
"diciembre"
,
radar
RangeColors
[
0
],
1
);
rad
ar
Chart
.
cursor
=
new
am4charts
.
RadarCursor
();
// add line legend
rad
ial
Chart
.
legend
=
new
am4charts
.
Legend
();
//rad
ial
Chart.legend.fontSize = 10;
rad
ial
Chart
.
legend
.
fontSize
=
9
;
rad
ial
Chart
.
legend
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
rad
ar
Chart
.
legend
=
new
am4charts
.
Legend
();
//rad
ar
Chart.legend.fontSize = 10;
rad
ar
Chart
.
legend
.
fontSize
=
9
;
rad
ar
Chart
.
legend
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
let
legenddata
=
[{
name
:
"Estiaje"
,
fill
:
am4core
.
color
(
"#595952"
)
},
{
name
:
"Tropical"
,
fill
:
am4core
.
color
(
"#838278"
)
},
...
...
@@ -211,11 +211,11 @@ const makeRadialChart = (data, months, years) => {
];
// add second legend
rad
ial
Chart
.
legend
=
new
am4charts
.
Legend
();
rad
ial
Chart
.
legend
.
data
=
legenddata
;
rad
ial
Chart
.
legend
.
fontSize
=
10
;
rad
ial
Chart
.
legend
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
let
markerTemplate
=
rad
ial
Chart
.
legend
.
markers
.
template
;
rad
ar
Chart
.
legend
=
new
am4charts
.
Legend
();
rad
ar
Chart
.
legend
.
data
=
legenddata
;
rad
ar
Chart
.
legend
.
fontSize
=
10
;
rad
ar
Chart
.
legend
.
labels
.
template
.
fill
=
am4core
.
color
(
mainTextColor
);
let
markerTemplate
=
rad
ar
Chart
.
legend
.
markers
.
template
;
markerTemplate
.
width
=
20
;
markerTemplate
.
height
=
20
;
}
...
...
public/js/cuerpos_functions.js
View file @
89c8ea79
...
...
@@ -5,7 +5,7 @@
* January 2021
*/
/* globals omnivore, Promise, makeBaseMap, am4core, makeRad
ial
Chart, makeLineChart, makeAreaChart, makeBarChart */
/* globals omnivore, Promise, makeBaseMap, am4core, makeRad
ar
Chart, makeLineChart, makeAreaChart, makeBarChart */
/* exported. userFiles, userDates, timeParse, layerControl, yearList */
let
timeParse
,
...
...
@@ -65,10 +65,11 @@ const sortInitialDateAscending = (a, b) => {
return
a
-
b
;
}
const
baseUrl
=
new
URL
(
`/data`
,
window
.
location
.
href
).
href
;
// query available dates on DB
const
setupDates
=
()
=>
{
return
new
Promise
(
resolve
=>
{
const
baseUrl
=
new
URL
(
`/data`
,
window
.
location
.
href
).
href
;
let
layersQuery
=
`
${
baseUrl
}
/list_layers`
;
d3
.
json
(
layersQuery
).
then
(
layers
=>
{
layers
.
forEach
(
layer
=>
{
...
...
@@ -179,6 +180,7 @@ const populateDates = (dates) => { // fill out date pickers with available dates
}
else
{
// FIXME: no need to pass data here?
updateMap
({
map
:
map
,
min
:
startUserDate
,
max
:
endUserDate
});
updateCharts
();
}
},
beforeShow
:
(
el
,
inst
)
=>
{
...
...
@@ -317,7 +319,6 @@ const populateMap = async (mapData) => {
// after mapboxGL map is ready with styles do this:
glmap
.
getMapboxMap
().
on
(
"style.load"
,
()
=>
{
const
baseUrl
=
new
URL
(
`/data`
,
window
.
location
.
href
).
href
;
glmap
.
getMapboxMap
().
addSource
(
"basecuerposagua"
,
{
type
:
"vector"
,
//tiles: [`${baseUrl}/basecuerposagua/mvt/{z}/{x}/{y}?geom_column=geom&columns=${indicators.join()},descrip`],
...
...
@@ -426,7 +427,7 @@ const updateMap = (mapData) => {
// define MVT layer for given month table and all indicators
const
mapboxLayer
=
(
monthYear
)
=>
{
const
baseUrl
=
new
URL
(
`/data`
,
window
.
location
.
href
).
href
;
let
pbfLayer
=
{
id
:
monthYear
,
source
:
{
...
...
@@ -572,19 +573,20 @@ legend.onAdd = () => {
return
div
;
};
let
barChartData
=
[],
let
radarData
=
[],
barChartData
=
[],
yearList
;
const
makeCharts
=
()
=>
{
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
allYears
=
[...
new
Set
(
dateArray
.
map
(
date
=>
new
Date
(
date
).
getFullYear
()
))];
//let formatter = new Intl.DateTimeFormat('es', { month: 'long' });
// RAD
IAL
CHART
// RAD
AR
CHART
// get am chart - may need this to update chart on date change
//let rad
ial
Chart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "radial_chart");
//let rad
ar
Chart = am4core.registry.baseSprites.find(c => c.htmlContainer.id === "radial_chart");
// async queries for each date that has been loaded
const
queries
=
userFiles
.
map
(
async
(
mes
,
i
)
=>
{
...
...
@@ -592,25 +594,26 @@ const makeCharts = () => {
let
query
=
`
${
baseUrl
}
/query/
${
mes
}
?columns=sum(areacpo)/10000 area`
;
const
queryData
=
await
d3
.
json
(
query
);
queryData
[
0
].
date
=
new
Date
(
userDates
[
i
]);
radarData
.
push
(
queryData
[
0
]);
return
new
Promise
(
resolve
=>
{
resolve
(
queryData
[
0
]);
});
});
// wait for all queries to complete and then set chart data
Promise
.
all
(
queries
).
then
(
data
=>
{
radarData
.
sort
((
a
,
b
)
=>
(
a
.
date
>
b
.
date
)
?
1
:
-
1
);
// TODO: maybe make this a function so it can be used to update chart when changing dates
let
rad
ial
ChartData
=
[];
let
rad
ar
ChartData
=
[];
monthArray
.
forEach
(
(
month
,
index
)
=>
{
let
element
=
{};
yearList
.
forEach
(
year
=>
{
//element.month = formatter.format(new Date(year, month, 1, 0, 0, 0));
element
.
month
=
month
.
toLowerCase
();
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
;
});
rad
ial
ChartData
.
push
(
element
);
rad
ar
ChartData
.
push
(
element
);
});
makeRad
ialChart
(
radialChartData
,
monthArray
,
yearList
);
makeRad
arChart
(
radarChartData
,
monthArray
,
allYears
);
});
// LINE + STACKED AREA CHARTS
...
...
@@ -707,3 +710,49 @@ const updateBarChart = (filterDate, data) => {
popBarsChart
.
invalidateRawData
();
}
}
const
updateCharts
=
()
=>
{
yearList
=
[...
new
Set
(
userDates
.
map
(
date
=>
new
Date
(
date
).
getFullYear
()
))];
// update radar chart
let
radarChart
=
am4core
.
registry
.
baseSprites
.
find
(
c
=>
c
.
htmlContainer
.
id
===
"radial_chart"
);
const
queries
=
userDates
.
map
(
async
(
date
,
i
)
=>
{
let
checkDate
=
new
Date
(
date
);
// check what month has not been queried before and query it
if
(
!
radarData
.
some
(
d
=>
d
.
date
.
valueOf
()
===
checkDate
.
valueOf
()))
{
let
query
=
`
${
baseUrl
}
/query/
${
userFiles
[
i
]}
?columns=sum(areacpo)/10000 area`
;
const
queryData
=
await
d3
.
json
(
query
);
queryData
[
0
].
date
=
new
Date
(
userDates
[
i
]);
radarData
.
push
(
queryData
[
0
]);
return
new
Promise
(
resolve
=>
{
resolve
(
queryData
[
0
]);
});
}
})
// wait for all queries to complete and then set chart data
Promise
.
all
(
queries
).
then
(
()
=>
{
// TODO: maybe make this a function so it can be used to update chart when changing dates
let
radarChartData
=
[];
// check what months should have data
radarData
.
sort
((
a
,
b
)
=>
(
a
.
date
>
b
.
date
)
?
1
:
-
1
);
let
booleanCheck
=
radarData
.
map
(
(
d
,
i
)
=>
d
.
date
.
valueOf
()
==
userDates
[
i
]);
let
newRadarData
=
radarData
.
map
(
(
d
,
i
)
=>
{
if
(
booleanCheck
[
i
])
return
d
;
});
let
filtredRadarData
=
newRadarData
.
filter
(
d
=>
d
!=
undefined
);
monthArray
.
forEach
(
(
month
,
index
)
=>
{
let
element
=
{};
yearList
.
forEach
(
year
=>
{
element
.
month
=
month
.
toLowerCase
();
let
yearData
=
filtredRadarData
.
filter
(
d
=>
new
Date
(
d
.
date
).
getMonth
()
==
index
&&
new
Date
(
d
.
date
).
getFullYear
()
==
year
);
element
[
year
]
=
yearData
.
length
>
0
?
yearData
[
0
].
area
:
null
;
});
radarChartData
.
push
(
element
);
});
radarChart
.
data
=
radarChartData
;
radarChart
.
invalidateRawData
();
});
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment