cleaned up some code, also refactored some of the cumulativeLine code, still needs some work

master-patched
Bob Monteverde 12 years ago
parent 0fbc206d77
commit c557770f27

@ -1,24 +1,24 @@
nv.models.cumulativeLine = function() {
var margin = {top: 30, right: 20, bottom: 50, left: 60},
var margin = {top: 30, right: 20, bottom: 30, left: 60},
width = 960,
height = 500,
dotRadius = function() { return 2.5 },
color = d3.scale.category10().range(),
dispatch = d3.dispatch('tooltipShow', 'tooltipHide'),
index = {x:0, i:0};
dotRadius = function() { return 2.5 },
getX = function(d) { return d.x },
getY = function(d) { return d.y },
id = Math.floor(Math.random() * 10000); //Create semi-unique ID incase user doesn't select one
var x = d3.scale.linear(),
dx = d3.scale.linear(),
y = d3.scale.linear(),
getX = function(d) { return d.x },
getY = function(d) { return d.y },
xAxis = nv.models.xaxis().scale(x),
yAxis = nv.models.yaxis().scale(y),
legend = nv.models.legend().height(30),
lines = nv.models.line();
lines = nv.models.line(),
dispatch = d3.dispatch('tooltipShow', 'tooltipHide'),
index = {i: 0, x: 0};
var updateChart;
var indexDrag = d3.behavior.drag()
.on('dragstart', dragStart)
@ -29,41 +29,33 @@ nv.models.cumulativeLine = function() {
function dragMove(d,i) {
d.x += d3.event.dx;
d.i = Math.round(dx.invert(d.x));
//if (d.x > x.range()[0]) d.x = x.range()[0];
if (d.x < 0) d.x = 0;
if (d.x > x.range()[1]) d.x = x.range()[1];
index.i = Math.round(dx.invert(d.x));
//d3.transition(d3.select('.chart-' + id)).call(chart);
d3.select(this).attr("transform", "translate(" + dx(d.i) + ",0)");
}
function dragEnd(d,i) {
updateChart(); //could do thisinside of dragMove, but would need to disable transitions.. and performance may be questionable
d3.transition(d3.select('.chart-' + id)).call(chart);
}
function chart(selection) {
//TODO: Think of a better way to update the chart from moving the 'indexLine'
updateChart = function() {
selection.call(chart);
};
selection.each(function(data) {
var series = indexify(index.i, data);
data = indexify(index.i, data);
var series = data.filter(function(d) { return !d.disabled })
var seriesData = series
.filter(function(d) { return !d.disabled })
.map(function(d) { return d.values });
x .domain(d3.extent(d3.merge(series), getX ))
x .domain(d3.extent(d3.merge(seriesData), getX ))
.range([0, width - margin.left - margin.right]);
dx .domain([0, data[0].values.length - 1])
.range([0, width - margin.left - margin.right]);
dx .domain([0, data[0].values.length - 1]) //Assumes all series have same length
.range([0, width - margin.left - margin.right])
.clamp(true);
y .domain(d3.extent(d3.merge(series), getY ))
y .domain(d3.extent(d3.merge(seriesData), getY ))
.range([height - margin.top - margin.bottom, 0]);
lines
@ -74,8 +66,8 @@ nv.models.cumulativeLine = function() {
}).filter(function(d,i) { return !data[i].disabled }))
var wrap = d3.select(this).selectAll('g.wrap').data([data]);
var gEnter = wrap.enter().append('g').attr('class', 'wrap d3lineWithLegend').append('g');
var wrap = d3.select(this).classed('chart-' + id, true).selectAll('g.wrap').data([series]);
var gEnter = wrap.enter().append('g').attr('class', 'wrap d3cumulativeLine').append('g');
gEnter.append('g').attr('class', 'x axis');
gEnter.append('g').attr('class', 'y axis');
@ -83,73 +75,33 @@ nv.models.cumulativeLine = function() {
gEnter.append('g').attr('class', 'legendWrap');
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)
});
lines.dispatch.on('pointMouseover.tooltip', function(e) {
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
});
});
lines.dispatch.on('pointMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
//TODO: margins should be adjusted based on what components are used: axes, axis labels, legend
margin.top = legend.height();
var g = wrap.select('g')
.attr('transform', 'translate(' + margin.left + ',' + legend.height() + ')');
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
legend.width(width/2 - margin.right);
g.select('.legendWrap')
.datum(data)
.attr('transform', 'translate(' + (width/2 - margin.left) + ',' + (-legend.height()) +')')
.attr('transform', 'translate(' + (width/2 - margin.left) + ',' + (-margin.top) +')')
.call(legend);
var linesWrap = g.select('.linesWrap')
.datum(data.filter(function(d) { return !d.disabled }))
.datum(series.filter(function(d) { return !d.disabled }))
d3.transition(linesWrap).call(lines);
//g.select('.linesWrap').selectAll('.indexLine')
var indexLine = linesWrap.selectAll('.indexLine')
var indexLine = linesWrap.selectAll('.indexLine')
.data([index]);
indexLine.enter().append('rect').attr('class', 'indexLine')
indexLine.enter().append('rect').attr('class', 'indexLine')
.attr('width', 3)
.attr('height', height - margin.top - margin.bottom)
.attr('x', -2)
.attr('fill', 'red')
.attr('fill-opacity', .5)
@ -157,6 +109,7 @@ nv.models.cumulativeLine = function() {
indexLine
.attr("transform", function(d) { return "translate(" + dx(d.i) + ",0)" })
.attr('height', height - margin.top - margin.bottom)
xAxis
@ -179,12 +132,60 @@ nv.models.cumulativeLine = function() {
d3.transition(g.select('.y.axis'))
.call(yAxis);
// ********** 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);
});
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)
});
lines.dispatch.on('pointMouseover.tooltip', function(e) {
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
});
});
lines.dispatch.on('pointMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
});
return chart;
}
// ********** FUNCTIONS **********
/* Normalize the data according to an index point. */
function indexify(idx, data) {
return data.map(function(line, i) {
@ -193,25 +194,24 @@ nv.models.cumulativeLine = function() {
return {
key: line.key,
values: line.values.map(function(point) {
return {'x': getX(point), 'y': (getY(point) - v) / (1 + v) };
}),
disabled: line.disabled,
hover: line.hover
/*
if (line.key == "Short") {
if ((options.yaxis.type == 'percent' && v < -90) || (options.yaxis.type == 'bps' && v < -90000)) {
shortDisabled = i;
return {'x': point.x, 'y': 0 };
} else {
shortDisabled = -1;
}
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)
}
*/
return {'x': getX(point), 'y': (getY(point) - v) / (1 + v) };
})
};
});
};
// ********** PUBLIC ACCESSORS **********
chart.dispatch = dispatch;
chart.x = function(_) {

@ -8,22 +8,22 @@ nv.models.lineWithFocus = function() {
height2 = 100,
dotRadius = function() { return 2.5 },
color = d3.scale.category10().range(),
id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
getX = function(d) { return d.x },
getY = function(d) { return d.y },
id = Math.floor(Math.random() * 10000); //Create semi-unique ID incase user doesn't select one
var x = d3.scale.linear(),
y = d3.scale.linear(),
x2 = d3.scale.linear(),
y2 = d3.scale.linear(),
getX = function(d) { return d.x },
getY = function(d) { return d.y },
xAxis = nv.models.xaxis().scale(x),
yAxis = nv.models.yaxis().scale(y),
xAxis2 = nv.models.xaxis().scale(x2),
yAxis2 = nv.models.yaxis().scale(y2),
legend = nv.models.legend().height(30),
focus = nv.models.line(),
context = nv.models.line().dotRadius(.1).interactive(false);
context = nv.models.line().dotRadius(.1).interactive(false),
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
var brush = d3.svg.brush()
.x(x2)

Loading…
Cancel
Save