Created
May 2, 2021 20:26
-
-
Save imaNNeo/04b1a38b5948c6b295880dbb14131507 to your computer and use it in GitHub Desktop.
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 'package:flutter/material.dart'; | |
import 'package:fl_chart/fl_chart.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class YourData { | |
final double date; | |
final double value; | |
YourData(this.date, this.value); | |
} | |
class MyApp extends StatelessWidget { | |
final List<YourData> myDataList = [ | |
YourData(202012.0, 8465.696263643998), | |
YourData(202101.0, 7771.7867338371125), | |
YourData(202102.0, 8789.520710887211), | |
]; | |
List<FlSpot> mapMyDataToFlSpots(List<YourData> data) { | |
return data.asMap().entries.map((entry) { | |
final index = entry.key; | |
final myData = entry.value; | |
return FlSpot(index.toDouble(), myData.value); | |
}).toList(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Scaffold( | |
body: Center( | |
child: Padding( | |
padding: EdgeInsets.only(right: 60, left: 22), | |
child: AspectRatio( | |
aspectRatio: 1, | |
child: LineChart( | |
LineChartData( | |
lineBarsData: [ | |
LineChartBarData( | |
spots: mapMyDataToFlSpots(myDataList) | |
) | |
], | |
titlesData: FlTitlesData( | |
show: true, | |
bottomTitles: SideTitles( | |
showTitles: true, | |
getTitles: (x) { | |
final myDataAtThisX = myDataList[x.toInt()]; | |
return myDataAtThisX.date.toString(); | |
} | |
) | |
) | |
) | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Author
imaNNeo
commented
May 2, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment