@@ -32,11 +32,14 @@ class GraphsContainer extends React.Component {
3232 this . tooltip = React . createRef ( ) ;
3333 this . leftChartPadding = 25 ;
3434 this . rightChartPadding = 10 ;
35+ const scatterPlotData = flatMap ( this . props . testData , ( item ) =>
36+ item . visible ? item . data : [ ] ,
37+ ) ;
38+ const zoomDomain = this . initZoomDomain ( scatterPlotData ) ;
3539 this . state = {
3640 highlights : [ ] ,
37- scatterPlotData : flatMap ( this . props . testData , ( item ) =>
38- item . visible ? item . data : [ ] ,
39- ) ,
41+ scatterPlotData,
42+ zoomDomain,
4043 lockTooltip : false ,
4144 externalMutation : undefined ,
4245 width : window . innerWidth ,
@@ -45,11 +48,15 @@ class GraphsContainer extends React.Component {
4548
4649 componentDidMount ( ) {
4750 const { zoom, selectedDataPoint } = this . props ;
48-
51+ const { scatterPlotData } = this . state ;
52+ const zoomDomain = this . initZoomDomain ( scatterPlotData ) ;
4953 this . addHighlights ( ) ;
5054 if ( selectedDataPoint ) this . verifySelectedDataPoint ( ) ;
5155 window . addEventListener ( 'resize' , ( ) =>
52- this . setState ( { width : window . innerWidth } ) ,
56+ this . setState ( {
57+ width : window . innerWidth ,
58+ zoomDomain,
59+ } ) ,
5360 ) ;
5461 }
5562
@@ -60,6 +67,9 @@ class GraphsContainer extends React.Component {
6067 testData,
6168 timeRange,
6269 } = this . props ;
70+ const scatterPlotData = flatMap ( testData , ( item ) =>
71+ item . visible ? item . data : [ ] ,
72+ ) ;
6373
6474 if (
6575 prevProps . highlightAlerts !== highlightAlerts ||
@@ -77,6 +87,35 @@ class GraphsContainer extends React.Component {
7787 }
7888 }
7989
90+ getToday = ( ) => {
91+ return moment . utc ( ) . toDate ( ) ;
92+ } ;
93+
94+ // limits for the zoomDomain of VictoryChart
95+ initZoomDomain = ( plotData ) => {
96+ const minDomainY = this . getMinY ( plotData ) ;
97+ const maxDomainY = this . getMaxY ( plotData ) ;
98+ // zoom domain padding is the space between the lowest/highest datapoint
99+ // and the top/bottom limits of the zoom domain. The minimum value is 100
100+ // as this is the top victory graph behavior
101+ let zoomDomPadd ;
102+ if ( minDomainY !== maxDomainY ) {
103+ zoomDomPadd = ( maxDomainY - minDomainY ) / 1.8 ;
104+ } else {
105+ zoomDomPadd = 100 ;
106+ }
107+ const minX = this . getMinX ( plotData ) ;
108+ const maxX = this . getToday ( ) ;
109+ const minY = minDomainY - zoomDomPadd < 0 ? 0 : minDomainY - zoomDomPadd ;
110+ const maxY = maxDomainY + zoomDomPadd ;
111+
112+ return { minX, maxX, minY, maxY } ;
113+ } ;
114+
115+ updateZoomDomain = ( plotData ) => {
116+ return this . initZoomDomain ( plotData ) ;
117+ } ;
118+
80119 verifySelectedDataPoint = ( ) => {
81120 const { selectedDataPoint, testData, updateStateParams } = this . props ;
82121
@@ -106,8 +145,10 @@ class GraphsContainer extends React.Component {
106145 item . visible ? item . data : [ ] ,
107146 ) ;
108147 this . addHighlights ( ) ;
148+ const zoomDomain = this . updateZoomDomain ( scatterPlotData ) ;
109149 this . setState ( {
110150 scatterPlotData,
151+ zoomDomain,
111152 } ) ;
112153
113154 if ( ! visibilityChanged ) {
@@ -267,11 +308,25 @@ class GraphsContainer extends React.Component {
267308 updateStateParams ( { zoom } ) ;
268309 } ;
269310
311+ // helper functions that allow the zoom domain to be tuned correctly
312+ getMinX = ( data ) => {
313+ return data . reduce ( ( min , p ) => ( p . x < min ? p . x : min ) , data [ 0 ] . x ) ;
314+ } ;
315+
316+ getMinY = ( data ) => {
317+ return data . reduce ( ( min , p ) => ( p . y < min ? p . y : min ) , data [ 0 ] . y ) ;
318+ } ;
319+
320+ getMaxY = ( data ) => {
321+ return data . reduce ( ( max , p ) => ( p . y > max ? p . y : max ) , data [ 0 ] . y ) ;
322+ } ;
323+
270324 render ( ) {
271325 const { testData, showTable, zoom, highlightedRevisions } = this . props ;
272326 const {
273327 highlights,
274328 scatterPlotData,
329+ zoomDomain,
275330 lockTooltip,
276331 externalMutation,
277332 width,
@@ -357,7 +412,8 @@ class GraphsContainer extends React.Component {
357412 style = { { parent : { maxHeight : '400px' , maxWidth : '1350px' } } }
358413 scale = { { x : 'time' , y : 'linear' } }
359414 domainPadding = { { y : 40 , x : [ 10 , 10 ] } }
360- maxDomain = { { x : today } }
415+ minDomain = { { x : zoomDomain . minX , y : zoomDomain . minY } }
416+ maxDomain = { { x : today , y : zoomDomain . maxY } }
361417 externalEventMutations = { externalMutation }
362418 containerComponent = {
363419 < VictoryZoomSelectionContainer
0 commit comments