1.0.0
This commit is contained in:
397
example/lib/presentation/samples/bar/bar_chart_sample1.dart
Normal file
397
example/lib/presentation/samples/bar/bar_chart_sample1.dart
Normal file
@@ -0,0 +1,397 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:fl_chart_app/util/extensions/color_extensions.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BarChartSample1 extends StatefulWidget {
|
||||
BarChartSample1({super.key});
|
||||
|
||||
List<Color> get availableColors => const <Color>[
|
||||
AppColors.contentColorPurple,
|
||||
AppColors.contentColorYellow,
|
||||
AppColors.contentColorBlue,
|
||||
AppColors.contentColorOrange,
|
||||
AppColors.contentColorPink,
|
||||
AppColors.contentColorRed,
|
||||
];
|
||||
|
||||
final Color barBackgroundColor =
|
||||
AppColors.contentColorWhite.darken().withValues(alpha: 0.3);
|
||||
final Color barColor = AppColors.contentColorWhite;
|
||||
final Color touchedBarColor = AppColors.contentColorGreen;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => BarChartSample1State();
|
||||
}
|
||||
|
||||
class BarChartSample1State extends State<BarChartSample1> {
|
||||
final Duration animDuration = const Duration(milliseconds: 250);
|
||||
|
||||
int touchedIndex = -1;
|
||||
|
||||
bool isPlaying = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
const Text(
|
||||
'Mingguan',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorGreen,
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 4,
|
||||
),
|
||||
Text(
|
||||
'Grafik konsumsi kalori',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorGreen.darken(),
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 38,
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
child: BarChart(
|
||||
isPlaying ? randomData() : mainBarData(),
|
||||
duration: animDuration,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 12,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: Align(
|
||||
alignment: Alignment.topRight,
|
||||
child: IconButton(
|
||||
icon: Icon(
|
||||
isPlaying ? Icons.pause : Icons.play_arrow,
|
||||
color: AppColors.contentColorGreen,
|
||||
),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
isPlaying = !isPlaying;
|
||||
if (isPlaying) {
|
||||
refreshState();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
BarChartGroupData makeGroupData(
|
||||
int x,
|
||||
double y, {
|
||||
bool isTouched = false,
|
||||
Color? barColor,
|
||||
double width = 22,
|
||||
List<int> showTooltips = const [],
|
||||
}) {
|
||||
barColor ??= widget.barColor;
|
||||
return BarChartGroupData(
|
||||
x: x,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: isTouched ? y + 1 : y,
|
||||
color: isTouched ? widget.touchedBarColor : barColor,
|
||||
width: width,
|
||||
borderSide: isTouched
|
||||
? BorderSide(color: widget.touchedBarColor.darken(80))
|
||||
: const BorderSide(color: Colors.white, width: 0),
|
||||
backDrawRodData: BackgroundBarChartRodData(
|
||||
show: true,
|
||||
toY: 20,
|
||||
color: widget.barBackgroundColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
showingTooltipIndicators: showTooltips,
|
||||
);
|
||||
}
|
||||
|
||||
List<BarChartGroupData> showingGroups() => List.generate(7, (i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
return makeGroupData(0, 5, isTouched: i == touchedIndex);
|
||||
case 1:
|
||||
return makeGroupData(1, 6.5, isTouched: i == touchedIndex);
|
||||
case 2:
|
||||
return makeGroupData(2, 5, isTouched: i == touchedIndex);
|
||||
case 3:
|
||||
return makeGroupData(3, 7.5, isTouched: i == touchedIndex);
|
||||
case 4:
|
||||
return makeGroupData(4, 9, isTouched: i == touchedIndex);
|
||||
case 5:
|
||||
return makeGroupData(5, 11.5, isTouched: i == touchedIndex);
|
||||
case 6:
|
||||
return makeGroupData(6, 6.5, isTouched: i == touchedIndex);
|
||||
default:
|
||||
return throw Error();
|
||||
}
|
||||
});
|
||||
|
||||
BarChartData mainBarData() {
|
||||
return BarChartData(
|
||||
barTouchData: BarTouchData(
|
||||
touchTooltipData: BarTouchTooltipData(
|
||||
getTooltipColor: (_) => Colors.blueGrey,
|
||||
tooltipHorizontalAlignment: FLHorizontalAlignment.right,
|
||||
tooltipMargin: -10,
|
||||
getTooltipItem: (group, groupIndex, rod, rodIndex) {
|
||||
String weekDay;
|
||||
switch (group.x) {
|
||||
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;
|
||||
default:
|
||||
throw Error();
|
||||
}
|
||||
return BarTooltipItem(
|
||||
'$weekDay\n',
|
||||
const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
),
|
||||
children: <TextSpan>[
|
||||
TextSpan(
|
||||
text: (rod.toY - 1).toString(),
|
||||
style: const TextStyle(
|
||||
color: Colors.white, //widget.touchedBarColor,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
touchCallback: (FlTouchEvent event, barTouchResponse) {
|
||||
setState(() {
|
||||
if (!event.isInterestedForInteractions ||
|
||||
barTouchResponse == null ||
|
||||
barTouchResponse.spot == null) {
|
||||
touchedIndex = -1;
|
||||
return;
|
||||
}
|
||||
touchedIndex = barTouchResponse.spot!.touchedBarGroupIndex;
|
||||
});
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: getTitles,
|
||||
reservedSize: 38,
|
||||
),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: false,
|
||||
),
|
||||
barGroups: showingGroups(),
|
||||
gridData: const FlGridData(show: false),
|
||||
);
|
||||
}
|
||||
|
||||
Widget getTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
);
|
||||
Widget text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = const Text('M', style: style);
|
||||
break;
|
||||
case 1:
|
||||
text = const Text('T', style: style);
|
||||
break;
|
||||
case 2:
|
||||
text = const Text('W', style: style);
|
||||
break;
|
||||
case 3:
|
||||
text = const Text('T', style: style);
|
||||
break;
|
||||
case 4:
|
||||
text = const Text('F', style: style);
|
||||
break;
|
||||
case 5:
|
||||
text = const Text('S', style: style);
|
||||
break;
|
||||
case 6:
|
||||
text = const Text('S', style: style);
|
||||
break;
|
||||
default:
|
||||
text = const Text('', style: style);
|
||||
break;
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 16,
|
||||
child: text,
|
||||
);
|
||||
}
|
||||
|
||||
BarChartData randomData() {
|
||||
return BarChartData(
|
||||
barTouchData: const BarTouchData(
|
||||
enabled: false,
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: getTitles,
|
||||
reservedSize: 38,
|
||||
),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: false,
|
||||
),
|
||||
barGroups: List.generate(7, (i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
return makeGroupData(
|
||||
0,
|
||||
Random().nextInt(15).toDouble() + 6,
|
||||
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)],
|
||||
);
|
||||
case 2:
|
||||
return makeGroupData(
|
||||
2,
|
||||
Random().nextInt(15).toDouble() + 6,
|
||||
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)],
|
||||
);
|
||||
case 4:
|
||||
return makeGroupData(
|
||||
4,
|
||||
Random().nextInt(15).toDouble() + 6,
|
||||
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)],
|
||||
);
|
||||
case 6:
|
||||
return makeGroupData(
|
||||
6,
|
||||
Random().nextInt(15).toDouble() + 6,
|
||||
barColor: widget.availableColors[
|
||||
Random().nextInt(widget.availableColors.length)],
|
||||
);
|
||||
default:
|
||||
return throw Error();
|
||||
}
|
||||
}),
|
||||
gridData: const FlGridData(show: false),
|
||||
);
|
||||
}
|
||||
|
||||
Future<dynamic> refreshState() async {
|
||||
setState(() {});
|
||||
await Future<dynamic>.delayed(
|
||||
animDuration + const Duration(milliseconds: 50),
|
||||
);
|
||||
if (isPlaying) {
|
||||
await refreshState();
|
||||
}
|
||||
}
|
||||
}
|
||||
283
example/lib/presentation/samples/bar/bar_chart_sample2.dart
Normal file
283
example/lib/presentation/samples/bar/bar_chart_sample2.dart
Normal file
@@ -0,0 +1,283 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:fl_chart_app/util/extensions/color_extensions.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BarChartSample2 extends StatefulWidget {
|
||||
BarChartSample2({super.key});
|
||||
final Color leftBarColor = AppColors.contentColorYellow;
|
||||
final Color rightBarColor = AppColors.contentColorRed;
|
||||
final Color avgColor =
|
||||
AppColors.contentColorOrange.avg(AppColors.contentColorRed);
|
||||
@override
|
||||
State<StatefulWidget> createState() => BarChartSample2State();
|
||||
}
|
||||
|
||||
class BarChartSample2State extends State<BarChartSample2> {
|
||||
final double width = 7;
|
||||
|
||||
late List<BarChartGroupData> rawBarGroups;
|
||||
late List<BarChartGroupData> showingBarGroups;
|
||||
|
||||
int touchedGroupIndex = -1;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final barGroup1 = makeGroupData(0, 5, 12);
|
||||
final barGroup2 = makeGroupData(1, 16, 12);
|
||||
final barGroup3 = makeGroupData(2, 18, 5);
|
||||
final barGroup4 = makeGroupData(3, 20, 16);
|
||||
final barGroup5 = makeGroupData(4, 17, 6);
|
||||
final barGroup6 = makeGroupData(5, 19, 1.5);
|
||||
final barGroup7 = makeGroupData(6, 10, 1.5);
|
||||
|
||||
final items = [
|
||||
barGroup1,
|
||||
barGroup2,
|
||||
barGroup3,
|
||||
barGroup4,
|
||||
barGroup5,
|
||||
barGroup6,
|
||||
barGroup7,
|
||||
];
|
||||
|
||||
rawBarGroups = items;
|
||||
|
||||
showingBarGroups = rawBarGroups;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
makeTransactionsIcon(),
|
||||
const SizedBox(
|
||||
width: 38,
|
||||
),
|
||||
const Text(
|
||||
'Transactions',
|
||||
style: TextStyle(color: Colors.white, fontSize: 22),
|
||||
),
|
||||
const SizedBox(
|
||||
width: 4,
|
||||
),
|
||||
const Text(
|
||||
'state',
|
||||
style: TextStyle(color: Color(0xff77839a), fontSize: 16),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 38,
|
||||
),
|
||||
Expanded(
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
maxY: 20,
|
||||
barTouchData: BarTouchData(
|
||||
touchTooltipData: BarTouchTooltipData(
|
||||
getTooltipColor: ((group) {
|
||||
return Colors.grey;
|
||||
}),
|
||||
getTooltipItem: (a, b, c, d) => null,
|
||||
),
|
||||
touchCallback: (FlTouchEvent event, response) {
|
||||
if (response == null || response.spot == null) {
|
||||
setState(() {
|
||||
touchedGroupIndex = -1;
|
||||
showingBarGroups = List.of(rawBarGroups);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
touchedGroupIndex = response.spot!.touchedBarGroupIndex;
|
||||
|
||||
setState(() {
|
||||
if (!event.isInterestedForInteractions) {
|
||||
touchedGroupIndex = -1;
|
||||
showingBarGroups = List.of(rawBarGroups);
|
||||
return;
|
||||
}
|
||||
showingBarGroups = List.of(rawBarGroups);
|
||||
if (touchedGroupIndex != -1) {
|
||||
var sum = 0.0;
|
||||
for (final rod
|
||||
in showingBarGroups[touchedGroupIndex].barRods) {
|
||||
sum += rod.toY;
|
||||
}
|
||||
final avg = sum /
|
||||
showingBarGroups[touchedGroupIndex]
|
||||
.barRods
|
||||
.length;
|
||||
|
||||
showingBarGroups[touchedGroupIndex] =
|
||||
showingBarGroups[touchedGroupIndex].copyWith(
|
||||
barRods: showingBarGroups[touchedGroupIndex]
|
||||
.barRods
|
||||
.map((rod) {
|
||||
return rod.copyWith(
|
||||
toY: avg, color: widget.avgColor);
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: bottomTitles,
|
||||
reservedSize: 42,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 28,
|
||||
interval: 1,
|
||||
getTitlesWidget: leftTitles,
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: false,
|
||||
),
|
||||
barGroups: showingBarGroups,
|
||||
gridData: const FlGridData(show: false),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 12,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
color: Color(0xff7589a2),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
);
|
||||
String text;
|
||||
if (value == 0) {
|
||||
text = '1K';
|
||||
} else if (value == 10) {
|
||||
text = '5K';
|
||||
} else if (value == 19) {
|
||||
text = '10K';
|
||||
} else {
|
||||
return Container();
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 0,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget bottomTitles(double value, TitleMeta meta) {
|
||||
final titles = <String>['Mn', 'Te', 'Wd', 'Tu', 'Fr', 'St', 'Su'];
|
||||
|
||||
final Widget text = Text(
|
||||
titles[value.toInt()],
|
||||
style: const TextStyle(
|
||||
color: Color(0xff7589a2),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
),
|
||||
);
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 16, //margin top
|
||||
child: text,
|
||||
);
|
||||
}
|
||||
|
||||
BarChartGroupData makeGroupData(int x, double y1, double y2) {
|
||||
return BarChartGroupData(
|
||||
barsSpace: 4,
|
||||
x: x,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: y1,
|
||||
color: widget.leftBarColor,
|
||||
width: width,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: y2,
|
||||
color: widget.rightBarColor,
|
||||
width: width,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget makeTransactionsIcon() {
|
||||
const width = 4.5;
|
||||
const space = 3.5;
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Container(
|
||||
width: width,
|
||||
height: 10,
|
||||
color: Colors.white.withValues(alpha: 0.4),
|
||||
),
|
||||
const SizedBox(
|
||||
width: space,
|
||||
),
|
||||
Container(
|
||||
width: width,
|
||||
height: 28,
|
||||
color: Colors.white.withValues(alpha: 0.8),
|
||||
),
|
||||
const SizedBox(
|
||||
width: space,
|
||||
),
|
||||
Container(
|
||||
width: width,
|
||||
height: 42,
|
||||
color: Colors.white.withValues(alpha: 1),
|
||||
),
|
||||
const SizedBox(
|
||||
width: space,
|
||||
),
|
||||
Container(
|
||||
width: width,
|
||||
height: 28,
|
||||
color: Colors.white.withValues(alpha: 0.8),
|
||||
),
|
||||
const SizedBox(
|
||||
width: space,
|
||||
),
|
||||
Container(
|
||||
width: width,
|
||||
height: 10,
|
||||
color: Colors.white.withValues(alpha: 0.4),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
209
example/lib/presentation/samples/bar/bar_chart_sample3.dart
Normal file
209
example/lib/presentation/samples/bar/bar_chart_sample3.dart
Normal file
@@ -0,0 +1,209 @@
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:fl_chart_app/util/extensions/color_extensions.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class _BarChart extends StatelessWidget {
|
||||
const _BarChart();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BarChart(
|
||||
BarChartData(
|
||||
barTouchData: barTouchData,
|
||||
titlesData: titlesData,
|
||||
borderData: borderData,
|
||||
barGroups: barGroups,
|
||||
gridData: const FlGridData(show: false),
|
||||
alignment: BarChartAlignment.spaceAround,
|
||||
maxY: 20,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
BarTouchData get barTouchData => BarTouchData(
|
||||
enabled: false,
|
||||
touchTooltipData: BarTouchTooltipData(
|
||||
getTooltipColor: (group) => Colors.transparent,
|
||||
tooltipPadding: EdgeInsets.zero,
|
||||
tooltipMargin: 8,
|
||||
getTooltipItem: (
|
||||
BarChartGroupData group,
|
||||
int groupIndex,
|
||||
BarChartRodData rod,
|
||||
int rodIndex,
|
||||
) {
|
||||
return BarTooltipItem(
|
||||
rod.toY.round().toString(),
|
||||
const TextStyle(
|
||||
color: AppColors.contentColorCyan,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
Widget getTitles(double value, TitleMeta meta) {
|
||||
final style = TextStyle(
|
||||
color: AppColors.contentColorBlue.darken(20),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = 'Mn';
|
||||
break;
|
||||
case 1:
|
||||
text = 'Te';
|
||||
break;
|
||||
case 2:
|
||||
text = 'Wd';
|
||||
break;
|
||||
case 3:
|
||||
text = 'Tu';
|
||||
break;
|
||||
case 4:
|
||||
text = 'Fr';
|
||||
break;
|
||||
case 5:
|
||||
text = 'St';
|
||||
break;
|
||||
case 6:
|
||||
text = 'Sn';
|
||||
break;
|
||||
default:
|
||||
text = '';
|
||||
break;
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 4,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
FlTitlesData get titlesData => FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 30,
|
||||
getTitlesWidget: getTitles,
|
||||
),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
);
|
||||
|
||||
FlBorderData get borderData => FlBorderData(
|
||||
show: false,
|
||||
);
|
||||
|
||||
LinearGradient get _barsGradient => LinearGradient(
|
||||
colors: [
|
||||
AppColors.contentColorBlue.darken(20),
|
||||
AppColors.contentColorCyan,
|
||||
],
|
||||
begin: Alignment.bottomCenter,
|
||||
end: Alignment.topCenter,
|
||||
);
|
||||
|
||||
List<BarChartGroupData> get barGroups => [
|
||||
BarChartGroupData(
|
||||
x: 0,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 8,
|
||||
gradient: _barsGradient,
|
||||
)
|
||||
],
|
||||
showingTooltipIndicators: [0],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 1,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 10,
|
||||
gradient: _barsGradient,
|
||||
)
|
||||
],
|
||||
showingTooltipIndicators: [0],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 2,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 14,
|
||||
gradient: _barsGradient,
|
||||
)
|
||||
],
|
||||
showingTooltipIndicators: [0],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 3,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 15,
|
||||
gradient: _barsGradient,
|
||||
)
|
||||
],
|
||||
showingTooltipIndicators: [0],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 4,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 13,
|
||||
gradient: _barsGradient,
|
||||
)
|
||||
],
|
||||
showingTooltipIndicators: [0],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 5,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 10,
|
||||
gradient: _barsGradient,
|
||||
)
|
||||
],
|
||||
showingTooltipIndicators: [0],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 6,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 16,
|
||||
gradient: _barsGradient,
|
||||
)
|
||||
],
|
||||
showingTooltipIndicators: [0],
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
class BarChartSample3 extends StatefulWidget {
|
||||
const BarChartSample3({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => BarChartSample3State();
|
||||
}
|
||||
|
||||
class BarChartSample3State extends State<BarChartSample3> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const AspectRatio(
|
||||
aspectRatio: 1.6,
|
||||
child: _BarChart(),
|
||||
);
|
||||
}
|
||||
}
|
||||
352
example/lib/presentation/samples/bar/bar_chart_sample4.dart
Normal file
352
example/lib/presentation/samples/bar/bar_chart_sample4.dart
Normal file
@@ -0,0 +1,352 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:fl_chart_app/util/extensions/color_extensions.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BarChartSample4 extends StatefulWidget {
|
||||
BarChartSample4({super.key});
|
||||
|
||||
final Color dark = AppColors.contentColorCyan.darken(60);
|
||||
final Color normal = AppColors.contentColorCyan.darken(30);
|
||||
final Color light = AppColors.contentColorCyan;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => BarChartSample4State();
|
||||
}
|
||||
|
||||
class BarChartSample4State extends State<BarChartSample4> {
|
||||
Widget bottomTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(fontSize: 10);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = 'Apr';
|
||||
break;
|
||||
case 1:
|
||||
text = 'May';
|
||||
break;
|
||||
case 2:
|
||||
text = 'Jun';
|
||||
break;
|
||||
case 3:
|
||||
text = 'Jul';
|
||||
break;
|
||||
case 4:
|
||||
text = 'Aug';
|
||||
break;
|
||||
default:
|
||||
text = '';
|
||||
break;
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitles(double value, TitleMeta meta) {
|
||||
if (value == meta.max) {
|
||||
return Container();
|
||||
}
|
||||
const style = TextStyle(
|
||||
fontSize: 10,
|
||||
);
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(
|
||||
meta.formattedValue,
|
||||
style: style,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 1.66,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final barsSpace = 4.0 * constraints.maxWidth / 400;
|
||||
final barsWidth = 8.0 * constraints.maxWidth / 400;
|
||||
return BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.center,
|
||||
barTouchData: const BarTouchData(
|
||||
enabled: false,
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 28,
|
||||
getTitlesWidget: bottomTitles,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 40,
|
||||
getTitlesWidget: leftTitles,
|
||||
),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
checkToShowHorizontalLine: (value) => value % 10 == 0,
|
||||
getDrawingHorizontalLine: (value) => FlLine(
|
||||
color: AppColors.borderColor.withValues(alpha: 0.1),
|
||||
strokeWidth: 1,
|
||||
),
|
||||
drawVerticalLine: false,
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: false,
|
||||
),
|
||||
groupsSpace: barsSpace,
|
||||
barGroups: getData(barsWidth, barsSpace),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<BarChartGroupData> getData(double barsWidth, double barsSpace) {
|
||||
return [
|
||||
BarChartGroupData(
|
||||
x: 0,
|
||||
barsSpace: barsSpace,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 17000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 2000000000, widget.dark),
|
||||
BarChartRodStackItem(2000000000, 12000000000, widget.normal),
|
||||
BarChartRodStackItem(12000000000, 17000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 24000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 13000000000, widget.dark),
|
||||
BarChartRodStackItem(13000000000, 14000000000, widget.normal),
|
||||
BarChartRodStackItem(14000000000, 24000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 23000000000.5,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 6000000000.5, widget.dark),
|
||||
BarChartRodStackItem(6000000000.5, 18000000000, widget.normal),
|
||||
BarChartRodStackItem(18000000000, 23000000000.5, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 29000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 9000000000, widget.dark),
|
||||
BarChartRodStackItem(9000000000, 15000000000, widget.normal),
|
||||
BarChartRodStackItem(15000000000, 29000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 32000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 2000000000.5, widget.dark),
|
||||
BarChartRodStackItem(2000000000.5, 17000000000.5, widget.normal),
|
||||
BarChartRodStackItem(17000000000.5, 32000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 1,
|
||||
barsSpace: barsSpace,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 31000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 11000000000, widget.dark),
|
||||
BarChartRodStackItem(11000000000, 18000000000, widget.normal),
|
||||
BarChartRodStackItem(18000000000, 31000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 35000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 14000000000, widget.dark),
|
||||
BarChartRodStackItem(14000000000, 27000000000, widget.normal),
|
||||
BarChartRodStackItem(27000000000, 35000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 31000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 8000000000, widget.dark),
|
||||
BarChartRodStackItem(8000000000, 24000000000, widget.normal),
|
||||
BarChartRodStackItem(24000000000, 31000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 15000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 6000000000.5, widget.dark),
|
||||
BarChartRodStackItem(6000000000.5, 12000000000.5, widget.normal),
|
||||
BarChartRodStackItem(12000000000.5, 15000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 17000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 9000000000, widget.dark),
|
||||
BarChartRodStackItem(9000000000, 15000000000, widget.normal),
|
||||
BarChartRodStackItem(15000000000, 17000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 2,
|
||||
barsSpace: barsSpace,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 34000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 6000000000, widget.dark),
|
||||
BarChartRodStackItem(6000000000, 23000000000, widget.normal),
|
||||
BarChartRodStackItem(23000000000, 34000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 32000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 7000000000, widget.dark),
|
||||
BarChartRodStackItem(7000000000, 24000000000, widget.normal),
|
||||
BarChartRodStackItem(24000000000, 32000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 14000000000.5,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 1000000000.5, widget.dark),
|
||||
BarChartRodStackItem(1000000000.5, 12000000000, widget.normal),
|
||||
BarChartRodStackItem(12000000000, 14000000000.5, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 20000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 4000000000, widget.dark),
|
||||
BarChartRodStackItem(4000000000, 15000000000, widget.normal),
|
||||
BarChartRodStackItem(15000000000, 20000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 24000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 4000000000, widget.dark),
|
||||
BarChartRodStackItem(4000000000, 15000000000, widget.normal),
|
||||
BarChartRodStackItem(15000000000, 24000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
],
|
||||
),
|
||||
BarChartGroupData(
|
||||
x: 3,
|
||||
barsSpace: barsSpace,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: 14000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 1000000000.5, widget.dark),
|
||||
BarChartRodStackItem(1000000000.5, 12000000000, widget.normal),
|
||||
BarChartRodStackItem(12000000000, 14000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 27000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 7000000000, widget.dark),
|
||||
BarChartRodStackItem(7000000000, 25000000000, widget.normal),
|
||||
BarChartRodStackItem(25000000000, 27000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 29000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 6000000000, widget.dark),
|
||||
BarChartRodStackItem(6000000000, 23000000000, widget.normal),
|
||||
BarChartRodStackItem(23000000000, 29000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 16000000000.5,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 9000000000, widget.dark),
|
||||
BarChartRodStackItem(9000000000, 15000000000, widget.normal),
|
||||
BarChartRodStackItem(15000000000, 16000000000.5, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: 15000000000,
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(0, 7000000000, widget.dark),
|
||||
BarChartRodStackItem(7000000000, 12000000000.5, widget.normal),
|
||||
BarChartRodStackItem(12000000000.5, 15000000000, widget.light),
|
||||
],
|
||||
borderRadius: BorderRadius.zero,
|
||||
width: barsWidth,
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
360
example/lib/presentation/samples/bar/bar_chart_sample5.dart
Normal file
360
example/lib/presentation/samples/bar/bar_chart_sample5.dart
Normal file
@@ -0,0 +1,360 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:fl_chart_app/util/app_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BarChartSample5 extends StatefulWidget {
|
||||
const BarChartSample5({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => BarChartSample5State();
|
||||
}
|
||||
|
||||
class BarChartSample5State extends State<BarChartSample5> {
|
||||
static const double barWidth = 22;
|
||||
static const shadowOpacity = 0.2;
|
||||
static const mainItems = <int, List<double>>{
|
||||
0: [2, 3, 2.5, 8],
|
||||
1: [-1.8, -2.7, -3, -6.5],
|
||||
2: [1.5, 2, 3.5, 6],
|
||||
3: [1.5, 1.5, 4, 6.5],
|
||||
4: [-2, -2, -5, -9],
|
||||
5: [-1.2, -1.5, -4.3, -10],
|
||||
6: [1.2, 4.8, 5, 5],
|
||||
};
|
||||
int touchedIndex = -1;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Widget bottomTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(color: Colors.white, fontSize: 10);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = 'Mon';
|
||||
break;
|
||||
case 1:
|
||||
text = 'Tue';
|
||||
break;
|
||||
case 2:
|
||||
text = 'Wed';
|
||||
break;
|
||||
case 3:
|
||||
text = 'Thu';
|
||||
break;
|
||||
case 4:
|
||||
text = 'Fri';
|
||||
break;
|
||||
case 5:
|
||||
text = 'Sat';
|
||||
break;
|
||||
case 6:
|
||||
text = 'Sun';
|
||||
break;
|
||||
default:
|
||||
text = '';
|
||||
break;
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget topTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(color: Colors.white, fontSize: 10);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = 'Mon';
|
||||
break;
|
||||
case 1:
|
||||
text = 'Tue';
|
||||
break;
|
||||
case 2:
|
||||
text = 'Wed';
|
||||
break;
|
||||
case 3:
|
||||
text = 'Thu';
|
||||
break;
|
||||
case 4:
|
||||
text = 'Fri';
|
||||
break;
|
||||
case 5:
|
||||
text = 'Sat';
|
||||
break;
|
||||
case 6:
|
||||
text = 'Sun';
|
||||
break;
|
||||
default:
|
||||
return Container();
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(color: Colors.white, fontSize: 10);
|
||||
String text;
|
||||
if (value == 0) {
|
||||
text = '0';
|
||||
} else {
|
||||
text = '${value.toInt()}0k';
|
||||
}
|
||||
return SideTitleWidget(
|
||||
angle: AppUtils().degreeToRadian(value < 0 ? -45 : 45),
|
||||
meta: meta,
|
||||
space: 4,
|
||||
child: Text(
|
||||
text,
|
||||
style: style,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget rightTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(color: Colors.white, fontSize: 10);
|
||||
String text;
|
||||
if (value == 0) {
|
||||
text = '0';
|
||||
} else {
|
||||
text = '${value.toInt()}0k';
|
||||
}
|
||||
return SideTitleWidget(
|
||||
angle: AppUtils().degreeToRadian(90),
|
||||
meta: meta,
|
||||
space: 0,
|
||||
child: Text(
|
||||
text,
|
||||
style: style,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
BarChartGroupData generateGroup(
|
||||
int x,
|
||||
double value1,
|
||||
double value2,
|
||||
double value3,
|
||||
double value4,
|
||||
) {
|
||||
final isTop = value1 > 0;
|
||||
final sum = value1 + value2 + value3 + value4;
|
||||
final isTouched = touchedIndex == x;
|
||||
return BarChartGroupData(
|
||||
x: x,
|
||||
groupVertically: true,
|
||||
showingTooltipIndicators: isTouched ? [0] : [],
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: sum,
|
||||
width: barWidth,
|
||||
borderRadius: isTop
|
||||
? const BorderRadius.only(
|
||||
topLeft: Radius.circular(6),
|
||||
topRight: Radius.circular(6),
|
||||
)
|
||||
: const BorderRadius.only(
|
||||
bottomLeft: Radius.circular(6),
|
||||
bottomRight: Radius.circular(6),
|
||||
),
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(
|
||||
0,
|
||||
value1,
|
||||
AppColors.contentColorGreen,
|
||||
BorderSide(
|
||||
color: Colors.white,
|
||||
width: isTouched ? 2 : 0,
|
||||
),
|
||||
),
|
||||
BarChartRodStackItem(
|
||||
value1,
|
||||
value1 + value2,
|
||||
AppColors.contentColorYellow,
|
||||
BorderSide(
|
||||
color: Colors.white,
|
||||
width: isTouched ? 2 : 0,
|
||||
),
|
||||
),
|
||||
BarChartRodStackItem(
|
||||
value1 + value2,
|
||||
value1 + value2 + value3,
|
||||
AppColors.contentColorPink,
|
||||
BorderSide(
|
||||
color: Colors.white,
|
||||
width: isTouched ? 2 : 0,
|
||||
),
|
||||
),
|
||||
BarChartRodStackItem(
|
||||
value1 + value2 + value3,
|
||||
value1 + value2 + value3 + value4,
|
||||
AppColors.contentColorBlue,
|
||||
BorderSide(
|
||||
color: Colors.white,
|
||||
width: isTouched ? 2 : 0,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: -sum,
|
||||
width: barWidth,
|
||||
color: Colors.transparent,
|
||||
borderRadius: isTop
|
||||
? const BorderRadius.only(
|
||||
bottomLeft: Radius.circular(6),
|
||||
bottomRight: Radius.circular(6),
|
||||
)
|
||||
: const BorderRadius.only(
|
||||
topLeft: Radius.circular(6),
|
||||
topRight: Radius.circular(6),
|
||||
),
|
||||
rodStackItems: [
|
||||
BarChartRodStackItem(
|
||||
0,
|
||||
-value1,
|
||||
AppColors.contentColorGreen.withValues(
|
||||
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
|
||||
const BorderSide(color: Colors.transparent),
|
||||
),
|
||||
BarChartRodStackItem(
|
||||
-value1,
|
||||
-(value1 + value2),
|
||||
AppColors.contentColorYellow.withValues(
|
||||
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
|
||||
const BorderSide(color: Colors.transparent),
|
||||
),
|
||||
BarChartRodStackItem(
|
||||
-(value1 + value2),
|
||||
-(value1 + value2 + value3),
|
||||
AppColors.contentColorPink.withValues(
|
||||
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
|
||||
const BorderSide(color: Colors.transparent),
|
||||
),
|
||||
BarChartRodStackItem(
|
||||
-(value1 + value2 + value3),
|
||||
-(value1 + value2 + value3 + value4),
|
||||
AppColors.contentColorBlue.withValues(
|
||||
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
|
||||
const BorderSide(color: Colors.transparent),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
bool isShadowBar(int rodIndex) => rodIndex == 1;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 0.8,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 16),
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.center,
|
||||
maxY: 20,
|
||||
minY: -20,
|
||||
groupsSpace: 12,
|
||||
barTouchData: BarTouchData(
|
||||
handleBuiltInTouches: false,
|
||||
touchCallback: (FlTouchEvent event, barTouchResponse) {
|
||||
if (!event.isInterestedForInteractions ||
|
||||
barTouchResponse == null ||
|
||||
barTouchResponse.spot == null) {
|
||||
setState(() {
|
||||
touchedIndex = -1;
|
||||
});
|
||||
return;
|
||||
}
|
||||
final rodIndex = barTouchResponse.spot!.touchedRodDataIndex;
|
||||
if (isShadowBar(rodIndex)) {
|
||||
setState(() {
|
||||
touchedIndex = -1;
|
||||
});
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
touchedIndex = barTouchResponse.spot!.touchedBarGroupIndex;
|
||||
});
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
topTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 32,
|
||||
getTitlesWidget: topTitles,
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 32,
|
||||
getTitlesWidget: bottomTitles,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: leftTitles,
|
||||
interval: 5,
|
||||
reservedSize: 42,
|
||||
),
|
||||
),
|
||||
rightTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: rightTitles,
|
||||
interval: 5,
|
||||
reservedSize: 42,
|
||||
),
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
checkToShowHorizontalLine: (value) => value % 5 == 0,
|
||||
getDrawingHorizontalLine: (value) {
|
||||
if (value == 0) {
|
||||
return FlLine(
|
||||
color: AppColors.borderColor.withValues(alpha: 0.1),
|
||||
strokeWidth: 3,
|
||||
);
|
||||
}
|
||||
return FlLine(
|
||||
color: AppColors.borderColor.withValues(alpha: 0.05),
|
||||
strokeWidth: 0.8,
|
||||
);
|
||||
},
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: false,
|
||||
),
|
||||
barGroups: mainItems.entries
|
||||
.map(
|
||||
(e) => generateGroup(
|
||||
e.key,
|
||||
e.value[0],
|
||||
e.value[1],
|
||||
e.value[2],
|
||||
e.value[3],
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
184
example/lib/presentation/samples/bar/bar_chart_sample6.dart
Normal file
184
example/lib/presentation/samples/bar/bar_chart_sample6.dart
Normal file
@@ -0,0 +1,184 @@
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:fl_chart_app/presentation/widgets/legend_widget.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BarChartSample6 extends StatelessWidget {
|
||||
const BarChartSample6({super.key});
|
||||
|
||||
final pilateColor = AppColors.contentColorPurple;
|
||||
final cyclingColor = AppColors.contentColorCyan;
|
||||
final quickWorkoutColor = AppColors.contentColorBlue;
|
||||
final betweenSpace = 0.2;
|
||||
|
||||
BarChartGroupData generateGroupData(
|
||||
int x,
|
||||
double pilates,
|
||||
double quickWorkout,
|
||||
double cycling,
|
||||
) {
|
||||
return BarChartGroupData(
|
||||
x: x,
|
||||
groupVertically: true,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
fromY: 0,
|
||||
toY: pilates,
|
||||
color: pilateColor,
|
||||
width: 5,
|
||||
),
|
||||
BarChartRodData(
|
||||
fromY: pilates + betweenSpace,
|
||||
toY: pilates + betweenSpace + quickWorkout,
|
||||
color: quickWorkoutColor,
|
||||
width: 5,
|
||||
),
|
||||
BarChartRodData(
|
||||
fromY: pilates + betweenSpace + quickWorkout + betweenSpace,
|
||||
toY: pilates + betweenSpace + quickWorkout + betweenSpace + cycling,
|
||||
color: cyclingColor,
|
||||
width: 5,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget bottomTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(fontSize: 10);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = 'JAN';
|
||||
break;
|
||||
case 1:
|
||||
text = 'FEB';
|
||||
break;
|
||||
case 2:
|
||||
text = 'MAR';
|
||||
break;
|
||||
case 3:
|
||||
text = 'APR';
|
||||
break;
|
||||
case 4:
|
||||
text = 'MAY';
|
||||
break;
|
||||
case 5:
|
||||
text = 'JUN';
|
||||
break;
|
||||
case 6:
|
||||
text = 'JUL';
|
||||
break;
|
||||
case 7:
|
||||
text = 'AUG';
|
||||
break;
|
||||
case 8:
|
||||
text = 'SEP';
|
||||
break;
|
||||
case 9:
|
||||
text = 'OCT';
|
||||
break;
|
||||
case 10:
|
||||
text = 'NOV';
|
||||
break;
|
||||
case 11:
|
||||
text = 'DEC';
|
||||
break;
|
||||
default:
|
||||
text = '';
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'Activity',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorBlue,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
LegendsListWidget(
|
||||
legends: [
|
||||
Legend('Pilates', pilateColor),
|
||||
Legend('Quick workouts', quickWorkoutColor),
|
||||
Legend('Cycling', cyclingColor),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
AspectRatio(
|
||||
aspectRatio: 2,
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.spaceBetween,
|
||||
titlesData: FlTitlesData(
|
||||
leftTitles: const AxisTitles(),
|
||||
rightTitles: const AxisTitles(),
|
||||
topTitles: const AxisTitles(),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: bottomTitles,
|
||||
reservedSize: 20,
|
||||
),
|
||||
),
|
||||
),
|
||||
barTouchData: const BarTouchData(enabled: false),
|
||||
borderData: FlBorderData(show: false),
|
||||
gridData: const FlGridData(show: false),
|
||||
barGroups: [
|
||||
generateGroupData(0, 2, 3, 2),
|
||||
generateGroupData(1, 2, 5, 1.7),
|
||||
generateGroupData(2, 1.3, 3.1, 2.8),
|
||||
generateGroupData(3, 3.1, 4, 3.1),
|
||||
generateGroupData(4, 0.8, 3.3, 3.4),
|
||||
generateGroupData(5, 2, 5.6, 1.8),
|
||||
generateGroupData(6, 1.3, 3.2, 2),
|
||||
generateGroupData(7, 2.3, 3.2, 3),
|
||||
generateGroupData(8, 2, 4.8, 2.5),
|
||||
generateGroupData(9, 1.2, 3.2, 2.5),
|
||||
generateGroupData(10, 1, 4.8, 3),
|
||||
generateGroupData(11, 2, 4.4, 2.8),
|
||||
],
|
||||
maxY: 11 + (betweenSpace * 3),
|
||||
extraLinesData: ExtraLinesData(
|
||||
horizontalLines: [
|
||||
HorizontalLine(
|
||||
y: 3.3,
|
||||
color: pilateColor,
|
||||
strokeWidth: 1,
|
||||
dashArray: [20, 4],
|
||||
),
|
||||
HorizontalLine(
|
||||
y: 8,
|
||||
color: quickWorkoutColor,
|
||||
strokeWidth: 1,
|
||||
dashArray: [20, 4],
|
||||
),
|
||||
HorizontalLine(
|
||||
y: 11,
|
||||
color: cyclingColor,
|
||||
strokeWidth: 1,
|
||||
dashArray: [20, 4],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
254
example/lib/presentation/samples/bar/bar_chart_sample7.dart
Normal file
254
example/lib/presentation/samples/bar/bar_chart_sample7.dart
Normal file
@@ -0,0 +1,254 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BarChartSample7 extends StatefulWidget {
|
||||
BarChartSample7({super.key});
|
||||
|
||||
final shadowColor = const Color(0xFFCCCCCC);
|
||||
final dataList = [
|
||||
const _BarData(AppColors.contentColorYellow, 18, 18),
|
||||
const _BarData(AppColors.contentColorGreen, 17, 8),
|
||||
const _BarData(AppColors.contentColorOrange, 10, 15),
|
||||
const _BarData(AppColors.contentColorPink, 2.5, 5),
|
||||
const _BarData(AppColors.contentColorBlue, 2, 2.5),
|
||||
const _BarData(AppColors.contentColorRed, 2, 2),
|
||||
];
|
||||
|
||||
@override
|
||||
State<BarChartSample7> createState() => _BarChartSample7State();
|
||||
}
|
||||
|
||||
class _BarChartSample7State extends State<BarChartSample7> {
|
||||
BarChartGroupData generateBarGroup(
|
||||
int x,
|
||||
Color color,
|
||||
double value,
|
||||
double shadowValue,
|
||||
) {
|
||||
return BarChartGroupData(
|
||||
x: x,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: value,
|
||||
color: color,
|
||||
width: 6,
|
||||
),
|
||||
BarChartRodData(
|
||||
toY: shadowValue,
|
||||
color: widget.shadowColor,
|
||||
width: 6,
|
||||
),
|
||||
],
|
||||
showingTooltipIndicators: touchedGroupIndex == x ? [0] : [],
|
||||
);
|
||||
}
|
||||
|
||||
int touchedGroupIndex = -1;
|
||||
|
||||
int rotationTurns = 1;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(child: Container()),
|
||||
const Text(
|
||||
'Horizontal Bar Chart',
|
||||
style: TextStyle(
|
||||
color: AppColors.mainTextColor1,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Tooltip(
|
||||
message: 'Rotate the chart 90 degrees (cw)',
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
rotationTurns += 1;
|
||||
});
|
||||
},
|
||||
icon: RotatedBox(
|
||||
quarterTurns: rotationTurns - 1,
|
||||
child: const Icon(
|
||||
Icons.rotate_90_degrees_cw,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
AspectRatio(
|
||||
aspectRatio: 1.4,
|
||||
child: BarChart(
|
||||
BarChartData(
|
||||
alignment: BarChartAlignment.spaceBetween,
|
||||
rotationQuarterTurns: rotationTurns,
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: Border.symmetric(
|
||||
horizontal: BorderSide(
|
||||
color: AppColors.borderColor.withValues(alpha: 0.2),
|
||||
),
|
||||
),
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
leftTitles: const AxisTitles(
|
||||
drawBelowEverything: true,
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 30,
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 36,
|
||||
getTitlesWidget: (value, meta) {
|
||||
final index = value.toInt();
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: _IconWidget(
|
||||
color: widget.dataList[index].color,
|
||||
isSelected: touchedGroupIndex == index,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
rightTitles: const AxisTitles(),
|
||||
topTitles: const AxisTitles(),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
getDrawingHorizontalLine: (value) => FlLine(
|
||||
color: AppColors.borderColor.withValues(alpha: 0.2),
|
||||
strokeWidth: 1,
|
||||
),
|
||||
),
|
||||
barGroups: widget.dataList.asMap().entries.map((e) {
|
||||
final index = e.key;
|
||||
final data = e.value;
|
||||
return generateBarGroup(
|
||||
index,
|
||||
data.color,
|
||||
data.value,
|
||||
data.shadowValue,
|
||||
);
|
||||
}).toList(),
|
||||
maxY: 20,
|
||||
barTouchData: BarTouchData(
|
||||
enabled: true,
|
||||
handleBuiltInTouches: false,
|
||||
touchTooltipData: BarTouchTooltipData(
|
||||
getTooltipColor: (group) => Colors.transparent,
|
||||
tooltipMargin: 0,
|
||||
getTooltipItem: (
|
||||
BarChartGroupData group,
|
||||
int groupIndex,
|
||||
BarChartRodData rod,
|
||||
int rodIndex,
|
||||
) {
|
||||
return BarTooltipItem(
|
||||
rod.toY.toString(),
|
||||
TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: rod.color,
|
||||
fontSize: 18,
|
||||
shadows: const [
|
||||
Shadow(
|
||||
color: Colors.black26,
|
||||
blurRadius: 12,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
touchCallback: (event, response) {
|
||||
if (event.isInterestedForInteractions &&
|
||||
response != null &&
|
||||
response.spot != null) {
|
||||
setState(() {
|
||||
touchedGroupIndex = response.spot!.touchedBarGroupIndex;
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
touchedGroupIndex = -1;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _BarData {
|
||||
const _BarData(this.color, this.value, this.shadowValue);
|
||||
|
||||
final Color color;
|
||||
final double value;
|
||||
final double shadowValue;
|
||||
}
|
||||
|
||||
class _IconWidget extends ImplicitlyAnimatedWidget {
|
||||
const _IconWidget({
|
||||
required this.color,
|
||||
required this.isSelected,
|
||||
}) : super(duration: const Duration(milliseconds: 300));
|
||||
final Color color;
|
||||
final bool isSelected;
|
||||
|
||||
@override
|
||||
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() =>
|
||||
_IconWidgetState();
|
||||
}
|
||||
|
||||
class _IconWidgetState extends AnimatedWidgetBaseState<_IconWidget> {
|
||||
Tween<double>? _rotationTween;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final rotation = math.pi * 4 * _rotationTween!.evaluate(animation);
|
||||
final scale = 1 + _rotationTween!.evaluate(animation) * 0.5;
|
||||
return Transform(
|
||||
transform: Matrix4.rotationZ(rotation).scaled(scale, scale),
|
||||
origin: const Offset(14, 14),
|
||||
child: Icon(
|
||||
widget.isSelected ? Icons.face_retouching_natural : Icons.face,
|
||||
color: widget.color,
|
||||
size: 28,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void forEachTween(TweenVisitor<dynamic> visitor) {
|
||||
_rotationTween = visitor(
|
||||
_rotationTween,
|
||||
widget.isSelected ? 1.0 : 0.0,
|
||||
(dynamic value) => Tween<double>(
|
||||
begin: value as double,
|
||||
end: widget.isSelected ? 1.0 : 0.0,
|
||||
),
|
||||
) as Tween<double>?;
|
||||
}
|
||||
}
|
||||
174
example/lib/presentation/samples/bar/bar_chart_sample8.dart
Normal file
174
example/lib/presentation/samples/bar/bar_chart_sample8.dart
Normal file
@@ -0,0 +1,174 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:fl_chart_app/util/extensions/color_extensions.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BarChartSample8 extends StatefulWidget {
|
||||
BarChartSample8({super.key});
|
||||
|
||||
final Color barBackgroundColor =
|
||||
AppColors.contentColorWhite.darken().withValues(alpha: 0.3);
|
||||
final Color barColor = AppColors.contentColorWhite;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => BarChartSample1State();
|
||||
}
|
||||
|
||||
class BarChartSample1State extends State<BarChartSample8> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.graphic_eq),
|
||||
const SizedBox(
|
||||
width: 32,
|
||||
),
|
||||
Text(
|
||||
'Sales forecasting chart',
|
||||
style: TextStyle(
|
||||
color: widget.barColor,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 32,
|
||||
),
|
||||
Expanded(
|
||||
child: BarChart(
|
||||
randomData(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
BarChartGroupData makeGroupData(
|
||||
int x,
|
||||
double y,
|
||||
FlErrorRange errorRange,
|
||||
) {
|
||||
return BarChartGroupData(
|
||||
x: x,
|
||||
barRods: [
|
||||
BarChartRodData(
|
||||
toY: y,
|
||||
toYErrorRange: errorRange,
|
||||
color: x >= 4 ? Colors.transparent : widget.barColor,
|
||||
borderRadius: BorderRadius.zero,
|
||||
borderDashArray: x >= 4 ? [4, 4] : null,
|
||||
width: 22,
|
||||
borderSide: BorderSide(color: widget.barColor, width: 2.0),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget getTitles(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
);
|
||||
List<String> days = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];
|
||||
|
||||
Widget text = Text(
|
||||
days[value.toInt()],
|
||||
style: style,
|
||||
);
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 16,
|
||||
child: text,
|
||||
);
|
||||
}
|
||||
|
||||
BarChartData randomData() {
|
||||
return BarChartData(
|
||||
maxY: 300.0,
|
||||
barTouchData: const BarTouchData(
|
||||
enabled: false,
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: getTitles,
|
||||
reservedSize: 38,
|
||||
),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
reservedSize: 30,
|
||||
showTitles: true,
|
||||
),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: false,
|
||||
),
|
||||
barGroups: List.generate(
|
||||
7,
|
||||
(i) {
|
||||
final y = Random().nextInt(290).toDouble() + 10;
|
||||
final lowerBy = y < 50
|
||||
? Random().nextDouble() * 10
|
||||
: Random().nextDouble() * 30 + 5;
|
||||
final upperBy = y > 290
|
||||
? Random().nextDouble() * 10
|
||||
: Random().nextDouble() * 30 + 5;
|
||||
return makeGroupData(
|
||||
i,
|
||||
y,
|
||||
FlErrorRange(
|
||||
lowerBy: lowerBy,
|
||||
upperBy: upperBy,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
gridData: const FlGridData(show: false),
|
||||
errorIndicatorData: FlErrorIndicatorData(
|
||||
painter: _errorPainter,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
FlSpotErrorRangePainter _errorPainter(
|
||||
BarChartSpotErrorRangeCallbackInput input,
|
||||
) =>
|
||||
FlSimpleErrorPainter(
|
||||
lineWidth: 2.0,
|
||||
capLength: 14,
|
||||
lineColor: input.groupIndex < 4
|
||||
? AppColors.contentColorOrange
|
||||
: AppColors.primary.withValues(alpha: 0.5),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user