2 nv.models.linePlusBarWithFocusChart = function() {
4 //============================================================
5 // Public Variables with Default Settings
6 //------------------------------------------------------------
8 var lines = nv.models.line()
9 , lines2 = nv.models.line()
10 , bars = nv.models.historicalBar()
11 , bars2 = nv.models.historicalBar()
12 , xAxis = nv.models.axis()
13 , x2Axis = nv.models.axis()
14 , y1Axis = nv.models.axis()
15 , y2Axis = nv.models.axis()
16 , y3Axis = nv.models.axis()
17 , y4Axis = nv.models.axis()
18 , legend = nv.models.legend()
19 , brush = d3.svg.brush()
22 var margin = {top: 30, right: 30, bottom: 30, left: 60}
23 , margin2 = {top: 0, right: 30, bottom: 20, left: 60}
27 , getX = function(d) { return d.x }
28 , getY = function(d) { return d.y }
29 , color = nv.utils.defaultColor()
34 , tooltip = function(key, x, y, e, graph) {
35 return '<h3>' + key + '</h3>' +
36 '<p>' + y + ' at ' + x + '</p>';
44 , noData = "No Data Available."
45 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'brush')
46 , transitionDuration = 0
76 //============================================================
79 //============================================================
81 //------------------------------------------------------------
83 var showTooltip = function(e, offsetElement) {
85 e.pointIndex += Math.ceil(extent[0]);
87 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
88 top = e.pos[1] + ( offsetElement.offsetTop || 0),
89 x = xAxis.tickFormat()(lines.x()(e.point, e.pointIndex)),
90 y = (e.series.bar ? y1Axis : y2Axis).tickFormat()(lines.y()(e.point, e.pointIndex)),
91 content = tooltip(e.series.key, x, y, e, chart);
93 nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
96 //------------------------------------------------------------
100 function chart(selection) {
101 selection.each(function(data) {
102 var container = d3.select(this),
105 var availableWidth = (width || parseInt(container.style('width')) || 960)
106 - margin.left - margin.right,
107 availableHeight1 = (height || parseInt(container.style('height')) || 400)
108 - margin.top - margin.bottom - height2,
109 availableHeight2 = height2 - margin2.top - margin2.bottom;
111 chart.update = function() { container.transition().duration(transitionDuration).call(chart); };
112 chart.container = this;
115 //------------------------------------------------------------
116 // Display No Data message if there's nothing to show.
118 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
119 var noDataText = container.selectAll('.nv-noData').data([noData]);
121 noDataText.enter().append('text')
122 .attr('class', 'nvd3 nv-noData')
124 .style('text-anchor', 'middle');
127 .attr('x', margin.left + availableWidth / 2)
128 .attr('y', margin.top + availableHeight1 / 2)
129 .text(function(d) { return d });
133 container.selectAll('.nv-noData').remove();
136 //------------------------------------------------------------
139 //------------------------------------------------------------
142 var dataBars = data.filter(function(d) { return !d.disabled && d.bar });
143 var dataLines = data.filter(function(d) { return !d.bar }); // removed the !d.disabled clause here to fix Issue #240
150 y4 = lines2.yScale();
153 .filter(function(d) { return !d.disabled && d.bar })
155 return d.values.map(function(d,i) {
156 return { x: getX(d,i), y: getY(d,i) }
161 .filter(function(d) { return !d.disabled && !d.bar })
163 return d.values.map(function(d,i) {
164 return { x: getX(d,i), y: getY(d,i) }
168 x .range([0, availableWidth]);
170 x2 .domain(d3.extent(d3.merge(series1.concat(series2)), function(d) { return d.x } ))
171 .range([0, availableWidth]);
174 //------------------------------------------------------------
177 //------------------------------------------------------------
178 // Setup containers and skeleton of chart
180 var wrap = container.selectAll('g.nv-wrap.nv-linePlusBar').data([data]);
181 var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-linePlusBar').append('g');
182 var g = wrap.select('g');
184 gEnter.append('g').attr('class', 'nv-legendWrap');
186 var focusEnter = gEnter.append('g').attr('class', 'nv-focus');
187 focusEnter.append('g').attr('class', 'nv-x nv-axis');
188 focusEnter.append('g').attr('class', 'nv-y1 nv-axis');
189 focusEnter.append('g').attr('class', 'nv-y2 nv-axis');
190 focusEnter.append('g').attr('class', 'nv-barsWrap');
191 focusEnter.append('g').attr('class', 'nv-linesWrap');
193 var contextEnter = gEnter.append('g').attr('class', 'nv-context');
194 contextEnter.append('g').attr('class', 'nv-x nv-axis');
195 contextEnter.append('g').attr('class', 'nv-y1 nv-axis');
196 contextEnter.append('g').attr('class', 'nv-y2 nv-axis');
197 contextEnter.append('g').attr('class', 'nv-barsWrap');
198 contextEnter.append('g').attr('class', 'nv-linesWrap');
199 contextEnter.append('g').attr('class', 'nv-brushBackground');
200 contextEnter.append('g').attr('class', 'nv-x nv-brush');
203 //------------------------------------------------------------
206 //------------------------------------------------------------
210 legend.width( availableWidth / 2 );
212 g.select('.nv-legendWrap')
213 .datum(data.map(function(series) {
214 series.originalKey = series.originalKey === undefined ? series.key : series.originalKey;
215 series.key = series.originalKey + (series.bar ? ' (left axis)' : ' (right axis)');
220 if ( margin.top != legend.height()) {
221 margin.top = legend.height();
222 availableHeight1 = (height || parseInt(container.style('height')) || 400)
223 - margin.top - margin.bottom - height2;
226 g.select('.nv-legendWrap')
227 .attr('transform', 'translate(' + ( availableWidth / 2 ) + ',' + (-margin.top) +')');
230 //------------------------------------------------------------
233 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
236 //------------------------------------------------------------
237 // Context Components
240 .width(availableWidth)
241 .height(availableHeight2)
242 .color(data.map(function(d,i) {
243 return d.color || color(d, i);
244 }).filter(function(d,i) { return !data[i].disabled && data[i].bar }));
247 .width(availableWidth)
248 .height(availableHeight2)
249 .color(data.map(function(d,i) {
250 return d.color || color(d, i);
251 }).filter(function(d,i) { return !data[i].disabled && !data[i].bar }));
253 var bars2Wrap = g.select('.nv-context .nv-barsWrap')
254 .datum(dataBars.length ? dataBars : [{values:[]}]);
256 var lines2Wrap = g.select('.nv-context .nv-linesWrap')
257 .datum(!dataLines[0].disabled ? dataLines : [{values:[]}]);
259 g.select('.nv-context')
260 .attr('transform', 'translate(0,' + ( availableHeight1 + margin.bottom + margin2.top) + ')')
262 bars2Wrap.transition().call(bars2);
263 lines2Wrap.transition().call(lines2);
265 //------------------------------------------------------------
269 //------------------------------------------------------------
274 .on('brush', onBrush);
276 if (brushExtent) brush.extent(brushExtent);
278 var brushBG = g.select('.nv-brushBackground').selectAll('g')
279 .data([brushExtent || brush.extent()])
281 var brushBGenter = brushBG.enter()
284 brushBGenter.append('rect')
285 .attr('class', 'left')
288 .attr('height', availableHeight2);
290 brushBGenter.append('rect')
291 .attr('class', 'right')
294 .attr('height', availableHeight2);
296 var gBrush = g.select('.nv-x.nv-brush')
298 gBrush.selectAll('rect')
300 .attr('height', availableHeight2);
301 gBrush.selectAll('.resize').append('path').attr('d', resizePath);
303 //------------------------------------------------------------
305 //------------------------------------------------------------
306 // Setup Secondary (Context) Axes
309 .ticks( availableWidth / 100 )
310 .tickSize(-availableHeight2, 0);
312 g.select('.nv-context .nv-x.nv-axis')
313 .attr('transform', 'translate(0,' + y3.range()[0] + ')');
314 g.select('.nv-context .nv-x.nv-axis').transition()
320 .ticks( availableHeight2 / 36 )
321 .tickSize( -availableWidth, 0);
323 g.select('.nv-context .nv-y1.nv-axis')
324 .style('opacity', dataBars.length ? 1 : 0)
325 .attr('transform', 'translate(0,' + x2.range()[0] + ')');
327 g.select('.nv-context .nv-y1.nv-axis').transition()
333 .ticks( availableHeight2 / 36 )
334 .tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none
336 g.select('.nv-context .nv-y2.nv-axis')
337 .style('opacity', dataLines.length ? 1 : 0)
338 .attr('transform', 'translate(' + x2.range()[1] + ',0)');
340 g.select('.nv-context .nv-y2.nv-axis').transition()
343 //------------------------------------------------------------
345 //============================================================
346 // Event Handling/Dispatching (in chart's scope)
347 //------------------------------------------------------------
349 legend.dispatch.on('stateChange', function(newState) {
353 dispatch.on('tooltipShow', function(e) {
354 if (tooltips) showTooltip(e, that.parentNode);
357 //============================================================
360 //============================================================
362 //------------------------------------------------------------
364 // Taken from crossfilter (http://square.github.com/crossfilter/)
365 function resizePath(d) {
368 y = availableHeight2 / 3;
369 return 'M' + (.5 * x) + ',' + y
370 + 'A6,6 0 0 ' + e + ' ' + (6.5 * x) + ',' + (y + 6)
372 + 'A6,6 0 0 ' + e + ' ' + (.5 * x) + ',' + (2 * y)
374 + 'M' + (2.5 * x) + ',' + (y + 8)
376 + 'M' + (4.5 * x) + ',' + (y + 8)
381 function updateBrushBG() {
382 if (!brush.empty()) brush.extent(brushExtent);
384 .data([brush.empty() ? x2.domain() : brushExtent])
385 .each(function(d,i) {
386 var leftWidth = x2(d[0]) - x2.range()[0],
387 rightWidth = x2.range()[1] - x2(d[1]);
388 d3.select(this).select('.left')
389 .attr('width', leftWidth < 0 ? 0 : leftWidth);
391 d3.select(this).select('.right')
393 .attr('width', rightWidth < 0 ? 0 : rightWidth);
399 brushExtent = brush.empty() ? null : brush.extent();
400 extent = brush.empty() ? x2.domain() : brush.extent();
403 dispatch.brush({extent: extent, brush: brush});
408 //------------------------------------------------------------
409 // Prepare Main (Focus) Bars and Lines
412 .width(availableWidth)
413 .height(availableHeight1)
414 .color(data.map(function(d,i) {
415 return d.color || color(d, i);
416 }).filter(function(d,i) { return !data[i].disabled && data[i].bar }));
420 .width(availableWidth)
421 .height(availableHeight1)
422 .color(data.map(function(d,i) {
423 return d.color || color(d, i);
424 }).filter(function(d,i) { return !data[i].disabled && !data[i].bar }));
426 var focusBarsWrap = g.select('.nv-focus .nv-barsWrap')
427 .datum(!dataBars.length ? [{values:[]}] :
432 values: d.values.filter(function(d,i) {
433 return bars.x()(d,i) >= extent[0] && bars.x()(d,i) <= extent[1];
439 var focusLinesWrap = g.select('.nv-focus .nv-linesWrap')
440 .datum(dataLines[0].disabled ? [{values:[]}] :
445 values: d.values.filter(function(d,i) {
446 return lines.x()(d,i) >= extent[0] && lines.x()(d,i) <= extent[1];
452 //------------------------------------------------------------
455 //------------------------------------------------------------
456 // Update Main (Focus) X Axis
458 if (dataBars.length) {
466 .ticks( availableWidth / 100 )
467 .tickSize(-availableHeight1, 0);
469 xAxis.domain([Math.ceil(extent[0]), Math.floor(extent[1])]);
471 g.select('.nv-x.nv-axis').transition().duration(transitionDuration)
473 //------------------------------------------------------------
476 //------------------------------------------------------------
477 // Update Main (Focus) Bars and Lines
479 focusBarsWrap.transition().duration(transitionDuration).call(bars);
480 focusLinesWrap.transition().duration(transitionDuration).call(lines);
482 //------------------------------------------------------------
485 //------------------------------------------------------------
486 // Setup and Update Main (Focus) Y Axes
488 g.select('.nv-focus .nv-x.nv-axis')
489 .attr('transform', 'translate(0,' + y1.range()[0] + ')');
494 .ticks( availableHeight1 / 36 )
495 .tickSize(-availableWidth, 0);
497 g.select('.nv-focus .nv-y1.nv-axis')
498 .style('opacity', dataBars.length ? 1 : 0);
503 .ticks( availableHeight1 / 36 )
504 .tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none
506 g.select('.nv-focus .nv-y2.nv-axis')
507 .style('opacity', dataLines.length ? 1 : 0)
508 .attr('transform', 'translate(' + x.range()[1] + ',0)');
510 g.select('.nv-focus .nv-y1.nv-axis').transition().duration(transitionDuration)
512 g.select('.nv-focus .nv-y2.nv-axis').transition().duration(transitionDuration)
516 //============================================================
526 //============================================================
527 // Event Handling/Dispatching (out of chart's scope)
528 //------------------------------------------------------------
530 lines.dispatch.on('elementMouseover.tooltip', function(e) {
531 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
532 dispatch.tooltipShow(e);
535 lines.dispatch.on('elementMouseout.tooltip', function(e) {
536 dispatch.tooltipHide(e);
539 bars.dispatch.on('elementMouseover.tooltip', function(e) {
540 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
541 dispatch.tooltipShow(e);
544 bars.dispatch.on('elementMouseout.tooltip', function(e) {
545 dispatch.tooltipHide(e);
548 dispatch.on('tooltipHide', function() {
549 if (tooltips) nv.tooltip.cleanup();
552 //============================================================
555 //============================================================
556 // Expose Public Variables
557 //------------------------------------------------------------
559 // expose chart's sub-components
560 chart.dispatch = dispatch;
561 chart.legend = legend;
563 chart.lines2 = lines2;
567 chart.x2Axis = x2Axis;
568 chart.y1Axis = y1Axis;
569 chart.y2Axis = y2Axis;
570 chart.y3Axis = y3Axis;
571 chart.y4Axis = y4Axis;
573 d3.rebind(chart, lines, 'defined', 'size', 'clipVoronoi', 'interpolate');
574 //TODO: consider rebinding x, y and some other stuff, and simply do soemthign lile bars.x(lines.x()), etc.
575 //d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
577 chart.options = nv.utils.optionsFunc.bind(chart);
579 chart.x = function(_) {
580 if (!arguments.length) return getX;
587 chart.y = function(_) {
588 if (!arguments.length) return getY;
595 chart.margin = function(_) {
596 if (!arguments.length) return margin;
597 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
598 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
599 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
600 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
604 chart.width = function(_) {
605 if (!arguments.length) return width;
610 chart.height = function(_) {
611 if (!arguments.length) return height;
616 chart.color = function(_) {
617 if (!arguments.length) return color;
618 color = nv.utils.getColor(_);
623 chart.showLegend = function(_) {
624 if (!arguments.length) return showLegend;
629 chart.tooltips = function(_) {
630 if (!arguments.length) return tooltips;
635 chart.tooltipContent = function(_) {
636 if (!arguments.length) return tooltip;
641 chart.noData = function(_) {
642 if (!arguments.length) return noData;
647 chart.brushExtent = function(_) {
648 if (!arguments.length) return brushExtent;
654 //============================================================