Appearance
Color
This example shows how to use the color attribute. Multiple color formats are available: hexadecimal, RGB(A), and CSS color names.
ts
import Ogma from '@linkurious/ogma';
const COLORS = [
'#161344',
'#3f1c4c',
'#632654',
'#86315b',
'#a93c63',
'#cd476a',
'#f35371'
];
const ogma = new Ogma({
container: 'graph-container'
});
// Create a rule that assigns a color to each node using a function.
// This function must be deterministic as it will be called each time the node is painted.
ogma.styles.addNodeRule({
color: n => COLORS[+n.getId() % COLORS.length]
});
// Generate a random graph
ogma.generate
.random({ nodes: 20, edges: 0 })
// Assign this graph to Ogma.
.then(graph => ogma.setGraph(graph))
.then(() => ogma.layouts.force({ locate: true, duration: 0, gravity: 0.03 }))
.then(() => {
// Color the node '0' in orange.
ogma.getNode('0')!.setAttributes({ color: 'orange' });
// Color the nodes who have a coordinate x above 90 in purple.
ogma
.getNodes()
.filter(n => n.getAttribute('x') > 90)
.setAttributes({
color: 'purple'
});
});
html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link type="text/css" rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="graph-container"></div>
<script type="module" src="index.ts"></script>
</body>
</html>
css
#graph-container {
top: 0;
bottom: 0;
left: 0;
right: 0;
position: absolute;
margin: 0;
overflow: hidden;
}