@@ -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
814815const 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
825838const 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 {
0 commit comments