0

I'm trying to implements booth zoom and brush snapping so the selection area set rather by zoom or a brush behaivour coincides rigidly with the bars boundaries. Please use illustration as a reference.

enter image description here

I have managed to make a very simple sketch and zoom works fine, but not brush as long as it tries to call recursively and I'm getting

d3.v7.min.js:2 Uncaught RangeError: Maximum call stack size exceeded.

In other words, need a working way at brushed() function to re-adjust brush selection range after doing it manually by hand.

is trying to call brushed forever , since

context.select(".brush").call(brush.move, [xb(continous[0].round()), xb(continous[1].round()) - offset]); 

var data = [{"date":"01 Jan 2000","price":"1394.46"},{"date":"02 Jan 2000","price":"1366.42"},{"date":"03 Jan 2000","price":"1498.58"},{"date":"04 Jan 2000","price":"1452.43"},{"date":"05 Jan 2000","price":"1420.6"},{"date":"06 Jan 2000","price":"1454.6"},{"date":"07 Jan 2000","price":"1430.83"},{"date":"08 Jan 2000","price":"1517.68"},{"date":"09 Jan 2000","price":"1436.51"},{"date":"10 Jan 2000","price":"1429.4"},{"date":"11 Jan 2000","price":"1314.95"},{"date":"12 Jan 2000","price":"1320.28"},{"date":"13 Jan 2000","price":"1366.01"},{"date":"14 Jan 2000","price":"1239.94"},{"date":"15 Jan 2000","price":"1160.33"},{"date":"16 Jan 2000","price":"1249.46"},{"date":"17 Jan 2000","price":"1255.82"},{"date":"18 Jan 2000","price":"1224.38"},{"date":"19 Jan 2000","price":"1211.23"},{"date":"20 Jan 2000","price":"1133.58"},{"date":"21 Jan 2000","price":"1040.94"},{"date":"22 Jan 2000","price":"1059.78"},{"date":"23 Jan 2000","price":"1139.45"},{"date":"24 Jan 2000","price":"1148.08"},{"date":"25 Jan 2000","price":"1130.2"},{"date":"26 Jan 2000","price":"1106.73"},{"date":"27 Jan 2000","price":"1147.39"},{"date":"28 Jan 2000","price":"1076.92"},{"date":"29 Jan 2000","price":"1067.14"},{"date":"30 Jan 2000","price":"989.82"}];
    
Date.prototype.round = function(){
    var dateObj = new Date(+this+43200000);
    return new Date(dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate());
};
    
var sourceEvent = null;
var interval = d3.timeHour.every(24);
var screen = {start: null, end: null };
    
var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 110, left: 40},
    margin2 = {top: 430, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

var parseDate = d3.timeParse("%d %b %Y");

var x = d3.scaleTime().range([0, width]),
    x2 = d3.scaleTime().range([0, width]),
    xb = d3.scaleBand().rangeRound([0, width]).padding(0.2),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]),
    xb2 = d3.scaleBand().rangeRound([0, width]).padding(0.2);
    
var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

var brush = d3.brushX()
    .extent([[0, 0], [width, height2]])
    .on("brush end", brushed);

var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([[0, 0], [width, height]])
    .extent([[0, 0], [width, height]])
    .on("zoom", zoomed);

var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x(parseDate(d.date)); })
    .y0(height)
    .y1(function(d) { return y(Number(d.price)); });

var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x2(parseDate(d.date)); })
    .y0(height2)
    .y1(function(d) { return y2(Number(d.price)); });

svg.append("defs").append("clipPath")
    .attr("id", "clip")
    .append("rect")
    .attr("width", width)
    .attr("height", height);

var focus = svg.append("g")
    .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

  x.domain(d3.extent(data, function(d) { return parseDate(d.date); }));
  y.domain([0, d3.max(data, function(d) { return Number(d.price); })]);
  x2.domain(x.domain());
  y2.domain(y.domain());
    
  xb.domain(d3.extent(data, function(d) { return parseDate(d.date); }));
  xb2.domain(d3.extent(data, function(d) { return parseDate(d.date); }));

  focus.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  focus.append("g")
      .attr("class", "axis axis--y")
      .call(yAxis);

  context.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

  svg.append("rect")
      .attr("class", "zoom")
      .attr("width", width)
      .attr("height", height)
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);
    
  drawBars(focus, xb, y, data, 0, data.length - 1, height);
  drawBars(context, xb2, y2, data, 0, data.length - 1, height2);


function drawBars(parent, x, y, data, x0, x1, height){
    
    x.domain(data.slice(x0, x1).map((d_) => { return parseDate(d_.date); }));
    parent.selectAll(".bar").remove();
    parent.selectAll(".bar")
    .data(data.slice(x0, x1))
    .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(parseDate(d.date)); })
    .attr("y", function(d) { return y(Number(d.price)); })
    .attr("width", xb.bandwidth())
    .attr("height", function(d) { return height - y(Number(d.price)); })
    .attr("fill", "#4682B4")
    
}
    
function brushed(event) {
    
  if (sourceEvent === "zoom") return; // ignore brush-by-zoom
  sourceEvent = "brush";

  //if(!event.selection) return;
    
  var s = event.selection || x2.range();

  let continous = [x2.invert(s[0]).round(), x2.invert(s[1]).round()];
    
  x.domain([continous[0].round(), continous[1].round()]);

  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (x2(continous[1]) - x2(continous[0])))
      .translate(-x2(continous[0]), 0));
    
  let offset = xb.padding() * xb.bandwidth();
  //context.select(".brush").call(brush.move, [xb(continous[0].round()), xb(continous[1].round()) - offset]); 
  sourceEvent = null;
    
}

function zoomed(event) {
    
  if (sourceEvent === "brush") return; // ignore zoom-by-brush
  sourceEvent = "zoom";
  var t = event.transform;
  let continous = t.rescaleX(x2).domain();
  x.domain([continous[0].round(), continous[1].round()]);
  let offset = xb.padding() * xb.bandwidth();
  if(!isNaN(xb(continous[0].round())) && !isNaN(xb(continous[1].round()) - offset)){
    context.select(".brush").call(brush.move, [xb(continous[0].round()), xb(continous[1].round()) - offset]); 
  }
  sourceEvent = null;
}

function type(d) {
  d.date = parseDate(d.date);
  d.price = +d.price;
  return d;
}
.area {
  fill: steelblue;
  clip-path: url(#clip);
}

.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}
<script src="https://d3js.org/d3.v7.min.js"></script>
<svg width="960" height="500"></svg>
toowren
  • 85
  • 7

1 Answers1

0

It seems that I've found solution with a little bug-zoom doesn't go back to full extent.

var data = [{"date":"01 Jan 2000","price":"1394.46"},{"date":"02 Jan 2000","price":"1366.42"},{"date":"03 Jan 2000","price":"1498.58"},{"date":"04 Jan 2000","price":"1452.43"},{"date":"05 Jan 2000","price":"1420.6"},{"date":"06 Jan 2000","price":"1454.6"},{"date":"07 Jan 2000","price":"1430.83"},{"date":"08 Jan 2000","price":"1517.68"},{"date":"09 Jan 2000","price":"1436.51"},{"date":"10 Jan 2000","price":"1429.4"},{"date":"11 Jan 2000","price":"1314.95"},{"date":"12 Jan 2000","price":"1320.28"},{"date":"13 Jan 2000","price":"1366.01"},{"date":"14 Jan 2000","price":"1239.94"},{"date":"15 Jan 2000","price":"1160.33"},{"date":"16 Jan 2000","price":"1249.46"},{"date":"17 Jan 2000","price":"1255.82"},{"date":"18 Jan 2000","price":"1224.38"},{"date":"19 Jan 2000","price":"1211.23"},{"date":"20 Jan 2000","price":"1133.58"},{"date":"21 Jan 2000","price":"1040.94"},{"date":"22 Jan 2000","price":"1059.78"},{"date":"23 Jan 2000","price":"1139.45"},{"date":"24 Jan 2000","price":"1148.08"},{"date":"25 Jan 2000","price":"1130.2"},{"date":"26 Jan 2000","price":"1106.73"},{"date":"27 Jan 2000","price":"1147.39"},{"date":"28 Jan 2000","price":"1076.92"},{"date":"29 Jan 2000","price":"1067.14"},{"date":"30 Jan 2000","price":"989.82"}];
    
Date.prototype.round = function(){
    var dateObj = new Date(+this+43200000);
    return new Date(dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate());
};
    
var sourceEvent = null;
var interval = d3.timeHour.every(24);
var screen = {start: null, end: null };
    
var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 110, left: 40},
    margin2 = {top: 430, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

var parseDate = d3.timeParse("%d %b %Y");

var x = d3.scaleTime().range([0, width]),
    x2 = d3.scaleTime().range([0, width]),
    xb = d3.scaleBand().rangeRound([0, width]).padding(0.2),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]),
    xb2 = d3.scaleBand().rangeRound([0, width]).padding(0.2);
    
var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

var brush = d3.brushX()
    .extent([[0, 0], [width, height2]])
    .on("brush end", brushed);

var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([[0, 0], [width, height]])
    .extent([[0, 0], [width, height]])
    .on("zoom", zoomed);

var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x(parseDate(d.date)); })
    .y0(height)
    .y1(function(d) { return y(Number(d.price)); });

var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x2(parseDate(d.date)); })
    .y0(height2)
    .y1(function(d) { return y2(Number(d.price)); });

svg.append("defs").append("clipPath")
    .attr("id", "clip")
    .append("rect")
    .attr("width", width)
    .attr("height", height);

var focus = svg.append("g")
    .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

  x.domain(d3.extent(data, function(d) { return parseDate(d.date); }));
  y.domain([0, d3.max(data, function(d) { return Number(d.price); })]);
  x2.domain(x.domain());
  y2.domain(y.domain());
    
  xb.domain(d3.extent(data, function(d) { return parseDate(d.date); }));
  xb2.domain(d3.extent(data, function(d) { return parseDate(d.date); }));

  focus.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  focus.append("g")
      .attr("class", "axis axis--y")
      .call(yAxis);

  context.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

  svg.append("rect")
      .attr("class", "zoom")
      .attr("width", width)
      .attr("height", height)
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);
    
  drawBars(focus, xb, y, data, 0, data.length - 1, height);
  drawBars(context, xb2, y2, data, 0, data.length - 1, height2);


function drawBars(parent, x, y, data, x0, x1, height){
    
    x.domain(data.slice(x0, x1).map((d_) => { return parseDate(d_.date); }));
    parent.selectAll(".bar").remove();
    parent.selectAll(".bar")
    .data(data.slice(x0, x1))
    .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) { return x(parseDate(d.date)); })
    .attr("y", function(d) { return y(Number(d.price)); })
    .attr("width", xb.bandwidth())
    .attr("height", function(d) { return height - y(Number(d.price)); })
    .attr("fill", "#4682B4")
    
}
    
function brushed(event) {
    
  if(!event.sourceEvent) return;
  if(!event.selection) return;
    
  if (sourceEvent === "zoom") return; // ignore brush-by-zoom
  sourceEvent = "brush";
    
  var s = event.selection || x2.range();

  let continous = [x2.invert(s[0]).round(), x2.invert(s[1]).round()];
    
  x.domain([continous[0].round(), continous[1].round()]);

  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (x2(continous[1]) - x2(continous[0])))
      .translate(-x2(continous[0]), 0));
    
  let offset = xb.padding() * xb.bandwidth();
  context.select(".brush").call(brush.move, [xb(continous[0].round()), xb(continous[1].round()) - offset]); 
  sourceEvent = null;
    
}

function zoomed(event) {
    
  if (sourceEvent === "brush") return; // ignore zoom-by-brush
  sourceEvent = "zoom";
  var t = event.transform;
  let continous = t.rescaleX(x2).domain();
  x.domain([continous[0].round(), continous[1].round()]);
  let offset = xb.padding() * xb.bandwidth();
  if(!isNaN(xb(continous[0].round())) && !isNaN(xb(continous[1].round()) - offset)){
    context.select(".brush").call(brush.move, [xb(continous[0].round()), xb(continous[1].round()) - offset]); 
  }
  sourceEvent = null;
}

function type(d) {
  d.date = parseDate(d.date);
  d.price = +d.price;
  return d;
}
.area {
  fill: steelblue;
  clip-path: url(#clip);
}

.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v7.min.js"></script>
toowren
  • 85
  • 7
  • It would be great if you point out what the bug was. – phanaz Sep 02 '22 at 10:22
  • Actually, round() causes this bug, so instead of x.domain([continous[0].round(), continous[1].round()]);, you have to use x.domain([continous[0].floor(), continous[1].ceil()]); – toowren Sep 02 '22 at 14:42