如何使用cytoscape.js为每个组设置不同的颜色

时间:2019-02-22 06:32:52

标签: background-color cytoscape.js

我最近正在学习cytoscape.js,并且知道可以使用以下代码为组设置背景色:

{
    selector: ':parent',
    style: {
        'background-color': '#ededeb',
        'background-opacity': 0.8
    }
}

但是,所有组的背景颜色都以此方式设置为#ededeb。参见下图,我无法区分黄色区域上的节点属于子组group01还是父组group0

enter image description here

我希望为每个组设置不同的颜色,以便于识别组。我猜代码应该是这样的:

const COLORS = [0x11, 0x22, 0x22, 0x33, ...];// Predefined colors array
let groupColorMap = {}, colorIndex = 0;
cytoscape.stylesheet().selector(':parent').setBackgroundColor(function(node){
    if(groupColorMap[node.id]){
        return groupColorMap[node.id];
    }else{
        return groupColorMap[node.id] = COLORS[(colorIndex++)%COLORS.length];
    }
});

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这只能通过向父样式表添加data()映射器来实现:

通过将映射器添加到样式表,您可以从节点本身检索数据,因此您还可以通过添加诸如parentColor之类的合理属性来定义父级颜色:

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

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: 'node',
      css: {
        'content': 'data(id)',
        'text-valign': 'center',
        'text-halign': 'center'
      }
    },
    {
      selector: '$node > node',
      css: {
        'padding-top': '10px',
        'padding-left': '10px',
        'padding-bottom': '10px',
        'padding-right': '10px',
        'text-valign': 'top',
        'text-halign': 'center',
        'background-color': '#bbb'
      }
    },
    {
      selector: 'edge',
      css: {
        'curve-style': 'bezier',
        'target-arrow-shape': 'triangle'
      }
    },
    {
      selector: ':parent',
      css: {
        'border-color': 'data(parentColor)',
        'line-color': 'black',
        'target-arrow-color': 'black',
        'source-arrow-color': 'black'
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: 'a',
          parent: 'b'
        },
        position: {
          x: 215,
          y: 85
        }
      },
      {
        data: {
          id: 'b',
          parentColor: 'blue'
        }
      },
      {
        data: {
          id: 'c',
          parent: 'b'
        },
        position: {
          x: 300,
          y: 85
        }
      },
      {
        data: {
          id: 'd'
        },
        position: {
          x: 215,
          y: 175
        }
      },
      {
        data: {
          id: 'e',
          parentColor: 'red'
        }
      },
      {
        data: {
          id: 'f',
          parent: 'e'
        },
        position: {
          x: 300,
          y: 175
        }
      }
    ],
    edges: [{
        data: {
          id: 'ad',
          source: 'a',
          target: 'd'
        }
      },
      {
        data: {
          id: 'eb',
          source: 'e',
          target: 'b'
        }
      }

    ]
  },

  layout: {
    name: 'preset',
    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>

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

</html>

有了它,您应该能够将颜色添加到父项中,然后设置好。

相关问题