2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
nv.models.scatterWithLegend = function() {
|
|
|
|
var margin = {top: 30, right: 20, bottom: 50, left: 60},
|
2012-05-31 08:00:01 +00:00
|
|
|
width = function() { return 960 }, //TODO: should probably make this consistent... or maybe constant in the models, closure in the charts
|
2012-05-28 02:11:43 +00:00
|
|
|
height = function() { return 500 },
|
2012-03-20 23:43:21 +00:00
|
|
|
xAxisLabelText = false,
|
|
|
|
yAxisLabelText = false,
|
2012-05-28 06:50:36 +00:00
|
|
|
showDistX = false,
|
|
|
|
showDistY = false,
|
2012-05-31 08:00:01 +00:00
|
|
|
color = d3.scale.category20().range();
|
|
|
|
|
|
|
|
var xAxis = nv.models.axis().orient('bottom').tickPadding(10),
|
|
|
|
yAxis = nv.models.axis().orient('left').tickPadding(10),
|
2012-03-20 23:43:21 +00:00
|
|
|
legend = nv.models.legend().height(30),
|
2012-05-28 22:31:26 +00:00
|
|
|
scatter = nv.models.scatter(),
|
2012-05-29 00:06:08 +00:00
|
|
|
dispatch = d3.dispatch('tooltipShow', 'tooltipHide'),
|
2012-05-31 08:00:01 +00:00
|
|
|
x, y, x0, y0;
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
function chart(selection) {
|
|
|
|
selection.each(function(data) {
|
2012-05-31 08:00:01 +00:00
|
|
|
var availableWidth = width() - margin.left - margin.right,
|
2012-05-28 02:11:43 +00:00
|
|
|
availableHeight = height() - margin.top - margin.bottom;
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-05-31 08:00:01 +00:00
|
|
|
x0 = x0 || scatter.xScale();
|
|
|
|
y0 = y0 || scatter.yScale();
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-01 21:11:19 +00:00
|
|
|
var wrap = d3.select(this).selectAll('g.wrap.scatterWithLegend').data([data]);
|
|
|
|
var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 scatterWithLegend').append('g');
|
2012-05-31 08:00:01 +00:00
|
|
|
|
|
|
|
gEnter.append('g').attr('class', 'legendWrap');
|
|
|
|
gEnter.append('g').attr('class', 'x axis');
|
|
|
|
gEnter.append('g').attr('class', 'y axis');
|
|
|
|
gEnter.append('g').attr('class', 'scatterWrap');
|
|
|
|
//gEnter.append('g').attr('class', 'distWrap');
|
2012-05-29 00:06:08 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
scatter
|
2012-05-28 02:11:43 +00:00
|
|
|
.width(availableWidth)
|
|
|
|
.height(availableHeight)
|
2012-05-31 08:00:01 +00:00
|
|
|
//.xDomain(x.domain())
|
|
|
|
//.yDomain(y.domain())
|
2012-03-20 23:43:21 +00:00
|
|
|
.color(data.map(function(d,i) {
|
|
|
|
return d.color || color[i % 20];
|
|
|
|
}).filter(function(d,i) { return !data[i].disabled }))
|
|
|
|
|
2012-05-31 08:00:01 +00:00
|
|
|
var scatterWrap = wrap.select('.scatterWrap')
|
|
|
|
.datum(data.filter(function(d) { return !d.disabled }));
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-05-31 08:00:01 +00:00
|
|
|
d3.transition(scatterWrap).call(scatter);
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
2012-05-31 08:00:01 +00:00
|
|
|
x = scatter.xScale();
|
|
|
|
y = scatter.yScale();
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-05-31 08:00:01 +00:00
|
|
|
xAxis.scale(x);
|
|
|
|
yAxis.scale(y);
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
//TODO: margins should be adjusted based on what components are used: axes, axis labels, legend
|
2012-05-29 04:35:31 +00:00
|
|
|
//TODO: Fix height issue on first render if legend height is larger than margin.top, NEED TO FIX EVERY MODEL WITH A LEGEND
|
2012-03-20 23:43:21 +00:00
|
|
|
margin.top = legend.height();
|
|
|
|
|
|
|
|
var g = wrap.select('g')
|
|
|
|
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
|
|
|
|
|
|
|
|
2012-05-28 02:11:43 +00:00
|
|
|
legend.width(availableWidth / 2);
|
|
|
|
|
|
|
|
wrap.select('.legendWrap')
|
|
|
|
.datum(data)
|
|
|
|
.attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')')
|
|
|
|
.call(legend);
|
|
|
|
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
2012-05-28 22:31:26 +00:00
|
|
|
if ( showDistX || showDistY) {
|
|
|
|
var distWrap = scatterWrap.selectAll('g.distribution')
|
2012-05-29 00:06:08 +00:00
|
|
|
.data(function(d) { return d }, function(d) { return d.key });
|
2012-05-28 06:03:23 +00:00
|
|
|
|
2012-05-28 22:31:26 +00:00
|
|
|
distWrap.enter().append('g').attr('class', function(d,i) { return 'distribution series-' + i })
|
2012-05-28 06:03:23 +00:00
|
|
|
|
2012-05-28 22:31:26 +00:00
|
|
|
distWrap.style('stroke', function(d,i) { return color.filter(function(d,i) { return data[i] && !data[i].disabled })[i % 10] })
|
|
|
|
}
|
2012-05-28 06:03:23 +00:00
|
|
|
|
2012-05-28 22:31:26 +00:00
|
|
|
if (showDistX) {
|
|
|
|
var distX = distWrap.selectAll('line.distX')
|
|
|
|
.data(function(d) { return d.values })
|
|
|
|
distX.enter().append('line')
|
2012-05-29 00:06:08 +00:00
|
|
|
.attr('x1', function(d,i) { return x0(scatter.x()(d,i)) })
|
|
|
|
.attr('x2', function(d,i) { return x0(scatter.x()(d,i)) })
|
2012-05-28 22:31:26 +00:00
|
|
|
//d3.transition(distX.exit())
|
|
|
|
d3.transition(distWrap.exit().selectAll('line.distX'))
|
|
|
|
.attr('x1', function(d,i) { return x(scatter.x()(d,i)) })
|
|
|
|
.attr('x2', function(d,i) { return x(scatter.x()(d,i)) })
|
|
|
|
.remove();
|
|
|
|
distX
|
|
|
|
.attr('class', function(d,i) { return 'distX distX-' + i })
|
|
|
|
.attr('y1', y.range()[0])
|
|
|
|
.attr('y2', y.range()[0] + 8);
|
|
|
|
d3.transition(distX)
|
|
|
|
.attr('x1', function(d,i) { return x(scatter.x()(d,i)) })
|
|
|
|
.attr('x2', function(d,i) { return x(scatter.x()(d,i)) })
|
|
|
|
}
|
2012-05-28 06:03:23 +00:00
|
|
|
|
|
|
|
|
2012-05-28 22:31:26 +00:00
|
|
|
if (showDistY) {
|
2012-05-28 06:03:23 +00:00
|
|
|
var distY = distWrap.selectAll('line.distY')
|
|
|
|
.data(function(d) { return d.values })
|
|
|
|
distY.enter().append('line')
|
2012-05-29 00:06:08 +00:00
|
|
|
.attr('y1', function(d,i) { return y0(scatter.y()(d,i)) })
|
|
|
|
.attr('y2', function(d,i) { return y0(scatter.y()(d,i)) });
|
2012-05-28 06:03:23 +00:00
|
|
|
//d3.transition(distY.exit())
|
|
|
|
d3.transition(distWrap.exit().selectAll('line.distY'))
|
|
|
|
.attr('y1', function(d,i) { return y(scatter.y()(d,i)) })
|
|
|
|
.attr('y2', function(d,i) { return y(scatter.y()(d,i)) })
|
|
|
|
.remove();
|
|
|
|
distY
|
|
|
|
.attr('class', function(d,i) { return 'distY distY-' + i })
|
|
|
|
.attr('x1', x.range()[0])
|
|
|
|
.attr('x2', x.range()[0] - 8)
|
|
|
|
d3.transition(distY)
|
2012-05-29 00:06:08 +00:00
|
|
|
.attr('y1', function(d,i) { return y(scatter.y()(d,i)) }) .attr('y2', function(d,i) { return y(scatter.y()(d,i)) });
|
2012-05-28 22:31:26 +00:00
|
|
|
}
|
2012-05-28 06:03:23 +00:00
|
|
|
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
xAxis
|
2012-05-28 02:11:43 +00:00
|
|
|
.ticks( availableWidth / 100 )
|
2012-05-31 08:00:01 +00:00
|
|
|
.tickSize( -availableHeight , 0);
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
g.select('.x.axis')
|
|
|
|
.attr('transform', 'translate(0,' + y.range()[0] + ')');
|
|
|
|
|
|
|
|
d3.transition(g.select('.x.axis'))
|
|
|
|
.call(xAxis);
|
|
|
|
|
|
|
|
|
|
|
|
yAxis
|
2012-05-28 02:11:43 +00:00
|
|
|
.ticks( availableHeight / 36 )
|
|
|
|
.tickSize( -availableWidth, 0);
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
d3.transition(g.select('.y.axis'))
|
|
|
|
.call(yAxis);
|
|
|
|
|
2012-05-28 22:31:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
legend.dispatch.on('legendClick', function(d,i, that) {
|
|
|
|
d.disabled = !d.disabled;
|
|
|
|
|
|
|
|
if (!data.filter(function(d) { return !d.disabled }).length) {
|
|
|
|
data.map(function(d) {
|
|
|
|
d.disabled = false;
|
|
|
|
wrap.selectAll('.series').classed('disabled', false);
|
|
|
|
return d;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
selection.transition().call(chart)
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
legend.dispatch.on('legendMouseover', function(d, i) {
|
|
|
|
d.hover = true;
|
|
|
|
selection.transition().call(chart)
|
|
|
|
});
|
|
|
|
|
|
|
|
legend.dispatch.on('legendMouseout', function(d, i) {
|
|
|
|
d.hover = false;
|
|
|
|
selection.transition().call(chart)
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-05-31 07:40:10 +00:00
|
|
|
scatter.dispatch.on('elementMouseover.tooltip', function(e) {
|
2012-05-28 22:31:26 +00:00
|
|
|
dispatch.tooltipShow({
|
|
|
|
point: e.point,
|
|
|
|
series: e.series,
|
|
|
|
pos: [e.pos[0] + margin.left, e.pos[1] + margin.top],
|
|
|
|
seriesIndex: e.seriesIndex,
|
|
|
|
pointIndex: e.pointIndex
|
|
|
|
});
|
2012-05-31 08:00:01 +00:00
|
|
|
|
|
|
|
scatterWrap.select('.series-' + e.seriesIndex + ' .distX-' + e.pointIndex)
|
|
|
|
.attr('y1', e.pos[1]);
|
|
|
|
scatterWrap.select('.series-' + e.seriesIndex + ' .distY-' + e.pointIndex)
|
|
|
|
.attr('x1', e.pos[0]);
|
2012-05-28 22:31:26 +00:00
|
|
|
});
|
|
|
|
|
2012-05-31 07:40:10 +00:00
|
|
|
scatter.dispatch.on('elementMouseout.tooltip', function(e) {
|
2012-05-28 22:31:26 +00:00
|
|
|
dispatch.tooltipHide(e);
|
|
|
|
|
2012-05-31 08:00:01 +00:00
|
|
|
scatterWrap.select('.series-' + e.seriesIndex + ' .distX-' + e.pointIndex)
|
|
|
|
.attr('y1', y.range()[0]);
|
|
|
|
scatterWrap.select('.series-' + e.seriesIndex + ' .distY-' + e.pointIndex)
|
|
|
|
.attr('x1', x.range()[0]);
|
2012-05-28 22:31:26 +00:00
|
|
|
});
|
|
|
|
|
2012-05-29 00:06:08 +00:00
|
|
|
|
|
|
|
//store old scales for use in transitions on update, to animate from old to new positions, and sizes
|
|
|
|
x0 = x.copy();
|
|
|
|
y0 = y.copy();
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
chart.dispatch = dispatch;
|
2012-05-28 02:11:43 +00:00
|
|
|
chart.legend = legend;
|
|
|
|
chart.xAxis = xAxis;
|
|
|
|
chart.yAxis = yAxis;
|
|
|
|
|
2012-05-31 08:00:01 +00:00
|
|
|
d3.rebind(chart, scatter, 'interactive', 'size', 'xScale', 'yScale', 'zScale', 'xDomain', 'yDomain', 'sizeDomain', 'forceX', 'forceY', 'forceSize', 'clipVoronoi', 'clipRadius');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-05-27 23:52:28 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
chart.margin = function(_) {
|
|
|
|
if (!arguments.length) return margin;
|
|
|
|
margin = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.width = function(_) {
|
|
|
|
if (!arguments.length) return width;
|
2012-05-28 02:11:43 +00:00
|
|
|
width = d3.functor(_);
|
2012-03-20 23:43:21 +00:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.height = function(_) {
|
|
|
|
if (!arguments.length) return height;
|
2012-05-28 02:11:43 +00:00
|
|
|
height = d3.functor(_);
|
2012-03-20 23:43:21 +00:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-05-29 08:12:28 +00:00
|
|
|
chart.color = function(_) {
|
|
|
|
if (!arguments.length) return color;
|
|
|
|
color = _;
|
|
|
|
legend.color(_);
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-05-28 06:50:36 +00:00
|
|
|
chart.showDistX = function(_) {
|
|
|
|
if (!arguments.length) return showDistX;
|
|
|
|
showDistX = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.showDistY = function(_) {
|
|
|
|
if (!arguments.length) return showDistY;
|
|
|
|
showDistY = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|