-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: brush selection error #7246
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
base: v5
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { Chart } from '../../../src'; | ||
|
|
||
| export async function issue6751(context) { | ||
| const { container, canvas } = context; | ||
| const chart = new Chart({ | ||
| container, | ||
| canvas, | ||
| }); | ||
| const data = [ | ||
| { | ||
| x: 0.8, | ||
| y: 'A', | ||
| }, | ||
| { | ||
| x: 1, | ||
| y: 'A', | ||
| }, | ||
| { | ||
| x: 2, | ||
| y: 'B', | ||
| }, | ||
| { | ||
| x: 3, | ||
| y: 'C', | ||
| }, | ||
| ]; | ||
| chart | ||
| .point() | ||
| .data(data) | ||
| .encode('x', 'x') | ||
| .encode('y', 'y') | ||
| .interaction('brushHighlight', {}) | ||
| .scale({ | ||
| x: { | ||
| domain: [0.5, 5], | ||
| }, | ||
| y: { | ||
| range: [1, 0], | ||
| }, | ||
| }); | ||
|
|
||
| chart.on('brush:end', (e) => { | ||
| console.log(e.data.selection); | ||
| }); | ||
|
|
||
| const finished = chart.render(); | ||
|
|
||
| return { | ||
| chart, | ||
| finished, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,27 +1,32 @@ | ||||||
| import { bisectLeft, sort } from '@antv/vendor/d3-array'; | ||||||
|
|
||||||
| function constrain(x, lo, hi) { | ||||||
| return Math.min(hi, Math.max(lo, x)); | ||||||
| } | ||||||
| import { sort } from '@antv/vendor/d3-array'; | ||||||
|
|
||||||
| export function isOrdinalScale(scale) { | ||||||
| return !!scale.getBandWidth; | ||||||
| } | ||||||
|
|
||||||
| export function invert(scale, x, start) { | ||||||
| export function invert(scale, x) { | ||||||
| if (!isOrdinalScale(scale)) return scale.invert(x); | ||||||
| const { adjustedRange } = scale; | ||||||
| if (adjustedRange.includes(x)) { | ||||||
| return scale.invert(x); | ||||||
| } | ||||||
| const { domain } = scale.getOptions(); | ||||||
| const offset = start ? -1 : 0; | ||||||
| const domain = scale.getOptions().domain; | ||||||
| const range = scale.adjustedRange; | ||||||
| const step = scale.getStep(); | ||||||
| const range = start ? adjustedRange : adjustedRange.map((d) => d + step); | ||||||
| // R[i0 - 1] < x <= R[i0] | ||||||
| const i0 = bisectLeft(range, x); | ||||||
| const i1 = constrain(i0 + offset, 0, domain.length - 1); | ||||||
| return domain[i1]; | ||||||
| const bandwidth = scale.getBandWidth?.() ?? step; | ||||||
|
|
||||||
| // compute centers of each band | ||||||
| const centers = range.map((r) => r + bandwidth / 2); | ||||||
|
|
||||||
| // find nearest center | ||||||
| let min = Infinity; | ||||||
| let index = 0; | ||||||
|
|
||||||
| for (let i = 0; i < centers.length; i++) { | ||||||
| const d = Math.abs(x - centers[i]); | ||||||
| if (d < min) { | ||||||
| min = d; | ||||||
| index = i; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return domain[index]; | ||||||
| } | ||||||
|
|
||||||
| export function domainOf(scale, values, ratioX?) { | ||||||
|
|
@@ -50,14 +55,14 @@ export function domainOf(scale, values, ratioX?) { | |||||
|
|
||||||
| export function selectionOf(x, y, x1, y1, scale, coordinate) { | ||||||
| const { x: scaleX, y: scaleY } = scale; | ||||||
| const abstractDomain = (point, start) => { | ||||||
| const [x, y] = coordinate.invert(point); | ||||||
| return [invert(scaleX, x, start), invert(scaleY, y, start)]; | ||||||
| const abstract = (point) => { | ||||||
| const [px, py] = coordinate.invert(point); | ||||||
| return [invert(scaleX, px), invert(scaleY, py)]; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible for
Suggested change
|
||||||
| }; | ||||||
| const p0 = abstractDomain([x, y], true); | ||||||
| const p1 = abstractDomain([x1, y1], false); | ||||||
| const domainX = domainOf(scaleX, [p0[0], p1[0]]); | ||||||
| const domainY = domainOf(scaleY, [p0[1], p1[1]]); | ||||||
| const p0 = abstract([x, y]); | ||||||
| const p1 = abstract([x1, y1]); | ||||||
| const domainX = domainOf(scaleX, p0[0] !== undefined ? [p0[0], p1[0]] : []); | ||||||
| const domainY = domainOf(scaleY, p0[1] !== undefined ? [p0[1], p1[1]] : []); | ||||||
| return [domainX, domainY]; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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.
This
chart.on('brush:end', ...)block contains aconsole.logwhich appears to be for debugging. This should be removed from the test code before merging. If the event emission needs to be tested, it should be replaced with an assertion.