Creating Animated Circular Charts in Flutter: A Step-by-Step Guide

Feb 26, 2022 | Programming

In the world of data visualization, charts play a vital role in helping us understand and analyze information. Flutter Circular Chart is a library designed to create stunning animated pie and radial charts effortlessly. Whether you’re aiming to enhance your app with beautiful analytics or just want to show some enticing data visuals, this library has got you covered!

Overview

This library allows you to create animated circular charts by simply providing data objects for plotting. Inspired by the article Zero to One with Flutter, it offers easy customization and vibrant animations. Below are some examples of what you can achieve:

  • Animated Random Radial Chart
  • Animated Pie Chart
  • Animated Radial Chart with Labels

Let’s dive into how you can get started with it!

Installation

To get started, install the latest version of the Flutter Circular Chart by visiting pub.dev.

Getting Started

Here’s a simple guide to help you kick off your circular charts:

 // Import the package
import 'package:flutter_circular_chart/flutter_circular_chart.dart';

// Create a GlobalKey to manage the chart state
final GlobalKey _chartKey = new GlobalKey();

// Prepare your chart data
List data = CircularStackEntry(
    [
        new CircularSegmentEntry(500.0, Colors.red[200], rankKey: 'Q1'),
        new CircularSegmentEntry(1000.0, Colors.green[200], rankKey: 'Q2'),
        new CircularSegmentEntry(2000.0, Colors.blue[200], rankKey: 'Q3'),
        new CircularSegmentEntry(1000.0, Colors.yellow[200], rankKey: 'Q4'),
    ],
    rankKey: 'Quarterly Profits',
);

// Create an AnimatedCircularChart
@override
Widget build(BuildContext context) {
    return new AnimatedCircularChart(
        key: _chartKey,
        size: const Size(300.0, 300.0),
        initialChartData: data,
        chartType: CircularChartType.Pie,
    );
}

// Function to update and animate the chart
void _cycleSamples() {
    List nextData = CircularStackEntry(
        [
            new CircularSegmentEntry(1500.0, Colors.red[200], rankKey: 'Q1'),
            new CircularSegmentEntry(750.0, Colors.green[200], rankKey: 'Q2'),
            new CircularSegmentEntry(2000.0, Colors.blue[200], rankKey: 'Q3'),
            new CircularSegmentEntry(1000.0, Colors.yellow[200], rankKey: 'Q4'),
        ],
        rankKey: 'Quarterly Profits',
    );
    setState(() {
        _chartKey.currentState.updateData(nextData);
    });
} 

Understanding the Code: A Delicious Pie Analogy

Imagine creating a mouthwatering pie where each segment represents a portion of data. The code above is like layering flavors into that pie!

  • The GlobalKey is like your pie dish, holding everything together and allowing access to edit what’s inside.
  • The CircularStackEntry is the base layer of your pie, defining each segment or flavor – think of it as the filling of the pie.
  • Setting up the AnimatedCircularChart is akin to placing your beautifully crafted pie into the oven, ready to serve and impress with wonderful animations once it’s done.
  • Finally, the _cycleSamples function serves as the chef, bringing new delightful flavors at each update, making your pie an exciting dish to relish.

Customization

The library allows you to personalize your charts with various properties:

Hole Label

Enhance your chart’s presentation with a hole label like so:

 new AnimatedCircularChart(
    key: _chartKey,
    size: _chartSize,
    initialChartData: CircularStackEntry(
        [
            new CircularSegmentEntry(33.33, Colors.blue[400], rankKey: 'completed'),
            new CircularSegmentEntry(66.67, Colors.blueGrey[600], rankKey: 'remaining'),
        ],
        rankKey: 'progress',
    ),
    chartType: CircularChartType.Radial,
    percentageValues: true,
    holeLabel: '13',
    labelStyle: new TextStyle(
        color: Colors.blueGrey[600],
        fontWeight: FontWeight.bold,
        fontSize: 24.0,
    ),
); 

Segment Edge Style

This property allows you to pick how the edges of your segments appear:

 new AnimatedCircularChart(
    key: _chartKey,
    size: _chartSize,
    initialChartData: CircularStackEntry(
        [
            new CircularSegmentEntry(33.33, Colors.blue[400], rankKey: 'completed'),
            new CircularSegmentEntry(66.67, Colors.blueGrey[600], rankKey: 'remaining'),
        ],
        rankKey: 'progress',
    ),
    chartType: CircularChartType.Radial,
    edgeStyle: SegmentEdgeStyle.round,
    percentageValues: true,
); 

Troubleshooting

If you encounter issues while working with the Flutter Circular Chart, here are a few troubleshooting ideas:

  • Ensure that the library is correctly installed. Check your pubspec.yaml file.
  • Check if your keys and states are defined properly to avoid runtime errors.
  • Experiment with different chart types and data values to see which best fits your needs.
  • Lastly, consult the examples folder included in the library for reference.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Armed with this knowledge, you’re well on your way to incorporating animated circular charts into your Flutter applications. Visualization is key in making data understandable and engaging, and with the Flutter Circular Chart library, it has never been easier!

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox