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

new(brush): expose updateBrush method #934

Merged
merged 7 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 12 additions & 8 deletions packages/visx-brush/src/Brush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export type BrushProps = {
resetOnEnd?: boolean;
/** Size of Brush handles, applies to all `resizeTriggerAreas`. */
handleSize: number;
/** Reference to the BaseBrush component. */
innerRef?: React.MutableRefObject<BaseBrush | null>;
};

class Brush extends Component<BrushProps> {
Expand Down Expand Up @@ -160,6 +162,7 @@ class Brush extends Component<BrushProps> {
margin,
brushDirection,
initialBrushPosition,
innerRef,
resizeTriggerAreas,
brushRegion,
yAxisOrientation,
Expand Down Expand Up @@ -216,20 +219,21 @@ class Brush extends Component<BrushProps> {
height={brushRegionHeight}
left={left}
top={top}
brushDirection={brushDirection}
disableDraggingSelection={disableDraggingSelection}
handleSize={handleSize}
inheritedMargin={margin}
initialBrushPosition={initialBrushPosition}
onChange={this.handleChange}
onBrushEnd={this.handleBrushEnd}
onBrushStart={this.handleBrushStart}
handleSize={handleSize}
ref={innerRef}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the added line, the rest are just sorted by proptype & name

resetOnEnd={resetOnEnd}
resizeTriggerAreas={resizeTriggerAreas}
brushDirection={brushDirection}
selectedBoxStyle={selectedBoxStyle}
disableDraggingSelection={disableDraggingSelection}
resetOnEnd={resetOnEnd}
onBrushEnd={this.handleBrushEnd}
onBrushStart={this.handleBrushStart}
onChange={this.handleChange}
onClick={onClick}
onMouseLeave={onMouseLeave}
onMouseMove={onMouseMove}
onClick={onClick}
/>
);
}
Expand Down
36 changes: 35 additions & 1 deletion packages/visx-demo/src/sandboxes/visx-brush/Example.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState, useMemo } from 'react';
import React, { useRef, useState, useMemo } from 'react';
import { scaleTime, scaleLinear } from '@visx/scale';
import appleStock, { AppleStock } from '@visx/mock-data/lib/mocks/appleStock';
import { Brush } from '@visx/brush';
import { Bounds } from '@visx/brush/lib/types';
import BaseBrush, { BaseBrushState, UpdateBrush } from '@visx/brush/lib/BaseBrush';
import { PatternLines } from '@visx/pattern';
import { LinearGradient } from '@visx/gradient';
import { max, extent } from 'd3-array';
Expand Down Expand Up @@ -45,6 +46,7 @@ function BrushChart({
right: 20,
},
}: BrushProps) {
const brushRef = useRef<BaseBrush | null>(null);
const [filteredStock, setFilteredStock] = useState(stock);

const onBrushChange = (domain: Bounds | null) => {
Expand Down Expand Up @@ -113,6 +115,35 @@ function BrushChart({
[brushDateScale],
);

// event handlers
const handleClearClick = () => {
if (brushRef?.current) {
setFilteredStock(stock);
brushRef.current.reset();
}
};

const handleResetClick = () => {
if (brushRef?.current) {
const updater: UpdateBrush = prevBrush => {
const newExtent = brushRef.current!.getExtent(
initialBrushPosition.start,
initialBrushPosition.end,
);

const newState: BaseBrushState = {
...prevBrush,
start: { y: newExtent.y0, x: newExtent.x0 },
end: { y: newExtent.y1, x: newExtent.x1 },
extent: newExtent,
};

return newState;
};
brushRef.current.updateBrush(updater);
}
};

return (
<div>
<svg width={width} height={height}>
Expand Down Expand Up @@ -155,6 +186,7 @@ function BrushChart({
height={yBrushMax}
margin={brushMargin}
handleSize={8}
innerRef={brushRef}
resizeTriggerAreas={['left', 'right']}
brushDirection="horizontal"
initialBrushPosition={initialBrushPosition}
Expand All @@ -164,6 +196,8 @@ function BrushChart({
/>
</AreaChart>
</svg>
<button onClick={handleClearClick}>Clear</button>&nbsp;
<button onClick={handleResetClick}>Reset</button>
</div>
);
}
Expand Down