Skip to content

Commit

Permalink
Merge pull request #1322 from palantir/stacked_area_projector
Browse files Browse the repository at this point in the history
Fix projector on stacked area
  • Loading branch information
jtlan committed Nov 12, 2014
2 parents 2d06add + b062ba1 commit ad22586
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions plottable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3208,6 +3208,7 @@ declare module Plottable {
_updateYDomainer(): void;
_onDatasetUpdate(): void;
_generateAttrToProjector(): AttributeToProjector;
_wholeDatumAttributes(): string[];
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions plottable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7993,14 +7993,21 @@ var Plottable;
StackedArea.prototype._generateAttrToProjector = function () {
var _this = this;
var attrToProjector = _super.prototype._generateAttrToProjector.call(this);
var wholeDatumAttributes = this._wholeDatumAttributes();
var isSingleDatumAttr = function (attr) { return wholeDatumAttributes.indexOf(attr) === -1; };
var singleDatumAttributes = d3.keys(attrToProjector).filter(isSingleDatumAttr);
singleDatumAttributes.forEach(function (attribute) {
var projector = attrToProjector[attribute];
attrToProjector[attribute] = function (data, i) { return data.length > 0 ? projector(data[0], i) : null; };
});
var yAccessor = this._projectors["y"].accessor;
attrToProjector["y"] = function (d) { return _this._yScale.scale(+yAccessor(d) + d["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"]); };
attrToProjector["y0"] = function (d) { return _this._yScale.scale(d["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"]); };
// Align fill with first index
var fillProjector = attrToProjector["fill"];
attrToProjector["fill"] = function (d, i) { return (d && d[0]) ? fillProjector(d[0], i) : null; };
return attrToProjector;
};
StackedArea.prototype._wholeDatumAttributes = function () {
return ["x", "y", "defined"];
};
return StackedArea;
})(Plot.AbstractStacked);
Plot.StackedArea = StackedArea;
Expand Down
16 changes: 12 additions & 4 deletions src/components/plots/stackedAreaPlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,24 @@ export module Plot {

public _generateAttrToProjector() {
var attrToProjector = super._generateAttrToProjector();
var wholeDatumAttributes = this._wholeDatumAttributes();
var isSingleDatumAttr = (attr: string) => wholeDatumAttributes.indexOf(attr) === -1;
var singleDatumAttributes = d3.keys(attrToProjector).filter(isSingleDatumAttr);
singleDatumAttributes.forEach((attribute: string) => {
var projector = attrToProjector[attribute];
attrToProjector[attribute] = (data: any[], i: number) => data.length > 0 ? projector(data[0], i) : null;
});

var yAccessor = this._projectors["y"].accessor;
attrToProjector["y"] = (d: any) => this._yScale.scale(+yAccessor(d) + d["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"]);
attrToProjector["y0"] = (d: any) => this._yScale.scale(d["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"]);

// Align fill with first index
var fillProjector = attrToProjector["fill"];
attrToProjector["fill"] = (d, i) => (d && d[0]) ? fillProjector(d[0], i) : null;

return attrToProjector;
}

public _wholeDatumAttributes() {
return ["x", "y", "defined"];
}
}
}
}
11 changes: 11 additions & 0 deletions test/components/plots/stackedAreaPlotTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,16 @@ describe("Plots", () => {
svg.remove();
});

it("project works correctly", () => {
renderer.project("check", "type");
var areas = renderer._renderArea.selectAll(".area");
var area0 = d3.select(areas[0][0]);
assert.strictEqual(area0.attr("check"), "a", "projector has been applied to first area");

var area1 = d3.select(areas[0][1]);
assert.strictEqual(area1.attr("check"), "b", "projector has been applied to second area");
svg.remove();
});

});
});
9 changes: 9 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3506,6 +3506,15 @@ describe("Plots", function () {
assert.strictEqual(4, domain[1], "highest area stacking is at upper limit of yScale domain");
svg.remove();
});
it("project works correctly", function () {
renderer.project("check", "type");
var areas = renderer._renderArea.selectAll(".area");
var area0 = d3.select(areas[0][0]);
assert.strictEqual(area0.attr("check"), "a", "projector has been applied to first area");
var area1 = d3.select(areas[0][1]);
assert.strictEqual(area1.attr("check"), "b", "projector has been applied to second area");
svg.remove();
});
});
});

Expand Down

0 comments on commit ad22586

Please sign in to comment.