节点上的内容和标签-Cytoscape

时间:2019-01-09 17:29:31

标签: cytoscape.js

我正在尝试使用“内容”和下面的文本标签显示在节点中心具有字体图标的节点。

我的样式当前为:

{
  'selector': 'node[icon]',
  'style': {
    'content': 'data(icon)',
    'font-family': 'Material Icons',
    'text-valign': 'center',
    'text-halign': 'center'
  }
},
{
  'selector': 'node[label]',
  'style': {
    'label': 'data(label)',
    'text-valign': 'bottom',
    'text-halign': 'center'
  }
}

但是,这不起作用,因为我假设在一个元素(节点)上使用了两种样式。

我考虑过一些解决方案,例如:

  • 将标签放置在父节点上
  • 使用Popper.js或类似标签显示标签
  • 使用多行标签

前两个似乎是“ hacky”,第三个可能导致很多对齐问题。有更好的解决方案吗?

2 个答案:

答案 0 :(得分:0)

如果要将图标作为节点主体,则可以将其用作背景图像,并像定义标签一样定义标签:

var cy = window.cy = cytoscape({
  container: document.getElementById('cy'),

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: 'node',
      css: {
        'label': 'data(id)',
        'text-valign': 'bottom',
        'text-halign': 'center',
        'height': '60px',
        'width': '60px',
        'border-color': 'black',
        'border-opacity': '1',
        'background-image': 'https://farm8.staticflickr.com/7272/7633179468_3e19e45a0c_b.jpg',
        "text-background-opacity": 1,
        "text-background-color": "lightgray"
      }
    },
    {
      selector: ':selected',
      css: {
        'background-color': 'black',
        'line-color': 'black',
        'target-arrow-color': 'black',
        'source-arrow-color': 'black'
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: 'n0'
        }
      },
      {
        data: {
          id: 'n1'
        }
      },
      {
        data: {
          id: 'n2'
        }
      },
      {
        data: {
          id: 'n3'
        }
      },
      {
        data: {
          id: 'n4'
        }
      },
      {
        data: {
          id: 'n5'
        }
      },
      {
        data: {
          id: 'n6'
        }
      },
      {
        data: {
          id: 'n7'
        }
      },
      {
        data: {
          id: 'n8'
        }
      },
      {
        data: {
          id: 'n9'
        }
      },
      {
        data: {
          id: 'n10'
        }
      },
      {
        data: {
          id: 'n11'
        }
      },
      {
        data: {
          id: 'n12'
        }
      },
      {
        data: {
          id: 'n13'
        }
      },
      {
        data: {
          id: 'n14'
        }
      },
      {
        data: {
          id: 'n15'
        }
      },
      {
        data: {
          id: 'n16'
        }
      }
    ],
    edges: [{
        data: {
          source: 'n0',
          target: 'n1'
        }
      },
      {
        data: {
          source: 'n1',
          target: 'n2'
        }
      },
      {
        data: {
          source: 'n1',
          target: 'n3'
        }
      },
      {
        data: {
          source: 'n2',
          target: 'n7'
        }
      },
      {
        data: {
          source: 'n2',
          target: 'n11'
        }
      },
      {
        data: {
          source: 'n2',
          target: 'n16'
        }
      },
      {
        data: {
          source: 'n3',
          target: 'n4'
        }
      },
      {
        data: {
          source: 'n3',
          target: 'n16'
        }
      },
      {
        data: {
          source: 'n4',
          target: 'n5'
        }
      },
      {
        data: {
          source: 'n4',
          target: 'n6'
        }
      },
      {
        data: {
          source: 'n6',
          target: 'n8'
        }
      },
      {
        data: {
          source: 'n8',
          target: 'n9'
        }
      },
      {
        data: {
          source: 'n8',
          target: 'n10'
        }
      },
      {
        data: {
          source: 'n11',
          target: 'n12'
        }
      },
      {
        data: {
          source: 'n12',
          target: 'n13'
        }
      },
      {
        data: {
          source: 'n13',
          target: 'n14'
        }
      },
      {
        data: {
          source: 'n13',
          target: 'n15'
        }
      },
    ]
  },

  layout: {
    name: 'dagre',
    padding: 5
  }
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 75%;
  position: absolute;
  left: 0;
  top: 0;
  float: left;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.17/cytoscape.min.js"></script>
  <script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
  <script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>

答案 1 :(得分:0)

我找到了使用扩展名https://github.com/kaluginserg/cytoscape-node-html-label的解决方案。

您可以为不干扰基本Cytoscape标签的节点创建自定义HTML标签。使用材质图标的示例:

// Initialise the HTML Label
this.cy.nodeHtmlLabel([{
  query: '.nodeIcon',
  halign: 'center',
  valign: 'center',
  halignBox: 'center',
  valignBox: 'center',
  tpl: (data) => {
    return '<i class="material-icons">' + data.icon + '</i>';
  }
}]);

// Add the HTML Label to the node:
const node =  {
  group: 'nodes',
  data: {
    id: data.id,
    label: data.label,
    icon: data.icon
  },
  classes: 'nodeIcon' // <---- Add the HTML Label class here
};

使用该方法,您可以动态创建带有字体图标的节点,而无需下载大量图像。

Node Icon

您甚至可以添加CSS样式来更改图标的颜色:

enter image description here