【问题标题】:Can labels be hidden by default, but shown when the node is selected?标签可以默认隐藏,但在选择节点时显示?
【发布时间】:2021-12-12 02:00:44
【问题描述】:

我希望我的图表默认没有标签,但是当我选择一个节点时,它的标签会显示出来。有chosen.label 看起来很有希望,但我仍然不知道如何编写函数。还有a question about scaling.label,但正如其中所示,它似乎也不起作用。

另一种方法是使用复选框来打开和关闭标签。见:Can the filter in the configure option specify an only option?

【问题讨论】:

    标签: label zooming vis.js-network


    【解决方案1】:

    这可以使用chosen.label 选项结合transparent 字体颜色来实现。

    在 options 对象中首先将节点的默认字体颜色设置为透明,然后在 selected.label 中将其调整为可见颜色。

    var options = {
      nodes: {
        font: {
          // Set default label font color to transparent
          color: "transparent"
        },
        chosen: {
          label: function(values, id, selected, hovering) {
            // Adjust label font color so it is visible
            // Updates to the values object are applied to the network
            values.color = "#343434";
          }
        }
      }
    };
    

    工作示例如下。

    // create an array with nodes
    var nodes = new vis.DataSet([
      { id: 1, label: "Node 1" },
      { id: 2, label: "Node 2" },
      { id: 3, label: "Node 3" }
    ]);
    
    // create an array with edges
    var edges = new vis.DataSet([
      { from: 1, to: 3 },
      { from: 1, to: 2 },
      { from: 3, to: 3 },
    ]);
    
    // create a network
    var container = document.getElementById("mynetwork");
    var treeData = {
      nodes: nodes,
      edges: edges,
    };
    var options = {
      nodes: {
        font: {
          // Set default label font color to transparent
          color: "transparent"
        },
        chosen: {
          label: function(values, id, selected, hovering) {
            // Adjust label font color so it is visible
            // Updates to the values object are applied to the network
            values.color = "#343434";
          }
        }
      }
    };
    var network = new vis.Network(container, treeData, options);
    #mynetwork {
      width: 600px;
      height: 180px;
      border: 1px solid lightgray;
    }
    <script src="https://visjs.github.io/vis-network/standalone/umd/vis-network.min.js"></script>
    <div id="mynetwork"></div>

    【讨论】:

    • 是否可以同时选择多个节点?
    • 是的,可以多选,交互下的多选选项必须设置为真interaction: { multiselect: true }。设置完成后,您可以 CTRL + 单击选择多个,此文档为 here。基于上述的工作示例位于jsfiddle.net/1fe7wq0k
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2019-11-27
    • 2017-02-12
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    相关资源
    最近更新 更多