Created
March 8, 2022 16:40
-
-
Save prajwal27/3b061fbeb020e62a91d1a2dbb7dcc91c to your computer and use it in GitHub Desktop.
This file contains 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:fl_chart/fl_chart.dart'; | |
import 'package:flutter/material.dart'; | |
class LineChartSample7 extends StatelessWidget { | |
const LineChartSample7({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return AspectRatio( | |
aspectRatio: 2.6, | |
child: Padding( | |
padding: const EdgeInsets.only(left: 28, right: 18), | |
child: LineChart( | |
LineChartData( | |
lineTouchData: LineTouchData( | |
enabled: true, | |
touchTooltipData: LineTouchTooltipData( | |
tooltipBgColor: Colors.blueAccent, | |
maxContentWidth: 600, | |
tooltipPadding: const EdgeInsets.all(8), | |
getTooltipItems: (List<LineBarSpot> touchedBarSpots) { | |
return touchedBarSpots.map((barSpot) { | |
final flSpot = barSpot; | |
// if (flSpot.x == 0 || flSpot.x == 6) { | |
// return null; | |
// } | |
return LineTooltipItem( | |
flSpot.barIndex % 2 == 0 | |
? 'Company Name' | |
: 'Longgest Company Name', | |
const TextStyle( | |
color: Colors.white, | |
fontWeight: FontWeight.bold, | |
), | |
textAlign: TextAlign.center, | |
children: [ | |
TextSpan( | |
text: flSpot.y.toString(), | |
style: TextStyle( | |
color: Colors.grey[100], | |
fontWeight: FontWeight.normal, | |
), | |
), | |
const TextSpan( | |
text: ' k ', | |
style: TextStyle( | |
fontStyle: FontStyle.italic, | |
fontWeight: FontWeight.normal, | |
), | |
), | |
const TextSpan( | |
text: 'calories', | |
style: TextStyle( | |
fontWeight: FontWeight.normal, | |
), | |
), | |
], | |
); | |
}).toList(); | |
}), | |
), | |
lineBarsData: [ | |
LineChartBarData( | |
spots: const [ | |
FlSpot(0, 4), | |
FlSpot(1, 3.5), | |
FlSpot(2, 4.5), | |
FlSpot(3, 1), | |
FlSpot(4, 4), | |
FlSpot(5, 6), | |
FlSpot(6, 6.5), | |
FlSpot(7, 6), | |
FlSpot(8, 4), | |
FlSpot(9, 6), | |
FlSpot(10, 6), | |
FlSpot(11, 7), | |
], | |
isCurved: false, | |
barWidth: 2, | |
colors: [ | |
Colors.green, | |
], | |
dotData: FlDotData( | |
show: false, | |
), | |
), | |
LineChartBarData( | |
spots: const [ | |
FlSpot(0, 0), | |
FlSpot(1, 3), | |
FlSpot(2, 4.5), | |
FlSpot(3, 5), | |
FlSpot(4, 8), | |
FlSpot(5, 3), | |
FlSpot(6, 5), | |
FlSpot(7, 8), | |
FlSpot(8, 4), | |
FlSpot(9, 7), | |
FlSpot(10, 7), | |
FlSpot(11, 8), | |
], | |
isCurved: false, | |
barWidth: 2, | |
colors: [ | |
Colors.black, | |
], | |
dotData: FlDotData( | |
show: false, | |
), | |
), | |
LineChartBarData( | |
spots: const [ | |
FlSpot(0, 7), | |
FlSpot(1, 3), | |
FlSpot(2, 4), | |
FlSpot(3, 0), | |
FlSpot(4, 3.5), | |
FlSpot(5, 4), | |
FlSpot(6, 5), | |
FlSpot(7, 3), | |
FlSpot(8, 2), | |
FlSpot(9, 4), | |
FlSpot(10, 1), | |
FlSpot(11, 3), | |
], | |
isCurved: false, | |
barWidth: 2, | |
colors: [ | |
Colors.red, | |
], | |
dotData: FlDotData( | |
show: false, | |
), | |
), | |
], | |
betweenBarsData: [ | |
BetweenBarsData( | |
fromIndex: 0, | |
toIndex: 2, | |
colors: [Colors.red.withOpacity(0.3)], | |
) | |
], | |
minY: 0, | |
titlesData: FlTitlesData( | |
bottomTitles: SideTitles( | |
showTitles: true, | |
interval: 1, | |
getTextStyles: (context, value) => const TextStyle( | |
fontSize: 10, | |
color: Colors.purple, | |
fontWeight: FontWeight.bold), | |
getTitles: (value) { | |
switch (value.toInt()) { | |
case 0: | |
return 'Jan'; | |
case 1: | |
return 'Feb'; | |
case 2: | |
return 'Mar'; | |
case 3: | |
return 'Apr'; | |
case 4: | |
return 'May'; | |
case 5: | |
return 'Jun'; | |
case 6: | |
return 'Jul'; | |
case 7: | |
return 'Aug'; | |
case 8: | |
return 'Sep'; | |
case 9: | |
return 'Oct'; | |
case 10: | |
return 'Nov'; | |
case 11: | |
return 'Dec'; | |
default: | |
return ''; | |
} | |
}), | |
leftTitles: SideTitles( | |
showTitles: true, | |
getTitles: (value) { | |
return '\$ ${value + 0.5}'; | |
}, | |
interval: 1, | |
reservedSize: 40, | |
getTextStyles: (context, value) => | |
const TextStyle(fontSize: 10), | |
), | |
topTitles: SideTitles(showTitles: false), | |
rightTitles: SideTitles(showTitles: false), | |
), | |
gridData: FlGridData( | |
show: true, | |
drawVerticalLine: false, | |
horizontalInterval: 1, | |
checkToShowHorizontalLine: (double value) { | |
return value == 1 || value == 6 || value == 4 || value == 5; | |
}, | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment