Skip to content

Instantly share code, notes, and snippets.

@mit-mit
Last active January 31, 2023 11:25
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mit-mit/08168a773a56bb58c7abfd9b8e82f467 to your computer and use it in GitHub Desktop.
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),
),
);
}
}
@lorrainekan
Copy link

lorrainekan commented Mar 29, 2018

Please update this to new charts.BarChart<ClicksPerYear> for Dart2

@rodydavis
Copy link

This is not working on Dart 2 and Flutter 0.2.8

@mit-mit
Copy link
Author

mit-mit commented Apr 17, 2018

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

@vignesh7501
Copy link

I’m trying to do combine chart(Bar and Line) in a flutter, this is possible in flutter charts?

@karelric
Copy link

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?

@riftninja
Copy link

riftninja commented May 8, 2018

Any ideas on how to set and adjust axis/grid lines

@andrewpmoore
Copy link

@krel024 just remove the from new charts.BarChart for it to compile

@marianoarga
Copy link

How can I change the time series chart language?

@JosephDunivan
Copy link

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?

@JosephDunivan
Copy link

I changed it to var chart = new charts.BarChart(...); to solve my issue if anyone is wondering.

@nosamttam
Copy link

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?

@caiohsr14
Copy link

@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)

@Alam969
Copy link

Alam969 commented Jan 17, 2019

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.

@Moses41
Copy link

Moses41 commented Jan 22, 2019

Just Remove the "" from the code "var chart = new charts.BarChart(...);"

@Allan-Nava
Copy link

This is not working on Dart 2 and Flutter 0.2.8

yeah i have the same problem!

@gonzdeveloper
Copy link

Replace this

var chart = new charts.BarChart<ClicksPerYear>(
  series,
  animate: true,
);

with

var chart = new charts.BarChart(
  series,
  animate: true,
);

it works for me

@lascapi
Copy link

lascapi commented Apr 16, 2019

Thanks @gonzdeveloper :-)

@fabric-io-rodrigues
Copy link

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.

@AliPunjabi
Copy link

Error: The getter 'body1' isn't defined for the class 'TextTheme'.

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