zooming in a scatterplot with the density contour added (V4)

47 views
Skip to first unread message

guillaume.cha...@gmail.com

unread,
Jul 5, 2017, 5:39:16 AM7/5/17
to d3-js
Hi,
I have realized this kind of plot: 
and I have added this zoom: 

However, I don't know how to 'redraw' the contour part of the plot.

    function zoom() {
      var t = svg.transition().duration(750);
      svg.select(".axis--x").transition(t).call(xAxis);
      svg.select(".axis--y").transition(t).call(yAxis);
      svg.selectAll(".dot").transition(t)
        .attr("cx", function(d) {
          return x(d.x);
        })
        .attr("cy", function(d) {
          return y(d.y);
        });
      svg.selectAll("path").transition(t) 
        .attr("d", d3.geoPath());

    }

Thanks

guillaume.cha...@gmail.com

unread,
Jul 6, 2017, 3:57:49 AM7/6/17
to d3-js

steve rickus

unread,
Jul 6, 2017, 11:23:48 AM7/6/17
to d3-js
I think you need a projection scales x,y data points -- something like this:

    var xyPath = d3.geoTransform({
      point
: function(ptx, pty) {
       
this.stream.point(x(ptx), y(pty));
     
}
   
});

Then, where you define your path generator for the initial plot, pass the projection as the first arg, instead of overriding the contourDensity.x and .y functions:

    .selectAll("path")
   
.data(d3.contourDensity()
       
.x(function(d) { return d.x; })
       
.y(function(d) { return d.y; })
       
.size([width, height])
       
.bandwidth(20)
     
(plotdata))
   
.enter().append("path")
     
.attr("d", d3.geoPath(xyPath));

You also need to use the same projection for the path generator during the zoom transition:

      svg2.selectAll(".levelLine path").transition(t)
       
.attr("d", d3.geoPath(xyPath));

Notice that the selectAll needs to get the "path" elements under the div.levelLine... Also, I needed to move your data array definition to the top of the script to avoid referencing it before it was defined.
--
Steve

guillaume.cha...@gmail.com

unread,
Jul 6, 2017, 12:28:31 PM7/6/17
to d3-js

In this updated fiddle https://jsfiddle.net/ja4qnLnt/  thanks to your advice, I think there is 'scale' issue in the projection scales xyPath.

Thanks Steve for your awesome contribution
Reply all
Reply to author
Forward
0 new messages