updateSegmentGradient method

  1. @nonVirtual
void updateSegmentGradient(
  1. ChartSegment segment
)

Implementation

@nonVirtual
void updateSegmentGradient(ChartSegment segment) {
  final int segmentIndex = segment.currentSegmentIndex;

  // This method is called by the data source setter before updating the
  // segments, when the data source is updated dynamically. As a result,
  // the length of the data source is updated while the length of the
  // segments is not updated, which can lead to a range error exception.
  // To prevent this, a condition has been added to ensure the segmentIndex
  // is less than the data source length.
  if (!segment.isEmpty &&
      dataSource != null &&
      segmentIndex < dataSource!.length) {
    if (pointShaderMapper != null) {
      final Shader shader = pointShaderMapper!(
          dataSource![segmentIndex],
          segmentIndex,
          palette[segmentIndex % palette.length],
          Rect.fromCircle(center: center, radius: currentRadius));
      segment.fillPaint.shader = shader;
    } else if (onCreateShader != null) {
      final ChartShaderDetails details = createShaderDetails();
      segment.fillPaint.shader = onCreateShader!(details);
    } else if (pointRenderMode == PointRenderMode.gradient) {
      final List<Color> colors = List<Color>.generate(
          segments.length, (int i) => palette[i % palette.length]);
      final List<double> stops = <double>[];
      num initialStops = 0;
      for (int i = 0; i < segments.length; i++) {
        final double segmentRatio = circularYValues[i] / sumOfY;
        if (stops.isEmpty) {
          initialStops = segmentRatio / 4;
          stops.add(segmentRatio - initialStops);
        } else {
          if (stops.length == 1) {
            stops.add((segmentRatio + stops.last) + initialStops / 1.5);
          } else {
            stops.add(segmentRatio + stops.last);
          }
        }
      }
      final SweepGradient sweep = SweepGradient(
          startAngle: degreeToRadian(startAngle),
          endAngle: degreeToRadian(endAngle),
          colors: colors,
          stops: stops,
          transform: GradientRotation(degreeToRadian(-90)));
      segment.fillPaint.shader = sweep.createShader(
          createShaderDetails().outerRect,
          textDirection: TextDirection.ltr);
    }
  }
}