| 1 |
Rickshaw.namespace('Rickshaw.Graph.Renderer.Area'); |
| 2 |
|
| 3 |
Rickshaw.Graph.Renderer.Area = Rickshaw.Class.create( Rickshaw.Graph.Renderer, { |
| 4 |
|
| 5 |
name: 'area', |
| 6 |
|
| 7 |
defaults: function($super) { |
| 8 |
|
| 9 |
return Rickshaw.extend( $super(), { |
| 10 |
unstack: false, |
| 11 |
fill: false, |
| 12 |
stroke: false |
| 13 |
} ); |
| 14 |
}, |
| 15 |
|
| 16 |
seriesPathFactory: function() { |
| 17 |
|
| 18 |
var graph = this.graph; |
| 19 |
|
| 20 |
var factory = d3.svg.area() |
| 21 |
.x( function(d) { return graph.x(d.x) } ) |
| 22 |
.y0( function(d) { return graph.y(d.y0) } ) |
| 23 |
.y1( function(d) { return graph.y(d.y + d.y0) } ) |
| 24 |
.interpolate(graph.interpolation).tension(this.tension); |
| 25 |
|
| 26 |
factory.defined && factory.defined( function(d) { return d.y !== null } ); |
| 27 |
return factory; |
| 28 |
}, |
| 29 |
|
| 30 |
seriesStrokeFactory: function() { |
| 31 |
|
| 32 |
var graph = this.graph; |
| 33 |
|
| 34 |
var factory = d3.svg.line() |
| 35 |
.x( function(d) { return graph.x(d.x) } ) |
| 36 |
.y( function(d) { return graph.y(d.y + d.y0) } ) |
| 37 |
.interpolate(graph.interpolation).tension(this.tension); |
| 38 |
|
| 39 |
factory.defined && factory.defined( function(d) { return d.y !== null } ); |
| 40 |
return factory; |
| 41 |
}, |
| 42 |
|
| 43 |
render: function(args) { |
| 44 |
|
| 45 |
args = args || {}; |
| 46 |
|
| 47 |
var graph = this.graph; |
| 48 |
var series = args.series || graph.series; |
| 49 |
|
| 50 |
var vis = args.vis || graph.vis; |
| 51 |
vis.selectAll('*').remove(); |
| 52 |
|
| 53 |
// insert or stacked areas so strokes lay on top of areas |
| 54 |
var method = this.unstack ? 'append' : 'insert'; |
| 55 |
|
| 56 |
var data = series |
| 57 |
.filter(function(s) { return !s.disabled }) |
| 58 |
.map(function(s) { return s.stack }); |
| 59 |
|
| 60 |
var nodes = vis.selectAll("path") |
| 61 |
.data(data) |
| 62 |
.enter()[method]("svg:g", 'g'); |
| 63 |
|
| 64 |
nodes.append("svg:path") |
| 65 |
.attr("d", this.seriesPathFactory()) |
| 66 |
.attr("class", 'area'); |
| 67 |
|
| 68 |
if (this.stroke) { |
| 69 |
nodes.append("svg:path") |
| 70 |
.attr("d", this.seriesStrokeFactory()) |
| 71 |
.attr("class", 'line'); |
| 72 |
} |
| 73 |
|
| 74 |
var i = 0; |
| 75 |
series.forEach( function(series) { |
| 76 |
if (series.disabled) return; |
| 77 |
series.path = nodes[0][i++]; |
| 78 |
this._styleSeries(series); |
| 79 |
}, this ); |
| 80 |
}, |
| 81 |
|
| 82 |
_styleSeries: function(series) { |
| 83 |
|
| 84 |
if (!series.path) return; |
| 85 |
|
| 86 |
d3.select(series.path).select('.area') |
| 87 |
.attr('fill', series.color); |
| 88 |
|
| 89 |
if (this.stroke) { |
| 90 |
d3.select(series.path).select('.line') |
| 91 |
.attr('fill', 'none') |
| 92 |
.attr('stroke', series.stroke || d3.interpolateRgb(series.color, 'black')(0.125)) |
| 93 |
.attr('stroke-width', this.strokeWidth); |
| 94 |
} |
| 95 |
|
| 96 |
if (series.className) { |
| 97 |
series.path.setAttribute('class', series.className); |
| 98 |
} |
| 99 |
} |
| 100 |
} ); |
| 101 |
|