1111</template >
1212
1313<script >
14- import axios from " axios" ;
15- import Plotly from " plotly.js-dist-min" ;
16- import { onMounted , ref , watch } from " vue" ;
14+ import axios from " axios"
15+ import Plotly from " plotly.js-dist-min"
16+ import { onMounted , ref , watch } from " vue"
1717
1818export default {
1919 name: " YearlyNdviTemperaturePlot" ,
2020 setup () {
2121 // Constants for location and data configuration
22- const location = ref (" TEMPELHOFER_FELD" );
23- const temporalResolution = ref (" MONTHLY" );
24- const aggregation = ref (" MEAN" );
25- const startDate = ref (1514764800 ); // 2018-01-01 as Unix timestamp
26- const endDate = ref (1733007599 ); // 2024-11-30 as Unix timestamp
27- const temperatureData = ref (null );
28- const ndviData = ref (null );
29- const plotContainer = ref (null ); // Reference to the container for the Plotly chart
22+ const location = ref (" TEMPELHOFER_FELD" )
23+ const temporalResolution = ref (" MONTHLY" )
24+ const aggregation = ref (" MEAN" )
25+ const startDate = ref (1514764800 ) // 2018-01-01 as Unix timestamp
26+ const endDate = ref (1733007599 ) // 2024-11-30 as Unix timestamp
27+ const temperatureData = ref (null )
28+ const ndviData = ref (null )
29+ const plotContainer = ref (null ) // Reference to the container for the Plotly chart
3030
3131 const fetchTemperatureData = async () => {
32- const apiUrl = " https://thf-climate-run-1020174331409.europe-west3.run.app/weather/index" ;
32+ const apiUrl =
33+ " https://thf-climate-run-1020174331409.europe-west3.run.app/weather/index"
3334
3435 // Query parameters for the temperature API
3536 const params = {
@@ -39,19 +40,19 @@ export default {
3940 location: location .value ,
4041 temporalResolution: temporalResolution .value ,
4142 aggregation: aggregation .value ,
42- };
43+ }
4344
4445 try {
45- const response = await axios .get (apiUrl, { params });
46- temperatureData .value = response .data ; // Store the response in a reactive variable
46+ const response = await axios .get (apiUrl, { params })
47+ temperatureData .value = response .data // Store the response in a reactive variable
4748 } catch (error) {
48- console .error (" Error fetching temperature data:" , error);
49+ console .error (" Error fetching temperature data:" , error)
4950 }
50- };
51+ }
5152
52-
5353 const fetchNdviData = async () => {
54- const apiUrl = " https://thf-climate-run-1020174331409.europe-west3.run.app/index/ndvi" ;
54+ const apiUrl =
55+ " https://thf-climate-run-1020174331409.europe-west3.run.app/index/ndvi"
5556
5657 // Query parameters for the NDVI API
5758 const params = {
@@ -60,100 +61,97 @@ export default {
6061 location: location .value ,
6162 temporalResolution: temporalResolution .value ,
6263 aggregation: aggregation .value ,
63- };
64+ }
6465
6566 try {
66- const response = await axios .get (apiUrl, { params });
67- ndviData .value = response .data ; // Store the response in a reactive variable
67+ const response = await axios .get (apiUrl, { params })
68+ ndviData .value = response .data // Store the response in a reactive variable
6869 } catch (error) {
69- console .error (" Error fetching NDVI data:" , error);
70+ console .error (" Error fetching NDVI data:" , error)
7071 }
71- };
72+ }
7273
73-
74- // Calculates yearly averages from time-series data.
74+ // Calculates yearly averages from time-series data.
7575 const calculateYearlyAverages = (data ) => {
76- const yearlyData = {};
76+ const yearlyData = {}
7777
7878 // Group data by year, summing values and counting occurrences
7979 data .forEach (({ timestamp, value }) => {
80- const year = new Date (timestamp * 1000 ).getFullYear (); // Convert Unix timestamp to year
81- if (! yearlyData[year]) yearlyData[year] = { sum: 0 , count: 0 };
82- yearlyData[year].sum += value;
83- yearlyData[year].count += 1 ;
84- });
80+ const year = new Date (timestamp * 1000 ).getFullYear () // Convert Unix timestamp to year
81+ if (! yearlyData[year]) yearlyData[year] = { sum: 0 , count: 0 }
82+ yearlyData[year].sum += value
83+ yearlyData[year].count += 1
84+ })
8585
8686 // Calculate average for each year and return as an array
8787 return Object .entries (yearlyData).map (([year , { sum, count }]) => ({
8888 year: parseInt (year, 10 ),
8989 average: sum / count,
90- }));
91- };
90+ }))
91+ }
9292
93-
9493 const renderPlot = () => {
9594 if (temperatureData .value ? .data && ndviData .value ? .data ) {
9695 // Calculate yearly averages for temperature and NDVI
9796 const yearlyTemperature = calculateYearlyAverages (
98- temperatureData .value .data
99- );
100- const yearlyNdvi = calculateYearlyAverages (ndviData .value .data );
97+ temperatureData .value .data ,
98+ )
99+ const yearlyNdvi = calculateYearlyAverages (ndviData .value .data )
101100
102101 // Define trace for temperature data
103102 const tempTrace = {
104- x: yearlyTemperature .map ((e ) => e .year ),
105- y: yearlyTemperature .map ((e ) => e .average ),
103+ x: yearlyTemperature .map ((e ) => e .year ),
104+ y: yearlyTemperature .map ((e ) => e .average ),
106105 mode: " lines+markers" ,
107106 name: " Temperature (°C)" ,
108107 marker: { color: " red" , size: 7 , symbol: " square" },
109108 hoverinfo: " text" ,
110109 text: yearlyTemperature .map ((e ) => ` ${ e .average .toFixed (2 )} °C` ),
111- };
110+ }
112111
113112 // Define trace for NDVI data
114113 const ndviTrace = {
115- x: yearlyNdvi .map ((e ) => e .year ),
114+ x: yearlyNdvi .map ((e ) => e .year ),
116115 y: yearlyNdvi .map ((e ) => e .average ),
117116 mode: " lines+markers" ,
118117 name: " NDVI" ,
119118 line: { color: " blue" },
120119 yaxis: " y2" , // Use secondary y-axis for NDVI values
121120 hoverinfo: " text" ,
122121 text: yearlyNdvi .map ((e ) => ` ${ e .average .toFixed (2 )} ` ),
123- };
122+ }
124123
125124 // Layout configuration for the dual y-axis plot
126125 const layout = {
127126 title: " Yearly NDVI vs. Temperature (2018-2024)" ,
128127 xaxis: { title: " Year" , type: " category" },
129128 yaxis: { title: " Temperature (°C)" },
130129 yaxis2: { title: " NDVI" , overlaying: " y" , side: " right" },
131- template: " plotly_white" ,
132- };
130+ template: " plotly_white" ,
131+ }
133132
134133 // Render the chart using Plotly
135- Plotly .newPlot (plotContainer .value , [tempTrace, ndviTrace], layout);
134+ Plotly .newPlot (plotContainer .value , [tempTrace, ndviTrace], layout)
136135 }
137- };
136+ }
138137
139138 // Fetch data when the component is mounted
140139 onMounted (() => {
141- fetchTemperatureData ();
142- fetchNdviData ();
143- });
140+ fetchTemperatureData ()
141+ fetchNdviData ()
142+ })
144143
145144 // Watch for updates to temperatureData and ndviData, and render the chart
146145 watch ([temperatureData, ndviData], ([temp , ndvi ]) => {
147- if (temp && ndvi) renderPlot ();
148- });
146+ if (temp && ndvi) renderPlot ()
147+ })
149148
150- return { plotContainer }; // Return the reference to the plot container
149+ return { plotContainer } // Return the reference to the plot container
151150 },
152- };
151+ }
153152< / script>
154153
155154< style scoped>
156-
157155.plot - container {
158156 width: 100 % ;
159157 height: 400px ;
0 commit comments