Created
August 22, 2021 19:31
-
-
Save imaNNeo/a09a202fac8b30ce0dfc6e141478f6f4 to your computer and use it in GitHub Desktop.
Custom filtered data points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:ui'; | |
| import 'package:fl_chart/fl_chart.dart'; | |
| import 'package:flutter/foundation.dart'; | |
| import 'package:flutter/material.dart'; | |
| final rawData = [ | |
| { | |
| 'week_day': 'Sat', | |
| 'date': '2022-02-01', | |
| 'value': 11, | |
| }, | |
| { | |
| 'week_day': 'Sun', | |
| 'date': '2022-02-02', | |
| 'value': 15, | |
| }, | |
| { | |
| 'week_day': 'Mon', | |
| 'date': '2022-02-03', | |
| 'value': 13, | |
| }, | |
| { | |
| 'week_day': 'Tue', | |
| 'date': '2022-02-04', | |
| 'value': 16, | |
| }, | |
| { | |
| 'week_day': 'Wed', | |
| 'date': '2022-02-05', | |
| 'value': 14, | |
| }, | |
| { | |
| 'week_day': 'Thu', | |
| 'date': '2022-02-06', | |
| 'value': 15, | |
| }, | |
| { | |
| 'week_day': 'Fri', | |
| 'date': '2022-02-07', | |
| 'value': 18, | |
| }, | |
| { | |
| 'week_day': 'Sat', | |
| 'date': '2022-02-08', | |
| 'value': 16, | |
| }, | |
| { | |
| 'week_day': 'Sun', | |
| 'date': '2022-02-09', | |
| 'value': 19, | |
| }, | |
| { | |
| 'week_day': 'Mon', | |
| 'date': '2022-02-10', | |
| 'value': 17, | |
| }, | |
| { | |
| 'week_day': 'Tue', | |
| 'date': '2022-02-11', | |
| 'value': 15, | |
| }, | |
| { | |
| 'week_day': 'Wen', | |
| 'date': '2022-02-12', | |
| 'value': 8, | |
| }, | |
| { | |
| 'week_day': 'Thu', | |
| 'date': '2022-02-13', | |
| 'value': 16, | |
| }, | |
| { | |
| 'week_day': 'Sat', | |
| 'date': '2022-02-14', | |
| 'value': 18, | |
| }, | |
| { | |
| 'week_day': 'Sun', | |
| 'date': '2022-02-15', | |
| 'value': 19, | |
| }, | |
| { | |
| 'week_day': 'Mon', | |
| 'date': '2022-02-16', | |
| 'value': 14, | |
| } | |
| ]; | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| final dataPoints = rawData.map((e) => RawDataPoint(e)).toList(); | |
| final skippedWeekends = dataPoints.where((dataPoint) => dataPoint.isNotWeekend).toList(); | |
| return MaterialApp( | |
| title: 'FlChart Demo', | |
| showPerformanceOverlay: false, | |
| home: MyHomePage(showingPoints: skippedWeekends), | |
| ); | |
| } | |
| } | |
| class RawDataPoint { | |
| final String weekDay; | |
| final String date; | |
| final int value; | |
| RawDataPoint(Map<String, dynamic> json) | |
| : weekDay = json['week_day'], | |
| date = json['date'], | |
| value = json['value']; | |
| bool get isWeekend => weekDay == 'Sat' || weekDay == 'Sun'; | |
| bool get isNotWeekend => !isWeekend; | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| final List<RawDataPoint> showingPoints; | |
| MyHomePage({Key? key, required this.showingPoints}) : super(key: key); | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| @override | |
| Widget build(BuildContext context) { | |
| var flSpots = widget.showingPoints.asMap().entries.map((e) { | |
| final index = e.key; | |
| final dataPoint = e.value; | |
| return FlSpot(index.toDouble(), dataPoint.value.toDouble()); | |
| }).toList(); | |
| return Scaffold( | |
| body: SafeArea( | |
| child: Center( | |
| child: AspectRatio( | |
| aspectRatio: 1.8, | |
| child: Container( | |
| margin: const EdgeInsets.only(right: 42.0, left: 28), | |
| child: LineChart( | |
| LineChartData( | |
| lineBarsData: [ | |
| LineChartBarData( | |
| spots: flSpots, | |
| ), | |
| ], | |
| minY: 4, | |
| maxY: 20, | |
| titlesData: FlTitlesData( | |
| show: true, | |
| leftTitles: SideTitles( | |
| showTitles: true, | |
| checkToShowTitle: (minValue, maxValue, sideTitles, appliedInterval, value) => value % 2 == 0, | |
| ), | |
| bottomTitles: SideTitles( | |
| showTitles: true, | |
| getTitles: (value) { | |
| final dataPoint = widget.showingPoints[value.toInt()]; | |
| return '${dataPoint.date}'; | |
| }, | |
| rotateAngle: -35, | |
| getTextStyles: (context, value) => TextStyle(fontSize: 10), | |
| )), | |
| gridData: FlGridData( | |
| show: false, | |
| ), | |
| lineTouchData: LineTouchData( | |
| enabled: true, | |
| touchTooltipData: LineTouchTooltipData( | |
| getTooltipItems: (List<LineBarSpot> touchedSpots) { | |
| return touchedSpots.map((lineBarSpot) { | |
| final dataSpot = widget.showingPoints[lineBarSpot.spotIndex]; | |
| return LineTooltipItem(dataSpot.value.toString(), TextStyle(color: Colors.white), children: [ | |
| TextSpan( | |
| text: '\n${dataSpot.weekDay}', | |
| style: TextStyle( | |
| color: Colors.white, | |
| fontWeight: FontWeight.bold, | |
| ), | |
| ) | |
| ]); | |
| }).toList(); | |
| }, | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
custom_filtered_data_points.mp4