Skip to content

Flag to position labels at the beginning of each interval #2369

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class DateTimeAxis extends ChartAxis {
super.labelStyle,
this.dateFormat,
this.intervalType = DateTimeIntervalType.auto,
this.labelsAtBeginning = false,
super.interactiveTooltip,
this.labelFormat,
this.minimum,
Expand Down Expand Up @@ -111,6 +112,25 @@ class DateTimeAxis extends ChartAxis {
/// ```
final String? labelFormat;

/// If true and an [intervalType] is set to a value different than `DateTimeIntervalType.auto`
/// the labels are positioned at the beginning of each interval, e.g., at the first of a month.
/// Otherwise, for `DateTimeIntervalType.years` and `DateTimeIntervalType.months` the labels are placed dynamically
///
/// Defaults to `false`.
///
/// Also refer [DateTimeIntervalType].
/// ```dart
/// Widget build(BuildContext context) {
/// return Container(
/// child: SfCartesianChart(
/// primaryXAxis:
/// DateTimeAxis(intervalType: DateTimeIntervalType.years, labelsAtBeginning: true),
/// )
/// );
/// }
/// ```
final bool labelsAtBeginning;

/// Customizes the date-time axis intervals. Intervals can be set to days,
/// hours, milliseconds, minutes, months, seconds, years, and auto. If it is
/// set to auto, interval type will be decided based on the data.
Expand Down Expand Up @@ -342,6 +362,7 @@ class DateTimeAxis extends ChartAxis {
..dateFormat = dateFormat
..labelFormat = labelFormat
..intervalType = intervalType
..labelsAtBeginning = labelsAtBeginning
..minimum = minimum
..maximum = maximum
..initialVisibleMinimum = initialVisibleMinimum
Expand All @@ -360,6 +381,7 @@ class DateTimeAxis extends ChartAxis {
..dateFormat = dateFormat
..labelFormat = labelFormat
..intervalType = intervalType
..labelsAtBeginning = labelsAtBeginning
..minimum = minimum
..maximum = maximum
..autoScrollingDeltaType = autoScrollingDeltaType
Expand Down Expand Up @@ -410,6 +432,15 @@ class RenderDateTimeAxis extends RenderChartAxis {
}
}

bool get labelsAtBeginning => _labelsAtBeginning;
bool _labelsAtBeginning = false;
set labelsAtBeginning(bool value) {
if (_labelsAtBeginning != value) {
_labelsAtBeginning = value;
markNeedsLayout();
}
}

DateTime? get minimum => _minimum;
DateTime? _minimum;
set minimum(DateTime? value) {
Expand Down Expand Up @@ -1236,13 +1267,13 @@ class RenderDateTimeAxis extends RenderChartAxis {
case DateTimeIntervalType.years:
final int year =
((date.year / visibleInterval).floor() * visibleInterval).floor();
date = DateTime(year, date.month, date.day);
date = DateTime(year, labelsAtBeginning ? 1 : date.month, labelsAtBeginning ? 1 : date.day);
break;

case DateTimeIntervalType.months:
final int month =
((date.month / visibleInterval) * visibleInterval).floor();
date = DateTime(date.year, month, date.day);
date = DateTime(date.year, month, labelsAtBeginning ? 1 : date.day);
break;

case DateTimeIntervalType.days:
Expand Down