modify method

  1. @override
AttributesGroups modify(
  1. AttributesGroups groups,
  2. Map<String, ScaleConv<dynamic, num>> scales,
  3. AlgForm form,
  4. CoordConv coord,
  5. Offset origin,
)
override

Modifies the position of mark items.

The aesthetic encodes are in the groups.

Note that the modifier should be functional, which means to return a new groups list as result, not to change the input groups.

Implementation

@override
AttributesGroups modify(
    AttributesGroups groups,
    Map<String, ScaleConv<dynamic, num>> scales,
    AlgForm form,
    CoordConv coord,
    Offset origin) {
  final xField = form.first[0];
  final band = (scales[xField] as DiscreteScaleConv).band;

  final ratio = this.ratio ?? 0.5;

  final random = Random();

  final AttributesGroups rst = [];
  for (var group in groups) {
    final groupRst = <Attributes>[];
    for (var attributes in group) {
      final oldPosition = attributes.position;
      final bias = ratio * band * (random.nextDouble() - 0.5);
      groupRst.add(attributes.withPosition(oldPosition
          .map(
            (point) => Offset(point.dx + bias, point.dy),
          )
          .toList()));
    }
    rst.add(groupRst);
  }

  return rst;
}