Skip to content

Commit 73da507

Browse files
authored
fix: refit the graph on the value axis when the query results change (#5041)
1 parent 093ba50 commit 73da507

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/visualization/utils/useVisDomainSettings.ts

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,25 @@ export const useZoomRequeryXDomainSettings = (args: ZoomRequeryArgs) => {
173173
return getValidRange(data, timeRange)
174174
}, [storedDomain, data]) // eslint-disable-line react-hooks/exhaustive-deps
175175

176-
const [domain, setDomain] = useState(initialDomain)
176+
/*
177+
* preZoomDomain:
178+
* - can be changed only by the query results
179+
* - uses one-way state to capture the very first set of values
180+
*/
177181
const [preZoomDomain] = useOneWayState(initialDomain)
178182

183+
/*
184+
* domain:
185+
* - can be changed by user action: zooming or unzooming
186+
* - zooming will trigger a new query if the windowPeriod changes
187+
* - unzooming will revert the query results back to its original state
188+
* (before zooming), but does not query
189+
*
190+
* - can be changed also by the preZoomDomain, by syncing,
191+
* to keep the graph fitted, and does not query
192+
*/
193+
const [domain, setDomain] = useState(initialDomain)
194+
179195
const getAllVariablesWithTimeDomain = (state: AppState) =>
180196
getAllVariablesForZoomRequery(state, timeRange ? domain : [])
181197
const orgId = useSelector(getOrg)?.id
@@ -185,13 +201,6 @@ export const useZoomRequeryXDomainSettings = (args: ZoomRequeryArgs) => {
185201
getWindowPeriodFromVariables(query, variables)
186202
)
187203

188-
/*
189-
* When the user zooms in, re-run the query
190-
* When the user un-zooms, do not re-run but revert back to old data
191-
* Hence re-run the query only when both conditions are met:
192-
* - the window period changes from the domain changing (zooming in)
193-
* - the domain does not equal the original pre-zoom domain (unzooming)
194-
*/
195204
useEffect(() => {
196205
const updatedWindowPeriod = getWindowPeriodFromVariables(query, variables)
197206
if (isNotEqual(windowPeriod, updatedWindowPeriod)) {
@@ -224,6 +233,15 @@ export const useZoomRequeryXDomainSettings = (args: ZoomRequeryArgs) => {
224233
}
225234
}, [domain]) // eslint-disable-line react-hooks/exhaustive-deps
226235

236+
// sync preZoomDomain and domain
237+
// - when query results change to refit the graph
238+
// - except when it is the time axis to prevent a shrunken graph
239+
useEffect(() => {
240+
if (!timeRange && isNotEqual(preZoomDomain, domain)) {
241+
setDomain(preZoomDomain)
242+
}
243+
}, [preZoomDomain]) // eslint-disable-line react-hooks/exhaustive-deps
244+
227245
const setZoomDomain = (updatedDomain: number[]) => {
228246
setRequeryStatus(RemoteDataState.NotStarted)
229247
if (!preZoomResult) {
@@ -272,9 +290,25 @@ export const useZoomRequeryYDomainSettings = (args: ZoomRequeryArgs) => {
272290
return storedDomain
273291
}, [storedDomain, data]) // eslint-disable-line react-hooks/exhaustive-deps
274292

275-
const [domain, setDomain] = useState(initialDomain)
293+
/*
294+
* preZoomDomain:
295+
* - can be changed only by the query results
296+
* - uses one-way state to capture the very first set of values
297+
*/
276298
const [preZoomDomain] = useOneWayState(initialDomain)
277299

300+
/*
301+
* domain:
302+
* - can be changed by user action: zooming or unzooming
303+
* - zooming will trigger a new query if the windowPeriod changes
304+
* - unzooming will revert the query results back to its original state
305+
* (before zooming), but does not query
306+
*
307+
* - can be changed also by the preZoomDomain, by syncing,
308+
* to keep the graph fitted, and does not query
309+
*/
310+
const [domain, setDomain] = useState(initialDomain)
311+
278312
const getAllVariablesWithTimeDomain = (state: AppState) =>
279313
getAllVariablesForZoomRequery(state, timeRange ? domain : [])
280314
const orgId = useSelector(getOrg)?.id
@@ -284,13 +318,6 @@ export const useZoomRequeryYDomainSettings = (args: ZoomRequeryArgs) => {
284318
getWindowPeriodFromVariables(query, variables)
285319
)
286320

287-
/*
288-
* When the user zooms in, re-run the query
289-
* When the user un-zooms, do not re-run but revert back to old data
290-
* Hence re-run the query only when both conditions are met:
291-
* - the window period changes from the domain changing (zooming in)
292-
* - the domain does not equal the original pre-zoom domain (unzooming)
293-
*/
294321
useEffect(() => {
295322
const updatedWindowPeriod = getWindowPeriodFromVariables(query, variables)
296323
if (isNotEqual(windowPeriod, updatedWindowPeriod)) {
@@ -323,6 +350,15 @@ export const useZoomRequeryYDomainSettings = (args: ZoomRequeryArgs) => {
323350
}
324351
}, [domain]) // eslint-disable-line react-hooks/exhaustive-deps
325352

353+
// sync preZoomDomain and domain
354+
// - when query results change to refit the graph
355+
// - except when it is the time axis to prevent a shrunken graph
356+
useEffect(() => {
357+
if (!timeRange && isNotEqual(preZoomDomain, domain)) {
358+
setDomain(preZoomDomain)
359+
}
360+
}, [preZoomDomain]) // eslint-disable-line react-hooks/exhaustive-deps
361+
326362
const setZoomDomain = (updatedDomain: number[]) => {
327363
setRequeryStatus(RemoteDataState.NotStarted)
328364
if (!preZoomResult) {

0 commit comments

Comments
 (0)