Working with Labels on the Server

Label Layout

In order to calculate label layouts on the server (either using integrated or generic labeling), some prerequisites have to be met:

  • A label model has to be used that can be converted to a corresponding yFiles Java label model on the server. For edge labels, the RotatedSliderEdgeLabelModel, RotatedSideSliderEdgeLabelModel, or FreeEdgeLabelModel should be chosen. For node labels, use InteriorLabelModel or ExteriorLabelModel.
  • The label data has to be transferred between client and server. When using a RoundtripHandler on the client, this can be configured using the updateOptions and sendOptions properties. At least GraphCanvasComponent.OPTION_COPY_LABEL_STYLE should be used in order to transfer the label data. When using a custom GraphMLIOHandler, the readLabels and writeLabels properties allow to configure reading and writing of label data.
  • On the server, class GraphRoundtripSupport has to be used for graph reading and writing. Also, setMapLabelModels(boolean) has to be set to true (the default). This guarantees that the I/O handler used by the roundtrip support will be properly configured to map the client label models to yFiles Java label models and vice versa when the graph is transferred back to the client.

If label mapping is enabled, the label layout data calculated by the layout algorithm will be committed to the label data objects when the graph is written back to the client. If the label has to be accessed/changed before the graph is written back, but after the layout, this can be triggered manually using copyLabelLayouts(LayoutGraph graph).

Adding Labels

Label instances can be added on the server using StyledLayoutGraph's methods addLabel(Node node, Label label) and addLabel(Edge edge, Label label), or by adding the Label instances manually to the list of labels returned by getLabels(Node node) and getLabels(Edge edge).

When a label is added on the server, the layout graph is not automatically updated. This shouldn't be a problem, as long as no label layout is calculated before sending the graph back to the client. In order to synchronize the list of labels with the layout graph, FlexIOTools's static methods syncLabels(Node node, boolean createIfNotExisting) and syncLabels(Edge edge, boolean createIfNotExisting) can be used.

Note that before calling the synchronize methods either a reasonable preferred size should be set on the new labels or a style which implements a LabelSizeCalculator. If no size is set or can be calculated, a default size (40,40) is used. If no preferred size is set and the synchronize methods are not used, the correct preferred size will be calculated on the client when the new label is parsed.