2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
nv.models.lineWithLegend = function() {
|
|
|
|
var margin = {top: 30, right: 20, bottom: 50, left: 60},
|
2012-06-09 06:51:41 +00:00
|
|
|
color = d3.scale.category20().range(),
|
|
|
|
width, height;
|
|
|
|
|
|
|
|
var lines = nv.models.line(),
|
|
|
|
//x = d3.scale.linear(),
|
|
|
|
//y = d3.scale.linear(),
|
|
|
|
x = lines.xScale(),
|
|
|
|
y = lines.yScale(),
|
2012-05-17 06:04:33 +00:00
|
|
|
xAxis = nv.models.axis().scale(x).orient('bottom'),
|
|
|
|
yAxis = nv.models.axis().scale(y).orient('left'),
|
2012-03-20 23:43:21 +00:00
|
|
|
legend = nv.models.legend().height(30),
|
2012-05-28 22:31:26 +00:00
|
|
|
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
function chart(selection) {
|
|
|
|
selection.each(function(data) {
|
|
|
|
|
2012-06-09 06:51:41 +00:00
|
|
|
var availableWidth = (width || parseInt(d3.select(this).style('width')) || 960)
|
|
|
|
- margin.left - margin.right,
|
|
|
|
availableHeight = (height || parseInt(d3.select(this).style('height')) || 400)
|
|
|
|
- margin.top - margin.bottom;
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
lines
|
2012-04-05 20:49:27 +00:00
|
|
|
.width(availableWidth)
|
|
|
|
.height(availableHeight)
|
2012-03-20 23:43:21 +00:00
|
|
|
.color(data.map(function(d,i) {
|
2012-06-09 06:51:41 +00:00
|
|
|
return d.color || color[i % 10];
|
|
|
|
}).filter(function(d,i) { return !data[i].disabled }));
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-05-28 22:31:26 +00:00
|
|
|
|
2012-06-01 21:11:19 +00:00
|
|
|
var wrap = d3.select(this).selectAll('g.wrap.lineWithLegend').data([data]);
|
|
|
|
var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 lineWithLegend').append('g');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
gEnter.append('g').attr('class', 'x axis');
|
|
|
|
gEnter.append('g').attr('class', 'y axis');
|
|
|
|
gEnter.append('g').attr('class', 'linesWrap');
|
|
|
|
gEnter.append('g').attr('class', 'legendWrap');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO: margins should be adjusted based on what components are used: axes, axis labels, legend
|
|
|
|
margin.top = legend.height();
|
|
|
|
|
|
|
|
var g = wrap.select('g')
|
2012-04-05 20:49:27 +00:00
|
|
|
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
2012-06-09 06:51:41 +00:00
|
|
|
|
2012-05-28 02:11:43 +00:00
|
|
|
legend.width(availableWidth / 2);
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
g.select('.legendWrap')
|
|
|
|
.datum(data)
|
2012-05-28 02:11:43 +00:00
|
|
|
.attr('transform', 'translate(' + (availableWidth / 2) + ',' + (-margin.top) +')')
|
2012-03-20 23:43:21 +00:00
|
|
|
.call(legend);
|
|
|
|
|
|
|
|
|
2012-06-09 06:51:41 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
var linesWrap = g.select('.linesWrap')
|
|
|
|
.datum(data.filter(function(d) { return !d.disabled }))
|
|
|
|
|
|
|
|
d3.transition(linesWrap).call(lines);
|
|
|
|
|
|
|
|
|
2012-06-09 06:51:41 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
xAxis
|
|
|
|
.domain(x.domain())
|
|
|
|
.range(x.range())
|
2012-05-28 02:11:43 +00:00
|
|
|
.ticks( availableWidth / 100 )
|
2012-04-05 20:49:27 +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);
|
|
|
|
|
2012-05-28 02:11:43 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
yAxis
|
|
|
|
.domain(y.domain())
|
|
|
|
.range(y.range())
|
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) {
|
|
|
|
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
|
|
|
lines.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 07:40:10 +00:00
|
|
|
lines.dispatch.on('elementMouseout.tooltip', function(e) {
|
2012-05-28 22:31:26 +00:00
|
|
|
dispatch.tooltipHide(e);
|
|
|
|
});
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
});
|
|
|
|
|
2012-06-09 06:51:41 +00:00
|
|
|
|
|
|
|
// If the legend changed the margin's height, need to recalc positions... should think of a better way to prevent duplicate work
|
|
|
|
if (margin.top != legend.height())
|
|
|
|
chart(selection);
|
|
|
|
|
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
return chart;
|
|
|
|
}
|
|
|
|
|
2012-05-28 02:11:43 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
chart.dispatch = dispatch;
|
2012-04-05 09:18:21 +00:00
|
|
|
chart.legend = legend;
|
|
|
|
chart.xAxis = xAxis;
|
|
|
|
chart.yAxis = yAxis;
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-05-28 02:11:43 +00:00
|
|
|
d3.rebind(chart, lines, 'x', 'y', 'size', 'xDomain', 'yDomain', 'forceX', 'forceY', 'interactive', 'clipEdge', 'clipVoronoi', 'id');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
chart.margin = function(_) {
|
|
|
|
if (!arguments.length) return margin;
|
|
|
|
margin = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.width = function(_) {
|
2012-05-28 02:11:43 +00:00
|
|
|
if (!arguments.length) return width;
|
2012-06-09 06:51:41 +00:00
|
|
|
width = _;
|
|
|
|
//width = d3.functor(_);
|
2012-03-20 23:43:21 +00:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.height = function(_) {
|
2012-05-28 02:11:43 +00:00
|
|
|
if (!arguments.length) return height;
|
2012-06-09 06:51:41 +00:00
|
|
|
height = _;
|
|
|
|
//height = d3.functor(_);
|
2012-03-20 23:43:21 +00:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|