Flutter Chart with Graphic
A skill for helping users build data visualizations in Flutter using the
Graphic library — a Grammar of Graphics-based charting library.
When to Use
- User wants to create any type of chart in Flutter (bar, line, area, pie, scatter, heatmap, etc.)
- User needs help configuring chart appearance, interactivity, or animations
- User asks about Graphic library APIs or usage patterns
- User wants to convert data into visual representations
- User wants to create custom, bespoke visualizations — Graphic's customization system is its most powerful feature and a key strength of AI-assisted development
Why Custom Charts?
Standard chart types (bar, line, pie) only cover a fraction of data visualization needs. Graphic is built on Grammar of Graphics theory, which means every visual layer is independently customizable:
- Custom Shapes — Render any geometry (triangles, lollipops, arrows, gauges, bullet charts, sparklines, etc.)
- Custom Tooltips — Fully custom interactive overlays with any layout
- Custom Annotations — Draw any graphics at data or absolute positions
- Custom Encoders — Map data to visuals using arbitrary logic
- Custom Modifiers — Define custom collision/arrangement behavior
This makes AI-assisted development especially valuable: the AI can write custom shape renderers, coordinate math, and drawing code that would be tedious to implement manually.
Always consider customization when the user's requirements don't perfectly match a standard chart type. See
references/customization.md
for the comprehensive customization guide.
Quick Start
Installation
yaml
dependencies:
graphic: ^latest
Import
dart
import 'package:graphic/graphic.dart';
Minimal Example
dart
Chart(
data: [
{'category': 'A', 'value': 10},
{'category': 'B', 'value': 20},
{'category': 'C', 'value': 15},
],
variables: {
'category': Variable(
accessor: (Map map) => map['category'] as String,
),
'value': Variable(
accessor: (Map map) => map['value'] as num,
),
},
marks: [IntervalMark()],
axes: [Defaults.horizontalAxis, Defaults.verticalAxis],
)
Core Concepts
Graphic follows the Grammar of Graphics theory. A chart is composed of independent, declarative layers:
data → Variable → Scale → Encode → Mark → Shape → Render
Everything is configured through the single
widget constructor. There are no imperative APIs — all configuration is declarative.
The Chart Widget
is the only public widget. The type parameter
is the data item type. Key parameters:
| Parameter | Type | Purpose |
|---|
| | Required. Data to visualize |
| Map<String, Variable<D, dynamic>>
| Required. How to extract values from data |
| | Required. Geometry types to render |
| | Coordinate system (default: ) |
| | Axis configuration |
| | Tooltip on interaction |
| | Crosshair on interaction |
| | Static annotations |
| | Named selection behaviors |
| | Data transforms (filter, sort, proportion) |
| EdgeInsets Function(Size)?
| Padding around the plot area |
| — | Set on each Mark, not on Chart |
See
references/chart-widget.md
for full parameter details.
Variables
Variables define how raw data maps to abstract values:
dart
variables: {
'date': Variable(
accessor: (MyData d) => d.date,
scale: TimeScale(formatter: (t) => DateFormat.MMMd().format(t)),
),
'value': Variable(
accessor: (MyData d) => d.value,
scale: LinearScale(min: 0),
),
}
Each variable can have a
that controls domain-to-range mapping. See
.
Marks
Marks are the geometric elements that represent data:
| Mark | Use Case | Default Shape |
|---|
| Bar charts, histograms, pie charts | |
| Line charts, sparklines | |
| Area charts, stream graphs | |
| Scatter plots, bubble charts | |
| Heatmaps, treemaps | |
| Candlestick, custom shapes | Any |
See
for parameters and
for shape options.
Encodes (Aesthetic Mappings)
Encodes map data values to visual properties. Every encode supports three modes:
- Fixed value:
ColorEncode(value: Colors.blue)
- Variable mapping:
ColorEncode(variable: 'type', values: Defaults.colors10)
- Custom function:
ColorEncode(encoder: (tuple) => myColorLogic(tuple))
Available encodes:
,
,
,
,
,
.
Position Algebra (Varset)
Position is specified using
algebra with three operators:
| Operator | Name | Effect |
|---|
| Cross | Assigns variables to different dimensions (x, y) |
| Blend | Combines variables on the same dimension |
| Nest | Groups data by a variable |
dart
// x=date, y=value, grouped by type
position: Varset('date') * Varset('value') / Varset('type')
Coordinates
- — Cartesian coordinates (default). Supports , , .
- — Polar/radial coordinates for pie charts, radar charts, rose charts. Supports , , , .
See
references/coordinates.md
for details.
Interaction
Define named selections and use them in encode updaters:
dart
selections: {
'tap': PointSelection(dim: Dim.x),
},
marks: [
IntervalMark(
color: ColorEncode(
value: Colors.blue,
updaters: {
'tap': {
true: (color) => color.withAlpha(255), // selected
false: (color) => color.withAlpha(100), // not selected
},
},
),
),
],
See
for selection types and gesture configuration.
Modifiers
Modifiers handle geometry collision/arrangement:
- — Stack elements (stacked bar, stacked area)
- — Place side by side (grouped bar)
- — Random scatter (strip plot)
- — Center symmetrically (stream graph)
Animation
dart
IntervalMark(
transition: Transition(duration: Duration(seconds: 1), curve: Curves.easeOut),
entrance: {MarkEntrance.y}, // Animate from y=0
tag: (tuple) => tuple['id'].toString(), // Element matching for transitions
)
Common Chart Recipes
Bar Chart
dart
marks: [IntervalMark()]
coord: RectCoord() // default
Horizontal Bar Chart
dart
marks: [IntervalMark()]
coord: RectCoord(transposed: true)
Grouped Bar Chart
dart
marks: [
IntervalMark(
position: Varset('x') * Varset('y') / Varset('group'),
color: ColorEncode(variable: 'group', values: Defaults.colors10),
modifiers: [DodgeModifier()],
),
]
Stacked Bar Chart
dart
marks: [
IntervalMark(
position: Varset('x') * Varset('y') / Varset('group'),
color: ColorEncode(variable: 'group', values: Defaults.colors10),
modifiers: [StackModifier()],
),
]
Line Chart
Smooth Line Chart
dart
marks: [
LineMark(shape: ShapeEncode(value: BasicLineShape(smooth: true))),
]
Area Chart
Pie Chart
dart
transforms: [Proportion(variable: 'value', as: 'percent')],
marks: [
IntervalMark(
position: Varset('percent') / Varset('category'),
color: ColorEncode(variable: 'category', values: Defaults.colors10),
modifiers: [StackModifier()],
),
],
coord: PolarCoord(transposed: true, dimCount: 1),
Scatter Plot
dart
marks: [
PointMark(
size: SizeEncode(variable: 'magnitude', values: [2, 20]),
color: ColorEncode(variable: 'type', values: Defaults.colors10),
),
]
Rose Chart
dart
marks: [IntervalMark(color: ColorEncode(variable: 'name', values: Defaults.colors10))],
coord: PolarCoord(startRadius: 0.15),
See
for more complete examples.
Custom Chart Development
Graphic's greatest strength is its fully customizable rendering pipeline. When standard chart types don't meet requirements, create custom visualizations by implementing your own shapes, tooltips, annotations, and encoders.
Custom Shapes — The Core Extension Point
Create entirely new chart geometries by extending a Shape base class and implementing
:
dart
class LollipopShape extends IntervalShape {
LollipopShape({this.radius = 6});
final double radius;
@override
List<MarkElement> drawGroupPrimitives(
List<Attributes> group, CoordConv coord, Offset origin,
) {
final rst = <MarkElement>[];
for (var item in group) {
if (item.position.any((p) => !p.dy.isFinite)) continue;
final style = getPaintStyle(item, false, 0, null, null);
final base = coord.convert(item.position[0]);
final tip = coord.convert(item.position[1]);
// Stem line
rst.add(PolylineElement(
points: [base, tip],
style: PaintStyle(strokeColor: style.fillColor, strokeWidth: 2),
tag: item.tag,
));
// Circle head
rst.add(CircleElement(
center: tip, radius: radius, style: style, tag: item.tag,
));
}
return rst;
}
@override
bool equalTo(Object other) =>
other is LollipopShape && radius == other.radius;
}
Available base classes:
,
,
,
,
Available drawing primitives:
,
,
,
,
,
,
,
,
,
Custom Tooltip Renderer
dart
tooltip: TooltipGuide(
renderer: (Size size, Offset anchor, Map<int, Tuple> selected) {
final t = selected.values.first;
return [
RectElement(
rect: Rect.fromCenter(center: anchor, width: 100, height: 36),
borderRadius: BorderRadius.circular(6),
style: PaintStyle(fillColor: Colors.black87, elevation: 4),
),
LabelElement(
text: '${t['name']}: ${t['value']}',
anchor: anchor,
style: LabelStyle(
textStyle: TextStyle(color: Colors.white, fontSize: 12),
align: Alignment.center,
),
),
];
},
)
Custom Encoder Functions
Every encode supports arbitrary logic via
:
dart
color: ColorEncode(encoder: (tuple) {
final v = tuple['value'] as num;
return v > 100 ? Colors.red : v > 50 ? Colors.orange : Colors.green;
}),
label: LabelEncode(encoder: (tuple) => Label(
'${tuple['value']}%',
LabelStyle(textStyle: TextStyle(
fontSize: (tuple['value'] as num) > 50 ? 14 : 10,
fontWeight: FontWeight.bold,
)),
)),
Key Classes for Custom Development
| Class | Purpose |
|---|
| Encoded data element — contains , , , , , |
| Converts normalized [0,1] positions to canvas pixels via / |
| Full paint specification — fill, stroke, gradient, dash, elevation, shadow |
| Drawing primitives — the building blocks for custom rendering |
| Utility to extract from |
See references/customization.md
for the comprehensive guide including coordinate handling, all drawing primitives, custom annotations, custom modifiers, and best practices.
Guides & Annotations
- Axes: Use , for quick setup, or customize with . See .
- Tooltip: with optional custom renderer. See .
- Crosshair: . See .
- Annotations: , , , . See
references/annotations.md
.
Styling
- — Fill/stroke colors, gradients, dash patterns, shadows
- — Text style, alignment, rotation, offset
- — Built-in color palettes (, ), preset axes, default styles
Event Streams
Charts expose
parameters for external event coupling:
- — Send/receive gesture events
- — React to resize events
- — React to data change events
- (on Mark) — Programmatically set selections
Dynamic Data
Simply update the
parameter via
:
dart
setState(() {
data = newData;
});
The chart automatically re-renders with transition animations when
is set.
Important Notes
- The widget is the only public widget — all configuration is via its constructor
- and on encodes are mutually exclusive
- Pie charts require transform +
PolarCoord(transposed: true, dimCount: 1)
- Use on marks for smooth transition animations between data states
- (nest) operator is required for grouping (multi-series, stacked, dodged)
- Function-typed properties are always treated as "unchanged" in equality comparisons
Reference Files
| File | Content |
|---|
references/customization.md
| Custom chart development guide — shapes, tooltips, annotations, encoders, drawing primitives |
references/chart-widget.md
| Full Chart widget parameter reference |
| All mark types and parameters |
| Aesthetic encoding reference |
| Shape types for each mark |
| Scale types and configuration |
references/coordinates.md
| Coordinate system reference |
| Axis, Tooltip, Crosshair reference |
references/annotations.md
| Annotation types reference |
| Selection and interaction reference |
| Geometry modifier reference |
| Data transform reference |
| Varset algebra reference |
| Transition and entrance animation reference |
| PaintStyle, LabelStyle, Defaults reference |
| Complete chart examples |
When answering user questions, read the source code for the most accurate and up-to-date API details. The library uses extensive code comments as documentation. Key source directories:
- — Chart widget
- — Mark types
- — Encode types
- — Shape implementations
- — Scale types
- — Coordinate systems
- — Axis, Tooltip, Crosshair, Annotations
- — Selection, gestures
- — Variable and transforms
- — Varset algebra
- — Shared types (Label, PaintStyle, Defaults)