diff --git a/charts_common/lib/src/chart/bar/bar_renderer.dart b/charts_common/lib/src/chart/bar/bar_renderer.dart index 76cfd4f0f..b3126a8a1 100644 --- a/charts_common/lib/src/chart/bar/bar_renderer.dart +++ b/charts_common/lib/src/chart/bar/bar_renderer.dart @@ -137,6 +137,7 @@ class BarRenderer ImmutableAxis measureAxis, double measureAxisPosition, Color fillColor, + Color fillBackgroundColorPattern, FillPatternType fillPattern, double strokeWidthPx, int barGroupIndex, @@ -160,6 +161,7 @@ class BarRenderer measureAxis: measureAxis, fillColor: fillColor, fillPattern: fillPattern, + fillBackgroundColorPattern: fillBackgroundColorPattern, strokeWidthPx: strokeWidthPx, barGroupIndex: barGroupIndex, previousBarGroupWeight: previousBarGroupWeight, @@ -185,6 +187,7 @@ class BarRenderer double measureAxisPosition, Color fillColor, FillPatternType fillPattern, + Color fillBackgroundColorPattern, double strokeWidthPx, int barGroupIndex, double previousBarGroupWeight, @@ -197,6 +200,7 @@ class BarRenderer ..dashPattern = dashPattern ..fillColor = fillColor ..fillPattern = fillPattern + ..fillBackgroundColorPattern = fillBackgroundColorPattern ..measureAxisPosition = measureAxisPosition ..roundPx = details.roundPx ..strokeWidthPx = strokeWidthPx @@ -261,6 +265,7 @@ class BarRenderer bars.add(CanvasRect(bounds, dashPattern: bar.dashPattern, fill: bar.fillColor, + fillBackgroundColorPattern: bar.fillBackgroundColorPattern, pattern: bar.fillPattern, stroke: bar.color, strokeWidthPx: bar.strokeWidthPx)); diff --git a/charts_common/lib/src/chart/bar/bar_target_line_renderer.dart b/charts_common/lib/src/chart/bar/bar_target_line_renderer.dart index 1f4e7dae2..e079ec7be 100644 --- a/charts_common/lib/src/chart/bar/bar_target_line_renderer.dart +++ b/charts_common/lib/src/chart/bar/bar_target_line_renderer.dart @@ -67,6 +67,7 @@ class BarTargetLineRenderer extends BaseBarRenderer series) { series.colorFn ??= (_) => _color; series.fillColorFn ??= (_) => _color; + series.fillBackgroundColorPatternFn ??= (_) => Color.white; // Fill in missing seriesColor values with the color of the first datum in // the series. Note that [Series.colorFn] should always return a color. @@ -143,6 +144,7 @@ class BarTargetLineRenderer extends BaseBarRenderer measureAxis, double measureAxisPosition, Color fillColor, + Color fillBackgroundColorPattern, FillPatternType fillPattern, int barGroupIndex, double previousBarGroupWeight, @@ -166,6 +168,7 @@ class BarTargetLineRenderer extends BaseBarRenderer extends BaseBarRenderer measureAxis, double measureAxisPosition, Color fillColor, + Color fillBackgroundColorPattern, FillPatternType fillPattern, double strokeWidthPx, int barGroupIndex, @@ -203,6 +207,7 @@ class BarTargetLineRenderer extends BaseBarRenderer dashPattern; Color fillColor; + Color fillBackgroundColorPattern; FillPatternType fillPattern; double measureAxisPosition; num measureOffset; @@ -41,6 +42,9 @@ abstract class BaseBarRendererElement { fillColor = other.fillColor != null ? Color.fromOther(color: other.fillColor) : null; + fillBackgroundColorPattern = other.fillBackgroundColorPattern != null + ? Color.fromOther(color: other.fillBackgroundColorPattern) + : null; fillPattern = other.fillPattern; measureAxisPosition = other.measureAxisPosition; measureOffset = other.measureOffset; @@ -55,6 +59,8 @@ abstract class BaseBarRendererElement { color = getAnimatedColor(previous.color, target.color, animationPercent); fillColor = getAnimatedColor( previous.fillColor, target.fillColor, animationPercent); + fillBackgroundColorPattern = getAnimatedColor( + previous.fillBackgroundColorPattern, target.fillBackgroundColorPattern, animationPercent); measureIsNull = target.measureIsNull; measureIsNegative = target.measureIsNegative; } diff --git a/charts_common/lib/src/chart/common/canvas_shapes.dart b/charts_common/lib/src/chart/common/canvas_shapes.dart index 009dc6fc6..8275f9704 100644 --- a/charts_common/lib/src/chart/common/canvas_shapes.dart +++ b/charts_common/lib/src/chart/common/canvas_shapes.dart @@ -23,6 +23,7 @@ class CanvasRect { final Rectangle bounds; final List dashPattern; final Color fill; + final Color fillBackgroundColorPattern; final FillPatternType pattern; final Color stroke; final double strokeWidthPx; @@ -30,6 +31,7 @@ class CanvasRect { CanvasRect(this.bounds, {this.dashPattern, this.fill, + this.fillBackgroundColorPattern, this.pattern, this.stroke, this.strokeWidthPx}); diff --git a/charts_common/lib/src/chart/common/processed_series.dart b/charts_common/lib/src/chart/common/processed_series.dart index 7154e86ca..e903cafb2 100644 --- a/charts_common/lib/src/chart/common/processed_series.dart +++ b/charts_common/lib/src/chart/common/processed_series.dart @@ -53,6 +53,7 @@ class MutableSeries extends ImmutableSeries { AccessorFn colorFn; AccessorFn> dashPatternFn; AccessorFn fillColorFn; + AccessorFn fillBackgroundColorPatternFn; AccessorFn fillPatternFn; AccessorFn patternColorFn; AccessorFn radiusPxFn; @@ -104,6 +105,7 @@ class MutableSeries extends ImmutableSeries { colorFn = series.colorFn; dashPatternFn = series.dashPatternFn; fillColorFn = series.fillColorFn; + fillBackgroundColorPatternFn = series.fillBackgroundColorPatternFn; fillPatternFn = series.fillPatternFn; patternColorFn = series.patternColorFn; labelAccessorFn = series.labelAccessorFn ?? (i) => domainFn(i).toString(); @@ -147,6 +149,7 @@ class MutableSeries extends ImmutableSeries { colorFn = other.colorFn; dashPatternFn = other.dashPatternFn; fillColorFn = other.fillColorFn; + fillBackgroundColorPatternFn = other.fillBackgroundColorPatternFn; fillPatternFn = other.fillPatternFn; patternColorFn = other.patternColorFn; labelAccessorFn = other.labelAccessorFn; @@ -247,6 +250,8 @@ abstract class ImmutableSeries { AccessorFn get fillColorFn; + AccessorFn get fillBackgroundColorPatternFn; + AccessorFn get patternColorFn; AccessorFn get fillPatternFn; diff --git a/charts_common/lib/src/chart/common/series_renderer.dart b/charts_common/lib/src/chart/common/series_renderer.dart index 25fd0e8eb..9f61690b3 100644 --- a/charts_common/lib/src/chart/common/series_renderer.dart +++ b/charts_common/lib/src/chart/common/series_renderer.dart @@ -270,11 +270,13 @@ abstract class BaseSeriesRenderer implements SeriesRenderer { // Fill color defaults to the series color if no accessor is provided. series.fillColorFn ??= (int index) => series.colorFn(index); + series.fillBackgroundColorPatternFn ??= (int index) => Color.white; }); } else { seriesList.forEach((MutableSeries series) { // Fill color defaults to the series color if no accessor is provided. series.fillColorFn ??= (int index) => series.colorFn(index); + series.fillBackgroundColorPatternFn ??= (int index) => Color.white; }); } diff --git a/charts_common/lib/src/data/series.dart b/charts_common/lib/src/data/series.dart index ab8edec82..991f420de 100644 --- a/charts_common/lib/src/data/series.dart +++ b/charts_common/lib/src/data/series.dart @@ -93,6 +93,7 @@ class Series { /// [FillPatternType] beside `solid` is used. If not provided, then /// background color is used. final AccessorFn patternColorFn; + final AccessorFn fillBackgroundColorPatternFn; final AccessorFn fillPatternFn; final AccessorFn radiusPxFn; @@ -120,6 +121,7 @@ class Series { TypedAccessorFn domainUpperBoundFn, TypedAccessorFn fillColorFn, TypedAccessorFn patternColorFn, + TypedAccessorFn fillBackgroundColorPatternFn, TypedAccessorFn fillPatternFn, TypedAccessorFn keyFn, TypedAccessorFn labelAccessorFn, @@ -159,6 +161,9 @@ class Series { final _patternColorFn = patternColorFn == null ? null : (int index) => patternColorFn(data[index], index); + final _fillBackgroundColorPatternFn = fillBackgroundColorPatternFn == null + ? null + : (int index) => fillBackgroundColorPatternFn(data[index], index); final _fillPatternFn = fillPatternFn == null ? null : (int index) => fillPatternFn(data[index], index); @@ -208,6 +213,7 @@ class Series { fillPatternFn: _fillPatternFn, keyFn: _keyFn, patternColorFn: _patternColorFn, + fillBackgroundColorPatternFn: _fillBackgroundColorPatternFn, labelAccessorFn: _labelAccessorFn, insideLabelStyleAccessorFn: _insideLabelStyleAccessorFn, outsideLabelStyleAccessorFn: _outsideLabelStyleAccessorFn, @@ -238,6 +244,7 @@ class Series { this.fillColorFn, this.fillPatternFn, this.patternColorFn, + this.fillBackgroundColorPatternFn, this.keyFn, this.labelAccessorFn, this.insideLabelStyleAccessorFn, diff --git a/charts_flutter/example/lib/bar_chart/pattern_forward_hatch.dart b/charts_flutter/example/lib/bar_chart/pattern_forward_hatch.dart index 14e3bd4e6..b9d1093fc 100644 --- a/charts_flutter/example/lib/bar_chart/pattern_forward_hatch.dart +++ b/charts_flutter/example/lib/bar_chart/pattern_forward_hatch.dart @@ -76,12 +76,15 @@ class PatternForwardHatchBarChart extends StatelessWidget { domainFn: (OrdinalSales sales, _) => sales.year, measureFn: (OrdinalSales sales, _) => sales.sales, data: desktopSalesData, + fillPatternFn: (OrdinalSales sales, _) => + charts.FillPatternType.forwardHatch ), new charts.Series( id: 'Tablet', domainFn: (OrdinalSales sales, _) => sales.year, measureFn: (OrdinalSales sales, _) => sales.sales, data: tableSalesData, + fillBackgroundColorPatternFn: (OrdinalSales sales, _) => charts.Color.transparent, fillPatternFn: (OrdinalSales sales, _) => charts.FillPatternType.forwardHatch, ), @@ -90,6 +93,9 @@ class PatternForwardHatchBarChart extends StatelessWidget { domainFn: (OrdinalSales sales, _) => sales.year, measureFn: (OrdinalSales sales, _) => sales.sales, data: mobileSalesData, + fillBackgroundColorPatternFn: (OrdinalSales sales, _) => charts.Color.black, + fillPatternFn: (OrdinalSales sales, _) => + charts.FillPatternType.forwardHatch ), ]; } @@ -139,6 +145,7 @@ class PatternForwardHatchBarChart extends StatelessWidget { domainFn: (OrdinalSales sales, _) => sales.year, measureFn: (OrdinalSales sales, _) => sales.sales, data: tableSalesData, + fillBackgroundColorPatternFn: (OrdinalSales sales, _) => charts.Color.black, fillPatternFn: (OrdinalSales sales, _) => charts.FillPatternType.forwardHatch, ), diff --git a/charts_flutter/example/pubspec.yaml b/charts_flutter/example/pubspec.yaml index f898adf2d..bf1b7c255 100644 --- a/charts_flutter/example/pubspec.yaml +++ b/charts_flutter/example/pubspec.yaml @@ -12,3 +12,7 @@ dependencies: intl: ^0.15.2 flutter: uses-material-design: true + +dependency_overrides: + charts_common: + path: ../../charts_common \ No newline at end of file diff --git a/charts_flutter/lib/src/chart_canvas.dart b/charts_flutter/lib/src/chart_canvas.dart index ed38cdc24..825d758e1 100644 --- a/charts_flutter/lib/src/chart_canvas.dart +++ b/charts_flutter/lib/src/chart_canvas.dart @@ -33,6 +33,7 @@ import 'canvas/line_painter.dart' show LinePainter; import 'canvas/pie_painter.dart' show PiePainter; import 'canvas/point_painter.dart' show PointPainter; import 'canvas/polygon_painter.dart' show PolygonPainter; +import 'dart:math' as math; class ChartCanvas implements common.ChartCanvas { /// Pixels to allow to overdraw above the draw area that fades to transparent. @@ -148,6 +149,7 @@ class ChartCanvas implements common.ChartCanvas { void drawRect(Rectangle bounds, {common.Color fill, common.FillPatternType pattern, + common.Color fillPatternBackground, common.Color stroke, double strokeWidthPx, Rectangle drawAreaBounds}) { @@ -166,7 +168,7 @@ class ChartCanvas implements common.ChartCanvas { switch (pattern) { case common.FillPatternType.forwardHatch: _drawForwardHatchPattern(fillRectBounds, canvas, - fill: fill, drawAreaBounds: drawAreaBounds); + fill: fill, drawAreaBounds: drawAreaBounds, background: fillPatternBackground); break; case common.FillPatternType.solid: @@ -262,6 +264,7 @@ class ChartCanvas implements common.ChartCanvas { final segment = barStack.segments[barIndex]; drawRect(segment.bounds, fill: segment.fill, + fillPatternBackground: segment.fillBackgroundColorPattern, pattern: segment.pattern, stroke: segment.stroke, strokeWidthPx: segment.strokeWidthPx, @@ -386,6 +389,7 @@ class ChartCanvas implements common.ChartCanvas { drawAreaBounds.top.toDouble(), background); } + canvas.clipRect(_getRect(bounds), doAntiAlias: true); canvas.drawRect(_getRect(bounds), _paint); // As a simplification, we will treat the bounds as a large square and fill @@ -393,13 +397,14 @@ class ChartCanvas implements common.ChartCanvas { // Get the longer side of the bounds here for the size of this square. final size = max(bounds.width, bounds.height); + final isVertical = bounds.height >= bounds.width; + final x0 = bounds.left + size + fillWidthPx; final x1 = bounds.left - fillWidthPx; - final y0 = bounds.bottom - size - fillWidthPx; - final y1 = bounds.bottom + fillWidthPx; - final offset = 8; + final y0 = bounds.top + size + fillWidthPx; + final y1 = bounds.top - fillWidthPx; - final isVertical = bounds.height >= bounds.width; + final offset = 8; _linePainter ??= new LinePainter(); diff --git a/charts_flutter/pubspec.yaml b/charts_flutter/pubspec.yaml index 3bc3e2062..5135b45da 100644 --- a/charts_flutter/pubspec.yaml +++ b/charts_flutter/pubspec.yaml @@ -14,8 +14,7 @@ dependencies: # The pub version of charts_flutter will point to the pub version of charts_common. # The latest pub version is commented out and shown below as an example. # charts_common: 0.9.0 - charts_common: - path: ../charts_common/ + charts_common: ^0.9.0 collection: ^1.14.5 flutter: sdk: flutter @@ -23,9 +22,8 @@ dependencies: logging: any meta: ^1.1.1 - dev_dependencies: mockito: flutter_test: sdk: flutter - test: ^1.3.0 + test: ^1.3.0 \ No newline at end of file