@@ -84,6 +84,16 @@ export class ChartDatasourcePrometheusPlugin {
8484 // loop over queries
8585 // when we get all query results, we mix series into a single `datasets` array
8686 chart [ 'datasource-prometheus' ] . loading = true ;
87+
88+ if ( options . loadingMsg ) {
89+ this . writeText ( chart , options . loadingMsg . message , ( ctx ) => {
90+ ctx . direction = options . loadingMsg . direction ;
91+ ctx . textAlign = options . loadingMsg . textAlign ;
92+ ctx . textBaseline = options . loadingMsg . textBaseline ;
93+ ctx . font = options . loadingMsg . font ;
94+ } ) ;
95+ }
96+
8797 Promise . all ( reqs )
8898 . then ( ( results ) => {
8999 // extract data from responses and prepare series for Chart.js
@@ -142,34 +152,35 @@ export class ChartDatasourcePrometheusPlugin {
142152 const options = Object . assign ( new ChartDatasourcePrometheusPluginOptions ( ) , _options ) ;
143153
144154 if ( chart [ 'datasource-prometheus' ] . error != null ) {
145- const ctx = chart . ctx ;
146- const width = chart . width ;
147- const height = chart . height ;
148- chart . clear ( ) ;
149-
150- ctx . save ( ) ;
151- ctx . direction = options . errorMsg . direction ;
152- ctx . textAlign = options . errorMsg . textAlign ;
153- ctx . textBaseline = options . errorMsg . textBaseline ;
154- ctx . font = "16px normal 'Helvetica Nueue'" ;
155- ctx . fillText ( options . errorMsg ?. message || chart [ 'datasource-prometheus' ] . error , width / 2 , height / 2 ) ;
156- ctx . restore ( ) ;
157- return ;
158- } else if ( chart . data . datasets . length == 0 ) {
159- const ctx = chart . ctx ;
160- const width = chart . width ;
161- const height = chart . height ;
162- chart . clear ( ) ;
163-
164- ctx . save ( ) ;
165- ctx . direction = options . noDataMsg . direction ;
166- ctx . textAlign = options . noDataMsg . textAlign ;
167- ctx . textBaseline = options . noDataMsg . textBaseline ;
168- ctx . font = options . noDataMsg . font ;
169- ctx . fillText ( options . noDataMsg . message , width / 2 , height / 2 ) ;
170- ctx . restore ( ) ;
171- return ;
155+ this . writeText ( chart , options . errorMsg ?. message || chart [ 'datasource-prometheus' ] . error , ( ctx ) => {
156+ ctx . direction = options . errorMsg . direction ;
157+ ctx . textAlign = options . errorMsg . textAlign ;
158+ ctx . textBaseline = options . errorMsg . textBaseline ;
159+ ctx . font = "16px normal 'Helvetica Nueue'" ;
160+ } ) ;
161+ } else if ( chart . data . datasets . length == 0 && chart [ 'datasource-prometheus' ] . loading !== true ) {
162+ this . writeText ( chart , options . noDataMsg . message , ( ctx ) => {
163+ ctx . direction = options . noDataMsg . direction ;
164+ ctx . textAlign = options . noDataMsg . textAlign ;
165+ ctx . textBaseline = options . noDataMsg . textBaseline ;
166+ ctx . font = options . noDataMsg . font ;
167+ } ) ;
168+ }
169+ }
170+
171+ public writeText ( chart : Chart , message : string , fn ?: ( ctx : CanvasRenderingContext2D ) => void ) {
172+ const ctx = chart . ctx ;
173+ const width = chart . width ;
174+ const height = chart . height ;
175+ chart . clear ( ) ;
176+
177+ ctx . save ( ) ;
178+ if ( fn ) {
179+ fn ( ctx ) ;
172180 }
181+
182+ ctx . fillText ( message , width / 2 , height / 2 ) ;
183+ ctx . restore ( ) ;
173184 }
174185
175186 public destroy ( chart : Chart , args : any , _options : any ) {
0 commit comments