2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
nv.models.cumulativeLine = function() {
|
2012-03-23 04:44:52 +00:00
|
|
|
var margin = {top: 30, right: 20, bottom: 30, left: 60},
|
2012-05-14 21:09:45 +00:00
|
|
|
getWidth = function() { return 960 },
|
|
|
|
getHeight = function() { return 500 },
|
2012-05-29 04:35:31 +00:00
|
|
|
color = d3.scale.category20().range(),
|
2012-03-23 04:44:52 +00:00
|
|
|
dotRadius = function() { return 2.5 },
|
|
|
|
getX = function(d) { return d.x },
|
|
|
|
getY = function(d) { return d.y },
|
2012-06-01 16:23:03 +00:00
|
|
|
id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one
|
2012-06-01 17:11:18 +00:00
|
|
|
showRescaleToggle = true,
|
2012-06-01 16:23:03 +00:00
|
|
|
rescaleY = true;
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
var x = d3.scale.linear(),
|
|
|
|
dx = d3.scale.linear(),
|
|
|
|
y = d3.scale.linear(),
|
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-22 23:26:02 +00:00
|
|
|
legend = nv.models.legend().height(30),
|
2012-06-01 16:23:03 +00:00
|
|
|
controls = nv.models.legend().height(30),
|
2012-03-23 04:44:52 +00:00
|
|
|
lines = nv.models.line(),
|
|
|
|
dispatch = d3.dispatch('tooltipShow', 'tooltipHide'),
|
|
|
|
index = {i: 0, x: 0};
|
2012-03-22 23:26:02 +00:00
|
|
|
|
2012-06-01 16:23:03 +00:00
|
|
|
//TODO: let user select default
|
|
|
|
var controlsData = [
|
|
|
|
{ key: 'Re-scale y-axis' }
|
|
|
|
];
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
var indexDrag = d3.behavior.drag()
|
|
|
|
.on('dragstart', dragStart)
|
|
|
|
.on('drag', dragMove)
|
|
|
|
.on('dragend', dragEnd);
|
|
|
|
|
|
|
|
function dragStart(d,i) {}
|
|
|
|
|
|
|
|
function dragMove(d,i) {
|
|
|
|
d.x += d3.event.dx;
|
2012-03-23 04:44:52 +00:00
|
|
|
d.i = Math.round(dx.invert(d.x));
|
2012-03-22 23:26:02 +00:00
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
//d3.transition(d3.select('.chart-' + id)).call(chart);
|
2012-03-22 23:26:02 +00:00
|
|
|
d3.select(this).attr("transform", "translate(" + dx(d.i) + ",0)");
|
|
|
|
}
|
|
|
|
|
|
|
|
function dragEnd(d,i) {
|
2012-03-23 04:44:52 +00:00
|
|
|
d3.transition(d3.select('.chart-' + id)).call(chart);
|
2012-03-22 23:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function chart(selection) {
|
|
|
|
selection.each(function(data) {
|
2012-05-14 21:09:45 +00:00
|
|
|
var width = getWidth(),
|
|
|
|
height = getHeight(),
|
|
|
|
availableWidth = width - margin.left - margin.right,
|
|
|
|
availableHeight = height - margin.top - margin.bottom;
|
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
var series = indexify(index.i, data);
|
2012-03-22 23:26:02 +00:00
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
var seriesData = series
|
2012-06-01 16:23:03 +00:00
|
|
|
.filter(function(d) { return !rescaleY || !d.disabled }) // only filter out if rescaling y axis
|
2012-03-22 23:26:02 +00:00
|
|
|
.map(function(d) { return d.values });
|
|
|
|
|
2012-06-01 16:23:03 +00:00
|
|
|
|
2012-05-14 21:09:45 +00:00
|
|
|
x .domain(d3.extent(d3.merge(seriesData), function(d) { return d.x } ))
|
2012-06-01 16:23:03 +00:00
|
|
|
.range([0, availableWidth]);
|
2012-03-22 23:26:02 +00:00
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
dx .domain([0, data[0].values.length - 1]) //Assumes all series have same length
|
2012-06-01 16:23:03 +00:00
|
|
|
.range([0, availableWidth])
|
2012-03-23 04:44:52 +00:00
|
|
|
.clamp(true);
|
2012-03-22 23:26:02 +00:00
|
|
|
|
2012-05-14 21:09:45 +00:00
|
|
|
y .domain(d3.extent(d3.merge(seriesData), function(d) { return d.y } ))
|
2012-06-01 16:23:03 +00:00
|
|
|
.range([availableHeight, 0]);
|
2012-03-22 23:26:02 +00:00
|
|
|
|
2012-05-14 21:09:45 +00:00
|
|
|
|
2012-03-22 23:26:02 +00:00
|
|
|
lines
|
2012-06-01 16:23:03 +00:00
|
|
|
.width(availableWidth)
|
|
|
|
.height(availableHeight)
|
2012-03-22 23:26:02 +00:00
|
|
|
.color(data.map(function(d,i) {
|
2012-06-20 21:04:30 +00:00
|
|
|
return d.color || color[i % color.length];
|
2012-03-22 23:26:02 +00:00
|
|
|
}).filter(function(d,i) { return !data[i].disabled }))
|
|
|
|
|
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
var wrap = d3.select(this).classed('chart-' + id, true).selectAll('g.wrap').data([series]);
|
2012-06-01 21:11:19 +00:00
|
|
|
var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 cumulativeLine').append('g');
|
2012-03-22 23:26:02 +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');
|
2012-06-01 16:23:03 +00:00
|
|
|
gEnter.append('g').attr('class', 'controlsWrap');
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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-03-23 04:44:52 +00:00
|
|
|
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
legend.width(width/2 - margin.right);
|
|
|
|
|
|
|
|
g.select('.legendWrap')
|
|
|
|
.datum(data)
|
2012-03-23 04:44:52 +00:00
|
|
|
.attr('transform', 'translate(' + (width/2 - margin.left) + ',' + (-margin.top) +')')
|
2012-03-22 23:26:02 +00:00
|
|
|
.call(legend);
|
|
|
|
|
2012-06-01 17:11:18 +00:00
|
|
|
if (showRescaleToggle) {
|
|
|
|
controls.width(140).color(['#444', '#444', '#444']);
|
|
|
|
g.select('.controlsWrap')
|
|
|
|
.datum(controlsData)
|
|
|
|
.attr('transform', 'translate(0,' + (-margin.top) +')')
|
|
|
|
.call(controls);
|
|
|
|
}
|
2012-06-01 16:23:03 +00:00
|
|
|
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
var linesWrap = g.select('.linesWrap')
|
2012-03-23 04:44:52 +00:00
|
|
|
.datum(series.filter(function(d) { return !d.disabled }))
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
d3.transition(linesWrap).call(lines);
|
|
|
|
|
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
var indexLine = linesWrap.selectAll('.indexLine')
|
2012-03-22 23:26:02 +00:00
|
|
|
.data([index]);
|
2012-03-23 04:44:52 +00:00
|
|
|
indexLine.enter().append('rect').attr('class', 'indexLine')
|
2012-03-22 23:26:02 +00:00
|
|
|
.attr('width', 3)
|
|
|
|
.attr('x', -2)
|
|
|
|
.attr('fill', 'red')
|
|
|
|
.attr('fill-opacity', .5)
|
|
|
|
.call(indexDrag)
|
|
|
|
|
|
|
|
indexLine
|
|
|
|
.attr("transform", function(d) { return "translate(" + dx(d.i) + ",0)" })
|
2012-06-01 16:23:03 +00:00
|
|
|
.attr('height', availableHeight)
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
xAxis
|
|
|
|
.domain(x.domain())
|
|
|
|
.range(x.range())
|
2012-05-14 21:30:40 +00:00
|
|
|
.ticks( width / 100 )
|
2012-06-01 16:23:03 +00:00
|
|
|
.tickSize(-availableHeight, 0);
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
g.select('.x.axis')
|
|
|
|
.attr('transform', 'translate(0,' + y.range()[0] + ')');
|
|
|
|
d3.transition(g.select('.x.axis'))
|
|
|
|
.call(xAxis);
|
|
|
|
|
|
|
|
yAxis
|
|
|
|
.domain(y.domain())
|
|
|
|
.range(y.range())
|
|
|
|
.ticks( height / 36 )
|
2012-06-01 16:23:03 +00:00
|
|
|
.tickSize(-availableWidth, 0);
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
d3.transition(g.select('.y.axis'))
|
|
|
|
.call(yAxis);
|
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ********** EVENT LISTENERS **********
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2012-05-11 20:01:51 +00:00
|
|
|
/*
|
2012-03-23 04:44:52 +00:00
|
|
|
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-11 20:01:51 +00:00
|
|
|
*/
|
2012-03-23 04:44:52 +00:00
|
|
|
|
2012-06-01 16:23:03 +00:00
|
|
|
|
|
|
|
controls.dispatch.on('legendClick', function(d,i) {
|
|
|
|
d.disabled = !d.disabled;
|
|
|
|
rescaleY = !d.disabled;
|
|
|
|
|
|
|
|
//console.log(d,i,arguments);
|
|
|
|
|
|
|
|
selection.transition().call(chart);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-31 07:40:10 +00:00
|
|
|
lines.dispatch.on('elementMouseover.tooltip', function(e) {
|
2012-03-23 04:44:52 +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-03-23 04:44:52 +00:00
|
|
|
dispatch.tooltipHide(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-03-22 23:26:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
|
|
|
|
// ********** FUNCTIONS **********
|
|
|
|
|
2012-03-22 23:26:02 +00:00
|
|
|
/* Normalize the data according to an index point. */
|
|
|
|
function indexify(idx, data) {
|
|
|
|
return data.map(function(line, i) {
|
2012-05-14 21:09:45 +00:00
|
|
|
var v = getY(line.values[idx], idx);
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
key: line.key,
|
2012-05-14 21:09:45 +00:00
|
|
|
values: line.values.map(function(point, pointIndex) {
|
|
|
|
return {'x': getX(point, pointIndex), 'y': (getY(point, pointIndex) - v) / (1 + v) };
|
2012-03-23 04:44:52 +00:00
|
|
|
}),
|
|
|
|
disabled: line.disabled,
|
|
|
|
hover: line.hover
|
2012-03-22 23:26:02 +00:00
|
|
|
/*
|
2012-03-23 04:44:52 +00:00
|
|
|
if (v < -.9) {
|
|
|
|
//if a series loses more than 100%, calculations fail.. anything close can cause major distortion (but is mathematically currect till it hits 100)
|
2012-03-22 23:26:02 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-23 04:44:52 +00:00
|
|
|
|
|
|
|
// ********** PUBLIC ACCESSORS **********
|
|
|
|
|
2012-03-22 23:26:02 +00:00
|
|
|
chart.dispatch = dispatch;
|
|
|
|
|
|
|
|
chart.x = function(_) {
|
|
|
|
if (!arguments.length) return getX;
|
|
|
|
getX = _;
|
2012-05-14 21:09:45 +00:00
|
|
|
//lines.x(_);
|
2012-03-22 23:26:02 +00:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.y = function(_) {
|
|
|
|
if (!arguments.length) return getY;
|
|
|
|
getY = _;
|
2012-05-14 21:09:45 +00:00
|
|
|
//lines.y(_);
|
2012-03-22 23:26:02 +00:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.margin = function(_) {
|
|
|
|
if (!arguments.length) return margin;
|
|
|
|
margin = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-05-14 21:09:45 +00:00
|
|
|
/*
|
2012-03-22 23:26:02 +00:00
|
|
|
chart.width = function(_) {
|
|
|
|
if (!arguments.length) return width;
|
|
|
|
width = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.height = function(_) {
|
|
|
|
if (!arguments.length) return height;
|
|
|
|
height = _;
|
|
|
|
return chart;
|
|
|
|
};
|
2012-05-14 21:09:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
chart.width = function(_) {
|
|
|
|
if (!arguments.length) return getWidth;
|
|
|
|
getWidth = d3.functor(_);
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.height = function(_) {
|
|
|
|
if (!arguments.length) return getHeight;
|
|
|
|
getHeight = d3.functor(_);
|
|
|
|
return chart;
|
|
|
|
};
|
2012-03-22 23:26:02 +00:00
|
|
|
|
2012-06-01 16:23:03 +00:00
|
|
|
chart.color = function(_) {
|
|
|
|
if (!arguments.length) return color;
|
|
|
|
color = _;
|
2012-06-01 17:11:18 +00:00
|
|
|
legend.color(_);
|
2012-06-01 16:23:03 +00:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-03-22 23:26:02 +00:00
|
|
|
chart.dotRadius = function(_) {
|
|
|
|
if (!arguments.length) return dotRadius;
|
|
|
|
dotRadius = d3.functor(_);
|
|
|
|
lines.dotRadius = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-06-01 17:11:18 +00:00
|
|
|
chart.showRescaleToggle = function(_) {
|
|
|
|
if (!arguments.length) return showRescaleToggle;
|
|
|
|
showRescaleToggle = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-03-22 23:26:02 +00:00
|
|
|
|
|
|
|
// Expose the x-axis' tickFormat method.
|
|
|
|
//chart.xAxis = {};
|
|
|
|
//d3.rebind(chart.xAxis, xAxis, 'tickFormat');
|
|
|
|
chart.xAxis = xAxis;
|
|
|
|
|
|
|
|
// Expose the y-axis' tickFormat method.
|
|
|
|
//chart.yAxis = {};
|
|
|
|
//d3.rebind(chart.yAxis, yAxis, 'tickFormat');
|
|
|
|
chart.yAxis = yAxis;
|
|
|
|
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|