drawDataSet method

  1. @override
  2. @protected
void drawDataSet(
  1. Canvas c,
  2. IBarDataSet dataSet,
  3. int index
)
override

Same as the base class, but skips bars by their vertical position and fills custom fills left to right.

Implementation

@override
@protected
void drawDataSet(ui.Canvas c, IBarDataSet dataSet, int index) {
  if (dataSet.entryCount < 1) return;

  final trans = chart.getTransformer(dataSet.axisDependency);

  barBorderPaint.color = dataSet.barBorderColor;
  barBorderPaint.strokeWidth = Utils.convertDpToPixel(dataSet.barBorderWidth);

  final drawBorder = dataSet.barBorderWidth > 0;

  final phaseX = animator.phaseX;
  final phaseY = animator.phaseY;

  xBounds.set(chart, dataSet);
  reduceVisible(chart, dataSet);

  final buffer = barBuffers[index];
  buffer.setPhases(phaseX, phaseY);
  buffer.dataSetIndex = index;
  buffer.isInverted = chart.isInverted(dataSet.axisDependency);
  final barWidth = chart.barData?.barWidth;
  if (barWidth == null) return;
  buffer.barWidth = barWidth;
  buffer.columnWidth = reducedColumnWidth;

  if (chart.isDrawBarShadowEnabled) {
    shadowPaint.color = dataSet.barShadowColor;

    for (var k = 0; k < reducer.count; k++) {
      final e = dataSet.getEntryForIndex(reducer.indices[k]);

      final x = e.x;
      final barWidthHalf = buffer.widthOfBar(reducer.columnCounts[k]) / 2;

      barShadowRectBuffer.top = x - barWidthHalf;
      barShadowRectBuffer.bottom = x + barWidthHalf;

      trans.rectValueToPixel(barShadowRectBuffer);

      if (!viewPortHandler.isInBoundsTop(barShadowRectBuffer.bottom)) {
        continue;
      }

      if (!viewPortHandler.isInBoundsBottom(barShadowRectBuffer.top)) break;

      barShadowRectBuffer.left = viewPortHandler.contentLeft;
      barShadowRectBuffer.right = viewPortHandler.contentRight;

      drawBarShape(
        c,
        dataSet,
        barShadowRectBuffer.left,
        barShadowRectBuffer.top,
        barShadowRectBuffer.right,
        barShadowRectBuffer.bottom,
        BarCorners.ALL,
        () => c.drawRectP(barShadowRectBuffer, shadowPaint),
      );
    }
  }

  buffer.feedIndices(
    dataSet,
    reducer.indices,
    reducer.columnCounts,
    reducer.count,
  );

  trans.pointValuesToPixel(buffer.buffer, buffer.filledSize);

  final fills = dataSet.fills;
  final isCustomFill = fills != null && fills.isNotEmpty;
  final isSingleColor = dataSet.colors.length == 1;
  final isInverted = chart.isInverted(dataSet.axisDependency);

  if (isSingleColor) {
    renderPaint.color = dataSet.color;
  }

  final barsPerEntry = buffer.barsPerEntry;
  var sections = 1;

  for (var j = 0; j < buffer.filledSize; j += 4) {
    final bar = j ~/ 4;
    final pos = buffer.positionOfBar(bar);

    if (dataSet.isStacked && bar % barsPerEntry == 0) {
      sections =
          dataSet
              .getEntryForIndex(buffer.entryOfBar(bar))
              .stackValues
              ?.length ??
          1;
    }

    if (!viewPortHandler.isInBoundsTop(buffer.buffer[j + 3])) break;

    if (!viewPortHandler.isInBoundsBottom(buffer.buffer[j + 1])) continue;

    if (!isSingleColor) {
      renderPaint.color = dataSet.getColor(pos);
    }

    final left = buffer.buffer[j];
    final top = buffer.buffer[j + 1];
    final right = buffer.buffer[j + 2];
    final bottom = buffer.buffer[j + 3];

    final corners = barCorners(dataSet, pos, sections, BarCorners.RIGHT);
    drawBarShape(c, dataSet, left, top, right, bottom, corners, () {
      if (isCustomFill) {
        dataSet
            .getFill(pos)
            .fillRect(
              c,
              renderPaint,
              left,
              top,
              right,
              bottom,
              isInverted ? FillDirection.LEFT : FillDirection.RIGHT,
            );
      } else {
        c.drawRectP(_barDrawRect..set(left, top, right, bottom), renderPaint);
      }

      if (drawBorder) {
        c.drawRectP(
          _barDrawRect..set(left, top, right, bottom),
          barBorderPaint,
        );
      }
    });
  }
}