onCreateRenderer property

ChartSeriesRendererFactory<T, D>? onCreateRenderer
final

Used to create the renderer for custom series.

This is applicable only when the custom series is defined in the sample and for built-in series types, it is not applicable.

Renderer created in this will hold the series state and this should be created for each series. onCreateRenderer callback function should return the renderer class and should not return null.

Series state will be created only once per series and will not be created again when we update the series.

Defaults to null.

Widget build(BuildContext context) {
 return SfCartesianChart(
   series: <LineSeries<SalesData, num>>[
     LineSeries<SalesData, num>(
         dataSource: chartData,
         xValueMapper: (datum, index) => datum.x,
         yValueMapper: (datum, index) => datum.y,
         onCreateRenderer: (ChartSeries<SalesData, num> series) {
           return CustomLinerSeriesRenderer(
               series as LineSeries<SalesData, num>);
         }),
   ],
 );
}

class CustomLinerSeriesRenderer extends LineSeriesRenderer<SalesData, num> {
  CustomLinerSeriesRenderer(this.series);
  final LineSeries<SalesData, num> series;

  @override
  LineSegment<SalesData, num> createSegment() {
    return _LineCustomPainter(series);
  }
}

class _LineCustomPainter extends LineSegment<SalesData, num> {
  _LineCustomPainter(this._series);
  final LineSeries<SalesData, num> _series;

  @override
  int get currentSegmentIndex => super.currentSegmentIndex;

  @override
  Paint getFillPaint() {
    final Paint customerFillPaint = Paint();
    customerFillPaint.color = _series.dataSource![currentSegmentIndex].y > 30
      ? Colors.red
      : Colors.green;
    customerFillPaint.style = PaintingStyle.fill;
    return customerFillPaint;
  }

  @override
  void onPaint(Canvas canvas) {
     super.onPaint(canvas);
   }
 }

Implementation

final ChartSeriesRendererFactory<T, D>? onCreateRenderer;