1+ <template >
2+ <v-container >
3+ <v-row justify =" center" class =" pb-16" >
4+ <div
5+ id =" plotlyScatterTemperatureNdvi"
6+ style =" width : 100% ; height : 400px "
7+ class =" d-flex justify-center"
8+ ></div >
9+ </v-row >
10+ </v-container >
11+ </template >
12+
13+ <script >
14+ import axios from ' axios'
15+ import Plotly from ' plotly.js-dist-min'
16+ import { onMounted , ref , render } from ' vue'
17+
18+ export default {
19+ name: ' TemperatureNdviScatter' ,
20+ setup () {
21+ const temperatureData = ref (null )
22+ const ndviData = ref (null )
23+ const startDate = ref (1514761200 ) // 2018-01-01
24+ const endDate = ref (1704063599 ) // 2023-12-31
25+ const temporalResolution = ref (" Monthly" ) // options: "Daily", "Monthly"
26+ const aggregation = ref (" Mean" ) // options: "Mean", "Median", "Max", "Min"
27+
28+ const fetchTemperatureData = async () => {
29+ const apiUrl = ' https://thf-climate-run-1020174331409.europe-west3.run.app/weather/index'
30+ const params = {
31+ weatherVariable: " temperature_2m" ,
32+ startDate: startDate .value ,
33+ endDate: endDate .value ,
34+ location: " TEMPELHOFER_FELD" ,
35+ temporalResolution: temporalResolution .value .toUpperCase (),
36+ aggregation: aggregation .value .toUpperCase ()
37+ }
38+
39+ try {
40+ const response = await axios .get (apiUrl, { params })
41+ temperatureData .value = response .data
42+ renderPlot ()
43+ } catch (error) {
44+ console .error (" Error fetching temperature data:" , error)
45+ }
46+ }
47+
48+ const fetchNdviData = async () => {
49+ const apiUrl = ' https://thf-climate-run-1020174331409.europe-west3.run.app/index/ndvi'
50+ const params = {
51+ startDate: startDate .value ,
52+ endDate: endDate .value ,
53+ location: " TEMPELHOFER_FELD" ,
54+ temporalResolution: temporalResolution .value .toUpperCase (),
55+ aggregation: aggregation .value .toUpperCase ()
56+ }
57+
58+ try {
59+ const response = await axios .get (apiUrl, { params })
60+ ndviData .value = response .data
61+ renderPlot ()
62+ } catch (error) {
63+ console .error (" Error fetching NDVI data:" , error)
64+ }
65+ }
66+
67+ const calculateRegression = (xValues , yValues ) => {
68+ const n = xValues .length
69+ const sumX = xValues .reduce ((acc , val ) => acc + val, 0 )
70+ const sumY = yValues .reduce ((acc , val ) => acc + val, 0 )
71+ const sumXY = xValues .reduce ((acc , val , index ) => acc + val * yValues[index], 0 )
72+ const sumX2 = xValues .reduce ((acc , val ) => acc + val * val, 0 )
73+
74+ const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX)
75+ const intercept = (sumY - slope * sumX) / n
76+
77+ return { slope, intercept }
78+ }
79+
80+ const renderPlot = () => {
81+ if (temperatureData .value && ndviData .value ) {
82+ const tempValues = temperatureData .value .data .map (entry => entry .value )
83+ const ndviValues = ndviData .value .data .map (entry => entry .value )
84+
85+ const { slope , intercept } = calculateRegression (tempValues, ndviValues)
86+ const trendLineValues = tempValues .map (x => slope * x + intercept)
87+ const trendlineEquation = ` y = ${ slope .toFixed (2 )} x + ${ intercept .toFixed (2 )} ` ;
88+
89+ const scatterTrace = {
90+ x: tempValues,
91+ y: ndviValues,
92+ mode: ' markers' ,
93+ marker: { color: ' green' },
94+ type: ' scatter' ,
95+ name: ' Temperature vs NDVI'
96+ }
97+
98+ const trendLineTrace = {
99+ x: tempValues,
100+ y: trendLineValues,
101+ mode: ' lines' ,
102+ line: { color: ' grey' , dash: ' dash' },
103+ type: ' scatter' ,
104+ name: ` Trend Line: ${ trendlineEquation} `
105+ }
106+
107+ const layout = {
108+ title: ' Scatter Plot of Temperature vs. NDVI' ,
109+ xaxis: { title: ' Temperature (°C)' },
110+ yaxis: { title: ' NDVI' },
111+ template: ' plotly_white' ,
112+ legend: { x: 1.02 , y: 0.5 },
113+ }
114+
115+ Plotly .newPlot (' plotlyScatterTemperatureNdvi' , [scatterTrace, trendLineTrace], layout)
116+ }
117+ }
118+
119+ onMounted (() => {
120+ fetchTemperatureData ()
121+ fetchNdviData ()
122+ })
123+ }
124+ }
125+ </script >
126+
127+ <style scoped></style >
128+
0 commit comments