I am doing a multi-line graph, and I implemented a brush to be able to scale in a specific domain along the x axis. However, when I zoom in, I want the y axis scaled so that its domain goes from [0, maxY], where maxYis the maximum y value for the current x-axis selection. For generating strings I use d3.line()(which has a relationship between x and y values). This is how I calculate the value now maxY:
var maxArray = updatedData.map(function(variable){
var valuesArray = variable.values.map(function(d){
return d.value;
})
return Math.max(...valuesArray);
});
var maxY = Math.max(...maxArray);
And this is where I set the scales and create d3.line():
var xScale = d3.scaleTime()
.range([0, chartWidth]);
var yScale = d3.scaleLinear()
.domain([0, maxY])
.range([chartHeight, 0]);
var brush = d3.brushX()
.on("end", brushend);
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) {return xScale(d.date)})
.y(function(d) {return yScale(d.value)})
var originalDomain = [new Date(data[0].Timestamp), new Date(data[data.length-1].Timestamp)];
xScale.domain(originalDomain);
Here is the code in which I install a new one xScale.domain()and increase it at this interval (which is called when cleaning is completed):
function brushend(){
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return;
var brushInterval = d3.event.selection;
if(!brushInterval) return;
resetButton.attr("disabled", null)
.on("click", resetAxis);
var newDomain = brushInterval.map(xScale.invert, xScale);
xScale.domain(newDomain);
chart.selectAll(".line")
.transition()
.duration(1000)
.attr("d", function(d){ return line(d.values)});
chart.select(".x-axis")
.transition()
.duration(1000)
.call(xAxis);
d3.select(".brush").call(brush.move, null);
}
, y . , , , , (, ). , . d3.line(), .
d3.line()?