我在我的有角项目中使用cytoscape,当我创建新的细胞图时,我尝试使用一种样式的"background-opacity"
属性,但是当我为项目提供服务时,在编译后它会在控制台中引发错误
ERROR in src/app/graph/graph.component.ts(101,5): error TS2322: Type '({ selector: string; style: { 'background-color': string; label: string; }; } | { selector: string; style: { color: string; "background-color": string; "background-opacity": string; "text-valign": string; label: string; }; } | { ...; })[]' is not assignable to type 'Stylesheet[] | Promise<Stylesheet[]>'.
Type '({ selector: string; style: { 'background-color': string; label: string; }; } | { selector: string; style: { color: string; "background-color": string; "background-opacity": string; "text-valign": string; label: string; }; } | { ...; })[]' is not assignable to type 'Stylesheet[]'.
Type '{ selector: string; style: { 'background-color': string; label: string; }; } | { selector: string; style: { color: string; "background-color": string; "background-opacity": string; "text-valign": string; label: string; }; } | { ...; }' is not assignable to type 'Stylesheet'.
Type '{ selector: string; style: { color: string; "background-color": string; "background-opacity": string; "text-valign": string; label: string; }; }' is not assignable to type 'Stylesheet'.
Type '{ selector: string; style: { color: string; "background-color": string; "background-opacity": string; "text-valign": string; label: string; }; }' is not assignable to type 'StylesheetStyle'.
Types of property 'style' are incompatible.
Type '{ color: string; "background-color": string; "background-opacity": string; "text-valign": string; label: string; }' is not assignable to type 'Node | Edge | Core'.
Type '{ color: string; "background-color": string; "background-opacity": string; "text-valign": string; label: string; }' is not assignable to type 'Node'.
Types of property '"background-opacity"' are incompatible.
Type 'string' is not assignable to type 'PropertyValue<NodeSingular, number>'.
它显示了图形,但一直给我一个错误。当我从样式表中删除属性时,它消失了。 这是我的细胞样式表部分
style: [
{
selector: 'node',
style: {
'background-color': 'black',
label: 'data(id)'
}
},
{
selector: '.count',
style: {
color: 'white',
"background-color": 'black',
"background-opacity": '0',
"text-valign": 'center',
label: 'data(id)'
}
},
{
selector: 'edge',
style: {
"text-margin-y": '-10',
label: 'data(value)'
}
}
]
答案 0 :(得分:2)
我通常只将background-color
属性与rgba(0,0,0,0)
一起使用来设置不透明度,但是cytoscape.js仅接受rgba,十六进制,颜色名称和色相。因此,您可以使用opacity
键设置不透明度:
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
style: [{
selector: 'node',
css: {
'label': 'data(id)',
'background-color': 'black',
}
},
{
selector: '.count',
css: {
'color': 'white',
'background-color': 'rgb(0,0,0)',
'opacity': '0.5',
'text-valign': 'center',
'label': 'data(id)',
}
},
{
selector: 'edge',
css: {
'text-margin-y': '-10',
'label': 'data(value)'
}
}
],
elements: {
nodes: [{
data: {
id: 'n0'
}
},
{
data: {
id: 'n1'
}
},
{
data: {
id: 'n2'
}
},
{
data: {
id: 'n3'
}
}
],
edges: [{
data: {
source: 'n0',
target: 'n1',
arrow: 'triangle',
value: '0->1'
}
},
{
data: {
source: 'n1',
target: 'n2',
arrow: 'triangle',
value: '1->2'
}
},
{
data: {
source: 'n1',
target: 'n3',
arrow: 'triangle',
value: '1->3'
}
},
]
},
layout: {
name: 'concentric',
minNodeSpacing: 140,
}
});
cy.ready(() => {
cy.fit()
cy.center()
cy.$('#n1').addClass('count')
})
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 100%;
width: 100%;
left: 0;
top: 0;
float: left;
position: absolute;
}
<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://cdn.jsdelivr.net/npm/cytoscape@3.10.1/dist/cytoscape.min.js"></script>
</head>
<body>
<div id="cy"></div>
</body>
</html>