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

Update LineChartSample5 #1309

Merged
merged 1 commit into from
Apr 15, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* **FEATURE** (by @Motionz-Von)BarChart groupSpace also takes effect when alignment is BarChartAlignment.end or BarChartAlignment.start.
* **FEATURE** (by @Motionz-Von) supports setting line StrokeCap on HorizontalLine/VerticalLine
* **BUGFIX** (by @nav-28) Fix radar chart tick and graph point not matching #1078
* **IMPROVEMENT** (by @imaNNeo) Update LineChartSample5 to demonstrate click to toggle show/hide tooltip, #118

## 0.61.0
* **IMPROVEMENT** (by @imaNNeo) Remove assertion to check to provide only one of `color` or `gradient` property in the [BarChartRodData](https://github.com/imaNNeo/fl_chart/blob/master/repo_files/documentations/bar_chart.md#barchartroddata) and [BackgroundBarChartRodData](https://github.com/imaNNeo/fl_chart/blob/master/repo_files/documentations/bar_chart.md#backgroundbarchartroddata), #1121.
Expand Down
53 changes: 41 additions & 12 deletions example/lib/presentation/samples/line/line_chart_sample5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

class LineChartSample5 extends StatelessWidget {
class LineChartSample5 extends StatefulWidget {
const LineChartSample5({
super.key,
Color? gradientColor1,
Expand All @@ -19,7 +19,12 @@ class LineChartSample5 extends StatelessWidget {
final Color gradientColor3;
final Color indicatorStrokeColor;

List<int> get showIndexes => const [1, 3, 5];
@override
State<LineChartSample5> createState() => _LineChartSample5State();
}

class _LineChartSample5State extends State<LineChartSample5> {
List<int> showingTooltipOnSpots = [1, 3, 5];

List<FlSpot> get allSpots => const [
FlSpot(0, 1),
Expand Down Expand Up @@ -75,7 +80,7 @@ class LineChartSample5 extends StatelessWidget {
Widget build(BuildContext context) {
final lineBarsData = [
LineChartBarData(
showingIndicators: showIndexes,
showingIndicators: showingTooltipOnSpots,
spots: allSpots,
isCurved: true,
barWidth: 4,
Expand All @@ -86,18 +91,18 @@ class LineChartSample5 extends StatelessWidget {
show: true,
gradient: LinearGradient(
colors: [
gradientColor1.withOpacity(0.4),
gradientColor2.withOpacity(0.4),
gradientColor3.withOpacity(0.4),
widget.gradientColor1.withOpacity(0.4),
widget.gradientColor2.withOpacity(0.4),
widget.gradientColor3.withOpacity(0.4),
],
),
),
dotData: FlDotData(show: false),
gradient: LinearGradient(
colors: [
gradientColor1,
gradientColor2,
gradientColor3,
widget.gradientColor1,
widget.gradientColor2,
widget.gradientColor3,
],
stops: const [0.1, 0.4, 0.9],
),
Expand All @@ -116,7 +121,7 @@ class LineChartSample5 extends StatelessWidget {
child: LayoutBuilder(builder: (context, constraints) {
return LineChart(
LineChartData(
showingTooltipIndicators: showIndexes.map((index) {
showingTooltipIndicators: showingTooltipOnSpots.map((index) {
return ShowingTooltipIndicators([
LineBarSpot(
tooltipsOnBar,
Expand All @@ -126,7 +131,31 @@ class LineChartSample5 extends StatelessWidget {
]);
}).toList(),
lineTouchData: LineTouchData(
enabled: false,
enabled: true,
handleBuiltInTouches: false,
touchCallback:
(FlTouchEvent event, LineTouchResponse? response) {
if (response == null || response.lineBarSpots == null) {
return;
}
if (event is FlTapUpEvent) {
final spotIndex = response.lineBarSpots!.first.spotIndex;
setState(() {
if (showingTooltipOnSpots.contains(spotIndex)) {
showingTooltipOnSpots.remove(spotIndex);
} else {
showingTooltipOnSpots.add(spotIndex);
}
});
}
},
mouseCursorResolver:
(FlTouchEvent event, LineTouchResponse? response) {
if (response == null || response.lineBarSpots == null) {
return SystemMouseCursors.basic;
}
return SystemMouseCursors.click;
},
getTouchedSpotIndicator:
(LineChartBarData barData, List<int> spotIndexes) {
return spotIndexes.map((index) {
Expand All @@ -145,7 +174,7 @@ class LineChartSample5 extends StatelessWidget {
percent / 100,
),
strokeWidth: 2,
strokeColor: indicatorStrokeColor,
strokeColor: widget.indicatorStrokeColor,
),
),
);
Expand Down