Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added example/assets/icons/ic_meal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions example/lib/presentation/samples/chart_samples.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fl_chart_app/presentation/samples/candlestick/candlestick_chart_sample1.dart';
import 'package:fl_chart_app/presentation/samples/line/line_chart_sample14.dart';
import 'package:fl_chart_app/util/app_helper.dart';

import 'bar/bar_chart_sample1.dart';
Expand Down Expand Up @@ -46,6 +47,7 @@ class ChartSamples {
LineChartSample(11, (context) => const LineChartSample11()),
LineChartSample(12, (context) => const LineChartSample12()),
LineChartSample(13, (context) => const LineChartSample13()),
LineChartSample(14, (context) => const LineChartSample14()),
],
ChartType.bar: [
BarChartSample(1, (context) => BarChartSample1()),
Expand Down
122 changes: 122 additions & 0 deletions example/lib/presentation/samples/line/line_chart_sample14.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

/// LineChartSample14 demonstrates how to use FlDotImagePainter
/// to display custom images as dot markers on a line chart.
///
/// This sample shows:
/// - Loading images asynchronously before creating the chart
/// - Using FlDotImagePainter with custom image assets
/// - Handling loading states while images are being loaded
class LineChartSample14 extends StatefulWidget {
const LineChartSample14({super.key});

@override
State<LineChartSample14> createState() => _LineChartSample14State();
}

class _LineChartSample14State extends State<LineChartSample14> {
/// The custom dot painter that will render images at each data point.
/// Null until the image is loaded asynchronously.
FlDotImagePainter? _dotPainter;

@override
void initState() {
super.initState();
_loadImage();
}

/// Loads the image asset and creates a FlDotImagePainter.
///
/// This must be done asynchronously before the chart can be rendered
/// because the draw() method is synchronous and cannot load images on-demand.
Future<void> _loadImage() async {
final image = await FlDotImagePainter.loadImageFromAsset(
'assets/icons/image_annotation.png',
);
setState(() {
_dotPainter = FlDotImagePainter(image: image, size: 20.0);
});
}

@override
Widget build(BuildContext context) {
// Show a loading indicator while the image is being loaded
if (_dotPainter == null) {
return const Center(child: CircularProgressIndicator());
}

return AspectRatio(
aspectRatio: 1.5,
child: Padding(
padding: const EdgeInsets.all(16),
child: LineChart(
LineChartData(
// Define the chart boundaries
minX: 0,
maxX: 6,
minY: 0,
maxY: 6,
// Configure axis titles
Comment thread
InsoooooooooJANG marked this conversation as resolved.
Outdated
titlesData: FlTitlesData(
show: true,
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 30,
interval: 1,
),
),
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 40,
interval: 1,
),
),
topTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
rightTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
),
gridData: const FlGridData(show: true),
borderData: FlBorderData(
show: true,
border: Border.all(color: Colors.grey),
),
lineBarsData: [
LineChartBarData(
spots: const [
FlSpot(0, 3),
FlSpot(1, 1),
FlSpot(2, 4),
FlSpot(3, 2),
FlSpot(4, 5),
FlSpot(5, 3),
FlSpot(6, 4),
],
isCurved: true,
color: Colors.blue,
barWidth: 3,
// Use FlDotImagePainter to display custom images at each data point
dotData: FlDotData(
show: true,
getDotPainter: (spot, percent, barData, index) {
// Return the pre-loaded image painter for each dot
return _dotPainter!;
},
),
belowBarData: BarAreaData(
show: true,
color: Colors.blue.withValues(alpha: 0.3),
),
),
],
),
),
),
);
}
}
68 changes: 68 additions & 0 deletions lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:fl_chart/src/utils/canvas_wrapper.dart';
import 'package:fl_chart/src/utils/lerp.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart' hide Image;
import 'package:flutter/services.dart';

/// This is the base class for axis base charts data
/// that contains a [FlGridData] that holds data for showing grid lines,
Expand Down Expand Up @@ -1466,6 +1467,73 @@ abstract class FlDotPainter with EquatableMixin {
}
}

/// This class is an implementation of a [FlDotPainter] that draws
/// an image as the dot marker
class FlDotImagePainter extends FlDotPainter {
/// Creates an image dot painter.
///
/// [image] must be loaded before creating this painter.
/// Use [loadImageFromAsset] to load images from assets.
FlDotImagePainter({
required this.image,
this.size = 24.0,
});

/// The image to draw as the dot marker
final Image image;

/// The size of the dot (width and height)
final double size;

/// Loads an image from asset path and returns a [Image] object.
///
/// Example:
/// ```dart
/// final image = await FlDotImagePainter.loadImageFromAsset('assets/dot.png');
/// final painter = FlDotImagePainter(image: image, size: 20.0);
/// ```
static Future<Image> loadImageFromAsset(String assetPath) async {
Comment thread
InsoooooooooJANG marked this conversation as resolved.
Outdated
final byteData = await rootBundle.load(assetPath);
final codec = await instantiateImageCodec(byteData.buffer.asUint8List());
final frame = await codec.getNextFrame();
return frame.image;
}

@override
void draw(Canvas canvas, FlSpot spot, Offset offsetInCanvas) {
// Center the image at the offset
final drawOffset = offsetInCanvas - Offset(size / 2, size / 2);
final rect = Rect.fromLTWH(drawOffset.dx, drawOffset.dy, size, size);
paintImage(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use canvasWrapper.drawImage() instead of paintImage. This way, we keep the consistency and testability. Just like here:

if (line.image != null) {
final centerX = line.image!.width / 2;
final centerY = line.image!.height / 2;
final centeredImageOffset = Offset(centerX, to.dy - centerY);
canvasWrapper.drawImage(
line.image!,
centeredImageOffset,
_imagePaint,
);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paintImage is gone. I couldn't route it through CanvasWrapper though, and I'd rather check the direction with you than guess:

  • FlDotPainter.draw only receives a raw CanvasCanvasWrapper.drawDot unwraps it at canvas_wrapper.dart:123, and that's the only call site.
  • CanvasWrapper isn't exported from lib/fl_chart.dart, so changing draw to take one would make FlDotPainter impossible for users to implement without importing package:fl_chart/src/.... That's a public breaking change.
  • Canvas.drawImage (the one in the code you linked) has no scaling — it draws at native size. Using it directly would silently ignore FlDotImagePainter.size.
  • The raw-Canvas shape also seems to be the convention for user-extensible painters: GaugeTickPainter, GaugeMarkerPainter and GaugePointerPainter all take a Canvas and call canvas.drawLine/save/translate directly, as do the three built-in dot painters and FlSimpleErrorPainter.

So I dropped paintImage for canvas.drawImageRect with applyBoxFit(BoxFit.contain, ...), which keeps both size and the aspect ratio working. For the testability half of your point: draw is now unit-tested against MockCanvas and asserts the exact source/destination rects, the same way FlSimpleErrorPainter.draw is tested in axis_chart_data_test.dart.

If you'd rather have CanvasWrapper in the painter interfaces, I'm glad to do it — I just think it deserves its own breaking PR that exports canvas_wrapper.dart and converts all five painter interfaces at once, rather than changing only this one. Let me know which you prefer.

canvas: canvas,
rect: rect,
image: image,
fit: BoxFit.contain,
filterQuality: FilterQuality.high,
);
}

@override
Color get mainColor => Colors.transparent;

@override
Size getSize(FlSpot spot) => Size(size, size);

@override
FlDotPainter lerp(FlDotPainter a, FlDotPainter b, double t) {
if (a is! FlDotImagePainter || b is! FlDotImagePainter) {
return b;
}
return FlDotImagePainter(
image: b.image,
size: lerpDouble(a.size, b.size, t) ?? b.size,
);
}

@override
List<Object?> get props => [image, size];
}

/// This class is an implementation of a [FlDotPainter] that draws
/// a circled shape
class FlDotCirclePainter extends FlDotPainter {
Expand Down