Category / Section
How to set CSSClass property for the nodes/connectors/labels/ports
2 mins read
You can customize the appearance of a node by setting Cascading Style Sheet (CSS) class name as the value for cssClass property. Similarly, you also can apply CSS for connectors, labels, decorators and ports using theirs cssClass property.
The following code illustrates how to set CSS class for Diagram elements.
HTML
<style> .fillNode { fill: green; } .lineColor { stroke: red; } .text:hover { cursor:text; } </style>
Java-script
//set a class name to node’s cssClass property to customize its appearance var node = { name: "node1", width: 100, height: 100, offsetX: 150, offsetY: 150, cssClass: "fillNode"}; //set a class name to label’s cssClass property to customize its cursor node.labels: [{ text: "Node", cssClass: "text" }]; //set a class name to port ‘s cssClass property to customize its appearance node.ports = [{ name: "aport", offset: { x: 0, y: 0.5 }, visibility: ej.datavisualization.Diagram.PortVisibility.Visible, cssClass: "fillNode" }] //set a class name to connector ‘s cssClass property to customize its appearance var connector = { name: "connector1", sourcePoint: { x: 200, y: 300 }, targetPoint: { x: 300, y: 400 }, cssClass: "lineColor" };
C#
Node node = new Node(); node.Name = "Node1"; node.OffsetX = 150; node.OffsetY = 150; //set a class name to node’s cssClass property to customize its appearance node.CssClass = "fillNode"; Label label = new Label(); label.text = "Node"; //set a class name to label’s cssClass property to customize its cursor label.CssClass = "text"; Port port = new Port(); port.Offset = new DiagramPoint(0f, 0.5f); port.Visibility = PortVisibility.Visible; //set a class name to port ‘s cssClass property to customize its appearance port.CssClass = "fillNode"; node.Ports.Add(port); Connector connector = new Connector (); connector.Name = "Connector1"; connector.SourcePoint = new DiagramPoint(200f, 300f); connector.TargetPoint = new DiagramPoint(300f, 400f); //set a class name to connector ‘s cssClass property to customize its appearance connector.CssClass = " lineColor ";
The limitations of using the CSSClass are as follows.
- When we export the diagram, the style applied via CssClass will not be exported.
- Serialization (save and load) can be performed in the diagram. The class properties which we define will be serialized as a string and the style properties should be maintained it in the application level.
- If we define the style property such as stroke,fill,stroke-width in both CssClass and the node’s properties, then the style defined in the CssClass will be rendered.
- If we hover on the label, the CssClass which we have applied for the node will not be reflected because we have rendered the node’s and connector’s in the different layers.
- CssClass will not applied for the elements which have inline styles.