@@ -87,20 +87,24 @@ export class CFDRenderer extends UIControlsRenderer {
8787 * @param {string } graphElementSelector - Selector of the DOM element to render the graph.
8888 */
8989 renderGraph ( graphElementSelector ) {
90+ console . log ( 'setupBrush-b-selectedTimeRange' , this . selectedTimeRange ) ;
9091 this . graphElementSelector = graphElementSelector ;
9192 this . #drawSvg( graphElementSelector ) ;
9293 this . svg . append ( 'g' ) . attr ( 'transform' , `translate(${ this . margin . left } , ${ this . margin . top } )` ) ;
9394 this . #stackedData = this . #computeStackData( ) ;
9495 this . #drawAxes( ) ;
9596 this . #drawArea( ) ;
97+ console . log ( 'setupBrush-a-selectedTimeRange' , this . selectedTimeRange ) ;
9698 }
9799
98100 /**
99101 * Renders a brush component with the time range selection.
100102 */
101103 renderBrush ( ) {
102104 const svgBrush = this . #drawBrushSvg( this . brushSelector ) ;
103- const defaultSelectionRange = this . defaultTimeRange . map ( ( d ) => this . x ( d ) ) ;
105+ console . log ( 'selectedTimeRange' , this . selectedTimeRange ) ;
106+ console . log ( 'defaultTimeRange' , this . defaultTimeRange ) ;
107+ const defaultSelectionRange = this . selectedTimeRange . map ( ( d ) => this . x ( d ) ) ;
104108 this . brush = d3
105109 . brushX ( )
106110 . extent ( [
@@ -109,6 +113,7 @@ export class CFDRenderer extends UIControlsRenderer {
109113 ] )
110114 . on ( 'brush' , ( { selection } ) => {
111115 this . selectedTimeRange = selection . map ( this . x ?. invert , this . x ) ;
116+ console . log ( 'inside-selectedTimeRange---------' , this . selectedTimeRange ) ;
112117 this . updateGraph ( this . selectedTimeRange ) ;
113118 if ( this . isManualBrushUpdate && this . eventBus ) {
114119 this . eventBus ?. emitEvents ( `change-time-range-${ this . chartName } ` , this . selectedTimeRange ) ;
@@ -133,17 +138,51 @@ export class CFDRenderer extends UIControlsRenderer {
133138 }
134139
135140 /**
136- * Clears the CFD graph and brush from the specified DOM elements.
137- * @param {string } graphElementSelector - Selector of the DOM element to clear the graph.
138- * @param {string } cfdBrushElementSelector - Selector of the DOM element to clear the brush.
141+ * Updates the renderer's data and re-renders the graph, preserving persistent props.
142+ * @param {Array } newData - The new data to render.
143+ */
144+ updateData ( newData ) {
145+ this . data = newData ;
146+ // After updating this.data
147+ const dataDates = this . data . map ( ( d ) => new Date ( d . date ) ) ;
148+ const minDate = d3 . min ( dataDates ) ;
149+ const maxDate = d3 . max ( dataDates ) ;
150+
151+ if ( this . selectedTimeRange ) {
152+ let [ selectedStart , selectedEnd ] = this . selectedTimeRange ;
153+ selectedStart = selectedStart < minDate ? minDate : selectedStart ;
154+ selectedEnd = selectedEnd > maxDate ? maxDate : selectedEnd ;
155+ this . selectedTimeRange = [ selectedStart , selectedEnd ] ;
156+ } else {
157+ this . selectedTimeRange = [ minDate , maxDate ] ;
158+ }
159+ // Preserve persistent props like selectedTimeRange, reportingRangeDays, etc.
160+ // Re-render the graph with the new data
161+ if ( this . graphElementSelector ) {
162+ this . clearGraph ( this . graphElementSelector , this . brushSelector ) ;
163+ this . renderGraph ( this . graphElementSelector ) ;
164+ this . renderBrush ( this . brushSelector ) ;
165+ }
166+ }
167+
168+ /**
169+ * Clears the graph and brush SVGs, removes listeners, and handles missing event bus gracefully.
139170 */
140171 clearGraph ( graphElementSelector , cfdBrushElementSelector ) {
141- this . eventBus . removeAllListeners ( 'change-time-range-scatterplot' ) ;
142- this . eventBus . removeAllListeners ( 'scatterplot-mousemove' ) ;
143- this . eventBus . removeAllListeners ( 'scatterplot-mouseleave' ) ;
144- this . eventBus . removeAllListeners ( 'change-time-interval-scatterplot' ) ;
172+ console . log ( 'clearGraph-selectedTimeRange' , this . selectedTimeRange ) ;
173+ if ( this . eventBus && typeof this . eventBus . removeAllListeners === 'function' ) {
174+ this . eventBus . removeAllListeners ( 'change-time-range-scatterplot' ) ;
175+ this . eventBus . removeAllListeners ( 'scatterplot-mousemove' ) ;
176+ this . eventBus . removeAllListeners ( 'scatterplot-mouseleave' ) ;
177+ this . eventBus . removeAllListeners ( 'change-time-interval-scatterplot' ) ;
178+ }
179+ // Remove all children from the SVG elements
180+ d3 . select ( graphElementSelector ) . selectAll ( '*' ) . remove ( ) ;
181+ d3 . select ( cfdBrushElementSelector ) . selectAll ( '*' ) . remove ( ) ;
182+
145183 this . #drawBrushSvg( cfdBrushElementSelector ) ;
146184 this . #drawSvg( graphElementSelector ) ;
185+ console . log ( 'clearGraph-after-selectedTimeRange' , this . selectedTimeRange ) ;
147186 }
148187
149188 /**
@@ -372,7 +411,7 @@ export class CFDRenderer extends UIControlsRenderer {
372411 g . selectAll ( 'text' ) . attr ( 'y' , 30 ) . style ( 'fill' , 'black' ) ;
373412 g . attr ( 'clip-path' , `url(#${ clipId } )` ) ;
374413 } else {
375- axis = this . createXAxis ( x , 'months ' ) ;
414+ axis = this . createXAxis ( x , 'quarters ' ) ;
376415 g . call ( axis ) . attr ( 'transform' , `translate(0, ${ height } )` ) ;
377416 }
378417 }
0 commit comments