Skip to content

Commit

Permalink
invoke stroke factory in renderer parent class; closes #337; closes #338
Browse files Browse the repository at this point in the history
  • Loading branch information
dchester committed Dec 2, 2013
1 parent 958c043 commit 5fc0869
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/js/Rickshaw.Graph.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,29 @@ Rickshaw.Graph.Renderer = Rickshaw.Class.create( {
.filter(function(s) { return !s.disabled })
.map(function(s) { return s.stack });

var nodes = vis.selectAll("path")
var pathNodes = vis.selectAll("path.path")
.data(data)
.enter().append("svg:path")
.classed('path', true)
.attr("d", this.seriesPathFactory());

if (this.stroke) {
var strokeNodes = vis.selectAll('path.stroke')
.data(data)
.enter().append("svg:path")
.classed('stroke', true)
.attr("d", this.seriesStrokeFactory());
}

var i = 0;
series.forEach( function(series) {
if (series.disabled) return;
series.path = nodes[0][i++];
series.path = pathNodes[0][i];
if (this.stroke) series.stroke = strokeNodes[0][i];
this._styleSeries(series);
i++;
}, this );

},

_styleSeries: function(series) {
Expand All @@ -112,7 +124,13 @@ Rickshaw.Graph.Renderer = Rickshaw.Class.create( {
series.path.setAttribute('fill', fill);
series.path.setAttribute('stroke', stroke);
series.path.setAttribute('stroke-width', this.strokeWidth);
series.path.setAttribute('class', series.className);

if (series.className) {
d3.select(series.path).classed(series.className, true);
}
if (series.className && this.stroke) {
d3.select(series.stroke).classed(series.className, true);
}
},

configure: function(args) {
Expand Down
65 changes: 65 additions & 0 deletions tests/Rickshaw.Graph.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,68 @@ exports.domain = function(test) {

test.done();
};


exports.respectStrokeFactory = function(test) {

var el = document.createElement("div");

Rickshaw.Graph.Renderer.RespectStrokeFactory = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {

name: 'respectStrokeFactory',

seriesPathFactory: function() {
var graph = this.graph;
var factory = d3.svg.line()
.x( function(d) { return graph.x(d.x) } )
.y( function(d) { return graph.y(d.y + d.y0) } )
.interpolate(graph.interpolation).tension(this.tension);
factory.defined && factory.defined( function(d) { return d.y !== null } );
return factory;
},

seriesStrokeFactory: function() {
var graph = this.graph;
var factory = d3.svg.line()
.x( function(d) { return graph.x(d.x) } )
.y( function(d) { return graph.y(d.y + d.y0) } )
.interpolate(graph.interpolation).tension(this.tension);
factory.defined && factory.defined( function(d) { return d.y !== null } );
return factory;
}
});

var graph = new Rickshaw.Graph({
element: el,
stroke: true,
width: 10,
height: 10,
renderer: 'respectStrokeFactory',
series: [
{
className: 'fnord',
data: [
{ x: 0, y: 40 },
{ x: 1, y: 49 },
{ x: 2, y: 38 },
{ x: 3, y: 30 },
{ x: 4, y: 32 }
]
}
]
});
graph.render();

var path = graph.vis.select('path.path.fnord');
test.equals(path.size(), 1, "we have a fnord path");

var stroke = graph.vis.select('path.stroke.fnord');
test.equals(stroke.size(), 1, "we have a fnord stroke");

// should also be availeable via series
var firstSeries = graph.series[0];
test.ok(d3.select(firstSeries.path).classed('path'), "selectable path");
test.ok(d3.select(firstSeries.stroke).classed('stroke', "selectable stroke"));

test.done();
};

0 comments on commit 5fc0869

Please sign in to comment.