使用cytoscape.js如何同时显示节点之间的箭头和自定义标签

With cytoscape.js how to show simultaneously arrows and custom labels for the arcs between nodes?

本文关键字:标签 自定义 之间 节点 js cytoscape 何同时 显示 使用      更新时间:2023-09-26

使用cytoscape.js我想同时获得两种效果:

  1. 为节点
  2. 之间的弧线显示有向箭头
  3. 显示节点间弧线的定制标签。

使用下面可用的代码,我只能得到显示的弧标签,而不是箭头。

如果我从代码中删除

style: {'label': 'data(label)'},

那么我可以得到箭头显示,但弧标签消失。

我计划在各种独立的js库等相对复杂的系统中使用我的代码,因此我想听到一个可以在各种条件下灵活使用的解决方案。谢谢你的帮助。

<!DOCTYPE>
<html>
<head>
    <title>cytoscape-springy.js demo</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
    <script src="jquery.min.js"></script>
    <script src="cytoscape.min.js"></script>
    <script src="springy.js"></script>
    <script src="./cytoscape-springy.js"></script>
    <style>
        #cy {
            width: 100%;
            height: 100%;
            position: absolute;
        //  left: 0;
        //  top: 0;
            z-index: 999;
        }

    </style>
    <script>
        $(function(){
            var cy = window.cy = cytoscape({
                container: document.getElementById('cy'),
                layout: {
                    name: 'springy',
                    directed: true
                },
                style: [
                    {
                        selector: 'node',
                        css: {
                            'content': 'data(name)'
                        }
                    },
                    {
                        selector: 'edge',
                        // to get a custom label shown for each arc
                        style: {
                        'label': 'data(label)'
                        },
                        // to get an arrowhead shown for each arc
                        css: {
                            'curve-style': 'bezier',
                            'target-arrow-shape': 'triangle',
                            'target-arrow-color': 'black',
                            'line-color': 'black',
                            'width': 3
                        }
                    }
                ],
                elements: {
                    nodes: [
                        { data: { id: 'a', name: 'a' } },
                        { data: { id: 'b', name: 'b' } },
                        { data: { id: 'c', name: 'c' } }
                    ],
                    edges: [
                        { data: { source: 'a', target: 'b', label: 'a_to_b' } },
                        { data: { source: 'b', target: 'c', label: 'b_to_c' } },
                        { data: { source: 'c', target: 'a', label: 'c_to_a' } }
                    ]
                },
            });
        });
    </script>
</head>
<body>
    <h1>cytoscape-springy demo</h1>
    <div id="cy"></div>
</body>
</html>

指定一个密钥—stylecss密钥—但不能同时指定两个。

                    style: {
                        'label': 'data(label)',
                        'curve-style': 'bezier',
                        'target-arrow-shape': 'triangle',
                        'target-arrow-color': 'black',
                        'line-color': 'black',
                        'width': 3
                    }
相关文章: