精华内容
下载资源
问答
  • flutter饼图

    2021-02-26 15:42:01
    import 'package:charts_flutter/flutter.dart' as charts; void main()=>runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { .

    在这里插入图片描述

    使用插件快多了,
    代码:

    import 'dart:math';
    import 'package:flutter/material.dart';
    import 'package:charts_flutter/flutter.dart' as charts;
    
    void main()=>runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowMaterialGrid: false,
          home:piePage()
        );
      }
    }
    
    class piePage extends StatelessWidget {
      const piePage({Key key}) : super(key: key);
    
    
      Widget _Pie() {
        var random = Random();
    
        var data = [
        PieSales(0, random.nextInt(30)),
        PieSales(1, random.nextInt(20)),
        PieSales(2, random.nextInt(10)),
        ];
    
        var seriesList = [
          charts.Series<PieSales, int>(
            id: 'Sales',
            domainFn: (PieSales sales, _) => sales.year,
            measureFn: (PieSales sales, _) => sales.sales,
            data: data,
          )
        ];
    
        return charts.PieChart(seriesList, animate: true);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("饼图")),
          body: Column(children: [
            Container(height: 300, child: _Pie()),
          ]),
        );
      }
    }
    
    class PieSales {
      final int year;
      final int sales;
    
      PieSales(this.year, this.sales);
    }
    
    
    展开全文
  • flutter饼图demo.rar

    2020-04-16 16:37:46
    flutter制作的饼图,写了一些方法让扇形居中
  • flutter_echart 是基于Flutter Canvas 绘图思想实现的图表功能,支持饼状图、柱状图、拆线图,在2020-12-02 更新1.0.1版本

    在码农的世界里,优美的应用体验,来源于程序员对细节的处理以及自我要求的境界,年轻人也是忙忙碌碌的码农中一员,每天、每周,都会留下一些脚印,就是这些创作的内容,有一种执着,就是不知为什么,如果你迷茫,不妨来瞅瞅码农的轨迹。


    1 简介

    flutter_echart 是基于Flutter Canvas 绘图思想实现的图表功能,支持饼状图、柱状图、拆线图,在2020-12-02 更新1.0.1版本,将动态饼状图发布到 pub 仓库,地址如下:

    https://pub.flutter-io.cn/packages/flutter_echart
    

    大家可以直接在 Flutter 项目中添加依赖如下:

    dependencies:
      flutter_echart: ^1.0.0
    

    然后加载一下依赖

    flutter pub get
    

    然后在使用的地方导包如下:

    import 'package:flutter_echart/flutter_echart.dart';
    

    1 饼状图

    首先需要有饼图数据,flutter_echart提供了EChartPieBean对象来封装数据。

    1.1 EChartPieBean的定义
    class EChartPieBean {
      //用户自定义的数据
      dynamic id;
      //直线上显示的文案
      String title;
      //当前数值占用的比例
      int number;
      //当前饼状的颜色
      Color color;
      //内部使用
      bool isClick;
    
      EChartPieBean(
          {this.id,
          this.title = '',
          this.number = 100,
          this.color = Colors.blue,
          this.isClick = false});
    }
    
    
    1.2 饼图基本使用

    首先定义数据

      List<EChartPieBean> _dataList = [
        EChartPieBean(title: "生活费", number: 200, color: Colors.lightBlueAccent),
        EChartPieBean(title: "游玩费", number: 200, color: Colors.deepOrangeAccent),
        EChartPieBean(title: "交通费", number: 400, color: Colors.green),
        EChartPieBean(title: "贷款费", number: 300, color: Colors.amber),
        EChartPieBean(title: "电话费", number: 200, color: Colors.orange),
      ];
    

    如下图所示,以动画的方式显示:
    在这里插入图片描述
    对应代码如下:

      PieChatWidget buildPieChatWidget() {
        return PieChatWidget(
          dataList: _dataList,
          //是否输出日志
          isLog: true,
          //是否需要背景
          isBackground: true,
          //是否画直线
          isLineText: true,
          //背景
          bgColor: Colors.white,
          //是否显示最前面的内容
          isFrontgText: true,
          //默认选择放大的块
          initSelect: 1,
          //初次显示以动画方式展开
          openType: OpenType.ANI,
          //旋转类型
          loopType: LoopType.DOWN_LOOP,
          //点击回调
          clickCallBack: (int value) {
            print("当前点击显示 $value");
          },
        );
      }
    

    initSelect 属性为默认选择放大显示的饼块,当此值为 -2 时,不会有放大效果

    当 isBackground 属性为 false时,效果如下:

    在这里插入图片描述
    当 isLineText 为false 时 :

    在这里插入图片描述
    当 isFrontgText 为false 时:

    在这里插入图片描述

    1.3 饼图交互

    使用 属性 loopType 来配置:

    //饼图交互
    enum LoopType {
      //无交互
      NON,
      //按下时放大
      DOWN,
      //按下移动放大
      MOVE,
      //按下旋转
      DOWN_LOOP,
      //自动旋转
      AUTO_LOOP,
    }
    
    

    取值为 LoopType.NON 无任何交互
    在这里插入图片描述
    取值为 LoopType.DOWN 点击放大,如下:
    在这里插入图片描述
    取值为 LoopType.MOVE 手指在饼图触点范围内就会放大,如下:
    在这里插入图片描述
    取值为 LoopType.DOWN_LOOP 手指按下可旋转,如下:
    在这里插入图片描述

    取值为 LoopType.AUTO_LOOP 自动循环旋转,手指按下停止旋转:
    在这里插入图片描述

    不局限于思维,不局限语言限制,才是编程的最高境界。

    以小编的性格,肯定是要录制一套视频的,随后会上传

    有兴趣 你可以关注一下 西瓜视频 — 早起的年轻人

    在这里插入图片描述

    展开全文
  • flutter绘制自定义组件,flutter绘制自定义组件,flutter绘制自定义组件flutter绘制自定义组件
  • 本博主用flutter纯手工打造饼图,canvas绘制,好累,记得收藏学习哦,我的小粉丝们~ 老规矩,看效果 上代码: import 'dart:math'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material....

    好累,不用插件真的是费神啊

    本博主用flutter纯手工打造饼图,canvas绘制,好累,记得收藏学习哦,我的小粉丝们~

    老规矩,看效果
    在这里插入图片描述
    上代码:

    import 'dart:math';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: piepage(),
        );
      }
    }
    
    class piepage extends StatefulWidget {
      @override
      _piepageState createState() => _piepageState();
    }
    
    Color mainColor = Color(0xFFCADCED);
    
    class _piepageState extends State<piepage> with SingleTickerProviderStateMixin {
      AnimationController _animationController;
    
      Animation<double> _bgAnimation;
    
      Animation<double> _progressAnimation;
    
      Animation<double> _numberAnimation;
    
      @override
      void initState() {
        super.initState();
    
        _animationController = new AnimationController(
            duration: Duration(milliseconds: 1000), vsync: this);
    
        _bgAnimation = Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
          parent: _animationController,
          curve: Interval(0.0, 0.5),
        ));
    
        _progressAnimation = Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
          parent: _animationController,
          curve: Interval(0.4, 0.8),
        ));
    
        _numberAnimation = Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
          parent: _animationController,
          curve: Interval(0.7, 1.0),
        ));
    
        _animationController.addListener(() {
          setState(() {});
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Container(
              height: 260,
              width: MediaQuery.of(context).size.width,
              //背景
              color: mainColor,
              //封装方法
              child: buildRow(),
            ),
          ),
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              _animationController.reset();
              _animationController.forward();
            },
          ),
        );
      }
    
      //定义数据模型
      List _list = [
        {"title": "生活费", "number": 200, "color": Colors.lightBlueAccent},
        {"title": "伙食费", "number": 200, "color": Colors.deepOrangeAccent},
        {"title": "交通费", "number": 400, "color": Colors.green},
        {"title": "网购", "number": 300, "color": Colors.red},
        {"title": "电话费", "number": 200, "color": Colors.orange}
      ];
    
      buildRow() {
        return Row(
          children: [
            Expanded(
              flex: 5,
              child: buildLeftColumn(), //左边
            ),
            //饼图区域
            Expanded(
                flex: 6,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    //第一层
                    Container(
                      padding: EdgeInsets.all(22),
                      decoration: BoxDecoration(
                          color: mainColor,
                          shape: BoxShape.circle,
                          boxShadow: [
                            BoxShadow(
                              color: Colors.white,
                              spreadRadius: -8 * _bgAnimation.value,
                              offset: Offset(
                                  -5 * _bgAnimation.value, -5 * _bgAnimation.value),
                              //阴影偏移量
                              blurRadius: 30 * _bgAnimation.value,
                            ),
                            BoxShadow(
                              color: Colors.blue[300].withOpacity(0.3),
                              spreadRadius: 2 * _bgAnimation.value,
                              offset: Offset(
                                  5 * _bgAnimation.value, 5 * _bgAnimation.value),
                              //阴影偏移量
                              blurRadius: 20 * _bgAnimation.value,
                            )
                          ]),
    
                      //开始绘制
                      child: CustomPaint(
                        size: Size(200, 200),
                        painter:
                            CustomShapePainter(_list, _progressAnimation.value),
                      ),
                    ),
                  ],
                ))
          ],
        );
      }
    
    //左边
      Column buildLeftColumn() {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: _list.map(
            (data) {
              return Container(
                  padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                  child: Row(
                    children: [
                      Container(
                        margin: EdgeInsets.only(right: 10),
                        width: 10,
                        height: 10,
                        decoration: BoxDecoration(
                          color: data['color'],
                          shape: BoxShape.circle,
                        ),
                      ),
                      Text(
                        data['title'],
                        style: TextStyle(fontSize: 16),
                      )
                    ],
                  ));
            },
          ).toList(),
        );
      }
    }
    
    class CustomShapePainter extends CustomPainter {
      List list;
    
      double progress;
    
      CustomShapePainter(this.list, this.progress);
    
      Paint _paint = new Paint()..isAntiAlias = true;
    
      @override
      void paint(Canvas canvas, Size size) {
        //中心
        Offset center = Offset(size.width / 2, size.height / 2);
        //半径
        double radius = min(size.width / 2, size.height / 2);
        //弧度
        double starRadian = -pi / 2;
    
        double total = 0.0;
    
        list.forEach((element) {
          total += element['number'];
        });
    
        for (var i = 0; i < list.length; i++) {
          var item = list[i];
          double flag = item['number'] / total;
    
          double sweepRadin = flag * 2 * pi * progress;
    
          _paint.color = item['color'];
    
          canvas.drawArc(Rect.fromCircle(center: center, radius: radius),
              starRadian, sweepRadin, true, _paint);
    
          starRadian += sweepRadin;
        }
      }
    
      @override
      bool shouldRepaint(covariant CustomPainter oldDelegate) {
        return true;
      }
    }
    
    展开全文
  • flutter_circular_chart:Flutter的动画径向图和饼图
  • flutter 数据可视化——饼图

    千次阅读 2018-12-26 11:48:00
    import 'package:charts_flutter/flutter.dart' as charts; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new ...

    3、代码如下:

    import 'package:flutter/material.dart';
    import 'package:charts_flutter/flutter.dart' as charts;
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class ClicksPerYear {
      final String year;
      final int clicks;
      final charts.Color color;
    
      ClicksPerYear(this.year, this.clicks, Color color)
          : this.color = new charts.Color(
          r: color.red, g: color.green, b: color.blue, a: color.alpha);
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        var data = [
          new ClicksPerYear('2016', 12, Colors.red),
          new ClicksPerYear('2017', 42, Colors.yellow),
          new ClicksPerYear('2018', _counter, Colors.green),
        ];
    
        var series = [
          new charts.Series(
            domainFn: (ClicksPerYear clickData, _) => clickData.year,
            measureFn: (ClicksPerYear clickData, _) => clickData.clicks,
            colorFn: (ClicksPerYear clickData, _) => clickData.color,
            id: 'Clicks',
            data: data,
          ),
        ];
        var chart = new charts.BarChart(
          series,
          animate: true,
        );
        var chartWidget = new Padding(
          padding: new EdgeInsets.all(32.0),
          child: new SizedBox(
            height: 200.0,
            child: chart,
          ),
        );
    
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(widget.title),
          ),
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text(
                  'You have pushed the button this many times:',
                ),
                new Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.display1,
                ),
                chartWidget,
              ],
            ),
          ),
          floatingActionButton: new FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: new Icon(Icons.add),
          ),
        );
      }
    }

     

    转载于:https://my.oschina.net/zhizhisoft/blog/2993418

    展开全文
  • FL Chart 一个强大的Flutter图表库,支持折线图、条形图和饼图
  • fl_chart:功能强大的Flutter图表库,当前支持折线图,条形图,饼图和散点图
  • Flutter实现漂亮的饼图1.导入依赖2.饼图1.效果图2.所有代码3.代码解析1.代码结构3.组件作用 1.导入依赖 fl_chart: ^0.12.2 如果你想进一步了解这个插件的话,可以去看一下它的源码。框架地址 关于如何导入依赖步骤...
  • Flutter —— 二:如何绘制图表charts_flutter

    千次阅读 热门讨论 2020-04-03 00:51:03
    注:笔者的Flutter是以AndroidStudio为开发工具编写的 最近做了个项目需要有折线图和饼图 因为能查找到的资料很少 实现的过程比较艰难 所以记录下来(先开个头,持续更新中) ...
  • 前言接上文,本文讲解如何使用 Flutter 绘制饼状图,最终效果如图在线查看定义 PieChart & PiePart第一步定义 PieChart 和 PiePart 类。PieChart 是整个饼状图控件,有 datas 和 legends 两个属性,表示饼图的...

空空如也

空空如也

1 2 3
收藏数 41
精华内容 16
关键字:

flutter饼图