1 nv.models.lineWithFocusChart = function() {
3 //============================================================
4 // Public Variables with Default Settings
5 //------------------------------------------------------------
7 var lines = nv.models.line()
8 , lines2 = nv.models.line()
9 , xAxis = nv.models.axis()
10 , yAxis = nv.models.axis()
11 , x2Axis = nv.models.axis()
12 , y2Axis = nv.models.axis()
13 , legend = nv.models.legend()
14 , brush = d3.svg.brush()
17 var margin = {top: 30, right: 30, bottom: 30, left: 60}
18 , margin2 = {top: 0, right: 30, bottom: 20, left: 60}
19 , color = nv.utils.defaultColor()
30 , tooltip = function(key, x, y, e, graph) {
31 return '<h3>' + key + '</h3>' +
32 '<p>' + y + ' at ' + x + '</p>'
34 , noData = "No Data Available."
35 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'brush')
36 , transitionDuration = 250
59 //============================================================
62 //============================================================
64 //------------------------------------------------------------
66 var showTooltip = function(e, offsetElement) {
67 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
68 top = e.pos[1] + ( offsetElement.offsetTop || 0),
69 x = xAxis.tickFormat()(lines.x()(e.point, e.pointIndex)),
70 y = yAxis.tickFormat()(lines.y()(e.point, e.pointIndex)),
71 content = tooltip(e.series.key, x, y, e, chart);
73 nv.tooltip.show([left, top], content, null, null, offsetElement);
76 //============================================================
79 function chart(selection) {
80 selection.each(function(data) {
81 var container = d3.select(this),
84 var availableWidth = (width || parseInt(container.style('width')) || 960)
85 - margin.left - margin.right,
86 availableHeight1 = (height || parseInt(container.style('height')) || 400)
87 - margin.top - margin.bottom - height2,
88 availableHeight2 = height2 - margin2.top - margin2.bottom;
90 chart.update = function() { container.transition().duration(transitionDuration).call(chart) };
91 chart.container = this;
94 //------------------------------------------------------------
95 // Display No Data message if there's nothing to show.
97 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
98 var noDataText = container.selectAll('.nv-noData').data([noData]);
100 noDataText.enter().append('text')
101 .attr('class', 'nvd3 nv-noData')
103 .style('text-anchor', 'middle');
106 .attr('x', margin.left + availableWidth / 2)
107 .attr('y', margin.top + availableHeight1 / 2)
108 .text(function(d) { return d });
112 container.selectAll('.nv-noData').remove();
115 //------------------------------------------------------------
118 //------------------------------------------------------------
123 x2 = lines2.xScale();
124 y2 = lines2.yScale();
126 //------------------------------------------------------------
129 //------------------------------------------------------------
130 // Setup containers and skeleton of chart
132 var wrap = container.selectAll('g.nv-wrap.nv-lineWithFocusChart').data([data]);
133 var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-lineWithFocusChart').append('g');
134 var g = wrap.select('g');
136 gEnter.append('g').attr('class', 'nv-legendWrap');
138 var focusEnter = gEnter.append('g').attr('class', 'nv-focus');
139 focusEnter.append('g').attr('class', 'nv-x nv-axis');
140 focusEnter.append('g').attr('class', 'nv-y nv-axis');
141 focusEnter.append('g').attr('class', 'nv-linesWrap');
143 var contextEnter = gEnter.append('g').attr('class', 'nv-context');
144 contextEnter.append('g').attr('class', 'nv-x nv-axis');
145 contextEnter.append('g').attr('class', 'nv-y nv-axis');
146 contextEnter.append('g').attr('class', 'nv-linesWrap');
147 contextEnter.append('g').attr('class', 'nv-brushBackground');
148 contextEnter.append('g').attr('class', 'nv-x nv-brush');
150 //------------------------------------------------------------
153 //------------------------------------------------------------
157 legend.width(availableWidth);
159 g.select('.nv-legendWrap')
163 if ( margin.top != legend.height()) {
164 margin.top = legend.height();
165 availableHeight1 = (height || parseInt(container.style('height')) || 400)
166 - margin.top - margin.bottom - height2;
169 g.select('.nv-legendWrap')
170 .attr('transform', 'translate(0,' + (-margin.top) +')')
173 //------------------------------------------------------------
176 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
179 //------------------------------------------------------------
180 // Main Chart Component(s)
183 .width(availableWidth)
184 .height(availableHeight1)
188 return d.color || color(d, i);
190 .filter(function(d,i) {
191 return !data[i].disabled;
196 .defined(lines.defined())
197 .width(availableWidth)
198 .height(availableHeight2)
202 return d.color || color(d, i);
204 .filter(function(d,i) {
205 return !data[i].disabled;
209 g.select('.nv-context')
210 .attr('transform', 'translate(0,' + ( availableHeight1 + margin.bottom + margin2.top) + ')')
212 var contextLinesWrap = g.select('.nv-context .nv-linesWrap')
213 .datum(data.filter(function(d) { return !d.disabled }))
215 d3.transition(contextLinesWrap).call(lines2);
217 //------------------------------------------------------------
221 var focusLinesWrap = g.select('.nv-focus .nv-linesWrap')
222 .datum(data.filter(function(d) { return !d.disabled }))
224 d3.transition(focusLinesWrap).call(lines);
228 //------------------------------------------------------------
229 // Setup Main (Focus) Axes
233 .ticks( availableWidth / 100 )
234 .tickSize(-availableHeight1, 0);
238 .ticks( availableHeight1 / 36 )
239 .tickSize( -availableWidth, 0);
241 g.select('.nv-focus .nv-x.nv-axis')
242 .attr('transform', 'translate(0,' + availableHeight1 + ')');
244 //------------------------------------------------------------
247 //------------------------------------------------------------
252 .on('brush', function() {
253 //When brushing, turn off transitions because chart needs to change immediately.
254 var oldTransition = chart.transitionDuration();
255 chart.transitionDuration(0);
257 chart.transitionDuration(oldTransition);
260 if (brushExtent) brush.extent(brushExtent);
262 var brushBG = g.select('.nv-brushBackground').selectAll('g')
263 .data([brushExtent || brush.extent()])
265 var brushBGenter = brushBG.enter()
268 brushBGenter.append('rect')
269 .attr('class', 'left')
272 .attr('height', availableHeight2);
274 brushBGenter.append('rect')
275 .attr('class', 'right')
278 .attr('height', availableHeight2);
280 var gBrush = g.select('.nv-x.nv-brush')
282 gBrush.selectAll('rect')
284 .attr('height', availableHeight2);
285 gBrush.selectAll('.resize').append('path').attr('d', resizePath);
289 //------------------------------------------------------------
292 //------------------------------------------------------------
293 // Setup Secondary (Context) Axes
297 .ticks( availableWidth / 100 )
298 .tickSize(-availableHeight2, 0);
300 g.select('.nv-context .nv-x.nv-axis')
301 .attr('transform', 'translate(0,' + y2.range()[0] + ')');
302 d3.transition(g.select('.nv-context .nv-x.nv-axis'))
308 .ticks( availableHeight2 / 36 )
309 .tickSize( -availableWidth, 0);
311 d3.transition(g.select('.nv-context .nv-y.nv-axis'))
314 g.select('.nv-context .nv-x.nv-axis')
315 .attr('transform', 'translate(0,' + y2.range()[0] + ')');
317 //------------------------------------------------------------
320 //============================================================
321 // Event Handling/Dispatching (in chart's scope)
322 //------------------------------------------------------------
324 legend.dispatch.on('stateChange', function(newState) {
328 dispatch.on('tooltipShow', function(e) {
329 if (tooltips) showTooltip(e, that.parentNode);
332 //============================================================
335 //============================================================
337 //------------------------------------------------------------
339 // Taken from crossfilter (http://square.github.com/crossfilter/)
340 function resizePath(d) {
343 y = availableHeight2 / 3;
344 return 'M' + (.5 * x) + ',' + y
345 + 'A6,6 0 0 ' + e + ' ' + (6.5 * x) + ',' + (y + 6)
347 + 'A6,6 0 0 ' + e + ' ' + (.5 * x) + ',' + (2 * y)
349 + 'M' + (2.5 * x) + ',' + (y + 8)
351 + 'M' + (4.5 * x) + ',' + (y + 8)
356 function updateBrushBG() {
357 if (!brush.empty()) brush.extent(brushExtent);
359 .data([brush.empty() ? x2.domain() : brushExtent])
360 .each(function(d,i) {
361 var leftWidth = x2(d[0]) - x.range()[0],
362 rightWidth = x.range()[1] - x2(d[1]);
363 d3.select(this).select('.left')
364 .attr('width', leftWidth < 0 ? 0 : leftWidth);
366 d3.select(this).select('.right')
368 .attr('width', rightWidth < 0 ? 0 : rightWidth);
374 brushExtent = brush.empty() ? null : brush.extent();
375 var extent = brush.empty() ? x2.domain() : brush.extent();
377 //The brush extent cannot be less than one. If it is, don't update the line chart.
378 if (Math.abs(extent[0] - extent[1]) <= 1) {
382 dispatch.brush({extent: extent, brush: brush});
387 // Update Main (Focus)
388 var focusLinesWrap = g.select('.nv-focus .nv-linesWrap')
391 .filter(function(d) { return !d.disabled })
395 values: d.values.filter(function(d,i) {
396 return lines.x()(d,i) >= extent[0] && lines.x()(d,i) <= extent[1];
401 focusLinesWrap.transition().duration(transitionDuration).call(lines);
404 // Update Main (Focus) Axes
405 g.select('.nv-focus .nv-x.nv-axis').transition().duration(transitionDuration)
407 g.select('.nv-focus .nv-y.nv-axis').transition().duration(transitionDuration)
411 //============================================================
420 //============================================================
421 // Event Handling/Dispatching (out of chart's scope)
422 //------------------------------------------------------------
424 lines.dispatch.on('elementMouseover.tooltip', function(e) {
425 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
426 dispatch.tooltipShow(e);
429 lines.dispatch.on('elementMouseout.tooltip', function(e) {
430 dispatch.tooltipHide(e);
433 dispatch.on('tooltipHide', function() {
434 if (tooltips) nv.tooltip.cleanup();
437 //============================================================
440 //============================================================
441 // Expose Public Variables
442 //------------------------------------------------------------
444 // expose chart's sub-components
445 chart.dispatch = dispatch;
446 chart.legend = legend;
448 chart.lines2 = lines2;
451 chart.x2Axis = x2Axis;
452 chart.y2Axis = y2Axis;
454 d3.rebind(chart, lines, 'defined', 'isArea', 'size', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
456 chart.options = nv.utils.optionsFunc.bind(chart);
458 chart.x = function(_) {
459 if (!arguments.length) return lines.x;
465 chart.y = function(_) {
466 if (!arguments.length) return lines.y;
472 chart.margin = function(_) {
473 if (!arguments.length) return margin;
474 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
475 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
476 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
477 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
481 chart.margin2 = function(_) {
482 if (!arguments.length) return margin2;
487 chart.width = function(_) {
488 if (!arguments.length) return width;
493 chart.height = function(_) {
494 if (!arguments.length) return height;
499 chart.height2 = function(_) {
500 if (!arguments.length) return height2;
505 chart.color = function(_) {
506 if (!arguments.length) return color;
507 color =nv.utils.getColor(_);
512 chart.showLegend = function(_) {
513 if (!arguments.length) return showLegend;
518 chart.tooltips = function(_) {
519 if (!arguments.length) return tooltips;
524 chart.tooltipContent = function(_) {
525 if (!arguments.length) return tooltip;
530 chart.interpolate = function(_) {
531 if (!arguments.length) return lines.interpolate();
532 lines.interpolate(_);
533 lines2.interpolate(_);
537 chart.noData = function(_) {
538 if (!arguments.length) return noData;
543 // Chart has multiple similar Axes, to prevent code duplication, probably need to link all axis functions manually like below
544 chart.xTickFormat = function(_) {
545 if (!arguments.length) return xAxis.tickFormat();
547 x2Axis.tickFormat(_);
551 chart.yTickFormat = function(_) {
552 if (!arguments.length) return yAxis.tickFormat();
554 y2Axis.tickFormat(_);
558 chart.brushExtent = function(_) {
559 if (!arguments.length) return brushExtent;
564 chart.transitionDuration = function(_) {
565 if (!arguments.length) return transitionDuration;
566 transitionDuration = _;
570 //============================================================