draw method

  1. @override
void draw(
  1. Canvas canvas,
  2. Size size,
  3. Paint paint
)
override

Draw ChartItem on the canvas. Canvas with item size is passed, item's padding and margin need to be calculated This is to allow more flexibility for more custom items if needed

Use paint for drawing item to canvas, this allows us to change colors of the item from _ChartPainter

Implementation

@override
void draw(Canvas canvas, Size size, Paint paint) {
  final _maxValue = data.maxValue - data.minValue;
  final _verticalMultiplier = size.height / max(1, _maxValue);
  final _minValue = (data.minValue * _verticalMultiplier);

  final _radius = drawDataItem.radius ?? BorderRadius.zero;

  final _itemMaxValue = item.max ?? 0.0;

  // If item is empty, or it's max value is below chart's minValue then don't draw it.
  // minValue can be below 0, this will just ensure that animation is drawn correctly.
  if (item.isEmpty || _itemMaxValue < data.minValue) {
    return;
  }

  canvas.drawRRect(
    RRect.fromRectAndCorners(
      Rect.fromPoints(
        Offset(
          0.0,
          _maxValue * _verticalMultiplier -
              max(data.minValue, item.min ?? 0.0) * _verticalMultiplier +
              _minValue,
        ),
        Offset(
          size.width,
          _maxValue * _verticalMultiplier -
              _itemMaxValue * _verticalMultiplier +
              _minValue,
        ),
      ),
      bottomLeft:
          _itemMaxValue.isNegative ? _radius.topLeft : _radius.bottomLeft,
      bottomRight:
          _itemMaxValue.isNegative ? _radius.topRight : _radius.bottomRight,
      topLeft:
          _itemMaxValue.isNegative ? _radius.bottomLeft : _radius.topLeft,
      topRight:
          _itemMaxValue.isNegative ? _radius.bottomRight : _radius.topRight,
    ),
    paint,
  );

  final _border = drawDataItem.border;

  if (_border.style == BorderStyle.solid) {
    final _borderPaint = Paint();
    _borderPaint.style = PaintingStyle.stroke;
    _borderPaint.color = _border.color;
    _borderPaint.strokeWidth = _border.width;

    canvas.drawRRect(
      RRect.fromRectAndCorners(
        Rect.fromPoints(
          Offset(
            0.0,
            _maxValue * _verticalMultiplier -
                max(data.minValue, item.min ?? 0.0) * _verticalMultiplier +
                _minValue,
          ),
          Offset(
            size.width,
            _maxValue * _verticalMultiplier -
                _itemMaxValue * _verticalMultiplier +
                _minValue,
          ),
        ),
        bottomLeft:
            _itemMaxValue.isNegative ? _radius.topLeft : _radius.bottomLeft,
        bottomRight:
            _itemMaxValue.isNegative ? _radius.topRight : _radius.bottomRight,
        topLeft:
            _itemMaxValue.isNegative ? _radius.bottomLeft : _radius.topLeft,
        topRight:
            _itemMaxValue.isNegative ? _radius.bottomRight : _radius.topRight,
      ),
      _borderPaint,
    );
  }
}