diff --git a/CHANGELOG.md b/CHANGELOG.md index 48b7b8be3..772a7c40f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.12.3 +* [Bugfix] Fixed PieChart exception bug on sections tap, #514. +* [Bugfix] Fixed PieChart badges problem, #538. +* [Bugfix] Fixed Bug of drawing lines with strokeWidth zero, #558. +* [Improvement] Updated example app to support web. +* [Improvement] Show tooltips on mouse hover on Web, and Desktop. + ## 0.12.2 * [Bugfix] Fixed PieChart badges draw in first frame problem, #513. * [Improvement] Use CanvasWrapper to proxy draw functions (It does not have any effect on the result, it makes the code testable) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06b7b0fb2..52f55a278 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -21,6 +21,11 @@ an issue before starting writing your code. This helps us and the community to discuss the issue and choose what is deemed to be the best solution. +## Switch to `dev` branch + +You should make your changes upon the `dev` branch (All development changes happen in the `dev` branch, then I publish a new version and merge them into the `master`) + + ## Drawing architecture We have a *_chart_painter.dart class per each chart type. It draws elements into the Canvas. We made the CanvasWrapper class, because we wanted to test draw functions. @@ -49,6 +54,8 @@ Congratulations! Your code meets all of our guidelines :100:. Now you have to submit a pull request (or PR for short) to us. These are the steps you should follow when creating a PR: +- Make sure you select `dev` branch as your target branch. + - Make a descriptive title that summarizes what changes were in the PR. - Link to issues that this PR will fix (if any). diff --git a/README.md b/README.md index 7d193b05a..22ffe827a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Thank you all! ```yml dependencies: - fl_chart: ^0.12.2 + fl_chart: ^0.12.3 ``` @@ -69,9 +69,9 @@ flutter packages get #### :moneybag: Donation (bitcoin) :moneybag: ##### I work on this project in my free time because I have my personal life and job. You can push me to work more by donating. - + -`1BJt8apHJVreFyaPvseHcE17QQ3JwAdBcu` +`1L7ghKdcmgydmUJAnmYmMaiVjT1LoP4a45` ### Contributing diff --git a/analysis_options.yaml b/analysis_options.yaml index c7c7c577f..8dd99c73e 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,3 +1,4 @@ +include: package:pedantic/analysis_options.1.9.0.yaml analyzer: strong-mode: implicit-dynamic: false @@ -19,140 +20,4 @@ analyzer: - 'lib/i18n/stock_messages_*.dart' - 'lib/src/http/**' - 'example/**' - -linter: - rules: - # these rules are documented on and in the same order as - # the Dart Lint rules page to make maintenance easier - # https://github.com/dart-lang/linter/blob/master/example/all.yaml - - always_declare_return_types - - always_put_control_body_on_new_line - # - always_put_required_named_parameters_first # we prefer having parameters in the same order as fields https://github.com/flutter/flutter/issues/10219 - - always_require_non_null_named_parameters - - annotate_overrides - # - avoid_annotating_with_dynamic # conflicts with always_specify_types - - avoid_as - # - avoid_bool_literals_in_conditional_expressions # not yet tested - # - avoid_catches_without_on_clauses # we do this commonly - # - avoid_catching_errors # we do this commonly - - avoid_classes_with_only_static_members - # - avoid_double_and_int_checks # only useful when targeting JS runtime - - avoid_empty_else - - avoid_field_initializers_in_const_classes - - avoid_function_literals_in_foreach_calls - # - avoid_implementing_value_types # not yet tested - - avoid_init_to_null - # - avoid_js_rounded_ints # only useful when targeting JS runtime - - avoid_null_checks_in_equality_operators - # - avoid_positional_boolean_parameters # not yet tested - # - avoid_private_typedef_functions # we prefer having typedef (discussion in https://github.com/flutter/flutter/pull/16356) - - avoid_relative_lib_imports - - avoid_renaming_method_parameters - - avoid_return_types_on_setters - # - avoid_returning_null # there are plenty of valid reasons to return null - # - avoid_returning_null_for_future # not yet tested - - avoid_returning_null_for_void - # - avoid_returning_this # there are plenty of valid reasons to return this - # - avoid_setters_without_getters # not yet tested - # - avoid_shadowing_type_parameters # not yet tested - # - avoid_single_cascade_in_expression_statements # not yet tested - - avoid_slow_async_io - - avoid_types_as_parameter_names - # - avoid_types_on_closure_parameters # conflicts with always_specify_types - - avoid_unused_constructor_parameters - - avoid_void_async - - await_only_futures - - camel_case_types - - cancel_subscriptions - # - cascade_invocations # not yet tested - # - close_sinks # not reliable enough - # - comment_references # blocked on https://github.com/flutter/flutter/issues/20765 - # - constant_identifier_names # needs an opt-out https://github.com/dart-lang/linter/issues/204 - - control_flow_in_finally - # - curly_braces_in_flow_control_structures # not yet tested -# - directives_ordering - - empty_catches - - empty_constructor_bodies - - empty_statements - # - file_names # not yet tested - - flutter_style_todos - - hash_and_equals - - implementation_imports - # - invariant_booleans # too many false positives: https://github.com/dart-lang/linter/issues/811 - - iterable_contains_unrelated_type - # - join_return_with_assignment # not yet tested - - library_names - - library_prefixes - # - lines_longer_than_80_chars # not yet tested - - list_remove_unrelated_type - # - literal_only_boolean_expressions # too many false positives: https://github.com/dart-lang/sdk/issues/34181 - - no_adjacent_strings_in_list - - no_duplicate_case_values - - non_constant_identifier_names - # - null_closures # not yet tested - # - omit_local_variable_types # opposite of always_specify_types - # - one_member_abstracts # too many false positives - # - only_throw_errors # https://github.com/flutter/flutter/issues/5792 - - overridden_fields - - package_api_docs - - package_names - - package_prefixed_library_names - # - parameter_assignments # we do this commonly - - prefer_adjacent_string_concatenation - - prefer_asserts_in_initializer_lists - - prefer_collection_literals - - prefer_conditional_assignment - - prefer_const_constructors - - prefer_const_constructors_in_immutables - - prefer_const_declarations - - prefer_const_literals_to_create_immutables - # - prefer_constructors_over_static_methods # not yet tested - - prefer_contains - - prefer_equal_for_default_values - # - prefer_expression_function_bodies # conflicts with https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#consider-using--for-short-functions-and-methods - - prefer_final_fields - - prefer_final_locals - - prefer_foreach - # - prefer_function_declarations_over_variables # not yet tested - - prefer_generic_function_type_aliases - # - prefer_initializing_formals # Fucked up with Equatable, because it's added props field, then we couldn't have const constructor. - # - prefer_int_literals # not yet tested - # - prefer_interpolation_to_compose_strings # not yet tested - - prefer_is_empty - - prefer_is_not_empty - - prefer_iterable_whereType - # - prefer_mixin # https://github.com/dart-lang/language/issues/32 - # - prefer_null_aware_operators # disable until NNBD, see https://github.com/flutter/flutter/pull/32711#issuecomment-492930932 - - prefer_single_quotes - - prefer_typing_uninitialized_variables - - prefer_void_to_null - # - public_member_api_docs # enabled on a case-by-case basis; see e.g. packages/analysis_options.yaml - - recursive_getters - - slash_for_doc_comments - - sort_pub_dependencies - # - super_goes_last # no longer needed w/ Dart 2 - - test_types_in_equals - - throw_in_finally - # - type_annotate_public_apis # subset of always_specify_types - - type_init_formals - # - unawaited_futures # too many false positives - # - unnecessary_await_in_return # not yet tested - - unnecessary_brace_in_string_interps - - unnecessary_const - - unnecessary_getters_setters - # - unnecessary_lambdas # has false positives: https://github.com/dart-lang/linter/issues/498 - - unnecessary_new - - unnecessary_null_aware_assignments - - unnecessary_null_in_if_null_operators - - unnecessary_overrides - - unnecessary_parenthesis - - unnecessary_statements - - unnecessary_this - - unrelated_type_equality_checks - # - use_function_type_syntax_for_parameters # not yet tested - - use_rethrow_when_possible - # - use_setters_to_change_properties # not yet tested - # - use_string_buffers # has false positives: https://github.com/dart-lang/sdk/issues/34182 - # - use_to_and_as_if_applicable # has false positives, so we prefer to catch this by code-review - - valid_regexps - # - void_checks # not yet tested \ No newline at end of file + - 'test/**' \ No newline at end of file diff --git a/example/android/app/src/main/res/drawable-v21/launch_background.xml b/example/android/app/src/main/res/drawable-v21/launch_background.xml new file mode 100644 index 000000000..f74085f3f --- /dev/null +++ b/example/android/app/src/main/res/drawable-v21/launch_background.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/example/android/app/src/main/res/values-night/styles.xml b/example/android/app/src/main/res/values-night/styles.xml new file mode 100644 index 000000000..449a9f930 --- /dev/null +++ b/example/android/app/src/main/res/values-night/styles.xml @@ -0,0 +1,18 @@ + + + + + + + diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 000000000..18d981003 --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 000000000..f9b0d7c5e --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 000000000..18d981003 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 000000000..f9b0d7c5e --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/example/lib/main.dart b/example/lib/main.dart index 941dfdf7a..95d78fbb2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'bar_chart/bar_chart_page.dart'; @@ -36,23 +37,67 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + int _currentPage = 0; + + final _controller = PageController(initialPage: 0); + final _duration = Duration(milliseconds: 300); + final _curve = Curves.easeInOutCubic; + final _pages = [ + LineChartPage(), + BarChartPage(), + BarChartPage2(), + PieChartPage(), + LineChartPage2(), + LineChartPage3(), + LineChartPage4(), + ScatterChartPage(), + ]; + + @override + void initState() { + _controller.addListener(() { + setState(() { + _currentPage = _controller.page.round(); + }); + }); + } + @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: PageView( - children: [ - LineChartPage(), - BarChartPage(), - BarChartPage2(), - PieChartPage(), - LineChartPage2(), - LineChartPage3(), - LineChartPage4(), - ScatterChartPage(), - ], + physics: kIsWeb ? NeverScrollableScrollPhysics() : AlwaysScrollableScrollPhysics(), + controller: _controller, + children: _pages, ), ), + bottomNavigationBar: kIsWeb + ? Container( + padding: EdgeInsets.all(16), + color: Colors.transparent, + child: Row( + mainAxisSize: MainAxisSize.max, + children: [ + Visibility( + visible: _currentPage != 0, + child: FloatingActionButton( + onPressed: () => _controller.previousPage(duration: _duration, curve: _curve), + child: Icon(Icons.chevron_left_rounded), + ), + ), + Spacer(), + Visibility( + visible: _currentPage != _pages.length - 1, + child: FloatingActionButton( + onPressed: () => _controller.nextPage(duration: _duration, curve: _curve), + child: Icon(Icons.chevron_right_rounded), + ), + ), + ], + ), + ) + : null, ); } } diff --git a/example/lib/pie_chart/samples/pie_chart_sample1.dart b/example/lib/pie_chart/samples/pie_chart_sample1.dart index 02c567461..5daf5a1b2 100644 --- a/example/lib/pie_chart/samples/pie_chart_sample1.dart +++ b/example/lib/pie_chart/samples/pie_chart_sample1.dart @@ -78,7 +78,7 @@ class PieChartSample1State extends State { borderData: FlBorderData( show: false, ), - sectionsSpace: 12, + sectionsSpace: 1, centerSpaceRadius: 0, sections: showingSections()), ), diff --git a/example/lib/pie_chart/samples/pie_chart_sample3.dart b/example/lib/pie_chart/samples/pie_chart_sample3.dart index d2ba661b7..c7e2747b0 100644 --- a/example/lib/pie_chart/samples/pie_chart_sample3.dart +++ b/example/lib/pie_chart/samples/pie_chart_sample3.dart @@ -1,8 +1,8 @@ +import 'package:fl_chart/fl_chart.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; -import 'package:fl_chart/fl_chart.dart'; - /// Icons by svgrepo.com (https://www.svgrepo.com/collection/job-and-professions-3/) class PieChartSample3 extends StatefulWidget { @override @@ -154,10 +154,12 @@ class _Badge extends StatelessWidget { ), padding: EdgeInsets.all(size * .15), child: Center( - child: SvgPicture.asset( - svgAsset, - fit: BoxFit.contain, - ), + child: kIsWeb + ? Image.network(svgAsset, fit: BoxFit.contain) + : SvgPicture.asset( + svgAsset, + fit: BoxFit.contain, + ), ), ); } diff --git a/example/web/favicon.png b/example/web/favicon.png new file mode 100644 index 000000000..8aaa46ac1 Binary files /dev/null and b/example/web/favicon.png differ diff --git a/example/web/icons/Icon-192.png b/example/web/icons/Icon-192.png new file mode 100644 index 000000000..b749bfef0 Binary files /dev/null and b/example/web/icons/Icon-192.png differ diff --git a/example/web/icons/Icon-512.png b/example/web/icons/Icon-512.png new file mode 100644 index 000000000..88cfd48df Binary files /dev/null and b/example/web/icons/Icon-512.png differ diff --git a/example/web/index.html b/example/web/index.html new file mode 100644 index 000000000..1460b5e9b --- /dev/null +++ b/example/web/index.html @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + example + + + + + + + + diff --git a/example/web/manifest.json b/example/web/manifest.json new file mode 100644 index 000000000..8c012917d --- /dev/null +++ b/example/web/manifest.json @@ -0,0 +1,23 @@ +{ + "name": "example", + "short_name": "example", + "start_url": ".", + "display": "standalone", + "background_color": "#0175C2", + "theme_color": "#0175C2", + "description": "A new Flutter project.", + "orientation": "portrait-primary", + "prefer_related_applications": false, + "icons": [ + { + "src": "icons/Icon-192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "icons/Icon-512.png", + "sizes": "512x512", + "type": "image/png" + } + ] +} diff --git a/lib/src/chart/bar_chart/bar_chart.dart b/lib/src/chart/bar_chart/bar_chart.dart index 3daa7d65e..4238d6e3a 100644 --- a/lib/src/chart/bar_chart/bar_chart.dart +++ b/lib/src/chart/bar_chart/bar_chart.dart @@ -35,48 +35,24 @@ class _BarChartState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { - final BarChartData showingData = _getData(); - final BarTouchData touchData = showingData.barTouchData; + final showingData = _getData(); + final touchData = showingData.barTouchData; - return GestureDetector( - onLongPressStart: (d) { - final Size chartSize = _getChartSize(); + return MouseRegion( + onEnter: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } final BarTouchResponse response = - _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } - - final BarTouchResponse response = - _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onLongPressMoveUpdate: (d) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } - - final BarTouchResponse response = - _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanCancel: () { - final Size chartSize = _getChartSize(); + onExit: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -87,54 +63,116 @@ class _BarChartState extends AnimatedWidgetBaseState { touchData.touchCallback(response); } }, - onPanEnd: (DragEndDetails details) { - final Size chartSize = _getChartSize(); + onHover: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } final BarTouchResponse response = - _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanDown: (DragDownDetails details) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } + child: GestureDetector( + onLongPressStart: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } - final BarTouchResponse response = - _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanUpdate: (DragUpdateDetails details) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onLongPressEnd: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } - final BarTouchResponse response = - _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - child: CustomPaint( - key: _chartKey, - size: getDefaultSize(MediaQuery.of(context).size), - painter: BarChartPainter( - _withTouchedIndicators(_barChartDataTween.evaluate(animation)), - _withTouchedIndicators(showingData), - (touchHandler) { - setState(() { - _touchHandler = touchHandler; - }); - }, - textScale: MediaQuery.of(context).textScaleFactor, + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onLongPressMoveUpdate: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanCancel: () { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final BarTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanEnd: (DragEndDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanDown: (DragDownDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanUpdate: (DragUpdateDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + child: CustomPaint( + key: _chartKey, + size: getDefaultSize(MediaQuery.of(context).size), + painter: BarChartPainter( + _withTouchedIndicators(_barChartDataTween.evaluate(animation)), + _withTouchedIndicators(showingData), + (touchHandler) { + setState(() { + _touchHandler = touchHandler; + }); + }, + textScale: MediaQuery.of(context).textScaleFactor, + ), ), ), ); @@ -153,8 +191,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return barChartData; } - final List newGroups = []; - for (int i = 0; i < barChartData.barGroups.length; i++) { + final newGroups = []; + for (var i = 0; i < barChartData.barGroups.length; i++) { final group = barChartData.barGroups[i]; newGroups.add( diff --git a/lib/src/chart/bar_chart/bar_chart_data.dart b/lib/src/chart/bar_chart/bar_chart_data.dart index f453ca0b5..fa9a973d7 100644 --- a/lib/src/chart/bar_chart/bar_chart_data.dart +++ b/lib/src/chart/bar_chart/bar_chart_data.dart @@ -83,8 +83,8 @@ class BarChartData extends AxisChartData with EquatableMixin { double maxY, double minY, ) { - for (int i = 0; i < barGroups.length; i++) { - final BarChartGroupData barData = barGroups[i]; + for (var i = 0; i < barGroups.length; i++) { + final barData = barGroups[i]; if (barData.barRods == null || barData.barRods.isEmpty) { throw Exception('barRods could not be null or empty'); } @@ -101,10 +101,10 @@ class BarChartData extends AxisChartData with EquatableMixin { minY = 0; } - for (int i = 0; i < barGroups.length; i++) { - final BarChartGroupData barGroup = barGroups[i]; - for (int j = 0; j < barGroup.barRods.length; j++) { - final BarChartRodData rod = barGroup.barRods[j]; + for (var i = 0; i < barGroups.length; i++) { + final barGroup = barGroups[i]; + for (var j = 0; j < barGroup.barRods.length; j++) { + final rod = barGroup.barRods[j]; if (canModifyMaxY && rod.y > maxY) { maxY = rod.y; @@ -266,9 +266,9 @@ class BarChartGroupData with EquatableMixin { return 0; } - final double sumWidth = + final sumWidth = barRods.map((rodData) => rodData.width).reduce((first, second) => first + second); - final double spaces = (barRods.length - 1) * barsSpace; + final spaces = (barRods.length - 1) * barsSpace; return sumWidth + spaces; } @@ -752,7 +752,7 @@ BarTooltipItem defaultBarTooltipItem( BarChartRodData rod, int rodIndex, ) { - const TextStyle textStyle = TextStyle( + const textStyle = TextStyle( color: Colors.black, fontWeight: FontWeight.bold, fontSize: 14, diff --git a/lib/src/chart/bar_chart/bar_chart_painter.dart b/lib/src/chart/bar_chart/bar_chart_painter.dart index a30580250..1aca208d9 100644 --- a/lib/src/chart/bar_chart/bar_chart_painter.dart +++ b/lib/src/chart/bar_chart/bar_chart_painter.dart @@ -49,16 +49,16 @@ class BarChartPainter extends AxisChartPainter with TouchHandler groupsX = _calculateGroupsX(size, data.barGroups, data.alignment); + final groupsX = _calculateGroupsX(size, data.barGroups, data.alignment); _groupBarsPosition = _calculateGroupAndBarsPosition(size, groupsX, data.barGroups); _drawBars(canvasWrapper, _groupBarsPosition); drawAxisTitles(canvasWrapper); _drawTitles(canvasWrapper, _groupBarsPosition); - for (int i = 0; i < targetData.barGroups.length; i++) { + for (var i = 0; i < targetData.barGroups.length; i++) { final barGroup = targetData.barGroups[i]; - for (int j = 0; j < barGroup.barRods.length; j++) { + for (var j = 0; j < barGroup.barRods.length; j++) { if (!barGroup.showingTooltipIndicators.contains(j)) { continue; } @@ -73,15 +73,15 @@ class BarChartPainter extends AxisChartPainter with TouchHandler _calculateGroupsX( Size viewSize, List barGroups, BarChartAlignment alignment) { - final Size drawSize = getChartUsableDrawSize(viewSize); + final drawSize = getChartUsableDrawSize(viewSize); - final List groupsX = List(barGroups.length); + final groupsX = List.filled(barGroups.length, 0.0, growable: false); - final double leftTextsSpace = getLeftOffsetDrawSize(); + final leftTextsSpace = getLeftOffsetDrawSize(); switch (alignment) { case BarChartAlignment.start: - double tempX = 0; + var tempX = 0.0; barGroups.asMap().forEach((i, group) { groupsX[i] = leftTextsSpace + tempX + group.width / 2; tempX += group.width; @@ -89,8 +89,8 @@ class BarChartPainter extends AxisChartPainter with TouchHandler= 0; i--) { + var tempX = 0.0; + for (var i = barGroups.length - 1; i >= 0; i--) { final group = barGroups[i]; groupsX[i] = (leftTextsSpace + drawSize.width) - tempX - group.width / 2; tempX += group.width; @@ -98,26 +98,26 @@ class BarChartPainter extends AxisChartPainter with TouchHandler group.width).reduce((a, b) => a + b); + var sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); sumWidth += data.groupsSpace * (barGroups.length - 1); - final double horizontalMargin = (drawSize.width - sumWidth) / 2; + final horizontalMargin = (drawSize.width - sumWidth) / 2; - double tempX = 0; - for (int i = 0; i < barGroups.length; i++) { + var tempX = 0.0; + for (var i = 0; i < barGroups.length; i++) { final group = barGroups[i]; groupsX[i] = leftTextsSpace + horizontalMargin + tempX + group.width / 2; - final double groupSpace = i == barGroups.length - 1 ? 0 : data.groupsSpace; + final groupSpace = i == barGroups.length - 1 ? 0 : data.groupsSpace; tempX += group.width + groupSpace; } break; case BarChartAlignment.spaceBetween: - final double sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); - final double spaceAvailable = drawSize.width - sumWidth; - final double eachSpace = spaceAvailable / (barGroups.length - 1); + final sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); + final spaceAvailable = drawSize.width - sumWidth; + final eachSpace = spaceAvailable / (barGroups.length - 1); - double tempX = 0; + var tempX = 0.0; barGroups.asMap().forEach((index, group) { tempX += group.width / 2; if (index != 0) { @@ -129,11 +129,11 @@ class BarChartPainter extends AxisChartPainter with TouchHandler group.width).reduce((a, b) => a + b); - final double spaceAvailable = drawSize.width - sumWidth; - final double eachSpace = spaceAvailable / (barGroups.length * 2); + final sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); + final spaceAvailable = drawSize.width - sumWidth; + final eachSpace = spaceAvailable / (barGroups.length * 2); - double tempX = 0; + var tempX = 0.0; barGroups.asMap().forEach((i, group) { tempX += eachSpace; tempX += group.width / 2; @@ -144,11 +144,11 @@ class BarChartPainter extends AxisChartPainter with TouchHandler group.width).reduce((a, b) => a + b); - final double spaceAvailable = drawSize.width - sumWidth; - final double eachSpace = spaceAvailable / (barGroups.length + 1); + final sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); + final spaceAvailable = drawSize.width - sumWidth; + final eachSpace = spaceAvailable / (barGroups.length + 1); - double tempX = 0; + var tempX = 0.0; barGroups.asMap().forEach((i, group) { tempX += eachSpace; tempX += group.width / 2; @@ -168,15 +168,15 @@ class BarChartPainter extends AxisChartPainter with TouchHandler groupBarsPosition = []; - for (int i = 0; i < barGroups.length; i++) { - final BarChartGroupData barGroup = barGroups[i]; - final double groupX = groupsX[i]; + final groupBarsPosition = <_GroupBarsPosition>[]; + for (var i = 0; i < barGroups.length; i++) { + final barGroup = barGroups[i]; + final groupX = groupsX[i]; - double tempX = 0; - final List barsX = []; + var tempX = 0.0; + final barsX = []; barGroup.barRods.asMap().forEach((barIndex, barRod) { - final double widthHalf = barRod.width / 2; + final widthHalf = barRod.width / 2; barsX.add(groupX - (barGroup.width / 2) + tempX + widthHalf); tempX += barRod.width + barGroup.barsSpace; }); @@ -189,15 +189,14 @@ class BarChartPainter extends AxisChartPainter with TouchHandler with TouchHandler stops = []; + var stops = []; if (barRod.backDrawRodData.colorStops == null || barRod.backDrawRodData.colorStops.length != barRod.backDrawRodData.colors.length) { /// provided colorStops is invalid and we calculate it here @@ -296,7 +295,7 @@ class BarChartPainter extends AxisChartPainter with TouchHandler stops = []; + var stops = []; if (barRod.colorStops == null || barRod.colorStops.length != barRod.colors.length) { /// provided colorStops is invalid and we calculate it here barRod.colors.asMap().forEach((index, color) { @@ -324,7 +323,7 @@ class BarChartPainter extends AxisChartPainter with TouchHandler with TouchHandler with TouchHandler with TouchHandler with TouchHandler with TouchHandler with TouchHandler with TouchHandler with TouchHandler 0; - final double tooltipWidth = textWidth + tooltipData.tooltipPadding.horizontal; - final double tooltipHeight = textHeight + tooltipData.tooltipPadding.vertical; + final tooltipWidth = textWidth + tooltipData.tooltipPadding.horizontal; + final tooltipHeight = textHeight + tooltipData.tooltipPadding.vertical; - final double tooltipTop = isPositive + final tooltipTop = isPositive ? barOffset.dy - tooltipHeight - tooltipData.tooltipBottomMargin : barOffset.dy + tooltipData.tooltipBottomMargin; /// draw the background rect with rounded radius + // ignore: omit_local_variable_types Rect rect = Rect.fromLTWH(barOffset.dx - (tooltipWidth / 2), tooltipTop, tooltipWidth, tooltipHeight); @@ -597,14 +597,14 @@ class BarChartPainter extends AxisChartPainter with TouchHandler with TouchHandler with TouchHandler with TouchHandler with TouchHandler groupBarsPosition) { if (groupBarsPosition == null) { - final List groupsX = _calculateGroupsX(viewSize, data.barGroups, data.alignment); + final groupsX = _calculateGroupsX(viewSize, data.barGroups, data.alignment); groupBarsPosition = _calculateGroupAndBarsPosition(viewSize, groupsX, data.barGroups); } - final Size chartViewSize = getChartUsableDrawSize(viewSize); + final chartViewSize = getChartUsableDrawSize(viewSize); /// Find the nearest barRod - for (int i = 0; i < groupBarsPosition.length; i++) { - final _GroupBarsPosition groupBarPos = groupBarsPosition[i]; - for (int j = 0; j < groupBarPos.barsX.length; j++) { - final double barX = groupBarPos.barsX[j]; - final double barWidth = targetData.barGroups[i].barRods[j].width; - final double halfBarWidth = barWidth / 2; + for (var i = 0; i < groupBarsPosition.length; i++) { + final groupBarPos = groupBarsPosition[i]; + for (var j = 0; j < groupBarPos.barsX.length; j++) { + final barX = groupBarPos.barsX[j]; + final barWidth = targetData.barGroups[i].barRods[j].width; + final halfBarWidth = barWidth / 2; double barTopY; double barBotY; - final bool isPositive = targetData.barGroups[i].barRods[j].y > 0; + final isPositive = targetData.barGroups[i].barRods[j].y > 0; if (isPositive) { barTopY = getPixelY(targetData.barGroups[i].barRods[j].y, chartViewSize); barBotY = getPixelY(0, chartViewSize); @@ -728,15 +727,15 @@ class BarChartPainter extends AxisChartPainter with TouchHandler= barX - halfBarWidth - touchExtraThreshold.left); - final bool isYInBarBounds = (touchedPoint.dy <= barBotY + touchExtraThreshold.bottom) && + final isYInBarBounds = (touchedPoint.dy <= barBotY + touchExtraThreshold.bottom) && (touchedPoint.dy >= barTopY - touchExtraThreshold.top); bool isYInBarBackDrawBounds; @@ -748,7 +747,7 @@ class BarChartPainter extends AxisChartPainter with TouchHandler with TouchHandler= toPixel) { diff --git a/lib/src/chart/base/axis_chart/axis_chart_painter.dart b/lib/src/chart/base/axis_chart/axis_chart_painter.dart index 714e7b383..f28e0c9e5 100644 --- a/lib/src/chart/base/axis_chart/axis_chart_painter.dart +++ b/lib/src/chart/base/axis_chart/axis_chart_painter.dart @@ -4,6 +4,7 @@ import 'package:fl_chart/src/chart/bar_chart/bar_chart_painter.dart'; import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart'; import 'package:fl_chart/src/chart/line_chart/line_chart_painter.dart'; import 'package:fl_chart/src/extensions/canvas_extension.dart'; +import 'package:fl_chart/src/extensions/paint_extension.dart'; import 'package:fl_chart/src/utils/canvas_wrapper.dart'; import 'package:fl_chart/src/utils/utils.dart'; import 'package:flutter/material.dart'; @@ -56,8 +57,8 @@ abstract class AxisChartPainter extends BaseChartPainte // Left Title final leftTitle = axisTitles.leftTitle; if (leftTitle.showTitle) { - final TextSpan span = TextSpan(style: leftTitle.textStyle, text: leftTitle.titleText); - final TextPainter tp = TextPainter( + final span = TextSpan(style: leftTitle.textStyle, text: leftTitle.titleText); + final tp = TextPainter( text: span, textAlign: leftTitle.textAlign, textDirection: TextDirection.ltr, @@ -73,8 +74,8 @@ abstract class AxisChartPainter extends BaseChartPainte // Top title final topTitle = axisTitles.topTitle; if (topTitle.showTitle) { - final TextSpan span = TextSpan(style: topTitle.textStyle, text: topTitle.titleText); - final TextPainter tp = TextPainter( + final span = TextSpan(style: topTitle.textStyle, text: topTitle.titleText); + final tp = TextPainter( text: span, textAlign: topTitle.textAlign, textDirection: TextDirection.ltr, @@ -87,8 +88,8 @@ abstract class AxisChartPainter extends BaseChartPainte // Right Title final rightTitle = axisTitles.rightTitle; if (rightTitle.showTitle) { - final TextSpan span = TextSpan(style: rightTitle.textStyle, text: rightTitle.titleText); - final TextPainter tp = TextPainter( + final span = TextSpan(style: rightTitle.textStyle, text: rightTitle.titleText); + final tp = TextPainter( text: span, textAlign: rightTitle.textAlign, textDirection: TextDirection.ltr, @@ -106,8 +107,8 @@ abstract class AxisChartPainter extends BaseChartPainte // Bottom title final bottomTitle = axisTitles.bottomTitle; if (bottomTitle.showTitle) { - final TextSpan span = TextSpan(style: bottomTitle.textStyle, text: bottomTitle.titleText); - final TextPainter tp = TextPainter( + final span = TextSpan(style: bottomTitle.textStyle, text: bottomTitle.titleText); + final tp = TextPainter( text: span, textAlign: bottomTitle.textAlign, textDirection: TextDirection.ltr, @@ -126,7 +127,7 @@ abstract class AxisChartPainter extends BaseChartPainte /// It returns extra needed spaces in left and right side of the chart. @override double getExtraNeededHorizontalSpace() { - double sum = super.getExtraNeededHorizontalSpace(); + var sum = super.getExtraNeededHorizontalSpace(); if (data.axisTitleData.show) { final leftSide = data.axisTitleData.leftTitle; @@ -149,7 +150,7 @@ abstract class AxisChartPainter extends BaseChartPainte /// It returns extra needed spaces in bottom and top side of the chart. @override double getExtraNeededVerticalSpace() { - double sum = super.getExtraNeededVerticalSpace(); + var sum = super.getExtraNeededVerticalSpace(); if (data.axisTitleData.show) { final topSide = data.axisTitleData.topTitle; @@ -206,27 +207,28 @@ abstract class AxisChartPainter extends BaseChartPainte final usableViewSize = getChartUsableDrawSize(viewSize); // Show Vertical Grid if (data.gridData.drawVerticalLine) { - final double verticalInterval = data.gridData.verticalInterval ?? + final verticalInterval = data.gridData.verticalInterval ?? getEfficientInterval(viewSize.width, data.horizontalDiff); - double verticalSeek = data.minX + verticalInterval; + var verticalSeek = data.minX + verticalInterval; - final double delta = data.maxX - data.minX; - final int count = delta ~/ verticalInterval; - final double lastPosition = count * verticalSeek; - final bool lastPositionOverlapsWithBorder = lastPosition == data.maxX; + final delta = data.maxX - data.minX; + final count = delta ~/ verticalInterval; + final lastPosition = count * verticalSeek; + final lastPositionOverlapsWithBorder = lastPosition == data.maxX; final end = lastPositionOverlapsWithBorder ? data.maxX - verticalInterval : data.maxX; while (verticalSeek <= end) { if (data.gridData.checkToShowVerticalLine(verticalSeek)) { - final FlLine flLineStyle = data.gridData.getDrawingVerticalLine(verticalSeek); + final flLineStyle = data.gridData.getDrawingVerticalLine(verticalSeek); _gridPaint.color = flLineStyle.color; _gridPaint.strokeWidth = flLineStyle.strokeWidth; + _gridPaint.transparentIfWidthIsZero(); - final double bothX = getPixelX(verticalSeek, usableViewSize); - final double x1 = bothX; - final double y1 = 0 + getTopOffsetDrawSize(); - final double x2 = bothX; - final double y2 = usableViewSize.height + getTopOffsetDrawSize(); + final bothX = getPixelX(verticalSeek, usableViewSize); + final x1 = bothX; + final y1 = 0 + getTopOffsetDrawSize(); + final x2 = bothX; + final y2 = usableViewSize.height + getTopOffsetDrawSize(); canvasWrapper.drawDashedLine( Offset(x1, y1), Offset(x2, y2), _gridPaint, flLineStyle.dashArray); } @@ -236,28 +238,29 @@ abstract class AxisChartPainter extends BaseChartPainte // Show Horizontal Grid if (data.gridData.drawHorizontalLine) { - final double horizontalInterval = data.gridData.horizontalInterval ?? + final horizontalInterval = data.gridData.horizontalInterval ?? getEfficientInterval(viewSize.height, data.verticalDiff); - double horizontalSeek = data.minY + horizontalInterval; + var horizontalSeek = data.minY + horizontalInterval; - final double delta = data.maxY - data.minY; - final int count = delta ~/ horizontalInterval; - final double lastPosition = count * horizontalSeek; - final bool lastPositionOverlapsWithBorder = lastPosition == data.maxY; + final delta = data.maxY - data.minY; + final count = delta ~/ horizontalInterval; + final lastPosition = count * horizontalSeek; + final lastPositionOverlapsWithBorder = lastPosition == data.maxY; final end = lastPositionOverlapsWithBorder ? data.maxY - horizontalInterval : data.maxY; while (horizontalSeek <= end) { if (data.gridData.checkToShowHorizontalLine(horizontalSeek)) { - final FlLine flLine = data.gridData.getDrawingHorizontalLine(horizontalSeek); + final flLine = data.gridData.getDrawingHorizontalLine(horizontalSeek); _gridPaint.color = flLine.color; _gridPaint.strokeWidth = flLine.strokeWidth; + _gridPaint.transparentIfWidthIsZero(); - final double bothY = getPixelY(horizontalSeek, usableViewSize); - final double x1 = 0 + getLeftOffsetDrawSize(); - final double y1 = bothY; - final double x2 = usableViewSize.width + getLeftOffsetDrawSize(); - final double y2 = bothY; + final bothY = getPixelY(horizontalSeek, usableViewSize); + final x1 = 0 + getLeftOffsetDrawSize(); + final y1 = bothY; + final x2 = usableViewSize.width + getLeftOffsetDrawSize(); + final y2 = bothY; canvasWrapper.drawDashedLine( Offset(x1, y1), Offset(x2, y2), _gridPaint, flLine.dashArray); } @@ -274,7 +277,7 @@ abstract class AxisChartPainter extends BaseChartPainte } final viewSize = canvasWrapper.size; - final Size usableViewSize = getChartUsableDrawSize(viewSize); + final usableViewSize = getChartUsableDrawSize(viewSize); _backgroundPaint.color = data.backgroundColor; canvasWrapper.drawRect( Rect.fromLTWH( @@ -293,18 +296,18 @@ abstract class AxisChartPainter extends BaseChartPainte } final viewSize = canvasWrapper.size; - final Size chartUsableSize = getChartUsableDrawSize(viewSize); + final chartUsableSize = getChartUsableDrawSize(viewSize); if (data.rangeAnnotations.verticalRangeAnnotations.isNotEmpty) { - for (VerticalRangeAnnotation annotation in data.rangeAnnotations.verticalRangeAnnotations) { - final double topChartPadding = getTopOffsetDrawSize(); - final Offset from = Offset(getPixelX(annotation.x1, chartUsableSize), topChartPadding); + for (var annotation in data.rangeAnnotations.verticalRangeAnnotations) { + final topChartPadding = getTopOffsetDrawSize(); + final from = Offset(getPixelX(annotation.x1, chartUsableSize), topChartPadding); - final double bottomChartPadding = getExtraNeededVerticalSpace() - getTopOffsetDrawSize(); - final Offset to = Offset( + final bottomChartPadding = getExtraNeededVerticalSpace() - getTopOffsetDrawSize(); + final to = Offset( getPixelX(annotation.x2, chartUsableSize), viewSize.height - bottomChartPadding); //9 - final Rect rect = Rect.fromPoints(from, to); + final rect = Rect.fromPoints(from, to); _rangeAnnotationPaint.color = annotation.color; @@ -313,16 +316,15 @@ abstract class AxisChartPainter extends BaseChartPainte } if (data.rangeAnnotations.horizontalRangeAnnotations.isNotEmpty) { - for (HorizontalRangeAnnotation annotation - in data.rangeAnnotations.horizontalRangeAnnotations) { - final double leftChartPadding = getLeftOffsetDrawSize(); - final Offset from = Offset(leftChartPadding, getPixelY(annotation.y1, chartUsableSize)); + for (var annotation in data.rangeAnnotations.horizontalRangeAnnotations) { + final leftChartPadding = getLeftOffsetDrawSize(); + final from = Offset(leftChartPadding, getPixelY(annotation.y1, chartUsableSize)); - final double rightChartPadding = getExtraNeededHorizontalSpace() - getLeftOffsetDrawSize(); - final Offset to = + final rightChartPadding = getExtraNeededHorizontalSpace() - getLeftOffsetDrawSize(); + final to = Offset(viewSize.width - rightChartPadding, getPixelY(annotation.y2, chartUsableSize)); - final Rect rect = Rect.fromPoints(from, to); + final rect = Rect.fromPoints(from, to); _rangeAnnotationPaint.color = annotation.color; @@ -335,7 +337,7 @@ abstract class AxisChartPainter extends BaseChartPainte /// 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) { - final double deltaX = data.maxX - data.minX; + final deltaX = data.maxX - data.minX; if (deltaX == 0.0) { return getLeftOffsetDrawSize(); } @@ -348,12 +350,12 @@ abstract class AxisChartPainter extends BaseChartPainte double spotY, Size chartUsableSize, ) { - final double deltaY = data.maxY - data.minY; + final deltaY = data.maxY - data.minY; if (deltaY == 0.0) { return chartUsableSize.height + getTopOffsetDrawSize(); } - double y = ((spotY - data.minY) / deltaY) * chartUsableSize.height; + var y = ((spotY - data.minY) / deltaY) * chartUsableSize.height; y = chartUsableSize.height - y; return y + getTopOffsetDrawSize(); } diff --git a/lib/src/chart/base/base_chart/base_chart_painter.dart b/lib/src/chart/base/base_chart/base_chart_painter.dart index 3de737781..58e3a6bc6 100644 --- a/lib/src/chart/base/base_chart/base_chart_painter.dart +++ b/lib/src/chart/base/base_chart/base_chart_painter.dart @@ -1,6 +1,6 @@ import 'package:fl_chart/src/utils/canvas_wrapper.dart'; import 'package:flutter/material.dart'; - +import 'package:fl_chart/src/extensions/paint_extension.dart'; import 'base_chart_data.dart'; import 'touch_input.dart'; @@ -41,34 +41,38 @@ abstract class BaseChartPainter extends CustomPainter { getTopOffsetDrawSize() + chartViewSize.height); /// Draw Top Line - final BorderSide topBorder = data.borderData.border.top; + final topBorder = data.borderData.border.top; if (topBorder.width != 0.0) { _borderPaint.color = topBorder.color; _borderPaint.strokeWidth = topBorder.width; + _borderPaint.transparentIfWidthIsZero(); canvasWrapper.drawLine(topLeft, topRight, _borderPaint); } /// Draw Right Line - final BorderSide rightBorder = data.borderData.border.right; + final rightBorder = data.borderData.border.right; if (rightBorder.width != 0.0) { _borderPaint.color = rightBorder.color; _borderPaint.strokeWidth = rightBorder.width; + _borderPaint.transparentIfWidthIsZero(); canvasWrapper.drawLine(topRight, bottomRight, _borderPaint); } /// Draw Bottom Line - final BorderSide bottomBorder = data.borderData.border.bottom; + final bottomBorder = data.borderData.border.bottom; if (bottomBorder.width != 0.0) { _borderPaint.color = bottomBorder.color; _borderPaint.strokeWidth = bottomBorder.width; + _borderPaint.transparentIfWidthIsZero(); canvasWrapper.drawLine(bottomRight, bottomLeft, _borderPaint); } /// Draw Left Line - final BorderSide leftBorder = data.borderData.border.left; + final leftBorder = data.borderData.border.left; if (leftBorder.width != 0.0) { _borderPaint.color = leftBorder.color; _borderPaint.strokeWidth = leftBorder.width; + _borderPaint.transparentIfWidthIsZero(); canvasWrapper.drawLine(bottomLeft, topLeft, _borderPaint); } } diff --git a/lib/src/chart/line_chart/line_chart.dart b/lib/src/chart/line_chart/line_chart.dart index 5076bed1d..9b132d9ff 100644 --- a/lib/src/chart/line_chart/line_chart.dart +++ b/lib/src/chart/line_chart/line_chart.dart @@ -40,103 +40,141 @@ class _LineChartState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { - final LineChartData showingData = _getData(); - final LineTouchData touchData = showingData.lineTouchData; + final showingData = _getData(); + final touchData = showingData.lineTouchData; - return GestureDetector( - onLongPressStart: (d) { - final Size chartSize = _getChartSize(); + return MouseRegion( + onEnter: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } final LineTouchResponse response = - _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) { - final Size chartSize = _getChartSize(); + onExit: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } final LineTouchResponse response = - _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressMoveUpdate: (d) { - final Size chartSize = _getChartSize(); + onHover: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } final LineTouchResponse response = - _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } + child: GestureDetector( + onLongPressStart: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } - final LineTouchResponse response = _touchHandler?.handleTouch( - FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanEnd: (DragEndDetails details) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onLongPressEnd: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } - final LineTouchResponse response = - _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanDown: (DragDownDetails details) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onLongPressMoveUpdate: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } - final LineTouchResponse response = - _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanUpdate: (DragUpdateDetails details) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanCancel: () { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } - final LineTouchResponse response = - _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - child: CustomPaint( - key: _chartKey, - size: getDefaultSize(MediaQuery.of(context).size), - painter: LineChartPainter(_withTouchedIndicators(_lineChartDataTween.evaluate(animation)), - _withTouchedIndicators(showingData), (touchHandler) { - setState(() { - _touchHandler = touchHandler; - }); - }, textScale: MediaQuery.of(context).textScaleFactor), + final LineTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanEnd: (DragEndDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanDown: (DragDownDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanUpdate: (DragUpdateDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + child: CustomPaint( + key: _chartKey, + size: getDefaultSize(MediaQuery.of(context).size), + painter: LineChartPainter(_withTouchedIndicators(_lineChartDataTween.evaluate(animation)), + _withTouchedIndicators(showingData), (touchHandler) { + setState(() { + _touchHandler = touchHandler; + }); + }, textScale: MediaQuery.of(context).textScaleFactor), + ), ), ); } @@ -197,7 +235,7 @@ class _LineChartState extends AnimatedWidgetBaseState { sortedLineSpots.sort((spot1, spot2) => spot2.y.compareTo(spot1.y)); _showingTouchedIndicators.clear(); - for (int i = 0; i < touchResponse.lineBarSpots.length; i++) { + for (var i = 0; i < touchResponse.lineBarSpots.length; i++) { final touchedBarSpot = touchResponse.lineBarSpots[i]; final barPos = touchedBarSpot.barIndex; _showingTouchedIndicators[barPos] = [touchedBarSpot.spotIndex]; diff --git a/lib/src/chart/line_chart/line_chart_data.dart b/lib/src/chart/line_chart/line_chart_data.dart index 320d41505..6591b15c5 100644 --- a/lib/src/chart/line_chart/line_chart_data.dart +++ b/lib/src/chart/line_chart/line_chart_data.dart @@ -105,8 +105,8 @@ class LineChartData extends AxisChartData with EquatableMixin { double minY, double maxY, ) { - for (int i = 0; i < lineBarsData.length; i++) { - final LineChartBarData lineBarChart = lineBarsData[i]; + for (var i = 0; i < lineBarsData.length; i++) { + final lineBarChart = lineBarsData[i]; if (lineBarChart.spots == null || lineBarChart.spots.isEmpty) { throw Exception('spots could not be null or empty'); } @@ -117,10 +117,10 @@ class LineChartData extends AxisChartData with EquatableMixin { final canModifyMinY = minY == null; final canModifyMaxY = maxY == null; - for (int i = 0; i < lineBarsData.length; i++) { - final LineChartBarData barData = lineBarsData[i]; - for (int j = 0; j < barData.spots.length; j++) { - final FlSpot spot = barData.spots[j]; + for (var i = 0; i < lineBarsData.length; i++) { + final barData = lineBarsData[i]; + for (var j = 0; j < barData.spots.length; j++) { + final spot = barData.spots[j]; if (spot.isNotNull()) { if (canModifyMaxX && (maxX == null || spot.x > maxX)) { maxX = spot.x; @@ -1440,14 +1440,14 @@ List defaultTouchedIndicators( } return indicators.map((int index) { /// Indicator Line - Color lineColor = barData.colors[0]; + var lineColor = barData.colors[0]; if (barData.dotData.show) { lineColor = _defaultGetDotColor(barData.spots[index], 0, barData); } - const double lineStrokeWidth = 4; - final FlLine flLine = FlLine(color: lineColor, strokeWidth: lineStrokeWidth); + const lineStrokeWidth = 4.0; + final flLine = FlLine(color: lineColor, strokeWidth: lineStrokeWidth); - double dotSize = 10; + var dotSize = 10.0; if (barData.dotData.show) { dotSize = 4.0 * 1.8; } @@ -1557,7 +1557,7 @@ List defaultLineTooltipItem(List touchedSpots) { if (touchedSpot == null) { return null; } - final TextStyle textStyle = TextStyle( + final textStyle = TextStyle( color: touchedSpot.bar.colors[0], fontWeight: FontWeight.bold, fontSize: 14, diff --git a/lib/src/chart/line_chart/line_chart_painter.dart b/lib/src/chart/line_chart/line_chart_painter.dart index fa2f3435f..e7dfd52f6 100644 --- a/lib/src/chart/line_chart/line_chart_painter.dart +++ b/lib/src/chart/line_chart/line_chart_painter.dart @@ -8,6 +8,7 @@ import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart'; import 'package:fl_chart/src/chart/base/base_chart/touch_input.dart'; import 'package:fl_chart/src/extensions/canvas_extension.dart'; import 'package:fl_chart/src/extensions/path_extension.dart'; +import 'package:fl_chart/src/extensions/paint_extension.dart'; import 'package:fl_chart/src/utils/canvas_wrapper.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; @@ -85,7 +86,7 @@ class LineChartPainter extends AxisChartPainter super.paint(canvas, size); - for (BetweenBarsData betweenBarsData in data.betweenBarsData) { + for (var betweenBarsData in data.betweenBarsData) { _drawBetweenBarsArea(canvasWrapper, data, betweenBarsData); } @@ -94,7 +95,7 @@ class LineChartPainter extends AxisChartPainter } /// draw each line independently on the chart - for (int i = 0; i < data.lineBarsData.length; i++) { + for (var i = 0; i < data.lineBarsData.length; i++) { final barData = data.lineBarsData[i]; if (!barData.show) { @@ -119,16 +120,16 @@ class LineChartPainter extends AxisChartPainter _drawTitles(canvasWrapper); // Draw touch tooltip on most top spot - for (int i = 0; i < data.showingTooltipIndicators.length; i++) { - ShowingTooltipIndicators tooltipSpots = data.showingTooltipIndicators[i]; + for (var i = 0; i < data.showingTooltipIndicators.length; i++) { + var tooltipSpots = data.showingTooltipIndicators[i]; - final List showingBarSpots = tooltipSpots.showingSpots; + final showingBarSpots = tooltipSpots.showingSpots; if (showingBarSpots.isEmpty) { continue; } - final List barSpots = List.of(showingBarSpots); + final barSpots = List.of(showingBarSpots); FlSpot topSpot = barSpots[0]; - for (LineBarSpot barSpot in barSpots) { + for (var barSpot in barSpots) { if (barSpot.y > topSpot.y) { topSpot = barSpot; } @@ -145,10 +146,10 @@ class LineChartPainter extends AxisChartPainter final usableSize = getChartUsableDrawSize(size); final border = data.borderData.show ? data.borderData.border : null; - double left = 0.0; - double top = 0.0; - double right = size.width; - double bottom = size.height; + var left = 0.0; + var top = 0.0; + var right = size.width; + var bottom = size.height; if (clip.left) { final borderWidth = border?.left?.width ?? 0; @@ -172,7 +173,7 @@ class LineChartPainter extends AxisChartPainter void _drawBarLine(CanvasWrapper canvasWrapper, LineChartBarData barData) { final viewSize = canvasWrapper.size; - final List> barList = [[]]; + final barList = >[[]]; // handle nullability by splitting off the list into multiple // separate lists when separated by nulls @@ -192,7 +193,7 @@ class LineChartPainter extends AxisChartPainter // bar is passed in separately from barData // because barData is the whole line // and bar is a piece of that line - for (List bar in barList) { + for (var bar in barList) { final barPath = _generateBarPath(viewSize, barData, bar); final belowBarPath = _generateBelowBarPath(viewSize, barData, barPath, bar); @@ -212,10 +213,10 @@ class LineChartPainter extends AxisChartPainter void _drawBetweenBarsArea( CanvasWrapper canvasWrapper, LineChartData data, BetweenBarsData betweenBarsData) { final viewSize = canvasWrapper.size; - final LineChartBarData fromBarData = data.lineBarsData[betweenBarsData.fromIndex]; - final LineChartBarData toBarData = data.lineBarsData[betweenBarsData.toIndex]; + final fromBarData = data.lineBarsData[betweenBarsData.fromIndex]; + final toBarData = data.lineBarsData[betweenBarsData.toIndex]; - final List spots = []; + final spots = []; spots.addAll(toBarData.spots.reversed.toList()); final fromBarPath = _generateBarPath( viewSize, @@ -240,16 +241,15 @@ class LineChartPainter extends AxisChartPainter final barXDelta = _getBarLineXLength(barData, viewSize); - for (int i = 0; i < barData.spots.length; i++) { - final FlSpot spot = barData.spots[i]; + for (var i = 0; i < barData.spots.length; i++) { + final spot = barData.spots[i]; if (spot.isNotNull() && barData.dotData.checkToShowDot(spot, barData)) { - final double x = getPixelX(spot.x, viewSize); - final double y = getPixelY(spot.y, viewSize); + final x = getPixelX(spot.x, viewSize); + final y = getPixelY(spot.y, viewSize); - final double xPercentInLine = ((x - getLeftOffsetDrawSize()) / barXDelta) * 100; + final xPercentInLine = ((x - getLeftOffsetDrawSize()) / barXDelta) * 100; - final FlDotPainter painter = - barData.dotData.getDotPainter(spot, xPercentInLine, barData, i); + final painter = barData.dotData.getDotPainter(spot, xPercentInLine, barData, i); canvasWrapper.drawDot(painter, spot, Offset(x, y)); } @@ -264,34 +264,33 @@ class LineChartPainter extends AxisChartPainter // Todo technical debt, we can read the TouchedSpotIndicatorData directly, // Todo instead of mapping indexes to TouchedSpotIndicatorData - final List indicatorsData = + final indicatorsData = data.lineTouchData.getTouchedSpotIndicator(barData, barData.showingIndicators); if (indicatorsData.length != barData.showingIndicators.length) { throw Exception('indicatorsData and touchedSpotOffsets size should be same'); } - for (int i = 0; i < barData.showingIndicators.length; i++) { - final TouchedSpotIndicatorData indicatorData = indicatorsData[i]; - final int index = barData.showingIndicators[i]; - final FlSpot spot = barData.spots[index]; + for (var i = 0; i < barData.showingIndicators.length; i++) { + final indicatorData = indicatorsData[i]; + final index = barData.showingIndicators[i]; + final spot = barData.spots[index]; if (indicatorData == null) { continue; } - final Offset touchedSpot = + final touchedSpot = Offset(getPixelX(spot.x, chartViewSize), getPixelY(spot.y, chartViewSize)); /// For drawing the dot - final bool showingDots = + final showingDots = indicatorData.touchedSpotDotData != null && indicatorData.touchedSpotDotData.show; - double dotHeight = 0; + var dotHeight = 0.0; FlDotPainter dotPainter; if (showingDots) { - final double xPercentInLine = - ((touchedSpot.dx - getLeftOffsetDrawSize()) / barXDelta) * 100; + final xPercentInLine = ((touchedSpot.dx - getLeftOffsetDrawSize()) / barXDelta) * 100; dotPainter = indicatorData.touchedSpotDotData.getDotPainter(spot, xPercentInLine, barData, index); dotHeight = dotPainter.getSize(spot).height; @@ -302,11 +301,12 @@ class LineChartPainter extends AxisChartPainter final top = Offset(getPixelX(spot.x, chartViewSize), getTopOffsetDrawSize()); /// Draw to top or to the touchedSpot - final Offset lineEnd = + final lineEnd = data.lineTouchData.fullHeightTouchLine ? top : touchedSpot + Offset(0, dotHeight / 2); _touchLinePaint.color = indicatorData.indicatorBelowLine.color; _touchLinePaint.strokeWidth = indicatorData.indicatorBelowLine.strokeWidth; + _touchLinePaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine( bottom, lineEnd, _touchLinePaint, indicatorData.indicatorBelowLine.dashArray); @@ -339,19 +339,19 @@ class LineChartPainter extends AxisChartPainter Path _generateNormalBarPath(Size viewSize, LineChartBarData barData, List barSpots, {Path appendToPath}) { viewSize = getChartUsableDrawSize(viewSize); - final Path path = appendToPath ?? Path(); - final int size = barSpots.length; + final path = appendToPath ?? Path(); + final size = barSpots.length; var temp = const Offset(0.0, 0.0); - final double x = getPixelX(barSpots[0].x, viewSize); - final double y = getPixelY(barSpots[0].y, viewSize); + final x = getPixelX(barSpots[0].x, viewSize); + final y = getPixelY(barSpots[0].y, viewSize); if (appendToPath == null) { path.moveTo(x, y); } else { path.lineTo(x, y); } - for (int i = 1; i < size; i++) { + for (var i = 1; i < size; i++) { /// CurrentSpot final current = Offset( getPixelX(barSpots[i].x, viewSize), @@ -409,17 +409,17 @@ class LineChartPainter extends AxisChartPainter Path _generateStepBarPath(Size viewSize, LineChartBarData barData, List barSpots, {Path appendToPath}) { viewSize = getChartUsableDrawSize(viewSize); - final Path path = appendToPath ?? Path(); - final int size = barSpots.length; + final path = appendToPath ?? Path(); + final size = barSpots.length; - final double x = getPixelX(barSpots[0].x, viewSize); - final double y = getPixelY(barSpots[0].y, viewSize); + final x = getPixelX(barSpots[0].x, viewSize); + final y = getPixelY(barSpots[0].y, viewSize); if (appendToPath == null) { path.moveTo(x, y); } else { path.lineTo(x, y); } - for (int i = 0; i < size; i++) { + for (var i = 0; i < size; i++) { /// CurrentSpot final current = Offset( getPixelX(barSpots[i].x, viewSize), @@ -461,7 +461,7 @@ class LineChartPainter extends AxisChartPainter final chartViewSize = getChartUsableDrawSize(viewSize); /// Line To Bottom Right - double x = getPixelX(barSpots[barSpots.length - 1].x, chartViewSize); + var x = getPixelX(barSpots[barSpots.length - 1].x, chartViewSize); double y; if (!fillCompletely && barData.belowBarData.applyCutOffY) { y = getPixelY(barData.belowBarData.cutOffY, chartViewSize); @@ -500,7 +500,7 @@ class LineChartPainter extends AxisChartPainter final chartViewSize = getChartUsableDrawSize(viewSize); /// Line To Top Right - double x = getPixelX(barSpots[barSpots.length - 1].x, chartViewSize); + var x = getPixelX(barSpots[barSpots.length - 1].x, chartViewSize); double y; if (!fillCompletely && barData.aboveBarData.applyCutOffY) { y = getPixelY(barData.aboveBarData.cutOffY, chartViewSize); @@ -545,7 +545,7 @@ class LineChartPainter extends AxisChartPainter _barAreaPaint.color = barData.belowBarData.colors[0]; _barAreaPaint.shader = null; } else { - List stops = []; + var stops = []; if (barData.belowBarData.gradientColorStops == null || barData.belowBarData.gradientColorStops.length != barData.belowBarData.colors.length) { /// provided gradientColorStops is invalid and we calculate it here @@ -587,14 +587,14 @@ class LineChartPainter extends AxisChartPainter /// draw below spots line if (barData.belowBarData.spotsLine != null && barData.belowBarData.spotsLine.show) { - for (FlSpot spot in barData.spots) { + for (var spot in barData.spots) { if (barData.belowBarData.spotsLine.checkToShowSpotLine(spot)) { - final Offset from = Offset( + final from = Offset( getPixelX(spot.x, chartViewSize), getPixelY(spot.y, chartViewSize), ); - final double bottomPadding = getExtraNeededVerticalSpace() - getTopOffsetDrawSize(); + final bottomPadding = getExtraNeededVerticalSpace() - getTopOffsetDrawSize(); Offset to; // Check applyCutOffY @@ -612,6 +612,7 @@ class LineChartPainter extends AxisChartPainter _barAreaLinesPaint.color = barData.belowBarData.spotsLine.flLineStyle.color; _barAreaLinesPaint.strokeWidth = barData.belowBarData.spotsLine.flLineStyle.strokeWidth; + _barAreaLinesPaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine( from, to, _barAreaLinesPaint, barData.belowBarData.spotsLine.flLineStyle.dashArray); @@ -638,7 +639,7 @@ class LineChartPainter extends AxisChartPainter _barAreaPaint.color = barData.aboveBarData.colors[0]; _barAreaPaint.shader = null; } else { - List stops = []; + var stops = []; if (barData.aboveBarData.gradientColorStops == null || barData.aboveBarData.gradientColorStops.length != barData.aboveBarData.colors.length) { /// provided gradientColorStops is invalid and we calculate it here @@ -680,9 +681,9 @@ class LineChartPainter extends AxisChartPainter /// draw above spots line if (barData.aboveBarData.spotsLine != null && barData.aboveBarData.spotsLine.show) { - for (FlSpot spot in barData.spots) { + for (var spot in barData.spots) { if (barData.aboveBarData.spotsLine.checkToShowSpotLine(spot)) { - final Offset from = Offset( + final from = Offset( getPixelX(spot.x, chartViewSize), getPixelY(spot.y, chartViewSize), ); @@ -704,6 +705,7 @@ class LineChartPainter extends AxisChartPainter _barAreaLinesPaint.color = barData.aboveBarData.spotsLine.flLineStyle.color; _barAreaLinesPaint.strokeWidth = barData.aboveBarData.spotsLine.flLineStyle.strokeWidth; + _barAreaLinesPaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine( from, to, _barAreaLinesPaint, barData.aboveBarData.spotsLine.flLineStyle.dashArray); @@ -723,7 +725,7 @@ class LineChartPainter extends AxisChartPainter _barAreaPaint.color = betweenBarsData.colors[0]; _barAreaPaint.shader = null; } else { - List stops = []; + var stops = []; if (betweenBarsData.gradientColorStops == null || betweenBarsData.gradientColorStops.length != betweenBarsData.colors.length) { /// provided gradientColorStops is invalid and we calculate it here @@ -804,11 +806,11 @@ class LineChartPainter extends AxisChartPainter _barPaint.color = barData.colors[0]; _barPaint.shader = null; } else { - List stops = []; + var stops = []; if (barData.colorStops == null || barData.colorStops.length != barData.colors.length) { /// provided colorStops is invalid and we calculate it here barData.colors.asMap().forEach((index, color) { - final double percent = 1.0 / barData.colors.length; + final percent = 1.0 / barData.colors.length; stops.add(percent * index); }); } else { @@ -834,6 +836,8 @@ class LineChartPainter extends AxisChartPainter _barPaint.maskFilter = null; _barPaint.strokeWidth = barData.barWidth; + _barPaint.transparentIfWidthIsZero(); + barPath = barPath.toDashedPath(barData.dashArray); canvasWrapper.drawPath(barPath, _barPaint); } @@ -849,17 +853,17 @@ class LineChartPainter extends AxisChartPainter final leftInterval = leftTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); if (leftTitles.showTitles) { - double verticalSeek = data.minY; + var verticalSeek = data.minY; while (verticalSeek <= data.maxY) { if (leftTitles.checkToShowTitle( data.minY, data.maxY, leftTitles, leftInterval, verticalSeek)) { - double x = 0 + getLeftOffsetDrawSize(); - double y = getPixelY(verticalSeek, viewSize); + var x = 0 + getLeftOffsetDrawSize(); + var y = getPixelY(verticalSeek, viewSize); - final String text = leftTitles.getTitles(verticalSeek); + final text = leftTitles.getTitles(verticalSeek); - final TextSpan span = TextSpan(style: leftTitles.getTextStyles(verticalSeek), text: text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: leftTitles.getTextStyles(verticalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -888,18 +892,17 @@ class LineChartPainter extends AxisChartPainter final topInterval = topTitles.interval ?? getEfficientInterval(viewSize.width, data.horizontalDiff); if (topTitles.showTitles) { - double horizontalSeek = data.minX; + var horizontalSeek = data.minX; while (horizontalSeek <= data.maxX) { if (topTitles.checkToShowTitle( data.minX, data.maxX, topTitles, topInterval, horizontalSeek)) { - double x = getPixelX(horizontalSeek, viewSize); - double y = getTopOffsetDrawSize(); + var x = getPixelX(horizontalSeek, viewSize); + var y = getTopOffsetDrawSize(); - final String text = topTitles.getTitles(horizontalSeek); + final text = topTitles.getTitles(horizontalSeek); - final TextSpan span = - TextSpan(style: topTitles.getTextStyles(horizontalSeek), text: text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: topTitles.getTextStyles(horizontalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -929,18 +932,17 @@ class LineChartPainter extends AxisChartPainter final rightInterval = rightTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); if (rightTitles.showTitles) { - double verticalSeek = data.minY; + var verticalSeek = data.minY; while (verticalSeek <= data.maxY) { if (rightTitles.checkToShowTitle( data.minY, data.maxY, rightTitles, rightInterval, verticalSeek)) { - double x = viewSize.width + getLeftOffsetDrawSize(); - double y = getPixelY(verticalSeek, viewSize); + var x = viewSize.width + getLeftOffsetDrawSize(); + var y = getPixelY(verticalSeek, viewSize); - final String text = rightTitles.getTitles(verticalSeek); + final text = rightTitles.getTitles(verticalSeek); - final TextSpan span = - TextSpan(style: rightTitles.getTextStyles(verticalSeek), text: text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: rightTitles.getTextStyles(verticalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -971,16 +973,15 @@ class LineChartPainter extends AxisChartPainter final bottomInterval = bottomTitles.interval ?? getEfficientInterval(viewSize.width, data.horizontalDiff); if (bottomTitles.showTitles) { - double horizontalSeek = data.minX; + var horizontalSeek = data.minX; while (horizontalSeek <= data.maxX) { if (bottomTitles.checkToShowTitle( data.minX, data.maxX, bottomTitles, bottomInterval, horizontalSeek)) { - double x = getPixelX(horizontalSeek, viewSize); - double y = viewSize.height + getTopOffsetDrawSize(); - final String text = bottomTitles.getTitles(horizontalSeek); - final TextSpan span = - TextSpan(style: bottomTitles.getTextStyles(horizontalSeek), text: text); - final TextPainter tp = TextPainter( + var x = getPixelX(horizontalSeek, viewSize); + var y = viewSize.height + getTopOffsetDrawSize(); + final text = bottomTitles.getTitles(horizontalSeek); + final span = TextSpan(style: bottomTitles.getTextStyles(horizontalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -1013,27 +1014,27 @@ class LineChartPainter extends AxisChartPainter } final viewSize = canvasWrapper.size; - final Size chartUsableSize = getChartUsableDrawSize(viewSize); + final chartUsableSize = getChartUsableDrawSize(viewSize); if (data.extraLinesData.horizontalLines.isNotEmpty) { - for (HorizontalLine line in data.extraLinesData.horizontalLines) { - final double leftChartPadding = getLeftOffsetDrawSize(); - final Offset from = Offset(leftChartPadding, getPixelY(line.y, chartUsableSize)); + for (var line in data.extraLinesData.horizontalLines) { + final leftChartPadding = getLeftOffsetDrawSize(); + final from = Offset(leftChartPadding, getPixelY(line.y, chartUsableSize)); - final double rightChartPadding = getExtraNeededHorizontalSpace() - getLeftOffsetDrawSize(); - final Offset to = - Offset(viewSize.width - rightChartPadding, getPixelY(line.y, chartUsableSize)); + final rightChartPadding = getExtraNeededHorizontalSpace() - getLeftOffsetDrawSize(); + final to = Offset(viewSize.width - rightChartPadding, getPixelY(line.y, chartUsableSize)); _extraLinesPaint.color = line.color; _extraLinesPaint.strokeWidth = line.strokeWidth; + _extraLinesPaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine(from, to, _extraLinesPaint, line.dashArray); if (line.sizedPicture != null) { - final double centerX = line.sizedPicture.width / 2; - final double centerY = line.sizedPicture.height / 2; - final double xPosition = leftChartPadding - centerX; - final double yPosition = to.dy - centerY; + final centerX = line.sizedPicture.width / 2; + final centerY = line.sizedPicture.height / 2; + final xPosition = leftChartPadding - centerX; + final yPosition = to.dy - centerY; canvasWrapper.save(); canvasWrapper.translate(xPosition, yPosition); @@ -1042,23 +1043,23 @@ class LineChartPainter extends AxisChartPainter } if (line.image != null) { - final double centerX = line.image.width / 2; - final double centerY = line.image.height / 2; - final Offset centeredImageOffset = Offset(leftChartPadding - centerX, to.dy - centerY); + final centerX = line.image.width / 2; + final centerY = line.image.height / 2; + final centeredImageOffset = Offset(leftChartPadding - centerX, to.dy - centerY); canvasWrapper.drawImage(line.image, centeredImageOffset, _imagePaint); } if (line.label != null && line.label.show) { - final HorizontalLineLabel label = line.label; - final TextStyle style = TextStyle(fontSize: 11, color: line.color).merge(label.style); + final label = line.label; + final style = TextStyle(fontSize: 11, color: line.color).merge(label.style); final EdgeInsets padding = label.padding ?? EdgeInsets.zero; - final TextSpan span = TextSpan( + final span = TextSpan( text: label.labelResolver(line), style: style, ); - final TextPainter tp = TextPainter( + final tp = TextPainter( text: span, textDirection: TextDirection.ltr, ); @@ -1079,24 +1080,24 @@ class LineChartPainter extends AxisChartPainter } if (data.extraLinesData.verticalLines.isNotEmpty) { - for (VerticalLine line in data.extraLinesData.verticalLines) { - final double topChartPadding = getTopOffsetDrawSize(); - final Offset from = Offset(getPixelX(line.x, chartUsableSize), topChartPadding); + for (var line in data.extraLinesData.verticalLines) { + final topChartPadding = getTopOffsetDrawSize(); + final from = Offset(getPixelX(line.x, chartUsableSize), topChartPadding); - final double bottomChartPadding = getExtraNeededVerticalSpace() - getTopOffsetDrawSize(); - final Offset to = - Offset(getPixelX(line.x, chartUsableSize), viewSize.height - bottomChartPadding); + final bottomChartPadding = getExtraNeededVerticalSpace() - getTopOffsetDrawSize(); + final to = Offset(getPixelX(line.x, chartUsableSize), viewSize.height - bottomChartPadding); _extraLinesPaint.color = line.color; _extraLinesPaint.strokeWidth = line.strokeWidth; + _extraLinesPaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine(from, to, _extraLinesPaint, line.dashArray); if (line.sizedPicture != null) { - final double centerX = line.sizedPicture.width / 2; - final double centerY = line.sizedPicture.height / 2; - final double xPosition = to.dx - centerX; - final double yPosition = viewSize.height - bottomChartPadding - centerY; + final centerX = line.sizedPicture.width / 2; + final centerY = line.sizedPicture.height / 2; + final xPosition = to.dx - centerX; + final yPosition = viewSize.height - bottomChartPadding - centerY; canvasWrapper.save(); canvasWrapper.translate(xPosition, yPosition); @@ -1104,24 +1105,24 @@ class LineChartPainter extends AxisChartPainter canvasWrapper.restore(); } if (line.image != null) { - final double centerX = line.image.width / 2; - final double centerY = line.image.height / 2; - final Offset centeredImageOffset = + final centerX = line.image.width / 2; + final centerY = line.image.height / 2; + final centeredImageOffset = Offset(to.dx - centerX, viewSize.height - bottomChartPadding - centerY); canvasWrapper.drawImage(line.image, centeredImageOffset, _imagePaint); } if (line.label != null && line.label.show) { - final VerticalLineLabel label = line.label; - final TextStyle style = TextStyle(fontSize: 11, color: line.color).merge(label.style); + final label = line.label; + final style = TextStyle(fontSize: 11, color: line.color).merge(label.style); final EdgeInsets padding = label.padding ?? EdgeInsets.zero; - final TextSpan span = TextSpan( + final span = TextSpan( text: label.labelResolver(line), style: style, ); - final TextPainter tp = TextPainter( + final tp = TextPainter( text: span, textDirection: TextDirection.ltr, ); @@ -1149,25 +1150,24 @@ class LineChartPainter extends AxisChartPainter final viewSize = canvasWrapper.size; final chartUsableSize = getChartUsableDrawSize(viewSize); - const double textsBelowMargin = 4; + const textsBelowMargin = 4; /// creating TextPainters to calculate the width and height of the tooltip - final List drawingTextPainters = []; + final drawingTextPainters = []; - final List tooltipItems = - tooltipData.getTooltipItems(showingTooltipSpots.showingSpots); + final tooltipItems = tooltipData.getTooltipItems(showingTooltipSpots.showingSpots); if (tooltipItems.length != showingTooltipSpots.showingSpots.length) { throw Exception('tooltipItems and touchedSpots size should be same'); } - for (int i = 0; i < showingTooltipSpots.showingSpots.length; i++) { - final LineTooltipItem tooltipItem = tooltipItems[i]; + for (var i = 0; i < showingTooltipSpots.showingSpots.length; i++) { + final tooltipItem = tooltipItems[i]; if (tooltipItem == null) { continue; } - final TextSpan span = TextSpan(style: tooltipItem.textStyle, text: tooltipItem.text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: tooltipItem.textStyle, text: tooltipItem.text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -1186,9 +1186,9 @@ class LineChartPainter extends AxisChartPainter /// sumTextsHeight /// sum up all Texts height, then we should /// draw the tooltip's height as tall as sumTextsHeight - double biggerWidth = 0; - double sumTextsHeight = 0; - for (TextPainter tp in drawingTextPainters) { + var biggerWidth = 0.0; + var sumTextsHeight = 0.0; + for (var tp in drawingTextPainters) { if (tp.width > biggerWidth) { biggerWidth = tp.width; } @@ -1199,13 +1199,13 @@ class LineChartPainter extends AxisChartPainter /// if we have multiple bar lines, /// there are more than one FlCandidate on touch area, /// we should get the most top FlSpot Offset to draw the tooltip on top of it - final Offset mostTopOffset = Offset( + final mostTopOffset = Offset( getPixelX(showOnSpot.x, chartUsableSize), getPixelY(showOnSpot.y, chartUsableSize), ); - final double tooltipWidth = biggerWidth + tooltipData.tooltipPadding.horizontal; - final double tooltipHeight = sumTextsHeight + tooltipData.tooltipPadding.vertical; + final tooltipWidth = biggerWidth + tooltipData.tooltipPadding.horizontal; + final tooltipHeight = sumTextsHeight + tooltipData.tooltipPadding.vertical; double tooltipTopPosition; if (tooltipData.showOnTopOfTheChartBoxArea) { @@ -1215,7 +1215,7 @@ class LineChartPainter extends AxisChartPainter } /// draw the background rect with rounded radius - Rect rect = Rect.fromLTWH( + var rect = Rect.fromLTWH( mostTopOffset.dx - (tooltipWidth / 2), tooltipTopPosition, tooltipWidth, @@ -1266,15 +1266,15 @@ class LineChartPainter extends AxisChartPainter } } - final Radius radius = Radius.circular(tooltipData.tooltipRoundedRadius); - final RRect roundedRect = RRect.fromRectAndCorners(rect, + final radius = Radius.circular(tooltipData.tooltipRoundedRadius); + final roundedRect = RRect.fromRectAndCorners(rect, topLeft: radius, topRight: radius, bottomLeft: radius, bottomRight: radius); _bgTouchTooltipPaint.color = tooltipData.tooltipBgColor; canvasWrapper.drawRRect(roundedRect, _bgTouchTooltipPaint); /// draw the texts one by one in below of each other - double topPosSeek = tooltipData.tooltipPadding.top; - for (TextPainter tp in drawingTextPainters) { + var topPosSeek = tooltipData.tooltipPadding.top; + for (var tp in drawingTextPainters) { final drawOffset = Offset( rect.center.dx - (tp.width / 2), rect.topCenter.dy + topPosSeek, @@ -1306,7 +1306,7 @@ class LineChartPainter extends AxisChartPainter /// and the whole space is [getExtraNeededHorizontalSpace] @override double getExtraNeededHorizontalSpace() { - double sum = super.getExtraNeededHorizontalSpace(); + var sum = super.getExtraNeededHorizontalSpace(); if (data.titlesData.show) { final leftSide = data.titlesData.leftTitles; if (leftSide.showTitles) { @@ -1328,7 +1328,7 @@ class LineChartPainter extends AxisChartPainter /// and the whole space is [getExtraNeededVerticalSpace] @override double getExtraNeededVerticalSpace() { - double sum = super.getExtraNeededVerticalSpace(); + var sum = super.getExtraNeededVerticalSpace(); if (data.titlesData.show) { final topSide = data.titlesData.topTitles; if (topSide.showTitles) { @@ -1381,15 +1381,14 @@ class LineChartPainter extends AxisChartPainter LineTouchResponse handleTouch(FlTouchInput touchInput, Size size) { /// it holds list of nearest touched spots of each line /// and we use it to draw touch stuff on them - final List touchedSpots = []; + final touchedSpots = []; /// draw each line independently on the chart - for (int i = 0; i < data.lineBarsData.length; i++) { + for (var i = 0; i < data.lineBarsData.length; i++) { final barData = data.lineBarsData[i]; // find the nearest spot on touch area in this bar line - final LineBarSpot foundTouchedSpot = - _getNearestTouchedSpot(size, touchInput.getOffset(), barData, i); + final foundTouchedSpot = _getNearestTouchedSpot(size, touchInput.getOffset(), barData, i); if (foundTouchedSpot != null) { touchedSpots.add(foundTouchedSpot); } @@ -1405,10 +1404,10 @@ class LineChartPainter extends AxisChartPainter return null; } - final Size chartViewSize = getChartUsableDrawSize(viewSize); + final chartViewSize = getChartUsableDrawSize(viewSize); /// Find the nearest spot (on X axis) - for (int i = 0; i < barData.spots.length; i++) { + for (var i = 0; i < barData.spots.length; i++) { final spot = barData.spots[i]; if (spot.isNotNull()) { if ((touchedPoint.dx - getPixelX(spot.x, chartViewSize)).abs() <= diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index d92812c0d..0c495e16f 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -1,9 +1,8 @@ -import 'package:flutter/material.dart'; - import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart'; import 'package:fl_chart/src/chart/base/base_chart/touch_input.dart'; import 'package:fl_chart/src/chart/pie_chart/pie_chart_painter.dart'; import 'package:fl_chart/src/utils/utils.dart'; +import 'package:flutter/material.dart'; import 'pie_chart_data.dart'; @@ -57,47 +56,24 @@ class _PieChartState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { - final PieChartData showingData = _getData(); - final PieTouchData touchData = showingData.pieTouchData; - - return GestureDetector( - onLongPressStart: (d) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onLongPressEnd: (d) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } + final showingData = _getData(); + final touchData = showingData.pieTouchData; - final PieTouchResponse response = - _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onLongPressMoveUpdate: (d) { - final Size chartSize = _getChartSize(); + return MouseRegion( + onEnter: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } final PieTouchResponse response = - _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () { - final Size chartSize = _getChartSize(); + onExit: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -108,68 +84,133 @@ class _PieChartState extends AnimatedWidgetBaseState { touchData.touchCallback(response); } }, - onPanEnd: (DragEndDetails details) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } - - final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanDown: (DragDownDetails details) { - final Size chartSize = _getChartSize(); - if (chartSize == null) { - return; - } - - final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanUpdate: (DragUpdateDetails details) { - final Size chartSize = _getChartSize(); + onHover: (e) { + final chartSize = _getChartSize(); if (chartSize == null) { return; } final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - child: CustomPaint( - key: _chartKey, - size: getDefaultSize(MediaQuery.of(context).size), - painter: PieChartPainter( - _pieChartDataTween.evaluate(animation), - showingData, - (touchHandler) { - setState(() { - _touchHandler = touchHandler; - }); - }, - textScale: MediaQuery.of(context).textScaleFactor, - widgetsPositionHandler: (widgetPositionHandler) { - setState(() { - _widgetsPositionHandler = widgetPositionHandler; - }); - }, + child: GestureDetector( + onLongPressStart: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onLongPressEnd: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onLongPressMoveUpdate: (d) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanCancel: () { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanEnd: (DragEndDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanDown: (DragDownDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanUpdate: (DragUpdateDetails details) { + final chartSize = _getChartSize(); + if (chartSize == null) { + return; + } + + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + child: CustomPaint( + key: _chartKey, + size: getDefaultSize(MediaQuery.of(context).size), + painter: PieChartPainter( + _pieChartDataTween.evaluate(animation), + showingData, + (touchHandler) { + setState(() { + _touchHandler = touchHandler; + }); + }, + textScale: MediaQuery.of(context).textScaleFactor, + widgetsPositionHandler: (widgetPositionHandler) { + setState(() { + _widgetsPositionHandler = widgetPositionHandler; + }); + }, + ), + child: LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + return badgeWidgets(constraints); + }, + ), ), - child: badgeWidgets(), ), ); } - Widget badgeWidgets() { - final chartSize = _getChartSize(); - if (chartSize != null && _widgetsPositionHandler != null) { + Widget badgeWidgets(BoxConstraints constraints) { + final chartSize = constraints.biggest; + if (_widgetsPositionHandler != null) { final offsetsMap = _widgetsPositionHandler.getBadgeOffsets(chartSize); if (offsetsMap.isNotEmpty) { return CustomMultiChildLayout( @@ -180,7 +221,7 @@ class _PieChartState extends AnimatedWidgetBaseState { children: List.generate( offsetsMap.length, (index) { - final int _key = offsetsMap.keys.elementAt(index); + final _key = offsetsMap.keys.elementAt(index); if (offsetsMap.length != _getData().sections.length) { return LayoutId( @@ -189,7 +230,7 @@ class _PieChartState extends AnimatedWidgetBaseState { ); } - final Widget _badgeWidget = _getData().sections[_key].badgeWidget; + final _badgeWidget = _getData().sections[_key].badgeWidget; if (_badgeWidget == null) { return LayoutId( @@ -208,7 +249,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } } - return null; + return SizedBox(); } bool _canHandleTouch(PieTouchResponse response, PieTouchData touchData) { @@ -251,10 +292,10 @@ class BadgeWidgetsDelegate extends MultiChildLayoutDelegate { @override void performLayout(Size size) { - for (int index = 0; index < badgeWidgetsCount; index++) { - final int _key = badgeWidgetsOffsets.keys.elementAt(index); + for (var index = 0; index < badgeWidgetsCount; index++) { + final _key = badgeWidgetsOffsets.keys.elementAt(index); - final Size _size = layoutChild( + final _size = layoutChild( _key, BoxConstraints( maxWidth: size.width, diff --git a/lib/src/chart/pie_chart/pie_chart_painter.dart b/lib/src/chart/pie_chart/pie_chart_painter.dart index 3fa48e2d2..5ed18c618 100644 --- a/lib/src/chart/pie_chart/pie_chart_painter.dart +++ b/lib/src/chart/pie_chart/pie_chart_painter.dart @@ -60,7 +60,7 @@ class PieChartPainter extends BaseChartPainter final canvasWrapper = CanvasWrapper(canvas, size); - final List sectionsAngle = _calculateSectionsAngle(data.sections, data.sumValue); + final sectionsAngle = _calculateSectionsAngle(data.sections, data.sumValue); _drawCenterSpace(canvasWrapper); _drawSections(canvasWrapper, sectionsAngle); @@ -75,8 +75,8 @@ class PieChartPainter extends BaseChartPainter void _drawCenterSpace(CanvasWrapper canvasWrapper) { final viewSize = canvasWrapper.size; - final double centerX = viewSize.width / 2; - final double centerY = viewSize.height / 2; + final centerX = viewSize.width / 2; + final centerY = viewSize.height / 2; canvasWrapper.drawCircle(Offset(centerX, centerY), data.centerSpaceRadius, _centerSpacePaint); } @@ -90,11 +90,11 @@ class PieChartPainter extends BaseChartPainter canvasWrapper.saveLayer(Rect.fromLTWH(0, 0, viewSize.width, viewSize.height), Paint()); } - final Offset center = Offset(viewSize.width / 2, viewSize.height / 2); + final center = Offset(viewSize.width / 2, viewSize.height / 2); - double tempAngle = data.startDegreeOffset; + var tempAngle = data.startDegreeOffset; - for (int i = 0; i < data.sections.length; i++) { + for (var i = 0; i < data.sections.length; i++) { final section = data.sections[i]; final sectionDegree = sectionsAngle[i]; @@ -106,8 +106,8 @@ class PieChartPainter extends BaseChartPainter _sectionPaint.color = section.color; _sectionPaint.strokeWidth = section.radius; - final double startAngle = tempAngle; - final double sweepAngle = sectionDegree; + final startAngle = tempAngle; + final sweepAngle = sectionDegree; canvasWrapper.drawArc( rect, radians(startAngle), @@ -128,20 +128,20 @@ class PieChartPainter extends BaseChartPainter /// then here we clear a line with given [PieChartData.width] void _removeSectionsSpace(CanvasWrapper canvasWrapper) { final viewSize = canvasWrapper.size; - const double extraLineSize = 1; - final Offset center = Offset(viewSize.width / 2, viewSize.height / 2); + const extraLineSize = 1; + final center = Offset(viewSize.width / 2, viewSize.height / 2); - double tempAngle = data.startDegreeOffset; + var tempAngle = data.startDegreeOffset; data.sections.asMap().forEach((index, section) { - final int previousIndex = index == 0 ? data.sections.length - 1 : index - 1; + final previousIndex = index == 0 ? data.sections.length - 1 : index - 1; final previousSection = data.sections[previousIndex]; - final double maxSectionRadius = math.max(section.radius, previousSection.radius); + final maxSectionRadius = math.max(section.radius, previousSection.radius); - final double startAngle = tempAngle; - final double sweepAngle = 360 * (section.value / data.sumValue); + final startAngle = tempAngle; + final sweepAngle = 360 * (section.value / data.sumValue); - final Offset sectionsStartFrom = center + + final sectionsStartFrom = center + Offset( math.cos(radians(startAngle)) * (_calculateCenterRadius(viewSize, data.centerSpaceRadius) - extraLineSize), @@ -149,7 +149,7 @@ class PieChartPainter extends BaseChartPainter (_calculateCenterRadius(viewSize, data.centerSpaceRadius) - extraLineSize), ); - final Offset sectionsStartTo = center + + final sectionsStartTo = center + Offset( math.cos(radians(startAngle)) * (_calculateCenterRadius(viewSize, data.centerSpaceRadius) + @@ -173,15 +173,15 @@ class PieChartPainter extends BaseChartPainter /// - badge widget positions void _drawTexts(CanvasWrapper canvasWrapper) { final viewSize = canvasWrapper.size; - final Offset center = Offset(viewSize.width / 2, viewSize.height / 2); + final center = Offset(viewSize.width / 2, viewSize.height / 2); - double tempAngle = data.startDegreeOffset; + var tempAngle = data.startDegreeOffset; - for (int i = 0; i < data.sections.length; i++) { - final PieChartSectionData section = data.sections[i]; - final double startAngle = tempAngle; - final double sweepAngle = 360 * (section.value / data.sumValue); - final double sectionCenterAngle = startAngle + (sweepAngle / 2); + for (var i = 0; i < data.sections.length; i++) { + final section = data.sections[i]; + final startAngle = tempAngle; + final sweepAngle = 360 * (section.value / data.sumValue); + final sectionCenterAngle = startAngle + (sweepAngle / 2); Offset sectionCenter(double percentageOffset) => center + @@ -194,14 +194,14 @@ class PieChartPainter extends BaseChartPainter (section.radius * percentageOffset)), ); - final Offset sectionCenterOffsetTitle = sectionCenter(section.titlePositionPercentageOffset); + final sectionCenterOffsetTitle = sectionCenter(section.titlePositionPercentageOffset); if (section.showTitle) { - final TextSpan span = TextSpan( + final span = TextSpan( style: section.titleStyle, text: section.title, ); - final TextPainter tp = TextPainter( + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -220,8 +220,8 @@ class PieChartPainter extends BaseChartPainter return givenCenterRadius; } - double maxRadius = 0; - for (int i = 0; i < data.sections.length; i++) { + var maxRadius = 0.0; + for (var i = 0; i < data.sections.length; i++) { final section = data.sections[i]; if (section.radius > maxRadius) { maxRadius = section.radius; @@ -240,7 +240,7 @@ class PieChartPainter extends BaseChartPainter /// then makes a [PieTouchResponse] from the elements that has been touched. @override PieTouchResponse handleTouch(FlTouchInput touchInput, Size size) { - final List sectionsAngle = _calculateSectionsAngle(data.sections, data.sumValue); + final sectionsAngle = _calculateSectionsAngle(data.sections, data.sumValue); return _getTouchedDetails(size, touchInput, sectionsAngle); } @@ -259,7 +259,7 @@ class PieChartPainter extends BaseChartPainter final touchY = touchedPoint2.dy; final touchR = math.sqrt(math.pow(touchX, 2) + math.pow(touchY, 2)); - double touchAngle = degrees(math.atan2(touchY, touchX)); + var touchAngle = degrees(math.atan2(touchY, touchX)); touchAngle = touchAngle < 0 ? (180 - touchAngle.abs()) + 180 : touchAngle; PieChartSectionData foundSectionData; @@ -267,10 +267,10 @@ class PieChartPainter extends BaseChartPainter /// Find the nearest section base on the touch spot final relativeTouchAngle = (touchAngle - data.startDegreeOffset) % 360; - double tempAngle = 0.0; - for (int i = 0; i < data.sections.length; i++) { + var tempAngle = 0.0; + for (var i = 0; i < data.sections.length; i++) { final section = data.sections[i]; - double sectionAngle = sectionsAngle[i]; + var sectionAngle = sectionsAngle[i]; tempAngle %= 360; if (data.sections.length == 1) { @@ -306,16 +306,16 @@ class PieChartPainter extends BaseChartPainter /// Exposes offset for laying out the badge widgets upon the chart. @override Map getBadgeOffsets(Size viewSize) { - final Offset center = Offset(viewSize.width / 2, viewSize.height / 2); - final Map badgeWidgetsOffsets = {}; + final center = Offset(viewSize.width / 2, viewSize.height / 2); + final badgeWidgetsOffsets = {}; - double tempAngle = data.startDegreeOffset; + var tempAngle = data.startDegreeOffset; - for (int i = 0; i < data.sections.length; i++) { - final PieChartSectionData section = data.sections[i]; - final double startAngle = tempAngle; - final double sweepAngle = 360 * (section.value / data.sumValue); - final double sectionCenterAngle = startAngle + (sweepAngle / 2); + for (var i = 0; i < data.sections.length; i++) { + final section = data.sections[i]; + final startAngle = tempAngle; + final sweepAngle = 360 * (section.value / data.sumValue); + final sectionCenterAngle = startAngle + (sweepAngle / 2); Offset sectionCenter(double percentageOffset) => center + @@ -328,8 +328,7 @@ class PieChartPainter extends BaseChartPainter (section.radius * percentageOffset)), ); - final Offset sectionCenterOffsetBadgeWidget = - sectionCenter(section.badgePositionPercentageOffset); + final sectionCenterOffsetBadgeWidget = sectionCenter(section.badgePositionPercentageOffset); if (section.badgeWidget != null) { badgeWidgetsOffsets[i] = sectionCenterOffsetBadgeWidget; diff --git a/lib/src/chart/scatter_chart/scatter_chart.dart b/lib/src/chart/scatter_chart/scatter_chart.dart index d30914abd..6a4c08b98 100644 --- a/lib/src/chart/scatter_chart/scatter_chart.dart +++ b/lib/src/chart/scatter_chart/scatter_chart.dart @@ -37,89 +37,86 @@ class _ScatterChartState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { - final ScatterChartData showingData = _getData(); - final ScatterTouchData touchData = showingData.scatterTouchData; + final showingData = _getData(); + final touchData = showingData.scatterTouchData; return GestureDetector( onLongPressStart: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } - final ScatterTouchResponse response = - _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, onLongPressEnd: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } - final ScatterTouchResponse response = - _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, onLongPressMoveUpdate: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } - final ScatterTouchResponse response = + final response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, onPanCancel: () { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } - final ScatterTouchResponse response = _touchHandler?.handleTouch( + final response = _touchHandler?.handleTouch( FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, onPanEnd: (DragEndDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } - final ScatterTouchResponse response = + final response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, onPanDown: (DragDownDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } - final ScatterTouchResponse response = - _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, onPanUpdate: (DragUpdateDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } - final ScatterTouchResponse response = + final response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); diff --git a/lib/src/chart/scatter_chart/scatter_chart_data.dart b/lib/src/chart/scatter_chart/scatter_chart_data.dart index 2c805aebd..55f0f4c10 100644 --- a/lib/src/chart/scatter_chart/scatter_chart_data.dart +++ b/lib/src/chart/scatter_chart/scatter_chart_data.dart @@ -94,8 +94,8 @@ class ScatterChartData extends AxisChartData with EquatableMixin { maxY = scatterSpots[0].y; } - for (int j = 0; j < scatterSpots.length; j++) { - final ScatterSpot spot = scatterSpots[j]; + for (var j = 0; j < scatterSpots.length; j++) { + final spot = scatterSpots[j]; if (canModifyMaxX && spot.x > maxX) { maxX = spot.x; } @@ -443,7 +443,7 @@ ScatterTooltipItem defaultScatterTooltipItem(ScatterSpot touchedSpot) { if (touchedSpot == null) { return null; } - final TextStyle textStyle = TextStyle( + final textStyle = TextStyle( color: touchedSpot.color, fontWeight: FontWeight.bold, fontSize: 14, diff --git a/lib/src/chart/scatter_chart/scatter_chart_painter.dart b/lib/src/chart/scatter_chart/scatter_chart_painter.dart index ef85d4ab9..354efd5fb 100644 --- a/lib/src/chart/scatter_chart/scatter_chart_painter.dart +++ b/lib/src/chart/scatter_chart/scatter_chart_painter.dart @@ -50,12 +50,12 @@ class ScatterChartPainter extends AxisChartPainter _drawTitles(canvasWrapper); _drawSpots(canvasWrapper); - for (int i = 0; i < targetData.scatterSpots.length; i++) { + for (var i = 0; i < targetData.scatterSpots.length; i++) { if (!targetData.showingTooltipIndicators.contains(i)) { continue; } - final ScatterSpot scatterSpot = targetData.scatterSpots[i]; + final scatterSpot = targetData.scatterSpots[i]; _drawTouchTooltip(canvasWrapper, targetData.scatterTouchData.touchTooltipData, scatterSpot); } } @@ -71,17 +71,17 @@ class ScatterChartPainter extends AxisChartPainter final leftInterval = leftTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); if (leftTitles.showTitles) { - double verticalSeek = data.minY; + var verticalSeek = data.minY; while (verticalSeek <= data.maxY) { if (leftTitles.checkToShowTitle( data.minY, data.maxY, leftTitles, leftInterval, verticalSeek)) { - double x = 0 + getLeftOffsetDrawSize(); - double y = getPixelY(verticalSeek, viewSize); + var x = 0 + getLeftOffsetDrawSize(); + var y = getPixelY(verticalSeek, viewSize); - final String text = leftTitles.getTitles(verticalSeek); + final text = leftTitles.getTitles(verticalSeek); - final TextSpan span = TextSpan(style: leftTitles.getTextStyles(verticalSeek), text: text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: leftTitles.getTextStyles(verticalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -110,18 +110,17 @@ class ScatterChartPainter extends AxisChartPainter final topInterval = topTitles.interval ?? getEfficientInterval(viewSize.width, data.horizontalDiff); if (topTitles.showTitles) { - double horizontalSeek = data.minX; + var horizontalSeek = data.minX; while (horizontalSeek <= data.maxX) { if (topTitles.checkToShowTitle( data.minX, data.maxX, topTitles, topInterval, horizontalSeek)) { - double x = getPixelX(horizontalSeek, viewSize); - double y = getTopOffsetDrawSize(); + var x = getPixelX(horizontalSeek, viewSize); + var y = getTopOffsetDrawSize(); - final String text = topTitles.getTitles(horizontalSeek); + final text = topTitles.getTitles(horizontalSeek); - final TextSpan span = - TextSpan(style: topTitles.getTextStyles(horizontalSeek), text: text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: topTitles.getTextStyles(horizontalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -151,18 +150,17 @@ class ScatterChartPainter extends AxisChartPainter final rightInterval = rightTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); if (rightTitles.showTitles) { - double verticalSeek = data.minY; + var verticalSeek = data.minY; while (verticalSeek <= data.maxY) { if (rightTitles.checkToShowTitle( data.minY, data.maxY, rightTitles, rightInterval, verticalSeek)) { - double x = viewSize.width + getLeftOffsetDrawSize(); - double y = getPixelY(verticalSeek, viewSize); + var x = viewSize.width + getLeftOffsetDrawSize(); + var y = getPixelY(verticalSeek, viewSize); - final String text = rightTitles.getTitles(verticalSeek); + final text = rightTitles.getTitles(verticalSeek); - final TextSpan span = - TextSpan(style: rightTitles.getTextStyles(verticalSeek), text: text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: rightTitles.getTextStyles(verticalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -192,18 +190,17 @@ class ScatterChartPainter extends AxisChartPainter final bottomInterval = bottomTitles.interval ?? getEfficientInterval(viewSize.width, data.horizontalDiff); if (bottomTitles.showTitles) { - double horizontalSeek = data.minX; + var horizontalSeek = data.minX; while (horizontalSeek <= data.maxX) { if (bottomTitles.checkToShowTitle( data.minX, data.maxX, bottomTitles, bottomInterval, horizontalSeek)) { - double x = getPixelX(horizontalSeek, viewSize); - double y = viewSize.height + getTopOffsetDrawSize(); + var x = getPixelX(horizontalSeek, viewSize); + var y = viewSize.height + getTopOffsetDrawSize(); - final String text = bottomTitles.getTitles(horizontalSeek); + final text = bottomTitles.getTitles(horizontalSeek); - final TextSpan span = - TextSpan(style: bottomTitles.getTextStyles(horizontalSeek), text: text); - final TextPainter tp = TextPainter( + final span = TextSpan(style: bottomTitles.getTextStyles(horizontalSeek), text: text); + final tp = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -235,12 +232,12 @@ class ScatterChartPainter extends AxisChartPainter } final viewSize = canvasWrapper.size; final chartUsableSize = getChartUsableDrawSize(viewSize); - for (final ScatterSpot scatterSpot in data.scatterSpots) { + for (final scatterSpot in data.scatterSpots) { if (!scatterSpot.show) { continue; } - final double pixelX = getPixelX(scatterSpot.x, chartUsableSize); - final double pixelY = getPixelY(scatterSpot.y, chartUsableSize); + final pixelX = getPixelX(scatterSpot.x, chartUsableSize); + final pixelY = getPixelY(scatterSpot.y, chartUsableSize); _spotsPaint.color = scatterSpot.color; @@ -257,14 +254,14 @@ class ScatterChartPainter extends AxisChartPainter final viewSize = canvasWrapper.size; final chartUsableSize = getChartUsableDrawSize(viewSize); - final ScatterTooltipItem tooltipItem = tooltipData.getTooltipItems(showOnSpot); + final tooltipItem = tooltipData.getTooltipItems(showOnSpot); if (tooltipItem == null) { return; } - final TextSpan span = TextSpan(style: tooltipItem.textStyle, text: tooltipItem.text); - final TextPainter drawingTextPainter = TextPainter( + final span = TextSpan(style: tooltipItem.textStyle, text: tooltipItem.text); + final drawingTextPainter = TextPainter( text: span, textAlign: TextAlign.center, textDirection: TextDirection.ltr, @@ -277,16 +274,16 @@ class ScatterChartPainter extends AxisChartPainter /// if we have multiple bar lines, /// there are more than one FlCandidate on touch area, /// we should get the most top FlSpot Offset to draw the tooltip on top of it - final Offset mostTopOffset = Offset( + final mostTopOffset = Offset( getPixelX(showOnSpot.x, chartUsableSize), getPixelY(showOnSpot.y, chartUsableSize), ); - final double tooltipWidth = width + tooltipData.tooltipPadding.horizontal; - final double tooltipHeight = height + tooltipData.tooltipPadding.vertical; + final tooltipWidth = width + tooltipData.tooltipPadding.horizontal; + final tooltipHeight = height + tooltipData.tooltipPadding.vertical; /// draw the background rect with rounded radius - Rect rect = Rect.fromLTWH(mostTopOffset.dx - (tooltipWidth / 2), + var rect = Rect.fromLTWH(mostTopOffset.dx - (tooltipWidth / 2), mostTopOffset.dy - tooltipHeight - tooltipItem.bottomMargin, tooltipWidth, tooltipHeight); if (tooltipData.fitInsideHorizontally) { @@ -333,8 +330,8 @@ class ScatterChartPainter extends AxisChartPainter } } - final Radius radius = Radius.circular(tooltipData.tooltipRoundedRadius); - final RRect roundedRect = RRect.fromRectAndCorners(rect, + final radius = Radius.circular(tooltipData.tooltipRoundedRadius); + final roundedRect = RRect.fromRectAndCorners(rect, topLeft: radius, topRight: radius, bottomLeft: radius, bottomRight: radius); _bgTouchTooltipPaint.color = tooltipData.tooltipBgColor; canvasWrapper.drawRRect(roundedRect, _bgTouchTooltipPaint); @@ -354,7 +351,7 @@ class ScatterChartPainter extends AxisChartPainter /// and the whole space is [getExtraNeededHorizontalSpace] @override double getExtraNeededHorizontalSpace() { - double sum = super.getExtraNeededHorizontalSpace(); + var sum = super.getExtraNeededHorizontalSpace(); if (data.titlesData.show) { final leftSide = data.titlesData.leftTitles; if (leftSide.showTitles) { @@ -376,7 +373,7 @@ class ScatterChartPainter extends AxisChartPainter /// and the whole space is [getExtraNeededVerticalSpace] @override double getExtraNeededVerticalSpace() { - double sum = super.getExtraNeededVerticalSpace(); + var sum = super.getExtraNeededVerticalSpace(); if (data.titlesData.show) { final topSide = data.titlesData.topTitles; if (topSide.showTitles) { @@ -427,9 +424,9 @@ class ScatterChartPainter extends AxisChartPainter /// then makes a [ScatterTouchResponse] from the elements that has been touched. @override ScatterTouchResponse handleTouch(FlTouchInput touchInput, Size size) { - final Size chartViewSize = getChartUsableDrawSize(size); + final chartViewSize = getChartUsableDrawSize(size); - for (int i = 0; i < data.scatterSpots.length; i++) { + for (var i = 0; i < data.scatterSpots.length; i++) { final spot = data.scatterSpots[i]; final spotPixelX = getPixelX(spot.x, chartViewSize); diff --git a/lib/src/extensions/canvas_extension.dart b/lib/src/extensions/canvas_extension.dart index 441f9a6fd..b09697f4e 100644 --- a/lib/src/extensions/canvas_extension.dart +++ b/lib/src/extensions/canvas_extension.dart @@ -7,7 +7,7 @@ import 'package:fl_chart/src/utils/canvas_wrapper.dart'; extension DashedLine on CanvasWrapper { /// Draws a dashed line from passed in offsets void drawDashedLine(Offset from, Offset to, Paint painter, List dashArray) { - Path path = Path(); + var path = Path(); path.moveTo(from.dx, from.dy); path.lineTo(to.dx, to.dy); path = path.toDashedPath(dashArray); diff --git a/lib/src/extensions/paint_extension.dart b/lib/src/extensions/paint_extension.dart new file mode 100644 index 000000000..89a083e08 --- /dev/null +++ b/lib/src/extensions/paint_extension.dart @@ -0,0 +1,15 @@ +import 'package:flutter/material.dart'; + +extension ColorExtension on Paint { + /// Hides the paint's color, if strokeWidth is zero + void transparentIfWidthIsZero() { + if (strokeWidth == 0) { + shader = null; + if (color == null) { + color = Colors.transparent; + } else { + color = color.withOpacity(0.0); + } + } + } +} diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index 78930423f..8c998122a 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -108,13 +108,13 @@ Color lerpGradient(List colors, List stops, double t) { /// 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 5000, 10000,... double getEfficientInterval(double axisViewSize, double diffInYAxis, {double pixelPerInterval = 10}) { - final int allowedCount = axisViewSize ~/ pixelPerInterval; - final double accurateInterval = diffInYAxis / allowedCount; + final allowedCount = axisViewSize ~/ pixelPerInterval; + final accurateInterval = diffInYAxis / allowedCount; return _roundInterval(accurateInterval).toDouble(); } int _roundInterval(double input) { - int count = 0; + var count = 0; if (input >= 10) { count++; @@ -125,7 +125,7 @@ int _roundInterval(double input) { count++; } - final double scaled = input >= 10 ? input.round() / 10 : input; + final scaled = input >= 10 ? input.round() / 10 : input; if (scaled >= 2.6) { return 5 * pow(10, count); diff --git a/pubspec.yaml b/pubspec.yaml index 691c5dbfa..5fb9ceda7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: fl_chart description: A powerful Flutter chart library, currently supporting Line Chart, Bar Chart and Pie Chart. -version: 0.12.2 +version: 0.12.3 homepage: https://github.com/imaNNeoFighT/fl_chart environment: @@ -12,7 +12,10 @@ dependencies: path_drawing: ^0.4.1 equatable: ^1.1.1 + pedantic: ^1.9.0 dev_dependencies: flutter_test: sdk: flutter + + diff --git a/repo_files/documentations/bar_chart.md b/repo_files/documentations/bar_chart.md index 042d9b24b..5b19ed04f 100644 --- a/repo_files/documentations/bar_chart.md +++ b/repo_files/documentations/bar_chart.md @@ -25,7 +25,7 @@ BarChart( |gridData| check the [FlGridData](base_chart.md#FlGridData)|FlGridData()| |borderData| check the [FlBorderData](base_chart.md#FlBorderData)|FlBorderData()| |maxY| gets maximum y of y axis, if null, value will be read from the input barGroups | null| -|maxY| gets minimum y of y axis, if null, value will be read from the input barGroups | null| +|minY| gets minimum y of y axis, if null, value will be read from the input barGroups | null| ### BarChartGroupData @@ -138,4 +138,4 @@ enum values {`start`, `end`, `center`, `spaceEvenly`, `spaceAround`, `spaceBetwe ##### Sample 5 ([Source Code](/example/lib/bar_chart/samples/bar_chart_sample5.dart)) - \ No newline at end of file +