1.0.0
This commit is contained in:
366
example/lib/presentation/samples/line/line_chart_sample1.dart
Normal file
366
example/lib/presentation/samples/line/line_chart_sample1.dart
Normal file
@@ -0,0 +1,366 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class _LineChart extends StatelessWidget {
|
||||
const _LineChart({required this.isShowingMainData});
|
||||
|
||||
final bool isShowingMainData;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LineChart(
|
||||
isShowingMainData ? sampleData1 : sampleData2,
|
||||
duration: const Duration(milliseconds: 250),
|
||||
);
|
||||
}
|
||||
|
||||
LineChartData get sampleData1 => LineChartData(
|
||||
lineTouchData: lineTouchData1,
|
||||
gridData: gridData,
|
||||
titlesData: titlesData1,
|
||||
borderData: borderData,
|
||||
lineBarsData: lineBarsData1,
|
||||
minX: 0,
|
||||
maxX: 14,
|
||||
maxY: 4,
|
||||
minY: 0,
|
||||
);
|
||||
|
||||
LineChartData get sampleData2 => LineChartData(
|
||||
lineTouchData: lineTouchData2,
|
||||
gridData: gridData,
|
||||
titlesData: titlesData2,
|
||||
borderData: borderData,
|
||||
lineBarsData: lineBarsData2,
|
||||
minX: 0,
|
||||
maxX: 14,
|
||||
maxY: 6,
|
||||
minY: 0,
|
||||
);
|
||||
|
||||
LineTouchData get lineTouchData1 => LineTouchData(
|
||||
handleBuiltInTouches: true,
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
getTooltipColor: (touchedSpot) =>
|
||||
Colors.blueGrey.withValues(alpha: 0.8),
|
||||
),
|
||||
);
|
||||
|
||||
FlTitlesData get titlesData1 => FlTitlesData(
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: bottomTitles,
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: leftTitles(),
|
||||
),
|
||||
);
|
||||
|
||||
List<LineChartBarData> get lineBarsData1 => [
|
||||
lineChartBarData1_1,
|
||||
lineChartBarData1_2,
|
||||
lineChartBarData1_3,
|
||||
];
|
||||
|
||||
LineTouchData get lineTouchData2 => const LineTouchData(
|
||||
enabled: false,
|
||||
);
|
||||
|
||||
FlTitlesData get titlesData2 => FlTitlesData(
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: bottomTitles,
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: leftTitles(),
|
||||
),
|
||||
);
|
||||
|
||||
List<LineChartBarData> get lineBarsData2 => [
|
||||
lineChartBarData2_1,
|
||||
lineChartBarData2_2,
|
||||
lineChartBarData2_3,
|
||||
];
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 1:
|
||||
text = '1m';
|
||||
break;
|
||||
case 2:
|
||||
text = '2m';
|
||||
break;
|
||||
case 3:
|
||||
text = '3m';
|
||||
break;
|
||||
case 4:
|
||||
text = '5m';
|
||||
break;
|
||||
case 5:
|
||||
text = '6m';
|
||||
break;
|
||||
default:
|
||||
return Container();
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(
|
||||
text,
|
||||
style: style,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
SideTitles leftTitles() => SideTitles(
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
showTitles: true,
|
||||
interval: 1,
|
||||
reservedSize: 40,
|
||||
);
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
);
|
||||
Widget text;
|
||||
switch (value.toInt()) {
|
||||
case 2:
|
||||
text = const Text('SEPT', style: style);
|
||||
break;
|
||||
case 7:
|
||||
text = const Text('OCT', style: style);
|
||||
break;
|
||||
case 12:
|
||||
text = const Text('DEC', style: style);
|
||||
break;
|
||||
default:
|
||||
text = const Text('');
|
||||
break;
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 10,
|
||||
child: text,
|
||||
);
|
||||
}
|
||||
|
||||
SideTitles get bottomTitles => SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 32,
|
||||
interval: 1,
|
||||
getTitlesWidget: bottomTitleWidgets,
|
||||
);
|
||||
|
||||
FlGridData get gridData => const FlGridData(show: false);
|
||||
|
||||
FlBorderData get borderData => FlBorderData(
|
||||
show: true,
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: AppColors.primary.withValues(alpha: 0.2), width: 4),
|
||||
left: const BorderSide(color: Colors.transparent),
|
||||
right: const BorderSide(color: Colors.transparent),
|
||||
top: const BorderSide(color: Colors.transparent),
|
||||
),
|
||||
);
|
||||
|
||||
LineChartBarData get lineChartBarData1_1 => LineChartBarData(
|
||||
isCurved: true,
|
||||
color: AppColors.contentColorGreen,
|
||||
barWidth: 8,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(show: false),
|
||||
spots: const [
|
||||
FlSpot(1, 1),
|
||||
FlSpot(3, 1.5),
|
||||
FlSpot(5, 1.4),
|
||||
FlSpot(7, 3.4),
|
||||
FlSpot(10, 2),
|
||||
FlSpot(12, 2.2),
|
||||
FlSpot(13, 1.8),
|
||||
],
|
||||
);
|
||||
|
||||
LineChartBarData get lineChartBarData1_2 => LineChartBarData(
|
||||
isCurved: true,
|
||||
color: AppColors.contentColorPink,
|
||||
barWidth: 8,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(
|
||||
show: false,
|
||||
color: AppColors.contentColorPink.withValues(alpha: 0),
|
||||
),
|
||||
spots: const [
|
||||
FlSpot(1, 1),
|
||||
FlSpot(3, 2.8),
|
||||
FlSpot(7, 1.2),
|
||||
FlSpot(10, 2.8),
|
||||
FlSpot(12, 2.6),
|
||||
FlSpot(13, 3.9),
|
||||
],
|
||||
);
|
||||
|
||||
LineChartBarData get lineChartBarData1_3 => LineChartBarData(
|
||||
isCurved: true,
|
||||
color: AppColors.contentColorCyan,
|
||||
barWidth: 8,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(show: false),
|
||||
spots: const [
|
||||
FlSpot(1, 2.8),
|
||||
FlSpot(3, 1.9),
|
||||
FlSpot(6, 3),
|
||||
FlSpot(10, 1.3),
|
||||
FlSpot(13, 2.5),
|
||||
],
|
||||
);
|
||||
|
||||
LineChartBarData get lineChartBarData2_1 => LineChartBarData(
|
||||
isCurved: true,
|
||||
curveSmoothness: 0,
|
||||
color: AppColors.contentColorGreen.withValues(alpha: 0.5),
|
||||
barWidth: 4,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(show: false),
|
||||
spots: const [
|
||||
FlSpot(1, 1),
|
||||
FlSpot(3, 4),
|
||||
FlSpot(5, 1.8),
|
||||
FlSpot(7, 5),
|
||||
FlSpot(10, 2),
|
||||
FlSpot(12, 2.2),
|
||||
FlSpot(13, 1.8),
|
||||
],
|
||||
);
|
||||
|
||||
LineChartBarData get lineChartBarData2_2 => LineChartBarData(
|
||||
isCurved: true,
|
||||
color: AppColors.contentColorPink.withValues(alpha: 0.5),
|
||||
barWidth: 4,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
color: AppColors.contentColorPink.withValues(alpha: 0.2),
|
||||
),
|
||||
spots: const [
|
||||
FlSpot(1, 1),
|
||||
FlSpot(3, 2.8),
|
||||
FlSpot(7, 1.2),
|
||||
FlSpot(10, 2.8),
|
||||
FlSpot(12, 2.6),
|
||||
FlSpot(13, 3.9),
|
||||
],
|
||||
);
|
||||
|
||||
LineChartBarData get lineChartBarData2_3 => LineChartBarData(
|
||||
isCurved: true,
|
||||
curveSmoothness: 0,
|
||||
color: AppColors.contentColorCyan.withValues(alpha: 0.5),
|
||||
barWidth: 2,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(show: true),
|
||||
belowBarData: BarAreaData(show: false),
|
||||
spots: const [
|
||||
FlSpot(1, 3.8),
|
||||
FlSpot(3, 1.9),
|
||||
FlSpot(6, 5),
|
||||
FlSpot(10, 3.3),
|
||||
FlSpot(13, 4.5),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
class LineChartSample1 extends StatefulWidget {
|
||||
const LineChartSample1({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => LineChartSample1State();
|
||||
}
|
||||
|
||||
class LineChartSample1State extends State<LineChartSample1> {
|
||||
late bool isShowingMainData;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
isShowingMainData = true;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 1.23,
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
const SizedBox(
|
||||
height: 37,
|
||||
),
|
||||
const Text(
|
||||
'Monthly Sales',
|
||||
style: TextStyle(
|
||||
color: AppColors.primary,
|
||||
fontSize: 32,
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 2,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(
|
||||
height: 37,
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(right: 16, left: 6),
|
||||
child: _LineChart(isShowingMainData: isShowingMainData),
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
],
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.refresh,
|
||||
color:
|
||||
Colors.white.withValues(alpha: isShowingMainData ? 1.0 : 0.5),
|
||||
),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
isShowingMainData = !isShowingMainData;
|
||||
});
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
146
example/lib/presentation/samples/line/line_chart_sample10.dart
Normal file
146
example/lib/presentation/samples/line/line_chart_sample10.dart
Normal file
@@ -0,0 +1,146 @@
|
||||
import 'dart:async';
|
||||
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 LineChartSample10 extends StatefulWidget {
|
||||
const LineChartSample10({super.key});
|
||||
|
||||
final Color sinColor = AppColors.contentColorBlue;
|
||||
final Color cosColor = AppColors.contentColorPink;
|
||||
|
||||
@override
|
||||
State<LineChartSample10> createState() => _LineChartSample10State();
|
||||
}
|
||||
|
||||
class _LineChartSample10State extends State<LineChartSample10> {
|
||||
final limitCount = 100;
|
||||
final sinPoints = <FlSpot>[];
|
||||
final cosPoints = <FlSpot>[];
|
||||
|
||||
double xValue = 0;
|
||||
double step = 0.05;
|
||||
|
||||
late Timer timer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
timer = Timer.periodic(const Duration(milliseconds: 40), (timer) {
|
||||
while (sinPoints.length > limitCount) {
|
||||
sinPoints.removeAt(0);
|
||||
cosPoints.removeAt(0);
|
||||
}
|
||||
setState(() {
|
||||
sinPoints.add(FlSpot(xValue, math.sin(xValue)));
|
||||
cosPoints.add(FlSpot(xValue, math.cos(xValue)));
|
||||
});
|
||||
xValue += step;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return cosPoints.isNotEmpty
|
||||
? Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'x: ${xValue.toStringAsFixed(1)}',
|
||||
style: const TextStyle(
|
||||
color: AppColors.mainTextColor2,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'sin: ${sinPoints.last.y.toStringAsFixed(1)}',
|
||||
style: TextStyle(
|
||||
color: widget.sinColor,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'cos: ${cosPoints.last.y.toStringAsFixed(1)}',
|
||||
style: TextStyle(
|
||||
color: widget.cosColor,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 12,
|
||||
),
|
||||
AspectRatio(
|
||||
aspectRatio: 1.5,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 24.0),
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
minY: -1,
|
||||
maxY: 1,
|
||||
minX: sinPoints.first.x,
|
||||
maxX: sinPoints.last.x,
|
||||
lineTouchData: const LineTouchData(enabled: false),
|
||||
clipData: const FlClipData.all(),
|
||||
gridData: const FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
lineBarsData: [
|
||||
sinLine(sinPoints),
|
||||
cosLine(cosPoints),
|
||||
],
|
||||
titlesData: const FlTitlesData(
|
||||
show: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
)
|
||||
: Container();
|
||||
}
|
||||
|
||||
LineChartBarData sinLine(List<FlSpot> points) {
|
||||
return LineChartBarData(
|
||||
spots: points,
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
gradient: LinearGradient(
|
||||
colors: [widget.sinColor.withValues(alpha: 0), widget.sinColor],
|
||||
stops: const [0.1, 1.0],
|
||||
),
|
||||
barWidth: 4,
|
||||
isCurved: false,
|
||||
);
|
||||
}
|
||||
|
||||
LineChartBarData cosLine(List<FlSpot> points) {
|
||||
return LineChartBarData(
|
||||
spots: points,
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
gradient: LinearGradient(
|
||||
colors: [widget.cosColor.withValues(alpha: 0), widget.cosColor],
|
||||
stops: const [0.1, 1.0],
|
||||
),
|
||||
barWidth: 4,
|
||||
isCurved: false,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
timer.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
202
example/lib/presentation/samples/line/line_chart_sample11.dart
Normal file
202
example/lib/presentation/samples/line/line_chart_sample11.dart
Normal file
@@ -0,0 +1,202 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LineChartSample11 extends StatefulWidget {
|
||||
const LineChartSample11({super.key});
|
||||
|
||||
@override
|
||||
State<LineChartSample11> createState() => _LineChartSample11State();
|
||||
}
|
||||
|
||||
class _LineChartSample11State extends State<LineChartSample11> {
|
||||
var baselineX = 0.0;
|
||||
var baselineY = 0.0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 1.5,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 18.0,
|
||||
right: 18.0,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Row(
|
||||
children: [
|
||||
RotatedBox(
|
||||
quarterTurns: 1,
|
||||
child: Slider(
|
||||
value: baselineY,
|
||||
onChanged: (newValue) {
|
||||
setState(() {
|
||||
baselineY = newValue;
|
||||
});
|
||||
},
|
||||
min: -10,
|
||||
max: 10,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _Chart(
|
||||
baselineX,
|
||||
(20 - (baselineY + 10)) - 10,
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
Slider(
|
||||
value: baselineX,
|
||||
onChanged: (newValue) {
|
||||
setState(() {
|
||||
baselineX = newValue;
|
||||
});
|
||||
},
|
||||
min: -10,
|
||||
max: 10,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _Chart extends StatelessWidget {
|
||||
final double baselineX;
|
||||
final double baselineY;
|
||||
|
||||
const _Chart(this.baselineX, this.baselineY) : super();
|
||||
|
||||
Widget getHorizontalTitles(value, TitleMeta meta) {
|
||||
TextStyle style;
|
||||
if ((value - baselineX).abs() <= 0.1) {
|
||||
style = const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
} else {
|
||||
style = const TextStyle(
|
||||
color: Colors.white60,
|
||||
fontSize: 14,
|
||||
);
|
||||
}
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Text(meta.formattedValue, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget getVerticalTitles(value, TitleMeta meta) {
|
||||
TextStyle style;
|
||||
if ((value - baselineY).abs() <= 0.1) {
|
||||
style = const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
} else {
|
||||
style = const TextStyle(
|
||||
color: Colors.white60,
|
||||
fontSize: 14,
|
||||
);
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Text(meta.formattedValue, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
FlLine getHorizontalVerticalLine(double value) {
|
||||
if ((value - baselineY).abs() <= 0.1) {
|
||||
return const FlLine(
|
||||
color: Colors.white70,
|
||||
strokeWidth: 1,
|
||||
dashArray: [8, 4],
|
||||
);
|
||||
} else {
|
||||
return const FlLine(
|
||||
color: Colors.blueGrey,
|
||||
strokeWidth: 0.4,
|
||||
dashArray: [8, 4],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
FlLine getVerticalVerticalLine(double value) {
|
||||
if ((value - baselineX).abs() <= 0.1) {
|
||||
return const FlLine(
|
||||
color: Colors.white70,
|
||||
strokeWidth: 1,
|
||||
dashArray: [8, 4],
|
||||
);
|
||||
} else {
|
||||
return const FlLine(
|
||||
color: Colors.blueGrey,
|
||||
strokeWidth: 0.4,
|
||||
dashArray: [8, 4],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LineChart(
|
||||
LineChartData(
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: [],
|
||||
),
|
||||
],
|
||||
titlesData: FlTitlesData(
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: getVerticalTitles,
|
||||
reservedSize: 36,
|
||||
),
|
||||
),
|
||||
topTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: getHorizontalTitles,
|
||||
reservedSize: 32),
|
||||
),
|
||||
rightTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: getVerticalTitles,
|
||||
reservedSize: 36,
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: getHorizontalTitles,
|
||||
reservedSize: 32),
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawHorizontalLine: true,
|
||||
drawVerticalLine: true,
|
||||
getDrawingHorizontalLine: getHorizontalVerticalLine,
|
||||
getDrawingVerticalLine: getVerticalVerticalLine,
|
||||
),
|
||||
minY: -10,
|
||||
maxY: 10,
|
||||
baselineY: baselineY,
|
||||
minX: -10,
|
||||
maxX: 10,
|
||||
baselineX: baselineX,
|
||||
),
|
||||
duration: Duration.zero,
|
||||
);
|
||||
}
|
||||
}
|
||||
416
example/lib/presentation/samples/line/line_chart_sample12.dart
Normal file
416
example/lib/presentation/samples/line/line_chart_sample12.dart
Normal file
@@ -0,0 +1,416 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/presentation_utils.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_colors.dart';
|
||||
import 'package:fl_chart_app/util/extensions/color_extensions.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class LineChartSample12 extends StatefulWidget {
|
||||
const LineChartSample12({super.key});
|
||||
|
||||
@override
|
||||
State<LineChartSample12> createState() => _LineChartSample12State();
|
||||
}
|
||||
|
||||
class _LineChartSample12State extends State<LineChartSample12> {
|
||||
List<(DateTime, double)>? _bitcoinPriceHistory;
|
||||
late TransformationController _transformationController;
|
||||
bool _isPanEnabled = true;
|
||||
bool _isScaleEnabled = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_reloadData();
|
||||
_transformationController = TransformationController();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _reloadData() async {
|
||||
final dataStr = await rootBundle.loadString(
|
||||
'assets/data/btc_last_year_price.json',
|
||||
);
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
final json = jsonDecode(dataStr) as Map<String, dynamic>;
|
||||
setState(() {
|
||||
_bitcoinPriceHistory = (json['prices'] as List).map((item) {
|
||||
final timestamp = item[0] as int;
|
||||
final price = item[1] as double;
|
||||
return (DateTime.fromMillisecondsSinceEpoch(timestamp), price);
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const leftReservedSize = 52.0;
|
||||
return Column(
|
||||
spacing: 16,
|
||||
children: [
|
||||
LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final width = constraints.maxWidth;
|
||||
return width >= 380
|
||||
? Row(
|
||||
children: [
|
||||
const SizedBox(width: leftReservedSize),
|
||||
const _ChartTitle(),
|
||||
const Spacer(),
|
||||
Center(
|
||||
child: _TransformationButtons(
|
||||
controller: _transformationController,
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Column(
|
||||
children: [
|
||||
const _ChartTitle(),
|
||||
const SizedBox(height: 16),
|
||||
_TransformationButtons(
|
||||
controller: _transformationController,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
spacing: 16,
|
||||
children: [
|
||||
const Text('Pan'),
|
||||
Switch(
|
||||
value: _isPanEnabled,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isPanEnabled = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
const Text('Scale'),
|
||||
Switch(
|
||||
value: _isScaleEnabled,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isScaleEnabled = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
AspectRatio(
|
||||
aspectRatio: 1.4,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 0.0,
|
||||
right: 18.0,
|
||||
),
|
||||
child: LineChart(
|
||||
transformationConfig: FlTransformationConfig(
|
||||
scaleAxis: FlScaleAxis.horizontal,
|
||||
minScale: 1.0,
|
||||
maxScale: 25.0,
|
||||
panEnabled: _isPanEnabled,
|
||||
scaleEnabled: _isScaleEnabled,
|
||||
transformationController: _transformationController,
|
||||
),
|
||||
LineChartData(
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: _bitcoinPriceHistory?.asMap().entries.map((e) {
|
||||
final index = e.key;
|
||||
final item = e.value;
|
||||
final value = item.$2;
|
||||
return FlSpot(index.toDouble(), value);
|
||||
}).toList() ??
|
||||
[],
|
||||
dotData: const FlDotData(show: false),
|
||||
color: AppColors.contentColorYellow,
|
||||
barWidth: 1,
|
||||
shadow: const Shadow(
|
||||
color: AppColors.contentColorYellow,
|
||||
blurRadius: 2,
|
||||
),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
AppColors.contentColorYellow.withValues(alpha: 0.2),
|
||||
AppColors.contentColorYellow.withValues(alpha: 0.0),
|
||||
],
|
||||
stops: const [0.5, 1.0],
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
lineTouchData: LineTouchData(
|
||||
touchSpotThreshold: 5,
|
||||
getTouchLineStart: (_, __) => -double.infinity,
|
||||
getTouchLineEnd: (_, __) => double.infinity,
|
||||
getTouchedSpotIndicator:
|
||||
(LineChartBarData barData, List<int> spotIndexes) {
|
||||
return spotIndexes.map((spotIndex) {
|
||||
return TouchedSpotIndicatorData(
|
||||
const FlLine(
|
||||
color: AppColors.contentColorRed,
|
||||
strokeWidth: 1.5,
|
||||
dashArray: [8, 2],
|
||||
),
|
||||
FlDotData(
|
||||
show: true,
|
||||
getDotPainter: (spot, percent, barData, index) {
|
||||
return FlDotCirclePainter(
|
||||
radius: 6,
|
||||
color: AppColors.contentColorYellow,
|
||||
strokeWidth: 0,
|
||||
strokeColor: AppColors.contentColorYellow,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
getTooltipItems: (List<LineBarSpot> touchedBarSpots) {
|
||||
return touchedBarSpots.map((barSpot) {
|
||||
final price = barSpot.y;
|
||||
final date =
|
||||
_bitcoinPriceHistory![barSpot.x.toInt()].$1;
|
||||
return LineTooltipItem(
|
||||
'',
|
||||
const TextStyle(
|
||||
color: AppColors.contentColorBlack,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
children: [
|
||||
TextSpan(
|
||||
text: '${date.year}/${date.month}/${date.day}',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorGreen.darken(20),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text: '\n${AppUtils.getFormattedCurrency(
|
||||
context,
|
||||
price,
|
||||
noDecimals: true,
|
||||
)}',
|
||||
style: const TextStyle(
|
||||
color: AppColors.contentColorYellow,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
getTooltipColor: (LineBarSpot barSpot) =>
|
||||
AppColors.contentColorBlack,
|
||||
),
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
leftTitles: const AxisTitles(
|
||||
drawBelowEverything: true,
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: leftReservedSize,
|
||||
maxIncluded: false,
|
||||
minIncluded: false,
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 38,
|
||||
maxIncluded: false,
|
||||
getTitlesWidget: (double value, TitleMeta meta) {
|
||||
final date = _bitcoinPriceHistory![value.toInt()].$1;
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Transform.rotate(
|
||||
angle: -45 * 3.14 / 180,
|
||||
child: Text(
|
||||
'${date.month}/${date.day}',
|
||||
style: const TextStyle(
|
||||
color: AppColors.contentColorGreen,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
duration: Duration.zero,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_transformationController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class _ChartTitle extends StatelessWidget {
|
||||
const _ChartTitle();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(height: 14),
|
||||
Text(
|
||||
'Bitcoin Price History',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorYellow,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'2023/12/19 - 2024/12/17',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorGreen,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 14),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TransformationButtons extends StatelessWidget {
|
||||
const _TransformationButtons({
|
||||
required this.controller,
|
||||
});
|
||||
|
||||
final TransformationController controller;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
Tooltip(
|
||||
message: 'Zoom in',
|
||||
child: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.add,
|
||||
size: 16,
|
||||
),
|
||||
onPressed: _transformationZoomIn,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Tooltip(
|
||||
message: 'Move left',
|
||||
child: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.arrow_back_ios,
|
||||
size: 16,
|
||||
),
|
||||
onPressed: _transformationMoveLeft,
|
||||
),
|
||||
),
|
||||
Tooltip(
|
||||
message: 'Reset zoom',
|
||||
child: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.refresh,
|
||||
size: 16,
|
||||
),
|
||||
onPressed: _transformationReset,
|
||||
),
|
||||
),
|
||||
Tooltip(
|
||||
message: 'Move right',
|
||||
child: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
size: 16,
|
||||
),
|
||||
onPressed: _transformationMoveRight,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Tooltip(
|
||||
message: 'Zoom out',
|
||||
child: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.minimize,
|
||||
size: 16,
|
||||
),
|
||||
onPressed: _transformationZoomOut,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _transformationReset() {
|
||||
controller.value = Matrix4.identity();
|
||||
}
|
||||
|
||||
void _transformationZoomIn() {
|
||||
controller.value *= Matrix4.diagonal3Values(
|
||||
1.1,
|
||||
1.1,
|
||||
1,
|
||||
);
|
||||
}
|
||||
|
||||
void _transformationMoveLeft() {
|
||||
controller.value *= Matrix4.translationValues(
|
||||
20,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
void _transformationMoveRight() {
|
||||
controller.value *= Matrix4.translationValues(
|
||||
-20,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
void _transformationZoomOut() {
|
||||
controller.value *= Matrix4.diagonal3Values(
|
||||
0.9,
|
||||
0.9,
|
||||
1,
|
||||
);
|
||||
}
|
||||
}
|
||||
494
example/lib/presentation/samples/line/line_chart_sample13.dart
Normal file
494
example/lib/presentation/samples/line/line_chart_sample13.dart
Normal file
@@ -0,0 +1,494 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
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:fl_chart_app/util/csv_parser.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class LineChartSample13 extends StatefulWidget {
|
||||
const LineChartSample13({super.key});
|
||||
|
||||
@override
|
||||
State<LineChartSample13> createState() => _LineChartSample13State();
|
||||
}
|
||||
|
||||
class _LineChartSample13State extends State<LineChartSample13> {
|
||||
List<List<_WeatherData>>? monthlyWeatherData;
|
||||
int _currentMonthIndex = 0;
|
||||
late final List<String> monthsNames;
|
||||
|
||||
final int minDays = 1;
|
||||
final int maxDays = 31;
|
||||
late final double overallMinTemp;
|
||||
late final double overallMaxTemp;
|
||||
|
||||
int _interactedSpotIndex = -1;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
monthsNames = [
|
||||
'January',
|
||||
'February',
|
||||
'March',
|
||||
'April',
|
||||
'May',
|
||||
'June',
|
||||
'July',
|
||||
'August',
|
||||
'September',
|
||||
'October',
|
||||
'November',
|
||||
'December',
|
||||
];
|
||||
_loadWeatherData();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _loadWeatherData() async {
|
||||
final data =
|
||||
await rootBundle.loadString('assets/data/amsterdam_2024_weather.csv');
|
||||
final rows = CsvParser.parse(data);
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
final allWeatherData =
|
||||
rows.skip(1).map((row) => _WeatherData.fromCsv(row)).toList();
|
||||
monthlyWeatherData = List.generate(12, (index) {
|
||||
final month = index + 1;
|
||||
return allWeatherData
|
||||
.where((element) => element.datetime.month == month)
|
||||
.toList();
|
||||
});
|
||||
overallMinTemp = allWeatherData
|
||||
.map((e) => e.temp)
|
||||
.reduce((value, element) => value < element ? value : element);
|
||||
overallMaxTemp = allWeatherData
|
||||
.map((e) => e.temp)
|
||||
.reduce((value, element) => value > element ? value : element);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
const SizedBox(height: 18),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Text(
|
||||
'Amsterdam Weather 2024',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorOrange,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Tooltip(
|
||||
message: 'Source: visualcrossing.com',
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
AppUtils().tryToLaunchUrl(
|
||||
'https://www.visualcrossing.com/weather-history/Amsterdam,Netherlands/metric/2024-01-01/2024-12-31',
|
||||
);
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.info_outline_rounded,
|
||||
color: AppColors.contentColorOrange,
|
||||
size: 18,
|
||||
)),
|
||||
)
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: IconButton(
|
||||
onPressed: _canGoPrevious ? _previousMonth : null,
|
||||
icon: const Icon(Icons.navigate_before_rounded),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: 92,
|
||||
child: Text(
|
||||
monthsNames[_currentMonthIndex],
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
color: AppColors.contentColorBlue,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: IconButton(
|
||||
onPressed: _canGoNext ? _nextMonth : null,
|
||||
icon: const Icon(Icons.navigate_next_rounded),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
AspectRatio(
|
||||
aspectRatio: 1.4,
|
||||
child: Stack(
|
||||
children: [
|
||||
if (monthlyWeatherData != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 0.0,
|
||||
right: 18.0,
|
||||
),
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
minY: overallMinTemp - 5,
|
||||
maxY: overallMaxTemp + 5,
|
||||
minX: 0,
|
||||
maxX: 31,
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: monthlyWeatherData![_currentMonthIndex]
|
||||
.asMap()
|
||||
.entries
|
||||
.map((e) {
|
||||
final index = e.key;
|
||||
final item = e.value;
|
||||
final value = item.temp;
|
||||
return FlSpot(
|
||||
index.toDouble(),
|
||||
value,
|
||||
yError: FlErrorRange(
|
||||
lowerBy: (item.tempmin - value).abs(),
|
||||
upperBy: item.tempmax - value,
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
isCurved: false,
|
||||
dotData: const FlDotData(show: false),
|
||||
color: AppColors.contentColorBlue,
|
||||
barWidth: 1,
|
||||
errorIndicatorData: FlErrorIndicatorData(
|
||||
show: true,
|
||||
painter: _errorPainter,
|
||||
),
|
||||
),
|
||||
],
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawHorizontalLine: true,
|
||||
drawVerticalLine: false,
|
||||
horizontalInterval: 5,
|
||||
getDrawingHorizontalLine: _horizontalGridLines,
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
drawBelowEverything: true,
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
maxIncluded: false,
|
||||
minIncluded: false,
|
||||
reservedSize: 40,
|
||||
getTitlesWidget: (double value, TitleMeta meta) {
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(
|
||||
'${meta.formattedValue}°',
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
axisNameWidget: Container(
|
||||
margin: const EdgeInsets.only(bottom: 20),
|
||||
child: const Text(
|
||||
'Day of month',
|
||||
style: TextStyle(
|
||||
color: AppColors.contentColorGreen,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
),
|
||||
axisNameSize: 40,
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 38,
|
||||
maxIncluded: false,
|
||||
interval: 1,
|
||||
getTitlesWidget: _bottomTitles,
|
||||
),
|
||||
),
|
||||
),
|
||||
lineTouchData: LineTouchData(
|
||||
enabled: true,
|
||||
handleBuiltInTouches: false,
|
||||
touchCallback: _touchCallback,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (monthlyWeatherData == null)
|
||||
const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
bool get _canGoNext => _currentMonthIndex < 11;
|
||||
|
||||
bool get _canGoPrevious => _currentMonthIndex > 0;
|
||||
|
||||
void _previousMonth() {
|
||||
if (!_canGoPrevious) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_currentMonthIndex--;
|
||||
});
|
||||
}
|
||||
|
||||
void _nextMonth() {
|
||||
if (!_canGoNext) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_currentMonthIndex++;
|
||||
});
|
||||
}
|
||||
|
||||
FlSpotErrorRangePainter _errorPainter(
|
||||
LineChartSpotErrorRangeCallbackInput input,
|
||||
) =>
|
||||
FlSimpleErrorPainter(
|
||||
lineWidth: 1.0,
|
||||
lineColor: _interactedSpotIndex == input.spotIndex
|
||||
? Colors.white
|
||||
: Colors.white38,
|
||||
showErrorTexts: _interactedSpotIndex == input.spotIndex,
|
||||
);
|
||||
|
||||
FlLine _horizontalGridLines(double value) {
|
||||
final isZero = value == 0.0;
|
||||
return FlLine(
|
||||
color: isZero ? Colors.white38 : Colors.blueGrey,
|
||||
strokeWidth: isZero ? 0.8 : 0.4,
|
||||
dashArray: isZero ? null : [8, 4],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _bottomTitles(double value, TitleMeta meta) {
|
||||
final day = value.toInt() + 1;
|
||||
|
||||
final isDayHovered = _interactedSpotIndex == day - 1;
|
||||
|
||||
final isImportantToShow = day % 5 == 0 || day == 1;
|
||||
|
||||
if (!isImportantToShow && !isDayHovered) {
|
||||
return const SizedBox();
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(
|
||||
day.toString(),
|
||||
style: TextStyle(
|
||||
color: isDayHovered
|
||||
? AppColors.contentColorWhite
|
||||
: AppColors.contentColorGreen,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_touchCallback(FlTouchEvent event, LineTouchResponse? touchResponse) {
|
||||
if (!event.isInterestedForInteractions ||
|
||||
touchResponse?.lineBarSpots == null ||
|
||||
touchResponse!.lineBarSpots!.isEmpty) {
|
||||
setState(() {
|
||||
_interactedSpotIndex = -1;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_interactedSpotIndex = touchResponse.lineBarSpots!.first.spotIndex;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class _WeatherData with EquatableMixin {
|
||||
final String name;
|
||||
final DateTime datetime;
|
||||
final double tempmax;
|
||||
final double tempmin;
|
||||
final double temp;
|
||||
final double feelslikemax;
|
||||
final double feelslikemin;
|
||||
final double feelslike;
|
||||
final double dew;
|
||||
final double humidity;
|
||||
final double precip;
|
||||
final double precipprob;
|
||||
final double precipcover;
|
||||
final String preciptype;
|
||||
final double snow;
|
||||
final double snowdepth;
|
||||
final double windgust;
|
||||
final double windspeed;
|
||||
final double winddir;
|
||||
final double sealevelpressure;
|
||||
final double cloudcover;
|
||||
final double visibility;
|
||||
final double solarradiation;
|
||||
final double solarenergy;
|
||||
final double uvindex;
|
||||
final double severerisk;
|
||||
final DateTime sunrise;
|
||||
final DateTime sunset;
|
||||
final double moonphase;
|
||||
final String conditions;
|
||||
final String description;
|
||||
final String icon;
|
||||
final String stations;
|
||||
|
||||
const _WeatherData({
|
||||
required this.name,
|
||||
required this.datetime,
|
||||
required this.tempmax,
|
||||
required this.tempmin,
|
||||
required this.temp,
|
||||
required this.feelslikemax,
|
||||
required this.feelslikemin,
|
||||
required this.feelslike,
|
||||
required this.dew,
|
||||
required this.humidity,
|
||||
required this.precip,
|
||||
required this.precipprob,
|
||||
required this.precipcover,
|
||||
required this.preciptype,
|
||||
required this.snow,
|
||||
required this.snowdepth,
|
||||
required this.windgust,
|
||||
required this.windspeed,
|
||||
required this.winddir,
|
||||
required this.sealevelpressure,
|
||||
required this.cloudcover,
|
||||
required this.visibility,
|
||||
required this.solarradiation,
|
||||
required this.solarenergy,
|
||||
required this.uvindex,
|
||||
required this.severerisk,
|
||||
required this.sunrise,
|
||||
required this.sunset,
|
||||
required this.moonphase,
|
||||
required this.conditions,
|
||||
required this.description,
|
||||
required this.icon,
|
||||
required this.stations,
|
||||
});
|
||||
|
||||
// parse from csv row
|
||||
// name,datetime,tempmax,tempmin,temp,feelslikemax,feelslikemin,feelslike,dew,humidity,precip,precipprob,precipcover,preciptype,snow,snowdepth,windgust,windspeed,winddir,sealevelpressure,cloudcover,visibility,solarradiation,solarenergy,uvindex,severerisk,sunrise,sunset,moonphase,conditions,description,icon,stations
|
||||
// "Amsterdam,Netherlands",2024-01-01,9.1,6.4,8,5.3,2.5,4.1,5.1,82.4,14.26,100,37.5,rain,0,0,53.9,40.2,225.9,1000.1,88.7,20.5,20.6,1.8,2,,2024-01-01T08:50:34,2024-01-01T16:37:06,0.68,"Rain, Partially cloudy",Partly cloudy throughout the day with a chance of rain throughout the day.,rain,"06260099999,D3248,06348099999,06249099999,C0449,06240099999,06269099999,06257099999,06344099999"
|
||||
factory _WeatherData.fromCsv(List<String> row) => _WeatherData(
|
||||
name: row[0],
|
||||
datetime: DateTime.parse(row[1]),
|
||||
tempmax: double.parse(row[2]),
|
||||
tempmin: double.parse(row[3]),
|
||||
temp: double.parse(row[4]),
|
||||
feelslikemax: double.parse(row[5]),
|
||||
feelslikemin: double.parse(row[6]),
|
||||
feelslike: double.parse(row[7]),
|
||||
dew: double.parse(row[8]),
|
||||
humidity: double.parse(row[9]),
|
||||
precip: double.parse(row[10]),
|
||||
precipprob: double.parse(row[11]),
|
||||
precipcover: double.parse(row[12]),
|
||||
preciptype: row[13],
|
||||
snow: double.parse(row[14]),
|
||||
snowdepth: double.parse(row[15]),
|
||||
windgust: double.parse(row[16]),
|
||||
windspeed: double.parse(row[17]),
|
||||
winddir: double.parse(row[18]),
|
||||
sealevelpressure: double.parse(row[19]),
|
||||
cloudcover: double.parse(row[20]),
|
||||
visibility: double.parse(row[21]),
|
||||
solarradiation: double.parse(row[22]),
|
||||
solarenergy: double.parse(row[23]),
|
||||
uvindex: double.parse(row[24]),
|
||||
severerisk: row[25].isEmpty ? 0 : double.parse(row[25]),
|
||||
sunrise: DateTime.parse(row[26]),
|
||||
sunset: DateTime.parse(row[27]),
|
||||
moonphase: double.parse(row[28]),
|
||||
conditions: row[29],
|
||||
description: row[30],
|
||||
icon: row[31],
|
||||
stations: row[32],
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
name,
|
||||
datetime,
|
||||
tempmax,
|
||||
tempmin,
|
||||
temp,
|
||||
feelslikemax,
|
||||
feelslikemin,
|
||||
feelslike,
|
||||
dew,
|
||||
humidity,
|
||||
precip,
|
||||
precipprob,
|
||||
precipcover,
|
||||
preciptype,
|
||||
snow,
|
||||
snowdepth,
|
||||
windgust,
|
||||
windspeed,
|
||||
winddir,
|
||||
sealevelpressure,
|
||||
cloudcover,
|
||||
visibility,
|
||||
solarradiation,
|
||||
solarenergy,
|
||||
uvindex,
|
||||
severerisk,
|
||||
sunrise,
|
||||
sunset,
|
||||
moonphase,
|
||||
conditions,
|
||||
description,
|
||||
icon,
|
||||
stations,
|
||||
];
|
||||
}
|
||||
294
example/lib/presentation/samples/line/line_chart_sample2.dart
Normal file
294
example/lib/presentation/samples/line/line_chart_sample2.dart
Normal file
@@ -0,0 +1,294 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LineChartSample2 extends StatefulWidget {
|
||||
const LineChartSample2({super.key});
|
||||
|
||||
@override
|
||||
State<LineChartSample2> createState() => _LineChartSample2State();
|
||||
}
|
||||
|
||||
class _LineChartSample2State extends State<LineChartSample2> {
|
||||
List<Color> gradientColors = [
|
||||
AppColors.contentColorCyan,
|
||||
AppColors.contentColorBlue,
|
||||
];
|
||||
|
||||
bool showAvg = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Stack(
|
||||
children: <Widget>[
|
||||
AspectRatio(
|
||||
aspectRatio: 1.70,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
right: 18,
|
||||
left: 12,
|
||||
top: 24,
|
||||
bottom: 12,
|
||||
),
|
||||
child: LineChart(
|
||||
showAvg ? avgData() : mainData(),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: 60,
|
||||
height: 34,
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
showAvg = !showAvg;
|
||||
});
|
||||
},
|
||||
child: Text(
|
||||
'avg',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: showAvg
|
||||
? Colors.white.withValues(alpha: 0.5)
|
||||
: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
);
|
||||
Widget text;
|
||||
switch (value.toInt()) {
|
||||
case 2:
|
||||
text = const Text('MAR', style: style);
|
||||
break;
|
||||
case 5:
|
||||
text = const Text('JUN', style: style);
|
||||
break;
|
||||
case 8:
|
||||
text = const Text('SEP', style: style);
|
||||
break;
|
||||
default:
|
||||
text = const Text('', style: style);
|
||||
break;
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: text,
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 15,
|
||||
);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 1:
|
||||
text = '10K';
|
||||
break;
|
||||
case 3:
|
||||
text = '30k';
|
||||
break;
|
||||
case 5:
|
||||
text = '50k';
|
||||
break;
|
||||
default:
|
||||
return Container();
|
||||
}
|
||||
|
||||
return Text(text, style: style, textAlign: TextAlign.left);
|
||||
}
|
||||
|
||||
LineChartData mainData() {
|
||||
return LineChartData(
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: true,
|
||||
horizontalInterval: 1,
|
||||
verticalInterval: 1,
|
||||
getDrawingHorizontalLine: (value) {
|
||||
return const FlLine(
|
||||
color: AppColors.mainGridLineColor,
|
||||
strokeWidth: 1,
|
||||
);
|
||||
},
|
||||
getDrawingVerticalLine: (value) {
|
||||
return const FlLine(
|
||||
color: AppColors.mainGridLineColor,
|
||||
strokeWidth: 1,
|
||||
);
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 30,
|
||||
interval: 1,
|
||||
getTitlesWidget: bottomTitleWidgets,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
interval: 1,
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
reservedSize: 42,
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: Border.all(color: const Color(0xff37434d)),
|
||||
),
|
||||
minX: 0,
|
||||
maxX: 11,
|
||||
minY: 0,
|
||||
maxY: 6,
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: const [
|
||||
FlSpot(0, 3),
|
||||
FlSpot(2.6, 2),
|
||||
FlSpot(4.9, 5),
|
||||
FlSpot(6.8, 3.1),
|
||||
FlSpot(8, 4),
|
||||
FlSpot(9.5, 3),
|
||||
FlSpot(11, 4),
|
||||
],
|
||||
isCurved: true,
|
||||
gradient: LinearGradient(
|
||||
colors: gradientColors,
|
||||
),
|
||||
barWidth: 5,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
gradient: LinearGradient(
|
||||
colors: gradientColors
|
||||
.map((color) => color.withValues(alpha: 0.3))
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
LineChartData avgData() {
|
||||
return LineChartData(
|
||||
lineTouchData: const LineTouchData(enabled: false),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawHorizontalLine: true,
|
||||
verticalInterval: 1,
|
||||
horizontalInterval: 1,
|
||||
getDrawingVerticalLine: (value) {
|
||||
return const FlLine(
|
||||
color: Color(0xff37434d),
|
||||
strokeWidth: 1,
|
||||
);
|
||||
},
|
||||
getDrawingHorizontalLine: (value) {
|
||||
return const FlLine(
|
||||
color: Color(0xff37434d),
|
||||
strokeWidth: 1,
|
||||
);
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 30,
|
||||
getTitlesWidget: bottomTitleWidgets,
|
||||
interval: 1,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
reservedSize: 42,
|
||||
interval: 1,
|
||||
),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: Border.all(color: const Color(0xff37434d)),
|
||||
),
|
||||
minX: 0,
|
||||
maxX: 11,
|
||||
minY: 0,
|
||||
maxY: 6,
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: const [
|
||||
FlSpot(0, 3.44),
|
||||
FlSpot(2.6, 3.44),
|
||||
FlSpot(4.9, 3.44),
|
||||
FlSpot(6.8, 3.44),
|
||||
FlSpot(8, 3.44),
|
||||
FlSpot(9.5, 3.44),
|
||||
FlSpot(11, 3.44),
|
||||
],
|
||||
isCurved: true,
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
ColorTween(begin: gradientColors[0], end: gradientColors[1])
|
||||
.lerp(0.2)!,
|
||||
ColorTween(begin: gradientColors[0], end: gradientColors[1])
|
||||
.lerp(0.2)!,
|
||||
],
|
||||
),
|
||||
barWidth: 5,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
ColorTween(begin: gradientColors[0], end: gradientColors[1])
|
||||
.lerp(0.2)!
|
||||
.withValues(alpha: 0.1),
|
||||
ColorTween(begin: gradientColors[0], end: gradientColors[1])
|
||||
.lerp(0.2)!
|
||||
.withValues(alpha: 0.1),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
451
example/lib/presentation/samples/line/line_chart_sample3.dart
Normal file
451
example/lib/presentation/samples/line/line_chart_sample3.dart
Normal file
@@ -0,0 +1,451 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LineChartSample3 extends StatefulWidget {
|
||||
LineChartSample3({
|
||||
super.key,
|
||||
Color? lineColor,
|
||||
Color? indicatorLineColor,
|
||||
Color? indicatorTouchedLineColor,
|
||||
Color? indicatorSpotStrokeColor,
|
||||
Color? indicatorTouchedSpotStrokeColor,
|
||||
Color? bottomTextColor,
|
||||
Color? bottomTouchedTextColor,
|
||||
Color? averageLineColor,
|
||||
Color? tooltipBgColor,
|
||||
Color? tooltipTextColor,
|
||||
}) : lineColor = lineColor ?? AppColors.contentColorRed,
|
||||
indicatorLineColor = indicatorLineColor ??
|
||||
AppColors.contentColorYellow.withValues(alpha: 0.2),
|
||||
indicatorTouchedLineColor =
|
||||
indicatorTouchedLineColor ?? AppColors.contentColorYellow,
|
||||
indicatorSpotStrokeColor = indicatorSpotStrokeColor ??
|
||||
AppColors.contentColorYellow.withValues(alpha: 0.5),
|
||||
indicatorTouchedSpotStrokeColor =
|
||||
indicatorTouchedSpotStrokeColor ?? AppColors.contentColorYellow,
|
||||
bottomTextColor = bottomTextColor ??
|
||||
AppColors.contentColorYellow.withValues(alpha: 0.2),
|
||||
bottomTouchedTextColor =
|
||||
bottomTouchedTextColor ?? AppColors.contentColorYellow,
|
||||
averageLineColor = averageLineColor ??
|
||||
AppColors.contentColorGreen.withValues(alpha: 0.8),
|
||||
tooltipBgColor = tooltipBgColor ?? AppColors.contentColorGreen,
|
||||
tooltipTextColor = tooltipTextColor ?? Colors.black;
|
||||
|
||||
final Color lineColor;
|
||||
final Color indicatorLineColor;
|
||||
final Color indicatorTouchedLineColor;
|
||||
final Color indicatorSpotStrokeColor;
|
||||
final Color indicatorTouchedSpotStrokeColor;
|
||||
final Color bottomTextColor;
|
||||
final Color bottomTouchedTextColor;
|
||||
final Color averageLineColor;
|
||||
final Color tooltipBgColor;
|
||||
final Color tooltipTextColor;
|
||||
|
||||
List<String> get weekDays =>
|
||||
const ['Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
|
||||
|
||||
List<double> get yValues => const [1.3, 1, 1.8, 1.5, 2.2, 1.8, 3];
|
||||
|
||||
@override
|
||||
State createState() => _LineChartSample3State();
|
||||
}
|
||||
|
||||
class _LineChartSample3State extends State<LineChartSample3> {
|
||||
late double touchedValue;
|
||||
|
||||
bool fitInsideBottomTitle = true;
|
||||
bool fitInsideLeftTitle = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
touchedValue = -1;
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta) {
|
||||
if (value % 1 != 0) {
|
||||
return Container();
|
||||
}
|
||||
final style = TextStyle(
|
||||
color: AppColors.mainTextColor1.withValues(alpha: 0.5),
|
||||
fontSize: 10,
|
||||
);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = '';
|
||||
break;
|
||||
case 1:
|
||||
text = '1k calories';
|
||||
break;
|
||||
case 2:
|
||||
text = '2k calories';
|
||||
break;
|
||||
case 3:
|
||||
text = '3k calories';
|
||||
break;
|
||||
default:
|
||||
return Container();
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 6,
|
||||
fitInside: fitInsideLeftTitle
|
||||
? SideTitleFitInsideData.fromTitleMeta(meta)
|
||||
: SideTitleFitInsideData.disable(),
|
||||
child: Text(text, style: style, textAlign: TextAlign.center),
|
||||
);
|
||||
}
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta) {
|
||||
final isTouched = value == touchedValue;
|
||||
final style = TextStyle(
|
||||
color: isTouched ? widget.bottomTouchedTextColor : widget.bottomTextColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
|
||||
if (value % 1 != 0) {
|
||||
return Container();
|
||||
}
|
||||
return SideTitleWidget(
|
||||
space: 4,
|
||||
meta: meta,
|
||||
fitInside: fitInsideBottomTitle
|
||||
? SideTitleFitInsideData.fromTitleMeta(meta, distanceFromEdge: 0)
|
||||
: SideTitleFitInsideData.disable(),
|
||||
child: Text(
|
||||
widget.weekDays[value.toInt()],
|
||||
style: style,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
const SizedBox(height: 10),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
'Average Line',
|
||||
style: TextStyle(
|
||||
color: widget.averageLineColor.withValues(alpha: 1),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
const Text(
|
||||
' and ',
|
||||
style: TextStyle(
|
||||
color: AppColors.mainTextColor1,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'Indicators',
|
||||
style: TextStyle(
|
||||
color: widget.indicatorLineColor.withValues(alpha: 1),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(
|
||||
height: 18,
|
||||
),
|
||||
AspectRatio(
|
||||
aspectRatio: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(right: 20.0, left: 12),
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
lineTouchData: LineTouchData(
|
||||
getTouchedSpotIndicator:
|
||||
(LineChartBarData barData, List<int> spotIndexes) {
|
||||
return spotIndexes.map((spotIndex) {
|
||||
final spot = barData.spots[spotIndex];
|
||||
if (spot.x == 0 || spot.x == 6) {
|
||||
return null;
|
||||
}
|
||||
return TouchedSpotIndicatorData(
|
||||
FlLine(
|
||||
color: widget.indicatorTouchedLineColor,
|
||||
strokeWidth: 4,
|
||||
),
|
||||
FlDotData(
|
||||
getDotPainter: (spot, percent, barData, index) {
|
||||
if (index.isEven) {
|
||||
return FlDotCirclePainter(
|
||||
radius: 8,
|
||||
color: Colors.white,
|
||||
strokeWidth: 5,
|
||||
strokeColor:
|
||||
widget.indicatorTouchedSpotStrokeColor,
|
||||
);
|
||||
} else {
|
||||
return FlDotSquarePainter(
|
||||
size: 16,
|
||||
color: Colors.white,
|
||||
strokeWidth: 5,
|
||||
strokeColor:
|
||||
widget.indicatorTouchedSpotStrokeColor,
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
getTooltipColor: (touchedSpot) => widget.tooltipBgColor,
|
||||
getTooltipItems: (List<LineBarSpot> touchedBarSpots) {
|
||||
return touchedBarSpots.map((barSpot) {
|
||||
final flSpot = barSpot;
|
||||
if (flSpot.x == 0 || flSpot.x == 6) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TextAlign textAlign;
|
||||
switch (flSpot.x.toInt()) {
|
||||
case 1:
|
||||
textAlign = TextAlign.left;
|
||||
break;
|
||||
case 5:
|
||||
textAlign = TextAlign.right;
|
||||
break;
|
||||
default:
|
||||
textAlign = TextAlign.center;
|
||||
}
|
||||
|
||||
return LineTooltipItem(
|
||||
'${widget.weekDays[flSpot.x.toInt()]} \n',
|
||||
TextStyle(
|
||||
color: widget.tooltipTextColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
children: [
|
||||
TextSpan(
|
||||
text: flSpot.y.toString(),
|
||||
style: TextStyle(
|
||||
color: widget.tooltipTextColor,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
const TextSpan(
|
||||
text: ' k ',
|
||||
style: TextStyle(
|
||||
fontStyle: FontStyle.italic,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
const TextSpan(
|
||||
text: 'calories',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.normal,
|
||||
),
|
||||
),
|
||||
],
|
||||
textAlign: textAlign,
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
),
|
||||
touchCallback:
|
||||
(FlTouchEvent event, LineTouchResponse? lineTouch) {
|
||||
if (!event.isInterestedForInteractions ||
|
||||
lineTouch == null ||
|
||||
lineTouch.lineBarSpots == null) {
|
||||
setState(() {
|
||||
touchedValue = -1;
|
||||
});
|
||||
return;
|
||||
}
|
||||
final value = lineTouch.lineBarSpots![0].x;
|
||||
|
||||
if (value == 0 || value == 6) {
|
||||
setState(() {
|
||||
touchedValue = -1;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
touchedValue = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
extraLinesData: ExtraLinesData(
|
||||
horizontalLines: [
|
||||
HorizontalLine(
|
||||
y: 1.8,
|
||||
color: widget.averageLineColor,
|
||||
strokeWidth: 3,
|
||||
dashArray: [20, 10],
|
||||
),
|
||||
],
|
||||
),
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
isStepLineChart: true,
|
||||
spots: widget.yValues.asMap().entries.map((e) {
|
||||
return FlSpot(e.key.toDouble(), e.value);
|
||||
}).toList(),
|
||||
isCurved: false,
|
||||
barWidth: 4,
|
||||
color: widget.lineColor,
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
widget.lineColor.withValues(alpha: 0.5),
|
||||
widget.lineColor.withValues(alpha: 0),
|
||||
],
|
||||
stops: const [0.5, 1.0],
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
),
|
||||
spotsLine: BarAreaSpotsLine(
|
||||
show: true,
|
||||
flLineStyle: FlLine(
|
||||
color: widget.indicatorLineColor,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
checkToShowSpotLine: (spot) {
|
||||
if (spot.x == 0 || spot.x == 6) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
),
|
||||
),
|
||||
dotData: FlDotData(
|
||||
show: true,
|
||||
getDotPainter: (spot, percent, barData, index) {
|
||||
if (index.isEven) {
|
||||
return FlDotCirclePainter(
|
||||
radius: 6,
|
||||
color: Colors.white,
|
||||
strokeWidth: 3,
|
||||
strokeColor: widget.indicatorSpotStrokeColor,
|
||||
);
|
||||
} else {
|
||||
return FlDotSquarePainter(
|
||||
size: 12,
|
||||
color: Colors.white,
|
||||
strokeWidth: 3,
|
||||
strokeColor: widget.indicatorSpotStrokeColor,
|
||||
);
|
||||
}
|
||||
},
|
||||
checkToShowDot: (spot, barData) {
|
||||
return spot.x != 0 && spot.x != 6;
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
minY: 0,
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: Border.all(
|
||||
color: AppColors.borderColor,
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawHorizontalLine: true,
|
||||
drawVerticalLine: true,
|
||||
checkToShowHorizontalLine: (value) => value % 1 == 0,
|
||||
checkToShowVerticalLine: (value) => value % 1 == 0,
|
||||
getDrawingHorizontalLine: (value) {
|
||||
if (value == 0) {
|
||||
return const FlLine(
|
||||
color: AppColors.contentColorOrange,
|
||||
strokeWidth: 2,
|
||||
);
|
||||
} else {
|
||||
return const FlLine(
|
||||
color: AppColors.mainGridLineColor,
|
||||
strokeWidth: 0.5,
|
||||
);
|
||||
}
|
||||
},
|
||||
getDrawingVerticalLine: (value) {
|
||||
if (value == 0) {
|
||||
return const FlLine(
|
||||
color: Colors.redAccent,
|
||||
strokeWidth: 10,
|
||||
);
|
||||
} else {
|
||||
return const FlLine(
|
||||
color: AppColors.mainGridLineColor,
|
||||
strokeWidth: 0.5,
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 46,
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 40,
|
||||
getTitlesWidget: bottomTitleWidgets,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Column(
|
||||
children: [
|
||||
const Text('Fit Inside Title Option'),
|
||||
const Divider(),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text('Left Title'),
|
||||
Switch(
|
||||
value: fitInsideLeftTitle,
|
||||
onChanged: (value) => setState(() {
|
||||
fitInsideLeftTitle = value;
|
||||
}),
|
||||
),
|
||||
const Text('Bottom Title'),
|
||||
Switch(
|
||||
value: fitInsideBottomTitle,
|
||||
onChanged: (value) => setState(() {
|
||||
fitInsideBottomTitle = value;
|
||||
}),
|
||||
)
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
202
example/lib/presentation/samples/line/line_chart_sample4.dart
Normal file
202
example/lib/presentation/samples/line/line_chart_sample4.dart
Normal file
@@ -0,0 +1,202 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LineChartSample4 extends StatelessWidget {
|
||||
LineChartSample4({
|
||||
super.key,
|
||||
Color? mainLineColor,
|
||||
Color? belowLineColor,
|
||||
Color? aboveLineColor,
|
||||
}) : mainLineColor =
|
||||
mainLineColor ?? AppColors.contentColorYellow.withValues(alpha: 1),
|
||||
belowLineColor =
|
||||
belowLineColor ?? AppColors.contentColorPink.withValues(alpha: 1),
|
||||
aboveLineColor = aboveLineColor ??
|
||||
AppColors.contentColorPurple.withValues(alpha: 0.7);
|
||||
|
||||
final Color mainLineColor;
|
||||
final Color belowLineColor;
|
||||
final Color aboveLineColor;
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta) {
|
||||
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:
|
||||
return Container();
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 4,
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: mainLineColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
color: AppColors.mainTextColor3,
|
||||
fontSize: 12,
|
||||
);
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text('\$ ${value + 0.5}', style: style),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const cutOffYValue = 5.0;
|
||||
|
||||
return AspectRatio(
|
||||
aspectRatio: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 12,
|
||||
right: 28,
|
||||
top: 22,
|
||||
bottom: 12,
|
||||
),
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
lineTouchData: const LineTouchData(enabled: false),
|
||||
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: true,
|
||||
barWidth: 8,
|
||||
color: mainLineColor,
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
color: belowLineColor,
|
||||
cutOffY: cutOffYValue,
|
||||
applyCutOffY: true,
|
||||
),
|
||||
aboveBarData: BarAreaData(
|
||||
show: true,
|
||||
color: aboveLineColor,
|
||||
cutOffY: cutOffYValue,
|
||||
applyCutOffY: true,
|
||||
),
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
),
|
||||
],
|
||||
minY: 0,
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
axisNameWidget: Text(
|
||||
'2019',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: mainLineColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 18,
|
||||
interval: 1,
|
||||
getTitlesWidget: bottomTitleWidgets,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
axisNameSize: 20,
|
||||
axisNameWidget: const Text(
|
||||
'Value',
|
||||
style: TextStyle(
|
||||
color: AppColors.mainTextColor2,
|
||||
),
|
||||
),
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
interval: 1,
|
||||
reservedSize: 40,
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: Border.all(
|
||||
color: AppColors.borderColor,
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
horizontalInterval: 1,
|
||||
checkToShowHorizontalLine: (double value) {
|
||||
return value == 1 || value == 6 || value == 4 || value == 5;
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
289
example/lib/presentation/samples/line/line_chart_sample5.dart
Normal file
289
example/lib/presentation/samples/line/line_chart_sample5.dart
Normal file
@@ -0,0 +1,289 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LineChartSample5 extends StatefulWidget {
|
||||
const LineChartSample5({
|
||||
super.key,
|
||||
Color? gradientColor1,
|
||||
Color? gradientColor2,
|
||||
Color? gradientColor3,
|
||||
Color? indicatorStrokeColor,
|
||||
}) : gradientColor1 = gradientColor1 ?? AppColors.contentColorBlue,
|
||||
gradientColor2 = gradientColor2 ?? AppColors.contentColorPink,
|
||||
gradientColor3 = gradientColor3 ?? AppColors.contentColorRed,
|
||||
indicatorStrokeColor = indicatorStrokeColor ?? AppColors.mainTextColor1;
|
||||
|
||||
final Color gradientColor1;
|
||||
final Color gradientColor2;
|
||||
final Color gradientColor3;
|
||||
final Color indicatorStrokeColor;
|
||||
|
||||
@override
|
||||
State<LineChartSample5> createState() => _LineChartSample5State();
|
||||
}
|
||||
|
||||
class _LineChartSample5State extends State<LineChartSample5> {
|
||||
List<int> showingTooltipOnSpots = [1, 3, 5];
|
||||
|
||||
List<FlSpot> get allSpots => const [
|
||||
FlSpot(0, 1),
|
||||
FlSpot(1, 2),
|
||||
FlSpot(2, 1.5),
|
||||
FlSpot(3, 3),
|
||||
FlSpot(4, 3.5),
|
||||
FlSpot(5, 5),
|
||||
FlSpot(6, 8),
|
||||
];
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta, double chartWidth) {
|
||||
final style = TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.contentColorPink,
|
||||
fontFamily: 'Digital',
|
||||
fontSize: 18 * chartWidth / 500,
|
||||
);
|
||||
String text;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
text = '00:00';
|
||||
break;
|
||||
case 1:
|
||||
text = '04:00';
|
||||
break;
|
||||
case 2:
|
||||
text = '08:00';
|
||||
break;
|
||||
case 3:
|
||||
text = '12:00';
|
||||
break;
|
||||
case 4:
|
||||
text = '16:00';
|
||||
break;
|
||||
case 5:
|
||||
text = '20:00';
|
||||
break;
|
||||
case 6:
|
||||
text = '23:59';
|
||||
break;
|
||||
default:
|
||||
return Container();
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final lineBarsData = [
|
||||
LineChartBarData(
|
||||
showingIndicators: showingTooltipOnSpots,
|
||||
spots: allSpots,
|
||||
isCurved: true,
|
||||
barWidth: 4,
|
||||
shadow: const Shadow(
|
||||
blurRadius: 8,
|
||||
),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
widget.gradientColor1.withValues(alpha: 0.4),
|
||||
widget.gradientColor2.withValues(alpha: 0.4),
|
||||
widget.gradientColor3.withValues(alpha: 0.4),
|
||||
],
|
||||
),
|
||||
),
|
||||
dotData: const FlDotData(show: false),
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
widget.gradientColor1,
|
||||
widget.gradientColor2,
|
||||
widget.gradientColor3,
|
||||
],
|
||||
stops: const [0.1, 0.4, 0.9],
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
final tooltipsOnBar = lineBarsData[0];
|
||||
|
||||
return AspectRatio(
|
||||
aspectRatio: 2.5,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24.0,
|
||||
vertical: 10,
|
||||
),
|
||||
child: LayoutBuilder(builder: (context, constraints) {
|
||||
return LineChart(
|
||||
LineChartData(
|
||||
showingTooltipIndicators: showingTooltipOnSpots.map((index) {
|
||||
return ShowingTooltipIndicators([
|
||||
LineBarSpot(
|
||||
tooltipsOnBar,
|
||||
lineBarsData.indexOf(tooltipsOnBar),
|
||||
tooltipsOnBar.spots[index],
|
||||
),
|
||||
]);
|
||||
}).toList(),
|
||||
lineTouchData: LineTouchData(
|
||||
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) {
|
||||
return TouchedSpotIndicatorData(
|
||||
const FlLine(
|
||||
color: Colors.pink,
|
||||
),
|
||||
FlDotData(
|
||||
show: true,
|
||||
getDotPainter: (spot, percent, barData, index) =>
|
||||
FlDotCirclePainter(
|
||||
radius: 8,
|
||||
color: lerpGradient(
|
||||
barData.gradient!.colors,
|
||||
barData.gradient!.stops!,
|
||||
percent / 100,
|
||||
),
|
||||
strokeWidth: 2,
|
||||
strokeColor: widget.indicatorStrokeColor,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
getTooltipColor: (touchedSpot) => Colors.pink,
|
||||
tooltipBorderRadius: BorderRadius.circular(8),
|
||||
getTooltipItems: (List<LineBarSpot> lineBarsSpot) {
|
||||
return lineBarsSpot.map((lineBarSpot) {
|
||||
return LineTooltipItem(
|
||||
lineBarSpot.y.toString(),
|
||||
const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
),
|
||||
),
|
||||
lineBarsData: lineBarsData,
|
||||
minY: 0,
|
||||
titlesData: FlTitlesData(
|
||||
leftTitles: const AxisTitles(
|
||||
axisNameWidget: Text('count'),
|
||||
axisNameSize: 24,
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
reservedSize: 0,
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
interval: 1,
|
||||
getTitlesWidget: (value, meta) {
|
||||
return bottomTitleWidgets(
|
||||
value,
|
||||
meta,
|
||||
constraints.maxWidth,
|
||||
);
|
||||
},
|
||||
reservedSize: 30,
|
||||
),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
axisNameWidget: Text('count'),
|
||||
sideTitles: SideTitles(
|
||||
showTitles: false,
|
||||
reservedSize: 0,
|
||||
),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
axisNameWidget: Text(
|
||||
'Wall clock',
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
axisNameSize: 24,
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 0,
|
||||
),
|
||||
),
|
||||
),
|
||||
gridData: const FlGridData(show: false),
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: Border.all(
|
||||
color: AppColors.borderColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Lerps between a [LinearGradient] colors, based on [t]
|
||||
Color lerpGradient(List<Color> colors, List<double> stops, double t) {
|
||||
if (colors.isEmpty) {
|
||||
throw ArgumentError('"colors" is empty.');
|
||||
} else if (colors.length == 1) {
|
||||
return colors[0];
|
||||
}
|
||||
|
||||
if (stops.length != colors.length) {
|
||||
stops = [];
|
||||
|
||||
/// provided gradientColorStops is invalid and we calculate it here
|
||||
colors.asMap().forEach((index, color) {
|
||||
final percent = 1.0 / (colors.length - 1);
|
||||
stops.add(percent * index);
|
||||
});
|
||||
}
|
||||
|
||||
for (var s = 0; s < stops.length - 1; s++) {
|
||||
final leftStop = stops[s];
|
||||
final rightStop = stops[s + 1];
|
||||
final leftColor = colors[s];
|
||||
final rightColor = colors[s + 1];
|
||||
if (t <= leftStop) {
|
||||
return leftColor;
|
||||
} else if (t < rightStop) {
|
||||
final sectionT = (t - leftStop) / (rightStop - leftStop);
|
||||
return Color.lerp(leftColor, rightColor, sectionT)!;
|
||||
}
|
||||
}
|
||||
return colors.last;
|
||||
}
|
||||
286
example/lib/presentation/samples/line/line_chart_sample6.dart
Normal file
286
example/lib/presentation/samples/line/line_chart_sample6.dart
Normal file
@@ -0,0 +1,286 @@
|
||||
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';
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class LineChartSample6 extends StatelessWidget {
|
||||
LineChartSample6({
|
||||
super.key,
|
||||
Color? line1Color1,
|
||||
Color? line1Color2,
|
||||
Color? line2Color1,
|
||||
Color? line2Color2,
|
||||
}) : line1Color1 = line1Color1 ?? AppColors.contentColorOrange,
|
||||
line1Color2 = line1Color2 ?? AppColors.contentColorOrange.darken(60),
|
||||
line2Color1 = line2Color1 ?? AppColors.contentColorBlue.darken(60),
|
||||
line2Color2 = line2Color2 ?? AppColors.contentColorBlue {
|
||||
minSpotX = spots.first.x;
|
||||
maxSpotX = spots.first.x;
|
||||
minSpotY = spots.first.y;
|
||||
maxSpotY = spots.first.y;
|
||||
|
||||
for (final spot in spots) {
|
||||
if (spot.x > maxSpotX) {
|
||||
maxSpotX = spot.x;
|
||||
}
|
||||
|
||||
if (spot.x < minSpotX) {
|
||||
minSpotX = spot.x;
|
||||
}
|
||||
|
||||
if (spot.y > maxSpotY) {
|
||||
maxSpotY = spot.y;
|
||||
}
|
||||
|
||||
if (spot.y < minSpotY) {
|
||||
minSpotY = spot.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final Color line1Color1;
|
||||
final Color line1Color2;
|
||||
final Color line2Color1;
|
||||
final Color line2Color2;
|
||||
|
||||
final spots = const [
|
||||
FlSpot(0, 1),
|
||||
FlSpot(2, 5),
|
||||
FlSpot(4, 3),
|
||||
FlSpot(6, 5),
|
||||
];
|
||||
|
||||
final spots2 = const [
|
||||
FlSpot(0, 3),
|
||||
FlSpot(2, 1),
|
||||
FlSpot(4, 2),
|
||||
FlSpot(6, 1),
|
||||
];
|
||||
|
||||
late double minSpotX;
|
||||
late double maxSpotX;
|
||||
late double minSpotY;
|
||||
late double maxSpotY;
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta) {
|
||||
final style = TextStyle(
|
||||
color: line1Color1,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
);
|
||||
|
||||
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
|
||||
|
||||
if (intValue == (maxSpotY + minSpotY)) {
|
||||
return Text('', style: style);
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 6),
|
||||
child: Text(
|
||||
intValue.toString(),
|
||||
style: style,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget rightTitleWidgets(double value, TitleMeta meta) {
|
||||
final style = TextStyle(
|
||||
color: line2Color2,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
);
|
||||
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
|
||||
|
||||
if (intValue == (maxSpotY + minSpotY)) {
|
||||
return Text('', style: style);
|
||||
}
|
||||
|
||||
return Text(intValue.toString(), style: style, textAlign: TextAlign.right);
|
||||
}
|
||||
|
||||
Widget topTitleWidgets(double value, TitleMeta meta) {
|
||||
if (value % 1 != 0) {
|
||||
return Container();
|
||||
}
|
||||
const style = TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.mainTextColor2,
|
||||
);
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(value.toInt().toString(), style: style),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 22, bottom: 20),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 2,
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
lineTouchData: LineTouchData(
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
tooltipBorderRadius: BorderRadius.zero,
|
||||
getTooltipColor: (spot) => Colors.white,
|
||||
getTooltipItems: (List<LineBarSpot> touchedSpots) {
|
||||
return touchedSpots.map((LineBarSpot touchedSpot) {
|
||||
return LineTooltipItem(
|
||||
touchedSpot.y.toString(),
|
||||
TextStyle(
|
||||
color: touchedSpot.bar.gradient!.colors.first,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 20,
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
),
|
||||
getTouchedSpotIndicator: (
|
||||
_,
|
||||
indicators,
|
||||
) {
|
||||
return indicators
|
||||
.map((int index) => const TouchedSpotIndicatorData(
|
||||
FlLine(color: Colors.transparent),
|
||||
FlDotData(show: false),
|
||||
))
|
||||
.toList();
|
||||
},
|
||||
touchSpotThreshold: 12,
|
||||
distanceCalculator:
|
||||
(Offset touchPoint, Offset spotPixelCoordinates) =>
|
||||
(touchPoint - spotPixelCoordinates).distance,
|
||||
),
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
line1Color1,
|
||||
line1Color2,
|
||||
],
|
||||
),
|
||||
spots: reverseSpots(spots, minSpotY, maxSpotY),
|
||||
isCurved: true,
|
||||
isStrokeCapRound: true,
|
||||
barWidth: 10,
|
||||
belowBarData: BarAreaData(
|
||||
show: false,
|
||||
),
|
||||
dotData: FlDotData(
|
||||
show: true,
|
||||
getDotPainter: (spot, percent, barData, index) {
|
||||
return FlDotCirclePainter(
|
||||
radius: 12,
|
||||
color: Color.lerp(
|
||||
line1Color1,
|
||||
line1Color2,
|
||||
percent / 100,
|
||||
)!,
|
||||
strokeColor: Colors.white,
|
||||
strokeWidth: 1,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
LineChartBarData(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
line2Color1,
|
||||
line2Color2,
|
||||
],
|
||||
),
|
||||
spots: reverseSpots(spots2, minSpotY, maxSpotY),
|
||||
isCurved: true,
|
||||
isStrokeCapRound: true,
|
||||
barWidth: 10,
|
||||
belowBarData: BarAreaData(
|
||||
show: false,
|
||||
),
|
||||
dotData: FlDotData(
|
||||
show: true,
|
||||
getDotPainter: (spot, percent, barData, index) {
|
||||
return FlDotCirclePainter(
|
||||
radius: 12,
|
||||
color: Color.lerp(
|
||||
line2Color1,
|
||||
line2Color2,
|
||||
percent / 100,
|
||||
)!,
|
||||
strokeColor: Colors.white,
|
||||
strokeWidth: 1,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
minY: 0,
|
||||
maxY: maxSpotY + minSpotY,
|
||||
titlesData: FlTitlesData(
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
reservedSize: 38,
|
||||
),
|
||||
),
|
||||
rightTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: rightTitleWidgets,
|
||||
reservedSize: 30,
|
||||
),
|
||||
),
|
||||
bottomTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 32,
|
||||
getTitlesWidget: topTitleWidgets,
|
||||
),
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: true,
|
||||
checkToShowHorizontalLine: (value) {
|
||||
final intValue = reverseY(value, minSpotY, maxSpotY).toInt();
|
||||
|
||||
if (intValue == (maxSpotY + minSpotY).toInt()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: const Border(
|
||||
left: BorderSide(color: AppColors.borderColor),
|
||||
top: BorderSide(color: AppColors.borderColor),
|
||||
bottom: BorderSide(color: Colors.transparent),
|
||||
right: BorderSide(color: Colors.transparent),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
double reverseY(double y, double minX, double maxX) {
|
||||
return (maxX + minX) - y;
|
||||
}
|
||||
|
||||
List<FlSpot> reverseSpots(List<FlSpot> inputSpots, double minY, double maxY) {
|
||||
return inputSpots.map((spot) {
|
||||
return spot.copyWith(y: (maxY + minY) - spot.y);
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
193
example/lib/presentation/samples/line/line_chart_sample7.dart
Normal file
193
example/lib/presentation/samples/line/line_chart_sample7.dart
Normal file
@@ -0,0 +1,193 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LineChartSample7 extends StatelessWidget {
|
||||
LineChartSample7({
|
||||
super.key,
|
||||
Color? line1Color,
|
||||
Color? line2Color,
|
||||
Color? betweenColor,
|
||||
}) : line1Color = line1Color ?? AppColors.contentColorGreen,
|
||||
line2Color = line2Color ?? AppColors.contentColorRed,
|
||||
betweenColor =
|
||||
betweenColor ?? AppColors.contentColorRed.withValues(alpha: 0.5);
|
||||
|
||||
final Color line1Color;
|
||||
final Color line2Color;
|
||||
final Color betweenColor;
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
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:
|
||||
return Container();
|
||||
}
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 4,
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(fontSize: 10);
|
||||
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(
|
||||
'\$ ${value + 0.5}',
|
||||
style: style,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AspectRatio(
|
||||
aspectRatio: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 10,
|
||||
right: 18,
|
||||
top: 10,
|
||||
bottom: 4,
|
||||
),
|
||||
child: LineChart(
|
||||
LineChartData(
|
||||
lineTouchData: const LineTouchData(enabled: false),
|
||||
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: true,
|
||||
barWidth: 2,
|
||||
color: line1Color,
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
),
|
||||
LineChartBarData(
|
||||
spots: const [
|
||||
FlSpot(0, 7),
|
||||
FlSpot(1, 3),
|
||||
FlSpot(2, 4),
|
||||
FlSpot(3, 2),
|
||||
FlSpot(4, 3),
|
||||
FlSpot(5, 4),
|
||||
FlSpot(6, 5),
|
||||
FlSpot(7, 3),
|
||||
FlSpot(8, 1),
|
||||
FlSpot(9, 8),
|
||||
FlSpot(10, 1),
|
||||
FlSpot(11, 3),
|
||||
],
|
||||
isCurved: false,
|
||||
barWidth: 2,
|
||||
color: line2Color,
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
),
|
||||
],
|
||||
betweenBarsData: [
|
||||
BetweenBarsData(
|
||||
fromIndex: 0,
|
||||
toIndex: 1,
|
||||
color: betweenColor,
|
||||
)
|
||||
],
|
||||
minY: 0,
|
||||
borderData: FlBorderData(
|
||||
show: false,
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
interval: 1,
|
||||
getTitlesWidget: bottomTitleWidgets,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
interval: 1,
|
||||
reservedSize: 36,
|
||||
),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
horizontalInterval: 1,
|
||||
checkToShowHorizontalLine: (double value) {
|
||||
return value == 1 || value == 6 || value == 4 || value == 5;
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
291
example/lib/presentation/samples/line/line_chart_sample8.dart
Normal file
291
example/lib/presentation/samples/line/line_chart_sample8.dart
Normal file
@@ -0,0 +1,291 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
|
||||
class LineChartSample8 extends StatefulWidget {
|
||||
const LineChartSample8({super.key});
|
||||
|
||||
@override
|
||||
State<LineChartSample8> createState() => _LineChartSample8State();
|
||||
}
|
||||
|
||||
class _LineChartSample8State extends State<LineChartSample8> {
|
||||
List<Color> gradientColors = const [
|
||||
Color(0xffEEF3FE),
|
||||
Color(0xffEEF3FE),
|
||||
];
|
||||
|
||||
bool showAvg = false;
|
||||
|
||||
Future<ui.Image> loadImage(String asset) async {
|
||||
final data = await rootBundle.load(asset);
|
||||
final codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
|
||||
final fi = await codec.getNextFrame();
|
||||
return fi.image;
|
||||
}
|
||||
|
||||
Future<SizedPicture> loadSvg() async {
|
||||
const rawSvg =
|
||||
'<svg height="14" width="14" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" transform="translate(-.000014)"><circle cx="7" cy="7" fill="#495DFF" r="7"/><path d="m7 10.9999976c1.6562389 0 2.99998569-1.34374678 2.99998569-2.99999283s-1.34374679-4.99998808-2.99998569-4.99998808c-1.6562532 0-3 3.34374203-3 4.99998808s1.3437468 2.99999283 3 2.99999283z" fill="#fff" fill-rule="nonzero"/></g></svg>';
|
||||
|
||||
final pictureInfo =
|
||||
await vg.loadPicture(const SvgStringLoader(rawSvg), null);
|
||||
|
||||
final sizedPicture = SizedPicture(pictureInfo.picture, 14, 14);
|
||||
return sizedPicture;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<SizedPicture>(
|
||||
future: loadSvg(),
|
||||
builder: (BuildContext context, imageSnapshot) {
|
||||
if (imageSnapshot.connectionState == ConnectionState.done) {
|
||||
return Stack(
|
||||
children: <Widget>[
|
||||
AspectRatio(
|
||||
aspectRatio: 1.70,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
right: 18,
|
||||
left: 12,
|
||||
top: 24,
|
||||
bottom: 12,
|
||||
),
|
||||
child: LineChart(
|
||||
mainData(imageSnapshot.data!),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta) {
|
||||
const style = TextStyle(
|
||||
fontSize: 10,
|
||||
color: AppColors.mainTextColor1,
|
||||
);
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Text(meta.formattedValue, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta) {
|
||||
IconData icon;
|
||||
Color color;
|
||||
switch (value.toInt()) {
|
||||
case 0:
|
||||
icon = Icons.wb_sunny;
|
||||
color = AppColors.contentColorYellow;
|
||||
break;
|
||||
case 2:
|
||||
icon = Icons.wine_bar_sharp;
|
||||
color = AppColors.contentColorRed;
|
||||
break;
|
||||
case 4:
|
||||
icon = Icons.watch_later;
|
||||
color = AppColors.contentColorGreen;
|
||||
break;
|
||||
case 6:
|
||||
icon = Icons.whatshot;
|
||||
color = AppColors.contentColorOrange;
|
||||
break;
|
||||
default:
|
||||
throw StateError('Invalid');
|
||||
}
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
child: Icon(
|
||||
icon,
|
||||
color: color,
|
||||
size: 32,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
LineChartData mainData(SizedPicture sizedPicture) {
|
||||
return LineChartData(
|
||||
rangeAnnotations: RangeAnnotations(
|
||||
verticalRangeAnnotations: [
|
||||
VerticalRangeAnnotation(
|
||||
x1: 2,
|
||||
x2: 5,
|
||||
color: AppColors.contentColorBlue.withValues(alpha: 0.2),
|
||||
),
|
||||
VerticalRangeAnnotation(
|
||||
x1: 8,
|
||||
x2: 9,
|
||||
color: AppColors.contentColorBlue.withValues(alpha: 0.2),
|
||||
),
|
||||
],
|
||||
horizontalRangeAnnotations: [
|
||||
HorizontalRangeAnnotation(
|
||||
y1: 2,
|
||||
y2: 3,
|
||||
color: AppColors.contentColorGreen.withValues(alpha: 0.3),
|
||||
),
|
||||
],
|
||||
),
|
||||
// uncomment to see ExtraLines with RangeAnnotations
|
||||
extraLinesData: ExtraLinesData(
|
||||
// extraLinesOnTop: true,
|
||||
horizontalLines: [
|
||||
HorizontalLine(
|
||||
y: 5,
|
||||
color: AppColors.contentColorGreen,
|
||||
strokeWidth: 2,
|
||||
dashArray: [5, 10],
|
||||
label: HorizontalLineLabel(
|
||||
show: true,
|
||||
alignment: Alignment.topRight,
|
||||
padding: const EdgeInsets.only(right: 5, bottom: 5),
|
||||
style: const TextStyle(
|
||||
fontSize: 9,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
labelResolver: (line) => 'H: ${line.y}',
|
||||
),
|
||||
),
|
||||
],
|
||||
verticalLines: [
|
||||
VerticalLine(
|
||||
x: 5.7,
|
||||
color: AppColors.contentColorBlue,
|
||||
strokeWidth: 2,
|
||||
dashArray: [5, 10],
|
||||
label: VerticalLineLabel(
|
||||
show: true,
|
||||
alignment: Alignment.bottomRight,
|
||||
padding: const EdgeInsets.only(left: 5, bottom: 5),
|
||||
style: const TextStyle(
|
||||
fontSize: 9,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
direction: LabelDirection.vertical,
|
||||
labelResolver: (line) => 'V: ${line.x}',
|
||||
),
|
||||
),
|
||||
VerticalLine(
|
||||
x: 8.5,
|
||||
color: Colors.transparent,
|
||||
sizedPicture: sizedPicture,
|
||||
),
|
||||
VerticalLine(
|
||||
x: 3.5,
|
||||
color: Colors.transparent,
|
||||
sizedPicture: sizedPicture,
|
||||
)
|
||||
],
|
||||
),
|
||||
gridData: const FlGridData(
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
drawHorizontalLine: false,
|
||||
verticalInterval: 1,
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
show: true,
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 30,
|
||||
getTitlesWidget: bottomTitleWidgets,
|
||||
interval: 4,
|
||||
),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
drawBelowEverything: true,
|
||||
sideTitles: SideTitles(
|
||||
interval: 2,
|
||||
showTitles: true,
|
||||
getTitlesWidget: leftTitleWidgets,
|
||||
reservedSize: 40,
|
||||
),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
lineTouchData: LineTouchData(
|
||||
getTouchLineEnd: (data, index) => double.infinity,
|
||||
getTouchedSpotIndicator:
|
||||
(LineChartBarData barData, List<int> spotIndexes) {
|
||||
return spotIndexes.map((spotIndex) {
|
||||
return TouchedSpotIndicatorData(
|
||||
const FlLine(color: AppColors.contentColorOrange, strokeWidth: 3),
|
||||
FlDotData(
|
||||
getDotPainter: (spot, percent, barData, index) =>
|
||||
FlDotCirclePainter(
|
||||
radius: 8,
|
||||
color: AppColors.contentColorOrange,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
getTooltipColor: (touchedSpot) => AppColors.contentColorRed,
|
||||
getTooltipItems: (List<LineBarSpot> touchedSpots) => touchedSpots
|
||||
.map((LineBarSpot touchedSpot) => LineTooltipItem(
|
||||
touchedSpot.y.toString(),
|
||||
const TextStyle(
|
||||
color: AppColors.contentColorWhite,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
),
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
border: Border.all(
|
||||
color: AppColors.borderColor,
|
||||
),
|
||||
),
|
||||
minX: 0,
|
||||
maxX: 11,
|
||||
minY: 0,
|
||||
maxY: 6,
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: const [
|
||||
FlSpot(0, 1),
|
||||
FlSpot(2, 1),
|
||||
FlSpot(4.9, 5),
|
||||
FlSpot(6.8, 5),
|
||||
FlSpot(7.5, 3.5),
|
||||
FlSpot.nullSpot,
|
||||
FlSpot(7.5, 2),
|
||||
FlSpot(8, 1),
|
||||
FlSpot(10, 2),
|
||||
FlSpot(11, 2.5),
|
||||
],
|
||||
dashArray: [10, 6],
|
||||
isCurved: true,
|
||||
color: AppColors.contentColorRed,
|
||||
barWidth: 4,
|
||||
isStrokeCapRound: true,
|
||||
dotData: const FlDotData(
|
||||
show: false,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
154
example/lib/presentation/samples/line/line_chart_sample9.dart
Normal file
154
example/lib/presentation/samples/line/line_chart_sample9.dart
Normal file
@@ -0,0 +1,154 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class LineChartSample9 extends StatelessWidget {
|
||||
LineChartSample9({super.key});
|
||||
|
||||
final spots = List.generate(101, (i) => (i - 50) / 10)
|
||||
.map((x) => FlSpot(x, cos(x)))
|
||||
.toList();
|
||||
|
||||
Widget bottomTitleWidgets(double value, TitleMeta meta, double chartWidth) {
|
||||
if (value % 1 != 0) {
|
||||
return Container();
|
||||
}
|
||||
final style = TextStyle(
|
||||
color: AppColors.contentColorBlue,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: min(18, 18 * chartWidth / 300),
|
||||
);
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 16,
|
||||
child: Text(meta.formattedValue, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
Widget leftTitleWidgets(double value, TitleMeta meta, double chartWidth) {
|
||||
final style = TextStyle(
|
||||
color: AppColors.contentColorYellow,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: min(18, 18 * chartWidth / 300),
|
||||
);
|
||||
return SideTitleWidget(
|
||||
meta: meta,
|
||||
space: 16,
|
||||
child: Text(meta.formattedValue, style: style),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 12,
|
||||
bottom: 12,
|
||||
right: 20,
|
||||
top: 20,
|
||||
),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
return LineChart(
|
||||
LineChartData(
|
||||
lineTouchData: LineTouchData(
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
maxContentWidth: 100,
|
||||
getTooltipColor: (touchedSpot) => Colors.black,
|
||||
getTooltipItems: (touchedSpots) {
|
||||
return touchedSpots.map((LineBarSpot touchedSpot) {
|
||||
final textStyle = TextStyle(
|
||||
color: touchedSpot.bar.gradient?.colors[0] ??
|
||||
touchedSpot.bar.color,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
);
|
||||
return LineTooltipItem(
|
||||
'${touchedSpot.x}, ${touchedSpot.y.toStringAsFixed(2)}',
|
||||
textStyle,
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
),
|
||||
handleBuiltInTouches: true,
|
||||
getTouchLineStart: (data, index) => 0,
|
||||
),
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
color: AppColors.contentColorPink,
|
||||
spots: spots,
|
||||
isCurved: true,
|
||||
isStrokeCapRound: true,
|
||||
barWidth: 3,
|
||||
belowBarData: BarAreaData(
|
||||
show: false,
|
||||
),
|
||||
dotData: const FlDotData(show: false),
|
||||
),
|
||||
],
|
||||
minY: -1.5,
|
||||
maxY: 1.5,
|
||||
titlesData: FlTitlesData(
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: (value, meta) =>
|
||||
leftTitleWidgets(value, meta, constraints.maxWidth),
|
||||
reservedSize: 56,
|
||||
),
|
||||
drawBelowEverything: true,
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
getTitlesWidget: (value, meta) =>
|
||||
bottomTitleWidgets(value, meta, constraints.maxWidth),
|
||||
reservedSize: 36,
|
||||
interval: 1,
|
||||
),
|
||||
drawBelowEverything: true,
|
||||
),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
gridData: FlGridData(
|
||||
show: true,
|
||||
drawHorizontalLine: true,
|
||||
drawVerticalLine: true,
|
||||
horizontalInterval: 1.5,
|
||||
verticalInterval: 5,
|
||||
checkToShowHorizontalLine: (value) {
|
||||
return value.toInt() == 0;
|
||||
},
|
||||
getDrawingHorizontalLine: (_) => FlLine(
|
||||
color: AppColors.contentColorBlue.withValues(alpha: 1),
|
||||
dashArray: [8, 2],
|
||||
strokeWidth: 0.8,
|
||||
),
|
||||
getDrawingVerticalLine: (_) => FlLine(
|
||||
color: AppColors.contentColorYellow.withValues(alpha: 1),
|
||||
dashArray: [8, 2],
|
||||
strokeWidth: 0.8,
|
||||
),
|
||||
checkToShowVerticalLine: (value) {
|
||||
return value.toInt() == 0;
|
||||
},
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user