Skip to content

Commit 923dbed

Browse files
committed
Modified getActiveChart function to return the correct chart.
1 parent 981b71f commit 923dbed

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

frontend/src/components/LineChart.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ onMounted(async () => {
9292
await nextTick()
9393
9494
// push the chart to the store
95-
//chartStore.storeMountedChart(activeReachChart.value, xLabel, yLabel)
96-
chartStore.storeMountedChart(activeReachChart.value)
95+
chartStore.storeMountedChart(activeReachChart.value, xLabel, yLabel)
9796
9897
// force a re-render of the line charts
9998
chartStore.updateCurrentChart()

frontend/src/stores/charts.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,8 @@ export const useChartsStore = defineStore('charts', () => {
757757
}
758758

759759
const updateAllCharts = () => {
760+
// This function updates the styling for all stored charts
761+
760762
// iterate over stored charts and update the line visibility
761763
storedCharts.value.forEach((storedChart) => {
762764
if (storedChart.chart != null) {
@@ -777,15 +779,14 @@ export const useChartsStore = defineStore('charts', () => {
777779

778780
}
779781

780-
const storeMountedChart = (chart) => {
781-
//, xlabel, ylabel) => {
782-
783-
// add x and y labels to the chart object so we can look it up later
784-
// chart.xlabel = xlabel
785-
// chart.ylabel = ylabel
782+
const storeMountedChart = (chart, x, y) => {
783+
784+
chart.x = x
785+
chart.y = y
786786
storedCharts.value.push(chart)
787787

788-
// clean stored charts that are undifined
788+
console.log('Storing New Chart -> ', chart)
789+
// clean stored charts that are undefined
789790
cleanStoredCharts()
790791

791792
}
@@ -795,41 +796,53 @@ export const useChartsStore = defineStore('charts', () => {
795796

796797
}
797798

798-
const sortChartByX = (plt) => {
799+
const sortChartByX = (chart) => {
799800

800801
// get the chart data and sort it by the x-axis variable.
801802
// If the x-axis variable is time, sort by time otherwise
802803
// sort numerically.
803-
let plotData = getActiveChart().chart.data.datasets[0].data
804-
let xvar = plt.xvar.abbreviation
804+
let chartData = chart.data.datasets[0].data
805+
let xvar = activePlt.value.xvar.abbreviation
805806

806807
if (xvar == 'time_str') {
807-
return plotData.sort((a,b) => new Date(a.time_str) - new Date(b.time_str));
808+
return chartData.sort((a,b) => new Date(a.time_str) - new Date(b.time_str));
808809
}
809810
else {
810-
return plotData.sort((a,b) => parseFloat(a[xvar]) - parseFloat(b[xvar]));
811+
return chartData.sort((a,b) => parseFloat(a[xvar]) - parseFloat(b[xvar]));
811812
}
812813
}
813814

814815
const getActiveChart = () =>
815816
{
816-
if (chartTab.value == 'timeseries')
817-
{
818-
return activeReachChart.value
819-
} else
820-
{
821-
return activeNodeChart.value
817+
// Retrieves the currently active chart from the list of
818+
// stored charts using its x and y labels
819+
820+
// return null if no charts exist in the storedCharts object. This
821+
// can happen on the initial page load.
822+
if (storedCharts.value.length == 0) {
823+
return null
822824
}
825+
826+
// get the x and y labels for the currenty active chart
827+
let xlabel = activePlt.value.xvar.name
828+
let ylabel = activePlt.value.yvar.name
829+
830+
// add units to the labels, if they exist
831+
if (activePlt.value.xvar.unit != null) { xlabel += ' (' + activePlt.value.xvar.unit + ')'}
832+
if (activePlt.value.yvar.unit != null) { ylabel += ' (' + activePlt.value.yvar.unit + ')'}
833+
834+
// return the stored chart that matches the current x and y labels
835+
return storedCharts.value.filter(c => (c.x == xlabel && c.y == ylabel) )[0]
823836
}
824837

825838
const updateCurrentChart = () => {
839+
840+
// get the currently active chart from the chartStore.
826841
let storedChart = getActiveChart()
827842
if (storedChart == null) {
828-
console.log('No chart to update')
829843
return
830844
}
831845

832-
console.log('storedChart', storedChart)
833846
try {
834847
// check if the chart is a node chart or a reach chart
835848
// and refresh the data accordingly
@@ -848,7 +861,9 @@ const updateCurrentChart = () => {
848861
})
849862

850863
// sort the chart data by the x-axis variable
851-
storedChart.chart.data.datasets[0].data = sortChartByX(activePlt.value)
864+
storedChart.chart.data.datasets[0].data = sortChartByX(storedChart.chart)
865+
866+
storedChart.chart.update()
852867
}
853868

854869
return {

frontend/src/views/TimeSeriesCharts.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const changePlot = (plt) => {
9595
// controls (e.g. quality) are changed later.
9696
activePlt.value = plt
9797
98+
router.push({ query: { ...router.currentRoute.value.query, variables: plt.abbreviation } })
9899
//chartData.value.datasets[0].data = chartStore.sortChartByX(plt)
99100
100101
// TODO: save this plt as the active plot so we can refresh the data in the chart.js class later.
@@ -103,7 +104,6 @@ const changePlot = (plt) => {
103104
chartStore.updateCurrentChart()
104105
// chartStore.updateShowLine()
105106
106-
router.push({ query: { ...router.currentRoute.value.query, variables: plt.abbreviation } })
107107
108108
}
109109

0 commit comments

Comments
 (0)