trySwitchChartTypeForSeriesShape function
Attempts to switch source to targetType without throwing.
Direct same-shape switches succeed without force. Cross-shape conversions
such as cartesian -> treemap/pie/candlestick require force: true.
Implementation
ChartTypeSwitchResult trySwitchChartTypeForSeriesShape(
Map<String, dynamic> source, {
required ChartType targetType,
bool force = false,
bool registeredOnly = true,
}) {
final compatibility = chartSwitchCompatibilityForJson(
source,
targetType: targetType,
registeredOnly: registeredOnly,
);
final canSwitch =
compatibility.isCompatible ||
(force && compatibility.forceConversionAvailable);
if (!canSwitch) {
return ChartTypeSwitchResult(
success: false,
targetType: targetType,
compatibility: compatibility,
payload: null,
usedForceConversion: false,
preservedRuntimeKeys: const [],
changedTopLevelKeys: const [],
message: compatibility.reason,
);
}
final payload = switchChartTypeForSeriesShape(
source,
targetType: targetType,
force: force,
);
return ChartTypeSwitchResult(
success: true,
targetType: targetType,
compatibility: compatibility,
payload: payload,
usedForceConversion: !compatibility.isCompatible,
preservedRuntimeKeys: _preservedRuntimeKeys(source, payload),
changedTopLevelKeys: _changedTopLevelKeys(source, payload),
message: compatibility.isCompatible
? 'Switched to ${chartTypeToString(targetType)} without data conversion.'
: 'Switched to ${chartTypeToString(targetType)} using force conversion.',
);
}