diff --git a/CHANGELOG.md b/CHANGELOG.md index 01fe2ca..b41ad8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.1.4] + +* Upgrade example to AndroidX and Android embedded v2 + ## [1.1.3] * Fix to support any lines diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 4ed2e25..5113dfc 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -38,7 +38,7 @@ android { targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { @@ -56,6 +56,6 @@ flutter { dependencies { testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 2877140..791cefa 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -7,7 +7,6 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> - + + + + + + + diff --git a/example/android/app/src/main/java/com/example/example/MainActivity.java b/example/android/app/src/main/java/com/example/example/MainActivity.java index a313e78..59f336d 100644 --- a/example/android/app/src/main/java/com/example/example/MainActivity.java +++ b/example/android/app/src/main/java/com/example/example/MainActivity.java @@ -1,14 +1,6 @@ package com.example.example; -import android.os.Bundle; - -import io.flutter.app.FlutterActivity; -import io.flutter.plugins.GeneratedPluginRegistrant; +import io.flutter.embedding.android.FlutterActivity; public class MainActivity extends FlutterActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - GeneratedPluginRegistrant.registerWith(this); - } } diff --git a/example/android/build.gradle b/example/android/build.gradle index bb8a303..6bc1cb8 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.android.tools.build:gradle:4.0.2' } } diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 2bd6f4f..2324ab5 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,2 +1,5 @@ org.gradle.jvmargs=-Xmx1536M +android.enableR8=true +android.useAndroidX=true +android.enableJetifier=true diff --git a/example/android/gradle/wrapper/gradle-wrapper.properties b/example/android/gradle/wrapper/gradle-wrapper.properties index 2819f02..1490bec 100644 --- a/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/example/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Jun 23 08:50:38 CEST 2017 +#Mon Oct 19 14:04:52 CEST 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip diff --git a/example/lib/main.dart b/example/lib/main.dart index 16e70bb..83f23c8 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -40,16 +40,11 @@ class _MyHomePageState extends State with FakeChartSeries { LineChart chart; if (chartIndex == 0) { - chart = LineChart.fromDateTimeMaps( - [line1, line2], [Colors.green, Colors.blue], ['C', 'C'], - tapTextFontWeight: FontWeight.w400); + chart = LineChart.fromDateTimeMaps([line1, line2], [Colors.green, Colors.blue], ['C', 'C'], tapTextFontWeight: FontWeight.w400); } else if (chartIndex == 1) { - chart = LineChart.fromDateTimeMaps( - [createLineAlmostSaveValues()], [Colors.green], ['C'], - tapTextFontWeight: FontWeight.w400); + chart = LineChart.fromDateTimeMaps([createLineAlmostSaveValues()], [Colors.green], ['C'], tapTextFontWeight: FontWeight.w400); } else { - chart = AreaLineChart.fromDateTimeMaps( - [line1], [Colors.red.shade900], ['C'], + chart = AreaLineChart.fromDateTimeMaps([line1], [Colors.red.shade900], ['C'], gradients: [Pair(Colors.yellow.shade400, Colors.red.shade700)]); } @@ -71,14 +66,10 @@ class _MyHomePageState extends State with FakeChartSeries { children: [ FlatButton( shape: RoundedRectangleBorder( - side: BorderSide(color: Colors.black45), - borderRadius: BorderRadius.all(Radius.circular(3))), + side: BorderSide(color: Colors.black45), borderRadius: BorderRadius.all(Radius.circular(3))), child: Text( 'LineChart', - style: TextStyle( - color: chartIndex == 0 - ? Colors.black - : Colors.black12), + style: TextStyle(color: chartIndex == 0 ? Colors.black : Colors.black12), ), onPressed: () { setState(() { @@ -88,13 +79,8 @@ class _MyHomePageState extends State with FakeChartSeries { ), FlatButton( shape: RoundedRectangleBorder( - side: BorderSide(color: Colors.black45), - borderRadius: BorderRadius.all(Radius.circular(3))), - child: Text('LineChart2', - style: TextStyle( - color: chartIndex == 1 - ? Colors.black - : Colors.black12)), + side: BorderSide(color: Colors.black45), borderRadius: BorderRadius.all(Radius.circular(3))), + child: Text('LineChart2', style: TextStyle(color: chartIndex == 1 ? Colors.black : Colors.black12)), onPressed: () { setState(() { chartIndex = 1; @@ -103,13 +89,8 @@ class _MyHomePageState extends State with FakeChartSeries { ), FlatButton( shape: RoundedRectangleBorder( - side: BorderSide(color: Colors.black45), - borderRadius: BorderRadius.all(Radius.circular(3))), - child: Text('AreaChart', - style: TextStyle( - color: chartIndex == 2 - ? Colors.black - : Colors.black12)), + side: BorderSide(color: Colors.black45), borderRadius: BorderRadius.all(Radius.circular(3))), + child: Text('AreaChart', style: TextStyle(color: chartIndex == 2 ? Colors.black : Colors.black12)), onPressed: () { setState(() { chartIndex = 2; @@ -125,6 +106,9 @@ class _MyHomePageState extends State with FakeChartSeries { child: AnimatedLineChart( chart, key: UniqueKey(), + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, ), //Unique key to force animations )), SizedBox(width: 200, height: 50, child: Text('')), diff --git a/example/pubspec.lock b/example/pubspec.lock index 1965fa0..f7ca2bd 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,69 +1,62 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.2.0-nullsafety.1" + clock: dependency: transitive description: - name: collection + name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.1.0-nullsafety.1" + collection: dependency: transitive description: - name: convert + name: collection url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.15.0-nullsafety.3" + fake_async: dependency: transitive description: - name: crypto + name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.2.0-nullsafety.1" fl_animated_linechart: dependency: "direct main" description: path: ".." relative: true source: path - version: "1.1.3" + version: "1.1.4" flutter: dependency: "direct main" description: flutter @@ -74,13 +67,6 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.12" intl: dependency: transitive description: @@ -94,35 +80,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.3" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -134,62 +106,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.6.1" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.6.0 <3.0.0" + dart: ">=2.10.0-110 <2.11.0" diff --git a/lib/chart/animated_line_chart.dart b/lib/chart/animated_line_chart.dart index 82e053b..b80d6b6 100644 --- a/lib/chart/animated_line_chart.dart +++ b/lib/chart/animated_line_chart.dart @@ -16,29 +16,32 @@ typedef TapText = String Function(String prefix, double y, String unit); class AnimatedLineChart extends StatefulWidget { final LineChart chart; final TapText tapText; + final TextStyle textStyle; + final Color toolTipColor; + final Color gridColor; const AnimatedLineChart( this.chart, { Key key, this.tapText, + @required this.textStyle, + @required this.gridColor, + @required this.toolTipColor, }) : super(key: key); @override _AnimatedLineChartState createState() => _AnimatedLineChartState(); } -class _AnimatedLineChartState extends State - with SingleTickerProviderStateMixin { +class _AnimatedLineChartState extends State with SingleTickerProviderStateMixin { AnimationController _controller; Animation _animation; @override void initState() { - _controller = - AnimationController(vsync: this, duration: Duration(milliseconds: 700)); + _controller = AnimationController(vsync: this, duration: Duration(milliseconds: 700)); - Animation curve = - CurvedAnimation(parent: _controller, curve: Curves.easeIn); + Animation curve = CurvedAnimation(parent: _controller, curve: Curves.easeIn); _animation = Tween(begin: 0.0, end: 1.0).animate(curve); @@ -55,13 +58,15 @@ class _AnimatedLineChartState extends State @override Widget build(BuildContext context) { - return LayoutBuilder( - builder: (BuildContext context, BoxConstraints constraints) { - widget.chart.initialize(constraints.maxWidth, constraints.maxHeight); + return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) { + widget.chart.initialize(constraints.maxWidth, constraints.maxHeight, widget.textStyle); return _GestureWrapper( widget.chart, _animation, tapText: widget.tapText, + gridColor: widget.gridColor, + textStyle: widget.textStyle, + toolTipColor: widget.toolTipColor, ); }); } @@ -72,12 +77,18 @@ class _GestureWrapper extends StatefulWidget { final LineChart _chart; final Animation _animation; final TapText tapText; + final TextStyle textStyle; + final Color toolTipColor; + final Color gridColor; const _GestureWrapper( this._chart, this._animation, { Key key, this.tapText, + this.gridColor, + this.toolTipColor, + this.textStyle }) : super(key: key); @override @@ -97,6 +108,9 @@ class _GestureWrapperState extends State<_GestureWrapper> { _horizontalDragPosition, animation: widget._animation, tapText: widget.tapText, + gridColor: widget.gridColor, + style: widget.textStyle, + toolTipColor: widget.toolTipColor, ), onTapDown: (tap) { _horizontalDragActive = true; @@ -131,20 +145,29 @@ class _AnimatedChart extends AnimatedWidget { final bool _horizontalDragActive; final double _horizontalDragPosition; final TapText tapText; + final TextStyle style; + final Color gridColor; + final Color toolTipColor; _AnimatedChart( - this._chart, this._horizontalDragActive, this._horizontalDragPosition, - {this.tapText, Key key, Animation animation}) - : super(key: key, listenable: animation); + this._chart, + this._horizontalDragActive, + this._horizontalDragPosition, { + this.tapText, + Key key, + Animation animation, + this.style, + this.gridColor, + this.toolTipColor, + }) : super(key: key, listenable: animation); @override Widget build(BuildContext context) { Animation animation = listenable as Animation; return CustomPaint( - painter: ChartPainter(animation?.value, _chart, _horizontalDragActive, - _horizontalDragPosition, - tapText: tapText), + painter: ChartPainter(animation?.value, _chart, _horizontalDragActive, _horizontalDragPosition, style, + tapText: tapText, gridColor: gridColor, toolTipColor: toolTipColor), ); } } @@ -156,62 +179,70 @@ class ChartPainter extends CustomPainter { final Paint _gridPainter = Paint() ..style = PaintingStyle.stroke - ..strokeWidth = 1 - ..color = Colors.black26; + ..strokeWidth = 1; Paint _linePainter = Paint() ..style = PaintingStyle.stroke - ..strokeWidth = 2 - ..color = Colors.black26; + ..strokeWidth = 2; Paint _fillPainter = Paint() ..style = PaintingStyle.fill ..strokeWidth = 2; - Paint _tooltipPainter = Paint() - ..style = PaintingStyle.fill - ..color = Colors.white.withAlpha(230); + Paint _tooltipPainter = Paint()..style = PaintingStyle.fill; final double _progress; final LineChart _chart; final bool _horizontalDragActive; final double _horizontalDragPosition; - final TapText tapText; + TapText tapText; + final TextStyle style; - static final TapText _defaultTapText = - (prefix, y, unit) => '$prefix: ${y.toStringAsFixed(1)} $unit'; + static final TapText _defaultTapText = (prefix, y, unit) => '$prefix: ${y.toStringAsFixed(1)} $unit'; - ChartPainter(this._progress, this._chart, this._horizontalDragActive, - this._horizontalDragPosition, - {TapText tapText}) - : tapText = tapText ?? _defaultTapText; + ChartPainter( + this._progress, + this._chart, + this._horizontalDragActive, + this._horizontalDragPosition, + this.style, { + this.tapText, + Color gridColor, + Color toolTipColor, + }) { + tapText = tapText ?? _defaultTapText; + _tooltipPainter.color = toolTipColor; + _gridPainter.color = gridColor; + _linePainter.color = gridColor; + } @override void paint(Canvas canvas, Size size) { _drawGrid(canvas, size); - _drawUnits(canvas, size); + _drawUnits(canvas, size, style); _drawLines(size, canvas); _drawAxisValues(canvas, size); if (_horizontalDragActive) { - _drawHighlights(size, canvas, _chart.tapTextFontWeight); + _drawHighlights( + size, + canvas, + _chart.tapTextFontWeight, + _tooltipPainter.color, + ); } } - void _drawHighlights(Size size, Canvas canvas, FontWeight tapTextFontWeight) { - _linePainter.color = Colors.black45; + void _drawHighlights(Size size, Canvas canvas, FontWeight tapTextFontWeight, Color onTapLineColor) { + _linePainter.color = onTapLineColor; - if (_horizontalDragPosition > LineChart.axisOffsetPX && - _horizontalDragPosition < size.width) { + if (_horizontalDragPosition > LineChart.axisOffsetPX && _horizontalDragPosition < size.width) { canvas.drawLine( - Offset(_horizontalDragPosition, 0), - Offset(_horizontalDragPosition, size.height - LineChart.axisOffsetPX), - _linePainter); + Offset(_horizontalDragPosition, 0), Offset(_horizontalDragPosition, size.height - LineChart.axisOffsetPX), _linePainter); } - List highlights = - _chart.getClosetHighlightPoints(_horizontalDragPosition); + List highlights = _chart.getClosetHighlightPoints(_horizontalDragPosition); List textPainters = List(); int index = 0; double minHighlightX = highlights[0].chartPoint.x; @@ -228,31 +259,23 @@ class ChartPainter extends CustomPainter { }); highlights.forEach((highlight) { - canvas.drawCircle(Offset(highlight.chartPoint.x, highlight.chartPoint.y), - 5, _linePainter); + canvas.drawCircle(Offset(highlight.chartPoint.x, highlight.chartPoint.y), 5, _linePainter); String prefix = ''; if (highlight.chartPoint is DateTimeChartPoint) { DateTimeChartPoint dateTimeChartPoint = highlight.chartPoint; - prefix = - _formatMonthDayHoursMinutes.format(dateTimeChartPoint.dateTime); + prefix = _formatMonthDayHoursMinutes.format(dateTimeChartPoint.dateTime); } TextSpan span = TextSpan( - style: TextStyle( - color: _chart.lines[index].color, - fontWeight: tapTextFontWeight ?? FontWeight.w400, - fontSize: 12), + style: style, text: tapText( prefix, highlight.yValue, _chart.lines[index].unit, )); - TextPainter tp = TextPainter( - text: span, - textAlign: TextAlign.right, - textDirection: TextDirectionHelper.getDirection()); + TextPainter tp = TextPainter(text: span, textAlign: TextAlign.right, textDirection: TextDirectionHelper.getDirection()); tp.layout(); @@ -272,15 +295,12 @@ class ChartPainter extends CustomPainter { minHighlightX -= 34; } - if (minHighlightY + tooltipHeight > - size.height - _chart.axisOffSetWithPadding) { - minHighlightY = - size.height - _chart.axisOffSetWithPadding - tooltipHeight; + if (minHighlightY + tooltipHeight > size.height - _chart.axisOffSetWithPadding) { + minHighlightY = size.height - _chart.axisOffSetWithPadding - tooltipHeight; } //Draw highlight bordered box: - Rect tooltipRect = Rect.fromLTWH( - minHighlightX - 5, minHighlightY - 5, maxWidth + 20, tooltipHeight); + Rect tooltipRect = Rect.fromLTWH(minHighlightX - 5, minHighlightY - 5, maxWidth + 20, tooltipHeight); canvas.drawRect(tooltipRect, _tooltipPainter); canvas.drawRect(tooltipRect, _gridPainter); @@ -297,13 +317,8 @@ class ChartPainter extends CustomPainter { //Draw main axis, should always be available: for (int c = 0; c <= (_stepCount + 1); c++) { TextPainter tp = _chart.yAxisTexts(0)[c]; - tp.paint( - canvas, - Offset( - _chart.axisOffSetWithPadding - tp.width, - (size.height - 6) - - (c * _chart.heightStepSize) - - LineChart.axisOffsetPX)); + tp.paint(canvas, + Offset(_chart.axisOffSetWithPadding - tp.width, (size.height - 6) - (c * _chart.heightStepSize) - LineChart.axisOffsetPX)); } if (_chart.yAxisCount == 2) { @@ -311,22 +326,15 @@ class ChartPainter extends CustomPainter { TextPainter tp = _chart.yAxisTexts(1)[c]; tp.paint( canvas, - Offset( - LineChart.axisMargin + size.width - _chart.xAxisOffsetPXright, - (size.height - 6) - - (c * _chart.heightStepSize) - - LineChart.axisOffsetPX)); + Offset(LineChart.axisMargin + size.width - _chart.xAxisOffsetPXright, + (size.height - 6) - (c * _chart.heightStepSize) - LineChart.axisOffsetPX)); } } //TODO: calculate and cache for (int c = 0; c <= (_stepCount + 1); c++) { - _drawRotatedText( - canvas, - _chart.xAxisTexts[c], - _chart.axisOffSetWithPadding + (c * _chart.widthStepSize), - size.height - (LineChart.axisOffsetPX - 5), - pi * 1.5); + _drawRotatedText(canvas, _chart.xAxisTexts[c], _chart.axisOffSetWithPadding + (c * _chart.widthStepSize), + size.height - (LineChart.axisOffsetPX - 5), pi * 1.5); } } @@ -342,14 +350,12 @@ class ChartPainter extends CustomPainter { bool drawCircles = points.length < 100; if (_progress < 1.0) { - path = AnimatedPathUtil.createAnimatedPath( - _chart.getPathCache(index), _progress); + path = AnimatedPathUtil.createAnimatedPath(_chart.getPathCache(index), _progress); } else { path = _chart.getPathCache(index); if (drawCircles) { - points.forEach((p) => canvas.drawCircle( - Offset(p.chartPoint.x, p.chartPoint.y), 2, _linePainter)); + points.forEach((p) => canvas.drawCircle(Offset(p.chartPoint.x, p.chartPoint.y), 2, _linePainter)); } } @@ -361,17 +367,14 @@ class ChartPainter extends CustomPainter { if (areaLineChart.gradients != null) { Pair gradient = areaLineChart.gradients[index]; - _fillPainter.shader = LinearGradient(stops: [ - 0.0, - 0.6 - ], colors: [ - gradient.left.withAlpha((220 * _progress).round()), - gradient.right.withAlpha((220 * _progress).round()) - ], begin: Alignment.bottomCenter, end: Alignment.topCenter) + _fillPainter.shader = LinearGradient( + stops: [0.0, 0.6], + colors: [gradient.left.withAlpha((220 * _progress).round()), gradient.right.withAlpha((220 * _progress).round())], + begin: Alignment.bottomCenter, + end: Alignment.topCenter) .createShader(Rect.fromLTWH(0, 0, size.width, size.height)); } else { - _fillPainter.color = - chartLine.color.withAlpha((200 * _progress).round()); + _fillPainter.color = chartLine.color.withAlpha((200 * _progress).round()); } Path areaPathCache = areaLineChart.getAreaPathCache(index); @@ -382,78 +385,39 @@ class ChartPainter extends CustomPainter { }); } - void _drawUnits(Canvas canvas, Size size) { + void _drawUnits(Canvas canvas, Size size, TextStyle style) { if (_chart.indexToUnit.length > 0) { - Color color; - - if (_chart.lines.length == 2 && _chart.indexToUnit.length == 2) { - color = _chart.lines[0].color; - } else { - color = Colors.black54; - } - - TextSpan span = TextSpan( - style: TextStyle( - color: color, fontWeight: FontWeight.w200, fontSize: 14), - text: _chart.indexToUnit[0]); - TextPainter tp = TextPainter( - text: span, - textAlign: TextAlign.right, - textDirection: TextDirectionHelper.getDirection()); + TextSpan span = TextSpan(style: style, text: _chart.indexToUnit[0]); + TextPainter tp = TextPainter(text: span, textAlign: TextAlign.right, textDirection: TextDirectionHelper.getDirection()); tp.layout(); tp.paint(canvas, Offset(_chart.xAxisOffsetPX, -16)); } if (_chart.indexToUnit.length == 2) { - Color color; - - if (_chart.lines.length == 2) { - color = _chart.lines[1].color; - } else { - color = Colors.black54; - } - - TextSpan span = TextSpan( - style: TextStyle( - color: color, fontWeight: FontWeight.w200, fontSize: 14), - text: _chart.indexToUnit[1]); - TextPainter tp = TextPainter( - text: span, - textAlign: TextAlign.right, - textDirection: TextDirectionHelper.getDirection()); + TextSpan span = TextSpan(style: style, text: _chart.indexToUnit[1]); + TextPainter tp = TextPainter(text: span, textAlign: TextAlign.right, textDirection: TextDirectionHelper.getDirection()); tp.layout(); - tp.paint(canvas, - Offset(size.width - tp.width - _chart.xAxisOffsetPXright, -16)); + tp.paint(canvas, Offset(size.width - tp.width - _chart.xAxisOffsetPXright, -16)); } } void _drawGrid(Canvas canvas, Size size) { canvas.drawRect( Rect.fromLTWH( - _chart.xAxisOffsetPX, - 0, - size.width - _chart.xAxisOffsetPX - _chart.xAxisOffsetPXright, - size.height - LineChart.axisOffsetPX), + _chart.xAxisOffsetPX, 0, size.width - _chart.xAxisOffsetPX - _chart.xAxisOffsetPXright, size.height - LineChart.axisOffsetPX), _gridPainter); for (double c = 1; c <= _stepCount; c++) { - canvas.drawLine( - Offset(_chart.xAxisOffsetPX, c * _chart.heightStepSize), - Offset(size.width - _chart.xAxisOffsetPXright, - c * _chart.heightStepSize), - _gridPainter); - canvas.drawLine( - Offset(c * _chart.widthStepSize + _chart.xAxisOffsetPX, 0), - Offset(c * _chart.widthStepSize + _chart.xAxisOffsetPX, - size.height - LineChart.axisOffsetPX), - _gridPainter); + canvas.drawLine(Offset(_chart.xAxisOffsetPX, c * _chart.heightStepSize), + Offset(size.width - _chart.xAxisOffsetPXright, c * _chart.heightStepSize), _gridPainter); + canvas.drawLine(Offset(c * _chart.widthStepSize + _chart.xAxisOffsetPX, 0), + Offset(c * _chart.widthStepSize + _chart.xAxisOffsetPX, size.height - LineChart.axisOffsetPX), _gridPainter); } } - void _drawRotatedText(Canvas canvas, TextPainter tp, double x, double y, - double angleRotationInRadians) { + void _drawRotatedText(Canvas canvas, TextPainter tp, double x, double y, double angleRotationInRadians) { canvas.save(); canvas.translate(x, y + tp.width); canvas.rotate(angleRotationInRadians); diff --git a/lib/chart/line_chart.dart b/lib/chart/line_chart.dart index 910a962..2ead150 100644 --- a/lib/chart/line_chart.dart +++ b/lib/chart/line_chart.dart @@ -113,7 +113,7 @@ class LineChart { } //Calculate ui pixels values - void initialize(double widthPX, double heightPX) { + void initialize(double widthPX, double heightPX, TextStyle style) { _calcScales(heightPX); //calc axis textpainters, before using @@ -154,10 +154,7 @@ class LineChart { } TextSpan span = new TextSpan( - style: new TextStyle( - color: Colors.grey[800], - fontWeight: FontWeight.w200, - fontSize: 10), + style: style, text: axisValueString); TextPainter tp = new TextPainter( text: span, @@ -225,10 +222,7 @@ class LineChart { fromTo.min.add(Duration(seconds: (stepInSeconds * c).round())); TextSpan span = new TextSpan( - style: new TextStyle( - color: Colors.grey[800], - fontSize: 11.0, - fontWeight: FontWeight.w200), + style: style, text: _formatDateTime(tick, duration)); TextPainter tp = new TextPainter( text: span, diff --git a/pubspec.lock b/pubspec.lock index b0bc373..8f49bbf 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,62 +1,55 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.2.0-nullsafety.1" + clock: dependency: transitive description: - name: collection + name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.1.0-nullsafety.1" + collection: dependency: transitive description: - name: convert + name: collection url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.15.0-nullsafety.3" + fake_async: dependency: transitive description: - name: crypto + name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -67,13 +60,6 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.12" intl: dependency: "direct main" description: @@ -87,35 +73,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.3" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -127,62 +99,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.6.1" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.6.0 <3.0.0" + dart: ">=2.10.0-110 <2.11.0" diff --git a/pubspec.yaml b/pubspec.yaml index 6317233..af41a24 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: fl_animated_linechart description: Animated flutter line & area chart. Performs with big datasets, support multiple y axis and datetime x axis -version: 1.1.3 -homepage: https://github.com/JacobJust/fl_animated_linechart +version: 1.1.4 +homepage: https://github.com/umbrew/fl_animated_linechart environment: sdk: ">=2.0.0 <3.0.0" @@ -9,7 +9,7 @@ environment: dependencies: flutter: sdk: flutter - intl: ^0.16.0 + intl: ^0.16.1 dev_dependencies: flutter_test: diff --git a/test/chart/animated_line_chart_test.dart b/test/chart/animated_line_chart_test.dart index fdd9f6d..f14d4d3 100644 --- a/test/chart/animated_line_chart_test.dart +++ b/test/chart/animated_line_chart_test.dart @@ -17,18 +17,29 @@ void main() { line[start.add(Duration(minutes: 10))] = 1.7; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.pink], ['P']); - lineChart.initialize(200, 100); - - await tester.pumpWidget(buildTestableWidget(AnimatedLineChart(lineChart))); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.pink], ['P']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); + + await tester.pumpWidget( + buildTestableWidget( + AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), + ), + ); int count = 0; expect(tester.hasRunningAnimations, true); await tester.pump(Duration(milliseconds: 50)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartWhileAnimating.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartWhileAnimating.png')); while (count < 20) { await tester.pump(Duration(milliseconds: 50)); @@ -37,12 +48,10 @@ void main() { expect(tester.hasRunningAnimations, false); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartAfterAnimation.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartAfterAnimation.png')); }); - testWidgets('Test horizontal drag multiple series', - (WidgetTester tester) async { + testWidgets('Test horizontal drag multiple series', (WidgetTester tester) async { DateTime start = DateTime.now(); List> series = List(); @@ -66,25 +75,31 @@ void main() { line2[start.add(Duration(minutes: 34))] = 1.1; series.add(line2); - LineChart lineChart = LineChart.fromDateTimeMaps( - series.reversed.toList(), [Colors.amber, Colors.pink], ['P', 'P']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series.reversed.toList(), [Colors.amber, Colors.pink], ['P', 'P']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); await tester.pumpWidget(buildTestableWidget(SizedBox( - child: AnimatedLineChart(lineChart), + child: AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), width: 500, height: 500, ))); await tester.pump(Duration(seconds: 1)); - TestGesture testGesture = - await tester.startGesture(Offset(0, 250), pointer: 7); + TestGesture testGesture = await tester.startGesture(Offset(0, 250), pointer: 7); await tester.pump(Duration(milliseconds: 50)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartDuringDrag.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartDuringDrag.png')); for (double c = 1; c < 100; c++) { await testGesture.moveBy(Offset(5 * c, 0)); @@ -95,12 +110,10 @@ void main() { await tester.pump(Duration(milliseconds: 50)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartAfterDrag.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartAfterDrag.png')); }); - testWidgets('Test horizontal drag multiple series different unit', - (WidgetTester tester) async { + testWidgets('Test horizontal drag multiple series different unit', (WidgetTester tester) async { DateTime start = DateTime.now(); List> series = List(); @@ -124,25 +137,31 @@ void main() { line2[start.add(Duration(minutes: 34))] = 1.1; series.add(line2); - LineChart lineChart = LineChart.fromDateTimeMaps( - series.reversed.toList(), [Colors.amber, Colors.pink], ['C', 'F']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series.reversed.toList(), [Colors.amber, Colors.pink], ['C', 'F']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); await tester.pumpWidget(buildTestableWidget(SizedBox( - child: AnimatedLineChart(lineChart), + child: AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), width: 500, height: 500, ))); await tester.pump(Duration(seconds: 1)); - TestGesture testGesture = - await tester.startGesture(Offset(0, 250), pointer: 7); + TestGesture testGesture = await tester.startGesture(Offset(0, 250), pointer: 7); await tester.pump(Duration(milliseconds: 50)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartDuringDragMultiUnit.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartDuringDragMultiUnit.png')); for (double c = 1; c < 100; c++) { await testGesture.moveBy(Offset(5 * c, 0)); @@ -153,8 +172,7 @@ void main() { await tester.pump(Duration(milliseconds: 50)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartAfterDragMultiUnit.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartAfterDragMultiUnit.png')); }); testWidgets('Test horizontal drag single serie', (WidgetTester tester) async { @@ -171,25 +189,31 @@ void main() { line[start.add(Duration(minutes: 30))] = 1.7; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); await tester.pumpWidget(buildTestableWidget(SizedBox( - child: AnimatedLineChart(lineChart), + child: AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), width: 500, height: 500, ))); await tester.pump(Duration(seconds: 1)); - TestGesture testGesture = - await tester.startGesture(Offset(250, 250), pointer: 7); + TestGesture testGesture = await tester.startGesture(Offset(250, 250), pointer: 7); await tester.pump(Duration(milliseconds: 50)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartDuringDragSingle.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartDuringDragSingle.png')); await testGesture.moveBy(Offset(20, 0)); @@ -201,8 +225,7 @@ void main() { await tester.pump(Duration(milliseconds: 50)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartAfterDragSingle.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartAfterDragSingle.png')); }); testWidgets('Test tooltip triggered by tap', (WidgetTester tester) async { @@ -219,12 +242,20 @@ void main() { line[start.add(Duration(minutes: 30))] = 1.7; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); await tester.pumpWidget(buildTestableWidget(SizedBox( - child: AnimatedLineChart(lineChart), + child: AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), width: 500, height: 500, ))); @@ -233,8 +264,7 @@ void main() { await tester.tapAt(Offset(250, 250)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartAfterDragSingle.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartAfterDragSingle.png')); }); testWidgets('serie with same values', (WidgetTester tester) async { @@ -251,20 +281,27 @@ void main() { line[start.add(Duration(minutes: 30))] = 100.0; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); await tester.pumpWidget(buildTestableWidget(SizedBox( - child: AnimatedLineChart(lineChart), + child: AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), width: 500, height: 500, ))); await tester.pump(Duration(seconds: 1)); - await expectLater(find.byType(AnimatedLineChart), - matchesGoldenFile('animatedLineChartSerieWithSameValues.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('animatedLineChartSerieWithSameValues.png')); }); testWidgets('area chart', (WidgetTester tester) async { @@ -281,20 +318,27 @@ void main() { line[start.add(Duration(minutes: 30))] = 90.0; series.add(line); - AreaLineChart lineChart = - AreaLineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); - lineChart.initialize(200, 100); + AreaLineChart lineChart = AreaLineChart.fromDateTimeMaps(series, [Colors.amber], ['W']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); await tester.pumpWidget(buildTestableWidget(SizedBox( - child: AnimatedLineChart(lineChart), + child: AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), width: 500, height: 500, ))); await tester.pump(Duration(seconds: 1)); - await expectLater( - find.byType(AnimatedLineChart), matchesGoldenFile('areaChart.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('areaChart.png')); }); testWidgets('area chart with gradient', (WidgetTester tester) async { @@ -311,19 +355,27 @@ void main() { line[start.add(Duration(minutes: 30))] = 90.0; series.add(line); - AreaLineChart lineChart = - AreaLineChart.fromDateTimeMaps(series, [Colors.red.shade900], ['C'], gradients: [Pair(Colors.yellow.shade400, Colors.red.shade700)]); - lineChart.initialize(200, 100); + AreaLineChart lineChart = AreaLineChart.fromDateTimeMaps(series, [Colors.red.shade900], ['C'], + gradients: [Pair(Colors.yellow.shade400, Colors.red.shade700)]); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); await tester.pumpWidget(buildTestableWidget(SizedBox( - child: AnimatedLineChart(lineChart), + child: AnimatedLineChart( + lineChart, + gridColor: Colors.black54, + textStyle: TextStyle(fontSize: 10, color: Colors.black54), + toolTipColor: Colors.white, + ), width: 500, height: 500, ))); await tester.pump(Duration(seconds: 1)); - await expectLater( - find.byType(AnimatedLineChart), matchesGoldenFile('areaChartGradient.png')); + await expectLater(find.byType(AnimatedLineChart), matchesGoldenFile('areaChartGradient.png')); }); } diff --git a/test/chart/area_line_chart_test.dart b/test/chart/area_line_chart_test.dart index 4b971f7..3ba5f48 100644 --- a/test/chart/area_line_chart_test.dart +++ b/test/chart/area_line_chart_test.dart @@ -12,54 +12,44 @@ void main() { test('path calculate', () async { double chartHeight = 100.0; - AreaLineChart lineChart = AreaLineChart( - [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], - Dates(DateTime.now(), DateTime.now()), null); - lineChart.initialize(200, chartHeight); + AreaLineChart lineChart = + AreaLineChart([ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], Dates(DateTime.now(), DateTime.now()), null); + lineChart.initialize( + 200, + chartHeight, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expectPathCacheToMatch( - lineChart.getAreaPathCache(0), - 10, - 2, - lineChart, - chartHeight, - 'W', - lineChart.xAxisOffsetPX, - lineChart.getPathCache(0).getBounds()); + lineChart.getAreaPathCache(0), 10, 2, lineChart, chartHeight, 'W', lineChart.xAxisOffsetPX, lineChart.getPathCache(0).getBounds()); }); test('path cache', () async { double chartHeight = 100.0; - AreaLineChart lineChart = AreaLineChart( - [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], - Dates(DateTime.now(), DateTime.now()), null); - lineChart.initialize(200, chartHeight); + AreaLineChart lineChart = + AreaLineChart([ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], Dates(DateTime.now(), DateTime.now()), null); + lineChart.initialize( + 200, + chartHeight, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); Path path = lineChart.getAreaPathCache(0); Path pathCached = lineChart.getAreaPathCache(0); - expect(path, pathCached, - reason: 'The two part should reference the same object reference'); + expect(path, pathCached, reason: 'The two part should reference the same object reference'); }); } -void expectPathCacheToMatch( - Path pathCache, - int pointCount, - double pointFactor, - LineChart lineChart, - double chartHeight, - String unit, - double xAxisOffsetPX, - Rect bounds) { +void expectPathCacheToMatch(Path pathCache, int pointCount, double pointFactor, LineChart lineChart, double chartHeight, String unit, + double xAxisOffsetPX, Rect bounds) { for (double c = 0; c < pointCount; c++) { double adjustedY = (c * pointFactor * lineChart.yScale(unit)); double y = (chartHeight - LineChart.axisOffsetPX) - adjustedY; Offset offset = Offset((c * lineChart.xScale) + xAxisOffsetPX, y); - expect(pathCache.contains(offset), true, - reason: 'Expect path to contain $c,${c * pointFactor}'); + expect(pathCache.contains(offset), true, reason: 'Expect path to contain $c,${c * pointFactor}'); } expect(pathCache.contains(Offset(bounds.left, bounds.bottom)), true); diff --git a/test/chart/line_chart_test.dart b/test/chart/line_chart_test.dart index bf3f776..76e92de 100644 --- a/test/chart/line_chart_test.dart +++ b/test/chart/line_chart_test.dart @@ -13,22 +13,31 @@ import '../testHelpers/chart_data_helper.dart'; void main() { test('init with empty dataset', () async { LineChart lineChart = LineChart([], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.seriesMap.isEmpty, true); - lineChart = LineChart([ChartLine([], Colors.black, 'C')], - Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + lineChart = LineChart([ChartLine([], Colors.black, 'C')], Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.seriesMap.isEmpty, true); }); test('init with 1 line', () async { - LineChart lineChart = LineChart( - [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], - Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart([ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.seriesMap.length, 1); expect(lineChart.seriesMap[0].length, 10); @@ -41,11 +50,14 @@ void main() { }); test('init with multiple lines', () async { - LineChart lineChart = LineChart([ - ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), - ChartLineHelper.createLine(15, 1, Colors.cyan, 'W') - ], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart( + [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), ChartLineHelper.createLine(15, 1, Colors.cyan, 'W')], + Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.seriesMap.length, 2); expect(lineChart.seriesMap[0].length, 10); @@ -68,9 +80,12 @@ void main() { line[start.add(Duration(minutes: 10))] = 1.7; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['C']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['C']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); List list = lineChart.seriesMap[0]; @@ -80,51 +95,66 @@ void main() { }); test('test xScale', () async { - LineChart lineChart = LineChart([ - ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), - ChartLineHelper.createLine(15, 1, Colors.cyan, 'W') - ], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart( + [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), ChartLineHelper.createLine(15, 1, Colors.cyan, 'W')], + Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.xScale, 11); }); test('test yScale', () async { - LineChart lineChart = LineChart([ - ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), - ChartLineHelper.createLine(15, 1, Colors.cyan, 'W') - ], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart( + [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), ChartLineHelper.createLine(15, 1, Colors.cyan, 'W')], + Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.yScale('W'), 2.314814814814815); }); test('test heightStepSize', () async { - LineChart lineChart = LineChart([ - ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), - ChartLineHelper.createLine(15, 1, Colors.cyan, 'W') - ], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart( + [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), ChartLineHelper.createLine(15, 1, Colors.cyan, 'W')], + Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.heightStepSize, 8.333333333333334); }); test('test widthStepSize', () async { - LineChart lineChart = LineChart([ - ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), - ChartLineHelper.createLine(15, 1, Colors.cyan, 'W') - ], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart( + [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), ChartLineHelper.createLine(15, 1, Colors.cyan, 'W')], + Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.widthStepSize, 25.666666666666668); }); test('test axisOffSetWithPadding', () async { - LineChart lineChart = LineChart([ - ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), - ChartLineHelper.createLine(15, 1, Colors.cyan, 'W') - ], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart( + [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), ChartLineHelper.createLine(15, 1, Colors.cyan, 'W')], + Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.axisOffSetWithPadding, 40.0); }); @@ -143,9 +173,12 @@ void main() { ChartPoint(minX, minY), ], Colors.pink, 'W'); - LineChart lineChart = LineChart([ChartLine([], Colors.amber, 'W'), line], - Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart([ChartLine([], Colors.amber, 'W'), line], Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.minX, minX); expect(lineChart.maxX, maxX); @@ -154,10 +187,12 @@ void main() { }); test('width and height calculated based on min and max values', () async { - LineChart lineChart = LineChart( - [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], - Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart([ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.width, 9); expect(lineChart.height('W'), 18.0); @@ -166,41 +201,44 @@ void main() { test('path calculate', () async { double chartHeight = 100.0; - LineChart lineChart = LineChart([ - ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), - ChartLineHelper.createLine(10, 4, Colors.cyan, 'W') - ], Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, chartHeight); - - expectPathCacheToMatch(lineChart.getPathCache(0), 10, 2, lineChart, - chartHeight, 'W', lineChart.xAxisOffsetPX); - expectPathCacheToMatch(lineChart.getPathCache(1), 10, 4, lineChart, - chartHeight, 'W', lineChart.xAxisOffsetPX); + LineChart lineChart = LineChart( + [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W'), ChartLineHelper.createLine(10, 4, Colors.cyan, 'W')], + Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + chartHeight, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); + + expectPathCacheToMatch(lineChart.getPathCache(0), 10, 2, lineChart, chartHeight, 'W', lineChart.xAxisOffsetPX); + expectPathCacheToMatch(lineChart.getPathCache(1), 10, 4, lineChart, chartHeight, 'W', lineChart.xAxisOffsetPX); }); test('path cache', () async { double chartHeight = 100.0; - LineChart lineChart = LineChart( - [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], - Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, chartHeight); + LineChart lineChart = LineChart([ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + chartHeight, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); Path path = lineChart.getPathCache(0); Path pathCached = lineChart.getPathCache(0); - expect(path, pathCached, - reason: 'The two part should reference the same object reference'); + expect(path, pathCached, reason: 'The two part should reference the same object reference'); }); test('find the highlight closest', () async { - LineChart lineChart = LineChart( - [ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], - Dates(DateTime.now(), DateTime.now())); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart([ChartLineHelper.createLine(10, 2, Colors.cyan, 'W')], Dates(DateTime.now(), DateTime.now())); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); - List closetHighlightPoints = - lineChart.getClosetHighlightPoints(101); + List closetHighlightPoints = lineChart.getClosetHighlightPoints(101); expect(closetHighlightPoints.length, 1); expect(closetHighlightPoints[0].chartPoint.x, 96.33333333333333); @@ -221,9 +259,12 @@ void main() { line[start.add(Duration(minutes: 30))] = 20; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['A']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['A']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); List axisTexts = lineChart.yAxisTexts(0); @@ -250,9 +291,12 @@ void main() { line[start.add(Duration(minutes: 30))] = 24.8; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['A']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['A']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); List axisTexts = lineChart.yAxisTexts(0); @@ -279,9 +323,12 @@ void main() { line[start.add(Duration(minutes: 30))] = 20; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['D']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['D']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); List axisTexts = lineChart.xAxisTexts; @@ -302,9 +349,12 @@ void main() { line[start.add(Duration(days: 6))] = 20; series.add(line); - LineChart lineChart = - LineChart.fromDateTimeMaps(series, [Colors.amber], ['F']); - lineChart.initialize(200, 100); + LineChart lineChart = LineChart.fromDateTimeMaps(series, [Colors.amber], ['F']); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); List axisTexts = lineChart.xAxisTexts; @@ -318,7 +368,11 @@ void main() { ChartLineHelper.createLine(10, 10.0, Colors.green, 'F'), ], Dates(DateTime.now().subtract(Duration(hours: 1)), DateTime.now())); - lineChart.initialize(200, 100); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); expect(lineChart.yScale('C'), 4.62962962962963); expect(lineChart.yScale('F'), 0.462962962962963); @@ -330,7 +384,11 @@ void main() { ChartLineHelper.createLine(10, 10.0, Colors.green, 'F'), ], Dates(DateTime.now().subtract(Duration(hours: 1)), DateTime.now())); - lineChart.initialize(200, 100); + lineChart.initialize( + 200, + 100, + TextStyle(color: Colors.grey[800], fontSize: 11.0, fontWeight: FontWeight.w200), + ); List leftSide = lineChart.seriesMap[0]; List rightSide = lineChart.seriesMap[1]; @@ -343,19 +401,12 @@ void main() { } void expectPathCacheToMatch( - Path pathCache, - int pointCount, - double pointFactor, - LineChart lineChart, - double chartHeight, - String unit, - double xAxisOffsetPX) { + Path pathCache, int pointCount, double pointFactor, LineChart lineChart, double chartHeight, String unit, double xAxisOffsetPX) { for (double c = 0; c < pointCount; c++) { double adjustedY = (c * pointFactor * lineChart.yScale(unit)); double y = (chartHeight - LineChart.axisOffsetPX) - adjustedY; Offset offset = Offset((c * lineChart.xScale) + xAxisOffsetPX, y); - expect(pathCache.contains(offset), true, - reason: 'Expect path to contain $c,${c * pointFactor}'); + expect(pathCache.contains(offset), true, reason: 'Expect path to contain $c,${c * pointFactor}'); } }