addBehavior method

void addBehavior(
  1. ChartBehavior<D> behavior
)

Attaches a behavior to the chart.

Setting a behavior with the same role as a behavior already attached to the chart will replace the old behavior. The old behavior's removeFrom method will be called before we attach the behavior.

Implementation

void addBehavior(ChartBehavior<D> behavior) {
  final role = behavior.role;

  if (_behaviorRoleMap[role] != behavior) {
    // Remove any old behavior with the same role.
    removeBehavior(_behaviorRoleMap[role]);
    // Add the behavior.
    _behaviorRoleMap[role] = behavior;
  }

  // Add the behavior if it wasn't already added.
  if (!_behaviorStack.contains(behavior)) {
    _behaviorStack.add(behavior);
    behavior.attachTo(this);
  }
}