|
| 1 | +<template> |
| 2 | +<v-container> |
| 3 | + <h2 class="pb-10">Temperature vs. NDVI</h2> |
| 4 | + <v-row justify="center"> |
| 5 | + <div |
| 6 | + id="plotlyGraphTemperatureNdvi" |
| 7 | + style="width: 100%; height: 400px" |
| 8 | + class="d-flex justify-center" |
| 9 | + ></div> |
| 10 | + </v-row> |
| 11 | +</v-container> |
| 12 | +</template> |
| 13 | + |
| 14 | +<script> |
| 15 | +import axios from 'axios' |
| 16 | +import Plotly from 'plotly.js-dist-min' |
| 17 | +import { onMounted, ref, render } from 'vue' |
| 18 | +
|
| 19 | +export default { |
| 20 | + name: 'TemperatureNdviBarAndLine', |
| 21 | + setup() { |
| 22 | + const temperatureData = ref(null) |
| 23 | + const ndviData = ref(null) |
| 24 | + const startDate = ref(1514761200) // 2018-01-01 |
| 25 | + const endDate = ref(1704063599) // 2023-12-31 |
| 26 | + const temporalResolution = ref("Monthly") |
| 27 | + const aggregation = ref("Mean") |
| 28 | +
|
| 29 | + const fetchTemperatureData = async () => { |
| 30 | + const apiUrl = 'http://localhost:8000/weather/index' |
| 31 | + const params = { |
| 32 | + weatherVariable: "temperature_2m", |
| 33 | + startDate: startDate.value, |
| 34 | + endDate: endDate.value, |
| 35 | + location: "TEMPELHOFER_FELD", |
| 36 | + temporalResolution: temporalResolution.value.toUpperCase(), |
| 37 | + aggregation: aggregation.value.toUpperCase() |
| 38 | + } |
| 39 | +
|
| 40 | + try { |
| 41 | + const response = await axios.get(apiUrl, { params }) |
| 42 | + temperatureData.value = response.data |
| 43 | + renderPlot() |
| 44 | + } catch (error) { |
| 45 | + console.error("Error fetching temperature data:", error) |
| 46 | + } |
| 47 | + } |
| 48 | +
|
| 49 | + const fetchNdviData = async () => { |
| 50 | + const apiUrl = 'http://localhost:8000/index/ndvi' |
| 51 | + const params = { |
| 52 | + startDate: startDate.value, |
| 53 | + endDate: endDate.value, |
| 54 | + location: "TEMPELHOFER_FELD", |
| 55 | + temporalResolution: temporalResolution.value.toUpperCase(), |
| 56 | + aggregation: aggregation.value.toUpperCase() |
| 57 | + } |
| 58 | +
|
| 59 | + try { |
| 60 | + const response = await axios.get(apiUrl, { params }) |
| 61 | + ndviData.value = response.data |
| 62 | + renderPlot() |
| 63 | + } catch (error) { |
| 64 | + console.error("Error fetching NDVI data:", error) |
| 65 | + } |
| 66 | + } |
| 67 | +
|
| 68 | + const renderPlot = () => { |
| 69 | + if (temperatureData.value && ndviData.value) { |
| 70 | + const tempTimestamps = temperatureData.value.data.map(entry => |
| 71 | + new Date(entry.timestamp * 1000).toISOString().split('T')[0] |
| 72 | + ) |
| 73 | + const tempValues = temperatureData.value.data.map(entry => entry.value) |
| 74 | + const ndviTimestamps = ndviData.value.data.map(entry => new Date(entry.timestamp * 1000)) |
| 75 | + const ndviValues = ndviData.value.data.map(entry => entry.value) |
| 76 | +
|
| 77 | + const tempTrace = { |
| 78 | + x: tempTimestamps, |
| 79 | + y: tempValues, |
| 80 | + type: 'bar', |
| 81 | + name: 'Temperature (°C)', |
| 82 | + marker: { color: 'red' } |
| 83 | + }; |
| 84 | +
|
| 85 | + const ndviTrace = { |
| 86 | + x: ndviTimestamps, |
| 87 | + y: ndviValues, |
| 88 | + mode: 'lines+markers', |
| 89 | + name: 'NDVI', |
| 90 | + line: { color: 'blue' }, |
| 91 | + yaxis: 'y2', |
| 92 | + }; |
| 93 | +
|
| 94 | + const layout = { |
| 95 | + title: 'NDVI vs. Monthly Temperature for Tempelhofer Feld (2018-2023)', |
| 96 | + xaxis: { title: 'Date', type: 'date', rangeslider: { visible: true } }, |
| 97 | + yaxis: { title: 'NDVI' }, |
| 98 | + yaxis2: { |
| 99 | + title: 'Temperature (°C)', |
| 100 | + overlaying: 'y', |
| 101 | + side: 'right', |
| 102 | + }, |
| 103 | + legend: { x: 1.1, y: 0.5 }, |
| 104 | + template: 'plotly_white' |
| 105 | + }; |
| 106 | +
|
| 107 | + Plotly.newPlot('plotlyGraphTemperatureNdvi', [tempTrace, ndviTrace], layout); |
| 108 | + } |
| 109 | + } |
| 110 | +
|
| 111 | + onMounted(() => { |
| 112 | + fetchTemperatureData() |
| 113 | + fetchNdviData() |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | +</script> |
| 118 | + |
| 119 | +<style scoped></style> |
0 commit comments