fl_chart Documentation

repository·main·Indexed 27 days ago

https://github.com/imanneo/fl_chart

A highly customizable Flutter library for creating interactive charts, including Line, Bar, Pie, Scatter, Radar, and Candlestick charts. It provides comprehensive tools for data visualization, such as the BarChart widget and BarChartData configuration, as well as a robust touch event system (FlTouchEvent) for handling taps, pans, long presses, and pointer hover events.

Tokens
14.8K
Snippets
29
Records
99
Agent score
89%

What's inside fl_chart

  1. Overview of supported chart types

    main

    FL Chart is a highly customizable Flutter library that provides several types of charts:

    • LineChart: For displaying data points connected by straight line segments.
    • BarChart: For displaying categorical data with rectangular bars.
    • PieChart: For showing proportions of a whole.
    • ScatterChart: For displaying values for two variables on an X and Y axis.
    • RadarChart: For multivariate data visualization.
    • CandlestickChart: For financial data visualization.
  2. Explore supported chart types in FlChart

    main

    FlChart provides several specialized chart components for Flutter. You can implement the following chart types in your application:

    • LineChart: For displaying data points connected by lines.
    • BarChart: For displaying categorical data with rectangular bars.
    • PieChart: For showing proportions of a whole.
    • ScatterChart: For displaying individual data points on a Cartesian plane.
    • RadarChart: For multivariate data visualization.
    • CandlestickChart: For financial price movement data.
    • GaugeChart: For displaying single values within a range.
  3. Configure chart animations using duration and curve

    main

    Animations in fl_chart are handled implicitly using the ImplicitlyAnimatedWidget pattern. To customize the animation behavior, use the duration and curve properties on the chart widget. Changing data values will automatically trigger an animation between the old and new states.

    LineChart(
      duration: Duration(milliseconds: 150),
      curve: Curves.linear,
      LineChartData(
        isShowingMainData ? sampleData1() : sampleData2(),
      ),
    )
  4. Migrate tooltipBgColor to getTooltipColor in Bar, Line, and Scatter Charts

    main

    In version 0.67.0, the static Color tooltipBgColor property was replaced with a callback Color Function(spot) getTooltipColor. This change allows for customizing the tooltip background color dynamically based on the specific data point (spot) being touched. This applies to BarChartData, LineChartData, and ScatterChartData.

    // For BarChartData
    BarChartData(
      barTouchData: BarTouchData(
        touchTooltipData: BarTouchTooltipData(
          getTooltipColor: (BarChartGroupData group) => Colors.blueGrey,
        ),
      ),
    )
    
    // For LineChartData
    LineChartData(
      lineTouchData: LineTouchData(
        touchTooltipData: LineTouchTooltipData(
          getTooltipColor: (LineBarSpot touchedSpot) => Colors.blueGrey,
        ),
      ),
    )
    
    // For ScatterChartData
    ScatterChartData(
      scatterTouchData: ScatterTouchData(
        touchTooltipData: ScatterTouchTooltipData(
          getTooltipColor: (ScatterSpot touchedBarSpot) => Colors.blueGrey,
        ),
      ),
    )
  5. Customize iOS Launch Screen Assets

    main

    To customize the iOS launch screen, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    To use Xcode:

    1. Open the iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, select Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog.
    open ios/Runner.xcworkspace
  6. Control transformations programmatically with TransformationController

    main

    For advanced control, such as implementing custom zoom/pan buttons or resetting the chart state, provide a TransformationController within your FlTransformationConfig.

    Warning: When using a custom TransformationController, the library does not prevent the chart from moving out of the visible screen area. You are responsible for implementing logic to keep the chart within bounds and within transformation limits.

    class ChartWithControls extends StatefulWidget {
      @override
      State<ChartWithControls> createState() => _ChartWithControlsState();
    }
    
    class _ChartWithControlsState extends State<ChartWithControls> {
      late TransformationController _controller;
    
      @override
      void initState() {
        super.initState();
        _controller = TransformationController();
      }
    
      @override
      void dispose() {
        _controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            AspectRatio(
              aspectRatio: 1.4,
              child: LineChart(
                LineChartData(...),
                transformationConfig: FlTransformationConfig(
                  scaleAxis: FlScaleAxis.horizontal,
                  minScale: 1.0,
                  maxScale: 25.0,
                  transformationController: _controller,
                ),
              ),
            ),
            Row(
              children: [
                IconButton(
                  icon: Icon(Icons.zoom_in),
                  onPressed: () {
                    _controller.value *= Matrix4.diagonal3Values(1.1, 1.1, 1);
                  },
                ),
                IconButton(
                  icon: Icon(Icons.zoom_out),
                  onPressed: () {
                    _controller.value *= Matrix4.diagonal3Values(0.9, 0.9, 1);
                  },
                ),
                IconButton(
                  icon: Icon(Icons.refresh),
                  onPressed: () {
                    _controller.value = Matrix4.identity();
                  },
                ),
              ],
            ),
          ],
        );
      }
    }
  7. Use the PieChart widget

    main

    To implement a pie chart, use the PieChart widget and provide a PieChartData object. You can optionally configure implicit animations using duration and curve properties.

    Important Note: If you wrap the PieChart in a Padding widget, you must set PieChartData.centerSpaceRadius to double.infinity to ensure the chart is calculated according to the view size.

    PieChart(
      PieChartData(
        // configuration goes here
      ),
      duration: Duration(milliseconds: 150), // Optional
      curve: Curves.linear, // Optional
    );
  8. Configure Solid Colors and Gradients

    main

    Starting from version 0.50.0, the way solid colors and gradients are applied has changed. Instead of using a list of colors and various offset/stop properties, you now use explicit color or gradient properties. This change affects several classes including BarChartRodData, BackgroundBarChartRodData, BarAreaData, BetweenBarsData, and LineChartBarData.

    • For a solid color: Use the color property with a single Color object.
    • For a gradient: Use the gradient property with a Flutter Gradient object (e.g., LinearGradient, RadialGradient).

    You must provide exactly one of these properties.

    // Solid Color
    LineChartBarData(
      color: Colors.red
    )
    
    // Linear Gradient
    LineChartBarData(
      gradient: LinearGradient(
        colors: [Colors.green, Colors.blue],
        begin: Alignment.centerLeft,
        end: Alignment.centerRight,
      )
    )
    
    // Gradient with Stops
    LineChartBarData(
      gradient: LinearGradient(
        colors: [Colors.green, Colors.blue],
        stops: [0.1, 0.10],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      )
    )
  9. Enable built-in touch tooltips in Line and Bar Charts

    main

    To show built-in tooltips on touched spots in LineChart or BarChart, set handleBuiltInTouches to true (default behavior) and configure the touchTooltipData within the respective chart's touch data object. This allows you to customize the appearance of the tooltip, such as its color.

    LineChart(
      LineChartData(
        lineTouchData: LineTouchData(
          touchTooltipData: TouchTooltipData (
            getTooltipColor: (touchedSpot) => Colors.blueGrey.withOpacity(0.8),
             .
             .
             .
          )
        )
      )
    )