Skip to content

Commit

Permalink
Remove getChartUsableDrawSize, getExtraNeededHorizontalSpace, getExtr…
Browse files Browse the repository at this point in the history
…aNeededVerticalSpace, getLeftOffsetDrawSize, and getTopOffsetDrawSize functions from base_chart_painter
  • Loading branch information
imaNNeo committed Mar 23, 2022
1 parent b368b82 commit 291f6ef
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 776 deletions.
46 changes: 21 additions & 25 deletions lib/src/chart/bar_chart/bar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
) {
final data = holder.data;
final viewSize = canvasWrapper.size;
final drawSize = getChartUsableDrawSize(viewSize, holder);

for (var i = 0; i < data.barGroups.length; i++) {
final barGroup = data.barGroups[i];
Expand Down Expand Up @@ -145,11 +144,11 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
// positive
final bottom = getPixelY(
max(data.minY, barRod.backDrawRodData.fromY),
drawSize,
viewSize,
holder,
);
final top = min(
getPixelY(barRod.backDrawRodData.toY, drawSize, holder),
getPixelY(barRod.backDrawRodData.toY, viewSize, holder),
bottom - cornerHeight,
);

Expand All @@ -167,11 +166,11 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
// negative
final top = getPixelY(
min(data.maxY, barRod.backDrawRodData.fromY),
drawSize,
viewSize,
holder,
);
final bottom = max(
getPixelY(barRod.backDrawRodData.toY, drawSize, holder),
getPixelY(barRod.backDrawRodData.toY, viewSize, holder),
top + cornerHeight,
);

Expand Down Expand Up @@ -201,9 +200,9 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
if (barRod.toY > barRod.fromY) {
// positive
final bottom =
getPixelY(max(data.minY, barRod.fromY), drawSize, holder);
getPixelY(max(data.minY, barRod.fromY), viewSize, holder);
final top = min(
getPixelY(barRod.toY, drawSize, holder), bottom - cornerHeight);
getPixelY(barRod.toY, viewSize, holder), bottom - cornerHeight);

barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom,
topLeft: borderRadius.topLeft,
Expand All @@ -213,9 +212,9 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
} else {
// negative
final top =
getPixelY(min(data.maxY, barRod.fromY), drawSize, holder);
getPixelY(min(data.maxY, barRod.fromY), viewSize, holder);
final bottom = max(
getPixelY(barRod.toY, drawSize, holder), top + cornerHeight);
getPixelY(barRod.toY, viewSize, holder), top + cornerHeight);

barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom,
topLeft: borderRadius.topLeft,
Expand All @@ -241,8 +240,8 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
if (barRod.rodStackItems.isNotEmpty) {
for (var i = 0; i < barRod.rodStackItems.length; i++) {
final stackItem = barRod.rodStackItems[i];
final stackFromY = getPixelY(stackItem.fromY, drawSize, holder);
final stackToY = getPixelY(stackItem.toY, drawSize, holder);
final stackFromY = getPixelY(stackItem.fromY, viewSize, holder);
final stackToY = getPixelY(stackItem.toY, viewSize, holder);

_barPaint.color = stackItem.color;
canvasWrapper.save();
Expand All @@ -259,7 +258,7 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
barRod.rodStackItems.length,
barRod.width,
barRRect,
drawSize,
viewSize,
holder);
}
}
Expand All @@ -281,7 +280,6 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
PaintHolder<BarChartData> holder,
) {
final viewSize = canvasWrapper.size;
final chartUsableSize = getChartUsableDrawSize(viewSize, holder);

const textsBelowMargin = 4;

Expand Down Expand Up @@ -327,13 +325,13 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
/// we should get the most top FlSpot Offset to draw the tooltip on top of it
final barOffset = Offset(
groupPositions[barGroupIndex].barsX[barRodIndex],
getPixelY(showOnRodData.toY, chartUsableSize, holder),
getPixelY(showOnRodData.toY, viewSize, holder),
);

final tooltipWidth = textWidth + tooltipData.tooltipPadding.horizontal;
final tooltipHeight = textHeight + tooltipData.tooltipPadding.vertical;

final zeroY = getPixelY(0, chartUsableSize, holder);
final zeroY = getPixelY(0, viewSize, holder);
final barTopY = min(zeroY, barOffset.dy);
final barBottomY = max(zeroY, barOffset.dy);
final drawTooltipOnTop = tooltipData.direction == TooltipDirection.top ||
Expand Down Expand Up @@ -508,8 +506,6 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
calculateGroupAndBarsPosition(viewSize, groupsX, data.barGroups);
}

final chartViewSize = getChartUsableDrawSize(viewSize, holder);

/// Find the nearest barRod
for (var i = 0; i < _groupBarsPosition!.length; i++) {
final groupBarPos = _groupBarsPosition![i];
Expand All @@ -524,17 +520,17 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
final isPositive = targetData.barGroups[i].barRods[j].toY > 0;
if (isPositive) {
barTopY = getPixelY(
targetData.barGroups[i].barRods[j].toY, chartViewSize, holder);
barBotY = getPixelY(0, chartViewSize, holder);
targetData.barGroups[i].barRods[j].toY, viewSize, holder);
barBotY = getPixelY(0, viewSize, holder);
} else {
barTopY = getPixelY(0, chartViewSize, holder);
barTopY = getPixelY(0, viewSize, holder);
barBotY = getPixelY(
targetData.barGroups[i].barRods[j].toY, chartViewSize, holder);
targetData.barGroups[i].barRods[j].toY, viewSize, holder);
}

final backDrawBarY = getPixelY(
targetData.barGroups[i].barRods[j].backDrawRodData.toY,
chartViewSize,
viewSize,
holder);
final touchExtraThreshold = targetData.barTouchData.touchExtraThreshold;

Expand Down Expand Up @@ -568,16 +564,16 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
final nearestSpot =
FlSpot(nearestGroup.x.toDouble(), nearestBarRod.toY);
final nearestSpotPos =
Offset(barX, getPixelY(nearestSpot.y, chartViewSize, holder));
Offset(barX, getPixelY(nearestSpot.y, viewSize, holder));

var touchedStackIndex = -1;
BarChartRodStackItem? touchedStack;
for (var stackIndex = 0;
stackIndex < nearestBarRod.rodStackItems.length;
stackIndex++) {
final stackItem = nearestBarRod.rodStackItems[stackIndex];
final fromPixel = getPixelY(stackItem.fromY, chartViewSize, holder);
final toPixel = getPixelY(stackItem.toY, chartViewSize, holder);
final fromPixel = getPixelY(stackItem.fromY, viewSize, holder);
final toPixel = getPixelY(stackItem.toY, viewSize, holder);
if (touchedPoint.dy <= fromPixel && touchedPoint.dy >= toPixel) {
touchedStackIndex = stackIndex;
touchedStack = stackItem;
Expand Down
67 changes: 23 additions & 44 deletions lib/src/chart/base/axis_chart/axis_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ abstract class AxisChartPainter<D extends AxisChartData>
return;
}
final viewSize = canvasWrapper.size;
final usableViewSize = getChartUsableDrawSize(viewSize, holder);
// Show Vertical Grid
if (data.gridData.drawVerticalLine) {
final verticalInterval = data.gridData.verticalInterval ??
Utils().getEfficientInterval(
usableViewSize.width,
viewSize.width,
data.horizontalDiff,
);
AxisChartHelper().iterateThroughAxis(
Expand All @@ -68,11 +67,11 @@ abstract class AxisChartPainter<D extends AxisChartData>
_gridPaint.strokeWidth = flLineStyle.strokeWidth;
_gridPaint.transparentIfWidthIsZero();

final bothX = getPixelX(axisValue, usableViewSize, holder);
final bothX = getPixelX(axisValue, viewSize, holder);
final x1 = bothX;
final y1 = 0 + getTopOffsetDrawSize(holder);
const y1 = 0.0;
final x2 = bothX;
final y2 = usableViewSize.height + getTopOffsetDrawSize(holder);
final y2 = viewSize.height;
canvasWrapper.drawDashedLine(Offset(x1, y1), Offset(x2, y2),
_gridPaint, flLineStyle.dashArray);
}
Expand All @@ -83,8 +82,7 @@ abstract class AxisChartPainter<D extends AxisChartData>
// Show Horizontal Grid
if (data.gridData.drawHorizontalLine) {
final horizontalInterval = data.gridData.horizontalInterval ??
Utils()
.getEfficientInterval(usableViewSize.height, data.verticalDiff);
Utils().getEfficientInterval(viewSize.height, data.verticalDiff);

AxisChartHelper().iterateThroughAxis(
min: data.minY,
Expand All @@ -100,10 +98,10 @@ abstract class AxisChartPainter<D extends AxisChartData>
_gridPaint.strokeWidth = flLine.strokeWidth;
_gridPaint.transparentIfWidthIsZero();

final bothY = getPixelY(axisValue, usableViewSize, holder);
final x1 = 0 + getLeftOffsetDrawSize(holder);
final bothY = getPixelY(axisValue, viewSize, holder);
const x1 = 0.0;
final y1 = bothY;
final x2 = usableViewSize.width + getLeftOffsetDrawSize(holder);
final x2 = viewSize.width;
final y2 = bothY;
canvasWrapper.drawDashedLine(
Offset(x1, y1), Offset(x2, y2), _gridPaint, flLine.dashArray);
Expand All @@ -122,15 +120,9 @@ abstract class AxisChartPainter<D extends AxisChartData>
}

final viewSize = canvasWrapper.size;
final usableViewSize = getChartUsableDrawSize(viewSize, holder);
_backgroundPaint.color = data.backgroundColor;
canvasWrapper.drawRect(
Rect.fromLTWH(
getLeftOffsetDrawSize(holder),
getTopOffsetDrawSize(holder),
usableViewSize.width,
usableViewSize.height,
),
Rect.fromLTWH(0, 0, viewSize.width, viewSize.height),
_backgroundPaint,
);
}
Expand All @@ -139,19 +131,13 @@ abstract class AxisChartPainter<D extends AxisChartData>
void drawRangeAnnotation(CanvasWrapper canvasWrapper, PaintHolder<D> holder) {
final data = holder.data;
final viewSize = canvasWrapper.size;
final chartUsableSize = getChartUsableDrawSize(viewSize, holder);

if (data.rangeAnnotations.verticalRangeAnnotations.isNotEmpty) {
for (var annotation in data.rangeAnnotations.verticalRangeAnnotations) {
final topChartPadding = getTopOffsetDrawSize(holder);
final from = Offset(
getPixelX(annotation.x1, chartUsableSize, holder), topChartPadding);

final bottomChartPadding =
getExtraNeededVerticalSpace(holder) - getTopOffsetDrawSize(holder);
final from = Offset(getPixelX(annotation.x1, viewSize, holder), 0.0);
final to = Offset(
getPixelX(annotation.x2, chartUsableSize, holder),
viewSize.height - bottomChartPadding,
getPixelX(annotation.x2, viewSize, holder),
viewSize.height,
);

final rect = Rect.fromPoints(from, to);
Expand All @@ -164,15 +150,10 @@ abstract class AxisChartPainter<D extends AxisChartData>

if (data.rangeAnnotations.horizontalRangeAnnotations.isNotEmpty) {
for (var annotation in data.rangeAnnotations.horizontalRangeAnnotations) {
final leftChartPadding = getLeftOffsetDrawSize(holder);
final from = Offset(leftChartPadding,
getPixelY(annotation.y1, chartUsableSize, holder));

final rightChartPadding = getExtraNeededHorizontalSpace(holder) -
getLeftOffsetDrawSize(holder);
final from = Offset(0.0, getPixelY(annotation.y1, viewSize, holder));
final to = Offset(
viewSize.width - rightChartPadding,
getPixelY(annotation.y2, chartUsableSize, holder),
viewSize.width,
getPixelY(annotation.y2, viewSize, holder),
);

final rect = Rect.fromPoints(from, to);
Expand All @@ -187,27 +168,25 @@ abstract class AxisChartPainter<D extends AxisChartData>
/// With this function we can convert our [FlSpot] x
/// to the view base axis x .
/// the view 0, 0 is on the top/left, but the spots is bottom/left
double getPixelX(double spotX, Size chartUsableSize, PaintHolder<D> holder) {
double getPixelX(double spotX, Size viewSize, PaintHolder<D> holder) {
final data = holder.data;
final deltaX = data.maxX - data.minX;
if (deltaX == 0.0) {
return getLeftOffsetDrawSize(holder);
return 0.0;
}
return (((spotX - data.minX) / deltaX) * chartUsableSize.width) +
getLeftOffsetDrawSize(holder);
return ((spotX - data.minX) / deltaX) * viewSize.width;
}

/// With this function we can convert our [FlSpot] y
/// to the view base axis y.
double getPixelY(double spotY, Size chartUsableSize, PaintHolder<D> holder) {
double getPixelY(double spotY, Size viewSize, PaintHolder<D> holder) {
final data = holder.data;
final deltaY = data.maxY - data.minY;
if (deltaY == 0.0) {
return chartUsableSize.height + getTopOffsetDrawSize(holder);
return viewSize.height;
}

var y = ((spotY - data.minY) / deltaY) * chartUsableSize.height;
y = chartUsableSize.height - y;
return y + getTopOffsetDrawSize(holder);
var y = ((spotY - data.minY) / deltaY) * viewSize.height;
y = viewSize.height - y;
return y;
}
}
44 changes: 4 additions & 40 deletions lib/src/chart/base/base_chart/base_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ class BaseChartPainter<D extends BaseChartData> {
}

final viewSize = canvasWrapper.size;
final chartViewSize = getChartUsableDrawSize(viewSize, holder);

final topLeft =
Offset(getLeftOffsetDrawSize(holder), getTopOffsetDrawSize(holder));
final topRight = Offset(getLeftOffsetDrawSize(holder) + chartViewSize.width,
getTopOffsetDrawSize(holder));
final bottomLeft = Offset(getLeftOffsetDrawSize(holder),
getTopOffsetDrawSize(holder) + chartViewSize.height);
final bottomRight = Offset(
getLeftOffsetDrawSize(holder) + chartViewSize.width,
getTopOffsetDrawSize(holder) + chartViewSize.height);
const topLeft = Offset(0, 0);
final topRight = Offset(viewSize.width, 0);
final bottomLeft = Offset(0, viewSize.height);
final bottomRight = Offset(viewSize.width, viewSize.height);

/// Draw Top Line
final topBorder = borderData.border.top;
Expand Down Expand Up @@ -80,36 +74,6 @@ class BaseChartPainter<D extends BaseChartData> {
canvasWrapper.drawLine(bottomLeft, topLeft, _borderPaint);
}
}

/// Calculate the size that we can draw our chart's main content.
/// [getExtraNeededHorizontalSpace] and [getExtraNeededVerticalSpace]
/// is the needed space to draw horizontal and vertical
/// stuff around our chart.
/// then we subtract them from raw [viewSize]
@Deprecated("It can be removed, we need to use [viewSize] directly")
Size getChartUsableDrawSize(Size viewSize, PaintHolder<D> holder) => viewSize;

/// Extra space needed to show horizontal contents around the chart,
/// like: left, right padding, left, right titles, and so on,
@Deprecated("We don't need it anymore, because there is no titles to paint")
double getExtraNeededHorizontalSpace(PaintHolder<D> holder) => 0;

/// Extra space needed to show vertical contents around the chart,
/// like: top, bottom padding, top, bottom titles, and so on,
@Deprecated("We don't need it anymore, because there is no titles to paint")
double getExtraNeededVerticalSpace(PaintHolder<D> holder) => 0;

/// Left offset to draw the chart's main content
/// we should use this to offset our x axis when we drawing the chart,
/// and the width space we can use to draw chart is[getChartUsableDrawSize.width]
@Deprecated("We don't need it anymore, because there is no titles to paint")
double getLeftOffsetDrawSize(PaintHolder<D> holder) => 0;

/// Top offset to draw the chart's main content
/// we should use this to offset our y axis when we drawing the chart,
/// and the height space we can use to draw chart is[getChartUsableDrawSize.height]
@Deprecated("We don't need it anymore, because there is no titles to paint")
double getTopOffsetDrawSize(PaintHolder<D> holder) => 0;
}

/// Holds data for painting on canvas
Expand Down
Loading

0 comments on commit 291f6ef

Please sign in to comment.