Skip to content

Commit

Permalink
fix: Removed typing mistakes and replace switch statements to dart 3 …
Browse files Browse the repository at this point in the history
…switch expression syntax
  • Loading branch information
Apeksha Mehta committed Aug 5, 2023
1 parent 2d45fa3 commit b2471aa
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 100 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
## nextVersion
* **BUGFIX** (by @Anas35) Fix Tooltip not displaying when value from BackgroundBarChartRodData is less than zero. #1345.
* **BUGFIX** (by @imaNNeo) Fix Negative BarChartRodStackItem are not drawn correctly bug, #1347

## nextVersion
* **FEATURE** (by @apekshamehta) Added new method called getTooltipColor for axis charts (bar,line,scatter) to change backgroud color of tooltip dynamically.[issue](https://github.com/imaNNeo/fl_chart/issues/1279).
* **FEATURE** (by @apekshamehta) Added new method called getTooltipColor for axis charts (bar,line,scatter) to change background color of tooltip dynamically.[issue](https://github.com/imaNNeo/fl_chart/issues/1279).
* **BREAKING** (by @apekshamehta) Removed tooltipBgColor property from Bar,Line and Scatter Charts.


Expand Down
36 changes: 10 additions & 26 deletions example/lib/presentation/samples/bar/bar_chart_sample1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,32 +163,16 @@ class BarChartSample1State extends State<BarChartSample1> {
barTouchData: BarTouchData(
touchTooltipData: BarTouchTooltipData(
getTooltipColor: (group) {
Color bgColor;
switch (group.x) {
case 0:
bgColor = AppColors.contentColorPurple.withAlpha(100);
break;
case 1:
bgColor = AppColors.contentColorYellow.withAlpha(100);
break;
case 2:
bgColor = AppColors.contentColorBlue.withAlpha(100);
break;
case 3:
bgColor = AppColors.contentColorOrange.withAlpha(100);
break;
case 4:
bgColor = AppColors.contentColorPink.withAlpha(100);
break;
case 5:
bgColor = AppColors.contentColorRed.withAlpha(100);
break;
case 6:
bgColor = AppColors.contentColorPurple.withAlpha(150);
break;
default:
throw Error();
}
Color bgColor = switch (group.x) {
0 => AppColors.contentColorPurple.withAlpha(100),
1 => AppColors.contentColorYellow.withAlpha(100),
2 => AppColors.contentColorBlue.withAlpha(100),
3 => AppColors.contentColorOrange.withAlpha(100),
4 => AppColors.contentColorPink.withAlpha(100),
5 => AppColors.contentColorRed.withAlpha(100),
6 => AppColors.contentColorPurple.withAlpha(150),
_ => throw Error(),
};
return bgColor;
},
tooltipHorizontalAlignment: FLHorizontalAlignment.right,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class _LineChart extends StatelessWidget {
LineTouchData get lineTouchData1 => LineTouchData(
handleBuiltInTouches: true,
touchTooltipData: LineTouchTooltipData(
getTooltipColor: (touchedSpots) => Colors.blueGrey.withOpacity(0.8),
getTooltipColor: (touchedSpot) => Colors.blueGrey.withOpacity(0.8),
),
);

Expand Down
36 changes: 10 additions & 26 deletions example/lib/presentation/samples/line/line_chart_sample3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,35 +207,19 @@ class _LineChartSample3State extends State<LineChartSample3> {
},
touchTooltipData: LineTouchTooltipData(
getTooltipColor: (touchedSpot) {
Color bgColor = Colors.white;
switch ((
Color bgColor = switch ((
touchedSpot.x.toInt(),
touchedSpot.y.toDouble()
)) {
case (0, 1.3):
bgColor = AppColors.contentColorPurple.withAlpha(100);
break;
case (1, 1):
bgColor = AppColors.contentColorYellow.withAlpha(100);
break;
case (2, 1.8):
bgColor = AppColors.contentColorBlue.withAlpha(100);
break;
case (3, 1.5):
bgColor = AppColors.contentColorOrange.withAlpha(100);
break;
case (4, 2.2):
bgColor = AppColors.contentColorPink.withAlpha(100);
break;
case (5, 1.8):
bgColor = AppColors.contentColorRed.withAlpha(100);
break;
case (6, 3):
bgColor = AppColors.contentColorPurple.withAlpha(150);
break;
default:
throw Error();
}
(0, 1.3) => AppColors.contentColorPurple.withAlpha(100),
(1, 1) => AppColors.contentColorYellow.withAlpha(100),
(2, 1.8) => AppColors.contentColorBlue.withAlpha(100),
(3, 1.5) => AppColors.contentColorOrange.withAlpha(100),
(4, 2.2) => AppColors.contentColorPink.withAlpha(100),
(5, 1.8) => AppColors.contentColorRed.withAlpha(100),
(6, 3) => AppColors.contentColorPurple.withAlpha(150),
_ => throw Error(),
};
return bgColor;
},
getTooltipItems: (List<LineBarSpot> touchedBarSpots) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LineChartSample9 extends StatelessWidget {
lineTouchData: LineTouchData(
touchTooltipData: LineTouchTooltipData(
maxContentWidth: 100,
getTooltipColor: (touchedSpots) => Colors.black,
getTooltipColor: (touchedSpot) => Colors.black,
getTooltipItems: (touchedSpots) {
return touchedSpots.map((LineBarSpot touchedSpot) {
final textStyle = TextStyle(
Expand Down
44 changes: 14 additions & 30 deletions example/lib/presentation/samples/scatter/scatter_chart_sample2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,36 +122,20 @@ class _ScatterChartSample2State extends State {
},
touchTooltipData: ScatterTouchTooltipData(
getTooltipColor: (ScatterSpot touchedBarSpot) {
Color bgColor;

switch ((touchedBarSpot.x.toInt(), touchedBarSpot.y.toInt())) {
case (4, 4):
bgColor = AppColors.contentColorPurple.withAlpha(100);
break;
case (2, 5):
bgColor = AppColors.contentColorYellow.withAlpha(100);
break;
case (4, 5):
bgColor = AppColors.contentColorBlue.withAlpha(100);
break;
case (8, 6):
bgColor = AppColors.contentColorOrange.withAlpha(100);
break;
case (5, 7):
bgColor = AppColors.contentColorPink.withAlpha(100);
break;
case (7, 2):
bgColor = AppColors.contentColorRed.withAlpha(100);
break;
case (3, 2):
bgColor = AppColors.contentColorPurple.withAlpha(150);
break;
case (2, 8):
bgColor = AppColors.contentColorYellow.withAlpha(100);
break;
default:
throw Error();
}
Color bgColor = switch ((
touchedBarSpot.x.toInt(),
touchedBarSpot.y.toInt()
)) {
(4, 4) => AppColors.contentColorPurple.withAlpha(100),
(2, 5) => AppColors.contentColorYellow.withAlpha(100),
(4, 5) => AppColors.contentColorBlue.withAlpha(100),
(8, 6) => AppColors.contentColorOrange.withAlpha(100),
(5, 7) => AppColors.contentColorPink.withAlpha(100),
(7, 2) => AppColors.contentColorRed.withAlpha(100),
(3, 2) => AppColors.contentColorPurple.withAlpha(150),
(2, 8) => AppColors.contentColorYellow.withAlpha(100),
_ => throw Error(),
};
return bgColor;
},
getTooltipItems: (ScatterSpot touchedBarSpot) {
Expand Down
8 changes: 6 additions & 2 deletions lib/src/chart/bar_chart/bar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,7 @@ class BarTouchTooltipData with EquatableMixin {
tooltipHorizontalOffset = tooltipHorizontalOffset ?? 0,
maxContentWidth = maxContentWidth ?? 120,
getTooltipItem = getTooltipItem ?? defaultBarTooltipItem,
getTooltipColor = getTooltipColor ??
((BarChartGroupData? group) => Colors.blueGrey.darken(15)),
getTooltipColor = getTooltipColor ?? defaultBarTooltipColor,
fitInsideHorizontally = fitInsideHorizontally ?? false,
fitInsideVertically = fitInsideVertically ?? false,
direction = direction ?? TooltipDirection.auto,
Expand Down Expand Up @@ -855,6 +854,11 @@ typedef GetBarTooltipColor = Color Function(
BarChartGroupData group,
);

/// Default implementation for [BarTouchTooltipData.getTooltipColor].
Color defaultBarTooltipColor(BarChartGroupData group) {
return Colors.blueGrey.darken(15);
}

/// Holds information about touch response in the [BarChart].
///
/// You can override [BarTouchData.touchCallback] to handle touch events,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1346,8 +1346,8 @@ typedef GetLineTooltipColor = Color Function(
LineBarSpot touchedSpot,
);

/// Default implementation for [LineTouchTooltipData.getTooltipItems].
Color defaultLineTooltipColor(LineBarSpot touchedSpots) {
/// Default implementation for [LineTouchTooltipData.getTooltipColor].
Color defaultLineTooltipColor(LineBarSpot touchedSpot) {
return Colors.blueGrey.darken(15);
}

Expand Down
6 changes: 3 additions & 3 deletions test/chart/data_pool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1162,11 +1162,11 @@ List<LineTooltipItem?> lineChartGetTooltipItems(List<LineBarSpot> list) {
return list.map((s) => lineTooltipItem1).toList();
}

Color lineChartGetGreenColor(LineBarSpot touchedSpots) {
Color lineChartGetGreenColor(LineBarSpot touchedSpot) {
return Colors.green;
}

Color lineChartGetRedColor(LineBarSpot touchedSpots) {
Color lineChartGetRedColor(LineBarSpot touchedSpot) {
return Colors.red;
}

Expand Down Expand Up @@ -2278,7 +2278,7 @@ final ScatterChartData scatterChartData1 = ScatterChartData(
fitInsideHorizontally: true,
fitInsideVertically: false,
maxContentWidth: 33,
getTooltipColor: (touchedSpots) => Colors.white,
getTooltipColor: (touchedSpot) => Colors.white,
tooltipPadding: const EdgeInsets.all(23),
tooltipRoundedRadius: 534,
),
Expand Down
6 changes: 3 additions & 3 deletions test/chart/line_chart/line_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,7 @@ void main() {
);

final tooltipData = LineTouchTooltipData(
getTooltipColor: (tochedSpots) => const Color(0x11111111),
getTooltipColor: (touchedSpot) => const Color(0x11111111),
tooltipRoundedRadius: 12,
rotateAngle: 43,
maxContentWidth: 100,
Expand Down Expand Up @@ -2367,7 +2367,7 @@ void main() {
);

final tooltipData = LineTouchTooltipData(
getTooltipColor: (tochedSpots) => const Color(0x11111111),
getTooltipColor: (touchedSpot) => const Color(0x11111111),
tooltipRoundedRadius: 12,
rotateAngle: 43,
maxContentWidth: 100,
Expand Down Expand Up @@ -2477,7 +2477,7 @@ void main() {
);

final tooltipData = LineTouchTooltipData(
getTooltipColor: (tochedSpots) => const Color(0x11111111),
getTooltipColor: (touchedSpot) => const Color(0x11111111),
tooltipRoundedRadius: 12,
rotateAngle: 43,
maxContentWidth: 100,
Expand Down
6 changes: 3 additions & 3 deletions test/chart/scatter_chart/scatter_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void main() {
scatterTouchData: ScatterTouchData(
touchTooltipData: ScatterTouchTooltipData(
rotateAngle: 18,
getTooltipColor: (touchedSpots) => const Color(0xFF00FF00),
getTooltipColor: (touchedSpot) => const Color(0xFF00FF00),
tooltipRoundedRadius: 85,
tooltipPadding: const EdgeInsets.all(12),
getTooltipItems: (_) {
Expand Down Expand Up @@ -428,7 +428,7 @@ void main() {
scatterTouchData: ScatterTouchData(
touchTooltipData: ScatterTouchTooltipData(
rotateAngle: 18,
getTooltipColor: (touchedSpots) => const Color(0xFFFFFF00),
getTooltipColor: (touchedSpot) => const Color(0xFFFFFF00),
tooltipRoundedRadius: 22,
fitInsideHorizontally: false,
fitInsideVertically: true,
Expand Down Expand Up @@ -531,7 +531,7 @@ void main() {
scatterTouchData: ScatterTouchData(
touchTooltipData: ScatterTouchTooltipData(
rotateAngle: 18,
getTooltipColor: (touchedSpots) => const Color(0xFFFFFF00),
getTooltipColor: (touchedSpot) => const Color(0xFFFFFF00),
tooltipRoundedRadius: 22,
fitInsideHorizontally: false,
fitInsideVertically: true,
Expand Down

0 comments on commit b2471aa

Please sign in to comment.