D3 geom.hull with custom accessors

 ̄綄美尐妖づ 提交于 2019-12-24 12:19:47

问题


According to the D3 documentation the hull method can be assigned custom accessors to get the x and y coordinates.

Hull documentation

I would like to use these custom accessors, but I can not figure out the syntax. This is what I have done which is basically an unelegant workaround where I manually transform my verticesarray.

var width = 900,
    height = 600;

var svg = d3.select("#content").append("svg")
        .attr("width", width)
        .attr("height", height);

var hull = svg.append("path")
                .attr("class", "hull");

var circle = svg.selectAll("circle");

var vertices = [{"keyWord" : "key", "x" : 10, "y" : 20},
            {"keyWord" : "key1", "x" : 0, "y" : 10},
            {"keyWord" : "key2", "x" : 100, "y" : 25},
            {"keyWord" : "key3", "x" : 80, "y" : 50},
            {"keyWord" : "key4", "x" : 15, "y" : 35},
            {"keyWord" : "key4", "x" : 500, "y" : 500},
];

test = [];

vertices.forEach(function(node){
    test.push([node.x, node.y]);
});

redraw();

function redraw(){
    hull.datum(d3.geom.hull(test)).attr("d", function(d) { return "M" + d.join("L") + "Z"; });
    circle = circle.data(vertices);
    circle.enter().append("circle").attr("r", 3);
    circle.attr("transform", function(d){ return "translate(" + d + ")";});
}

I would like an example to be able to use my vertices array's x and y values directly instead of having to resort to transforming my array.

Here's a fiddle.


回答1:


After trying a little bit I got the code working the way I wanted it initially.

The key to setting the custom accessors was :

var customHull = d3.geom.hull();
customHull.x(function(d){return d.x;});
customHull.y(function(d){return d.y;});

And here is the whole code :

var width = 900,
    height = 600;

var svg = d3.select("#content").append("svg")
        .attr("width", width)
        .attr("height", height);

var hull = svg.append("path")
                .attr("class", "hull");

var circle = svg.selectAll("circle");

vertices = [{"keyWord" : "key", "x" : 10, "y" : 20},
            {"keyWord" : "key1", "x" : 0, "y" : 10},
            {"keyWord" : "key2", "x" : 100, "y" : 25},
            {"keyWord" : "key3", "x" : 80, "y" : 50},
            {"keyWord" : "key4", "x" : 15, "y" : 35},
            {"keyWord" : "key5", "x" : 500, "y" : 500},
];

var customHull = d3.geom.hull();
customHull.x(function(d){return d.x;});
customHull.y(function(d){return d.y;});

redraw();

function redraw(){
    hull.datum(customHull(vertices)).attr("d", function(d) { console.log(d); return "M" + d.map(function(n){ return [n.x, n.y] }).join("L") + "Z"; });
    circle = circle.data(vertices);
    circle.enter().append("circle").attr("r", 3);
    circle.attr("transform", function(d){ return "translate(" + d + ")";});
}

And the jsFiddle http://jsfiddle.net/udvaritibor/3YC5C/1/



来源:https://stackoverflow.com/questions/21092006/d3-geom-hull-with-custom-accessors

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