decodeAnnotationSegment<T> static method

AnnotationSegment<Object>? decodeAnnotationSegment<T>(
  1. dynamic map, {
  2. bool validate = true,
})

Decodes the object from a Map-like dynamic structure. This expects the JSON format to be one of the following structures:

{
  "axisId": <String>,
  "axisType": <RangeAnnotationAxisType>,
  "color": <Color>,
  "dashPattern": <List<int>>,
  "endLabel": <String>,
  "labelAnchor": <AnnotationLabelAnchor>,
  "labelDirection": <AnnotationLabelDirection>,
  "labelPosition": <AnnotationLabelPosition>,
  "labelStyleSpec": <TextStyleSpec>,
  "middleLabel": <String>,
  "startLabel": <String>,
  "strokeWidthPx": <double>,
  "type": "line_annotation",
  "value": <T>
}

... or ...

{
  "axisId": <String>,
  "axisType": <RangeAnnotationAxisType>,
  "color": <Color>,
  "endLabel": <String>,
  "endValue": <T>,
  "labelAnchor": <AnnotationLabelAnchor>,
  "labelDirection": <AnnotationLabelDirection>,
  "labelPosition": <AnnotationLabelPosition>,
  "labelStyleSpec": <TextStyleSpec>,
  "middleLabel": <String>,
  "startLabel": <String>,
  "startValue": <T>,
  "strokeWidthPx": <double>,
  "type": "range_annotation"
}

See also:

Implementation

static common.AnnotationSegment<Object>? decodeAnnotationSegment<T>(
  dynamic map, {
  bool validate = true,
}) {
  common.AnnotationSegment<Object>? result;

  if (map is common.AnnotationSegment<Object>) {
    result = map;
  } else if (map != null) {
    assert(SchemaValidator.validate(
      schemaId: '$_baseSchemaUrl/annotation_segment',
      value: map,
      validate: validate,
    ));

    final type = map['type'];
    final axisType = decodeRangeAnnotationAxisType(
      map['axisType'],
      validate: false,
    )!;

    switch (type) {
      case 'line_annotation':
        result = common.LineAnnotationSegment<Object>(
          axisType == common.RangeAnnotationAxisType.domain
              ? JsonClass.parseValue<T>(map['value'])
              : (JsonClass.parseDouble(map['value']) ?? 0.0),
          axisType,
          axisId: map['axisId']?.toString(),
          color: decodeColor(
            map['color'],
            validate: false,
          ),
          dashPattern: map['dashPattern'] == null
              ? null
              : List<int>.from(
                  map['dashPattern'].map((e) => JsonClass.parseInt(e)!),
                ),
          endLabel: map['endLabel']?.toString(),
          labelAnchor: decodeAnnotationLabelAnchor(
            map['labelAnchor'],
            validate: false,
          ),
          labelDirection: decodeAnnotationLabelDirection(
            map['labelDirection'],
            validate: false,
          ),
          labelPosition: decodeAnnotationLabelPosition(
            map['labelPosition'],
            validate: false,
          ),
          labelStyleSpec: decodeTextStyleSpec(
            map['labelStyleSpec'],
            validate: false,
          ),
          middleLabel: map['middleLabel']?.toString(),
          startLabel: map['startLabel']?.toString(),
          strokeWidthPx: JsonClass.parseDouble(map['strokeWidthPx']) ?? 2.0,
        );
        break;

      case 'range_annotation':
        result = common.RangeAnnotationSegment<Object>(
          axisType == common.RangeAnnotationAxisType.domain
              ? JsonClass.parseValue<T>(map['startValue'])
              : (JsonClass.parseDouble(map['startValue']) ?? 0.0),
          axisType == common.RangeAnnotationAxisType.domain
              ? JsonClass.parseValue<T>(map['endValue'])
              : (JsonClass.parseDouble(map['endValue']) ?? 0.0),
          axisType,
          axisId: map['axisId']?.toString(),
          color: decodeColor(
            map['color'],
            validate: false,
          ),
          endLabel: map['endLabel']?.toString(),
          labelAnchor: decodeAnnotationLabelAnchor(
            map['labelAnchor'],
            validate: false,
          ),
          labelDirection: decodeAnnotationLabelDirection(
            map['labelDirection'],
            validate: false,
          ),
          labelPosition: decodeAnnotationLabelPosition(
            map['labelPosition'],
            validate: false,
          ),
          labelStyleSpec: decodeTextStyleSpec(
            map['labelStyleSpec'],
            validate: false,
          ),
          middleLabel: map['middleLabel']?.toString(),
          startLabel: map['startLabel']?.toString(),
        );
        break;

      default:
        throw Exception('[AnnotationSegment.type]: unknown type: [$type]');
    }
  }

  return result;
}