Flutter图表绘制插件fcharts的使用

发布于 1周前 作者 zlyuanteng 来自 Flutter

Flutter图表绘制插件fcharts的使用

fcharts

Build Status
Pub Status

fcharts 是一个为 Flutter 开发的图表库,目前仍处于开发阶段。在版本达到 1.0.0 之前,API 可能会发生重大变化。因此,fcharts 当前并不适合用于生产环境。

该项目的目标是通过简单直观的 API 创建美观且响应式的图表。

该库受 Mikkel Ravn 在 Flutter 小部件和动画教程的启发,并且如果您熟悉 ReactJS 的 Recharts 库,那么您可能会发现它的高级 API 非常熟悉。

Demo

Bar chart demo
Touch demo

示例用法

以下是一个简单的折线图示例:

class SimpleLineChart extends StatelessWidget {
  // X 值 -> Y 值
  static const myData = [
    ["A", "✔"],
    ["B", "❓"],
    ["C", "✖"],
    ["D", "❓"],
    ["E", "✖"],
    ["F", "✖"],
    ["G", "✔"],
  ];
  
  [@override](/user/override)
  Widget build(BuildContext context) {
    return new LineChart(
      lines: [
        new Line<List<String>, String, String>(
          data: myData,
          xFn: (datum) => datum[0], // X 轴数据提取函数
          yFn: (datum) => datum[1], // Y 轴数据提取函数
        ),
      ],
      chartPadding: new EdgeInsets.fromLTRB(30.0, 10.0, 10.0, 30.0), // 图表边距
    );
  }
}

上述代码创建了以下折线图:

line chart

完整示例 Demo

以下是完整的示例代码,展示了如何使用 fcharts 绘制不同类型的图表:

import 'bar/simple.dart';
import 'line/cities.dart';
import 'line/simple.dart';
import 'line/updating.dart';
import 'package:flutter/material.dart';

void main() => runApp(new FChartsExampleApp());

class ChartExample {
  ChartExample(this.name, this.widget, this.description);

  final String name;
  final Widget widget;
  final String description;
}

// 定义不同类型的图表示例
final charts = [
  new ChartExample(
    'Simple Line Chart',
    new SimpleLineChart(),
    'Strings on the X-Axis and their index in the list on the Y-Axis.',
  ),
  new ChartExample(
    'City Coolness & Size Line Chart',
    new CityLineChart(),
    'Cities on the X-Axis with coolness & size on the Y-Axis with painted lines.',
  ),
  new ChartExample(
    'Updating Sparkline Chart',
    new UpdatingChart(),
    'Just a list of doubles that updates periodically.',
  ),
  new ChartExample(
    'Simple Bar Chart',
    new SimpleBarChart(),
    'Bar charts are not quite ready yet.',
  ),
];

// 主应用类
class FChartsExampleApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<FChartsExampleApp> {
  var _chartIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    final chart = charts[_chartIndex % charts.length];

    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Example: ${chart.name}'),
        ),
        body: new Container(
          decoration: new BoxDecoration(
            color: Colors.white,
          ),
          child: new Column(
            children: [
              new Padding(
                padding: new EdgeInsets.all(30.0),
                child: new Text(
                  chart.description,
                  textAlign: TextAlign.center,
                ),
              ),
              new Padding(
                  padding: new EdgeInsets.all(20.0),
                  child: new Container(
                    child: chart.widget,
                  )),
            ],
          ),
        ),
        floatingActionButton: new FloatingActionButton(
          onPressed: () {
            setState(() => _chartIndex++);
          },
          child: new Icon(Icons.refresh),
        ),
      ),
    );
  }
}

更多关于Flutter图表绘制插件fcharts的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter图表绘制插件fcharts的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


fcharts 是一个用于在 Flutter 中绘制图表的插件,它提供了简单易用的 API 来创建各种类型的图表,如折线图、柱状图、饼图等。虽然 fcharts 并不是 Flutter 官方推荐的图表库(官方推荐的是 charts_flutter),但它仍然是一个轻量级且易于使用的选择。

以下是如何在 Flutter 项目中使用 fcharts 插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 fcharts 依赖:

dependencies:
  flutter:
    sdk: flutter
  fcharts: ^0.1.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入库

在需要使用图表的 Dart 文件中导入 fcharts

import 'package:fcharts/fcharts.dart';

3. 创建图表

fcharts 提供了多种图表类型,以下是一些常见图表的示例:

折线图 (Line Chart)

import 'package:flutter/material.dart';
import 'package:fcharts/fcharts.dart';

class LineChartExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Line Chart Example'),
      ),
      body: Center(
        child: LineChart(
          lines: [
            Line<num, num>(
              data: [
                Point(1, 2),
                Point(2, 3),
                Point(3, 5),
                Point(4, 1),
                Point(5, 4),
              ],
              stroke: Paint()..color = Colors.blue,
            ),
          ],
          chartPadding: EdgeInsets.all(20.0),
        ),
      ),
    );
  }
}

柱状图 (Bar Chart)

import 'package:flutter/material.dart';
import 'package:fcharts/fcharts.dart';

class BarChartExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bar Chart Example'),
      ),
      body: Center(
        child: BarChart(
          bars: [
            Bar<num, num>(
              data: [
                Point(1, 2),
                Point(2, 3),
                Point(3, 5),
                Point(4, 1),
                Point(5, 4),
              ],
              fill: Paint()..color = Colors.green,
            ),
          ],
          chartPadding: EdgeInsets.all(20.0),
        ),
      ),
    );
  }
}

饼图 (Pie Chart)

import 'package:flutter/material.dart';
import 'package:fcharts/fcharts.dart';

class PieChartExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Pie Chart Example'),
      ),
      body: Center(
        child: PieChart(
          slices: [
            PieSlice(
              value: 30,
              label: 'Slice 1',
              color: Colors.red,
            ),
            PieSlice(
              value: 20,
              label: 'Slice 2',
              color: Colors.blue,
            ),
            PieSlice(
              value: 50,
              label: 'Slice 3',
              color: Colors.green,
            ),
          ],
          chartPadding: EdgeInsets.all(20.0),
        ),
      ),
    );
  }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!