【问题标题】:How to implement hyperlinks in JSON & D3 plugin?如何在 JSON & D3 插件中实现超链接?
【发布时间】:2021-12-31 15:06:57
【问题描述】:

我的目标是让无子节点包含超链接。这是我基于的 D3 插件:https://github.com/deltoss/d3-mitch-tree Image Example

我对 JS 和 JSON 比较陌生,所以我很难弄清楚如何进行,特别是因为在超链接和 JSON 方面几乎没有什么可参考的。如果有更好的方法来解决这个问题,我当然愿意接受新的想法。

提前谢谢你

<script src="https://cdn.jsdelivr.net/gh/deltoss/d3-mitch-tree@1.0.2/dist/js/d3-mitch-tree.min.js"></script>
    <script>
        var data = {
            "id": 1,
            "name": "Animals",
            "type": "Root",
            "description": "A living that feeds on organic matter",
            "children": [
                {
                    "id": 6,
                    "name": "Herbivores",
                    "type": "Type",
                    "description": "Diet consists solely of plant matter",
                    "children": [
                        {
                            "id": 7,
                            "name": "Angus Cattle",
                            "type": "Organism",
                            "description": "Scottish breed of black cattle",
                            "children": []
                        },
                        {
                            "id": 8,
                            "name": "Barb Horse",
                            "type": "Organism",
                            "description": "A breed of Northern African horses with high stamina and hardiness. Their generally hot temperament makes it harder to tame.",
                            "children": []
                        }
                    ]
                }
            ]
        };

        var treePlugin = new d3.mitchTree.boxedTree()
        .setData(data)
        .setElement(document.getElementById("visualisation"))
        .setMinScale(0.5)
        .setAllowZoom(false)
        .setIdAccessor(function(data) {
                return data.id;
            })
            .setChildrenAccessor(function(data) {
                return data.children;
            })
            .setBodyDisplayTextAccessor(function(data) {
                return data.description;
            })
            .setTitleDisplayTextAccessor(function(data) {
                return data.name;
            })

                    
            .initialize();          

    </script>

【问题讨论】:

    标签: javascript json d3.js


    【解决方案1】:

    .initialize()之前链接这个方法:

    .on("nodeClick", function(event, index, arr) {
      if (!event.data.children.length) {
        console.log('you clicked a child-less item', event.data);
      }
    })
    

    在条件中,event.data 是被点击的无子项。随意将 URL 放置在这些对象中并使用这些 URL 导航。

    取自 repo 的 /examples 文件夹,我在其中找到了一个名为 Advanced example with Node Click Event 的文件夹。

    根据同页的评论,也可以使用options语法实现:

    /* const tree = [
      place your data here...
    ]; // */
    new d3.mitchTree.boxedTree({
      events: {
        nodeClick({ data }) {
          if (!data.children.length) {
            console.log('You clicked a childless item', data);
          }
        }
      }
    })
    .setData(tree)
    // ...rest of chain
    .initialize();
    
        
    

    【讨论】:

    • 谢谢你的开始 - 我能够通过实施获得预期的效果:window.open(event.data.url)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多