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 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
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 */
peterwiebe marked this conversation as resolved.
Show resolved Hide resolved
innerRef?: React.MutableRefObject<BaseBrush>;
};

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
38 changes: 37 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 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,37 @@ function BrushChart({
[brushDateScale],
);

const handleResetClick = () => {
if (!brushRef?.current) {
console.log(`innerRef Prop not properly set.`);
}

setFilteredStock(stock);
brushRef?.current?.reset();
};

const handleLastWeekClick = () => {
if (!brushRef?.current) {
console.log(`innerRef Prop not properly set.`);
peterwiebe marked this conversation as resolved.
Show resolved Hide resolved
}

// Update Brush
brushRef?.current?.updateBrush(prevBrush => {
const start = { x: brushDateScale(getDate(stock[stock.length - 8])) };
const end = { x: brushDateScale(getDate(stock[stock.length - 1])) };
const extent = brushRef?.current?.getExtent(start, end);

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

return newState;
});
};

return (
<div>
<svg width={width} height={height}>
Expand Down Expand Up @@ -155,6 +188,7 @@ function BrushChart({
height={yBrushMax}
margin={brushMargin}
handleSize={8}
innerRef={brushRef}
resizeTriggerAreas={['left', 'right']}
brushDirection="horizontal"
initialBrushPosition={initialBrushPosition}
Expand All @@ -164,6 +198,8 @@ function BrushChart({
/>
</AreaChart>
</svg>
<button onClick={handleResetClick}>Reset</button>&nbsp;
peterwiebe marked this conversation as resolved.
Show resolved Hide resolved
<button onClick={handleLastWeekClick}>1 Week</button>
</div>
);
}
Expand Down