Reputation: 14614
Following this example of using a brush on a d3.js chart, I'm trying to do similar with a line chart where new lines can be added or removed.
I've made a jsFiddle which shows the issue I'm having. Do these two steps:
The two issues are:
I'm guessing, for issue 1, I'd need to alter something in the brushed()
function?
function brushed() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.selectAll("path.line").attr("d", focusLine);
focus.select(".x.axis").call(xAxis);
}
Or, maybe in renderBrush()
? Or in renderLines()
? I'm stumped.
Upvotes: 0
Views: 1089
Reputation: 1446
First, you don't need to update bottom chart scales, i.e. x2 and y2. Also, since you know dates domain initially after reading the csv, you don't need to update it. So, right after you get the data, you should do the following:
// Get min and max of all dates for all the lines.
x.domain([
d3.min(csvdata, function(d) {
return d.date;
}),
d3.max(csvdata, function(d) {
return d.date;
})
]);
// Get 0 and max of all prices for all the lines.
y.domain([
0,
d3.max(csvdata, function(d) {
return d.price;
})
]);
x2.domain(x.domain());
y2.domain(y.domain());
Complete example is here.
Upvotes: 1