trySwitchChartTypeForSeriesShapeAuto function

ChartAutoSwitchResult trySwitchChartTypeForSeriesShapeAuto(
  1. Map<String, dynamic> source, {
  2. List<ChartType>? preferredOrder,
  3. bool includeCurrentType = false,
  4. bool registeredOnly = true,
})

Attempts to automatically switch source to the highest-ranked compatible chart type without throwing.

Implementation

ChartAutoSwitchResult trySwitchChartTypeForSeriesShapeAuto(
  Map<String, dynamic> source, {
  List<ChartType>? preferredOrder,
  bool includeCurrentType = false,
  bool registeredOnly = true,
}) {
  final sourceShape = inferSeriesDataShape(source);
  final options = chartSwitchOptionsForJson(
    source,
    preferredOrder: preferredOrder,
    includeCurrentType: includeCurrentType,
    registeredOnly: registeredOnly,
  );
  if (options.isEmpty) {
    return ChartAutoSwitchResult(
      success: false,
      sourceShape: sourceShape,
      selectedType: null,
      selectedOption: null,
      options: const [],
      payload: null,
      keptCurrentType: false,
      preservedRuntimeKeys: const [],
      changedTopLevelKeys: const [],
      message:
          'No compatible chart type found for data shape '
          '${sourceShape.name}.',
    );
  }

  final selected = options.first;
  final payload = selected.previewPayload;
  return ChartAutoSwitchResult(
    success: true,
    sourceShape: sourceShape,
    selectedType: selected.type,
    selectedOption: selected,
    options: options,
    payload: payload,
    keptCurrentType: selected.isCurrentType,
    preservedRuntimeKeys: _preservedRuntimeKeys(source, payload),
    changedTopLevelKeys: _changedTopLevelKeys(source, payload),
    message: selected.isCurrentType
        ? 'Kept current chart type ${selected.typeString}.'
        : 'Auto-switched to ${selected.typeString}.',
  );
}