2 nv.models.scatterPlusLineChart = function() {
4 //============================================================
5 // Public Variables with Default Settings
6 //------------------------------------------------------------
8 var scatter = nv.models.scatter()
9 , xAxis = nv.models.axis()
10 , yAxis = nv.models.axis()
11 , legend = nv.models.legend()
12 , controls = nv.models.legend()
13 , distX = nv.models.distribution()
14 , distY = nv.models.distribution()
17 var margin = {top: 30, right: 20, bottom: 50, left: 75}
20 , color = nv.utils.defaultColor()
21 , x = d3.fisheye ? d3.fisheye.scale(d3.scale.linear).distortion(0) : scatter.xScale()
22 , y = d3.fisheye ? d3.fisheye.scale(d3.scale.linear).distortion(0) : scatter.yScale()
28 , rightAlignYAxis = false
29 , showControls = !!d3.fisheye
31 , pauseFisheye = false
33 , tooltipX = function(key, x, y) { return '<strong>' + x + '</strong>' }
34 , tooltipY = function(key, x, y) { return '<strong>' + y + '</strong>' }
35 , tooltip = function(key, x, y, date) { return '<h3>' + key + '</h3>'
36 + '<p>' + date + '</p>' }
39 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'stateChange', 'changeState')
40 , noData = "No Data Available."
41 , transitionDuration = 250
53 .orient((rightAlignYAxis) ? 'right' : 'left')
63 controls.updateState(false);
64 //============================================================
67 //============================================================
69 //------------------------------------------------------------
73 var showTooltip = function(e, offsetElement) {
74 //TODO: make tooltip style an option between single or dual on axes (maybe on all charts with axes?)
76 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
77 top = e.pos[1] + ( offsetElement.offsetTop || 0),
78 leftX = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
79 topX = y.range()[0] + margin.top + ( offsetElement.offsetTop || 0),
80 leftY = x.range()[0] + margin.left + ( offsetElement.offsetLeft || 0 ),
81 topY = e.pos[1] + ( offsetElement.offsetTop || 0),
82 xVal = xAxis.tickFormat()(scatter.x()(e.point, e.pointIndex)),
83 yVal = yAxis.tickFormat()(scatter.y()(e.point, e.pointIndex));
85 if( tooltipX != null )
86 nv.tooltip.show([leftX, topX], tooltipX(e.series.key, xVal, yVal, e, chart), 'n', 1, offsetElement, 'x-nvtooltip');
87 if( tooltipY != null )
88 nv.tooltip.show([leftY, topY], tooltipY(e.series.key, xVal, yVal, e, chart), 'e', 1, offsetElement, 'y-nvtooltip');
90 nv.tooltip.show([left, top], tooltip(e.series.key, xVal, yVal, e.point.tooltip, e, chart), e.value < 0 ? 'n' : 's', null, offsetElement);
94 { key: 'Magnify', disabled: true }
97 //============================================================
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 availableHeight = (height || parseInt(container.style('height')) || 400)
108 - margin.top - margin.bottom;
110 chart.update = function() { container.transition().duration(transitionDuration).call(chart); };
111 chart.container = this;
114 state.disabled = data.map(function(d) { return !!d.disabled });
120 if (state[key] instanceof Array)
121 defaultState[key] = state[key].slice(0);
123 defaultState[key] = state[key];
127 //------------------------------------------------------------
128 // Display noData message if there's nothing to show.
130 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
131 var noDataText = container.selectAll('.nv-noData').data([noData]);
133 noDataText.enter().append('text')
134 .attr('class', 'nvd3 nv-noData')
136 .style('text-anchor', 'middle');
139 .attr('x', margin.left + availableWidth / 2)
140 .attr('y', margin.top + availableHeight / 2)
141 .text(function(d) { return d });
145 container.selectAll('.nv-noData').remove();
148 //------------------------------------------------------------
151 //------------------------------------------------------------
154 x = scatter.xScale();
155 y = scatter.yScale();
160 //------------------------------------------------------------
163 //------------------------------------------------------------
164 // Setup containers and skeleton of chart
166 var wrap = container.selectAll('g.nv-wrap.nv-scatterChart').data([data]);
167 var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-scatterChart nv-chart-' + scatter.id());
168 var gEnter = wrapEnter.append('g');
169 var g = wrap.select('g')
171 // background for pointer events
172 gEnter.append('rect').attr('class', 'nvd3 nv-background').style("pointer-events","none");
174 gEnter.append('g').attr('class', 'nv-x nv-axis');
175 gEnter.append('g').attr('class', 'nv-y nv-axis');
176 gEnter.append('g').attr('class', 'nv-scatterWrap');
177 gEnter.append('g').attr('class', 'nv-regressionLinesWrap');
178 gEnter.append('g').attr('class', 'nv-distWrap');
179 gEnter.append('g').attr('class', 'nv-legendWrap');
180 gEnter.append('g').attr('class', 'nv-controlsWrap');
182 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
184 if (rightAlignYAxis) {
185 g.select(".nv-y.nv-axis")
186 .attr("transform", "translate(" + availableWidth + ",0)");
189 //------------------------------------------------------------
192 //------------------------------------------------------------
196 legend.width( availableWidth / 2 );
198 wrap.select('.nv-legendWrap')
202 if ( margin.top != legend.height()) {
203 margin.top = legend.height();
204 availableHeight = (height || parseInt(container.style('height')) || 400)
205 - margin.top - margin.bottom;
208 wrap.select('.nv-legendWrap')
209 .attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')');
212 //------------------------------------------------------------
215 //------------------------------------------------------------
219 controls.width(180).color(['#444']);
220 g.select('.nv-controlsWrap')
222 .attr('transform', 'translate(0,' + (-margin.top) +')')
226 //------------------------------------------------------------
229 //------------------------------------------------------------
230 // Main Chart Component(s)
233 .width(availableWidth)
234 .height(availableHeight)
235 .color(data.map(function(d,i) {
236 return d.color || color(d, i);
237 }).filter(function(d,i) { return !data[i].disabled }))
239 wrap.select('.nv-scatterWrap')
240 .datum(data.filter(function(d) { return !d.disabled }))
243 wrap.select('.nv-regressionLinesWrap')
244 .attr('clip-path', 'url(#nv-edge-clip-' + scatter.id() + ')');
246 var regWrap = wrap.select('.nv-regressionLinesWrap').selectAll('.nv-regLines')
247 .data(function(d) {return d });
249 regWrap.enter().append('g').attr('class', 'nv-regLines');
251 var regLine = regWrap.selectAll('.nv-regLine').data(function(d){return [d]});
252 var regLineEnter = regLine.enter()
253 .append('line').attr('class', 'nv-regLine')
254 .style('stroke-opacity', 0);
258 .attr('x1', x.range()[0])
259 .attr('x2', x.range()[1])
260 .attr('y1', function(d,i) {return y(x.domain()[0] * d.slope + d.intercept) })
261 .attr('y2', function(d,i) { return y(x.domain()[1] * d.slope + d.intercept) })
262 .style('stroke', function(d,i,j) { return color(d,j) })
263 .style('stroke-opacity', function(d,i) {
264 return (d.disabled || typeof d.slope === 'undefined' || typeof d.intercept === 'undefined') ? 0 : 1
267 //------------------------------------------------------------
270 //------------------------------------------------------------
276 .ticks( xAxis.ticks() ? xAxis.ticks() : availableWidth / 100 )
277 .tickSize( -availableHeight , 0);
279 g.select('.nv-x.nv-axis')
280 .attr('transform', 'translate(0,' + y.range()[0] + ')')
287 .ticks( yAxis.ticks() ? yAxis.ticks() : availableHeight / 36 )
288 .tickSize( -availableWidth, 0);
290 g.select('.nv-y.nv-axis')
297 .getData(scatter.x())
299 .width(availableWidth)
300 .color(data.map(function(d,i) {
301 return d.color || color(d, i);
302 }).filter(function(d,i) { return !data[i].disabled }));
303 gEnter.select('.nv-distWrap').append('g')
304 .attr('class', 'nv-distributionX');
305 g.select('.nv-distributionX')
306 .attr('transform', 'translate(0,' + y.range()[0] + ')')
307 .datum(data.filter(function(d) { return !d.disabled }))
313 .getData(scatter.y())
315 .width(availableHeight)
316 .color(data.map(function(d,i) {
317 return d.color || color(d, i);
318 }).filter(function(d,i) { return !data[i].disabled }));
319 gEnter.select('.nv-distWrap').append('g')
320 .attr('class', 'nv-distributionY');
321 g.select('.nv-distributionY')
322 .attr('transform', 'translate(' + (rightAlignYAxis ? availableWidth : -distY.size() ) + ',0)')
323 .datum(data.filter(function(d) { return !d.disabled }))
327 //------------------------------------------------------------
333 g.select('.nv-background')
334 .attr('width', availableWidth)
335 .attr('height', availableHeight)
338 g.select('.nv-background').on('mousemove', updateFisheye);
339 g.select('.nv-background').on('click', function() { pauseFisheye = !pauseFisheye;});
340 scatter.dispatch.on('elementClick.freezeFisheye', function() {
341 pauseFisheye = !pauseFisheye;
346 function updateFisheye() {
348 g.select('.nv-point-paths').style('pointer-events', 'all');
352 g.select('.nv-point-paths').style('pointer-events', 'none' );
354 var mouse = d3.mouse(this);
355 x.distortion(fisheye).focus(mouse[0]);
356 y.distortion(fisheye).focus(mouse[1]);
358 g.select('.nv-scatterWrap')
359 .datum(data.filter(function(d) { return !d.disabled }))
363 g.select('.nv-x.nv-axis').call(xAxis);
366 g.select('.nv-y.nv-axis').call(yAxis);
368 g.select('.nv-distributionX')
369 .datum(data.filter(function(d) { return !d.disabled }))
371 g.select('.nv-distributionY')
372 .datum(data.filter(function(d) { return !d.disabled }))
378 //============================================================
379 // Event Handling/Dispatching (in chart's scope)
380 //------------------------------------------------------------
382 controls.dispatch.on('legendClick', function(d,i) {
383 d.disabled = !d.disabled;
385 fisheye = d.disabled ? 0 : 2.5;
386 g.select('.nv-background') .style('pointer-events', d.disabled ? 'none' : 'all');
387 g.select('.nv-point-paths').style('pointer-events', d.disabled ? 'all' : 'none' );
390 x.distortion(fisheye).focus(0);
391 y.distortion(fisheye).focus(0);
393 g.select('.nv-scatterWrap').call(scatter);
394 g.select('.nv-x.nv-axis').call(xAxis);
395 g.select('.nv-y.nv-axis').call(yAxis);
397 pauseFisheye = false;
403 legend.dispatch.on('stateChange', function(newState) {
405 dispatch.stateChange(state);
410 scatter.dispatch.on('elementMouseover.tooltip', function(e) {
411 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-distx-' + e.pointIndex)
412 .attr('y1', e.pos[1] - availableHeight);
413 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-disty-' + e.pointIndex)
414 .attr('x2', e.pos[0] + distX.size());
416 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
417 dispatch.tooltipShow(e);
420 dispatch.on('tooltipShow', function(e) {
421 if (tooltips) showTooltip(e, that.parentNode);
424 // Update chart from a state object passed to event handler
425 dispatch.on('changeState', function(e) {
427 if (typeof e.disabled !== 'undefined') {
428 data.forEach(function(series,i) {
429 series.disabled = e.disabled[i];
432 state.disabled = e.disabled;
438 //============================================================
441 //store old scales for use in transitions on update
452 //============================================================
453 // Event Handling/Dispatching (out of chart's scope)
454 //------------------------------------------------------------
456 scatter.dispatch.on('elementMouseout.tooltip', function(e) {
457 dispatch.tooltipHide(e);
459 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-distx-' + e.pointIndex)
461 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-disty-' + e.pointIndex)
462 .attr('x2', distY.size());
464 dispatch.on('tooltipHide', function() {
465 if (tooltips) nv.tooltip.cleanup();
468 //============================================================
471 //============================================================
472 // Expose Public Variables
473 //------------------------------------------------------------
475 // expose chart's sub-components
476 chart.dispatch = dispatch;
477 chart.scatter = scatter;
478 chart.legend = legend;
479 chart.controls = controls;
485 d3.rebind(chart, scatter, 'id', 'interactive', 'pointActive', 'x', 'y', 'shape', 'size', 'xScale', 'yScale', 'zScale', 'xDomain', 'yDomain', 'xRange', 'yRange', 'sizeDomain', 'sizeRange', 'forceX', 'forceY', 'forceSize', 'clipVoronoi', 'clipRadius', 'useVoronoi');
487 chart.options = nv.utils.optionsFunc.bind(chart);
489 chart.margin = function(_) {
490 if (!arguments.length) return margin;
491 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
492 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
493 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
494 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
498 chart.width = function(_) {
499 if (!arguments.length) return width;
504 chart.height = function(_) {
505 if (!arguments.length) return height;
510 chart.color = function(_) {
511 if (!arguments.length) return color;
512 color = nv.utils.getColor(_);
519 chart.showDistX = function(_) {
520 if (!arguments.length) return showDistX;
525 chart.showDistY = function(_) {
526 if (!arguments.length) return showDistY;
531 chart.showControls = function(_) {
532 if (!arguments.length) return showControls;
537 chart.showLegend = function(_) {
538 if (!arguments.length) return showLegend;
543 chart.showXAxis = function(_) {
544 if (!arguments.length) return showXAxis;
549 chart.showYAxis = function(_) {
550 if (!arguments.length) return showYAxis;
555 chart.rightAlignYAxis = function(_) {
556 if(!arguments.length) return rightAlignYAxis;
558 yAxis.orient( (_) ? 'right' : 'left');
562 chart.fisheye = function(_) {
563 if (!arguments.length) return fisheye;
568 chart.tooltips = function(_) {
569 if (!arguments.length) return tooltips;
574 chart.tooltipContent = function(_) {
575 if (!arguments.length) return tooltip;
580 chart.tooltipXContent = function(_) {
581 if (!arguments.length) return tooltipX;
586 chart.tooltipYContent = function(_) {
587 if (!arguments.length) return tooltipY;
592 chart.state = function(_) {
593 if (!arguments.length) return state;
598 chart.defaultState = function(_) {
599 if (!arguments.length) return defaultState;
604 chart.noData = function(_) {
605 if (!arguments.length) return noData;
610 chart.transitionDuration = function(_) {
611 if (!arguments.length) return transitionDuration;
612 transitionDuration = _;
616 //============================================================