Skip to content

Commit 9de6156

Browse files
committed
Create monthly chart and overlay graph
1 parent 1719d80 commit 9de6156

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<template>
2+
<v-container>
3+
<h2 class="pb-10">NDVI Year Overlay (2018-2023)</h2>
4+
<v-row>
5+
<div
6+
id="plotlyGraphNdviOverlay"
7+
v-show="ndviData"
8+
style="width: 100%"
9+
class="d-flex justify-center"
10+
></div>
11+
</v-row>
12+
</v-container>
13+
</template>
14+
15+
<script>
16+
import { ref, nextTick, onMounted } from 'vue'
17+
import axios from 'axios'
18+
import Plotly from 'plotly.js-dist-min'
19+
20+
export default {
21+
name: 'NdviComparisonOverlay',
22+
setup() {
23+
const ndviData = ref(null)
24+
const startDate = ref(1514761200) // 2018-01-01
25+
const endDate = ref(1704063599) // 2023-12-31
26+
const years = [2018, 2019, 2020, 2021, 2022, 2023]
27+
28+
const fetchNdviData = async (params) => {
29+
const apiUrl = 'http://localhost:8000/index/ndvi'
30+
try {
31+
const response = await axios.get(apiUrl, { params })
32+
ndviData.value = response.data
33+
await nextTick()
34+
renderPlot()
35+
} catch (error) {
36+
console.error("Error fetching NDVI data:", error)
37+
}
38+
}
39+
40+
const renderPlot = () => {
41+
if (ndviData.value && ndviData.value.data) {
42+
const traces = years.map(year => {
43+
const filteredData = ndviData.value.data.filter(d => new Date(d.timestamp * 1000).getFullYear() === year)
44+
45+
const months = filteredData.map(d => new Date(d.timestamp * 1000).getMonth() + 1)
46+
const values = filteredData.map(d => d.value)
47+
48+
return {
49+
x: months,
50+
y: values,
51+
mode: 'lines+markers',
52+
type: 'scatter',
53+
name: `NDVI ${year}`,
54+
line: { width: 2 },
55+
}
56+
})
57+
58+
const layout = {
59+
title: 'NDVI of Tempelhofer Feld',
60+
xaxis: {
61+
title: '',
62+
tickmode: 'array',
63+
tickvals: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
64+
ticktext: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
65+
},
66+
yaxis: { title: 'NDVI Value' },
67+
}
68+
69+
Plotly.newPlot('plotlyGraphNdviOverlay', traces, layout)
70+
}
71+
}
72+
73+
onMounted(() => {
74+
const params = {
75+
startDate: startDate.value,
76+
endDate: endDate.value,
77+
location: "TEMPELHOFER_FELD",
78+
temporalResolution: "MONTHLY",
79+
aggregation: "MEAN",
80+
}
81+
fetchNdviData(params)
82+
})
83+
84+
return {
85+
ndviData,
86+
years,
87+
}
88+
}
89+
}
90+
</script>
91+
92+
<style scoped>
93+
h2 {
94+
text-align: center;
95+
}
96+
</style>
97+
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<template>
2+
<v-container>
3+
<h2 class="pb-10">NDVI Monthly Means (2018-2023)</h2>
4+
<v-row justify="center">
5+
<v-col class="py-0" style="max-width: 350px">
6+
<v-select
7+
v-model="month"
8+
:items="monthOptions"
9+
label="Month"
10+
variant="outlined"
11+
density="compact"
12+
required
13+
/>
14+
</v-col>
15+
</v-row>
16+
<v-row>
17+
<div
18+
id="plotlyGraphNdviMonthly"
19+
v-show="ndviData"
20+
style="width: 100%"
21+
class="d-flex justify-center"
22+
></div>
23+
</v-row>
24+
</v-container>
25+
</template>
26+
27+
<script>
28+
import { ref, nextTick, watch, onMounted } from 'vue'
29+
import axios from 'axios'
30+
import Plotly from 'plotly.js-dist-min'
31+
32+
export default {
33+
name: 'NdviSelectMonthGraph',
34+
setup() {
35+
const ndviData = ref(null)
36+
const month = ref("August")
37+
const monthOptions = [
38+
"January", "February", "March", "April",
39+
"May", "June", "July", "August",
40+
"September", "October", "November", "December"
41+
]
42+
const startDate = ref(1514761200) // 2018-01-01
43+
const endDate = ref(1704063599) // 2023-12-31
44+
45+
const fetchNdviData = async () => {
46+
const apiUrl = 'http://localhost:8000/index/ndvi'
47+
try {
48+
const response = await axios.get(apiUrl, {
49+
params: {
50+
startDate: startDate.value,
51+
endDate: endDate.value,
52+
location: "TEMPELHOFER_FELD",
53+
temporalResolution: "MONTHLY",
54+
aggregation: "MEAN"
55+
}
56+
})
57+
ndviData.value = response.data
58+
await nextTick()
59+
renderPlot()
60+
} catch (error) {
61+
console.error("Error fetching NDVI data:", error)
62+
}
63+
}
64+
65+
const renderPlot = () => {
66+
if (ndviData.value && ndviData.value.data) {
67+
const monthIndex = monthOptions.indexOf(month.value) + 1
68+
const filteredData = ndviData.value.data.filter(d => new Date(d.timestamp * 1000).getMonth() + 1 === monthIndex)
69+
70+
const years = filteredData.map(d => new Date(d.timestamp * 1000).getFullYear())
71+
const values = filteredData.map(d => d.value)
72+
73+
const trace = {
74+
x: years,
75+
y: values,
76+
mode: 'lines+markers',
77+
type: 'bar',
78+
name: '',
79+
marker: { color: 'green' }
80+
}
81+
82+
const layout = {
83+
title: `NDVI of Tempelhofer Feld (${month.value})`,
84+
xaxis: { title: '' },
85+
yaxis: { title: 'NDVI Value' }
86+
}
87+
88+
Plotly.newPlot('plotlyGraphNdviMonthly', [trace], layout)
89+
}
90+
}
91+
92+
const updateGraph = () => {
93+
if (ndviData.value) {
94+
renderPlot()
95+
}
96+
}
97+
98+
watch(month, updateGraph)
99+
100+
onMounted(fetchNdviData)
101+
102+
return {
103+
ndviData,
104+
monthOptions,
105+
month
106+
}
107+
}
108+
}
109+
</script>
110+
111+
<style scoped>
112+
h2 {
113+
text-align: center;
114+
}
115+
</style>

0 commit comments

Comments
 (0)