2 nv.models.stackedArea = function() {
4 //============================================================
5 // Public Variables with Default Settings
6 //------------------------------------------------------------
8 var margin = {top: 0, right: 0, bottom: 0, left: 0}
11 , color = nv.utils.defaultColor() // a function that computes the color
12 , id = Math.floor(Math.random() * 100000) //Create semi-unique ID incase user doesn't selet one
13 , getX = function(d) { return d.x } // accessor to get the x value from a data point
14 , getY = function(d) { return d.y } // accessor to get the y value from a data point
18 , interpolate = 'linear' // controls the line interpolation
19 , clipEdge = false // if true, masks lines within x and y scale
20 , x //can be accessed via chart.xScale()
21 , y //can be accessed via chart.yScale()
22 , scatter = nv.models.scatter()
23 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'areaClick', 'areaMouseover', 'areaMouseout')
27 .size(2.2) // default size
28 .sizeDomain([2.2,2.2]) // all the same size by default
31 /************************************
35 * 'expand' (normalize to 100%)
36 * 'silhouette' (simple centered)
39 * 'inside-out' (stream)
40 * 'default' (input order)
41 ************************************/
43 //============================================================
46 function chart(selection) {
47 selection.each(function(data) {
48 var availableWidth = width - margin.left - margin.right,
49 availableHeight = height - margin.top - margin.bottom,
50 container = d3.select(this);
52 //------------------------------------------------------------
58 //------------------------------------------------------------
61 // Injecting point index into each point because d3.layout.stack().out does not give index
62 data.forEach(function(aseries, i) {
63 aseries.seriesIndex = i;
64 aseries.values = aseries.values.map(function(d, j) {
71 var dataFiltered = data.filter(function(series) {
72 return !series.disabled;
75 data = d3.layout.stack()
78 .values(function(d) { return d.values }) //TODO: make values customizeable in EVERY model in this fashion
81 .out(function(d, y0, y) {
82 var yHeight = (getY(d) === 0) ? 0 : y;
91 //------------------------------------------------------------
92 // Setup containers and skeleton of chart
94 var wrap = container.selectAll('g.nv-wrap.nv-stackedarea').data([data]);
95 var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-stackedarea');
96 var defsEnter = wrapEnter.append('defs');
97 var gEnter = wrapEnter.append('g');
98 var g = wrap.select('g');
100 gEnter.append('g').attr('class', 'nv-areaWrap');
101 gEnter.append('g').attr('class', 'nv-scatterWrap');
103 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
105 //------------------------------------------------------------
109 .width(availableWidth)
110 .height(availableHeight)
112 .y(function(d) { return d.display.y + d.display.y0 })
114 .color(data.map(function(d,i) {
115 return d.color || color(d, d.seriesIndex);
119 var scatterWrap = g.select('.nv-scatterWrap')
122 scatterWrap.call(scatter);
124 defsEnter.append('clipPath')
125 .attr('id', 'nv-edge-clip-' + id)
128 wrap.select('#nv-edge-clip-' + id + ' rect')
129 .attr('width', availableWidth)
130 .attr('height', availableHeight);
132 g .attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
134 var area = d3.svg.area()
135 .x(function(d,i) { return x(getX(d,i)) })
137 return y(d.display.y0)
140 return y(d.display.y + d.display.y0)
142 .interpolate(interpolate);
144 var zeroArea = d3.svg.area()
145 .x(function(d,i) { return x(getX(d,i)) })
146 .y0(function(d) { return y(d.display.y0) })
147 .y1(function(d) { return y(d.display.y0) });
150 var path = g.select('.nv-areaWrap').selectAll('path.nv-area')
151 .data(function(d) { return d });
153 path.enter().append('path').attr('class', function(d,i) { return 'nv-area nv-area-' + i })
154 .attr('d', function(d,i){
155 return zeroArea(d.values, d.seriesIndex);
157 .on('mouseover', function(d,i) {
158 d3.select(this).classed('hover', true);
159 dispatch.areaMouseover({
162 pos: [d3.event.pageX, d3.event.pageY],
166 .on('mouseout', function(d,i) {
167 d3.select(this).classed('hover', false);
168 dispatch.areaMouseout({
171 pos: [d3.event.pageX, d3.event.pageY],
175 .on('click', function(d,i) {
176 d3.select(this).classed('hover', false);
180 pos: [d3.event.pageX, d3.event.pageY],
184 path.exit().transition()
185 .attr('d', function(d,i) { return zeroArea(d.values,i) })
188 .style('fill', function(d,i){
189 return d.color || color(d, d.seriesIndex)
191 .style('stroke', function(d,i){ return d.color || color(d, d.seriesIndex) });
193 .attr('d', function(d,i) {
194 return area(d.values,i)
199 //============================================================
200 // Event Handling/Dispatching (in chart's scope)
201 //------------------------------------------------------------
203 scatter.dispatch.on('elementMouseover.area', function(e) {
204 g.select('.nv-chart-' + id + ' .nv-area-' + e.seriesIndex).classed('hover', true);
206 scatter.dispatch.on('elementMouseout.area', function(e) {
207 g.select('.nv-chart-' + id + ' .nv-area-' + e.seriesIndex).classed('hover', false);
210 //============================================================
211 //Special offset functions
212 chart.d3_stackedOffset_stackPercent = function(stackData) {
213 var n = stackData.length, //How many series
214 m = stackData[0].length, //how many points per series
221 for (j = 0; j < m; ++j) { //Looping through all points
222 for (i = 0, o = 0; i < dataRaw.length; i++) //looping through series'
223 o += getY(dataRaw[i].values[j]) //total value of all points at a certian point in time.
225 if (o) for (i = 0; i < n; i++)
226 stackData[i][j][1] /= o;
228 for (i = 0; i < n; i++)
229 stackData[i][j][1] = k;
231 for (j = 0; j < m; ++j) y0[j] = 0;
242 //============================================================
243 // Event Handling/Dispatching (out of chart's scope)
244 //------------------------------------------------------------
246 scatter.dispatch.on('elementClick.area', function(e) {
247 dispatch.areaClick(e);
249 scatter.dispatch.on('elementMouseover.tooltip', function(e) {
250 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top],
251 dispatch.tooltipShow(e);
253 scatter.dispatch.on('elementMouseout.tooltip', function(e) {
254 dispatch.tooltipHide(e);
257 //============================================================
259 //============================================================
260 // Global getters and setters
261 //------------------------------------------------------------
263 chart.dispatch = dispatch;
264 chart.scatter = scatter;
266 d3.rebind(chart, scatter, 'interactive', 'size', 'xScale', 'yScale', 'zScale', 'xDomain', 'yDomain', 'xRange', 'yRange',
267 'sizeDomain', 'forceX', 'forceY', 'forceSize', 'clipVoronoi', 'useVoronoi','clipRadius','highlightPoint','clearHighlights');
269 chart.options = nv.utils.optionsFunc.bind(chart);
271 chart.x = function(_) {
272 if (!arguments.length) return getX;
273 getX = d3.functor(_);
277 chart.y = function(_) {
278 if (!arguments.length) return getY;
279 getY = d3.functor(_);
283 chart.margin = function(_) {
284 if (!arguments.length) return margin;
285 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
286 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
287 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
288 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
292 chart.width = function(_) {
293 if (!arguments.length) return width;
298 chart.height = function(_) {
299 if (!arguments.length) return height;
304 chart.clipEdge = function(_) {
305 if (!arguments.length) return clipEdge;
310 chart.color = function(_) {
311 if (!arguments.length) return color;
312 color = nv.utils.getColor(_);
316 chart.offset = function(_) {
317 if (!arguments.length) return offset;
322 chart.order = function(_) {
323 if (!arguments.length) return order;
328 //shortcut for offset + order
329 chart.style = function(_) {
330 if (!arguments.length) return style;
335 chart.offset('zero');
336 chart.order('default');
339 chart.offset('wiggle');
340 chart.order('inside-out');
342 case 'stream-center':
343 chart.offset('silhouette');
344 chart.order('inside-out');
347 chart.offset('expand');
348 chart.order('default');
350 case 'stack_percent':
351 chart.offset(chart.d3_stackedOffset_stackPercent);
352 chart.order('default');
359 chart.interpolate = function(_) {
360 if (!arguments.length) return interpolate;
364 //============================================================