1 nv.models.multiChart = function() {
3 //============================================================
4 // Public Variables with Default Settings
5 //------------------------------------------------------------
7 var margin = {top: 30, right: 20, bottom: 50, left: 60},
8 color = d3.scale.category20().range(),
13 tooltip = function(key, x, y, e, graph) {
14 return '<h3>' + key + '</h3>' +
15 '<p>' + y + ' at ' + x + '</p>'
21 ; //can be accessed via chart.lines.[x/y]Scale()
23 //============================================================
25 //------------------------------------------------------------
27 var x = d3.scale.linear(),
28 yScale1 = d3.scale.linear(),
29 yScale2 = d3.scale.linear(),
31 lines1 = nv.models.line().yScale(yScale1),
32 lines2 = nv.models.line().yScale(yScale2),
34 bars1 = nv.models.multiBar().stacked(false).yScale(yScale1),
35 bars2 = nv.models.multiBar().stacked(false).yScale(yScale2),
37 stack1 = nv.models.stackedArea().yScale(yScale1),
38 stack2 = nv.models.stackedArea().yScale(yScale2),
40 xAxis = nv.models.axis().scale(x).orient('bottom').tickPadding(5),
41 yAxis1 = nv.models.axis().scale(yScale1).orient('left'),
42 yAxis2 = nv.models.axis().scale(yScale2).orient('right'),
44 legend = nv.models.legend().height(30),
45 dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
47 var showTooltip = function(e, offsetElement) {
48 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
49 top = e.pos[1] + ( offsetElement.offsetTop || 0),
50 x = xAxis.tickFormat()(lines1.x()(e.point, e.pointIndex)),
51 y = ((e.series.yAxis == 2) ? yAxis2 : yAxis1).tickFormat()(lines1.y()(e.point, e.pointIndex)),
52 content = tooltip(e.series.key, x, y, e, chart);
54 nv.tooltip.show([left, top], content, undefined, undefined, offsetElement.offsetParent);
57 function chart(selection) {
58 selection.each(function(data) {
59 var container = d3.select(this),
62 chart.update = function() { container.transition().call(chart); };
63 chart.container = this;
65 var availableWidth = (width || parseInt(container.style('width')) || 960)
66 - margin.left - margin.right,
67 availableHeight = (height || parseInt(container.style('height')) || 400)
68 - margin.top - margin.bottom;
70 var dataLines1 = data.filter(function(d) {return !d.disabled && d.type == 'line' && d.yAxis == 1})
71 var dataLines2 = data.filter(function(d) {return !d.disabled && d.type == 'line' && d.yAxis == 2})
72 var dataBars1 = data.filter(function(d) {return !d.disabled && d.type == 'bar' && d.yAxis == 1})
73 var dataBars2 = data.filter(function(d) {return !d.disabled && d.type == 'bar' && d.yAxis == 2})
74 var dataStack1 = data.filter(function(d) {return !d.disabled && d.type == 'area' && d.yAxis == 1})
75 var dataStack2 = data.filter(function(d) {return !d.disabled && d.type == 'area' && d.yAxis == 2})
77 var series1 = data.filter(function(d) {return !d.disabled && d.yAxis == 1})
79 return d.values.map(function(d,i) {
80 return { x: d.x, y: d.y }
84 var series2 = data.filter(function(d) {return !d.disabled && d.yAxis == 2})
86 return d.values.map(function(d,i) {
87 return { x: d.x, y: d.y }
91 x .domain(d3.extent(d3.merge(series1.concat(series2)), function(d) { return d.x } ))
92 .range([0, availableWidth]);
94 var wrap = container.selectAll('g.wrap.multiChart').data([data]);
95 var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 multiChart').append('g');
97 gEnter.append('g').attr('class', 'x axis');
98 gEnter.append('g').attr('class', 'y1 axis');
99 gEnter.append('g').attr('class', 'y2 axis');
100 gEnter.append('g').attr('class', 'lines1Wrap');
101 gEnter.append('g').attr('class', 'lines2Wrap');
102 gEnter.append('g').attr('class', 'bars1Wrap');
103 gEnter.append('g').attr('class', 'bars2Wrap');
104 gEnter.append('g').attr('class', 'stack1Wrap');
105 gEnter.append('g').attr('class', 'stack2Wrap');
106 gEnter.append('g').attr('class', 'legendWrap');
108 var g = wrap.select('g');
111 legend.width( availableWidth / 2 );
113 g.select('.legendWrap')
114 .datum(data.map(function(series) {
115 series.originalKey = series.originalKey === undefined ? series.key : series.originalKey;
116 series.key = series.originalKey + (series.yAxis == 1 ? '' : ' (right axis)');
121 if ( margin.top != legend.height()) {
122 margin.top = legend.height();
123 availableHeight = (height || parseInt(container.style('height')) || 400)
124 - margin.top - margin.bottom;
127 g.select('.legendWrap')
128 .attr('transform', 'translate(' + ( availableWidth / 2 ) + ',' + (-margin.top) +')');
133 .width(availableWidth)
134 .height(availableHeight)
135 .interpolate("monotone")
136 .color(data.map(function(d,i) {
137 return d.color || color[i % color.length];
138 }).filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 1 && data[i].type == 'line'}));
141 .width(availableWidth)
142 .height(availableHeight)
143 .interpolate("monotone")
144 .color(data.map(function(d,i) {
145 return d.color || color[i % color.length];
146 }).filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 2 && data[i].type == 'line'}));
149 .width(availableWidth)
150 .height(availableHeight)
151 .color(data.map(function(d,i) {
152 return d.color || color[i % color.length];
153 }).filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 1 && data[i].type == 'bar'}));
156 .width(availableWidth)
157 .height(availableHeight)
158 .color(data.map(function(d,i) {
159 return d.color || color[i % color.length];
160 }).filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 2 && data[i].type == 'bar'}));
163 .width(availableWidth)
164 .height(availableHeight)
165 .color(data.map(function(d,i) {
166 return d.color || color[i % color.length];
167 }).filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 1 && data[i].type == 'area'}));
170 .width(availableWidth)
171 .height(availableHeight)
172 .color(data.map(function(d,i) {
173 return d.color || color[i % color.length];
174 }).filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 2 && data[i].type == 'area'}));
176 g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
179 var lines1Wrap = g.select('.lines1Wrap')
181 var bars1Wrap = g.select('.bars1Wrap')
183 var stack1Wrap = g.select('.stack1Wrap')
186 var lines2Wrap = g.select('.lines2Wrap')
188 var bars2Wrap = g.select('.bars2Wrap')
190 var stack2Wrap = g.select('.stack2Wrap')
193 var extraValue1 = dataStack1.length ? dataStack1.map(function(a){return a.values}).reduce(function(a,b){
194 return a.map(function(aVal,i){return {x: aVal.x, y: aVal.y + b[i].y}})
195 }).concat([{x:0, y:0}]) : []
196 var extraValue2 = dataStack2.length ? dataStack2.map(function(a){return a.values}).reduce(function(a,b){
197 return a.map(function(aVal,i){return {x: aVal.x, y: aVal.y + b[i].y}})
198 }).concat([{x:0, y:0}]) : []
200 yScale1 .domain(yDomain1 || d3.extent(d3.merge(series1).concat(extraValue1), function(d) { return d.y } ))
201 .range([0, availableHeight])
203 yScale2 .domain(yDomain2 || d3.extent(d3.merge(series2).concat(extraValue2), function(d) { return d.y } ))
204 .range([0, availableHeight])
206 lines1.yDomain(yScale1.domain())
207 bars1.yDomain(yScale1.domain())
208 stack1.yDomain(yScale1.domain())
210 lines2.yDomain(yScale2.domain())
211 bars2.yDomain(yScale2.domain())
212 stack2.yDomain(yScale2.domain())
214 if(dataStack1.length){d3.transition(stack1Wrap).call(stack1);}
215 if(dataStack2.length){d3.transition(stack2Wrap).call(stack2);}
217 if(dataBars1.length){d3.transition(bars1Wrap).call(bars1);}
218 if(dataBars2.length){d3.transition(bars2Wrap).call(bars2);}
220 if(dataLines1.length){d3.transition(lines1Wrap).call(lines1);}
221 if(dataLines2.length){d3.transition(lines2Wrap).call(lines2);}
226 .ticks( availableWidth / 100 )
227 .tickSize(-availableHeight, 0);
230 .attr('transform', 'translate(0,' + availableHeight + ')');
231 d3.transition(g.select('.x.axis'))
235 .ticks( availableHeight / 36 )
236 .tickSize( -availableWidth, 0);
239 d3.transition(g.select('.y1.axis'))
243 .ticks( availableHeight / 36 )
244 .tickSize( -availableWidth, 0);
246 d3.transition(g.select('.y2.axis'))
250 .style('opacity', series2.length ? 1 : 0)
251 .attr('transform', 'translate(' + x.range()[1] + ',0)');
253 legend.dispatch.on('stateChange', function(newState) {
257 dispatch.on('tooltipShow', function(e) {
258 if (tooltips) showTooltip(e, that.parentNode);
267 //============================================================
268 // Event Handling/Dispatching (out of chart's scope)
269 //------------------------------------------------------------
271 lines1.dispatch.on('elementMouseover.tooltip', function(e) {
272 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
273 dispatch.tooltipShow(e);
276 lines1.dispatch.on('elementMouseout.tooltip', function(e) {
277 dispatch.tooltipHide(e);
280 lines2.dispatch.on('elementMouseover.tooltip', function(e) {
281 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
282 dispatch.tooltipShow(e);
285 lines2.dispatch.on('elementMouseout.tooltip', function(e) {
286 dispatch.tooltipHide(e);
289 bars1.dispatch.on('elementMouseover.tooltip', function(e) {
290 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
291 dispatch.tooltipShow(e);
294 bars1.dispatch.on('elementMouseout.tooltip', function(e) {
295 dispatch.tooltipHide(e);
298 bars2.dispatch.on('elementMouseover.tooltip', function(e) {
299 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
300 dispatch.tooltipShow(e);
303 bars2.dispatch.on('elementMouseout.tooltip', function(e) {
304 dispatch.tooltipHide(e);
307 stack1.dispatch.on('tooltipShow', function(e) {
308 //disable tooltips when value ~= 0
309 //// TODO: consider removing points from voronoi that have 0 value instead of this hack
310 if (!Math.round(stack1.y()(e.point) * 100)) { // 100 will not be good for very small numbers... will have to think about making this valu dynamic, based on data range
311 setTimeout(function() { d3.selectAll('.point.hover').classed('hover', false) }, 0);
315 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top],
316 dispatch.tooltipShow(e);
319 stack1.dispatch.on('tooltipHide', function(e) {
320 dispatch.tooltipHide(e);
323 stack2.dispatch.on('tooltipShow', function(e) {
324 //disable tooltips when value ~= 0
325 //// TODO: consider removing points from voronoi that have 0 value instead of this hack
326 if (!Math.round(stack2.y()(e.point) * 100)) { // 100 will not be good for very small numbers... will have to think about making this valu dynamic, based on data range
327 setTimeout(function() { d3.selectAll('.point.hover').classed('hover', false) }, 0);
331 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top],
332 dispatch.tooltipShow(e);
335 stack2.dispatch.on('tooltipHide', function(e) {
336 dispatch.tooltipHide(e);
339 lines1.dispatch.on('elementMouseover.tooltip', function(e) {
340 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
341 dispatch.tooltipShow(e);
344 lines1.dispatch.on('elementMouseout.tooltip', function(e) {
345 dispatch.tooltipHide(e);
348 lines2.dispatch.on('elementMouseover.tooltip', function(e) {
349 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
350 dispatch.tooltipShow(e);
353 lines2.dispatch.on('elementMouseout.tooltip', function(e) {
354 dispatch.tooltipHide(e);
357 dispatch.on('tooltipHide', function() {
358 if (tooltips) nv.tooltip.cleanup();
363 //============================================================
364 // Global getters and setters
365 //------------------------------------------------------------
367 chart.dispatch = dispatch;
368 chart.lines1 = lines1;
369 chart.lines2 = lines2;
372 chart.stack1 = stack1;
373 chart.stack2 = stack2;
375 chart.yAxis1 = yAxis1;
376 chart.yAxis2 = yAxis2;
377 chart.options = nv.utils.optionsFunc.bind(chart);
379 chart.x = function(_) {
380 if (!arguments.length) return getX;
387 chart.y = function(_) {
388 if (!arguments.length) return getY;
395 chart.yDomain1 = function(_) {
396 if (!arguments.length) return yDomain1;
401 chart.yDomain2 = function(_) {
402 if (!arguments.length) return yDomain2;
407 chart.margin = function(_) {
408 if (!arguments.length) return margin;
413 chart.width = function(_) {
414 if (!arguments.length) return width;
419 chart.height = function(_) {
420 if (!arguments.length) return height;
425 chart.color = function(_) {
426 if (!arguments.length) return color;
432 chart.showLegend = function(_) {
433 if (!arguments.length) return showLegend;
438 chart.tooltips = function(_) {
439 if (!arguments.length) return tooltips;
444 chart.tooltipContent = function(_) {
445 if (!arguments.length) return tooltip;