-
Notifications
You must be signed in to change notification settings - Fork 185
brush, pointer #721
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
Draft
mbostock
wants to merge
1
commit into
main
Choose a base branch
from
mbostock/pointer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
brush, pointer #721
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {brush as brusher, brushX as brusherX, brushY as brusherY, create, select} from "d3"; | ||
import {identity, maybeTuple} from "../options.js"; | ||
import {Mark} from "../plot.js"; | ||
import {selection, selectionEquals} from "../selection.js"; | ||
import {applyDirectStyles, applyIndirectStyles} from "../style.js"; | ||
|
||
const defaults = { | ||
ariaLabel: "brush", | ||
fill: "#777", | ||
fillOpacity: 0.3, | ||
stroke: "#fff" | ||
}; | ||
|
||
export class Brush extends Mark { | ||
constructor(data, {x, y, ...options} = {}) { | ||
super( | ||
data, | ||
[ | ||
{name: "x", value: x, scale: "x", optional: true}, | ||
{name: "y", value: y, scale: "y", optional: true} | ||
], | ||
options, | ||
defaults | ||
); | ||
this.activeElement = null; | ||
} | ||
render(index, {x, y}, {x: X, y: Y}, dimensions) { | ||
const {ariaLabel, ariaDescription, ariaHidden, ...options} = this; | ||
const {marginLeft, width, marginRight, marginTop, height, marginBottom} = dimensions; | ||
const brush = this; | ||
const g = create("svg:g") | ||
.call(applyIndirectStyles, {ariaLabel, ariaDescription, ariaHidden}, dimensions) | ||
.call((X && Y ? brusher : X ? brusherX : brusherY)() | ||
.extent([[marginLeft, marginTop], [width - marginRight, height - marginBottom]]) | ||
.on("start brush end", function(event) { | ||
const {type, selection: extent} = event; | ||
// For faceting, when starting a brush in a new facet, clear the | ||
// brush and selection on the old facet. In the future, we might | ||
// allow independent brushes across facets by disabling this? | ||
if (type === "start" && brush.activeElement !== this) { | ||
if (brush.activeElement !== null) { | ||
select(brush.activeElement).call(event.target.clear, event); | ||
brush.activeElement[selection] = null; | ||
} | ||
brush.activeElement = this; | ||
} | ||
let S = null; | ||
if (extent) { | ||
S = index; | ||
if (X) { | ||
let [x0, x1] = Y ? [extent[0][0], extent[1][0]] : extent; | ||
if (x.bandwidth) x0 -= x.bandwidth(); | ||
S = S.filter(i => x0 <= X[i] && X[i] <= x1); | ||
} | ||
if (Y) { | ||
let [y0, y1] = X ? [extent[0][1], extent[1][1]] : extent; | ||
if (y.bandwidth) y0 -= y.bandwidth(); | ||
S = S.filter(i => y0 <= Y[i] && Y[i] <= y1); | ||
} | ||
} | ||
if (!selectionEquals(this[selection], S)) { | ||
this[selection] = S; | ||
this.dispatchEvent(new Event("input", {bubbles: true})); | ||
} | ||
})) | ||
.call(g => g.selectAll(".selection") | ||
.attr("shape-rendering", null) // reset d3-brush | ||
.call(applyIndirectStyles, options, dimensions) | ||
.call(applyDirectStyles, options)) | ||
.node(); | ||
g[selection] = null; | ||
return g; | ||
} | ||
} | ||
|
||
export function brush(data, {x, y, ...options} = {}) { | ||
([x, y] = maybeTuple(x, y)); | ||
return new Brush(data, {...options, x, y}); | ||
} | ||
|
||
export function brushX(data, {x = identity, ...options} = {}) { | ||
return new Brush(data, {...options, x}); | ||
} | ||
|
||
export function brushY(data, {y = identity, ...options} = {}) { | ||
return new Brush(data, {...options, y}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how much of a "public" api your wanting to expose in these events... but access to the selected data indexes or the range of the data is needed. e.g.
where range is something like
if(X && x.invert) range = extent.map(x.invert);
Then in the listener, you can use the indexes for filtering right away or show the textual range to to the user.
Without access to the indexes used, callers only can view the
plot.value
. This only has what was selected. It does not provide what criteria was used for the selection, nor the data left out.