Hi...how can I create an image without going through the UI? I'm trying to save an image of my SfCartesianChart to a PDF file (I'm using Syncfusion's PdfDocument library). I'm not displaying the SfCartesianChart on the screen that has my "Generate PDF" button. I'd like to call a function similar to the following. But, apparently I need a key to make this happen and it also needs to be part of the Widget tree. Is there anyway around this?
Future<Uint8List> createImageFromChart() async {
SfCartesianChart chart = SfCartesianChart(
primaryXAxis: NumericAxis(
isInversed: true,
decimalPlaces: 0,
enableAutoIntervalOnZooming: false,
maximumLabels: 10,
anchorRangeToVisiblePoints: true,
),
primaryYAxis: NumericAxis(
anchorRangeToVisiblePoints: false,
),
legend: Legend(
isVisible: true,
toggleSeriesVisibility: false,
position: LegendPosition.bottom,
alignment: ChartAlignment.near,
),
series: series,
);
final ui.Image data = await chart.toImage(pixelRatio: 3.0)!;
final ByteData? bytes = await data.toByteData(format: ui.ImageByteFormat.png);
final Uint8List imageBytes = bytes!.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
return imageBytes;
}