Skip to content

Commit

Permalink
Merge pull request #23 from Triipaxx/feature/background-color
Browse files Browse the repository at this point in the history
added backgroundColor (initial)
  • Loading branch information
imaNNeo authored Jun 13, 2019
2 parents 969b547 + b838e79 commit 44fefbf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/src/chart/bar_chart/bar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ class BarChartData extends AxisChartData {
),
FlBorderData borderData,
double maxY,
Color backgroundColor,
}) : super(
gridData: gridData,
borderData: borderData,
backgroundColor: backgroundColor,
) {
initSuperMinMaxValues(maxY);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ class AxisChartData extends BaseChartData {
/// clip the chart to the border (prevent draw outside the border)
bool clipToBorder;

/// A background color which is drawn behind th chart.
Color backgroundColor;

AxisChartData({
this.gridData = const FlGridData(),
FlBorderData borderData,
this.minX, this.maxX,
this.minY, this.maxY,
this.clipToBorder = false,
this.backgroundColor,
}) : super(borderData: borderData);
}

Expand Down
23 changes: 23 additions & 0 deletions lib/src/chart/base/axis_chart/axis_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ abstract class AxisChartPainter<D extends AxisChartData> extends BaseChartPainte
final D data;

Paint gridPaint;
Paint backgroundPaint;

AxisChartPainter(this.data) : super(data) {
gridPaint = Paint()..style = PaintingStyle.fill;
backgroundPaint = Paint()..style = PaintingStyle.fill;
}

@override
void paint(Canvas canvas, Size size) {
super.paint(canvas, size);

drawBackground(canvas, size);
drawGrid(canvas, size);
}

Expand Down Expand Up @@ -76,6 +80,25 @@ abstract class AxisChartPainter<D extends AxisChartData> extends BaseChartPainte
}
}

/// This function draws a colored background behind the chart.
void drawBackground(Canvas canvas, Size viewSize) {
if (data.backgroundColor == null) {
return;
}

final Size usableViewSize = getChartUsableDrawSize(viewSize);
backgroundPaint.color = data.backgroundColor;
canvas.drawRect(
Rect.fromLTWH(
getLeftOffsetDrawSize(),
getTopOffsetDrawSize(),
usableViewSize.width,
usableViewSize.height,
),
backgroundPaint,
);
}

/// 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
Expand Down
2 changes: 2 additions & 0 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class LineChartData extends AxisChartData {
double minY,
double maxY,
bool clipToBorder = false,
Color backgroundColor,
}) : super(
gridData: gridData,
borderData: borderData,
clipToBorder: clipToBorder,
backgroundColor: backgroundColor,
) {
initSuperMinMaxValues(minX, maxX, minY, maxY);
}
Expand Down

0 comments on commit 44fefbf

Please sign in to comment.