Skip to content

Commit 5f782b6

Browse files
dhiyaaulauliyaaimaNNeo
authored andcommitted
Move fitInside offset calculation to AxisChartHelper
1 parent 60a41b9 commit 5f782b6

File tree

2 files changed

+60
-55
lines changed

2 files changed

+60
-55
lines changed

lib/src/chart/base/axis_chart/axis_chart_helper.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import 'package:fl_chart/fl_chart.dart';
12
import 'package:fl_chart/src/utils/utils.dart';
3+
import 'package:flutter/material.dart';
24

35
class AxisChartHelper {
46
factory AxisChartHelper() {
@@ -51,4 +53,52 @@ class AxisChartHelper {
5153
yield max;
5254
}
5355
}
56+
57+
/// Calculate translate offset to keep [SideTitle] child
58+
/// placed inside its corresponding axis.
59+
/// The offset will translate the child to the closest edge inside
60+
/// of the corresponding axis bounding box
61+
Offset calcFitInsideOffset({
62+
required AxisSide axisSide,
63+
required double? childSize,
64+
required double parentAxisSize,
65+
required double axisPosition,
66+
required double distanceFromEdge,
67+
}) {
68+
if (childSize == null) return Offset.zero;
69+
70+
// Find title alignment along its axis
71+
final axisMid = parentAxisSize / 2;
72+
final mainAxisAligment = (axisPosition - axisMid).isNegative
73+
? MainAxisAlignment.start
74+
: MainAxisAlignment.end;
75+
76+
// Find if child widget overflowed outside the chart
77+
late bool isOverflowed;
78+
if (mainAxisAligment == MainAxisAlignment.start) {
79+
isOverflowed = (axisPosition - (childSize / 2)).isNegative;
80+
} else {
81+
isOverflowed = (axisPosition + (childSize / 2)) > parentAxisSize;
82+
}
83+
84+
if (isOverflowed == false) return Offset.zero;
85+
86+
// Calc offset if child overflowed
87+
late double offset;
88+
if (mainAxisAligment == MainAxisAlignment.start) {
89+
offset = (childSize / 2) - axisPosition + distanceFromEdge;
90+
} else {
91+
offset =
92+
-(childSize / 2) + (parentAxisSize - axisPosition) - distanceFromEdge;
93+
}
94+
95+
switch (axisSide) {
96+
case AxisSide.left:
97+
case AxisSide.right:
98+
return Offset(0, offset);
99+
case AxisSide.top:
100+
case AxisSide.bottom:
101+
return Offset(offset, 0);
102+
}
103+
}
54104
}

lib/src/chart/base/axis_chart/axis_chart_widgets.dart

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:fl_chart/fl_chart.dart';
2+
import 'package:fl_chart/src/chart/base/axis_chart/axis_chart_helper.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flutter/scheduler.dart';
45

@@ -62,8 +63,6 @@ class _SideTitleWidgetState extends State<SideTitleWidget> {
6263
return Alignment.centerLeft;
6364
case AxisSide.bottom:
6465
return Alignment.topCenter;
65-
default:
66-
throw StateError('Invalid side');
6766
}
6867
}
6968

@@ -77,64 +76,12 @@ class _SideTitleWidgetState extends State<SideTitleWidget> {
7776
return EdgeInsets.only(left: widget.space);
7877
case AxisSide.bottom:
7978
return EdgeInsets.only(top: widget.space);
80-
default:
81-
throw StateError('Invalid side');
82-
}
83-
}
84-
85-
/// Calculate translate offset to keep child
86-
/// placed inside its corresponding axis.
87-
/// The offset will translate the child to the closest edge inside
88-
/// of the parent
89-
Offset _getOffset() {
90-
if (!widget.fitInside.enabled || _childSize == null) return Offset.zero;
91-
92-
final parentAxisSize = widget.fitInside.parentAxisSize;
93-
final axisPosition = widget.fitInside.axisPosition;
94-
95-
// Find title alignment along its axis
96-
final axisMid = parentAxisSize / 2;
97-
final mainAxisAligment = (axisPosition - axisMid).isNegative
98-
? MainAxisAlignment.start
99-
: MainAxisAlignment.end;
100-
101-
// Find if child widget overflowed outside the chart
102-
final childSize = _childSize!;
103-
late bool isOverflowed;
104-
if (mainAxisAligment == MainAxisAlignment.start) {
105-
isOverflowed = (axisPosition - (childSize / 2)).isNegative;
106-
} else {
107-
isOverflowed = (axisPosition + (childSize / 2)) > parentAxisSize;
108-
}
109-
110-
if (isOverflowed == false) return Offset.zero;
111-
112-
// Calc offset if child overflowed
113-
late double offset;
114-
if (mainAxisAligment == MainAxisAlignment.start) {
115-
offset =
116-
(childSize / 2) - axisPosition + widget.fitInside.distanceFromEdge;
117-
} else {
118-
offset = -(childSize / 2) +
119-
(parentAxisSize - axisPosition) -
120-
widget.fitInside.distanceFromEdge;
121-
}
122-
123-
switch (widget.axisSide) {
124-
case AxisSide.left:
125-
case AxisSide.right:
126-
return Offset(0, offset);
127-
case AxisSide.top:
128-
case AxisSide.bottom:
129-
return Offset(offset, 0);
13079
}
13180
}
13281

13382
/// Calculate child width/height
13483
final GlobalKey widgetKey = GlobalKey();
135-
13684
double? _childSize;
137-
13885
void _getChildSize(_) {
13986
// If fitInside is false, no need to find child size
14087
if (!widget.fitInside.enabled) return;
@@ -179,7 +126,15 @@ class _SideTitleWidgetState extends State<SideTitleWidget> {
179126
@override
180127
Widget build(BuildContext context) {
181128
return Transform.translate(
182-
offset: _getOffset(),
129+
offset: !widget.fitInside.enabled
130+
? Offset.zero
131+
: AxisChartHelper().calcFitInsideOffset(
132+
axisSide: widget.axisSide,
133+
childSize: _childSize,
134+
parentAxisSize: widget.fitInside.parentAxisSize,
135+
axisPosition: widget.fitInside.axisPosition,
136+
distanceFromEdge: widget.fitInside.distanceFromEdge,
137+
),
183138
child: Transform.rotate(
184139
angle: widget.angle,
185140
child: Container(

0 commit comments

Comments
 (0)