Skip to content

Commit 30897cf

Browse files
tamasappsimaNNeo
authored andcommitted
Add tooltip align to line and scatter chart
1 parent 1339d6f commit 30897cf

File tree

8 files changed

+76
-22
lines changed

8 files changed

+76
-22
lines changed

example/lib/presentation/samples/bar/bar_chart_sample1.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class BarChartSample1State extends State<BarChartSample1> {
163163
barTouchData: BarTouchData(
164164
touchTooltipData: BarTouchTooltipData(
165165
tooltipBgColor: Colors.blueGrey,
166-
tooltipAlign: BarTooltipAlign.right,
166+
tooltipAlign: FLTooltipAlign.right,
167167
tooltipMargin: -10,
168168
getTooltipItem: (group, groupIndex, rod, rodIndex) {
169169
String weekDay;

lib/src/chart/bar_chart/bar_chart_data.dart

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -680,18 +680,6 @@ enum TooltipDirection {
680680
bottom,
681681
}
682682

683-
/// Controls showing tooltip horizontal alignment relative to the rod.
684-
enum BarTooltipAlign {
685-
/// Tooltip shows horizontally center aligned with the rod.
686-
center,
687-
688-
/// Tooltip shows on the left side of the rod.
689-
left,
690-
691-
/// Tooltip shows on the right side of the rod.
692-
right,
693-
}
694-
695683
/// Holds representation data for showing tooltip popup on top of rods.
696684
class BarTouchTooltipData with EquatableMixin {
697685
/// if [BarTouchData.handleBuiltInTouches] is true,
@@ -712,7 +700,7 @@ class BarTouchTooltipData with EquatableMixin {
712700
double? tooltipRoundedRadius,
713701
EdgeInsets? tooltipPadding,
714702
double? tooltipMargin,
715-
BarTooltipAlign? tooltipAlign,
703+
FLTooltipAlign? tooltipAlign,
716704
double? tooltipHorizontalOffset,
717705
double? maxContentWidth,
718706
GetBarTooltipItem? getTooltipItem,
@@ -726,15 +714,15 @@ class BarTouchTooltipData with EquatableMixin {
726714
tooltipPadding = tooltipPadding ??
727715
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
728716
tooltipMargin = tooltipMargin ?? 16,
717+
tooltipAlign = tooltipAlign ?? FLTooltipAlign.center,
718+
tooltipHorizontalOffset = tooltipHorizontalOffset ?? 0,
729719
maxContentWidth = maxContentWidth ?? 120,
730720
getTooltipItem = getTooltipItem ?? defaultBarTooltipItem,
731721
fitInsideHorizontally = fitInsideHorizontally ?? false,
732722
fitInsideVertically = fitInsideVertically ?? false,
733723
direction = direction ?? TooltipDirection.auto,
734724
rotateAngle = rotateAngle ?? 0.0,
735725
tooltipBorder = tooltipBorder ?? BorderSide.none,
736-
tooltipAlign = tooltipAlign ?? BarTooltipAlign.center,
737-
tooltipHorizontalOffset = tooltipHorizontalOffset ?? 0,
738726
super();
739727

740728
/// The tooltip background color.
@@ -750,7 +738,7 @@ class BarTouchTooltipData with EquatableMixin {
750738
final double tooltipMargin;
751739

752740
/// Controls showing tooltip on left side, right side or center aligned with rod, default is center
753-
final BarTooltipAlign tooltipAlign;
741+
final FLTooltipAlign tooltipAlign;
754742

755743
/// Applies horizontal offset for showing tooltip, default is zero.
756744
final double tooltipHorizontalOffset;

lib/src/chart/bar_chart/bar_chart_painter.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,15 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
393393

394394
double tooltipLeft;
395395
switch (tooltipData.tooltipAlign) {
396-
case BarTooltipAlign.center:
396+
case FLTooltipAlign.center:
397397
tooltipLeft = barOffset.dx -
398398
(tooltipWidth / 2) +
399399
tooltipData.tooltipHorizontalOffset;
400400
break;
401-
case BarTooltipAlign.right:
401+
case FLTooltipAlign.right:
402402
tooltipLeft = barOffset.dx + tooltipData.tooltipHorizontalOffset;
403403
break;
404-
case BarTooltipAlign.left:
404+
case FLTooltipAlign.left:
405405
tooltipLeft =
406406
barOffset.dx - tooltipWidth + tooltipData.tooltipHorizontalOffset;
407407
break;

lib/src/chart/base/base_chart/base_chart_data.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,15 @@ typedef MouseCursorResolver<R extends BaseTouchResponse> = MouseCursor Function(
190190
abstract class BaseTouchResponse {
191191
BaseTouchResponse();
192192
}
193+
194+
/// Controls showing tooltip horizontal alignment relative to the rod/spot.
195+
enum FLTooltipAlign {
196+
/// Tooltip shows horizontally center aligned with the rod/spot.
197+
center,
198+
199+
/// Tooltip shows on the left side of the rod/spot.
200+
left,
201+
202+
/// Tooltip shows on the right side of the rod/spot.
203+
right,
204+
}

lib/src/chart/line_chart/line_chart_data.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,8 @@ class LineTouchTooltipData with EquatableMixin {
12921292
double? tooltipRoundedRadius,
12931293
EdgeInsets? tooltipPadding,
12941294
double? tooltipMargin,
1295+
FLTooltipAlign? tooltipAlign,
1296+
double? tooltipHorizontalOffset,
12951297
double? maxContentWidth,
12961298
GetLineTooltipItems? getTooltipItems,
12971299
bool? fitInsideHorizontally,
@@ -1304,6 +1306,8 @@ class LineTouchTooltipData with EquatableMixin {
13041306
tooltipPadding = tooltipPadding ??
13051307
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
13061308
tooltipMargin = tooltipMargin ?? 16,
1309+
tooltipAlign = tooltipAlign ?? FLTooltipAlign.center,
1310+
tooltipHorizontalOffset = tooltipHorizontalOffset ?? 0,
13071311
maxContentWidth = maxContentWidth ?? 120,
13081312
getTooltipItems = getTooltipItems ?? defaultLineTooltipItem,
13091313
fitInsideHorizontally = fitInsideHorizontally ?? false,
@@ -1325,6 +1329,12 @@ class LineTouchTooltipData with EquatableMixin {
13251329
/// Applies a bottom margin for showing tooltip on top of rods.
13261330
final double tooltipMargin;
13271331

1332+
/// Controls showing tooltip on left side, right side or center aligned with spot, default is center
1333+
final FLTooltipAlign tooltipAlign;
1334+
1335+
/// Applies horizontal offset for showing tooltip, default is zero.
1336+
final double tooltipHorizontalOffset;
1337+
13281338
/// Restricts the tooltip's width.
13291339
final double maxContentWidth;
13301340

lib/src/chart/line_chart/line_chart_painter.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,9 +1037,26 @@ class LineChartPainter extends AxisChartPainter<LineChartData> {
10371037
mostTopOffset.dy - tooltipHeight - tooltipData.tooltipMargin;
10381038
}
10391039

1040+
double tooltipLeft;
1041+
switch (tooltipData.tooltipAlign) {
1042+
case FLTooltipAlign.center:
1043+
tooltipLeft = mostTopOffset.dx -
1044+
(tooltipWidth / 2) +
1045+
tooltipData.tooltipHorizontalOffset;
1046+
break;
1047+
case FLTooltipAlign.right:
1048+
tooltipLeft = mostTopOffset.dx + tooltipData.tooltipHorizontalOffset;
1049+
break;
1050+
case FLTooltipAlign.left:
1051+
tooltipLeft = mostTopOffset.dx -
1052+
tooltipWidth +
1053+
tooltipData.tooltipHorizontalOffset;
1054+
break;
1055+
}
1056+
10401057
/// draw the background rect with rounded radius
10411058
var rect = Rect.fromLTWH(
1042-
mostTopOffset.dx - (tooltipWidth / 2),
1059+
tooltipLeft,
10431060
tooltipTopPosition,
10441061
tooltipWidth,
10451062
tooltipHeight,

lib/src/chart/scatter_chart/scatter_chart_data.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ class ScatterTouchTooltipData with EquatableMixin {
398398
Color? tooltipBgColor,
399399
double? tooltipRoundedRadius,
400400
EdgeInsets? tooltipPadding,
401+
FLTooltipAlign? tooltipAlign,
402+
double? tooltipHorizontalOffset,
401403
double? maxContentWidth,
402404
GetScatterTooltipItems? getTooltipItems,
403405
bool? fitInsideHorizontally,
@@ -408,6 +410,8 @@ class ScatterTouchTooltipData with EquatableMixin {
408410
tooltipRoundedRadius = tooltipRoundedRadius ?? 4,
409411
tooltipPadding = tooltipPadding ??
410412
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
413+
tooltipAlign = tooltipAlign ?? FLTooltipAlign.center,
414+
tooltipHorizontalOffset = tooltipHorizontalOffset ?? 0,
411415
maxContentWidth = maxContentWidth ?? 120,
412416
getTooltipItems = getTooltipItems ?? defaultScatterTooltipItem,
413417
fitInsideHorizontally = fitInsideHorizontally ?? false,
@@ -425,6 +429,12 @@ class ScatterTouchTooltipData with EquatableMixin {
425429
/// Applies a padding for showing contents inside the tooltip.
426430
final EdgeInsets tooltipPadding;
427431

432+
/// Controls showing tooltip on left side, right side or center aligned with spot, default is center
433+
final FLTooltipAlign tooltipAlign;
434+
435+
/// Applies horizontal offset for showing tooltip, default is zero.
436+
final double tooltipHorizontalOffset;
437+
428438
/// Restricts the tooltip's width.
429439
final double maxContentWidth;
430440

lib/src/chart/scatter_chart/scatter_chart_painter.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,26 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> {
251251
final tooltipWidth = width + tooltipData.tooltipPadding.horizontal;
252252
final tooltipHeight = height + tooltipData.tooltipPadding.vertical;
253253

254+
double tooltipLeft;
255+
switch (tooltipData.tooltipAlign) {
256+
case FLTooltipAlign.center:
257+
tooltipLeft = mostTopOffset.dx -
258+
(tooltipWidth / 2) +
259+
tooltipData.tooltipHorizontalOffset;
260+
break;
261+
case FLTooltipAlign.right:
262+
tooltipLeft = mostTopOffset.dx + tooltipData.tooltipHorizontalOffset;
263+
break;
264+
case FLTooltipAlign.left:
265+
tooltipLeft = mostTopOffset.dx -
266+
tooltipWidth +
267+
tooltipData.tooltipHorizontalOffset;
268+
break;
269+
}
270+
254271
/// draw the background rect with rounded radius
255272
var rect = Rect.fromLTWH(
256-
mostTopOffset.dx - (tooltipWidth / 2),
273+
tooltipLeft,
257274
mostTopOffset.dy -
258275
tooltipHeight -
259276
showOnSpot.radius -

0 commit comments

Comments
 (0)