Skip to content

Commit 1339d6f

Browse files
tamasappsimaNNeo
authored andcommitted
Add horizontal alignment and offset to bar chart tooltip
1 parent 8e302bb commit 1339d6f

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class BarChartSample1State extends State<BarChartSample1> {
163163
barTouchData: BarTouchData(
164164
touchTooltipData: BarTouchTooltipData(
165165
tooltipBgColor: Colors.blueGrey,
166+
tooltipAlign: BarTooltipAlign.right,
167+
tooltipMargin: -10,
166168
getTooltipItem: (group, groupIndex, rod, rodIndex) {
167169
String weekDay;
168170
switch (group.x) {

lib/src/chart/bar_chart/bar_chart_data.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,18 @@ 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+
683695
/// Holds representation data for showing tooltip popup on top of rods.
684696
class BarTouchTooltipData with EquatableMixin {
685697
/// if [BarTouchData.handleBuiltInTouches] is true,
@@ -700,6 +712,8 @@ class BarTouchTooltipData with EquatableMixin {
700712
double? tooltipRoundedRadius,
701713
EdgeInsets? tooltipPadding,
702714
double? tooltipMargin,
715+
BarTooltipAlign? tooltipAlign,
716+
double? tooltipHorizontalOffset,
703717
double? maxContentWidth,
704718
GetBarTooltipItem? getTooltipItem,
705719
bool? fitInsideHorizontally,
@@ -719,6 +733,8 @@ class BarTouchTooltipData with EquatableMixin {
719733
direction = direction ?? TooltipDirection.auto,
720734
rotateAngle = rotateAngle ?? 0.0,
721735
tooltipBorder = tooltipBorder ?? BorderSide.none,
736+
tooltipAlign = tooltipAlign ?? BarTooltipAlign.center,
737+
tooltipHorizontalOffset = tooltipHorizontalOffset ?? 0,
722738
super();
723739

724740
/// The tooltip background color.
@@ -733,6 +749,12 @@ class BarTouchTooltipData with EquatableMixin {
733749
/// Applies a bottom margin for showing tooltip on top of rods.
734750
final double tooltipMargin;
735751

752+
/// Controls showing tooltip on left side, right side or center aligned with rod, default is center
753+
final BarTooltipAlign tooltipAlign;
754+
755+
/// Applies horizontal offset for showing tooltip, default is zero.
756+
final double tooltipHorizontalOffset;
757+
736758
/// Restricts the tooltip's width.
737759
final double maxContentWidth;
738760

lib/src/chart/bar_chart/bar_chart_painter.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,26 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
391391
? barTopY - tooltipHeight - tooltipData.tooltipMargin
392392
: barBottomY + tooltipData.tooltipMargin;
393393

394+
double tooltipLeft;
395+
switch (tooltipData.tooltipAlign) {
396+
case BarTooltipAlign.center:
397+
tooltipLeft = barOffset.dx -
398+
(tooltipWidth / 2) +
399+
tooltipData.tooltipHorizontalOffset;
400+
break;
401+
case BarTooltipAlign.right:
402+
tooltipLeft = barOffset.dx + tooltipData.tooltipHorizontalOffset;
403+
break;
404+
case BarTooltipAlign.left:
405+
tooltipLeft =
406+
barOffset.dx - tooltipWidth + tooltipData.tooltipHorizontalOffset;
407+
break;
408+
}
409+
394410
/// draw the background rect with rounded radius
395411
// ignore: omit_local_variable_types
396412
Rect rect = Rect.fromLTWH(
397-
barOffset.dx,
413+
tooltipLeft,
398414
tooltipTop,
399415
tooltipWidth,
400416
tooltipHeight,

0 commit comments

Comments
 (0)