Skip to content

In case of PieChart with syncfusion_flutter_charts, the chart screen cannot be output to an image file. #1349

@TakaTeke

Description

@TakaTeke

Hello.

I'm trying to draw a chart with syncfusion_flutter_charts and output it to an image file.
I'm having the following problem and it's bothering me.
In the case of ColumnSeries and BarSeries of SfCartesianChart, the chart screen can be output to an image file,
However, PieSeries of SfCircularChart cannot be output to an image file.
The code for outputting to an image file looks like this:

405 Future _renderChartAsImage(String sFileName) async {
406 print('_cartesianChartKey.currentState: ${_cartesianChartKey.currentState}');
407 final ui. Image data =
408 await _cartesianChartKey.currentState!.toImage(pixelRatio: 3.0);
409 final ByteData? bytes =
410 await data.toByteData(format: ui.ImageByteFormat.png);
411 final Uint8List imageBytes =
412 bytes!.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
413
414 File file = File(sFileName);
415 print('file: $file');
416 //Write image bytes data
417 await file.writeAsBytes(imageBytes, flush: true);
418 //Open the image file in mobile
419 OpenFile.open(sFileName);
420 }

The execution result logs are as follows.

●For SfCircularChart and PieSeries
flutter: _cartesianChartKey.currentState: null
[ERROR: flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
#0 GraphDisplayPageState._renderChartAsImage (package:my_material_survey/pages/graph_display_page.dart:408:42)
#1 GraphDisplayPageState._fileSavePicker (package:my_material_survey/pages/graph_display_page.dart:389:13)

#2 GraphDisplayPageState.build. (package:my_material_survey/pages/graph_display_page.dart:186:25)

●SfCartesianChart, ColumnSeries
flutter: _cartesianChartKey.currentState: SfCartesianChartState#b170c(tickers: tracking 1 ticker)
flutter: file: File: '/Volumes/Macintosh HD/Users/taka/testFile2/export/output.png'

●SfCartesianChart, BarSeries
flutter: _cartesianChartKey.currentState: SfCartesianChartState#b170c(tickers: tracking 3 tickers)
flutter: file: File: '/Volumes/Macintosh HD/Users/taka/testFile2/export/output.png'

I will explain the situation.
For PieSeries of SfCircularChart, _cartesianChartKey.currentState is null. This seems to be the cause.
For SfCartesianChart ColumnSeries and BarSeries, _cartesianChartKey.currentState is not null.

The version of the syncfusion_flutter_charts package is ^22.2.9.
Please let me know if you have a better solution.
Thank you.

The code for PieSeries in SfCircularChart looks like this:

final List _pieChartData = [];

SfCircularChart(
backgroundColor: const Color(0xFFFFFFE6),
key: _cartesianChartKey,
legend: const Legend(
isVisible: true,
position: LegendPosition.right,
alignment: ChartAlignment.center,
iconWidth: 24.0,
iconHeight: 24.0,
textStyle:
TextStyle(color: Colors.black, fontSize: 30.0)),
title: ChartTitle(
alignment: ChartAlignment.near,
text: '${widget.questText}',
textStyle: const TextStyle(
fontSize: 28.0,
color: Colors.black,
)),
series: [
// Initialize circular series
(PieSeries<PieChartData, String>(
dataSource: _pieChartData,
pointColorMapper: (PieChartData data, _) =>
data. color,
xValueMapper: (PieChartData data, _) => data.x,
yValueMapper: (PieChartData data, _) => data.y,
radius: '100%',
dataLabelSettings: const DataLabelSettings(
color: Colors.white,
opacity: 1.0,
textStyle: TextStyle(
fontSize: 30.0, color: Colors.black),
isVisible: true),
)),
]
)

class PieChartData {
PieChartData(this.x, this.y, [this.color]);

final String x;
final double y;
final Color? color;
}

The code for ColumnSeries in SfCartesianChart looks like this:

final List _columnChartData = [];

SfCartesianChart(
backgroundColor: const Color(0xFFFFFFE6),
key: _cartesianChartKey,
// title
title: ChartTitle(
alignment: ChartAlignment.near,
text: ' ${widget.questText}',
textStyle: const TextStyle(
fontSize: 28.0,
color: Colors.black,
)),
primaryXAxis: CategoryAxis(
labelStyle: const TextStyle(
fontSize: 24.0, color: Colors.black)),
primaryYAxis: NumericAxis(
labelStyle: const TextStyle(fontSize: 20.0),
),
//tooltipBehavior: _tooltip,
series: <ChartSeries<ColumnChartData, String>>[
// Renders column chart
ColumnSeries<ColumnChartData, String>(
dataSource: _columnChartData,
xValueMapper: (ColumnChartData data, _) => data.x,
yValueMapper: (ColumnChartData data, _) => data.y,
dataLabelSettings: const DataLabelSettings(
color: Colors.white,
opacity: 1.0,
textStyle: TextStyle(
fontSize: 30.0, color: Colors.black),
isVisible: true),
),
]
)

class ColumnChartData {
ColumnChartData(this.x, this.y);

final String x;
final double y;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions