11import { defineStore , storeToRefs } from 'pinia'
2- import { ref , watch } from 'vue'
2+ import { nextTick , ref , watch } from 'vue'
33import { useFeaturesStore } from '@/stores/features'
44import { NODE_DATETIME_VARIATION } from '@/constants'
55import { addMinutes , subMinutes } from 'date-fns'
@@ -22,7 +22,7 @@ export const useChartsStore = defineStore(
2222 const hasNodeData = ref ( false )
2323 const chartTab = ref ( 'timeseries' )
2424 const showStatistics = ref ( false )
25- const showLine = ref ( true )
25+ const symbology = ref ( [ 'Lines' , 'Markers' ] )
2626 const dataQualityFlags = ref ( [ 0 , 1 , 2 ] ) // by default don't show bad data
2727 let colorScale = chroma . scale ( 'YlGnBu' ) . mode ( 'lch' ) . colors ( 3 )
2828 const router = useRouter ( )
@@ -141,17 +141,29 @@ export const useChartsStore = defineStore(
141141 hasNodeData . value = false
142142 }
143143
144+ const symbologyContains = ( value ) => {
145+ // check if symbology.value is an array
146+ if ( Array . isArray ( symbology . value ) ) {
147+ return symbology . value . includes ( value )
148+ }
149+ return false
150+ }
151+
144152 const getLabels = ( selectedFeatures ) => {
145153 // TODO: for now we just use the first query
146154 // when compact = true, there will only be a single feature
147- const propertyObject = selectedFeatures [ 0 ] . queries [ 0 ] . results . geojson . features [ 0 ] . properties
148- const labels = propertyObject . time_str . map ( ( time_str ) => {
149- if ( time_str == 'no_data' ) {
150- return
151- }
152- return time_str
153- } )
154- return labels . filter ( ( l ) => l != undefined )
155+ try {
156+ const propertyObject = selectedFeatures [ 0 ] . queries [ 0 ] . results . geojson . features [ 0 ] . properties
157+ const labels = propertyObject . time_str . map ( ( time_str ) => {
158+ if ( time_str == 'no_data' ) {
159+ return
160+ }
161+ return time_str
162+ } )
163+ return labels . filter ( ( l ) => l != undefined )
164+ } catch ( error ) {
165+ console . error ( 'Error getting labels' , error )
166+ }
155167 }
156168
157169 const getTitle = ( ) => {
@@ -234,7 +246,9 @@ export const useChartsStore = defineStore(
234246 datasets . forEach ( ( dataset ) => {
235247 dataQualityFilterSingleDataset ( dataset )
236248 // update the line visibility
237- dataset . showLine = showLine . value
249+ dataset . showLine = symbologyContains ( 'Lines' )
250+ // https://www.chartjs.org/docs/latest/charts/line.html#dataset-properties
251+ // storedChart.chart.options.elements.point.pointstyle = false
238252 } )
239253 }
240254 }
@@ -257,7 +271,7 @@ export const useChartsStore = defineStore(
257271 dataset . pointBorderColor = getPointBorderColors ( dataset )
258272 }
259273
260- const filterDatasetsToTimeRange = ( start , end , tolerance ) => {
274+ const filterDatasetsToTimeRange = async ( start , end , tolerance ) => {
261275 // if end is null, use now
262276 const featureStore = useFeaturesStore ( )
263277 const { timeRange } = storeToRefs ( featureStore )
@@ -338,8 +352,9 @@ export const useChartsStore = defineStore(
338352 }
339353 } )
340354 }
355+ await nextTick ( )
341356 updateNodeDataSetStyles ( )
342- updateAllCharts ( )
357+ refreshAllCharts ( )
343358 }
344359
345360 const filterDatasetsBySetOfDates = ( datasets , selectedTimeseriesPoints , tolerance ) => {
@@ -667,7 +682,7 @@ export const useChartsStore = defineStore(
667682
668683 const getDataSetStyle = ( ) => {
669684 const style = {
670- showLine : showLine . value ,
685+ showLine : symbologyContains ( 'Lines' ) ,
671686 fill : true ,
672687 pointBorderColor : ( ctx ) => getPointBorderColors ( ctx . dataset ) ,
673688 pointStyle : ( ctx ) => getPointStyles ( ctx . dataset ) ,
@@ -711,7 +726,7 @@ export const useChartsStore = defineStore(
711726 const getNodeDataSetStyle = ( dataSet ) => {
712727 const colors = getDateGradientColors ( dataSet )
713728 return {
714- showLine : showLine . value ,
729+ showLine : symbologyContains ( 'Lines' ) ,
715730 pointRadius : 5 ,
716731 pointHoverRadius : 15 ,
717732 //fill: styles.dynamicColors,
@@ -745,17 +760,26 @@ export const useChartsStore = defineStore(
745760 }
746761 }
747762 } )
763+ updateSymbology ( )
748764 }
749765
750- const updateShowLine = ( ) => {
766+ const updateSymbology = ( ) => {
767+ let showLine = true
768+ let showMarkers = false
769+ // if symbology.value is an array of strings, check if it includes 'Lines' or 'Markers'
770+ if ( Array . isArray ( symbology . value ) ) {
771+ showLine = symbology . value . includes ( 'Lines' )
772+ showMarkers = symbology . value . includes ( 'Markers' )
773+ }
751774 // iterate over stored charts and update the line visibility
752775 storedCharts . value . forEach ( ( storedChart ) => {
753776 try {
754777 if ( storedChart . chart != null ) {
755778 storedChart . chart . data . datasets
756779 . filter ( ( ds ) => ds . seriesType != 'computed_series' )
757780 . forEach ( ( dataset ) => {
758- dataset . showLine = showLine . value
781+ dataset . showLine = showLine
782+ dataset . pointRadius = showMarkers ? 5 : 0
759783 } )
760784 storedChart . chart . update ( )
761785 }
@@ -765,7 +789,7 @@ export const useChartsStore = defineStore(
765789 } )
766790 }
767791
768- const updateAllCharts = ( ) => {
792+ const updateAllChartsData = ( ) => {
769793 // iterate over stored charts and update the line visibility
770794 storedCharts . value . forEach ( ( storedChart ) => {
771795 try {
@@ -776,6 +800,7 @@ export const useChartsStore = defineStore(
776800 } else {
777801 storedChart . chart . data . datasets = chartData . value . datasets
778802 }
803+ updateSymbology ( )
779804 storedChart . chart . update ( )
780805
781806 // if all datasets in the chart are hidden, alert the user!
@@ -798,6 +823,18 @@ export const useChartsStore = defineStore(
798823 } )
799824 }
800825
826+ const refreshAllCharts = async ( ) => {
827+ // iterate over stored charts and refresh
828+ storedCharts . value . forEach ( async ( storedChart ) => {
829+ try {
830+ await storedChart . chart . update ( )
831+ } catch ( error ) {
832+ console . error ( 'Error refreshing chart' , error )
833+ }
834+ } )
835+ return
836+ }
837+
801838 const storeMountedChart = ( chart ) => {
802839 storedCharts . value . push ( chart )
803840
@@ -809,7 +846,7 @@ export const useChartsStore = defineStore(
809846 const query = {
810847 variables : activePlt . value . abbreviation ,
811848 plot : chartTab . value ,
812- showLine : showLine . value ,
849+ symbology : symbology . value ,
813850 showStatistics : showStatistics . value ,
814851 dataQualityFlags : dataQualityFlags . value
815852 // timeRange: timeRange.value,
@@ -840,12 +877,20 @@ export const useChartsStore = defineStore(
840877 console . error ( 'Invalid Plot Abbreviation' , query . variables )
841878 }
842879 }
843- if ( query . showLine ) {
844- showLine . value = query . showLine == 'true'
845- }
846880 if ( query . showStatistics ) {
847881 showStatistics . value = query . showStatistics == 'true'
848882 }
883+ if ( query . symbology ) {
884+ // if the query is a string, convert it to an array
885+ if ( typeof query . symbology === 'string' ) {
886+ query . symbology = [ query . symbology ]
887+ }
888+
889+ // and remove duplicates
890+ query . symbology = [ ...new Set ( query . symbology ) ]
891+
892+ symbology . value = query . symbology
893+ }
849894 if ( query . dataQualityFlags ) {
850895 // if the query is a string, convert it to an array
851896 if ( typeof query . dataQualityFlags === 'string' ) {
@@ -873,8 +918,8 @@ export const useChartsStore = defineStore(
873918 console . log ( 'Active PLT Changed' , pre , post )
874919 updateRouteAfterPlotChange ( )
875920 } )
876- watch ( showLine , ( ) => {
877- console . log ( 'Show Line Changed' , showLine . value )
921+ watch ( symbology , ( ) => {
922+ console . log ( 'Symbology Changed' , symbology . value )
878923 updateRouteAfterPlotChange ( )
879924 } )
880925 watch ( showStatistics , ( ) => {
@@ -906,7 +951,8 @@ export const useChartsStore = defineStore(
906951 hasNodeData,
907952 dynamicColors,
908953 dataQualityFilterAllDatasets,
909- updateAllCharts,
954+ updateAllChartsData,
955+ refreshAllCharts,
910956 filterDatasetsToTimeRange,
911957 filterDatasetsBySetOfDates,
912958 dataQualityOptions,
@@ -917,8 +963,8 @@ export const useChartsStore = defineStore(
917963 nodeCharts,
918964 reachCharts,
919965 showStatistics,
920- showLine ,
921- updateShowLine ,
966+ symbology ,
967+ updateSymbology ,
922968 storeMountedChart,
923969 activePlt,
924970 generateDataQualityLegend,
0 commit comments