-
-
Save mit-mit/08168a773a56bb58c7abfd9b8e82f467 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart'; | |
import 'package:charts_flutter/flutter.dart' as charts; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Chart Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Chart Demo'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class ClicksPerYear { | |
final String year; | |
final int clicks; | |
final charts.Color color; | |
ClicksPerYear(this.year, this.clicks, Color color) | |
: this.color = charts.Color( | |
r: color.red, g: color.green, b: color.blue, a: color.alpha); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
var data = [ | |
ClicksPerYear('2016', 12, Colors.red), | |
ClicksPerYear('2017', 42, Colors.yellow), | |
ClicksPerYear('2018', _counter, Colors.green), | |
]; | |
var series = [ | |
charts.Series( | |
domainFn: (ClicksPerYear clickData, _) => clickData.year, | |
measureFn: (ClicksPerYear clickData, _) => clickData.clicks, | |
colorFn: (ClicksPerYear clickData, _) => clickData.color, | |
id: 'Clicks', | |
data: data, | |
), | |
]; | |
var chart = charts.BarChart( | |
series, | |
animate: true, | |
); | |
var chartWidget = Padding( | |
padding: EdgeInsets.all(32.0), | |
child: SizedBox( | |
height: 200.0, | |
child: chart, | |
), | |
); | |
return Scaffold( | |
appBar: AppBar(title: Text(widget.title)), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('You have pushed the button this many times:'), | |
Text('$_counter', style: Theme.of(context).textTheme.display1), | |
chartWidget, | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
This is not working on Dart 2 and Flutter 0.2.8
Sample code has been updated.
When importing the library, make sure to specify version 0.2.0 or later to get the Dart 2 compatible version:
dependencies:
flutter:
sdk: flutter
charts_flutter: ^0.2.0
I’m trying to do combine chart(Bar and Line) in a flutter, this is possible in flutter charts?
I just run the same code here and I am getting this:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞════════════════════
The following assertion was thrown building RawGestureDetector(state:
RawGestureDetectorState#e7f44(gestures: [tap])):
type '(ClicksPerYear, int) => String' is not a subtype of type '(dynamic, int) => String'
What can I do?
Any ideas on how to set and adjust axis/grid lines
@krel024 just remove the from new charts.BarChart for it to compile
How can I change the time series chart language?
I'm getting an error of having 1 argument for var chart = new charts.BarChart<ClicksPerYear>(...);
. It's saying there should be zero arguments... Any idea how to fix this?
I changed it to var chart = new charts.BarChart(...);
to solve my issue if anyone is wondering.
I'd like to just assign a color rather than bury it in a class. I'd like to do something like this:
colorFn: (,) => Colors.red,
any ideas?
@nosamttam you can do something around this
colorFn: (_, __) => new charts.Color(r: Colors.red.red, g: Colors.red.green, b: Colors.red.blue, a: Colors.red.alpha)
I'm getting an error of having 1 argument for
var chart = new charts.BarChart<ClicksPerYear>(...);
. It's saying there should be zero arguments... Any idea how to fix this?
I got the same issue.
Just Remove the "" from the code "var chart = new charts.BarChart(...);"
This is not working on Dart 2 and Flutter 0.2.8
yeah i have the same problem!
Replace this
var chart = new charts.BarChart<ClicksPerYear>(
series,
animate: true,
);
with
var chart = new charts.BarChart(
series,
animate: true,
);
it works for me
Thanks @gonzdeveloper :-)
display1 has been discontinued (deprecated), replace this:
Text('$_counter', style: Theme.of(context).textTheme.display1),
with
Text('$_counter', style: Theme.of(context).textTheme.headline4),
Thanks for sharing the example code, it helped me a lot.
Error: The getter 'body1' isn't defined for the class 'TextTheme'.
Please update this to
new charts.BarChart<ClicksPerYear>
for Dart2