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

Improvement/touch #97

Merged
merged 13 commits into from
Nov 4, 2019
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.4.0
* BIG BREAKING CHANGES
* There is no `FlChart` class anymore, instead use [LineChart](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/line_chart.md), [BarChart](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md), and [PieChart](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/pie_chart.md) directly as a widget.
* Touch handling system is improved and for sure we have some changes, there is no `touchedResultSink` anymore and use `touchCallback` function which is added to each TouchData like ([LineTouchData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/line_chart.md#linetouchdata-read-about-touch-handling)), [read more](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/handle_touches.md).
* `TouchTooltipData` class inside `LineTouchData` and `BarTouchData` renamed to `LineTouchTooltipData` and `BarTouchTooltipData` respectively, and also `TooltipItem` class renamed to `LineTooltipItem` and `BarTooltipItem`.
* `spots` inside `LineTouchResponse` renamed to `lineBarSpots` and type changed from `LineTouchedSpot` to `LineBarSpot`.
* `FlTouchNormapInput` renamed to `FlTouchNormalInput` (fixed typo)


## 0.3.4
* BREAKING CHANGES
* swapped horizontal and vertical semantics in [FlGridData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/base_chart.md#FlGridData), fixed this [issue](https://github.com/imaNNeoFighT/fl_chart/issues/85).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Thank you all!

```kotlin
dependencies:
fl_chart: ^0.3.4
fl_chart: ^0.4.0
```


Expand Down
151 changes: 54 additions & 97 deletions example/lib/bar_chart/samples/bar_chart_sample1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,10 @@ class BarChartSample1State extends State<BarChartSample1> {
final Color barBackgroundColor = const Color(0xff72d8bf);
final Duration animDuration = Duration(milliseconds: 250);

StreamController<BarTouchResponse> barTouchedResultStreamController;

int touchedIndex;

bool isPlaying = false;

@override
void initState() {
super.initState();

barTouchedResultStreamController = StreamController();
barTouchedResultStreamController.stream
.distinct()
.listen((BarTouchResponse response) {
if (response == null) {
return;
}

if (response.spot == null) {
setState(() {
touchedIndex = -1;
});
return;
}

setState(() {
if (response.touchInput is FlLongPressEnd) {
touchedIndex = -1;
} else {
touchedIndex = response.spot.touchedBarGroupPosition;
}
});
});
}

@override
Widget build(BuildContext context) {
return AspectRatio(
Expand All @@ -76,30 +45,24 @@ class BarChartSample1State extends State<BarChartSample1> {
Text(
'Mingguan',
style: TextStyle(
color: const Color(0xff0f4a3c),
fontSize: 24,
fontWeight: FontWeight.bold),
color: const Color(0xff0f4a3c), fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(
height: 4,
),
Text(
'Grafik konsumsi kalori',
style: TextStyle(
color: const Color(0xff379982),
fontSize: 18,
fontWeight: FontWeight.bold),
color: const Color(0xff379982), fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(
height: 38,
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: FlChart(
child: BarChart(isPlaying ? randomData() : mainBarData(),
swapAnimationDuration: animDuration,
chart:
BarChart(isPlaying ? randomData() : mainBarData()),
),
),
),
Expand Down Expand Up @@ -141,6 +104,7 @@ class BarChartSample1State extends State<BarChartSample1> {
bool isTouched = false,
Color barColor = Colors.white,
double width = 22,
List<int> showTooltips = const [],
}) {
return BarChartGroupData(x: x, barRods: [
BarChartRodData(
Expand All @@ -154,13 +118,8 @@ class BarChartSample1State extends State<BarChartSample1> {
color: barBackgroundColor,
),
),
]);
}

@override
void dispose() {
super.dispose();
barTouchedResultStreamController.close();
], showingTooltipIndicators: showTooltips,
);
}

List<BarChartGroupData> showingGroups() => List.generate(7, (i) {
Expand All @@ -187,47 +146,53 @@ class BarChartSample1State extends State<BarChartSample1> {
BarChartData mainBarData() {
return BarChartData(
barTouchData: BarTouchData(
touchTooltipData: TouchTooltipData(
touchTooltipData: BarTouchTooltipData(
tooltipBgColor: Colors.blueGrey,
getTooltipItems: (touchedSpots) {
return touchedSpots.map((touchedSpot) {
String weekDay;
switch (touchedSpot.spot.x.toInt()) {
case 0:
weekDay = 'Monday';
break;
case 1:
weekDay = 'Tuesday';
break;
case 2:
weekDay = 'Wednesday';
break;
case 3:
weekDay = 'Thursday';
break;
case 4:
weekDay = 'Friday';
break;
case 5:
weekDay = 'Saturday';
break;
case 6:
weekDay = 'Sunday';
break;
}
return TooltipItem(
weekDay + '\n' + (touchedSpot.spot.y - 1).toString(),
TextStyle(color: Colors.yellow));
}).toList();
getTooltipItem: (group, groupIndex, rod, rodIndex) {
String weekDay;
switch (group.x.toInt()) {
case 0:
weekDay = 'Monday';
break;
case 1:
weekDay = 'Tuesday';
break;
case 2:
weekDay = 'Wednesday';
break;
case 3:
weekDay = 'Thursday';
break;
case 4:
weekDay = 'Friday';
break;
case 5:
weekDay = 'Saturday';
break;
case 6:
weekDay = 'Sunday';
break;
}
return BarTooltipItem(weekDay + '\n' + (rod.y - 1).toString(),
TextStyle(color: Colors.yellow));
}),
touchResponseSink: barTouchedResultStreamController.sink,
touchCallback: (barTouchResponse) {
setState(() {
if (barTouchResponse.spot != null &&
barTouchResponse.touchInput is! FlPanEnd &&
barTouchResponse.touchInput is! FlLongPressEnd) {
touchedIndex = barTouchResponse.spot.touchedBarGroupIndex;
} else {
touchedIndex = -1;
}
});
}
),
titlesData: FlTitlesData(
show: true,
bottomTitles: SideTitles(
showTitles: true,
textStyle: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 14),
textStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 14),
margin: 16,
getTitles: (double value) {
switch (value.toInt()) {
Expand Down Expand Up @@ -269,8 +234,7 @@ class BarChartSample1State extends State<BarChartSample1> {
show: true,
bottomTitles: SideTitles(
showTitles: true,
textStyle: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 14),
textStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 14),
margin: 16,
getTitles: (double value) {
switch (value.toInt()) {
Expand Down Expand Up @@ -303,32 +267,25 @@ class BarChartSample1State extends State<BarChartSample1> {
switch (i) {
case 0:
return makeGroupData(0, Random().nextInt(15).toDouble() + 6,
barColor: widget.availableColors[
Random().nextInt(widget.availableColors.length)]);
barColor: widget.availableColors[Random().nextInt(widget.availableColors.length)]);
case 1:
return makeGroupData(1, Random().nextInt(15).toDouble() + 6,
barColor: widget.availableColors[
Random().nextInt(widget.availableColors.length)]);
barColor: widget.availableColors[Random().nextInt(widget.availableColors.length)]);
case 2:
return makeGroupData(2, Random().nextInt(15).toDouble() + 6,
barColor: widget.availableColors[
Random().nextInt(widget.availableColors.length)]);
barColor: widget.availableColors[Random().nextInt(widget.availableColors.length)]);
case 3:
return makeGroupData(3, Random().nextInt(15).toDouble() + 6,
barColor: widget.availableColors[
Random().nextInt(widget.availableColors.length)]);
barColor: widget.availableColors[Random().nextInt(widget.availableColors.length)]);
case 4:
return makeGroupData(4, Random().nextInt(15).toDouble() + 6,
barColor: widget.availableColors[
Random().nextInt(widget.availableColors.length)]);
barColor: widget.availableColors[Random().nextInt(widget.availableColors.length)]);
case 5:
return makeGroupData(5, Random().nextInt(15).toDouble() + 6,
barColor: widget.availableColors[
Random().nextInt(widget.availableColors.length)]);
barColor: widget.availableColors[Random().nextInt(widget.availableColors.length)]);
case 6:
return makeGroupData(6, Random().nextInt(15).toDouble() + 6,
barColor: widget.availableColors[
Random().nextInt(widget.availableColors.length)]);
barColor: widget.availableColors[Random().nextInt(widget.availableColors.length)]);
default:
return null;
}
Expand Down
Loading