Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit x-axis by filtered measurements #1827

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/components/measurements/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useEffect, useMemo, useState } from "react";
import React, { useCallback, useRef, useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { isEqual, orderBy } from "lodash";
import { NODE_VISIBLE } from "../../util/globals";
Expand Down Expand Up @@ -41,6 +41,17 @@
return ref.current;
};

/**
* A wrapper around React's useCallback hook that does a deep comparison of the
* dependencies.
* @param {function} fn
* @param {Array} dependencies
* @returns
*/
const useDeepCompareCallback = (fn, dependencies) => {
return useCallback(fn, dependencies.map(useDeepCompareMemo))

Check warning on line 52 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (16)

React Hook useCallback has a missing dependency: 'fn'. Either include it or remove the dependency array

Check warning on line 52 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (18)

React Hook useCallback has a missing dependency: 'fn'. Either include it or remove the dependency array

Check warning on line 52 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (20)

React Hook useCallback has a missing dependency: 'fn'. Either include it or remove the dependency array

Check warning on line 52 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (22)

React Hook useCallback has a missing dependency: 'fn'. Either include it or remove the dependency array
}

// Checks visibility against global NODE_VISIBLE
const isVisible = (visibility) => visibility === NODE_VISIBLE;

Expand Down Expand Up @@ -159,9 +170,9 @@
}
const groupedMeasurements = groupMeasurements(filteredMeasurements, groupBy, groupByValueOrder);

// Memoize D3 scale functions to allow deep comparison to work below for svgData
const xScale = useMemo(() => createXScale(width, measurements), [width, measurements]);
const yScale = useMemo(() => createYScale(), []);
// Cache D3 scale functions to allow deep comparison to work below for svgData
const xScale = useDeepCompareCallback(createXScale(width, filteredMeasurements), [width, filteredMeasurements]);
const yScale = useCallback(createYScale(), []);

Check warning on line 175 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (16)

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead

Check warning on line 175 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (18)

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead

Check warning on line 175 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (20)

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead

Check warning on line 175 in src/components/measurements/index.js

View workflow job for this annotation

GitHub Actions / lint (22)

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead
// Memoize all data needed for basic SVG to avoid extra re-drawings
const svgData = useDeepCompareMemo({
containerHeight: height,
Expand All @@ -172,8 +183,8 @@
groupingOrderedValues,
groupedMeasurements
});
// Memoize handleHover function to avoid extra useEffect calls
const handleHover = useMemo(() => (data, dataType, mouseX, mouseY, colorByAttr=null) => {
// Cache handleHover function to avoid extra useEffect calls
const handleHover = useCallback((data, dataType, mouseX, mouseY, colorByAttr=null) => {
let newHoverData = null;
if (data !== null) {
// Set color-by attribute as title if provided
Expand Down
9 changes: 8 additions & 1 deletion src/components/measurements/measurementsD3.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ const getSubplotDOMId = (groupingValueIndex) => `measurement_subplot_${groupingV
* @returns {function}
*/
export const createXScale = (panelWidth, measurements) => {
// Padding the xScale based on proportion
// Copied from https://github.com/d3/d3-scale/issues/150#issuecomment-561304239
function padLinear([x0, x1], k) {
const dx = (x1 - x0) * k / 2;
return [x0 - dx, x1 + dx];
}

return (
scaleLinear()
.domain(extent(measurements, (m) => m.value))
.domain(padLinear(extent(measurements, (m) => m.value), 0.1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This proportion of 0.1 is the kind of parameter I could imagine exposing to users some day through the measurements config JSON, since there could be dataset-specific reasons to prefer a lower or higher value. Maybe for the sake of remembering what this value means here and eventually exposing it later, could we define 0.1 as a variable in this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's totally fair! I'll add it as an optional param with a default value for now and we can feed in a user defined value in the future if desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in c302939

.range([layout.leftPadding, panelWidth - layout.rightPadding])
.nice()
);
Expand Down
Loading