documentationfor yFiles for HTML 3.0.0.1

Generic Labeling

This chapter describes the major API changes to the GenericLabeling introduced with yFiles for HTML 3.0 and explains how to migrate from the older version.

Renamed, Moved, and Removed Classes and Members

Renamed, moved, and removed members of classes related to the GenericLabeling (classes are bold)
yFiles for HTML 2.6 yFiles for HTML 3.0 Remarks
GenericLabelingclass name unchanged
Members of class GenericLabeling
affectedLabelsDpKeyPLACE_EDGE_LABELS_DATA_KEY, PLACE_NODE_LABELS_DATA_KEYScope keys are now shared between layout algorithms
autoFlippingremovedAutomatically flipping labels right-side up is handled by the view, e.g. SmartEdgeLabelModel.autoRotation
customProfitModelRatioremovedCustom candidate weights and calculated weights can be combined using nodeLabelCandidateProcessors and edgeLabelCandidateProcessors
edgeGroupOverlapAllowedremoved
labelremovedRun the GenericLabeling algorithm normally. To specify a subset of labels to place see Scope
maximumDurationstopDuration
cptimizationStrategydefaultNodeLabelingCosts, defaultEdgeLabelingCosts
placeEdgeLabelsscope
placeNodeLabels
getProfitremovedSpecifying custom profits for label candidates has been reworked; see Candidate Positions
profitModelremovedSpecifying custom profits for label candidates has been reworked; see Candidate Positions
reduceAmbiguityremovedAmbiguity can be penalized using LabelingCosts.ambiguousPlacementCost
removeEdgeOverlapsremoved
removeNodeOverlapsremoved
LabelingDataGenericLabelingData<TNode,TEdge,TNodeLabel,TEdgeLabel>
Properties in class GenericLabelingData
abortHandlerremovedThe LayoutAbortController is now added by the LayoutExecutor and can be set directly on the LayoutGraphContext
affectedLabelsscope
edgeLabelModelsedgeLabelCandidatesValid edge label positions are instead specified as EdgeLabelCandidates; see Candidate Positions
nodeLabelModelsnodeLabelCandidatesValid node label positions are instead specified as NodeLabelCandidates; see Candidate Positions
edgeLabelPreferredPlacementedgeLabelPreferredPlacements
nodeHalosnodeMargins
OptimizationStrategyLabelingCosts, LabelingOptimizationStrategysee Optimization Strategy

Candidate Positions

yFiles for HTML 3.0 replaces the previous concept of specifying valid label positions using models with the more flexible NodeLabelCandidates and EdgeLabelCandidates. These sets of candidate positions offer convenience methods to create positions corresponding to the previous model implementations, as well as new functionality to add fixed positions. Additionally, weights can be specified for each individual candidate to designate preferred positions.

// specify candidates with different weights (lower weight means that it is less likely chosen by the algorithm)
// creates slider candidates on the left side
const leftSliderCandidates =
  new EdgeLabelCandidates().addSliderCandidates({
    mode: 'single-side',
    distance: 10,
    weight: 1
  })

// adds slider candidates with lower weight on the right side
const candidates = leftSliderCandidates.addSliderCandidates({
  mode: 'single-side',
  distance: -5,
  weight: 0
})

// apply layout
const labeling = new GenericLabeling()
const labelingData = labeling.createLayoutData(graph)
labelingData.edgeLabelCandidates.constant = candidates
labeling.applyLayout(graph)

Instead of specifying the weights when creating the candidates, the nodeLabelCandidateProcessors and edgeLabelCandidateProcessors also offer the option to adjust the weights during the execution of the generic labeling algorithm. This approach is especially helpful when the priorities of label candidates depend on the result of another layout that has not yet run at the time when candidates are specified. Note that, when the delegate is called, the label candidates already contain the weights calculated by the labeling algorithm.

const weightRatio = 0.8
labelingData.edgeLabelCandidateProcessors.constant = (
  candidates,
  label
) => {
  for (const labelCandidate of candidates) {
    const customWeight = calculateCustomWeight(label, labelCandidate)

    // the new weight combines the weight already calculated by the labeling algorithm with a custom weight
    labelCandidate.weight =
      labelCandidate.weight * (1 - weightRatio) +
      customWeight * weightRatio
  }
}

const weightRatio = 0.8
labelingData.edgeLabelCandidateProcessors.constant = (
  candidates: IEnumerable<LabelCandidate>,
  label: LayoutEdgeLabel
) => {
  for (const labelCandidate of candidates) {
    const customWeight = calculateCustomWeight(label, labelCandidate)

    // the new weight combines the weight already calculated by the labeling algorithm with a custom weight
    labelCandidate.weight =
      labelCandidate.weight * (1 - weightRatio) +
      customWeight * weightRatio
  }
}

Scope

The generic labeling algorithm now offers more convenient ways to specify the labels it should handle. While it is still possible to simply restrict the scope to edge labels or node labels using GenericLabeling.scope, the new scope property of the generic labeling data offers the option to select individual labels, either directly or based on their owners.

For more information on the changed scope API, see Scope and Affected Items.

Optimization Strategy

The labeling algorithm’s optimizationStrategy property for prioritizing different layout qualities has been removed. Instead, the optimization strategy can be specified using LabelingCosts and LabelingOptimizationStrategy for each node label or edge label. Each of the old optimization strategies corresponds to a LabelingOptimizationStrategy, which can be used to construct a LabelingCosts instance that replicates the old behavior. Costs applying to all node labels and edge labels respectively can be specified with defaultNodeLabelingCosts and defaultEdgeLabelingCosts. Individual costs that supersede the default may be specified using the nodeLabelingCosts and edgeLabelingCosts properties of the labeling data.

Note that there is also a new cost preset AMBIGUOUS_PLACEMENTS that replaces the old reduceAmbiguity property of the generic labeling algorithm.