Skip to content

Instantly share code, notes, and snippets.

@imaNNeo
Created June 21, 2024 15:51
Show Gist options
  • Save imaNNeo/40860a163db20affabaef346039dc422 to your computer and use it in GitHub Desktop.
Save imaNNeo/40860a163db20affabaef346039dc422 to your computer and use it in GitHub Desktop.
Update LineChart interval example
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double horizontalInterval = 1.0;
double verticalInterval = 1.0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
width: 500,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 300,
child: Row(
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'$horizontalInterval',
style: GoogleFonts.roboto(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Expanded(
child: RotatedBox(
quarterTurns: 3,
child: SizedBox(
width: 20,
height: 40,
child: Slider(
value: horizontalInterval,
divisions: 8,
onChanged: (newValue) {
setState(() {
horizontalInterval = newValue;
});
},
min: 1,
max: 5,
),
),
),
),
],
),
Expanded(
child: _MyChart(
horizontalInterval: horizontalInterval,
verticalInterval: verticalInterval,
),
),
],
),
),
Row(
children: [
Expanded(
child: Slider(
value: verticalInterval,
onChanged: (newValue) {
setState(() {
verticalInterval = newValue;
});
},
min: 1,
max: 5,
divisions: 8,
),
),
Text(
'$verticalInterval',
style: GoogleFonts.roboto(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
],
),
],
),
),
),
);
}
}
class _MyChart extends StatelessWidget {
const _MyChart({
required this.horizontalInterval,
required this.verticalInterval,
});
final double horizontalInterval;
final double verticalInterval;
@override
Widget build(BuildContext context) {
return LineChart(
LineChartData(
lineBarsData: [
LineChartBarData(
spots: const [
FlSpot(0, 1),
FlSpot(1, 2),
FlSpot(2, 3),
FlSpot(3, 2),
FlSpot(4, 5),
FlSpot(5, 3),
FlSpot(6, 6),
FlSpot(7, 4),
FlSpot(8, 5),
FlSpot(9, 9),
],
),
],
titlesData: FlTitlesData(
bottomTitles: AxisTitles(
sideTitles: SideTitles(
reservedSize: 26,
showTitles: true,
interval: verticalInterval,
),
),
topTitles: AxisTitles(
sideTitles: SideTitles(
reservedSize: 26,
showTitles: true,
interval: verticalInterval,
),
),
leftTitles: AxisTitles(
sideTitles: SideTitles(
reservedSize: 40,
showTitles: true,
interval: horizontalInterval,
),
),
rightTitles: AxisTitles(
sideTitles: SideTitles(
reservedSize: 40,
showTitles: true,
interval: horizontalInterval,
),
),
),
gridData: FlGridData(
horizontalInterval: horizontalInterval,
verticalInterval: verticalInterval,
),
),
);
}
}
@imaNNeo
Copy link
Author

imaNNeo commented Jun 21, 2024

Answered in here:
imaNNeo/fl_chart#1691

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment