sampleEntriesForPdf function

List<MapEntry<DateTime, double>> sampleEntriesForPdf(
  1. List<MapEntry<DateTime, double>> entries,
  2. int targetCount
)

Sample entries for PDF to reduce clutter.

Implementation

List<MapEntry<DateTime, double>> sampleEntriesForPdf(
  List<MapEntry<DateTime, double>> entries,
  int targetCount,
) {
  if (entries.length <= targetCount) return entries;

  final step = entries.length / targetCount;
  final sampled = <MapEntry<DateTime, double>>[];

  for (var i = 0; i < targetCount; i++) {
    final index = (i * step).floor().clamp(0, entries.length - 1);
    sampled.add(entries[index]);
  }

  // Always include the last entry.

  if (sampled.last.key != entries.last.key) {
    sampled.add(entries.last);
  }

  return sampled;
}