From 4e813ae53ccffea42cc4629236d439eda4dd5ada Mon Sep 17 00:00:00 2001 From: Wendel de Witte Date: Wed, 6 Jan 2021 15:15:27 +0100 Subject: [PATCH 01/22] Fix issue #514 --- lib/src/chart/pie_chart/pie_chart.dart | 49 +++++++++++++++----------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index d92812c0d..affb05976 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'; @@ -66,8 +65,8 @@ class _PieChartState extends AnimatedWidgetBaseState { if (chartSize == null) { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -78,8 +77,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -90,8 +89,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -103,7 +102,8 @@ class _PieChartState extends AnimatedWidgetBaseState { } final PieTouchResponse response = _touchHandler?.handleTouch( - FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), + chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -114,8 +114,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -126,8 +126,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -138,8 +138,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -162,14 +162,18 @@ class _PieChartState extends AnimatedWidgetBaseState { }); }, ), - child: badgeWidgets(), + child: LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + return badgeWidgets(constraints); + }, + ), ), ); } - 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( @@ -212,11 +216,14 @@ class _PieChartState extends AnimatedWidgetBaseState { } bool _canHandleTouch(PieTouchResponse response, PieTouchData touchData) { - return response != null && touchData != null && touchData.touchCallback != null; + return response != null && + touchData != null && + touchData.touchCallback != null; } Size _getChartSize() { - final RenderBox containerRenderBox = _chartKey.currentContext?.findRenderObject(); + final RenderBox containerRenderBox = + _chartKey.currentContext?.findRenderObject(); if (containerRenderBox != null && containerRenderBox.hasSize) { return containerRenderBox.size; } From 00b38fa3b930447c650cd6705928344b93d727aa Mon Sep 17 00:00:00 2001 From: Wendel de Witte Date: Wed, 6 Jan 2021 15:20:14 +0100 Subject: [PATCH 02/22] Flutter format --- lib/src/chart/pie_chart/pie_chart.dart | 34 ++++++++++++-------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index affb05976..92d41c84f 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -65,8 +65,8 @@ class _PieChartState extends AnimatedWidgetBaseState { if (chartSize == null) { return; } - final PieTouchResponse response = _touchHandler?.handleTouch( - FlLongPressStart(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -77,8 +77,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch( - FlLongPressEnd(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -89,8 +89,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch( - FlLongPressMoveUpdate(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -102,8 +102,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } final PieTouchResponse response = _touchHandler?.handleTouch( - FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), - chartSize); + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -114,8 +113,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch( - FlPanEnd(Offset.zero, details.velocity), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -126,8 +125,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch( - FlPanStart(details.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -138,8 +137,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch( - FlPanMoveUpdate(details.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -216,14 +215,11 @@ class _PieChartState extends AnimatedWidgetBaseState { } bool _canHandleTouch(PieTouchResponse response, PieTouchData touchData) { - return response != null && - touchData != null && - touchData.touchCallback != null; + return response != null && touchData != null && touchData.touchCallback != null; } Size _getChartSize() { - final RenderBox containerRenderBox = - _chartKey.currentContext?.findRenderObject(); + final RenderBox containerRenderBox = _chartKey.currentContext?.findRenderObject(); if (containerRenderBox != null && containerRenderBox.hasSize) { return containerRenderBox.size; } From 4f06302a5b011179b4794a98d018c6c9d1ba6b66 Mon Sep 17 00:00:00 2001 From: albertms10 Date: Sun, 10 Jan 2021 00:49:01 +0100 Subject: [PATCH 03/22] refactor(bar_chart_painter): change list constructor to filled --- lib/src/chart/bar_chart/bar_chart_painter.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/chart/bar_chart/bar_chart_painter.dart b/lib/src/chart/bar_chart/bar_chart_painter.dart index a30580250..4c2e57c3a 100644 --- a/lib/src/chart/bar_chart/bar_chart_painter.dart +++ b/lib/src/chart/bar_chart/bar_chart_painter.dart @@ -75,7 +75,7 @@ class BarChartPainter extends AxisChartPainter with TouchHandler barGroups, BarChartAlignment alignment) { final Size drawSize = getChartUsableDrawSize(viewSize); - final List groupsX = List(barGroups.length); + final List groupsX = List.filled(barGroups.length, null); final double leftTextsSpace = getLeftOffsetDrawSize(); From bd1c6c2ec43d4083d5263c918d9ab4757c26b333 Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 14:34:14 +0530 Subject: [PATCH 04/22] Update example app (add flutter web support) Signed-off-by: Shripal Jain --- .../res/drawable-v21/launch_background.xml | 12 +++++ .../app/src/main/res/values-night/styles.xml | 18 +++++++ .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++ .../xcshareddata/WorkspaceSettings.xcsettings | 8 ++++ .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++ .../xcshareddata/WorkspaceSettings.xcsettings | 8 ++++ example/web/favicon.png | Bin 0 -> 917 bytes example/web/icons/Icon-192.png | Bin 0 -> 5292 bytes example/web/icons/Icon-512.png | Bin 0 -> 8252 bytes example/web/index.html | 45 ++++++++++++++++++ example/web/manifest.json | 23 +++++++++ 11 files changed, 130 insertions(+) create mode 100644 example/android/app/src/main/res/drawable-v21/launch_background.xml create mode 100644 example/android/app/src/main/res/values-night/styles.xml create mode 100644 example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings create mode 100644 example/web/favicon.png create mode 100644 example/web/icons/Icon-192.png create mode 100644 example/web/icons/Icon-512.png create mode 100644 example/web/index.html create mode 100644 example/web/manifest.json 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/web/favicon.png b/example/web/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..8aaa46ac1ae21512746f852a42ba87e4165dfdd1 GIT binary patch literal 917 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|I14-?iy0X7 zltGxWVyS%@P(fs7NJL45ua8x7ey(0(N`6wRUPW#JP&EUCO@$SZnVVXYs8ErclUHn2 zVXFjIVFhG^g!Ppaz)DK8ZIvQ?0~DO|i&7O#^-S~(l1AfjnEK zjFOT9D}DX)@^Za$W4-*MbbUihOG|wNBYh(yU7!lx;>x^|#0uTKVr7USFmqf|i<65o z3raHc^AtelCMM;Vme?vOfh>Xph&xL%(-1c06+^uR^q@XSM&D4+Kp$>4P^%3{)XKjo zGZknv$b36P8?Z_gF{nK@`XI}Z90TzwSQO}0J1!f2c(B=V`5aP@1P1a|PZ!4!3&Gl8 zTYqUsf!gYFyJnXpu0!n&N*SYAX-%d(5gVjrHJWqXQshj@!Zm{!01WsQrH~9=kTxW#6SvuapgMqt>$=j#%eyGrQzr zP{L-3gsMA^$I1&gsBAEL+vxi1*Igl=8#8`5?A-T5=z-sk46WA1IUT)AIZHx1rdUrf zVJrJn<74DDw`j)Ki#gt}mIT-Q`XRa2-jQXQoI%w`nb|XblvzK${ZzlV)m-XcwC(od z71_OEC5Bt9GEXosOXaPTYOia#R4ID2TiU~`zVMl08TV_C%DnU4^+HE>9(CE4D6?Fz oujB08i7adh9xk7*FX66dWH6F5TM;?E2b5PlUHx3vIVCg!0Dx9vYXATM literal 0 HcmV?d00001 diff --git a/example/web/icons/Icon-192.png b/example/web/icons/Icon-192.png new file mode 100644 index 0000000000000000000000000000000000000000..b749bfef07473333cf1dd31e9eed89862a5d52aa GIT binary patch literal 5292 zcmZ`-2T+sGz6~)*FVZ`aW+(v>MIm&M-g^@e2u-B-DoB?qO+b1Tq<5uCCv>ESfRum& zp%X;f!~1{tzL__3=gjVJ=j=J>+nMj%ncXj1Q(b|Ckbw{Y0FWpt%4y%$uD=Z*c-x~o zE;IoE;xa#7Ll5nj-e4CuXB&G*IM~D21rCP$*xLXAK8rIMCSHuSu%bL&S3)8YI~vyp@KBu9Ph7R_pvKQ@xv>NQ`dZp(u{Z8K3yOB zn7-AR+d2JkW)KiGx0hosml;+eCXp6+w%@STjFY*CJ?udJ64&{BCbuebcuH;}(($@@ znNlgBA@ZXB)mcl9nbX#F!f_5Z=W>0kh|UVWnf!At4V*LQP%*gPdCXd6P@J4Td;!Ur z<2ZLmwr(NG`u#gDEMP19UcSzRTL@HsK+PnIXbVBT@oHm53DZr?~V(0{rsalAfwgo zEh=GviaqkF;}F_5-yA!1u3!gxaR&Mj)hLuj5Q-N-@Lra{%<4ONja8pycD90&>yMB` zchhd>0CsH`^|&TstH-8+R`CfoWqmTTF_0?zDOY`E`b)cVi!$4xA@oO;SyOjJyP^_j zx^@Gdf+w|FW@DMdOi8=4+LJl$#@R&&=UM`)G!y%6ZzQLoSL%*KE8IO0~&5XYR9 z&N)?goEiWA(YoRfT{06&D6Yuu@Qt&XVbuW@COb;>SP9~aRc+z`m`80pB2o%`#{xD@ zI3RAlukL5L>px6b?QW1Ac_0>ew%NM!XB2(H+1Y3AJC?C?O`GGs`331Nd4ZvG~bMo{lh~GeL zSL|tT*fF-HXxXYtfu5z+T5Mx9OdP7J4g%@oeC2FaWO1D{=NvL|DNZ}GO?O3`+H*SI z=grGv=7dL{+oY0eJFGO!Qe(e2F?CHW(i!!XkGo2tUvsQ)I9ev`H&=;`N%Z{L zO?vV%rDv$y(@1Yj@xfr7Kzr<~0{^T8wM80xf7IGQF_S-2c0)0D6b0~yD7BsCy+(zL z#N~%&e4iAwi4F$&dI7x6cE|B{f@lY5epaDh=2-(4N05VO~A zQT3hanGy_&p+7Fb^I#ewGsjyCEUmSCaP6JDB*=_()FgQ(-pZ28-{qx~2foO4%pM9e z*_63RT8XjgiaWY|*xydf;8MKLd{HnfZ2kM%iq}fstImB-K6A79B~YoPVa@tYN@T_$ zea+9)<%?=Fl!kd(Y!G(-o}ko28hg2!MR-o5BEa_72uj7Mrc&{lRh3u2%Y=Xk9^-qa zBPWaD=2qcuJ&@Tf6ue&)4_V*45=zWk@Z}Q?f5)*z)-+E|-yC4fs5CE6L_PH3=zI8p z*Z3!it{1e5_^(sF*v=0{`U9C741&lub89gdhKp|Y8CeC{_{wYK-LSbp{h)b~9^j!s z7e?Y{Z3pZv0J)(VL=g>l;<}xk=T*O5YR|hg0eg4u98f2IrA-MY+StQIuK-(*J6TRR z|IM(%uI~?`wsfyO6Tgmsy1b3a)j6M&-jgUjVg+mP*oTKdHg?5E`!r`7AE_#?Fc)&a z08KCq>Gc=ne{PCbRvs6gVW|tKdcE1#7C4e`M|j$C5EYZ~Y=jUtc zj`+?p4ba3uy7><7wIokM79jPza``{Lx0)zGWg;FW1^NKY+GpEi=rHJ+fVRGfXO zPHV52k?jxei_!YYAw1HIz}y8ZMwdZqU%ESwMn7~t zdI5%B;U7RF=jzRz^NuY9nM)&<%M>x>0(e$GpU9th%rHiZsIT>_qp%V~ILlyt^V`=d z!1+DX@ah?RnB$X!0xpTA0}lN@9V-ePx>wQ?-xrJr^qDlw?#O(RsXeAvM%}rg0NT#t z!CsT;-vB=B87ShG`GwO;OEbeL;a}LIu=&@9cb~Rsx(ZPNQ!NT7H{@j0e(DiLea>QD zPmpe90gEKHEZ8oQ@6%E7k-Ptn#z)b9NbD@_GTxEhbS+}Bb74WUaRy{w;E|MgDAvHw zL)ycgM7mB?XVh^OzbC?LKFMotw3r@i&VdUV%^Efdib)3@soX%vWCbnOyt@Y4swW925@bt45y0HY3YI~BnnzZYrinFy;L?2D3BAL`UQ zEj))+f>H7~g8*VuWQ83EtGcx`hun$QvuurSMg3l4IP8Fe`#C|N6mbYJ=n;+}EQm;< z!!N=5j1aAr_uEnnzrEV%_E|JpTb#1p1*}5!Ce!R@d$EtMR~%9# zd;h8=QGT)KMW2IKu_fA_>p_und#-;Q)p%%l0XZOXQicfX8M~7?8}@U^ihu;mizj)t zgV7wk%n-UOb z#!P5q?Ex+*Kx@*p`o$q8FWL*E^$&1*!gpv?Za$YO~{BHeGY*5%4HXUKa_A~~^d z=E*gf6&+LFF^`j4$T~dR)%{I)T?>@Ma?D!gi9I^HqvjPc3-v~=qpX1Mne@*rzT&Xw zQ9DXsSV@PqpEJO-g4A&L{F&;K6W60D!_vs?Vx!?w27XbEuJJP&);)^+VF1nHqHBWu z^>kI$M9yfOY8~|hZ9WB!q-9u&mKhEcRjlf2nm_@s;0D#c|@ED7NZE% zzR;>P5B{o4fzlfsn3CkBK&`OSb-YNrqx@N#4CK!>bQ(V(D#9|l!e9(%sz~PYk@8zt zPN9oK78&-IL_F zhsk1$6p;GqFbtB^ZHHP+cjMvA0(LqlskbdYE_rda>gvQLTiqOQ1~*7lg%z*&p`Ry& zRcG^DbbPj_jOKHTr8uk^15Boj6>hA2S-QY(W-6!FIq8h$<>MI>PYYRenQDBamO#Fv zAH5&ImqKBDn0v5kb|8i0wFhUBJTpT!rB-`zK)^SNnRmLraZcPYK7b{I@+}wXVdW-{Ps17qdRA3JatEd?rPV z4@}(DAMf5EqXCr4-B+~H1P#;t@O}B)tIJ(W6$LrK&0plTmnPpb1TKn3?f?Kk``?D+ zQ!MFqOX7JbsXfQrz`-M@hq7xlfNz;_B{^wbpG8des56x(Q)H)5eLeDwCrVR}hzr~= zM{yXR6IM?kXxauLza#@#u?Y|o;904HCqF<8yT~~c-xyRc0-vxofnxG^(x%>bj5r}N zyFT+xnn-?B`ohA>{+ZZQem=*Xpqz{=j8i2TAC#x-m;;mo{{sLB_z(UoAqD=A#*juZ zCv=J~i*O8;F}A^Wf#+zx;~3B{57xtoxC&j^ie^?**T`WT2OPRtC`xj~+3Kprn=rVM zVJ|h5ux%S{dO}!mq93}P+h36mZ5aZg1-?vhL$ke1d52qIiXSE(llCr5i=QUS?LIjc zV$4q=-)aaR4wsrQv}^shL5u%6;`uiSEs<1nG^?$kl$^6DL z43CjY`M*p}ew}}3rXc7Xck@k41jx}c;NgEIhKZ*jsBRZUP-x2cm;F1<5$jefl|ppO zmZd%%?gMJ^g9=RZ^#8Mf5aWNVhjAS^|DQO+q$)oeob_&ZLFL(zur$)); zU19yRm)z<4&4-M}7!9+^Wl}Uk?`S$#V2%pQ*SIH5KI-mn%i;Z7-)m$mN9CnI$G7?# zo`zVrUwoSL&_dJ92YhX5TKqaRkfPgC4=Q&=K+;_aDs&OU0&{WFH}kKX6uNQC6%oUH z2DZa1s3%Vtk|bglbxep-w)PbFG!J17`<$g8lVhqD2w;Z0zGsh-r zxZ13G$G<48leNqR!DCVt9)@}(zMI5w6Wo=N zpP1*3DI;~h2WDWgcKn*f!+ORD)f$DZFwgKBafEZmeXQMAsq9sxP9A)7zOYnkHT9JU zRA`umgmP9d6=PHmFIgx=0$(sjb>+0CHG)K@cPG{IxaJ&Ueo8)0RWgV9+gO7+Bl1(F z7!BslJ2MP*PWJ;x)QXbR$6jEr5q3 z(3}F@YO_P1NyTdEXRLU6fp?9V2-S=E+YaeLL{Y)W%6`k7$(EW8EZSA*(+;e5@jgD^I zaJQ2|oCM1n!A&-8`;#RDcZyk*+RPkn_r8?Ak@agHiSp*qFNX)&i21HE?yuZ;-C<3C zwJGd1lx5UzViP7sZJ&|LqH*mryb}y|%AOw+v)yc`qM)03qyyrqhX?ub`Cjwx2PrR! z)_z>5*!*$x1=Qa-0uE7jy0z`>|Ni#X+uV|%_81F7)b+nf%iz=`fF4g5UfHS_?PHbr zB;0$bK@=di?f`dS(j{l3-tSCfp~zUuva+=EWxJcRfp(<$@vd(GigM&~vaYZ0c#BTs z3ijkxMl=vw5AS&DcXQ%eeKt!uKvh2l3W?&3=dBHU=Gz?O!40S&&~ei2vg**c$o;i89~6DVns zG>9a*`k5)NI9|?W!@9>rzJ;9EJ=YlJTx1r1BA?H`LWijk(rTax9(OAu;q4_wTj-yj z1%W4GW&K4T=uEGb+E!>W0SD_C0RR91 literal 0 HcmV?d00001 diff --git a/example/web/icons/Icon-512.png b/example/web/icons/Icon-512.png new file mode 100644 index 0000000000000000000000000000000000000000..88cfd48dff1169879ba46840804b412fe02fefd6 GIT binary patch literal 8252 zcmd5=2T+s!lYZ%-(h(2@5fr2dC?F^$C=i-}R6$UX8af(!je;W5yC_|HmujSgN*6?W z3knF*TL1$|?oD*=zPbBVex*RUIKsL<(&Rj9%^UD2IK3W?2j>D?eWQgvS-HLymHo9%~|N2Q{~j za?*X-{b9JRowv_*Mh|;*-kPFn>PI;r<#kFaxFqbn?aq|PduQg=2Q;~Qc}#z)_T%x9 zE|0!a70`58wjREmAH38H1)#gof)U3g9FZ^ zF7&-0^Hy{4XHWLoC*hOG(dg~2g6&?-wqcpf{ z&3=o8vw7lMi22jCG9RQbv8H}`+}9^zSk`nlR8?Z&G2dlDy$4#+WOlg;VHqzuE=fM@ z?OI6HEJH4&tA?FVG}9>jAnq_^tlw8NbjNhfqk2rQr?h(F&WiKy03Sn=-;ZJRh~JrD zbt)zLbnabttEZ>zUiu`N*u4sfQaLE8-WDn@tHp50uD(^r-}UsUUu)`!Rl1PozAc!a z?uj|2QDQ%oV-jxUJmJycySBINSKdX{kDYRS=+`HgR2GO19fg&lZKyBFbbXhQV~v~L za^U944F1_GtuFXtvDdDNDvp<`fqy);>Vw=ncy!NB85Tw{&sT5&Ox%-p%8fTS;OzlRBwErvO+ROe?{%q-Zge=%Up|D4L#>4K@Ke=x%?*^_^P*KD zgXueMiS63!sEw@fNLB-i^F|@Oib+S4bcy{eu&e}Xvb^(mA!=U=Xr3||IpV~3K zQWzEsUeX_qBe6fky#M zzOJm5b+l;~>=sdp%i}}0h zO?B?i*W;Ndn02Y0GUUPxERG`3Bjtj!NroLoYtyVdLtl?SE*CYpf4|_${ku2s`*_)k zN=a}V8_2R5QANlxsq!1BkT6$4>9=-Ix4As@FSS;1q^#TXPrBsw>hJ}$jZ{kUHoP+H zvoYiR39gX}2OHIBYCa~6ERRPJ#V}RIIZakUmuIoLF*{sO8rAUEB9|+A#C|@kw5>u0 zBd=F!4I)Be8ycH*)X1-VPiZ+Ts8_GB;YW&ZFFUo|Sw|x~ZajLsp+_3gv((Q#N>?Jz zFBf`~p_#^${zhPIIJY~yo!7$-xi2LK%3&RkFg}Ax)3+dFCjGgKv^1;lUzQlPo^E{K zmCnrwJ)NuSaJEmueEPO@(_6h3f5mFffhkU9r8A8(JC5eOkux{gPmx_$Uv&|hyj)gN zd>JP8l2U&81@1Hc>#*su2xd{)T`Yw< zN$dSLUN}dfx)Fu`NcY}TuZ)SdviT{JHaiYgP4~@`x{&h*Hd>c3K_To9BnQi@;tuoL z%PYQo&{|IsM)_>BrF1oB~+`2_uZQ48z9!)mtUR zdfKE+b*w8cPu;F6RYJiYyV;PRBbThqHBEu_(U{(gGtjM}Zi$pL8Whx}<JwE3RM0F8x7%!!s)UJVq|TVd#hf1zVLya$;mYp(^oZQ2>=ZXU1c$}f zm|7kfk>=4KoQoQ!2&SOW5|JP1)%#55C$M(u4%SP~tHa&M+=;YsW=v(Old9L3(j)`u z2?#fK&1vtS?G6aOt@E`gZ9*qCmyvc>Ma@Q8^I4y~f3gs7*d=ATlP>1S zyF=k&6p2;7dn^8?+!wZO5r~B+;@KXFEn^&C=6ma1J7Au6y29iMIxd7#iW%=iUzq&C=$aPLa^Q zncia$@TIy6UT@69=nbty5epP>*fVW@5qbUcb2~Gg75dNd{COFLdiz3}kODn^U*=@E z0*$7u7Rl2u)=%fk4m8EK1ctR!6%Ve`e!O20L$0LkM#f+)n9h^dn{n`T*^~d+l*Qlx z$;JC0P9+en2Wlxjwq#z^a6pdnD6fJM!GV7_%8%c)kc5LZs_G^qvw)&J#6WSp< zmsd~1-(GrgjC56Pdf6#!dt^y8Rg}!#UXf)W%~PeU+kU`FeSZHk)%sFv++#Dujk-~m zFHvVJC}UBn2jN& zs!@nZ?e(iyZPNo`p1i#~wsv9l@#Z|ag3JR>0#u1iW9M1RK1iF6-RbJ4KYg?B`dET9 zyR~DjZ>%_vWYm*Z9_+^~hJ_|SNTzBKx=U0l9 z9x(J96b{`R)UVQ$I`wTJ@$_}`)_DyUNOso6=WOmQKI1e`oyYy1C&%AQU<0-`(ow)1 zT}gYdwWdm4wW6|K)LcfMe&psE0XGhMy&xS`@vLi|1#Za{D6l@#D!?nW87wcscUZgELT{Cz**^;Zb~7 z(~WFRO`~!WvyZAW-8v!6n&j*PLm9NlN}BuUN}@E^TX*4Or#dMMF?V9KBeLSiLO4?B zcE3WNIa-H{ThrlCoN=XjOGk1dT=xwwrmt<1a)mrRzg{35`@C!T?&_;Q4Ce=5=>z^*zE_c(0*vWo2_#TD<2)pLXV$FlwP}Ik74IdDQU@yhkCr5h zn5aa>B7PWy5NQ!vf7@p_qtC*{dZ8zLS;JetPkHi>IvPjtJ#ThGQD|Lq#@vE2xdl%`x4A8xOln}BiQ92Po zW;0%A?I5CQ_O`@Ad=`2BLPPbBuPUp@Hb%a_OOI}y{Rwa<#h z5^6M}s7VzE)2&I*33pA>e71d78QpF>sNK;?lj^Kl#wU7G++`N_oL4QPd-iPqBhhs| z(uVM}$ItF-onXuuXO}o$t)emBO3Hjfyil@*+GF;9j?`&67GBM;TGkLHi>@)rkS4Nj zAEk;u)`jc4C$qN6WV2dVd#q}2X6nKt&X*}I@jP%Srs%%DS92lpDY^K*Sx4`l;aql$ zt*-V{U&$DM>pdO?%jt$t=vg5|p+Rw?SPaLW zB6nvZ69$ne4Z(s$3=Rf&RX8L9PWMV*S0@R zuIk&ba#s6sxVZ51^4Kon46X^9`?DC9mEhWB3f+o4#2EXFqy0(UTc>GU| zGCJmI|Dn-dX#7|_6(fT)>&YQ0H&&JX3cTvAq(a@ydM4>5Njnuere{J8p;3?1az60* z$1E7Yyxt^ytULeokgDnRVKQw9vzHg1>X@@jM$n$HBlveIrKP5-GJq%iWH#odVwV6cF^kKX(@#%%uQVb>#T6L^mC@)%SMd4DF? zVky!~ge27>cpUP1Vi}Z32lbLV+CQy+T5Wdmva6Fg^lKb!zrg|HPU=5Qu}k;4GVH+x z%;&pN1LOce0w@9i1Mo-Y|7|z}fbch@BPp2{&R-5{GLoeu8@limQmFF zaJRR|^;kW_nw~0V^ zfTnR!Ni*;-%oSHG1yItARs~uxra|O?YJxBzLjpeE-=~TO3Dn`JL5Gz;F~O1u3|FE- zvK2Vve`ylc`a}G`gpHg58Cqc9fMoy1L}7x7T>%~b&irrNMo?np3`q;d3d;zTK>nrK zOjPS{@&74-fA7j)8uT9~*g23uGnxwIVj9HorzUX#s0pcp2?GH6i}~+kv9fWChtPa_ z@T3m+$0pbjdQw7jcnHn;Pi85hk_u2-1^}c)LNvjdam8K-XJ+KgKQ%!?2n_!#{$H|| zLO=%;hRo6EDmnOBKCL9Cg~ETU##@u^W_5joZ%Et%X_n##%JDOcsO=0VL|Lkk!VdRJ z^|~2pB@PUspT?NOeO?=0Vb+fAGc!j%Ufn-cB`s2A~W{Zj{`wqWq_-w0wr@6VrM zbzni@8c>WS!7c&|ZR$cQ;`niRw{4kG#e z70e!uX8VmP23SuJ*)#(&R=;SxGAvq|&>geL&!5Z7@0Z(No*W561n#u$Uc`f9pD70# z=sKOSK|bF~#khTTn)B28h^a1{;>EaRnHj~>i=Fnr3+Fa4 z`^+O5_itS#7kPd20rq66_wH`%?HNzWk@XFK0n;Z@Cx{kx==2L22zWH$Yg?7 zvDj|u{{+NR3JvUH({;b*$b(U5U z7(lF!1bz2%06+|-v(D?2KgwNw7( zJB#Tz+ZRi&U$i?f34m7>uTzO#+E5cbaiQ&L}UxyOQq~afbNB4EI{E04ZWg53w0A{O%qo=lF8d zf~ktGvIgf-a~zQoWf>loF7pOodrd0a2|BzwwPDV}ShauTK8*fmF6NRbO>Iw9zZU}u zw8Ya}?seBnEGQDmH#XpUUkj}N49tP<2jYwTFp!P+&Fd(%Z#yo80|5@zN(D{_pNow*&4%ql zW~&yp@scb-+Qj-EmErY+Tu=dUmf@*BoXY2&oKT8U?8?s1d}4a`Aq>7SV800m$FE~? zjmz(LY+Xx9sDX$;vU`xgw*jLw7dWOnWWCO8o|;}f>cu0Q&`0I{YudMn;P;L3R-uz# zfns_mZED_IakFBPP2r_S8XM$X)@O-xVKi4`7373Jkd5{2$M#%cRhWer3M(vr{S6>h zj{givZJ3(`yFL@``(afn&~iNx@B1|-qfYiZu?-_&Z8+R~v`d6R-}EX9IVXWO-!hL5 z*k6T#^2zAXdardU3Ao~I)4DGdAv2bx{4nOK`20rJo>rmk3S2ZDu}))8Z1m}CKigf0 z3L`3Y`{huj`xj9@`$xTZzZc3je?n^yG<8sw$`Y%}9mUsjUR%T!?k^(q)6FH6Af^b6 zlPg~IEwg0y;`t9y;#D+uz!oE4VP&Je!<#q*F?m5L5?J3i@!0J6q#eu z!RRU`-)HeqGi_UJZ(n~|PSNsv+Wgl{P-TvaUQ9j?ZCtvb^37U$sFpBrkT{7Jpd?HpIvj2!}RIq zH{9~+gErN2+}J`>Jvng2hwM`=PLNkc7pkjblKW|+Fk9rc)G1R>Ww>RC=r-|!m-u7( zc(a$9NG}w#PjWNMS~)o=i~WA&4L(YIW25@AL9+H9!?3Y}sv#MOdY{bb9j>p`{?O(P zIvb`n?_(gP2w3P#&91JX*md+bBEr%xUHMVqfB;(f?OPtMnAZ#rm5q5mh;a2f_si2_ z3oXWB?{NF(JtkAn6F(O{z@b76OIqMC$&oJ_&S|YbFJ*)3qVX_uNf5b8(!vGX19hsG z(OP>RmZp29KH9Ge2kKjKigUmOe^K_!UXP`von)PR8Qz$%=EmOB9xS(ZxE_tnyzo}7 z=6~$~9k0M~v}`w={AeqF?_)9q{m8K#6M{a&(;u;O41j)I$^T?lx5(zlebpY@NT&#N zR+1bB)-1-xj}R8uwqwf=iP1GbxBjneCC%UrSdSxK1vM^i9;bUkS#iRZw2H>rS<2<$ zNT3|sDH>{tXb=zq7XZi*K?#Zsa1h1{h5!Tq_YbKFm_*=A5-<~j63he;4`77!|LBlo zR^~tR3yxcU=gDFbshyF6>o0bdp$qmHS7D}m3;^QZq9kBBU|9$N-~oU?G5;jyFR7>z hN`IR97YZXIo@y!QgFWddJ3|0`sjFx!m))><{BI=FK%f8s literal 0 HcmV?d00001 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" + } + ] +} From 06c20054d06a5ea325a83fe7122daa3d2849447f Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 15:08:17 +0530 Subject: [PATCH 05/22] Update example app with next and previous buttons (for web only) - Disable touch scrolling for web only - Handle SVG display for badge for web in pie_chart_sample3 Signed-off-by: Shripal Jain --- example/lib/main.dart | 65 ++++++++++++++++--- .../pie_chart/samples/pie_chart_sample3.dart | 29 ++++----- 2 files changed, 68 insertions(+), 26 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 941dfdf7a..a22681279 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), + ), + ), + ], + ), + ) + : Container(), ); } } diff --git a/example/lib/pie_chart/samples/pie_chart_sample3.dart b/example/lib/pie_chart/samples/pie_chart_sample3.dart index d2ba661b7..73796a5f2 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 @@ -24,8 +24,7 @@ class PieChartSample3State extends State { PieChartData( pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) { setState(() { - if (pieTouchResponse.touchInput is FlLongPressEnd || - pieTouchResponse.touchInput is FlPanEnd) { + if (pieTouchResponse.touchInput is FlLongPressEnd || pieTouchResponse.touchInput is FlPanEnd) { touchedIndex = -1; } else { touchedIndex = pieTouchResponse.touchedSectionIndex; @@ -58,8 +57,7 @@ class PieChartSample3State extends State { value: 40, title: '40%', radius: radius, - titleStyle: TextStyle( - fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/ophthalmology-svgrepo-com.svg', size: widgetSize, @@ -73,8 +71,7 @@ class PieChartSample3State extends State { value: 30, title: '30%', radius: radius, - titleStyle: TextStyle( - fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/librarian-svgrepo-com.svg', size: widgetSize, @@ -88,8 +85,7 @@ class PieChartSample3State extends State { value: 16, title: '16%', radius: radius, - titleStyle: TextStyle( - fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/fitness-svgrepo-com.svg', size: widgetSize, @@ -103,8 +99,7 @@ class PieChartSample3State extends State { value: 15, title: '15%', radius: radius, - titleStyle: TextStyle( - fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/worker-svgrepo-com.svg', size: widgetSize, @@ -154,10 +149,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, + ), ), ); } From 5aa24facef66f2ea2ce4f789acb43b4ddd5f281e Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 15:12:26 +0530 Subject: [PATCH 06/22] Wrap GestureDetector in line_chart with MouseRegion - This enables tooltip showing on mouse hover for flutter web Signed-off-by: Shripal Jain --- lib/src/chart/line_chart/line_chart.dart | 161 +++++++++++++---------- 1 file changed, 94 insertions(+), 67 deletions(-) diff --git a/lib/src/chart/line_chart/line_chart.dart b/lib/src/chart/line_chart/line_chart.dart index 5076bed1d..40c0c8162 100644 --- a/lib/src/chart/line_chart/line_chart.dart +++ b/lib/src/chart/line_chart/line_chart.dart @@ -43,100 +43,127 @@ class _LineChartState extends AnimatedWidgetBaseState { final LineChartData showingData = _getData(); final LineTouchData touchData = showingData.lineTouchData; - return GestureDetector( - onLongPressStart: (d) { + return MouseRegion( + onEnter: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final LineTouchResponse response = - _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final LineTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) { + onExit: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final LineTouchResponse response = - _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressMoveUpdate: (d) { + onHover: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final LineTouchResponse response = - _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final LineTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () { - final Size 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(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(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(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), + child: GestureDetector( + onLongPressStart: (d) { + 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 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 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 Size 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(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(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(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), + ), ), ); } From c5ca631255457ca3f4f1d672b2dd6ad39535ff30 Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 16:15:01 +0530 Subject: [PATCH 07/22] Wrap GestureDetector in pie_chart with MouseRegion - This enables tooltip showing on mouse hover for flutter web/desktop Signed-off-by: Shripal Jain --- lib/src/chart/pie_chart/pie_chart.dart | 178 ++++++++++++++----------- 1 file changed, 103 insertions(+), 75 deletions(-) diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index d92812c0d..cad51281c 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -60,109 +60,137 @@ class _PieChartState extends AnimatedWidgetBaseState { 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 PieTouchResponse 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 PieTouchResponse response = - _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); - if (_canHandleTouch(response, touchData)) { - touchData.touchCallback(response); - } - }, - onPanCancel: () { + return MouseRegion( + onEnter: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final PieTouchResponse response = _touchHandler?.handleTouch( - FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanEnd: (DragEndDetails details) { + onExit: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanDown: (DragDownDetails details) { + onHover: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final PieTouchResponse response = - _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanUpdate: (DragUpdateDetails details) { - final Size 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: 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 PieTouchResponse 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 PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + if (_canHandleTouch(response, touchData)) { + touchData.touchCallback(response); + } + }, + onPanCancel: () { + final Size 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 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(); + 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: badgeWidgets(), ), - child: badgeWidgets(), ), ); } From 6fcf6b4641a0e6a54f1791aa9490c6ad3671e4df Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 16:17:47 +0530 Subject: [PATCH 08/22] Fix next/previous buttons in example app causing issue in mobile Signed-off-by: Shripal Jain --- example/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index a22681279..95d78fbb2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -97,7 +97,7 @@ class _MyHomePageState extends State { ], ), ) - : Container(), + : null, ); } } From e46d50b9f0b4eabffd2e9cb00520b3b04aa2761f Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 16:23:36 +0530 Subject: [PATCH 09/22] Wrap GestureDetector in bar_chart with MouseRegion - This enables tooltip showing on mouse hover for flutter web Signed-off-by: Shripal Jain --- lib/src/chart/bar_chart/bar_chart.dart | 168 ++++++++++++++----------- 1 file changed, 98 insertions(+), 70 deletions(-) diff --git a/lib/src/chart/bar_chart/bar_chart.dart b/lib/src/chart/bar_chart/bar_chart.dart index 3daa7d65e..19299a113 100644 --- a/lib/src/chart/bar_chart/bar_chart.dart +++ b/lib/src/chart/bar_chart/bar_chart.dart @@ -38,103 +38,131 @@ class _BarChartState extends AnimatedWidgetBaseState { final BarChartData showingData = _getData(); final BarTouchData touchData = showingData.barTouchData; - return GestureDetector( - onLongPressStart: (d) { + return MouseRegion( + onEnter: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final BarTouchResponse response = - _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final BarTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressEnd: (d) { + onExit: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final BarTouchResponse response = - _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onLongPressMoveUpdate: (d) { + onHover: (e) { final Size chartSize = _getChartSize(); if (chartSize == null) { return; } - final BarTouchResponse response = - _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final BarTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } }, - onPanCancel: () { - final Size 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 Size 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 Size 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(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, + child: GestureDetector( + onLongPressStart: (d) { + 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 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(); + 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 Size 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 Size 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(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, + ), ), ), ); From 7a951d4599d4f4f539c56d9bee9490773c1033e0 Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 16:31:12 +0530 Subject: [PATCH 10/22] Format files according to project's specification Signed-off-by: Shripal Jain --- .../pie_chart/samples/pie_chart_sample3.dart | 15 ++++++--- lib/src/chart/bar_chart/bar_chart.dart | 30 +++++++++++------ lib/src/chart/line_chart/line_chart.dart | 33 ++++++++++++------- lib/src/chart/pie_chart/pie_chart.dart | 30 +++++++++++------ 4 files changed, 72 insertions(+), 36 deletions(-) diff --git a/example/lib/pie_chart/samples/pie_chart_sample3.dart b/example/lib/pie_chart/samples/pie_chart_sample3.dart index 73796a5f2..c7e2747b0 100644 --- a/example/lib/pie_chart/samples/pie_chart_sample3.dart +++ b/example/lib/pie_chart/samples/pie_chart_sample3.dart @@ -24,7 +24,8 @@ class PieChartSample3State extends State { PieChartData( pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) { setState(() { - if (pieTouchResponse.touchInput is FlLongPressEnd || pieTouchResponse.touchInput is FlPanEnd) { + if (pieTouchResponse.touchInput is FlLongPressEnd || + pieTouchResponse.touchInput is FlPanEnd) { touchedIndex = -1; } else { touchedIndex = pieTouchResponse.touchedSectionIndex; @@ -57,7 +58,8 @@ class PieChartSample3State extends State { value: 40, title: '40%', radius: radius, - titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle( + fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/ophthalmology-svgrepo-com.svg', size: widgetSize, @@ -71,7 +73,8 @@ class PieChartSample3State extends State { value: 30, title: '30%', radius: radius, - titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle( + fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/librarian-svgrepo-com.svg', size: widgetSize, @@ -85,7 +88,8 @@ class PieChartSample3State extends State { value: 16, title: '16%', radius: radius, - titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle( + fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/fitness-svgrepo-com.svg', size: widgetSize, @@ -99,7 +103,8 @@ class PieChartSample3State extends State { value: 15, title: '15%', radius: radius, - titleStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), + titleStyle: TextStyle( + fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff)), badgeWidget: _Badge( 'assets/worker-svgrepo-com.svg', size: widgetSize, diff --git a/lib/src/chart/bar_chart/bar_chart.dart b/lib/src/chart/bar_chart/bar_chart.dart index 19299a113..2839f6b34 100644 --- a/lib/src/chart/bar_chart/bar_chart.dart +++ b/lib/src/chart/bar_chart/bar_chart.dart @@ -45,7 +45,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -56,7 +57,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final BarTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -67,7 +69,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -79,7 +82,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -90,7 +94,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -101,7 +106,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -112,7 +118,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final BarTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -123,7 +130,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -134,7 +142,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -145,7 +154,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } diff --git a/lib/src/chart/line_chart/line_chart.dart b/lib/src/chart/line_chart/line_chart.dart index 40c0c8162..7f84b4f77 100644 --- a/lib/src/chart/line_chart/line_chart.dart +++ b/lib/src/chart/line_chart/line_chart.dart @@ -50,7 +50,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -61,7 +62,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -72,7 +74,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -84,7 +87,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -95,7 +99,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -106,7 +111,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -117,7 +123,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final LineTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -128,7 +135,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -139,7 +147,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -150,7 +159,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -158,7 +168,8 @@ class _LineChartState extends AnimatedWidgetBaseState { child: CustomPaint( key: _chartKey, size: getDefaultSize(MediaQuery.of(context).size), - painter: LineChartPainter(_withTouchedIndicators(_lineChartDataTween.evaluate(animation)), _withTouchedIndicators(showingData), (touchHandler) { + painter: LineChartPainter(_withTouchedIndicators(_lineChartDataTween.evaluate(animation)), + _withTouchedIndicators(showingData), (touchHandler) { setState(() { _touchHandler = touchHandler; }); diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index cad51281c..2b78b7e4a 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -67,7 +67,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -78,7 +79,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -89,7 +91,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -100,7 +103,8 @@ class _PieChartState extends AnimatedWidgetBaseState { if (chartSize == null) { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -111,7 +115,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -122,7 +127,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -133,7 +139,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -144,7 +151,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -155,7 +163,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -166,7 +175,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } From 2c5a3dcb70c13415814c15591a26fcb9aa2c98f7 Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 16 Jan 2021 16:37:04 +0530 Subject: [PATCH 11/22] Fix deprecated constructor warning in bar_chart_painter Signed-off-by: Shripal Jain --- lib/src/chart/bar_chart/bar_chart_painter.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/chart/bar_chart/bar_chart_painter.dart b/lib/src/chart/bar_chart/bar_chart_painter.dart index a30580250..df0c54d76 100644 --- a/lib/src/chart/bar_chart/bar_chart_painter.dart +++ b/lib/src/chart/bar_chart/bar_chart_painter.dart @@ -75,7 +75,7 @@ class BarChartPainter extends AxisChartPainter with TouchHandler barGroups, BarChartAlignment alignment) { final Size drawSize = getChartUsableDrawSize(viewSize); - final List groupsX = List(barGroups.length); + final List groupsX = List.filled(barGroups.length, 0, growable: false); final double leftTextsSpace = getLeftOffsetDrawSize(); From af51913541490175ab42f8f3d424ef9562b01c71 Mon Sep 17 00:00:00 2001 From: Ayush <4429609+mathmetal@users.noreply.github.com> Date: Mon, 18 Jan 2021 07:38:47 +0530 Subject: [PATCH 12/22] Correct typo in field name --- repo_files/documentations/bar_chart.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 + From e72a1aa6a660bda1754dedf90e39159e52c56afb Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Fri, 25 Dec 2020 01:29:42 +0330 Subject: [PATCH 13/22] Add pedantic and resolve all analyze issues --- analysis_options.yaml | 139 +-------- lib/src/chart/bar_chart/bar_chart.dart | 22 +- lib/src/chart/bar_chart/bar_chart_data.dart | 18 +- .../chart/bar_chart/bar_chart_painter.dart | 209 +++++++------ .../base/axis_chart/axis_chart_painter.dart | 103 ++++--- .../base/base_chart/base_chart_painter.dart | 8 +- lib/src/chart/line_chart/line_chart.dart | 20 +- lib/src/chart/line_chart/line_chart_data.dart | 22 +- .../chart/line_chart/line_chart_painter.dart | 281 +++++++++--------- lib/src/chart/pie_chart/pie_chart.dart | 28 +- .../chart/pie_chart/pie_chart_painter.dart | 87 +++--- .../chart/scatter_chart/scatter_chart.dart | 35 +-- .../scatter_chart/scatter_chart_data.dart | 6 +- .../scatter_chart/scatter_chart_painter.dart | 87 +++--- lib/src/extensions/canvas_extension.dart | 2 +- lib/src/utils/utils.dart | 8 +- pubspec.yaml | 3 + 17 files changed, 464 insertions(+), 614 deletions(-) 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/lib/src/chart/bar_chart/bar_chart.dart b/lib/src/chart/bar_chart/bar_chart.dart index 3daa7d65e..b029b65b5 100644 --- a/lib/src/chart/bar_chart/bar_chart.dart +++ b/lib/src/chart/bar_chart/bar_chart.dart @@ -35,12 +35,12 @@ 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(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -52,7 +52,7 @@ class _BarChartState extends AnimatedWidgetBaseState { } }, onLongPressEnd: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -64,7 +64,7 @@ class _BarChartState extends AnimatedWidgetBaseState { } }, onLongPressMoveUpdate: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -76,7 +76,7 @@ class _BarChartState extends AnimatedWidgetBaseState { } }, onPanCancel: () { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -88,7 +88,7 @@ class _BarChartState extends AnimatedWidgetBaseState { } }, onPanEnd: (DragEndDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -100,7 +100,7 @@ class _BarChartState extends AnimatedWidgetBaseState { } }, onPanDown: (DragDownDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -112,7 +112,7 @@ class _BarChartState extends AnimatedWidgetBaseState { } }, onPanUpdate: (DragUpdateDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -153,8 +153,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..4c5efc2f5 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(barGroups.length); - 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..58a4f1602 100644 --- a/lib/src/chart/base/axis_chart/axis_chart_painter.dart +++ b/lib/src/chart/base/axis_chart/axis_chart_painter.dart @@ -56,8 +56,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 +73,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 +87,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 +106,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 +126,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 +149,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 +206,27 @@ 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; - 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 +236,28 @@ 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; - 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 +274,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 +293,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 +313,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 +334,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 +347,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..860336f7c 100644 --- a/lib/src/chart/base/base_chart/base_chart_painter.dart +++ b/lib/src/chart/base/base_chart/base_chart_painter.dart @@ -41,7 +41,7 @@ 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; @@ -49,7 +49,7 @@ abstract class BaseChartPainter extends CustomPainter { } /// 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; @@ -57,7 +57,7 @@ abstract class BaseChartPainter extends CustomPainter { } /// 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; @@ -65,7 +65,7 @@ abstract class BaseChartPainter extends CustomPainter { } /// 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; diff --git a/lib/src/chart/line_chart/line_chart.dart b/lib/src/chart/line_chart/line_chart.dart index 5076bed1d..84a3af4fa 100644 --- a/lib/src/chart/line_chart/line_chart.dart +++ b/lib/src/chart/line_chart/line_chart.dart @@ -40,12 +40,12 @@ 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(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -57,7 +57,7 @@ class _LineChartState extends AnimatedWidgetBaseState { } }, onLongPressEnd: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -69,7 +69,7 @@ class _LineChartState extends AnimatedWidgetBaseState { } }, onLongPressMoveUpdate: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -81,7 +81,7 @@ class _LineChartState extends AnimatedWidgetBaseState { } }, onPanCancel: () { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -93,7 +93,7 @@ class _LineChartState extends AnimatedWidgetBaseState { } }, onPanEnd: (DragEndDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -105,7 +105,7 @@ class _LineChartState extends AnimatedWidgetBaseState { } }, onPanDown: (DragDownDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -117,7 +117,7 @@ class _LineChartState extends AnimatedWidgetBaseState { } }, onPanUpdate: (DragUpdateDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -197,7 +197,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..a322b2097 100644 --- a/lib/src/chart/line_chart/line_chart_painter.dart +++ b/lib/src/chart/line_chart/line_chart_painter.dart @@ -85,7 +85,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 +94,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 +119,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 +145,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 +172,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 +192,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 +212,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 +240,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 +263,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,7 +300,7 @@ 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; @@ -339,19 +337,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 +407,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 +459,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 +498,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 +543,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 +585,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 @@ -638,7 +636,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 +678,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), ); @@ -723,7 +721,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 +802,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 { @@ -849,17 +847,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 +886,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 +926,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 +967,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,16 +1008,15 @@ 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; @@ -1030,10 +1024,10 @@ class LineChartPainter extends AxisChartPainter 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 +1036,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,13 +1073,12 @@ 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; @@ -1093,10 +1086,10 @@ class LineChartPainter extends AxisChartPainter 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 +1097,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 +1142,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 +1178,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 +1191,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 +1207,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 +1258,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 +1298,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 +1320,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 +1373,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 +1396,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..7134ade44 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -57,12 +57,12 @@ class _PieChartState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { - final PieChartData showingData = _getData(); - final PieTouchData touchData = showingData.pieTouchData; + final showingData = _getData(); + final touchData = showingData.pieTouchData; return GestureDetector( onLongPressStart: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -73,7 +73,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } }, onLongPressEnd: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -85,7 +85,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } }, onLongPressMoveUpdate: (d) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -97,7 +97,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } }, onPanCancel: () { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -109,7 +109,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } }, onPanEnd: (DragEndDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -121,7 +121,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } }, onPanDown: (DragDownDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -133,7 +133,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } }, onPanUpdate: (DragUpdateDetails details) { - final Size chartSize = _getChartSize(); + final chartSize = _getChartSize(); if (chartSize == null) { return; } @@ -180,7 +180,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 +189,7 @@ class _PieChartState extends AnimatedWidgetBaseState { ); } - final Widget _badgeWidget = _getData().sections[_key].badgeWidget; + final _badgeWidget = _getData().sections[_key].badgeWidget; if (_badgeWidget == null) { return LayoutId( @@ -251,10 +251,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/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..86856b7e6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,10 @@ dependencies: path_drawing: ^0.4.1 equatable: ^1.1.1 + pedantic: ^1.9.0 dev_dependencies: flutter_test: sdk: flutter + + From 7e898b962b4b10964534614f7d5e705929ee5992 Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Fri, 22 Jan 2021 23:49:16 +0330 Subject: [PATCH 14/22] Add guide for switch to `dev` branch in CONTRIBUTING.md --- CONTRIBUTING.md | 7 +++++++ 1 file changed, 7 insertions(+) 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). From c48d8ddc11f6a93c837b2d3a5c2fea7dad113b5a Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Sat, 23 Jan 2021 14:25:12 +0530 Subject: [PATCH 15/22] Format changed files Signed-off-by: Shripal Jain --- lib/src/chart/bar_chart/bar_chart.dart | 30 ++++++++++++++------- lib/src/chart/line_chart/line_chart.dart | 33 ++++++++++++++++-------- lib/src/chart/pie_chart/pie_chart.dart | 30 ++++++++++++++------- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/lib/src/chart/bar_chart/bar_chart.dart b/lib/src/chart/bar_chart/bar_chart.dart index 6a8dff26e..4238d6e3a 100644 --- a/lib/src/chart/bar_chart/bar_chart.dart +++ b/lib/src/chart/bar_chart/bar_chart.dart @@ -45,7 +45,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -56,7 +57,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final BarTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -67,7 +69,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -79,7 +82,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -90,7 +94,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -101,7 +106,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -112,7 +118,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final BarTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -123,7 +130,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -134,7 +142,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -145,7 +154,8 @@ class _BarChartState extends AnimatedWidgetBaseState { return; } - final BarTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + final BarTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } diff --git a/lib/src/chart/line_chart/line_chart.dart b/lib/src/chart/line_chart/line_chart.dart index ff6f1894b..9b132d9ff 100644 --- a/lib/src/chart/line_chart/line_chart.dart +++ b/lib/src/chart/line_chart/line_chart.dart @@ -50,7 +50,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -61,7 +62,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, Velocity.zero), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -72,7 +74,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -84,7 +87,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -95,7 +99,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -106,7 +111,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -117,7 +123,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final LineTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -128,7 +135,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -139,7 +147,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -150,7 +159,8 @@ class _LineChartState extends AnimatedWidgetBaseState { return; } - final LineTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + final LineTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -158,7 +168,8 @@ class _LineChartState extends AnimatedWidgetBaseState { child: CustomPaint( key: _chartKey, size: getDefaultSize(MediaQuery.of(context).size), - painter: LineChartPainter(_withTouchedIndicators(_lineChartDataTween.evaluate(animation)), _withTouchedIndicators(showingData), (touchHandler) { + painter: LineChartPainter(_withTouchedIndicators(_lineChartDataTween.evaluate(animation)), + _withTouchedIndicators(showingData), (touchHandler) { setState(() { _touchHandler = touchHandler; }); diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index 5bf8db9b0..7e4804372 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -67,7 +67,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -78,7 +79,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -89,7 +91,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(e.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -100,7 +103,8 @@ class _PieChartState extends AnimatedWidgetBaseState { if (chartSize == null) { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressStart(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -111,7 +115,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressEnd(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -122,7 +127,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlLongPressMoveUpdate(d.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -133,7 +139,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); + final PieTouchResponse response = _touchHandler?.handleTouch( + FlPanEnd(Offset.zero, const Velocity(pixelsPerSecond: Offset.zero)), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -144,7 +151,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanEnd(Offset.zero, details.velocity), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -155,7 +163,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanStart(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } @@ -166,7 +175,8 @@ class _PieChartState extends AnimatedWidgetBaseState { return; } - final PieTouchResponse response = _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); + final PieTouchResponse response = + _touchHandler?.handleTouch(FlPanMoveUpdate(details.localPosition), chartSize); if (_canHandleTouch(response, touchData)) { touchData.touchCallback(response); } From 8f97298a5b3e478eb48e1c53636e49ae8c30e9b0 Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Mon, 25 Jan 2021 13:09:29 +0530 Subject: [PATCH 16/22] Revert "bar_chart_painter.dart" Signed-off-by: Shripal Jain --- .../chart/bar_chart/bar_chart_painter.dart | 1591 +++++++++-------- 1 file changed, 796 insertions(+), 795 deletions(-) diff --git a/lib/src/chart/bar_chart/bar_chart_painter.dart b/lib/src/chart/bar_chart/bar_chart_painter.dart index 59fa2fca4..a30580250 100644 --- a/lib/src/chart/bar_chart/bar_chart_painter.dart +++ b/lib/src/chart/bar_chart/bar_chart_painter.dart @@ -1,795 +1,796 @@ -import 'dart:math'; -import 'dart:ui' as ui; - -import 'package:fl_chart/fl_chart.dart'; -import 'package:fl_chart/src/chart/bar_chart/bar_chart_data.dart'; -import 'package:fl_chart/src/chart/base/axis_chart/axis_chart_painter.dart'; -import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart'; -import 'package:fl_chart/src/utils/canvas_wrapper.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter/widgets.dart'; - -import '../../utils/utils.dart'; - -/// Paints [BarChartData] in the canvas, it can be used in a [CustomPainter] -class BarChartPainter extends AxisChartPainter with TouchHandler { - Paint _barPaint, _bgTouchTooltipPaint; - - List<_GroupBarsPosition> _groupBarsPosition; - - /// Paints [data] into canvas, it is the animating [BarChartData], - /// [targetData] is the animation's target and remains the same - /// during animation, then we should use it when we need to show - /// tooltips or something like that, because [data] is changing constantly. - /// - /// [touchHandler] passes a [TouchHandler] to the parent, - /// parent will use it for touch handling flow. - /// - /// [textScale] used for scaling texts inside the chart, - /// parent can use [MediaQuery.textScaleFactor] to respect - /// the system's font size. - BarChartPainter(BarChartData data, BarChartData targetData, Function(TouchHandler) touchHandler, - {double textScale = 1}) - : super(data, targetData, textScale: textScale) { - touchHandler(this); - _barPaint = Paint()..style = PaintingStyle.fill; - - _bgTouchTooltipPaint = Paint() - ..style = PaintingStyle.fill - ..color = Colors.white; - } - - /// Paints [BarChartData] into the provided canvas. - @override - void paint(Canvas canvas, Size size) { - super.paint(canvas, size); - final canvasWrapper = CanvasWrapper(canvas, size); - - if (data.barGroups.isEmpty) { - return; - } - - final groupsX = _calculateGroupsX(size, data.barGroups, data.alignment); - _groupBarsPosition = _calculateGroupAndBarsPosition(size, groupsX, data.barGroups); - - _drawBars(canvasWrapper, _groupBarsPosition); - drawAxisTitles(canvasWrapper); - _drawTitles(canvasWrapper, _groupBarsPosition); - - for (var i = 0; i < targetData.barGroups.length; i++) { - final barGroup = targetData.barGroups[i]; - for (var j = 0; j < barGroup.barRods.length; j++) { - if (!barGroup.showingTooltipIndicators.contains(j)) { - continue; - } - final barRod = barGroup.barRods[j]; - - _drawTouchTooltip(canvasWrapper, _groupBarsPosition, - targetData.barTouchData.touchTooltipData, barGroup, i, barRod, j); - } - } - } - - /// Calculates groups position for showing in the x axis using [alignment]. - List _calculateGroupsX( - Size viewSize, List barGroups, BarChartAlignment alignment) { - final drawSize = getChartUsableDrawSize(viewSize); - - final groupsX = List(barGroups.length); - - final leftTextsSpace = getLeftOffsetDrawSize(); - - switch (alignment) { - case BarChartAlignment.start: - var tempX = 0.0; - barGroups.asMap().forEach((i, group) { - groupsX[i] = leftTextsSpace + tempX + group.width / 2; - tempX += group.width; - }); - break; - - case BarChartAlignment.end: - 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; - } - break; - - case BarChartAlignment.center: - var sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); - sumWidth += data.groupsSpace * (barGroups.length - 1); - final horizontalMargin = (drawSize.width - sumWidth) / 2; - - 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 groupSpace = i == barGroups.length - 1 ? 0 : data.groupsSpace; - tempX += group.width + groupSpace; - } - break; - - case BarChartAlignment.spaceBetween: - final sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); - final spaceAvailable = drawSize.width - sumWidth; - final eachSpace = spaceAvailable / (barGroups.length - 1); - - var tempX = 0.0; - barGroups.asMap().forEach((index, group) { - tempX += group.width / 2; - if (index != 0) { - tempX += eachSpace; - } - groupsX[index] = leftTextsSpace + tempX; - tempX += group.width / 2; - }); - break; - - case BarChartAlignment.spaceAround: - final sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); - final spaceAvailable = drawSize.width - sumWidth; - final eachSpace = spaceAvailable / (barGroups.length * 2); - - var tempX = 0.0; - barGroups.asMap().forEach((i, group) { - tempX += eachSpace; - tempX += group.width / 2; - groupsX[i] = leftTextsSpace + tempX; - tempX += group.width / 2; - tempX += eachSpace; - }); - break; - - case BarChartAlignment.spaceEvenly: - final sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); - final spaceAvailable = drawSize.width - sumWidth; - final eachSpace = spaceAvailable / (barGroups.length + 1); - - var tempX = 0.0; - barGroups.asMap().forEach((i, group) { - tempX += eachSpace; - tempX += group.width / 2; - groupsX[i] = leftTextsSpace + tempX; - tempX += group.width / 2; - }); - break; - } - - return groupsX; - } - - /// Calculates bars position alongside group positions. - List<_GroupBarsPosition> _calculateGroupAndBarsPosition( - Size viewSize, List groupsX, List barGroups) { - if (groupsX.length != barGroups.length) { - throw Exception('inconsistent state groupsX.length != barGroups.length'); - } - - final groupBarsPosition = <_GroupBarsPosition>[]; - for (var i = 0; i < barGroups.length; i++) { - final barGroup = barGroups[i]; - final groupX = groupsX[i]; - - var tempX = 0.0; - final barsX = []; - barGroup.barRods.asMap().forEach((barIndex, barRod) { - final widthHalf = barRod.width / 2; - barsX.add(groupX - (barGroup.width / 2) + tempX + widthHalf); - tempX += barRod.width + barGroup.barsSpace; - }); - groupBarsPosition.add(_GroupBarsPosition(groupX, barsX)); - } - return groupBarsPosition; - } - - void _drawBars(CanvasWrapper canvasWrapper, List<_GroupBarsPosition> groupBarsPosition) { - final viewSize = canvasWrapper.size; - final drawSize = getChartUsableDrawSize(viewSize); - - for (var i = 0; i < data.barGroups.length; i++) { - final barGroup = data.barGroups[i]; - for (var j = 0; j < barGroup.barRods.length; j++) { - final barRod = barGroup.barRods[j]; - final widthHalf = barRod.width / 2; - final borderRadius = barRod.borderRadius ?? BorderRadius.circular(barRod.width / 2); - - final x = groupBarsPosition[i].barsX[j]; - - final left = x - widthHalf; - final right = x + widthHalf; - final cornerHeight = max(borderRadius.topLeft.y, borderRadius.topRight.y) + - max(borderRadius.bottomLeft.y, borderRadius.bottomRight.y); - - RRect barRRect; - - /// Draw [BackgroundBarChartRodData] - if (barRod.backDrawRodData.show && barRod.backDrawRodData.y != 0) { - if (barRod.backDrawRodData.y > 0) { - // positive - final bottom = getPixelY(max(data.minY, 0), drawSize); - final top = min(getPixelY(barRod.backDrawRodData.y, drawSize), bottom - cornerHeight); - - barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, - topLeft: borderRadius.topLeft, - topRight: borderRadius.topRight, - bottomLeft: borderRadius.bottomLeft, - bottomRight: borderRadius.bottomRight); - } else { - // negative - final top = getPixelY(min(data.maxY, 0), drawSize); - final bottom = max(getPixelY(barRod.backDrawRodData.y, drawSize), top + cornerHeight); - - barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, - topLeft: borderRadius.topLeft, - topRight: borderRadius.topRight, - bottomLeft: borderRadius.bottomLeft, - bottomRight: borderRadius.bottomRight); - } - - if (barRod.backDrawRodData.colors.length == 1) { - _barPaint.color = barRod.backDrawRodData.colors[0]; - _barPaint.shader = null; - } else { - final from = barRod.backDrawRodData.gradientFrom; - final to = barRod.backDrawRodData.gradientTo; - - var stops = []; - if (barRod.backDrawRodData.colorStops == null || - barRod.backDrawRodData.colorStops.length != barRod.backDrawRodData.colors.length) { - /// provided colorStops is invalid and we calculate it here - barRod.backDrawRodData.colors.asMap().forEach((index, color) { - final percent = 1.0 / barRod.backDrawRodData.colors.length; - stops.add(percent * index); - }); - } else { - stops = barRod.backDrawRodData.colorStops; - } - - _barPaint.shader = ui.Gradient.linear( - Offset( - getLeftOffsetDrawSize() + (drawSize.width * from.dx), - getTopOffsetDrawSize() + (drawSize.height * from.dy), - ), - Offset( - getLeftOffsetDrawSize() + (drawSize.width * to.dx), - getTopOffsetDrawSize() + (drawSize.height * to.dy), - ), - barRod.backDrawRodData.colors, - stops, - ); - } - - canvasWrapper.drawRRect(barRRect, _barPaint); - } - - // draw Main Rod - if (barRod.y != 0) { - if (barRod.y > 0) { - // positive - final bottom = getPixelY(max(data.minY, 0), drawSize); - final top = min(getPixelY(barRod.y, drawSize), bottom - cornerHeight); - - barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, - topLeft: borderRadius.topLeft, - topRight: borderRadius.topRight, - bottomLeft: borderRadius.bottomLeft, - bottomRight: borderRadius.bottomRight); - } else { - // negative - final top = getPixelY(min(data.maxY, 0), drawSize); - final bottom = max(getPixelY(barRod.y, drawSize), top + cornerHeight); - - barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, - topLeft: borderRadius.topLeft, - topRight: borderRadius.topRight, - bottomLeft: borderRadius.bottomLeft, - bottomRight: borderRadius.bottomRight); - } - if (barRod.colors.length == 1) { - _barPaint.color = barRod.colors[0]; - _barPaint.shader = null; - } else { - final from = barRod.gradientFrom; - final to = barRod.gradientTo; - - 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) { - final percent = 1.0 / barRod.colors.length; - stops.add(percent * index); - }); - } else { - stops = barRod.colorStops; - } - - _barPaint.shader = ui.Gradient.linear( - Offset( - getLeftOffsetDrawSize() + (drawSize.width * from.dx), - getTopOffsetDrawSize() + (drawSize.height * from.dy), - ), - Offset( - getLeftOffsetDrawSize() + (drawSize.width * to.dx), - getTopOffsetDrawSize() + (drawSize.height * to.dy), - ), - barRod.colors, - stops, - ); - } - canvasWrapper.drawRRect(barRRect, _barPaint); - - // draw rod stack - if (barRod.rodStackItems != null && barRod.rodStackItems.isNotEmpty) { - for (var i = 0; i < barRod.rodStackItems.length; i++) { - final stackItem = barRod.rodStackItems[i]; - final stackFromY = getPixelY(stackItem.fromY, drawSize); - final stackToY = getPixelY(stackItem.toY, drawSize); - - _barPaint.color = stackItem.color; - canvasWrapper.save(); - canvasWrapper.clipRect(Rect.fromLTRB(left, stackToY, right, stackFromY)); - canvasWrapper.drawRRect(barRRect, _barPaint); - canvasWrapper.restore(); - } - } - } - } - } - } - - void _drawTitles(CanvasWrapper canvasWrapper, List<_GroupBarsPosition> groupBarsPosition) { - if (!targetData.titlesData.show) { - return; - } - final viewSize = canvasWrapper.size; - final drawSize = getChartUsableDrawSize(viewSize); - - // Left Titles - final leftTitles = targetData.titlesData.leftTitles; - final leftInterval = - leftTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); - if (leftTitles.showTitles) { - var verticalSeek = data.minY; - while (verticalSeek <= data.maxY) { - if (leftTitles.checkToShowTitle( - data.minY, data.maxY, leftTitles, leftInterval, verticalSeek)) { - var x = 0 + getLeftOffsetDrawSize(); - var y = getPixelY(verticalSeek, drawSize); - - final text = leftTitles.getTitles(verticalSeek); - - final span = TextSpan(style: leftTitles.getTextStyles(verticalSeek), text: text); - final tp = TextPainter( - text: span, - textAlign: TextAlign.center, - textDirection: TextDirection.ltr, - textScaleFactor: textScale); - tp.layout(maxWidth: getExtraNeededHorizontalSpace()); - x -= tp.width + leftTitles.margin; - y -= tp.height / 2; - canvasWrapper.save(); - canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); - canvasWrapper.rotate(radians(leftTitles.rotateAngle)); - canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); - y -= translateRotatedPosition(tp.width, leftTitles.rotateAngle); - canvasWrapper.drawText(tp, Offset(x, y)); - canvasWrapper.restore(); - } - if (data.maxY - verticalSeek < leftInterval && data.maxY != verticalSeek) { - verticalSeek = data.maxY; - } else { - verticalSeek += leftInterval; - } - } - } - - // Top Titles - final topTitles = targetData.titlesData.topTitles; - if (topTitles.showTitles) { - for (var index = 0; index < groupBarsPosition.length; index++) { - final groupBarPos = groupBarsPosition[index]; - - final xValue = data.barGroups[index].x.toDouble(); - final text = topTitles.getTitles(xValue); - final span = TextSpan(style: topTitles.getTextStyles(xValue), text: text); - final tp = TextPainter( - text: span, - textAlign: TextAlign.center, - textDirection: TextDirection.ltr, - textScaleFactor: textScale); - tp.layout(); - var x = groupBarPos.groupX; - const y = 0.0; - - x -= tp.width / 2; - canvasWrapper.save(); - canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); - canvasWrapper.rotate(radians(topTitles.rotateAngle)); - canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); - x += translateRotatedPosition(tp.width, topTitles.rotateAngle); - canvasWrapper.drawText(tp, Offset(x, y)); - canvasWrapper.restore(); - } - } - - // Right Titles - final rightTitles = targetData.titlesData.rightTitles; - final rightInterval = - rightTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); - if (rightTitles.showTitles) { - var verticalSeek = data.minY; - while (verticalSeek <= data.maxY) { - if (rightTitles.checkToShowTitle( - data.minY, data.maxY, rightTitles, rightInterval, verticalSeek)) { - var x = drawSize.width + getLeftOffsetDrawSize(); - var y = getPixelY(verticalSeek, drawSize); - - final text = rightTitles.getTitles(verticalSeek); - - final span = TextSpan(style: rightTitles.getTextStyles(verticalSeek), text: text); - final tp = TextPainter( - text: span, - textAlign: TextAlign.center, - textDirection: TextDirection.ltr, - textScaleFactor: textScale); - tp.layout(maxWidth: getExtraNeededHorizontalSpace()); - x += rightTitles.margin; - y -= tp.height / 2; - canvasWrapper.save(); - canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); - canvasWrapper.rotate(radians(rightTitles.rotateAngle)); - canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); - y += translateRotatedPosition(tp.width, leftTitles.rotateAngle); - canvasWrapper.drawText(tp, Offset(x, y)); - canvasWrapper.restore(); - } - if (data.maxY - verticalSeek < rightInterval && data.maxY != verticalSeek) { - verticalSeek = data.maxY; - } else { - verticalSeek += rightInterval; - } - } - } - - // Bottom titles - final bottomTitles = targetData.titlesData.bottomTitles; - if (bottomTitles.showTitles) { - for (var index = 0; index < groupBarsPosition.length; index++) { - final groupBarPos = groupBarsPosition[index]; - - final xValue = data.barGroups[index].x.toDouble(); - final text = bottomTitles.getTitles(xValue); - // ignore: omit_local_variable_types - final span = TextSpan(style: bottomTitles.getTextStyles(xValue), text: text); - final tp = TextPainter( - text: span, - textAlign: TextAlign.center, - textDirection: TextDirection.ltr, - textScaleFactor: textScale); - tp.layout(); - var x = groupBarPos.groupX; - final y = drawSize.height + getTopOffsetDrawSize() + bottomTitles.margin; - - x -= tp.width / 2; - canvasWrapper.save(); - canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); - canvasWrapper.rotate(radians(bottomTitles.rotateAngle)); - canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); - x += translateRotatedPosition(tp.width, bottomTitles.rotateAngle); - canvasWrapper.drawText(tp, Offset(x, y)); - canvasWrapper.restore(); - } - } - } - - void _drawTouchTooltip( - CanvasWrapper canvasWrapper, - List<_GroupBarsPosition> groupPositions, - BarTouchTooltipData tooltipData, - BarChartGroupData showOnBarGroup, - int barGroupIndex, - BarChartRodData showOnRodData, - int barRodIndex, - ) { - final viewSize = canvasWrapper.size; - final chartUsableSize = getChartUsableDrawSize(viewSize); - - const textsBelowMargin = 4; - - final tooltipItem = tooltipData.getTooltipItem( - showOnBarGroup, - barGroupIndex, - showOnRodData, - barRodIndex, - ); - - if (tooltipItem == null) { - return; - } - - final span = TextSpan(style: tooltipItem.textStyle, text: tooltipItem.text); - final tp = TextPainter( - text: span, - textAlign: TextAlign.center, - textDirection: TextDirection.ltr, - textScaleFactor: textScale); - tp.layout(maxWidth: tooltipData.maxContentWidth); - - /// creating TextPainters to calculate the width and height of the tooltip - final drawingTextPainter = tp; - - /// biggerWidth - /// some texts maybe larger, then we should - /// draw the tooltip' width as wide as biggerWidth - /// - /// sumTextsHeight - /// sum up all Texts height, then we should - /// draw the tooltip's height as tall as sumTextsHeight - final textWidth = drawingTextPainter.width; - final textHeight = drawingTextPainter.height + textsBelowMargin; - - /// 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 barOffset = Offset( - groupPositions[barGroupIndex].barsX[barRodIndex], - getPixelY(showOnRodData.y, chartUsableSize), - ); - - final isPositive = showOnRodData.y > 0; - - final tooltipWidth = textWidth + tooltipData.tooltipPadding.horizontal; - final tooltipHeight = textHeight + tooltipData.tooltipPadding.vertical; - - 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); - - if (tooltipData.fitInsideHorizontally) { - if (rect.left < 0) { - final shiftAmount = 0 - rect.left; - rect = Rect.fromLTRB( - rect.left + shiftAmount, - rect.top, - rect.right + shiftAmount, - rect.bottom, - ); - } - - if (rect.right > viewSize.width) { - final shiftAmount = rect.right - viewSize.width; - rect = Rect.fromLTRB( - rect.left - shiftAmount, - rect.top, - rect.right - shiftAmount, - rect.bottom, - ); - } - } - - if (tooltipData.fitInsideVertically) { - if (rect.top < 0) { - final shiftAmount = 0 - rect.top; - rect = Rect.fromLTRB( - rect.left, - rect.top + shiftAmount, - rect.right, - rect.bottom + shiftAmount, - ); - } - - if (rect.bottom > viewSize.height) { - final shiftAmount = rect.bottom - viewSize.height; - rect = Rect.fromLTRB( - rect.left, - rect.top - shiftAmount, - rect.right, - rect.bottom - shiftAmount, - ); - } - } - - 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 - final top = tooltipData.tooltipPadding.top; - final drawOffset = Offset( - rect.center.dx - (tp.width / 2), - rect.topCenter.dy + top, - ); - canvasWrapper.drawText(tp, drawOffset); - } - - /// We add our needed horizontal space to parent needed. - /// we have some titles that maybe draw in the left and right side of our chart, - /// then we should draw the chart a with some left space, - /// the left space is [getLeftOffsetDrawSize], - /// and the whole space is [getExtraNeededHorizontalSpace] - @override - double getExtraNeededHorizontalSpace() { - var sum = super.getExtraNeededHorizontalSpace(); - if (data.titlesData.show) { - final leftSide = data.titlesData.leftTitles; - if (leftSide.showTitles) { - sum += leftSide.reservedSize + leftSide.margin; - } - - final rightSide = data.titlesData.rightTitles; - if (rightSide.showTitles) { - sum += rightSide.reservedSize + rightSide.margin; - } - } - return sum; - } - - /// We add our needed vertical space to parent needed. - /// we have some titles that maybe draw in the top and bottom side of our chart, - /// then we should draw the chart a with some top space, - /// the top space is [getTopOffsetDrawSize()], - /// and the whole space is [getExtraNeededVerticalSpace] - @override - double getExtraNeededVerticalSpace() { - var sum = super.getExtraNeededVerticalSpace(); - if (data.titlesData.show) { - final bottomSide = data.titlesData.bottomTitles; - if (bottomSide.showTitles) { - sum += bottomSide.reservedSize + bottomSide.margin; - } - - final topSide = data.titlesData.topTitles; - if (topSide.showTitles) { - sum += topSide.reservedSize + topSide.margin; - } - } - return sum; - } - - /// calculate left offset for draw the chart, - /// maybe we want to show both left and right titles, - /// then just the left titles will effect on this function. - @override - double getLeftOffsetDrawSize() { - var sum = super.getLeftOffsetDrawSize(); - - final leftTitles = data.titlesData.leftTitles; - if (data.titlesData.show && leftTitles.showTitles) { - sum += leftTitles.reservedSize + leftTitles.margin; - } - - return sum; - } - - /// calculate top offset for draw the chart, - /// maybe we want to show both top and bottom titles, - /// then just the top titles will effect on this function. - @override - double getTopOffsetDrawSize() { - var sum = super.getTopOffsetDrawSize(); - - final topTitles = data.titlesData.topTitles; - if (data.titlesData.show && topTitles.showTitles) { - sum += topTitles.reservedSize + topTitles.margin; - } - - return sum; - } - - /// Makes a [BarTouchResponse] based on the provided [FlTouchInput] - /// - /// Processes [FlTouchInput.getOffset] and checks - /// the elements of the chart that are near the offset, - /// then makes a [BarTouchResponse] from the elements that has been touched. - @override - BarTouchResponse handleTouch(FlTouchInput touchInput, Size size) { - final touchedSpot = _getNearestTouchedSpot(size, touchInput.getOffset(), _groupBarsPosition); - return BarTouchResponse(touchedSpot, touchInput); - } - - /// find the nearest spot base on the touched offset - BarTouchedSpot _getNearestTouchedSpot( - Size viewSize, Offset touchedPoint, List<_GroupBarsPosition> groupBarsPosition) { - if (groupBarsPosition == null) { - final groupsX = _calculateGroupsX(viewSize, data.barGroups, data.alignment); - groupBarsPosition = _calculateGroupAndBarsPosition(viewSize, groupsX, data.barGroups); - } - - final chartViewSize = getChartUsableDrawSize(viewSize); - - /// Find the nearest barRod - 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 isPositive = targetData.barGroups[i].barRods[j].y > 0; - if (isPositive) { - barTopY = getPixelY(targetData.barGroups[i].barRods[j].y, chartViewSize); - barBotY = getPixelY(0, chartViewSize); - } else { - barTopY = getPixelY(0, chartViewSize); - barBotY = getPixelY(targetData.barGroups[i].barRods[j].y, chartViewSize); - } - - final backDrawBarY = - getPixelY(targetData.barGroups[i].barRods[j].backDrawRodData.y, chartViewSize); - final touchExtraThreshold = targetData.barTouchData.touchExtraThreshold; - - final isXInTouchBounds = - (touchedPoint.dx <= barX + halfBarWidth + touchExtraThreshold.right) && - (touchedPoint.dx >= barX - halfBarWidth - touchExtraThreshold.left); - - final isYInBarBounds = (touchedPoint.dy <= barBotY + touchExtraThreshold.bottom) && - (touchedPoint.dy >= barTopY - touchExtraThreshold.top); - - bool isYInBarBackDrawBounds; - if (isPositive) { - isYInBarBackDrawBounds = (touchedPoint.dy <= barBotY + touchExtraThreshold.bottom) && - (touchedPoint.dy >= backDrawBarY - touchExtraThreshold.top); - } else { - isYInBarBackDrawBounds = (touchedPoint.dy >= barTopY - touchExtraThreshold.top) && - (touchedPoint.dy <= backDrawBarY + touchExtraThreshold.bottom); - } - - final isYInTouchBounds = - (targetData.barTouchData.allowTouchBarBackDraw && isYInBarBackDrawBounds) || - isYInBarBounds; - - if (isXInTouchBounds && isYInTouchBounds) { - final nearestGroup = targetData.barGroups[i]; - final nearestBarRod = nearestGroup.barRods[j]; - final nearestSpot = FlSpot(nearestGroup.x.toDouble(), nearestBarRod.y); - final nearestSpotPos = Offset(barX, getPixelY(nearestSpot.y, chartViewSize)); - - var touchedStackIndex = -1; - BarChartRodStackItem touchedStack; - for (var stackIndex = 0; stackIndex < nearestBarRod.rodStackItems.length; stackIndex++) { - final stackItem = nearestBarRod.rodStackItems[stackIndex]; - final fromPixel = getPixelY(stackItem.fromY, chartViewSize); - final toPixel = getPixelY(stackItem.toY, chartViewSize); - if (touchedPoint.dy <= fromPixel && touchedPoint.dy >= toPixel) { - touchedStackIndex = stackIndex; - touchedStack = stackItem; - break; - } - } - - return BarTouchedSpot(nearestGroup, i, nearestBarRod, j, touchedStack, touchedStackIndex, - nearestSpot, nearestSpotPos); - } - } - } - - return null; - } - - /// Determines should it redraw the chart or not. - /// - /// If there is a change in the [BarChartData], - /// [BarChartPainter] should repaint itself. - @override - bool shouldRepaint(BarChartPainter oldDelegate) => oldDelegate.data != data; -} - -class _GroupBarsPosition { - final double groupX; - final List barsX; - - _GroupBarsPosition(this.groupX, this.barsX); -} +import 'dart:math'; +import 'dart:ui' as ui; + +import 'package:fl_chart/fl_chart.dart'; +import 'package:fl_chart/src/chart/bar_chart/bar_chart_data.dart'; +import 'package:fl_chart/src/chart/base/axis_chart/axis_chart_painter.dart'; +import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart'; +import 'package:fl_chart/src/utils/canvas_wrapper.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; + +import '../../utils/utils.dart'; + +/// Paints [BarChartData] in the canvas, it can be used in a [CustomPainter] +class BarChartPainter extends AxisChartPainter with TouchHandler { + Paint _barPaint, _bgTouchTooltipPaint; + + List<_GroupBarsPosition> _groupBarsPosition; + + /// Paints [data] into canvas, it is the animating [BarChartData], + /// [targetData] is the animation's target and remains the same + /// during animation, then we should use it when we need to show + /// tooltips or something like that, because [data] is changing constantly. + /// + /// [touchHandler] passes a [TouchHandler] to the parent, + /// parent will use it for touch handling flow. + /// + /// [textScale] used for scaling texts inside the chart, + /// parent can use [MediaQuery.textScaleFactor] to respect + /// the system's font size. + BarChartPainter(BarChartData data, BarChartData targetData, Function(TouchHandler) touchHandler, + {double textScale = 1}) + : super(data, targetData, textScale: textScale) { + touchHandler(this); + _barPaint = Paint()..style = PaintingStyle.fill; + + _bgTouchTooltipPaint = Paint() + ..style = PaintingStyle.fill + ..color = Colors.white; + } + + /// Paints [BarChartData] into the provided canvas. + @override + void paint(Canvas canvas, Size size) { + super.paint(canvas, size); + final canvasWrapper = CanvasWrapper(canvas, size); + + if (data.barGroups.isEmpty) { + return; + } + + final List 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++) { + final barGroup = targetData.barGroups[i]; + for (int j = 0; j < barGroup.barRods.length; j++) { + if (!barGroup.showingTooltipIndicators.contains(j)) { + continue; + } + final barRod = barGroup.barRods[j]; + + _drawTouchTooltip(canvasWrapper, _groupBarsPosition, + targetData.barTouchData.touchTooltipData, barGroup, i, barRod, j); + } + } + } + + /// Calculates groups position for showing in the x axis using [alignment]. + List _calculateGroupsX( + Size viewSize, List barGroups, BarChartAlignment alignment) { + final Size drawSize = getChartUsableDrawSize(viewSize); + + final List groupsX = List(barGroups.length); + + final double leftTextsSpace = getLeftOffsetDrawSize(); + + switch (alignment) { + case BarChartAlignment.start: + double tempX = 0; + barGroups.asMap().forEach((i, group) { + groupsX[i] = leftTextsSpace + tempX + group.width / 2; + tempX += group.width; + }); + break; + + case BarChartAlignment.end: + double tempX = 0; + for (int i = barGroups.length - 1; i >= 0; i--) { + final group = barGroups[i]; + groupsX[i] = (leftTextsSpace + drawSize.width) - tempX - group.width / 2; + tempX += group.width; + } + break; + + case BarChartAlignment.center: + double sumWidth = barGroups.map((group) => group.width).reduce((a, b) => a + b); + sumWidth += data.groupsSpace * (barGroups.length - 1); + final double horizontalMargin = (drawSize.width - sumWidth) / 2; + + double tempX = 0; + for (int 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; + 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); + + double tempX = 0; + barGroups.asMap().forEach((index, group) { + tempX += group.width / 2; + if (index != 0) { + tempX += eachSpace; + } + groupsX[index] = leftTextsSpace + tempX; + tempX += group.width / 2; + }); + break; + + case BarChartAlignment.spaceAround: + 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 * 2); + + double tempX = 0; + barGroups.asMap().forEach((i, group) { + tempX += eachSpace; + tempX += group.width / 2; + groupsX[i] = leftTextsSpace + tempX; + tempX += group.width / 2; + tempX += eachSpace; + }); + break; + + case BarChartAlignment.spaceEvenly: + 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); + + double tempX = 0; + barGroups.asMap().forEach((i, group) { + tempX += eachSpace; + tempX += group.width / 2; + groupsX[i] = leftTextsSpace + tempX; + tempX += group.width / 2; + }); + break; + } + + return groupsX; + } + + /// Calculates bars position alongside group positions. + List<_GroupBarsPosition> _calculateGroupAndBarsPosition( + Size viewSize, List groupsX, List barGroups) { + if (groupsX.length != barGroups.length) { + throw Exception('inconsistent state groupsX.length != barGroups.length'); + } + + final List<_GroupBarsPosition> groupBarsPosition = []; + for (int i = 0; i < barGroups.length; i++) { + final BarChartGroupData barGroup = barGroups[i]; + final double groupX = groupsX[i]; + + double tempX = 0; + final List barsX = []; + barGroup.barRods.asMap().forEach((barIndex, barRod) { + final double widthHalf = barRod.width / 2; + barsX.add(groupX - (barGroup.width / 2) + tempX + widthHalf); + tempX += barRod.width + barGroup.barsSpace; + }); + groupBarsPosition.add(_GroupBarsPosition(groupX, barsX)); + } + return groupBarsPosition; + } + + void _drawBars(CanvasWrapper canvasWrapper, List<_GroupBarsPosition> groupBarsPosition) { + final viewSize = canvasWrapper.size; + final drawSize = getChartUsableDrawSize(viewSize); + + for (int i = 0; i < data.barGroups.length; i++) { + final barGroup = data.barGroups[i]; + for (int j = 0; j < barGroup.barRods.length; j++) { + final barRod = barGroup.barRods[j]; + final double widthHalf = barRod.width / 2; + final BorderRadius borderRadius = + barRod.borderRadius ?? BorderRadius.circular(barRod.width / 2); + + final double x = groupBarsPosition[i].barsX[j]; + + final left = x - widthHalf; + final right = x + widthHalf; + final cornerHeight = max(borderRadius.topLeft.y, borderRadius.topRight.y) + + max(borderRadius.bottomLeft.y, borderRadius.bottomRight.y); + + RRect barRRect; + + /// Draw [BackgroundBarChartRodData] + if (barRod.backDrawRodData.show && barRod.backDrawRodData.y != 0) { + if (barRod.backDrawRodData.y > 0) { + // positive + final bottom = getPixelY(max(data.minY, 0), drawSize); + final top = min(getPixelY(barRod.backDrawRodData.y, drawSize), bottom - cornerHeight); + + barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, + topLeft: borderRadius.topLeft, + topRight: borderRadius.topRight, + bottomLeft: borderRadius.bottomLeft, + bottomRight: borderRadius.bottomRight); + } else { + // negative + final top = getPixelY(min(data.maxY, 0), drawSize); + final bottom = max(getPixelY(barRod.backDrawRodData.y, drawSize), top + cornerHeight); + + barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, + topLeft: borderRadius.topLeft, + topRight: borderRadius.topRight, + bottomLeft: borderRadius.bottomLeft, + bottomRight: borderRadius.bottomRight); + } + + if (barRod.backDrawRodData.colors.length == 1) { + _barPaint.color = barRod.backDrawRodData.colors[0]; + _barPaint.shader = null; + } else { + final from = barRod.backDrawRodData.gradientFrom; + final to = barRod.backDrawRodData.gradientTo; + + List stops = []; + if (barRod.backDrawRodData.colorStops == null || + barRod.backDrawRodData.colorStops.length != barRod.backDrawRodData.colors.length) { + /// provided colorStops is invalid and we calculate it here + barRod.backDrawRodData.colors.asMap().forEach((index, color) { + final percent = 1.0 / barRod.backDrawRodData.colors.length; + stops.add(percent * index); + }); + } else { + stops = barRod.backDrawRodData.colorStops; + } + + _barPaint.shader = ui.Gradient.linear( + Offset( + getLeftOffsetDrawSize() + (drawSize.width * from.dx), + getTopOffsetDrawSize() + (drawSize.height * from.dy), + ), + Offset( + getLeftOffsetDrawSize() + (drawSize.width * to.dx), + getTopOffsetDrawSize() + (drawSize.height * to.dy), + ), + barRod.backDrawRodData.colors, + stops, + ); + } + + canvasWrapper.drawRRect(barRRect, _barPaint); + } + + // draw Main Rod + if (barRod.y != 0) { + if (barRod.y > 0) { + // positive + final bottom = getPixelY(max(data.minY, 0), drawSize); + final top = min(getPixelY(barRod.y, drawSize), bottom - cornerHeight); + + barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, + topLeft: borderRadius.topLeft, + topRight: borderRadius.topRight, + bottomLeft: borderRadius.bottomLeft, + bottomRight: borderRadius.bottomRight); + } else { + // negative + final top = getPixelY(min(data.maxY, 0), drawSize); + final bottom = max(getPixelY(barRod.y, drawSize), top + cornerHeight); + + barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom, + topLeft: borderRadius.topLeft, + topRight: borderRadius.topRight, + bottomLeft: borderRadius.bottomLeft, + bottomRight: borderRadius.bottomRight); + } + if (barRod.colors.length == 1) { + _barPaint.color = barRod.colors[0]; + _barPaint.shader = null; + } else { + final from = barRod.gradientFrom; + final to = barRod.gradientTo; + + List 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) { + final percent = 1.0 / barRod.colors.length; + stops.add(percent * index); + }); + } else { + stops = barRod.colorStops; + } + + _barPaint.shader = ui.Gradient.linear( + Offset( + getLeftOffsetDrawSize() + (drawSize.width * from.dx), + getTopOffsetDrawSize() + (drawSize.height * from.dy), + ), + Offset( + getLeftOffsetDrawSize() + (drawSize.width * to.dx), + getTopOffsetDrawSize() + (drawSize.height * to.dy), + ), + barRod.colors, + stops, + ); + } + canvasWrapper.drawRRect(barRRect, _barPaint); + + // draw rod stack + if (barRod.rodStackItems != null && barRod.rodStackItems.isNotEmpty) { + for (int i = 0; i < barRod.rodStackItems.length; i++) { + final stackItem = barRod.rodStackItems[i]; + final stackFromY = getPixelY(stackItem.fromY, drawSize); + final stackToY = getPixelY(stackItem.toY, drawSize); + + _barPaint.color = stackItem.color; + canvasWrapper.save(); + canvasWrapper.clipRect(Rect.fromLTRB(left, stackToY, right, stackFromY)); + canvasWrapper.drawRRect(barRRect, _barPaint); + canvasWrapper.restore(); + } + } + } + } + } + } + + void _drawTitles(CanvasWrapper canvasWrapper, List<_GroupBarsPosition> groupBarsPosition) { + if (!targetData.titlesData.show) { + return; + } + final viewSize = canvasWrapper.size; + final drawSize = getChartUsableDrawSize(viewSize); + + // Left Titles + final leftTitles = targetData.titlesData.leftTitles; + final leftInterval = + leftTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); + if (leftTitles.showTitles) { + double 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, drawSize); + + final String text = leftTitles.getTitles(verticalSeek); + + final TextSpan span = TextSpan(style: leftTitles.getTextStyles(verticalSeek), text: text); + final TextPainter tp = TextPainter( + text: span, + textAlign: TextAlign.center, + textDirection: TextDirection.ltr, + textScaleFactor: textScale); + tp.layout(maxWidth: getExtraNeededHorizontalSpace()); + x -= tp.width + leftTitles.margin; + y -= tp.height / 2; + canvasWrapper.save(); + canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); + canvasWrapper.rotate(radians(leftTitles.rotateAngle)); + canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); + y -= translateRotatedPosition(tp.width, leftTitles.rotateAngle); + canvasWrapper.drawText(tp, Offset(x, y)); + canvasWrapper.restore(); + } + if (data.maxY - verticalSeek < leftInterval && data.maxY != verticalSeek) { + verticalSeek = data.maxY; + } else { + verticalSeek += leftInterval; + } + } + } + + // Top Titles + final topTitles = targetData.titlesData.topTitles; + if (topTitles.showTitles) { + for (int index = 0; index < groupBarsPosition.length; index++) { + final _GroupBarsPosition groupBarPos = groupBarsPosition[index]; + + final xValue = data.barGroups[index].x.toDouble(); + final String text = topTitles.getTitles(xValue); + final TextSpan span = TextSpan(style: topTitles.getTextStyles(xValue), text: text); + final TextPainter tp = TextPainter( + text: span, + textAlign: TextAlign.center, + textDirection: TextDirection.ltr, + textScaleFactor: textScale); + tp.layout(); + double x = groupBarPos.groupX; + const double y = 0; + + x -= tp.width / 2; + canvasWrapper.save(); + canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); + canvasWrapper.rotate(radians(topTitles.rotateAngle)); + canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); + x += translateRotatedPosition(tp.width, topTitles.rotateAngle); + canvasWrapper.drawText(tp, Offset(x, y)); + canvasWrapper.restore(); + } + } + + // Right Titles + final rightTitles = targetData.titlesData.rightTitles; + final rightInterval = + rightTitles.interval ?? getEfficientInterval(viewSize.height, data.verticalDiff); + if (rightTitles.showTitles) { + double verticalSeek = data.minY; + while (verticalSeek <= data.maxY) { + if (rightTitles.checkToShowTitle( + data.minY, data.maxY, rightTitles, rightInterval, verticalSeek)) { + double x = drawSize.width + getLeftOffsetDrawSize(); + double y = getPixelY(verticalSeek, drawSize); + + final String text = rightTitles.getTitles(verticalSeek); + + final TextSpan span = + TextSpan(style: rightTitles.getTextStyles(verticalSeek), text: text); + final TextPainter tp = TextPainter( + text: span, + textAlign: TextAlign.center, + textDirection: TextDirection.ltr, + textScaleFactor: textScale); + tp.layout(maxWidth: getExtraNeededHorizontalSpace()); + x += rightTitles.margin; + y -= tp.height / 2; + canvasWrapper.save(); + canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); + canvasWrapper.rotate(radians(rightTitles.rotateAngle)); + canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); + y += translateRotatedPosition(tp.width, leftTitles.rotateAngle); + canvasWrapper.drawText(tp, Offset(x, y)); + canvasWrapper.restore(); + } + if (data.maxY - verticalSeek < rightInterval && data.maxY != verticalSeek) { + verticalSeek = data.maxY; + } else { + verticalSeek += rightInterval; + } + } + } + + // Bottom titles + final bottomTitles = targetData.titlesData.bottomTitles; + if (bottomTitles.showTitles) { + for (int index = 0; index < groupBarsPosition.length; index++) { + final _GroupBarsPosition groupBarPos = groupBarsPosition[index]; + + final xValue = data.barGroups[index].x.toDouble(); + final String text = bottomTitles.getTitles(xValue); + final TextSpan span = TextSpan(style: bottomTitles.getTextStyles(xValue), text: text); + final TextPainter tp = TextPainter( + text: span, + textAlign: TextAlign.center, + textDirection: TextDirection.ltr, + textScaleFactor: textScale); + tp.layout(); + double x = groupBarPos.groupX; + final double y = drawSize.height + getTopOffsetDrawSize() + bottomTitles.margin; + + x -= tp.width / 2; + canvasWrapper.save(); + canvasWrapper.translate(x + tp.width / 2, y + tp.height / 2); + canvasWrapper.rotate(radians(bottomTitles.rotateAngle)); + canvasWrapper.translate(-(x + tp.width / 2), -(y + tp.height / 2)); + x += translateRotatedPosition(tp.width, bottomTitles.rotateAngle); + canvasWrapper.drawText(tp, Offset(x, y)); + canvasWrapper.restore(); + } + } + } + + void _drawTouchTooltip( + CanvasWrapper canvasWrapper, + List<_GroupBarsPosition> groupPositions, + BarTouchTooltipData tooltipData, + BarChartGroupData showOnBarGroup, + int barGroupIndex, + BarChartRodData showOnRodData, + int barRodIndex, + ) { + final viewSize = canvasWrapper.size; + final Size chartUsableSize = getChartUsableDrawSize(viewSize); + + const double textsBelowMargin = 4; + + final BarTooltipItem tooltipItem = tooltipData.getTooltipItem( + showOnBarGroup, + barGroupIndex, + showOnRodData, + barRodIndex, + ); + + if (tooltipItem == null) { + return; + } + + final TextSpan span = TextSpan(style: tooltipItem.textStyle, text: tooltipItem.text); + final TextPainter tp = TextPainter( + text: span, + textAlign: TextAlign.center, + textDirection: TextDirection.ltr, + textScaleFactor: textScale); + tp.layout(maxWidth: tooltipData.maxContentWidth); + + /// creating TextPainters to calculate the width and height of the tooltip + final TextPainter drawingTextPainter = tp; + + /// biggerWidth + /// some texts maybe larger, then we should + /// draw the tooltip' width as wide as biggerWidth + /// + /// sumTextsHeight + /// sum up all Texts height, then we should + /// draw the tooltip's height as tall as sumTextsHeight + final double textWidth = drawingTextPainter.width; + final double textHeight = drawingTextPainter.height + textsBelowMargin; + + /// 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 barOffset = Offset( + groupPositions[barGroupIndex].barsX[barRodIndex], + getPixelY(showOnRodData.y, chartUsableSize), + ); + + final isPositive = showOnRodData.y > 0; + + final double tooltipWidth = textWidth + tooltipData.tooltipPadding.horizontal; + final double tooltipHeight = textHeight + tooltipData.tooltipPadding.vertical; + + final double tooltipTop = isPositive + ? barOffset.dy - tooltipHeight - tooltipData.tooltipBottomMargin + : barOffset.dy + tooltipData.tooltipBottomMargin; + + /// draw the background rect with rounded radius + Rect rect = + Rect.fromLTWH(barOffset.dx - (tooltipWidth / 2), tooltipTop, tooltipWidth, tooltipHeight); + + if (tooltipData.fitInsideHorizontally) { + if (rect.left < 0) { + final shiftAmount = 0 - rect.left; + rect = Rect.fromLTRB( + rect.left + shiftAmount, + rect.top, + rect.right + shiftAmount, + rect.bottom, + ); + } + + if (rect.right > viewSize.width) { + final shiftAmount = rect.right - viewSize.width; + rect = Rect.fromLTRB( + rect.left - shiftAmount, + rect.top, + rect.right - shiftAmount, + rect.bottom, + ); + } + } + + if (tooltipData.fitInsideVertically) { + if (rect.top < 0) { + final shiftAmount = 0 - rect.top; + rect = Rect.fromLTRB( + rect.left, + rect.top + shiftAmount, + rect.right, + rect.bottom + shiftAmount, + ); + } + + if (rect.bottom > viewSize.height) { + final shiftAmount = rect.bottom - viewSize.height; + rect = Rect.fromLTRB( + rect.left, + rect.top - shiftAmount, + rect.right, + rect.bottom - shiftAmount, + ); + } + } + + final Radius radius = Radius.circular(tooltipData.tooltipRoundedRadius); + final RRect 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 + final double top = tooltipData.tooltipPadding.top; + final drawOffset = Offset( + rect.center.dx - (tp.width / 2), + rect.topCenter.dy + top, + ); + canvasWrapper.drawText(tp, drawOffset); + } + + /// We add our needed horizontal space to parent needed. + /// we have some titles that maybe draw in the left and right side of our chart, + /// then we should draw the chart a with some left space, + /// the left space is [getLeftOffsetDrawSize], + /// and the whole space is [getExtraNeededHorizontalSpace] + @override + double getExtraNeededHorizontalSpace() { + double sum = super.getExtraNeededHorizontalSpace(); + if (data.titlesData.show) { + final leftSide = data.titlesData.leftTitles; + if (leftSide.showTitles) { + sum += leftSide.reservedSize + leftSide.margin; + } + + final rightSide = data.titlesData.rightTitles; + if (rightSide.showTitles) { + sum += rightSide.reservedSize + rightSide.margin; + } + } + return sum; + } + + /// We add our needed vertical space to parent needed. + /// we have some titles that maybe draw in the top and bottom side of our chart, + /// then we should draw the chart a with some top space, + /// the top space is [getTopOffsetDrawSize()], + /// and the whole space is [getExtraNeededVerticalSpace] + @override + double getExtraNeededVerticalSpace() { + double sum = super.getExtraNeededVerticalSpace(); + if (data.titlesData.show) { + final bottomSide = data.titlesData.bottomTitles; + if (bottomSide.showTitles) { + sum += bottomSide.reservedSize + bottomSide.margin; + } + + final topSide = data.titlesData.topTitles; + if (topSide.showTitles) { + sum += topSide.reservedSize + topSide.margin; + } + } + return sum; + } + + /// calculate left offset for draw the chart, + /// maybe we want to show both left and right titles, + /// then just the left titles will effect on this function. + @override + double getLeftOffsetDrawSize() { + var sum = super.getLeftOffsetDrawSize(); + + final leftTitles = data.titlesData.leftTitles; + if (data.titlesData.show && leftTitles.showTitles) { + sum += leftTitles.reservedSize + leftTitles.margin; + } + + return sum; + } + + /// calculate top offset for draw the chart, + /// maybe we want to show both top and bottom titles, + /// then just the top titles will effect on this function. + @override + double getTopOffsetDrawSize() { + var sum = super.getTopOffsetDrawSize(); + + final topTitles = data.titlesData.topTitles; + if (data.titlesData.show && topTitles.showTitles) { + sum += topTitles.reservedSize + topTitles.margin; + } + + return sum; + } + + /// Makes a [BarTouchResponse] based on the provided [FlTouchInput] + /// + /// Processes [FlTouchInput.getOffset] and checks + /// the elements of the chart that are near the offset, + /// then makes a [BarTouchResponse] from the elements that has been touched. + @override + BarTouchResponse handleTouch(FlTouchInput touchInput, Size size) { + final BarTouchedSpot touchedSpot = + _getNearestTouchedSpot(size, touchInput.getOffset(), _groupBarsPosition); + return BarTouchResponse(touchedSpot, touchInput); + } + + /// find the nearest spot base on the touched offset + BarTouchedSpot _getNearestTouchedSpot( + Size viewSize, Offset touchedPoint, List<_GroupBarsPosition> groupBarsPosition) { + if (groupBarsPosition == null) { + final List groupsX = _calculateGroupsX(viewSize, data.barGroups, data.alignment); + groupBarsPosition = _calculateGroupAndBarsPosition(viewSize, groupsX, data.barGroups); + } + + final Size 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; + + double barTopY; + double barBotY; + + final bool isPositive = targetData.barGroups[i].barRods[j].y > 0; + if (isPositive) { + barTopY = getPixelY(targetData.barGroups[i].barRods[j].y, chartViewSize); + barBotY = getPixelY(0, chartViewSize); + } else { + barTopY = getPixelY(0, chartViewSize); + barBotY = getPixelY(targetData.barGroups[i].barRods[j].y, chartViewSize); + } + + final double backDrawBarY = + getPixelY(targetData.barGroups[i].barRods[j].backDrawRodData.y, chartViewSize); + final EdgeInsets touchExtraThreshold = targetData.barTouchData.touchExtraThreshold; + + final bool isXInTouchBounds = + (touchedPoint.dx <= barX + halfBarWidth + touchExtraThreshold.right) && + (touchedPoint.dx >= barX - halfBarWidth - touchExtraThreshold.left); + + final bool isYInBarBounds = (touchedPoint.dy <= barBotY + touchExtraThreshold.bottom) && + (touchedPoint.dy >= barTopY - touchExtraThreshold.top); + + bool isYInBarBackDrawBounds; + if (isPositive) { + isYInBarBackDrawBounds = (touchedPoint.dy <= barBotY + touchExtraThreshold.bottom) && + (touchedPoint.dy >= backDrawBarY - touchExtraThreshold.top); + } else { + isYInBarBackDrawBounds = (touchedPoint.dy >= barTopY - touchExtraThreshold.top) && + (touchedPoint.dy <= backDrawBarY + touchExtraThreshold.bottom); + } + + final bool isYInTouchBounds = + (targetData.barTouchData.allowTouchBarBackDraw && isYInBarBackDrawBounds) || + isYInBarBounds; + + if (isXInTouchBounds && isYInTouchBounds) { + final nearestGroup = targetData.barGroups[i]; + final nearestBarRod = nearestGroup.barRods[j]; + final nearestSpot = FlSpot(nearestGroup.x.toDouble(), nearestBarRod.y); + final nearestSpotPos = Offset(barX, getPixelY(nearestSpot.y, chartViewSize)); + + int touchedStackIndex = -1; + BarChartRodStackItem touchedStack; + for (int stackIndex = 0; stackIndex < nearestBarRod.rodStackItems.length; stackIndex++) { + final BarChartRodStackItem stackItem = nearestBarRod.rodStackItems[stackIndex]; + final fromPixel = getPixelY(stackItem.fromY, chartViewSize); + final toPixel = getPixelY(stackItem.toY, chartViewSize); + if (touchedPoint.dy <= fromPixel && touchedPoint.dy >= toPixel) { + touchedStackIndex = stackIndex; + touchedStack = stackItem; + break; + } + } + + return BarTouchedSpot(nearestGroup, i, nearestBarRod, j, touchedStack, touchedStackIndex, + nearestSpot, nearestSpotPos); + } + } + } + + return null; + } + + /// Determines should it redraw the chart or not. + /// + /// If there is a change in the [BarChartData], + /// [BarChartPainter] should repaint itself. + @override + bool shouldRepaint(BarChartPainter oldDelegate) => oldDelegate.data != data; +} + +class _GroupBarsPosition { + final double groupX; + final List barsX; + + _GroupBarsPosition(this.groupX, this.barsX); +} From 80ac1a52ab186f0fc766f854c7e0a6554a50f485 Mon Sep 17 00:00:00 2001 From: Shripal Jain Date: Mon, 25 Jan 2021 13:21:23 +0530 Subject: [PATCH 17/22] Revert "bar_chart_painter" properly Signed-off-by: Shripal Jain --- .../chart/bar_chart/bar_chart_painter.dart | 209 +++++++++--------- 1 file changed, 104 insertions(+), 105 deletions(-) diff --git a/lib/src/chart/bar_chart/bar_chart_painter.dart b/lib/src/chart/bar_chart/bar_chart_painter.dart index a30580250..4c5efc2f5 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(barGroups.length); - 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) { From 97f263df59a2f6dd59d3daa0a14dd71ee5451211 Mon Sep 17 00:00:00 2001 From: Bruno D'Luka <45696119+bdlukaa@users.noreply.github.com> Date: Sat, 13 Feb 2021 18:02:35 -0300 Subject: [PATCH 18/22] Fixes 'Build function can not return null' --- lib/src/chart/pie_chart/pie_chart.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/chart/pie_chart/pie_chart.dart b/lib/src/chart/pie_chart/pie_chart.dart index 9d7ac98d9..0c495e16f 100644 --- a/lib/src/chart/pie_chart/pie_chart.dart +++ b/lib/src/chart/pie_chart/pie_chart.dart @@ -249,7 +249,7 @@ class _PieChartState extends AnimatedWidgetBaseState { } } - return null; + return SizedBox(); } bool _canHandleTouch(PieTouchResponse response, PieTouchData touchData) { From a8feaf6d4cda9ae79ecada0756c06c3d5ee44e4d Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Wed, 17 Feb 2021 21:58:22 +0330 Subject: [PATCH 19/22] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48b7b8be3..6be329c3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## newVersion +* [Bugfix] Fixed PieChart exception bug on sections tap, #514. +* [Bugfix] Fixed PieChart badges problem, #538. +* [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) From 2af726e62cf540b493039477a2a29f02382bf08b Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Wed, 17 Feb 2021 22:30:48 +0330 Subject: [PATCH 20/22] Fixed Bug of drawing lines with strokeWidth zero, #558. --- CHANGELOG.md | 1 + .../lib/pie_chart/samples/pie_chart_sample1.dart | 2 +- .../chart/base/axis_chart/axis_chart_painter.dart | 3 +++ .../chart/base/base_chart/base_chart_painter.dart | 6 +++++- lib/src/chart/line_chart/line_chart_painter.dart | 8 ++++++++ lib/src/extensions/paint_extension.dart | 15 +++++++++++++++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 lib/src/extensions/paint_extension.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 6be329c3c..905c8a9d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## newVersion * [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. 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/lib/src/chart/base/axis_chart/axis_chart_painter.dart b/lib/src/chart/base/axis_chart/axis_chart_painter.dart index 58a4f1602..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'; @@ -221,6 +222,7 @@ abstract class AxisChartPainter extends BaseChartPainte final flLineStyle = data.gridData.getDrawingVerticalLine(verticalSeek); _gridPaint.color = flLineStyle.color; _gridPaint.strokeWidth = flLineStyle.strokeWidth; + _gridPaint.transparentIfWidthIsZero(); final bothX = getPixelX(verticalSeek, usableViewSize); final x1 = bothX; @@ -252,6 +254,7 @@ abstract class AxisChartPainter extends BaseChartPainte final flLine = data.gridData.getDrawingHorizontalLine(horizontalSeek); _gridPaint.color = flLine.color; _gridPaint.strokeWidth = flLine.strokeWidth; + _gridPaint.transparentIfWidthIsZero(); final bothY = getPixelY(horizontalSeek, usableViewSize); final x1 = 0 + getLeftOffsetDrawSize(); 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 860336f7c..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'; @@ -45,6 +45,7 @@ abstract class BaseChartPainter extends CustomPainter { if (topBorder.width != 0.0) { _borderPaint.color = topBorder.color; _borderPaint.strokeWidth = topBorder.width; + _borderPaint.transparentIfWidthIsZero(); canvasWrapper.drawLine(topLeft, topRight, _borderPaint); } @@ -53,6 +54,7 @@ abstract class BaseChartPainter extends CustomPainter { if (rightBorder.width != 0.0) { _borderPaint.color = rightBorder.color; _borderPaint.strokeWidth = rightBorder.width; + _borderPaint.transparentIfWidthIsZero(); canvasWrapper.drawLine(topRight, bottomRight, _borderPaint); } @@ -61,6 +63,7 @@ abstract class BaseChartPainter extends CustomPainter { if (bottomBorder.width != 0.0) { _borderPaint.color = bottomBorder.color; _borderPaint.strokeWidth = bottomBorder.width; + _borderPaint.transparentIfWidthIsZero(); canvasWrapper.drawLine(bottomRight, bottomLeft, _borderPaint); } @@ -69,6 +72,7 @@ abstract class BaseChartPainter extends CustomPainter { 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_painter.dart b/lib/src/chart/line_chart/line_chart_painter.dart index a322b2097..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'; @@ -305,6 +306,7 @@ class LineChartPainter extends AxisChartPainter _touchLinePaint.color = indicatorData.indicatorBelowLine.color; _touchLinePaint.strokeWidth = indicatorData.indicatorBelowLine.strokeWidth; + _touchLinePaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine( bottom, lineEnd, _touchLinePaint, indicatorData.indicatorBelowLine.dashArray); @@ -610,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); @@ -702,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); @@ -832,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); } @@ -1020,6 +1026,7 @@ class LineChartPainter extends AxisChartPainter _extraLinesPaint.color = line.color; _extraLinesPaint.strokeWidth = line.strokeWidth; + _extraLinesPaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine(from, to, _extraLinesPaint, line.dashArray); @@ -1082,6 +1089,7 @@ class LineChartPainter extends AxisChartPainter _extraLinesPaint.color = line.color; _extraLinesPaint.strokeWidth = line.strokeWidth; + _extraLinesPaint.transparentIfWidthIsZero(); canvasWrapper.drawDashedLine(from, to, _extraLinesPaint, line.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); + } + } + } +} From af68569312fafe78b557f238b9611fb31ab9e9f3 Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Wed, 17 Feb 2021 22:46:43 +0330 Subject: [PATCH 21/22] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d193b05a..2b3522ebb 100644 --- a/README.md +++ b/README.md @@ -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 From 1e8d3c1181edb818f2ae1d104d5e779349093be5 Mon Sep 17 00:00:00 2001 From: FIRST_NAME LAST_NAME Date: Wed, 17 Feb 2021 22:47:14 +0330 Subject: [PATCH 22/22] Bump version to 0.12.3 --- CHANGELOG.md | 2 +- README.md | 2 +- pubspec.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 905c8a9d2..772a7c40f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## newVersion +## 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. diff --git a/README.md b/README.md index 2b3522ebb..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 ``` diff --git a/pubspec.yaml b/pubspec.yaml index 86856b7e6..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: