Create/save image without UI

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;

}



1 Reply

LP Lokesh Palani Syncfusion Team May 12, 2023 01:56 PM UTC

Hi Pieter,


We have analyzed your requirement at our end and currently we do not have direct support to achieve your requirement regarding to export chart to image without rendering it to the screen. However, we have tried to achieve your requirement by referring to the links below. 


Reference links:

·        https://github.com/flutter/flutter/issues/40064

·        https://stackoverflow.com/questions/55134343/renderrepaintboundary-to-image-without-adding-widget-to-screen

We have created a sample and attached here for your reference, please modify the sample based on your requirement. 


Regards,

Lokesh.


Attachment: chart_182218_1ed263f3.zip

Loader.
Up arrow icon