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

Use entityNearest() rather than entityNearestByXThenY() for LinePlot.entitiesAt() #3558

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 2 additions & 34 deletions src/plots/linePlot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright 2014-present Palantir Technologies
* @license MIT
*/
Expand Down Expand Up @@ -424,46 +424,14 @@ export class Line<X> extends XYPlot<X, number> {
}

public entitiesAt(point: Point): IPlotEntity[] {
const entity = this.entityNearestByXThenY(point);
const entity = this.entityNearest(point);
if (entity != null) {
return [entity];
} else {
return [];
}
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we shouldn't delete this because technically this would be a public API break and require a major version bump

* Returns the PlotEntity nearest to the query point by X then by Y, or undefined if no PlotEntity can be found.
*
* @param {Point} queryPoint
* @returns {PlotEntity} The nearest PlotEntity, or undefined if no PlotEntity can be found.
*/
public entityNearestByXThenY(queryPoint: Point): IPlotEntity {
let minXDist = Infinity;
let minYDist = Infinity;
let closest: IPlotEntity;

const chartBounds = this.bounds();
const entities = this.entities();
const entityLen = entities.length;
for (let i = 0; i < entityLen; i++) {
const entity = entities[i];
if (!Utils.Math.within(entity.position, chartBounds)) {
continue;
}
const xDist = Math.abs(queryPoint.x - entity.position.x);
const yDist = Math.abs(queryPoint.y - entity.position.y);

if (xDist < minXDist || xDist === minXDist && yDist < minYDist) {
closest = entity;
minXDist = xDist;
minYDist = yDist;
}
}

return closest;
}

protected _propertyProjectors(): AttributeToProjector {
const propertyToProjectors = super._propertyProjectors();
propertyToProjectors["d"] = this._constructLineProjector(Plot._scaledAccessor(this.x()), Plot._scaledAccessor(this.y()));
Expand Down