如何将预先存在的css类附加到Cytoscape JS for Angular中的节点标签

问题描述 投票:0回答:2

我在整个项目中都有一个统一的CSS主题,尽管节点的CSS不符合。我需要根据白天模式和夜间模式更改css标签颜色,我是如何实现的?

this.cy = cytoscape({
    container: document.getElementById(this.pageId),
    boxSelectionEnabled: false,
    autounselectify: true,
    zoomingEnabled: true,
    panningEnabled: true,
    autoungrabify: false,
    elements: this.nodes,    
    style: [{
      selector: 'node',
      style: {
        'width':'data(cpu)',
        'height': 'data(cpu)',
        //'shape': 'circle',
        'background-image': 'data(HealthImage)',
        'background-fit': 'contain contain',
        'background-color': '#f5f7fa',
        'label': 'data(' + this.nodeName + ')',
        'cssClass': 'form-group', //tried this didn't work
        "text-valign": "bottom",
        "text-halign": "center",
        "font-size": "12px",
        "text-margin-y": "8px"
      }
    }]
});
css angular class cytoscape.js
2个回答
0
投票

您可以使用javascript代码设置cytoscape js样式。也许您可以设置计时器来更改样式或始终检查时间并根据时间设置样式。

http://js.cytoscape.org/#cy.style


0
投票
this.cy = cytoscape({
    container: document.getElementById(this.pageId),
    boxSelectionEnabled: false,
    autounselectify: true,
    zoomingEnabled: true,
    panningEnabled: true,
    autoungrabify: false,
    elements: this.nodes,    
    style: [{
      selector: 'node',
      style: {
        'width':'data(cpu)',
        'height': 'data(cpu)',
        //'shape': 'circle',
        'background-image': 'data(HealthImage)',
        'background-fit': 'contain contain',
        'background-color': '#f5f7fa',
        'label': 'data(' + this.nodeName + ')',
        'cssClass': 'form-group', //tried this didn't work
        "text-valign": "bottom",
        "text-halign": "center",
        "font-size": "12px",
        "text-margin-y": "8px"
      }
    }, {
      selector: '.day',
      style: { "text-background-color": "white" }
    }, {
      selector: '.night',
      style: { "text-background-color": "black" }
    }]
});

在此之后,您只需要为每个节点添加正确的类,您就可以开始了。另外,请记住在添加其他类时删除非活动类。

 cy.elements().toggleClass('day').toggleClass('night');  // maybe replace that with add/removeClass if this doesn't work
© www.soinside.com 2019 - 2024. All rights reserved.