D3 Fisheye Distortion on simple Scatter plot

三世轮回 提交于 2019-12-12 01:36:01

问题


I'm trying to implement the d3 fisheye distortion (http://bost.ocks.org/mike/fisheye/) on a simple scatter plot.

Here the code that I have so far: http://plnkr.co/edit/yDWld6?p=preview

I am very unsure how I should call the circles for the distortion. At the moment it looks like this but nothing happens on "mousemove" so far.

svg.on("mousemove", function() {
  fisheye.center(d3.mouse(this));

  circles
    .selectAll("circle")
    .each(function (d) { d.fisheye = fisheye(d); })
    .attr("cx", function (d) { return d.fisheye.pages })
    .attr("cy", function (d) { return d.fisheye.books });
});

Thanks for the help!


回答1:


You have to prepare the data for the fisheye plugin:

var circles = svg.selectAll("circle")
    .data(data)
  .enter()
  .append("circle")
    .datum( function(d) {
        return {x: d.pages, y: d.books} // change data, to feed to the fisheye plugin
    })
    .attr("cx", function (d) {return d.x}) // changed data can be used here as well
    .attr("cy", function (d) {return d.y}) // ...and here
    .attr("r", 2);

...

// now we can pass in the d.x and d.y values expected by the fisheye plugin...
circles.each(function(d) { d.fisheye = fisheye(d); })
    .attr("cx", function(d) { return d.fisheye.x; })
    .attr("cy", function(d) { return d.fisheye.y; })
    .attr("r", function(d) { return d.fisheye.z * 2; });
});

I also made changes to the declaration of the fisheye in accordance with the latest official version of the plugin which I used in the plunk linked below.

So, here is a PLUNK with the fisheye distortion applied to the scatterplot.



来源:https://stackoverflow.com/questions/23407421/d3-fisheye-distortion-on-simple-scatter-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!