switchChartTypeForSeriesShape function

Map<String, dynamic> switchChartTypeForSeriesShape(
  1. Map<String, dynamic> source, {
  2. required ChartType targetType,
  3. bool force = false,
})

Switch chart type while preserving/normalizing data where possible.

Throws StateError when target type is not compatible (unless force).

Implementation

Map<String, dynamic> switchChartTypeForSeriesShape(
  Map<String, dynamic> source, {
  required ChartType targetType,
  bool force = false,
}) {
  final compatibility = chartSwitchCompatibilityForJson(
    source,
    targetType: targetType,
    registeredOnly: false,
  );
  final canSwitch =
      compatibility.isCompatible ||
      (force && compatibility.forceConversionAvailable);
  if (!canSwitch) {
    throw StateError(compatibility.reason);
  }

  final normalized = _normalizeDataForTarget(source, targetType);
  normalized['type'] = chartTypeToString(targetType);
  return normalized;
}