paint method
Called whenever the object needs to paint. The given Canvas has its
coordinate space configured such that the origin is at the top left of the
box. The area of the box is the size of the size argument.
Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.
Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.
To paint text on a Canvas, use a TextPainter.
To paint an image on a Canvas:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.
-
In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.
Implementation
@override
void paint(Canvas canvas, Size size) {
// Use theme padding
final leftPadding = theme.padding.left;
final rightPadding = theme.padding.right;
final topPadding = theme.padding.top;
final bottomPadding = theme.padding.bottom;
final chartSize = Size(size.width - leftPadding - rightPadding, size.height - topPadding - bottomPadding);
final chartOffset = Offset(leftPadding, topPadding);
// Calculate bounds (optimized single pass)
if (dataSets.isEmpty) return;
double minX = .infinity;
double maxX = double.negativeInfinity;
double maxY = double.negativeInfinity;
// Single pass through all points for better performance
for (final dataSet in dataSets) {
final point = dataSet.dataPoint;
if (point.x < minX) minX = point.x;
if (point.x > maxX) maxX = point.x;
if (point.y > maxY) maxY = point.y;
}
if (minX == .infinity) return; // No valid data
final minXAdjusted = minX * 0.95;
final maxXAdjusted = maxX * 1.05;
final minY = 0.0;
final maxYAdjusted = maxY * 1.2;
canvas.save();
canvas.translate(chartOffset.dx, chartOffset.dy);
// Draw grid
drawGrid(canvas, chartSize, minXAdjusted, maxXAdjusted, minY, maxYAdjusted);
// Draw axes
drawAxes(canvas, chartSize, minXAdjusted, maxXAdjusted, minY, maxYAdjusted);
// Use pre-calculated groups if available, otherwise calculate them
final Map<double, List<ChartDataSet>> effectiveGroupedByX;
final List<double> effectiveSortedXValues;
if (isGrouped) {
if (groupedData != null && sortedXValues != null) {
effectiveGroupedByX = groupedData!;
effectiveSortedXValues = sortedXValues!;
} else {
effectiveGroupedByX = {};
for (final dataSet in dataSets) {
final x = dataSet.dataPoint.x;
effectiveGroupedByX.putIfAbsent(x, () => []).add(dataSet);
}
effectiveSortedXValues = effectiveGroupedByX.keys.toList()..sort();
}
final maxGroups = effectiveSortedXValues.length;
if (effectiveGroupedByX.length > 1) {
// Grouped bars - group by x coordinate
final groupSpacing = chartSize.width / (maxGroups + 1);
final barSpacing = barWidth * 0.2;
for (int groupIndex = 0; groupIndex < effectiveSortedXValues.length; groupIndex++) {
final xValue = effectiveSortedXValues[groupIndex];
final groupDataSets = effectiveGroupedByX[xValue]!;
double currentX = groupSpacing * (groupIndex + 1);
for (final dataSet in groupDataSets) {
final point = dataSet.dataPoint;
// Validate point data to prevent NaN
if (!point.y.isFinite || point.y < 0 || maxYAdjusted <= 0) {
continue;
}
final barHeight = (point.y / maxYAdjusted) * chartSize.height;
final barY = chartSize.height - barHeight;
// Stagger animation for grouped bars
final barIndex = groupIndex / maxGroups;
final barProgress = math.max(0.0, math.min(1.0, (animationProgress - barIndex * 0.3) / 0.7));
// Check if this bar is selected or hovered
final datasetIndex = dataSets.indexOf(dataSet);
final isSelected =
selectedBar != null &&
selectedBar!.isHit &&
selectedBar!.datasetIndex == datasetIndex &&
selectedBar!.elementIndex == 0;
final isHovered =
hoveredBar != null &&
hoveredBar!.isHit &&
hoveredBar!.datasetIndex == datasetIndex &&
hoveredBar!.elementIndex == 0;
_drawRoundedBar(
canvas,
Offset(currentX - barWidth / 2, barY),
barWidth,
barHeight,
dataSet.color,
barProgress,
isSelected: isSelected,
isHovered: isHovered,
);
currentX += barWidth + barSpacing;
}
}
} else {
// Fallback to single bars if only one group
_drawSingleBars(canvas, chartSize, minXAdjusted, maxXAdjusted, maxYAdjusted);
}
} else {
// Single bars with animation
_drawSingleBars(canvas, chartSize, minXAdjusted, maxXAdjusted, maxYAdjusted);
}
canvas.restore();
// Draw axis labels
canvas.save();
canvas.translate(chartOffset.dx, chartOffset.dy);
drawAxisLabels(canvas, chartSize, minXAdjusted, maxXAdjusted, minY, maxYAdjusted, dataSets: dataSets);
canvas.restore();
}