Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculating side titles and horizontal lines from minY value #1713

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class SideTitles with EquatableMixin {
this.getTitlesWidget = defaultGetTitle,
this.reservedSize = 22,
this.interval,
this.startMin = false,
this.minIncluded = true,
this.maxIncluded = true,
}) : assert(interval != 0, "SideTitles.interval couldn't be zero");
Expand All @@ -209,6 +210,9 @@ class SideTitles with EquatableMixin {
/// we try to find a suitable value to set as [interval] under the hood.
final double? interval;

/// If [startMin] is true, then the interval will start from the min value.
final bool startMin;

/// If true (default), a title for the minimum data value is included
/// independent of the sampling interval
final bool minIncluded;
Expand Down Expand Up @@ -587,6 +591,7 @@ class FlGridData with EquatableMixin {
this.verticalInterval,
this.getDrawingVerticalLine = defaultGridLine,
this.checkToShowVerticalLine = showAllGrids,
this.startMin = false,
}) : assert(
horizontalInterval != 0,
"FlGridData.horizontalInterval couldn't be zero",
Expand Down Expand Up @@ -623,6 +628,9 @@ class FlGridData with EquatableMixin {
/// Gives you a x value, and gets a boolean that determines showing or hiding specified line.
final CheckToShowGrid checkToShowVerticalLine;

/// If true then the horizontal interval will start from the min value.
final bool startMin;

/// Lerps a [FlGridData] based on [t] value, check [Tween.lerp].
static FlGridData lerp(FlGridData a, FlGridData b, double t) => FlGridData(
show: b.show,
Expand Down
11 changes: 9 additions & 2 deletions lib/src/chart/base/axis_chart/axis_chart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ class AxisChartHelper {
bool maxIncluded = true,
required double baseLine,
required double interval,
bool startMin = false,
}) sync* {
final initialValue = Utils()
.getBestInitialIntervalValue(min, max, interval, baseline: baseLine);
final initialValue = startMin
? min
: Utils().getBestInitialIntervalValue(
min,
max,
interval,
baseline: baseLine,
);
var axisSeek = initialValue;
final firstPositionOverlapsWithMin = axisSeek == min;
if (!minIncluded && firstPositionOverlapsWithMin) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/chart/base/axis_chart/axis_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ abstract class AxisChartPainter<D extends AxisChartData>
maxIncluded: false,
baseLine: data.baselineX,
interval: verticalInterval,
startMin: data.gridData.startMin,
);
for (final axisValue in axisValues) {
if (!data.gridData.checkToShowVerticalLine(axisValue)) {
Expand Down Expand Up @@ -117,6 +118,7 @@ abstract class AxisChartPainter<D extends AxisChartData>
maxIncluded: false,
baseLine: data.baselineY,
interval: horizontalInterval,
startMin: data.gridData.startMin,
);
for (final axisValue in axisValues) {
if (!data.gridData.checkToShowHorizontalLine(axisValue)) {
Expand All @@ -125,6 +127,7 @@ abstract class AxisChartPainter<D extends AxisChartData>
final flLine = data.gridData.getDrawingHorizontalLine(axisValue);

final bothY = getPixelY(axisValue, viewSize, holder);

const x1 = 0.0;
final y1 = bothY;
final x2 = viewSize.width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class _SideTitlesWidgetState extends State<SideTitlesWidget> {
maxIncluded: sideTitles.maxIncluded,
baseLine: axisBaseLine,
interval: interval,
startMin: sideTitles.startMin,
);
axisPositions = axisValues.map((axisValue) {
final axisDiff = axisMax - axisMin;
Expand Down