1 nv.models.scatterChart = function() {
3 //============================================================
4 // Public Variables with Default Settings
5 //------------------------------------------------------------
7 var scatter = nv.models.scatter()
8 , xAxis = nv.models.axis()
9 , yAxis = nv.models.axis()
10 , legend = nv.models.legend()
11 , controls = nv.models.legend()
12 , distX = nv.models.distribution()
13 , distY = nv.models.distribution()
16 var margin = {top: 30, right: 20, bottom: 50, left: 75}
19 , color = nv.utils.defaultColor()
20 , x = d3.fisheye ? d3.fisheye.scale(d3.scale.linear).distortion(0) : scatter.xScale()
21 , y = d3.fisheye ? d3.fisheye.scale(d3.scale.linear).distortion(0) : scatter.yScale()
29 , rightAlignYAxis = false
30 , showControls = !!d3.fisheye
32 , pauseFisheye = false
34 , tooltipX = function(key, x, y) { return '<strong>' + x + '</strong>' }
35 , tooltipY = function(key, x, y) { return '<strong>' + y + '</strong>' }
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);
65 //============================================================
68 //============================================================
70 //------------------------------------------------------------
74 var showTooltip = function(e, offsetElement) {
75 //TODO: make tooltip style an option between single or dual on axes (maybe on all charts with axes?)
77 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
78 top = e.pos[1] + ( offsetElement.offsetTop || 0),
79 leftX = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
80 topX = y.range()[0] + margin.top + ( offsetElement.offsetTop || 0),
81 leftY = x.range()[0] + margin.left + ( offsetElement.offsetLeft || 0 ),
82 topY = e.pos[1] + ( offsetElement.offsetTop || 0),
83 xVal = xAxis.tickFormat()(scatter.x()(e.point, e.pointIndex)),
84 yVal = yAxis.tickFormat()(scatter.y()(e.point, e.pointIndex));
86 if( tooltipX != null )
87 nv.tooltip.show([leftX, topX], tooltipX(e.series.key, xVal, yVal, e, chart), 'n', 1, offsetElement, 'x-nvtooltip');
88 if( tooltipY != null )
89 nv.tooltip.show([leftY, topY], tooltipY(e.series.key, xVal, yVal, e, chart), 'e', 1, offsetElement, 'y-nvtooltip');
91 nv.tooltip.show([left, top], tooltip(e.series.key, xVal, yVal, e, chart), e.value < 0 ? 'n' : 's', null, offsetElement);
95 { key: 'Magnify', disabled: true }
98 //============================================================
101 function chart(selection) {
102 selection.each(function(data) {
103 var container = d3.select(this),
106 var availableWidth = (width || parseInt(container.style('width')) || 960)
107 - margin.left - margin.right,
108 availableHeight = (height || parseInt(container.style('height')) || 400)
109 - margin.top - margin.bottom;
111 chart.update = function() { container.transition().duration(transitionDuration).call(chart); };
112 chart.container = this;
115 state.disabled = data.map(function(d) { return !!d.disabled });
121 if (state[key] instanceof Array)
122 defaultState[key] = state[key].slice(0);
124 defaultState[key] = state[key];
128 //------------------------------------------------------------
129 // Display noData message if there's nothing to show.
131 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
132 var noDataText = container.selectAll('.nv-noData').data([noData]);
134 noDataText.enter().append('text')
135 .attr('class', 'nvd3 nv-noData')
137 .style('text-anchor', 'middle');
140 .attr('x', margin.left + availableWidth / 2)
141 .attr('y', margin.top + availableHeight / 2)
142 .text(function(d) { return d });
146 container.selectAll('.nv-noData').remove();
149 //------------------------------------------------------------
152 //------------------------------------------------------------
158 //------------------------------------------------------------
161 //------------------------------------------------------------
162 // Setup containers and skeleton of chart
164 var wrap = container.selectAll('g.nv-wrap.nv-scatterChart').data([data]);
165 var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-scatterChart nv-chart-' + scatter.id());
166 var gEnter = wrapEnter.append('g');
167 var g = wrap.select('g');
169 // background for pointer events
170 gEnter.append('rect').attr('class', 'nvd3 nv-background');
172 gEnter.append('g').attr('class', 'nv-x nv-axis');
173 gEnter.append('g').attr('class', 'nv-y nv-axis');
174 gEnter.append('g').attr('class', 'nv-scatterWrap');
175 gEnter.append('g').attr('class', 'nv-distWrap');
176 gEnter.append('g').attr('class', 'nv-legendWrap');
177 gEnter.append('g').attr('class', 'nv-controlsWrap');
179 //------------------------------------------------------------
182 //------------------------------------------------------------
186 var legendWidth = (showControls) ? availableWidth / 2 : availableWidth;
187 legend.width(legendWidth);
189 wrap.select('.nv-legendWrap')
193 if ( margin.top != legend.height()) {
194 margin.top = legend.height();
195 availableHeight = (height || parseInt(container.style('height')) || 400)
196 - margin.top - margin.bottom;
199 wrap.select('.nv-legendWrap')
200 .attr('transform', 'translate(' + (availableWidth - legendWidth) + ',' + (-margin.top) +')');
203 //------------------------------------------------------------
206 //------------------------------------------------------------
210 controls.width(180).color(['#444']);
211 g.select('.nv-controlsWrap')
213 .attr('transform', 'translate(0,' + (-margin.top) +')')
217 //------------------------------------------------------------
220 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
222 if (rightAlignYAxis) {
223 g.select(".nv-y.nv-axis")
224 .attr("transform", "translate(" + availableWidth + ",0)");
227 //------------------------------------------------------------
228 // Main Chart Component(s)
231 .width(availableWidth)
232 .height(availableHeight)
233 .color(data.map(function(d,i) {
234 return d.color || color(d, i);
235 }).filter(function(d,i) { return !data[i].disabled }));
238 scatter.xDomain(null);
241 scatter.yDomain(null);
243 wrap.select('.nv-scatterWrap')
244 .datum(data.filter(function(d) { return !d.disabled }))
247 //Adjust for x and y padding
248 if (xPadding !== 0) {
249 var xRange = x.domain()[1] - x.domain()[0];
250 scatter.xDomain([x.domain()[0] - (xPadding * xRange), x.domain()[1] + (xPadding * xRange)]);
253 if (yPadding !== 0) {
254 var yRange = y.domain()[1] - y.domain()[0];
255 scatter.yDomain([y.domain()[0] - (yPadding * yRange), y.domain()[1] + (yPadding * yRange)]);
258 //Only need to update the scatter again if x/yPadding changed the domain.
259 if (yPadding !== 0 || xPadding !== 0) {
260 wrap.select('.nv-scatterWrap')
261 .datum(data.filter(function(d) { return !d.disabled }))
265 //------------------------------------------------------------
268 //------------------------------------------------------------
273 .ticks( xAxis.ticks() && xAxis.ticks().length ? xAxis.ticks() : availableWidth / 100 )
274 .tickSize( -availableHeight , 0);
276 g.select('.nv-x.nv-axis')
277 .attr('transform', 'translate(0,' + y.range()[0] + ')')
285 .ticks( yAxis.ticks() && yAxis.ticks().length ? yAxis.ticks() : availableHeight / 36 )
286 .tickSize( -availableWidth, 0);
288 g.select('.nv-y.nv-axis')
295 .getData(scatter.x())
297 .width(availableWidth)
298 .color(data.map(function(d,i) {
299 return d.color || color(d, i);
300 }).filter(function(d,i) { return !data[i].disabled }));
301 gEnter.select('.nv-distWrap').append('g')
302 .attr('class', 'nv-distributionX');
303 g.select('.nv-distributionX')
304 .attr('transform', 'translate(0,' + y.range()[0] + ')')
305 .datum(data.filter(function(d) { return !d.disabled }))
311 .getData(scatter.y())
313 .width(availableHeight)
314 .color(data.map(function(d,i) {
315 return d.color || color(d, i);
316 }).filter(function(d,i) { return !data[i].disabled }));
317 gEnter.select('.nv-distWrap').append('g')
318 .attr('class', 'nv-distributionY');
319 g.select('.nv-distributionY')
321 'translate(' + (rightAlignYAxis ? availableWidth : -distY.size() ) + ',0)')
322 .datum(data.filter(function(d) { return !d.disabled }))
326 //------------------------------------------------------------
332 g.select('.nv-background')
333 .attr('width', availableWidth)
334 .attr('height', availableHeight);
336 g.select('.nv-background').on('mousemove', updateFisheye);
337 g.select('.nv-background').on('click', function() { pauseFisheye = !pauseFisheye;});
338 scatter.dispatch.on('elementClick.freezeFisheye', function() {
339 pauseFisheye = !pauseFisheye;
344 function updateFisheye() {
346 g.select('.nv-point-paths').style('pointer-events', 'all');
350 g.select('.nv-point-paths').style('pointer-events', 'none' );
352 var mouse = d3.mouse(this);
353 x.distortion(fisheye).focus(mouse[0]);
354 y.distortion(fisheye).focus(mouse[1]);
356 g.select('.nv-scatterWrap')
360 g.select('.nv-x.nv-axis').call(xAxis);
363 g.select('.nv-y.nv-axis').call(yAxis);
365 g.select('.nv-distributionX')
366 .datum(data.filter(function(d) { return !d.disabled }))
368 g.select('.nv-distributionY')
369 .datum(data.filter(function(d) { return !d.disabled }))
375 //============================================================
376 // Event Handling/Dispatching (in chart's scope)
377 //------------------------------------------------------------
379 controls.dispatch.on('legendClick', function(d,i) {
380 d.disabled = !d.disabled;
382 fisheye = d.disabled ? 0 : 2.5;
383 g.select('.nv-background') .style('pointer-events', d.disabled ? 'none' : 'all');
384 g.select('.nv-point-paths').style('pointer-events', d.disabled ? 'all' : 'none' );
387 x.distortion(fisheye).focus(0);
388 y.distortion(fisheye).focus(0);
390 g.select('.nv-scatterWrap').call(scatter);
391 g.select('.nv-x.nv-axis').call(xAxis);
392 g.select('.nv-y.nv-axis').call(yAxis);
394 pauseFisheye = false;
400 legend.dispatch.on('stateChange', function(newState) {
401 state.disabled = newState.disabled;
402 dispatch.stateChange(state);
406 scatter.dispatch.on('elementMouseover.tooltip', function(e) {
407 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-distx-' + e.pointIndex)
408 .attr('y1', function(d,i) { return e.pos[1] - availableHeight;});
409 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-disty-' + e.pointIndex)
410 .attr('x2', e.pos[0] + distX.size());
412 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
413 dispatch.tooltipShow(e);
416 dispatch.on('tooltipShow', function(e) {
417 if (tooltips) showTooltip(e, that.parentNode);
420 // Update chart from a state object passed to event handler
421 dispatch.on('changeState', function(e) {
423 if (typeof e.disabled !== 'undefined') {
424 data.forEach(function(series,i) {
425 series.disabled = e.disabled[i];
428 state.disabled = e.disabled;
434 //============================================================
437 //store old scales for use in transitions on update
448 //============================================================
449 // Event Handling/Dispatching (out of chart's scope)
450 //------------------------------------------------------------
452 scatter.dispatch.on('elementMouseout.tooltip', function(e) {
453 dispatch.tooltipHide(e);
455 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-distx-' + e.pointIndex)
457 d3.select('.nv-chart-' + scatter.id() + ' .nv-series-' + e.seriesIndex + ' .nv-disty-' + e.pointIndex)
458 .attr('x2', distY.size());
460 dispatch.on('tooltipHide', function() {
461 if (tooltips) nv.tooltip.cleanup();
464 //============================================================
467 //============================================================
468 // Expose Public Variables
469 //------------------------------------------------------------
471 // expose chart's sub-components
472 chart.dispatch = dispatch;
473 chart.scatter = scatter;
474 chart.legend = legend;
475 chart.controls = controls;
481 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');
482 chart.options = nv.utils.optionsFunc.bind(chart);
484 chart.margin = function(_) {
485 if (!arguments.length) return margin;
486 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
487 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
488 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
489 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
493 chart.width = function(_) {
494 if (!arguments.length) return width;
499 chart.height = function(_) {
500 if (!arguments.length) return height;
505 chart.color = function(_) {
506 if (!arguments.length) return color;
507 color = nv.utils.getColor(_);
514 chart.showDistX = function(_) {
515 if (!arguments.length) return showDistX;
520 chart.showDistY = function(_) {
521 if (!arguments.length) return showDistY;
526 chart.showControls = function(_) {
527 if (!arguments.length) return showControls;
532 chart.showLegend = function(_) {
533 if (!arguments.length) return showLegend;
538 chart.showXAxis = function(_) {
539 if (!arguments.length) return showXAxis;
544 chart.showYAxis = function(_) {
545 if (!arguments.length) return showYAxis;
550 chart.rightAlignYAxis = function(_) {
551 if(!arguments.length) return rightAlignYAxis;
553 yAxis.orient( (_) ? 'right' : 'left');
558 chart.fisheye = function(_) {
559 if (!arguments.length) return fisheye;
564 chart.xPadding = function(_) {
565 if (!arguments.length) return xPadding;
570 chart.yPadding = function(_) {
571 if (!arguments.length) return yPadding;
576 chart.tooltips = function(_) {
577 if (!arguments.length) return tooltips;
582 chart.tooltipContent = function(_) {
583 if (!arguments.length) return tooltip;
588 chart.tooltipXContent = function(_) {
589 if (!arguments.length) return tooltipX;
594 chart.tooltipYContent = function(_) {
595 if (!arguments.length) return tooltipY;
600 chart.state = function(_) {
601 if (!arguments.length) return state;
606 chart.defaultState = function(_) {
607 if (!arguments.length) return defaultState;
612 chart.noData = function(_) {
613 if (!arguments.length) return noData;
618 chart.transitionDuration = function(_) {
619 if (!arguments.length) return transitionDuration;
620 transitionDuration = _;
624 //============================================================