| 1 |
Rickshaw.namespace("Rickshaw.Graph.Renderer"); |
| 2 |
|
| 3 |
Rickshaw.Graph.Renderer = Rickshaw.Class.create( { |
| 4 |
|
| 5 |
initialize: function(args) { |
| 6 |
this.graph = args.graph; |
| 7 |
this.tension = args.tension || this.tension; |
| 8 |
this.configure(args); |
| 9 |
}, |
| 10 |
|
| 11 |
seriesPathFactory: function() { |
| 12 |
//implement in subclass |
| 13 |
}, |
| 14 |
|
| 15 |
seriesStrokeFactory: function() { |
| 16 |
// implement in subclass |
| 17 |
}, |
| 18 |
|
| 19 |
defaults: function() { |
| 20 |
return { |
| 21 |
tension: 0.8, |
| 22 |
strokeWidth: 2, |
| 23 |
unstack: true, |
| 24 |
padding: { top: 0.01, right: 0, bottom: 0.01, left: 0 }, |
| 25 |
stroke: false, |
| 26 |
fill: false |
| 27 |
}; |
| 28 |
}, |
| 29 |
|
| 30 |
domain: function(data) { |
| 31 |
// Requires that at least one series contains some data |
| 32 |
var stackedData = data || this.graph.stackedData || this.graph.stackData(); |
| 33 |
|
| 34 |
var xMin = +Infinity; |
| 35 |
var xMax = -Infinity; |
| 36 |
|
| 37 |
var yMin = +Infinity; |
| 38 |
var yMax = -Infinity; |
| 39 |
|
| 40 |
stackedData.forEach( function(series) { |
| 41 |
|
| 42 |
series.forEach( function(d) { |
| 43 |
|
| 44 |
if (d.y == null) return; |
| 45 |
|
| 46 |
var y = d.y + d.y0; |
| 47 |
|
| 48 |
if (y < yMin) yMin = y; |
| 49 |
if (y > yMax) yMax = y; |
| 50 |
} ); |
| 51 |
|
| 52 |
if (!series.length) return; |
| 53 |
|
| 54 |
if (series[0].x < xMin) xMin = series[0].x; |
| 55 |
if (series[series.length - 1].x > xMax) xMax = series[series.length - 1].x; |
| 56 |
} ); |
| 57 |
|
| 58 |
xMin -= (xMax - xMin) * this.padding.left; |
| 59 |
xMax += (xMax - xMin) * this.padding.right; |
| 60 |
|
| 61 |
yMin = this.graph.min === 'auto' ? yMin : this.graph.min || 0; |
| 62 |
yMax = this.graph.max === undefined ? yMax : this.graph.max; |
| 63 |
|
| 64 |
if (this.graph.min === 'auto' || yMin < 0) { |
| 65 |
yMin -= (yMax - yMin) * this.padding.bottom; |
| 66 |
} |
| 67 |
|
| 68 |
if (this.graph.max === undefined) { |
| 69 |
yMax += (yMax - yMin) * this.padding.top; |
| 70 |
} |
| 71 |
|
| 72 |
return { x: [xMin, xMax], y: [yMin, yMax] }; |
| 73 |
}, |
| 74 |
|
| 75 |
render: function(args) { |
| 76 |
|
| 77 |
args = args || {}; |
| 78 |
|
| 79 |
var graph = this.graph; |
| 80 |
var series = args.series || graph.series; |
| 81 |
|
| 82 |
var vis = args.vis || graph.vis; |
| 83 |
vis.selectAll('*').remove(); |
| 84 |
|
| 85 |
var data = series |
| 86 |
.filter(function(s) { return !s.disabled }) |
| 87 |
.map(function(s) { return s.stack }); |
| 88 |
|
| 89 |
var pathNodes = vis.selectAll("path.path") |
| 90 |
.data(data) |
| 91 |
.enter().append("svg:path") |
| 92 |
.classed('path', true) |
| 93 |
.attr("d", this.seriesPathFactory()); |
| 94 |
|
| 95 |
if (this.stroke) { |
| 96 |
var strokeNodes = vis.selectAll('path.stroke') |
| 97 |
.data(data) |
| 98 |
.enter().append("svg:path") |
| 99 |
.classed('stroke', true) |
| 100 |
.attr("d", this.seriesStrokeFactory()); |
| 101 |
} |
| 102 |
|
| 103 |
var i = 0; |
| 104 |
series.forEach( function(series) { |
| 105 |
if (series.disabled) return; |
| 106 |
series.path = pathNodes[0][i]; |
| 107 |
if (this.stroke) series.stroke = strokeNodes[0][i]; |
| 108 |
this._styleSeries(series); |
| 109 |
i++; |
| 110 |
}, this ); |
| 111 |
|
| 112 |
}, |
| 113 |
|
| 114 |
_styleSeries: function(series) { |
| 115 |
|
| 116 |
var fill = this.fill ? series.color : 'none'; |
| 117 |
var stroke = this.stroke ? series.color : 'none'; |
| 118 |
|
| 119 |
series.path.setAttribute('fill', fill); |
| 120 |
series.path.setAttribute('stroke', stroke); |
| 121 |
series.path.setAttribute('stroke-width', this.strokeWidth); |
| 122 |
|
| 123 |
if (series.className) { |
| 124 |
d3.select(series.path).classed(series.className, true); |
| 125 |
} |
| 126 |
if (series.className && this.stroke) { |
| 127 |
d3.select(series.stroke).classed(series.className, true); |
| 128 |
} |
| 129 |
}, |
| 130 |
|
| 131 |
configure: function(args) { |
| 132 |
|
| 133 |
args = args || {}; |
| 134 |
|
| 135 |
Rickshaw.keys(this.defaults()).forEach( function(key) { |
| 136 |
|
| 137 |
if (!args.hasOwnProperty(key)) { |
| 138 |
this[key] = this[key] || this.graph[key] || this.defaults()[key]; |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
if (typeof this.defaults()[key] == 'object') { |
| 143 |
|
| 144 |
Rickshaw.keys(this.defaults()[key]).forEach( function(k) { |
| 145 |
|
| 146 |
this[key][k] = |
| 147 |
args[key][k] !== undefined ? args[key][k] : |
| 148 |
this[key][k] !== undefined ? this[key][k] : |
| 149 |
this.defaults()[key][k]; |
| 150 |
}, this ); |
| 151 |
|
| 152 |
} else { |
| 153 |
this[key] = |
| 154 |
args[key] !== undefined ? args[key] : |
| 155 |
this[key] !== undefined ? this[key] : |
| 156 |
this.graph[key] !== undefined ? this.graph[key] : |
| 157 |
this.defaults()[key]; |
| 158 |
} |
| 159 |
|
| 160 |
}, this ); |
| 161 |
}, |
| 162 |
|
| 163 |
setStrokeWidth: function(strokeWidth) { |
| 164 |
if (strokeWidth !== undefined) { |
| 165 |
this.strokeWidth = strokeWidth; |
| 166 |
} |
| 167 |
}, |
| 168 |
|
| 169 |
setTension: function(tension) { |
| 170 |
if (tension !== undefined) { |
| 171 |
this.tension = tension; |
| 172 |
} |
| 173 |
} |
| 174 |
} ); |
| 175 |
|