Skip to content

Commit

Permalink
Merge pull request #1660 from palantir/getSelection
Browse files Browse the repository at this point in the history
drawer._getSelection
  • Loading branch information
bluong committed Feb 20, 2015
2 parents 25b9db2 + b56c72f commit 9006161
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plottable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@ declare module Plottable {
_getRenderArea(): D3.Selection;
_getSelector(): string;
_getPixelPoint(datum: any, index: number): Point;
_getSelection(index: number): D3.Selection;
}
}
}
Expand All @@ -1489,6 +1490,7 @@ declare module Plottable {
protected _drawStep(step: AppliedDrawStep): void;
_getSelector(): string;
_getPixelPoint(datum: any, index: number): Point;
_getSelection(index: number): D3.Selection;
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions plottable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,10 @@ var Plottable;
AbstractDrawer.prototype._getPixelPoint = function (datum, index) {
return null;
};
AbstractDrawer.prototype._getSelection = function (index) {
var allSelections = this._getRenderArea().selectAll(this._getSelector());
return d3.select(allSelections[0][index]);
};
return AbstractDrawer;
})();
_Drawer.AbstractDrawer = AbstractDrawer;
Expand Down Expand Up @@ -2866,6 +2870,9 @@ var Plottable;
Line.prototype._getPixelPoint = function (datum, index) {
return { x: this._attrToProjector["x"](datum, index), y: this._attrToProjector["y"](datum, index) };
};
Line.prototype._getSelection = function (index) {
return this._getRenderArea().select(this._getSelector());
};
Line.LINE_CLASS = "line";
return Line;
})(_Drawer.AbstractDrawer);
Expand Down
5 changes: 5 additions & 0 deletions src/drawers/abstractDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ export module _Drawer {
return null;
}

public _getSelection(index: number): D3.Selection {
var allSelections = this._getRenderArea().selectAll(this._getSelector());
return d3.select(allSelections[0][index]);
}

}
}
}
4 changes: 4 additions & 0 deletions src/drawers/lineDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export module _Drawer {
public _getPixelPoint(datum: any, index: number): Point {
return { x: this._attrToProjector["x"](datum, index), y: this._attrToProjector["y"](datum, index) };
}

public _getSelection(index: number): D3.Selection {
return this._getRenderArea().select(this._getSelector());
}
}
}
}
13 changes: 13 additions & 0 deletions test/drawers/drawerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,18 @@ describe("Drawers", () => {
drawer.draw([], steps, null, null);
assert.deepEqual(timings, [0, 20, 30], "setTimeout called with appropriate times");
});

it("_getSelection", () => {
var svg = generateSVG(300, 300);
var drawer = new Plottable._Drawer.AbstractDrawer("test");
drawer.setup(svg.append("g"));
(<any> drawer)._getSelector = () => "circle";
var data = [{one: 2, two: 1}, {one: 33, two: 21}, {one: 11, two: 10}];
var circles = drawer._getRenderArea().selectAll("circle").data(data);
circles.enter().append("circle").attr("cx", (datum: any) => datum.one).attr("cy", (datum: any) => datum.two).attr("r", 10);
var selection = drawer._getSelection(1);
assert.strictEqual(selection.node(), circles[0][1], "correct selection gotten");
svg.remove();
});
});
});
24 changes: 24 additions & 0 deletions test/drawers/lineDrawerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,29 @@ describe("Drawers", () => {

svg.remove();
});

it("getSelection", () => {
var svg = generateSVG(300, 300);
var data = [{a: 12, b: 10}, {a: 13, b: 24}, {a: 14, b: 21}, {a: 15, b: 14}];
var xScale = new Plottable.Scale.Linear();
var yScale = new Plottable.Scale.Linear();
var linePlot = new Plottable.Plot.Line(xScale, yScale);

var drawer = new Plottable._Drawer.Line("one");
(<any> linePlot)._getDrawer = () => drawer;

linePlot.addDataset("one", data);
linePlot.project("x", "a", xScale);
linePlot.project("y", "b", yScale);
linePlot.renderTo(svg);

var lineSelection = linePlot.getAllSelections();
data.forEach((datum: any, index: number) => {
var selection = drawer._getSelection(index);
assert.strictEqual(selection.node(), lineSelection.node(), "line selection retrieved");
});

svg.remove();
});
});
});
31 changes: 31 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ describe("Drawers", function () {
drawer.draw([], steps, null, null);
assert.deepEqual(timings, [0, 20, 30], "setTimeout called with appropriate times");
});
it("_getSelection", function () {
var svg = generateSVG(300, 300);
var drawer = new Plottable._Drawer.AbstractDrawer("test");
drawer.setup(svg.append("g"));
drawer._getSelector = function () { return "circle"; };
var data = [{ one: 2, two: 1 }, { one: 33, two: 21 }, { one: 11, two: 10 }];
var circles = drawer._getRenderArea().selectAll("circle").data(data);
circles.enter().append("circle").attr("cx", function (datum) { return datum.one; }).attr("cy", function (datum) { return datum.two; }).attr("r", 10);
var selection = drawer._getSelection(1);
assert.strictEqual(selection.node(), circles[0][1], "correct selection gotten");
svg.remove();
});
});
});

Expand Down Expand Up @@ -389,6 +401,25 @@ describe("Drawers", function () {
});
svg.remove();
});
it("getSelection", function () {
var svg = generateSVG(300, 300);
var data = [{ a: 12, b: 10 }, { a: 13, b: 24 }, { a: 14, b: 21 }, { a: 15, b: 14 }];
var xScale = new Plottable.Scale.Linear();
var yScale = new Plottable.Scale.Linear();
var linePlot = new Plottable.Plot.Line(xScale, yScale);
var drawer = new Plottable._Drawer.Line("one");
linePlot._getDrawer = function () { return drawer; };
linePlot.addDataset("one", data);
linePlot.project("x", "a", xScale);
linePlot.project("y", "b", yScale);
linePlot.renderTo(svg);
var lineSelection = linePlot.getAllSelections();
data.forEach(function (datum, index) {
var selection = drawer._getSelection(index);
assert.strictEqual(selection.node(), lineSelection.node(), "line selection retrieved");
});
svg.remove();
});
});
});

Expand Down

0 comments on commit 9006161

Please sign in to comment.