Flutter图表绘制插件fastyle_charts的使用

Flutter图表绘制插件fastyle_charts的使用

fastyle_charts 是一套用于 fastyle 库的图表小部件集合。通过该库,开发者可以轻松地在Flutter应用中绘制各种类型的图表。

完整示例代码

以下是一个完整的示例代码,展示了如何在Flutter应用中使用 fastyle_charts 绘制柱状图、饼图和甜甜圈图。

// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:fastyle_charts/fastyle_charts.dart';
import 'package:fastyle_core/fastyle_core.dart';
import 'package:go_router/go_router.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    // 定义数据列表
    final data = [
      FastChartData(label: 'Item 1', value: 80),
      FastChartData(label: 'Item 2', value: -20),
      FastChartData(label: 'Item 3', value: 40),
      FastChartData(label: 'Item 4', value: -60),
    ];

    // 定义饼图数据
    final pieData = [
      FastChartData(value: 0.12, color: Colors.blue.shade100, label: 'Blue'),
      FastChartData(value: 0.12, color: Colors.teal, label: 'Teal'),
      FastChartData(value: 0.15, color: Colors.mint, label: 'Mint'),
      FastChartData(value: 0.05, color: Colors.indigo, label: 'Indigo'),
      FastChartData(value: 0.30, color: Colors.purple, label: 'Purple'),
      FastChartData(value: 0.26, color: Colors.blueGrey, label: 'Blue Gray'),
    ];

    return FastApp(
      lightTheme: FastTheme.light.blue,
      darkTheme: FastTheme.dark.blue,
      routesForMediaType: (mediaType) => [
        GoRoute(
          path: '/',
          builder: (_, __) {
            return FastSectionPage(
              isViewScrollable: true,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const FastTitle(text: 'Bar Chart'),
                  const SizedBox(height: 24), // kFastVerticalSizedBox24
                  FractionallySizedBox(
                    widthFactor: 0.35,
                    alignment: FractionalOffset.center,
                    child: BarChart(data: data),
                  ),
                  const SizedBox(height: 24), // kFastVerticalSizedBox24
                  const FastTitle(text: 'Pie Chart'),
                  const SizedBox(height: 24), // kFastVerticalSizedBox24
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      FastPieChart(data: pieData, animate: true),
                      const SizedBox(width: 48), // kFastHorizontalSizedBox48
                      FastChartLegend(data: pieData),
                    ],
                  ),
                  const SizedBox(height: 24), // kFastVerticalSizedBox24
                  const FastTitle(text: 'Doughnut Chart'),
                  const SizedBox(height: 24), // kFastVerticalSizedBox24
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      FastDoughnutChart(data: pieData, animate: true),
                      const SizedBox(width: 48), // kFastHorizontalSizedBox48
                      FastChartLegend(data: pieData),
                    ],
                  ),
                ],
              ),
            );
          },
        ),
      ],
    );
  }
}

代码解释

  • 导入包

    import 'package:fastyle_charts/fastyle_charts.dart';
    import 'package:fastyle_core/fastyle_core.dart';
    import 'package:go_router/go_router.dart';
    

    导入 fastyle_charts 和其他必需的包。

  • 定义数据

    final data = [
      FastChartData(label: 'Item 1', value: 80),
      FastChartData(label: 'Item 2', value: -20),
      FastChartData(label: 'Item 3', value: 40),
      FastChartData(label: 'Item 4', value: -60),
    ];
    

    定义柱状图的数据。

  • 构建UI

    return FastApp(
      lightTheme: FastTheme.light.blue,
      darkTheme: FastTheme.dark.blue,
      routesForMediaType: (mediaType) => [
        GoRoute(
          path: '/',
          builder: (_, __) {
            return FastSectionPage(
              isViewScrollable: true,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const FastTitle(text: 'Bar Chart'),
                  const SizedBox(height: 24),
                  FractionallySizedBox(
                    widthFactor: 0.35,
                    alignment: FractionalOffset.center,
                    child: BarChart(data: data),
                  ),
                  const SizedBox(height: 24),
                  const FastTitle(text: 'Pie Chart'),
                  const SizedBox(height: 24),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      FastPieChart(data: pieData, animate: true),
                      const SizedBox(width: 48),
                      FastChartLegend(data: pieData),
                    ],
                  ),
                  const SizedBox(height: 24),
                  const FastTitle(text: 'Doughnut Chart'),
                  const SizedBox(height: 24),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      FastDoughnutChart(data: pieData, animate: true),
                      const SizedBox(width: 48),
                      FastChartLegend(data: pieData),
                    ],
                  ),
                ],
              ),
            );
          },
        ),
      ],
    );
    

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

1 回复

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


fastyle_charts 是一个用于 Flutter 的轻量级图表绘制插件,旨在帮助开发者快速构建各种类型的图表。它支持多种常见的图表类型,如折线图、柱状图、饼图等,并且具有高度的可定制性。

以下是如何使用 fastyle_charts 插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 fastyle_charts 的依赖:

dependencies:
  flutter:
    sdk: flutter
  fastyle_charts: ^0.0.1 # 请使用最新版本

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

2. 导入包

在你的 Dart 文件中导入 fastyle_charts

import 'package:fastyle_charts/fastyle_charts.dart';

3. 使用图表组件

fastyle_charts 提供了多种图表组件,你可以根据需要选择合适的组件并使用。

折线图(Line Chart)

class LineChartExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return FastLineChart(
      data: [
        FastLineDataPoint(x: 0, y: 0),
        FastLineDataPoint(x: 1, y: 2),
        FastLineDataPoint(x: 2, y: 1),
        FastLineDataPoint(x: 3, y: 3),
        FastLineDataPoint(x: 4, y: 4),
      ],
      lineColor: Colors.blue,
      lineWidth: 2.0,
    );
  }
}

柱状图(Bar Chart)

class BarChartExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return FastBarChart(
      data: [
        FastBarDataPoint(label: 'A', value: 10),
        FastBarDataPoint(label: 'B', value: 20),
        FastBarDataPoint(label: 'C', value: 30),
        FastBarDataPoint(label: 'D', value: 40),
      ],
      barColor: Colors.green,
    );
  }
}

饼图(Pie Chart)

class PieChartExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return FastPieChart(
      data: [
        FastPieDataPoint(label: 'A', value: 30),
        FastPieDataPoint(label: 'B', value: 20),
        FastPieDataPoint(label: 'C', value: 50),
      ],
      colors: [Colors.red, Colors.green, Colors.blue],
    );
  }
}

4. 自定义图表

fastyle_charts 提供了多种属性来自定义图表的外观和行为。你可以根据需求调整颜色、线宽、标签等。

5. 处理交互

某些图表类型支持交互功能,如点击事件。你可以通过设置回调函数来处理用户的交互。

class InteractivePieChartExample extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return FastPieChart(
      data: [
        FastPieDataPoint(label: 'A', value: 30),
        FastPieDataPoint(label: 'B', value: 20),
        FastPieDataPoint(label: 'C', value: 50),
      ],
      colors: [Colors.red, Colors.green, Colors.blue],
      onTap: (dataPoint) {
        print('Tapped on ${dataPoint.label}');
      },
    );
  }
}
回到顶部