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

fix: Use num instead of int for Horizontal Decoration #89

Open
wants to merge 10 commits into
base: master
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
2 changes: 1 addition & 1 deletion example/lib/charts/showcase_chart_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ final List<ChartState<bool>> _chartStates = [
verticalValuesPadding: const EdgeInsets.only(top: 24.0),
horizontalAxisValueFromValue: (value) => '${value + 1}h',
verticalAxisValueFromIndex: (value) =>
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][value],
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][value.toInt()],
textStyle: TextStyle(fontSize: 14.0, color: Colors.black45),
),
],
Expand Down
10 changes: 5 additions & 5 deletions lib/chart/render/decorations/horizontal_axis_decoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ enum HorizontalLegendPosition {
end,
}

typedef AxisValueFromValue = String Function(int value);
typedef AxisValueFromValue = String Function(num value);

/// Default axis generator, it will just take current index, convert it to string and return it.
String defaultAxisValue(int index) => '$index';
String defaultAxisValue(num index) => '$index';

typedef ShowLineForValue = bool Function(int value);
typedef ShowLineForValue = bool Function(num value);

/// Decoration for drawing horizontal lines on the chart, decoration can add horizontal axis legend
///
Expand Down Expand Up @@ -146,7 +146,7 @@ class HorizontalAxisDecoration extends DecorationPainter {
final _maxValue = state.data.maxValue - state.data.minValue;

for (var i = 0; i * axisStep <= _maxValue; i++) {
final _defaultValue = (axisStep * i + state.data.minValue).toInt();
final _defaultValue = axisStep * i + state.data.minValue;
final _value = axisValue.call(_defaultValue);
if ((_longestText?.length ?? 0) < _value.length) {
_longestText = _value;
Expand All @@ -168,7 +168,7 @@ class HorizontalAxisDecoration extends DecorationPainter {
final gridPath = Path();

for (var i = 0; i * scale * axisStep <= scale * _maxValue; i++) {
final _defaultValue = (axisStep * i + state.data.minValue).toInt();
final _defaultValue = axisStep * i + state.data.minValue;

final _isPositionStart = legendPosition == HorizontalLegendPosition.start;
final _startLine = _isPositionStart
Expand Down
11 changes: 9 additions & 2 deletions test/golden/complex/showcase_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,15 @@ void main() {
bottom: -8.0, right: 8.0, left: 8.0),
verticalValuesPadding: const EdgeInsets.only(top: 24.0),
horizontalAxisValueFromValue: (value) => '${value + 1}h',
verticalAxisValueFromIndex: (value) =>
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][value],
verticalAxisValueFromIndex: (value) => [
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
'Sun'
][value.toInt()],
textStyle: defaultTextStyle.copyWith(
fontSize: 12.0, color: Colors.black45),
),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions test/golden/decoration/horizontal_decoration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ void main() {
],
),
),
GoldenTestScenario(
name: 'Decimal Lines',
child: getDefaultChart(
backgroundDecorations: [
HorizontalAxisDecoration(
showLineForValue: (value) => true,
lineWidth: 3.0,
axisStep: 4.5,
),
],
),
),
]);
});
}