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) {
// Professional padding
final leftPadding = 50.0;
final rightPadding = 20.0;
final topPadding = 20.0;
final bottomPadding = 40.0;
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 = double.infinity;
double maxX = double.negativeInfinity;
double maxY = double.negativeInfinity;
// Single pass through all points for better performance
for (final dataSet in dataSets) {
for (final point in dataSet.dataPoints) {
if (point.x < minX) minX = point.x;
if (point.x > maxX) maxX = point.x;
if (point.y > maxY) maxY = point.y;
}
}
if (minX == double.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);
if (isGrouped && dataSets.length > 1) {
// Grouped bars
final maxLength =
dataSets.map((ds) => ds.dataPoints.length).reduce(math.max);
final groupSpacing = chartSize.width / (maxLength + 1);
final barSpacing = barWidth * 0.2;
for (int i = 0; i < maxLength; i++) {
double currentX = groupSpacing * (i + 1);
for (final dataSet in dataSets) {
if (i < dataSet.dataPoints.length) {
final point = dataSet.dataPoints[i];
// 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 = i / maxLength;
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 isSelected = selectedBar != null &&
selectedBar!.isHit &&
selectedBar!.datasetIndex == dataSets.indexOf(dataSet) &&
selectedBar!.elementIndex == i;
final isHovered = hoveredBar != null &&
hoveredBar!.isHit &&
hoveredBar!.datasetIndex == dataSets.indexOf(dataSet) &&
hoveredBar!.elementIndex == i;
_drawRoundedBar(
canvas,
Offset(currentX - barWidth / 2, barY),
barWidth,
barHeight,
dataSet.color,
barProgress,
isSelected: isSelected,
isHovered: isHovered,
);
currentX += barWidth + barSpacing;
}
}
}
} else {
// Single or stacked bars with animation
final dataSet = dataSets.first;
final totalBars = dataSet.dataPoints.length;
for (int i = 0; i < dataSet.dataPoints.length; i++) {
final point = dataSet.dataPoints[i];
// Validate point data to prevent NaN
if (!point.x.isFinite ||
!point.y.isFinite ||
point.y < 0 ||
maxYAdjusted <= 0) {
continue;
}
final xRange = maxXAdjusted - minXAdjusted;
final x = xRange > 0
? ((point.x - minXAdjusted) / xRange) * chartSize.width
: chartSize.width / 2;
final barHeight = (point.y / maxYAdjusted) * chartSize.height;
final barY = chartSize.height - barHeight;
// Stagger animation for each bar
final barIndex = i / totalBars;
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 isSelected = selectedBar != null &&
selectedBar!.isHit &&
selectedBar!.datasetIndex == 0 &&
selectedBar!.elementIndex == i;
final isHovered = hoveredBar != null &&
hoveredBar!.isHit &&
hoveredBar!.datasetIndex == 0 &&
hoveredBar!.elementIndex == i;
_drawRoundedBar(
canvas,
Offset(x - barWidth / 2, barY),
barWidth,
barHeight,
dataSet.color,
barProgress,
isSelected: isSelected,
isHovered: isHovered,
);
}
}
canvas.restore();
// Draw axis labels
canvas.save();
canvas.translate(chartOffset.dx, chartOffset.dy);
drawAxisLabels(
canvas,
chartSize,
minXAdjusted,
maxXAdjusted,
minY,
maxYAdjusted,
);
canvas.restore();
}