buildChart function

Widget buildChart({
  1. required String id,
  2. required Map<String, dynamic> props,
})

Implementation

Widget buildChart({
  required String id,
  required Map<String, dynamic> props,
}) {
  final xKey = props['xKey']?.toString() ?? 'x';
  final yKey = props['yKey']?.toString() ?? 'y';
  final rows = mapsOf(props['rows']);
  final labels = rows.map((row) => row[xKey]?.toString() ?? '').toList();
  final values = rows.map((row) {
    final raw = row[yKey];
    return raw is num ? raw.toDouble() : 0.0;
  }).toList();
  final title = props['title']?.toString();
  final height = (props['height'] is num) ? (props['height'] as num).toDouble() : 120.0;

  return Column(
    key: ValueKey(id),
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      if (title != null && title.isNotEmpty) Text(title),
      SizedBox(
        height: height,
        child: CustomPaint(
          painter: _BarPainter(values),
          child: const SizedBox.expand(),
        ),
      ),
      Row(
        children: [
          for (final label in labels)
            Expanded(child: Text(label, textAlign: TextAlign.center)),
        ],
      ),
    ],
  );
}