Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added backgroundColor (initial) #23

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
imaNNeo marked this conversation as resolved.
Show resolved Hide resolved
}) : 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,
imaNNeo marked this conversation as resolved.
Show resolved Hide resolved
}) : 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) {
imaNNeo marked this conversation as resolved.
Show resolved Hide resolved
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,
imaNNeo marked this conversation as resolved.
Show resolved Hide resolved
}) : super(
gridData: gridData,
borderData: borderData,
clipToBorder: clipToBorder,
backgroundColor: backgroundColor,
) {
initSuperMinMaxValues(minX, maxX, minY, maxY);
}
Expand Down