|
13 | 13 | <script> |
14 | 14 | import axios from 'axios' |
15 | 15 | import Plotly from 'plotly.js-dist-min' |
16 | | - import { onMounted, ref, render } from 'vue' |
| 16 | + import { onMounted, ref } from 'vue' |
17 | 17 | |
18 | 18 | export default { |
19 | 19 | name: 'TemperatureNdviScatter', |
20 | 20 | setup() { |
21 | 21 | const temperatureData = ref(null) |
22 | 22 | const ndviData = ref(null) |
23 | 23 | const startDate = ref(1514761200) // 2018-01-01 |
24 | | - const endDate = ref(1704063599) // 2023-12-31 |
| 24 | + const endDate = ref(1733007599) // 2024-11-30 |
25 | 25 | const temporalResolution = ref("Monthly") // options: "Daily", "Monthly" |
26 | 26 | const aggregation = ref("Mean") // options: "Mean", "Median", "Max", "Min" |
27 | 27 | |
|
63 | 63 | console.error("Error fetching NDVI data:", error) |
64 | 64 | } |
65 | 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 | 66 | |
80 | 67 | const renderPlot = () => { |
81 | 68 | if (temperatureData.value && ndviData.value) { |
82 | 69 | const tempValues = temperatureData.value.data.map(entry => entry.value) |
83 | 70 | const ndviValues = ndviData.value.data.map(entry => entry.value) |
84 | 71 |
|
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)}`; |
| 72 | + const timestamps = temperatureData.value.data.map(entry => entry.timestamp); |
| 73 | + const monthsAndYears = timestamps.map(ts => { |
| 74 | + const date = new Date(ts * 1000) |
| 75 | + const month = date.toLocaleString('default', { month: 'long' }) |
| 76 | + const year = date.getFullYear() |
| 77 | + return { month, year } |
| 78 | + }) |
| 79 | +
|
| 80 | + // Map months to a circular grayscale gradient |
| 81 | + const months = monthsAndYears.map(({ month }) => new Date(Date.parse(month + " 1")).getMonth()) |
| 82 | + const monthColors = months.map(month => { |
| 83 | + // Convert month index (0-11) to circular position |
| 84 | + const angle = (month / 12) * 2 * Math.PI // 0-2π range |
| 85 | + // Use cosine to create smooth gradient: July (π) = 0 (black), January (0) = 1 (white) |
| 86 | + const intensity = Math.round((Math.cos(angle) + 1) / 2 * 255) // Scale cosine to 0-255 |
| 87 | + return `rgb(${intensity}, ${intensity}, ${intensity})` |
| 88 | + }) |
88 | 89 |
|
89 | 90 | const scatterTrace = { |
90 | 91 | x: tempValues, |
91 | 92 | y: ndviValues, |
92 | 93 | mode: 'markers', |
93 | | - marker: { color: 'green' }, |
| 94 | + marker: { |
| 95 | + color: monthColors, |
| 96 | + size: 8, |
| 97 | + line: { |
| 98 | + color: 'black', |
| 99 | + width: 1, |
| 100 | + }, |
| 101 | + }, |
94 | 102 | 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 | | - } |
| 103 | + name: 'Temperature vs. NDVI', |
| 104 | + hovertemplate: '%{x}°C<br>NDVI: %{y}<br>%{customdata.month} %{customdata.year}<extra></extra>', |
| 105 | + customdata: monthsAndYears, |
| 106 | + }; |
106 | 107 |
|
107 | 108 | const layout = { |
108 | 109 | title: 'Scatter Plot of Temperature vs. NDVI', |
109 | 110 | xaxis: { title: 'Temperature (°C)' }, |
110 | 111 | yaxis: { title: 'NDVI' }, |
111 | 112 | template: 'plotly_white', |
112 | | - legend: { x: 1.02, y: 0.5 }, |
113 | | - } |
| 113 | + }; |
114 | 114 |
|
115 | | - Plotly.newPlot('plotlyScatterTemperatureNdvi', [scatterTrace, trendLineTrace], layout) |
| 115 | + Plotly.newPlot('plotlyScatterTemperatureNdvi', [scatterTrace], layout) |
116 | 116 | } |
117 | 117 | } |
118 | | - |
| 118 | +
|
119 | 119 | onMounted(() => { |
120 | 120 | fetchTemperatureData() |
121 | 121 | fetchNdviData() |
|
0 commit comments