Skip to content

Commit c393278

Browse files
committed
Add color gradient by month to points
1 parent 36c8b08 commit c393278

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

frontend/src/components/CorrelationGraphs/TemperatureNdviScatter.vue

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
<script>
1414
import axios from 'axios'
1515
import Plotly from 'plotly.js-dist-min'
16-
import { onMounted, ref, render } from 'vue'
16+
import { onMounted, ref } from 'vue'
1717
1818
export default {
1919
name: 'TemperatureNdviScatter',
2020
setup() {
2121
const temperatureData = ref(null)
2222
const ndviData = ref(null)
2323
const startDate = ref(1514761200) // 2018-01-01
24-
const endDate = ref(1704063599) // 2023-12-31
24+
const endDate = ref(1733007599) // 2024-11-30
2525
const temporalResolution = ref("Monthly") // options: "Daily", "Monthly"
2626
const aggregation = ref("Mean") // options: "Mean", "Median", "Max", "Min"
2727
@@ -63,59 +63,59 @@
6363
console.error("Error fetching NDVI data:", error)
6464
}
6565
}
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-
}
7966
8067
const renderPlot = () => {
8168
if (temperatureData.value && ndviData.value) {
8269
const tempValues = temperatureData.value.data.map(entry => entry.value)
8370
const ndviValues = ndviData.value.data.map(entry => entry.value)
8471
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+
})
8889
8990
const scatterTrace = {
9091
x: tempValues,
9192
y: ndviValues,
9293
mode: 'markers',
93-
marker: { color: 'green' },
94+
marker: {
95+
color: monthColors,
96+
size: 8,
97+
line: {
98+
color: 'black',
99+
width: 1,
100+
},
101+
},
94102
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+
};
106107
107108
const layout = {
108109
title: 'Scatter Plot of Temperature vs. NDVI',
109110
xaxis: { title: 'Temperature (°C)' },
110111
yaxis: { title: 'NDVI' },
111112
template: 'plotly_white',
112-
legend: { x: 1.02, y: 0.5 },
113-
}
113+
};
114114
115-
Plotly.newPlot('plotlyScatterTemperatureNdvi', [scatterTrace, trendLineTrace], layout)
115+
Plotly.newPlot('plotlyScatterTemperatureNdvi', [scatterTrace], layout)
116116
}
117117
}
118-
118+
119119
onMounted(() => {
120120
fetchTemperatureData()
121121
fetchNdviData()

0 commit comments

Comments
 (0)