-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from openearth/feat/graph-generic-update-ux
Update graph display and data loading to allow ux updates
- Loading branch information
Showing
11 changed files
with
229 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,81 @@ | ||
<template> | ||
<div ref="chartContainer" style="width: 100%; height: 100%"></div> | ||
<v-chart :option="option" autoresize /> | ||
</template> | ||
|
||
<script> | ||
import { nextTick } from "vue"; | ||
import * as echarts from "echarts"; | ||
<script setup> | ||
import { use } from "echarts/core"; | ||
import { CanvasRenderer } from "echarts/renderers"; | ||
import { BarChart } from "echarts/charts"; | ||
import { | ||
LegendComponent, | ||
TitleComponent, | ||
TooltipComponent, | ||
} from "echarts/components"; | ||
import { computed } from "vue"; | ||
import VChart from "vue-echarts"; | ||
export default { | ||
props: { | ||
graphData: { | ||
type: Object, | ||
required: true, | ||
}, | ||
}, | ||
data: function () { | ||
return { | ||
colors: ["#000000", "#173c66", "#f79320", "#951b1e"], // Standard colors | ||
}; | ||
use([ | ||
CanvasRenderer, | ||
BarChart, | ||
TitleComponent, | ||
TooltipComponent, | ||
LegendComponent, | ||
]); | ||
const props = defineProps({ | ||
graphData: { | ||
type: Object, | ||
required: true, | ||
}, | ||
methods: { | ||
generateSeries() { | ||
const series = []; | ||
// Loop through each scenario in the data and generate the series | ||
this.graphData.scenarios.forEach((scenario, index) => { | ||
const color = this.colors[index % this.colors.length]; // Use standard colors in a cycle | ||
}); | ||
// Add low data as a transparent placeholder (baseline) | ||
series.push({ | ||
name: `Low ${scenario.name}`, | ||
type: "bar", | ||
stack: scenario.name, | ||
itemStyle: { | ||
borderColor: "transparent", | ||
color: "transparent", | ||
}, | ||
emphasis: { | ||
itemStyle: { | ||
borderColor: "transparent", | ||
color: "transparent", | ||
}, | ||
}, | ||
data: scenario.msl_l.values, | ||
animation: false, | ||
silent: true, | ||
barWidth: 3, | ||
}); | ||
// Add medium sea level rise data | ||
series.push({ | ||
name: `Medium ${scenario.name}`, | ||
type: "bar", | ||
stack: scenario.name, | ||
color: color, // Use one of the standard colors | ||
itemStyle: { | ||
borderWidth: 0.2, | ||
borderColor: "#000000", | ||
}, | ||
data: scenario.msl_m.values, | ||
animation: false, | ||
silent: true, | ||
barWidth: 3, | ||
}); | ||
// Add high sea level rise data (top) | ||
series.push({ | ||
name: `High ${scenario.name}`, | ||
type: "bar", | ||
stack: scenario.name, | ||
color: color, // Use the same color as medium | ||
itemStyle: { | ||
borderWidth: 0.2, | ||
borderColor: "#000000", | ||
}, | ||
data: scenario.msl_h.values, | ||
animation: false, | ||
silent: true, | ||
barWidth: 3, | ||
}); | ||
const option = computed(() => ({ | ||
title: { | ||
text: "Sea Level Rise", | ||
left: "center", | ||
}, | ||
xAxis: { | ||
type: "category", | ||
data: props.graphData.time, // Use time for x-axis | ||
name: "Year", | ||
nameLocation: "middle", | ||
nameGap: 25, | ||
}, | ||
yAxis: { | ||
type: "value", | ||
name: "Sea level rise [m]", | ||
nameLocation: "middle", | ||
nameGap: 30, | ||
}, | ||
tooltip: { | ||
trigger: "axis", | ||
confine: false, | ||
formatter: function (params) { | ||
const formatValue = (value) => parseFloat(value).toFixed(2); | ||
let tooltip = params[0].axisValue.bold() + "<br/>"; | ||
params.forEach((param) => { | ||
tooltip += `${param.seriesName} : ${formatValue(param.value)} m<br/>`; | ||
}); | ||
// Move the first three items to the end | ||
// TODO: Etienne should provide them in the right order instead | ||
const firstThreeItems = series.splice(0, 3); | ||
series.push(...firstThreeItems); | ||
return series; | ||
return tooltip; | ||
}, | ||
renderChart() { | ||
const chartDom = this.$refs.chartContainer; | ||
if (chartDom && chartDom.clientWidth && chartDom.clientHeight) { | ||
const myChart = echarts.init(chartDom); | ||
const option = { | ||
title: { | ||
text: "Sea Level Rise", | ||
left: "center", | ||
}, | ||
xAxis: { | ||
type: "category", | ||
data: this.graphData.time, // Use time for x-axis | ||
name: "Year", | ||
nameLocation: "middle", | ||
nameGap: 25, | ||
}, | ||
yAxis: { | ||
type: "value", | ||
name: "Sea level rise [m]", | ||
nameLocation: "middle", | ||
nameGap: 30, | ||
}, | ||
tooltip: { | ||
trigger: "axis", | ||
confine: false, | ||
formatter: function (params) { | ||
const formatValue = (value) => parseFloat(value).toFixed(2); | ||
let tooltip = params[0].axisValue.bold() + "<br/>"; | ||
params.forEach((param) => { | ||
tooltip += `${param.seriesName} : ${formatValue( | ||
param.value | ||
)} m<br/>`; | ||
}); | ||
return tooltip; | ||
}, | ||
}, | ||
legend: { | ||
data: this.graphData.scenarios.map( | ||
(scenario) => `${scenario.name}` | ||
), | ||
selectedMode: false, | ||
orient: "horizontal", | ||
bottom: 0, | ||
itemStyle: { | ||
borderWidth: 0.5, | ||
}, | ||
}, | ||
grid: { | ||
top: "15%", | ||
left: "5%", | ||
right: "5%", | ||
bottom: "15%", | ||
containLabel: true, | ||
}, | ||
series: this.generateSeries(), | ||
}; | ||
myChart.setOption(option); | ||
} | ||
}, | ||
legend: { | ||
data: props.graphData.scenarios, | ||
selectedMode: false, | ||
orient: "horizontal", | ||
bottom: 0, | ||
itemStyle: { | ||
borderWidth: 0.5, | ||
}, | ||
}, | ||
mounted() { | ||
if (this.graphData && this.graphData.time) { | ||
nextTick(() => { | ||
this.renderChart(); | ||
}); | ||
window.addEventListener("resize", this.renderChart); | ||
} else { | ||
console.error("No valid sea level rise data provided."); | ||
} | ||
grid: { | ||
top: "15%", | ||
left: "5%", | ||
right: "5%", | ||
bottom: "15%", | ||
containLabel: true, | ||
}, | ||
}; | ||
series: props.graphData.values, | ||
})); | ||
</script> | ||
|
||
<style></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.