Skip to content

Commit

Permalink
Use the default theme available by the themeData for tooltips for bar…
Browse files Browse the repository at this point in the history
… chart

#1464
  • Loading branch information
devilk10 committed Oct 23, 2023
1 parent f76cb4d commit d0ea666
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* **BUGFIX** (by @imaNNeo) Fix Negative BarChartRodStackItem are not drawn correctly bug, #1347
* **BUGFIX** (by @imaNNeo) Fix bar_chart_helper minY calculation bug, #1388
* **IMPROVEMENT** (by @imaNNeo) Consider fraction digits when formatting chart side titles, #1267
* **IMPROVEMENT** (by @devilk10) Use the default theme available by the themeData for tooltips instead of color params #1464

## 0.63.0
* **BUGFIX** (by @imaNNeo) Fix PieChart crash on web-renderer html by ignoring `sectionsSpace` when `Path.combine()` does not work (it's flutter engine [issue](https://github.com/flutter/flutter/issues/44572)), #955
Expand Down
6 changes: 5 additions & 1 deletion example/lib/presentation/samples/bar/bar_chart_sample3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ class _BarChart extends StatelessWidget {
BarTouchData get barTouchData => BarTouchData(
enabled: false,
touchTooltipData: BarTouchTooltipData(
tooltipBgColor: Colors.transparent,
tooltipThemeData: const TooltipThemeData(
decoration: BoxDecoration(
color: Colors.transparent,
),
),
tooltipPadding: EdgeInsets.zero,
tooltipMargin: 8,
getTooltipItem: (
Expand Down
6 changes: 5 additions & 1 deletion example/lib/presentation/samples/bar/bar_chart_sample7.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ class _BarChartSample7State extends State<BarChartSample7> {
enabled: true,
handleBuiltInTouches: false,
touchTooltipData: BarTouchTooltipData(
tooltipBgColor: Colors.transparent,
tooltipThemeData: const TooltipThemeData(
decoration: BoxDecoration(
color: Colors.transparent,
),
),
tooltipMargin: 0,
getTooltipItem: (
BarChartGroupData group,
Expand Down
10 changes: 1 addition & 9 deletions lib/src/chart/bar_chart/bar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:ui';
import 'package:equatable/equatable.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/chart/bar_chart/bar_chart_helper.dart';
import 'package:fl_chart/src/extensions/color_extension.dart';
import 'package:fl_chart/src/utils/lerp.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -692,7 +691,6 @@ class BarTouchTooltipData with EquatableMixin {
/// you can set [fitInsideHorizontally] true to force it to shift inside the chart horizontally,
/// also you can set [fitInsideVertically] true to force it to shift inside the chart vertically.
BarTouchTooltipData({
Color? tooltipBgColor,
double? tooltipRoundedRadius,
EdgeInsets? tooltipPadding,
double? tooltipMargin,
Expand All @@ -706,8 +704,7 @@ class BarTouchTooltipData with EquatableMixin {
double? rotateAngle,
BorderSide? tooltipBorder,
this.tooltipThemeData,
}) : tooltipBgColor = tooltipBgColor ?? Colors.blueGrey.darken(15),
tooltipRoundedRadius = tooltipRoundedRadius ?? 4,
}) : tooltipRoundedRadius = tooltipRoundedRadius ?? 4,
tooltipPadding = tooltipPadding ??
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
tooltipMargin = tooltipMargin ?? 16,
Expand All @@ -723,10 +720,6 @@ class BarTouchTooltipData with EquatableMixin {
tooltipBorder = tooltipBorder ?? BorderSide.none,
super();

/// The tooltip background color.
@Deprecated('Use BarTouchTooltipData.tooltipThemeData instead')
final Color tooltipBgColor;

/// The tooltip background color and text decoration.
final TooltipThemeData? tooltipThemeData;

Expand Down Expand Up @@ -769,7 +762,6 @@ class BarTouchTooltipData with EquatableMixin {
/// Used for equality check, see [EquatableMixin].
@override
List<Object?> get props => [
tooltipBgColor,
tooltipRoundedRadius,
tooltipPadding,
tooltipMargin,
Expand Down
10 changes: 5 additions & 5 deletions lib/src/chart/bar_chart/bar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:fl_chart/fl_chart.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/extensions/bar_chart_data_extension.dart';
import 'package:fl_chart/src/extensions/color_extension.dart';
import 'package:fl_chart/src/extensions/paint_extension.dart';
import 'package:fl_chart/src/extensions/rrect_extension.dart';
import 'package:fl_chart/src/utils/canvas_wrapper.dart';
Expand Down Expand Up @@ -34,6 +35,7 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
..color = Colors.transparent
..strokeWidth = 1.0;
}

late Paint _barPaint;
late Paint _barStrokePaint;
late Paint _bgTouchTooltipPaint;
Expand Down Expand Up @@ -355,21 +357,18 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
switch (decoration.runtimeType) {
case BoxDecoration:
bgColor = (decoration as BoxDecoration).color;
break;
case ShapeDecoration:
bgColor = (decoration as ShapeDecoration).color;

Check warning on line 361 in lib/src/chart/bar_chart/bar_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/bar_chart/bar_chart_painter.dart#L360-L361

Added lines #L360 - L361 were not covered by tests
break;
default:
throw UnsupportedError(
'Unsupported decoration type: ${decoration.runtimeType}',

Check warning on line 364 in lib/src/chart/bar_chart/bar_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/bar_chart/bar_chart_painter.dart#L363-L364

Added lines #L363 - L364 were not covered by tests
);
}
}
_bgTouchTooltipPaint.color = bgColor ?? tooltipData.tooltipBgColor;
_bgTouchTooltipPaint.color = bgColor ?? Colors.blueGrey.darken(15);

final span = TextSpan(
style: Utils()
.getThemeAwareTextStyle(context, textStyle),
style: Utils().getThemeAwareTextStyle(context, textStyle),
text: tooltipItem.text,
children: tooltipItem.children,
);
Expand Down Expand Up @@ -723,6 +722,7 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
@visibleForTesting
class GroupBarsPosition {
GroupBarsPosition(this.groupX, this.barsX);

final double groupX;
final List<double> barsX;
}
17 changes: 13 additions & 4 deletions test/chart/bar_chart/bar_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,9 @@ void main() {

final tooltipData = BarTouchTooltipData(
tooltipThemeData: const TooltipThemeData(
decoration: BoxDecoration(color: Color(0xf33f33f3)),
textStyle: textStyle1,),
decoration: BoxDecoration(color: Color(0xf33f33f3)),
textStyle: textStyle1,
),
tooltipRoundedRadius: 8,
maxContentWidth: 80,
rotateAngle: 12,
Expand Down Expand Up @@ -1048,7 +1049,11 @@ void main() {

final tooltipData = BarTouchTooltipData(
tooltipRoundedRadius: 8,
tooltipBgColor: const Color(0xf33f33f3),
tooltipThemeData: const TooltipThemeData(
decoration: BoxDecoration(
color: Color(0xf33f33f3),
),
),
maxContentWidth: 80,
rotateAngle: 12,
direction: TooltipDirection.bottom,
Expand Down Expand Up @@ -1214,7 +1219,11 @@ void main() {

final tooltipData = BarTouchTooltipData(
tooltipRoundedRadius: 8,
tooltipBgColor: const Color(0xf33f33f3),
tooltipThemeData: const TooltipThemeData(
decoration: BoxDecoration(
color: Color(0xf33f33f3),
),
),
maxContentWidth: 8000,
rotateAngle: 12,
fitInsideHorizontally: true,
Expand Down
12 changes: 0 additions & 12 deletions test/chart/data_pool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2714,7 +2714,6 @@ final BarTouchTooltipData barTouchTooltipData1 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2725,7 +2724,6 @@ final BarTouchTooltipData barTouchTooltipData1Clone = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2736,7 +2734,6 @@ final BarTouchTooltipData barTouchTooltipData2 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2748,7 +2745,6 @@ final BarTouchTooltipData barTouchTooltipData3 = BarTouchTooltipData(
fitInsideVertically: true,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2760,7 +2756,6 @@ final BarTouchTooltipData barTouchTooltipData4 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: false,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2772,7 +2767,6 @@ final BarTouchTooltipData barTouchTooltipData5 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23.00001,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2785,7 +2779,6 @@ final BarTouchTooltipData barTouchTooltipData6 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.blue,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2798,7 +2791,6 @@ final BarTouchTooltipData barTouchTooltipData7 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
tooltipBorder: const BorderSide(color: Colors.red),
Expand All @@ -2810,7 +2802,6 @@ final BarTouchTooltipData barTouchTooltipData8 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
tooltipMargin: 12,
tooltipBorder: const BorderSide(color: Colors.red),
Expand All @@ -2820,7 +2811,6 @@ final BarTouchTooltipData barTouchTooltipData9 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 333,
Expand All @@ -2831,7 +2821,6 @@ final BarTouchTooltipData barTouchTooltipData10 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand All @@ -2842,7 +2831,6 @@ final BarTouchTooltipData barTouchTooltipData11 = BarTouchTooltipData(
fitInsideVertically: false,
fitInsideHorizontally: true,
maxContentWidth: 23,
tooltipBgColor: Colors.green,
tooltipPadding: const EdgeInsets.all(23),
getTooltipItem: getTooltipItem,
tooltipMargin: 12,
Expand Down

0 comments on commit d0ea666

Please sign in to comment.