fromLineChartData static method

List<LegendEntry> fromLineChartData(
  1. LineChartData data
)

Returns the legend entries to display for data, following these rules in order:

  1. If data.legend is non-empty, returns it verbatim (wrapped in an unmodifiable view) so user-specified entries always win.
  2. Otherwise, when the chart has more than one series (series.length + functionSeries.length > 1), auto-derives one entry per series from each series' name and color.
  3. Otherwise (zero or one series and no explicit legend), returns an empty list — single-series charts don't need a legend.

Implementation

static List<LegendEntry> fromLineChartData(LineChartData data) {
  if (data.legend.isNotEmpty) return List.unmodifiable(data.legend);

  final totalSeries = data.series.length + data.functionSeries.length;
  if (totalSeries > 1) {
    return List.unmodifiable([
      for (final s in data.series) LegendEntry(label: s.name, color: s.color),
      for (final f in data.functionSeries)
        LegendEntry(label: f.name, color: f.color),
    ]);
  }
  return const [];
}