Skip to content

Commit

Permalink
Merge pull request #30 from jja08111/amount_y_label
Browse files Browse the repository at this point in the history
Update amount chart y labels
  • Loading branch information
jja08111 authored May 27, 2022
2 parents 90ac1d3 + e12a6c7 commit df96a5f
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@ class AmountYLabelPainter extends YLabelPainter {

@override
void drawYLabels(Canvas canvas, Size size) {
final hourSuffix = translations.shortHour;
final double interval =
final String hourSuffix = translations.shortHour;
final double labelInterval =
(size.height - kXLabelHeight) / (topHour - bottomHour);
final int hourDuration = topHour - bottomHour;
final int timeStep;
if (hourDuration >= 12) {
timeStep = 4;
} else if (hourDuration >= 8) {
timeStep = 2;
} else {
timeStep = 1;
}
double posY = 0;

for (int time = topHour; time >= bottomHour; --time) {
drawYText(canvas, size,
time == bottomHour ? '0 $hourSuffix' : '$time $hourSuffix', posY);
for (int time = topHour; time >= bottomHour; time = time - timeStep) {
drawYText(canvas, size, '$time $hourSuffix', posY);
if (topHour > time && time > bottomHour) {
drawHorizontalLine(canvas, size, posY);
}

posY += interval;
posY += labelInterval * timeStep;
}
}
}
10 changes: 3 additions & 7 deletions lib/src/components/utils/time_data_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,9 @@ mixin TimeDataProcessor {
}

void _calcAmountPivotHeights(List<DateTimeRange> dataList) {
const double infinity = 10000.0;
final int len = dataList.length;

double maxResult = 0.0;
double minResult = infinity;
double sum = 0.0;

for (int i = 0; i < len; ++i) {
Expand All @@ -338,15 +336,13 @@ mixin TimeDataProcessor {
dataList[i].end.dateWithoutTime() !=
dataList[i + 1].end.dateWithoutTime()) {
maxResult = max(maxResult, sum);
if (sum > 0.0) {
minResult = min(minResult, sum);
}
sum = 0.0;
}
}

_topHour = maxResult.ceil();
_bottomHour = minResult == infinity ? 0 : max(0, minResult.floor() - 1);
// `_topHour`는 4의 배수가 되도록 한다.
_topHour = ((maxResult.ceil()) / 4).ceil() * 4;
_bottomHour = 0;
}

/// [b]에서 [a]로 흐른 시간을 구한다. 예를 들어 5시에서 3시로 흐른 시간은 22시간이고,
Expand Down
73 changes: 73 additions & 0 deletions test/amount_chart_test/amount_chart_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:time_chart/src/chart.dart';
import 'package:time_chart/time_chart.dart';

void main() {
group('AmountChart y labels', () {
testWidgets('have 4 hours intervals if max amount is over 8 hours',
(tester) async {
await tester.pumpWidget(MaterialApp(
home: TimeChart(
chartType: ChartType.amount,
data: [
DateTimeRange(
start: DateTime(2022, 5, 27, 0, 0),
end: DateTime(2022, 5, 27, 8, 1),
),
],
),
));

await expectLater(
find.byType(Chart),
matchesGoldenFile('golden/y_label_golden1.png'),
skip: !Platform.isMacOS,
);
});

testWidgets('have 2 hours intervals if max amount is in range (4, 8] hours',
(tester) async {
await tester.pumpWidget(MaterialApp(
home: TimeChart(
chartType: ChartType.amount,
data: [
DateTimeRange(
start: DateTime(2022, 5, 27, 0, 0),
end: DateTime(2022, 5, 27, 5, 0),
),
],
),
));

await expectLater(
find.byType(Chart),
matchesGoldenFile('golden/y_label_golden2.png'),
skip: !Platform.isMacOS,
);
});

testWidgets('have 1 hour interval if max amount is below 4 hours',
(tester) async {
await tester.pumpWidget(MaterialApp(
home: TimeChart(
chartType: ChartType.amount,
data: [
DateTimeRange(
start: DateTime(2022, 5, 27, 0, 0),
end: DateTime(2022, 5, 27, 4, 0),
),
],
),
));

await expectLater(
find.byType(Chart),
matchesGoldenFile('golden/y_label_golden3.png'),
skip: !Platform.isMacOS,
);
});
});
}
Binary file added test/amount_chart_test/golden/y_label_golden1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/amount_chart_test/golden/y_label_golden2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/amount_chart_test/golden/y_label_golden3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 42 additions & 2 deletions test/time_data_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Chart _getChart(
}

void main() {
group('Time chart data processor test', () {
group('TimeChart data processor', () {
testWidgets('merge if has overlapping time', (tester) async {
final _MockTimeDataProcessor processor = _MockTimeDataProcessor();
final data = [
Expand Down Expand Up @@ -235,9 +235,11 @@ void main() {
reason: 'default pivot hour is used as `topHour` if there is no data',
);
});
});

group('AmountChart data processor', () {
testWidgets(
'show empty graph if there is no data when chart type is amount',
'shows empty graph if there is no data when chart type is amount',
(tester) async {
final _MockTimeDataProcessor processor = _MockTimeDataProcessor();
final List<DateTimeRange> data = [];
Expand Down Expand Up @@ -305,5 +307,43 @@ void main() {

expect(processor.dayCount!, equals(18));
});

testWidgets('always sets bottomHour to 0', (tester) async {
final _MockTimeDataProcessor processor = _MockTimeDataProcessor();
final data = [
DateTimeRange(
start: DateTime(2021, 12, 1, 0, 12),
end: DateTime(2021, 12, 1, 17, 12),
),
];

processor.process(
data,
chartType: ChartType.amount,
);

expect(processor.bottomHour!, equals(0));
});

testWidgets('sets topHour to 20 if max amount is 17 hours', (tester) async {
final _MockTimeDataProcessor processor = _MockTimeDataProcessor();
final data = [
DateTimeRange(
start: DateTime(2021, 12, 2, 22, 10),
end: DateTime(2021, 12, 2, 23, 20),
),
DateTimeRange(
start: DateTime(2021, 12, 1, 0, 12),
end: DateTime(2021, 12, 1, 17, 12),
),
];

processor.process(
data,
chartType: ChartType.amount,
);

expect(processor.topHour!, equals(20));
});
});
}

0 comments on commit df96a5f

Please sign in to comment.