【问题标题】:javascript zoomable tree map click the last nodejavascript可缩放树图单击最后一个节点
【发布时间】:2017-10-22 03:27:04
【问题描述】:

如何捕获最后一个节点的点击事件?

我按照本教程(http://bl.ocks.org/ganeshv/6a8e9ada3ab7f2d88022)制作了树图。在我的目的中,我想让最后一个节点可点击,然后将节点数据发送到服务器。

我用开发者工具得到了树图的结构

我在js文件中的代码是:

function display(d) {
    lastGroupArray = [];
    // collectLastGroup(d);

    console.log(lastGroupArray);

    grandparent
        .datum(d.parent)
        .on("click", transition)
        .select("text")
        .text(name(d));

    var g1 = svg.insert("g", ".grandparent")
        .datum(d)
        .attr("class", "depth");

    var g = g1.selectAll("g")
        .data(d._children)
        .enter().append("g");

        g.filter(function (d) { return d._children; })
            .classed("children", true)
            .on("click", transition);

    var children = g.selectAll(".child")
        .data(function(d) { return d._children || [d]; })
        .enter().append("g");

        children.append("rect")
            .attr("class", "child")
            .call(rect)
            .append("title")
            .text(function(d) { return d.name + " (" + formatNumber(d.value) + ")"; });
        //append child text
        children.append("text")
            .attr("class", "ctext")
            .text(function(d) { return d.name; })
            .call(text2);

        //append parent text
        g.append("rect")
            .attr("class", "parent")
            .call(rect);

        var ccc = g.selectAll("rect").on("click", function (d){
            if(typeof d.lastGroup !== 'undefined'){
                d.lastGroup.forEach( function (dd) {
                    // lastGroupArray.push(new LastGroup(dd));     
                    console.log(dd.filePath);
                });
            }
        })

我上面的函数只有在有父节点​​时才能捕获最后一个节点。 如果加载树图时节点是最后一个子节点,则该函数将不起作用。 收款人是加载树形图时的最后一个节点。

这是我的 JSON 数据: 收款人在底部,它只有一个父节点,即根“digit-first2”。

{
"children": [
    {
        "lastGroup": [{
            "filePath": "Jarfile/output/result/ResultFolder/digit-first2-2017-10-22T16-05-53/result1.csv",
            "name": "0~2",
            "value": 112
        }],
        "children": [
            {
                "lastGroup": [{
                    "filePath": "Jarfile/output/result/ResultFolder/digit-first2-2017-10-22T16-05-53/result3.csv",
                    "name": "0~2",
                    "value": 218
                }],
                "children": [{
                    "lastGroup": [{
                        "filePath": "Jarfile/output/result/ResultFolder/digit-first2-2017-10-22T16-05-53/result7.csv",
                        "name": "0~2",
                        "value": 836
                    }],
                    "name": "Payee",
                    "value": 836
                }],
                "name": "Tran Type",
                "value": 218
            },
            {
                "lastGroup": [{
                    "filePath": "Jarfile/output/result/ResultFolder/digit-first2-2017-10-22T16-05-53/result5.csv",
                    "name": "0~2",
                    "value": 834
                }],
                "name": "Payee",
                "value": 834
            }
        ],
        "name": "[Code-Finger-Print, [Memo]]",
        "value": 112
    },
    {
        "lastGroup": [{
            "filePath": "Jarfile/output/result/ResultFolder/digit-first2-2017-10-22T16-05-53/result2.csv",
            "name": "0~2",
            "value": 138
        }],
        "children": [{
            "lastGroup": [{
                "filePath": "Jarfile/output/result/ResultFolder/digit-first2-2017-10-22T16-05-53/result6.csv",
                "name": "0~2",
                "value": 766
            }],
            "name": "Payee",
            "value": 766
        }],
        "name": "Tran Type",
        "value": 138
    },
    {
        "lastGroup": [{
            "filePath": "Jarfile/output/result/ResultFolder/digit-first2-2017-10-22T16-05-53/result4.csv",
            "name": "0~2",
            "value": 731
        }],
        "name": "Payee",
        "value": 731
    }
],
"name": "digit-first2"
}

我上面的函数只有在根不是最后一个节点的父节点时才有效。 我的问题是,如果根节点是该节点的父节点,如何捕获该节点的单击事件。

【问题讨论】:

  • 你找到任何答案了吗..我也陷入了困境。 @kreedz 甄

标签: javascript d3.js treemap


【解决方案1】:

可以通过改变json结构实现最后一个节点点击。最后一个孩子应该有一个更多的孩子键,所有最后一个孩子的属性都复制到具有相同名称的孩子键中。 例如,如果最后一个孩子是 AgglomerativeCluster 。那么结构可以是

children: [
        {
          name: "cluster",
          children: [
            { name: "AgglomerativeCluster", 
            children: [{name: "AgglomerativeCluster",value: 3938,count:39}] },
            { name: "CommunityStructure", value: 3812 },
          ]
        }

在这里找到完整的实现https://codesandbox.io/s/affectionate-thunder-l024x

【讨论】:

    【解决方案2】:

    有一个简单的例子;

    function display(d) {
        grandparent
            .datum(d.parent)
            .on("click", transition)
            .select("text")
            .text(name(d));
    
        var g1 = svg.insert("g", ".grandparent")
            .datum(d)
            .attr("class", "depth");
    
        var g = g1.selectAll("g")
            .data(d._children)
            .enter().append("g");
    
        g.filter(function (d) { return d._children; })
            .classed("children", true)
            .on("click", transition);
    
        var children = g.selectAll(".child")
            .data(function (d) { return d._children || [d]; })
            .enter().append("g");
    
        children.append("rect")
            .attr("class", "child")
            .call(rect)
            .append("title")
            .text(function (d) { 
                return d.key + " (" + formatNumber(d.value) + ")"; 
            });
        children.append("text")
            .attr("class", "ctext")
            .text(function (d) { return d.key; })
            .call(text2);
    
        g.append("rect")
            .attr("class", "parent")
            .call(rect);
    
        var t = g.append("text")
            .attr("class", "ptext")
            .attr("dy", ".75em")
    
        t.append("tspan")
            .text(function (d) { return d.key; });
        t.append("tspan")
            .attr("dy", "1.0em")
            .text(function (d) { return formatNumber(d.value); });
        t.call(text);
    
        var isDeph = false;
    
        g.selectAll("rect")
            .style("fill", function (d) {
                if (d.values != null) {
                    isDeph = true;
                }
                return d.color;
            })
            .attr('onclick', function (d) { 
                if (!isDeph) { 
                    return d.data 
                } 
                return '' 
            });
    
        function transition(d) {
            if (transitioning || !d) 
                return;
            transitioning = true;
    
            var g2 = display(d),
                t1 = g1.transition().duration(750),
                t2 = g2.transition().duration(750);
    
            // Update the domain only after entering new elements.
            x.domain([d.x, d.x + d.dx]);
            y.domain([d.y, d.y + d.dy]);
    
            // Enable anti-aliasing during the transition.
            svg.style("shape-rendering", null);
    
            // Draw child nodes on top of parent nodes.
            svg.selectAll(".depth").sort(function (a, b) { 
                return a.depth - b.depth; 
            });
    
            // Fade-in entering text.
            g2.selectAll("text").style("fill-opacity", 0);
    
            // Transition to the new view.
            t1.selectAll(".ptext").call(text).style("fill-opacity", 0);
            t1.selectAll(".ctext").call(text2).style("fill-opacity", 0);
            t2.selectAll(".ptext").call(text).style("fill-opacity", 1);
            t2.selectAll(".ctext").call(text2).style("fill-opacity", 1);
            t1.selectAll("rect").call(rect);
            t2.selectAll("rect").call(rect);
    
            // Remove the old node when the transition is finished.
            t1.remove().each("end", function () {
                svg.style("shape-rendering", "crispEdges");
                transitioning = false;
            });
        }
    
        return g;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 2017-12-17
      • 2021-11-05
      • 2021-06-21
      • 2014-10-03
      相关资源
      最近更新 更多