Flutter项目实现(空心,实心)饼状图

1、插件安装

依赖:charts_flutter: ^0.9.0
之后使用Pub get安装依赖

2、插件使用

在github上面的例子中,我们的饼图是在 StatelessWidget 里面写的,但是我们实际上经常使用的地方是有状态组件。
其他的就不说了,直接上案例代码哈,大家对此案例如果有什么疑问的话,可以在评论区留言,我们一起讨论哦



/// Donut chart with labels example. This is a simple pie chart with a hole in
/// the middle.
// EXCLUDE_FROM_GALLERY_DOCS_START
import 'dart:math';
// EXCLUDE_FROM_GALLERY_DOCS_END
import 'package:charts_flutter/flutter.dart' as charts;//引入依赖
import 'package:flutter/material.dart';
class PieSales {
  final int initstatus;
  final String statusInfo;
  final intsales;
  final color;

  PieSales(this.initstatus, this.intsales, this.color, this.statusInfo);
}

class DonutAutoLabelChart2 extends StatefulWidget {
  @override
  _DonutAutoLabelChart2State createState() => _DonutAutoLabelChart2State();
}


class _DonutAutoLabelChart2State extends State<DonutAutoLabelChart2> {
  final List<charts.Series> seriesList= _createRandomData();
  final bool animate=true;


  /// Creates a [PieChart] with sample data and no transition.


  /// Create random data.
  static List<charts.Series<PieSales, int>> _createRandomData() {

    final data = [
      new  PieSales(0,20,charts.ColorUtil.fromDartColor(Color(0xFF48BFFE)), "Zero",),
     PieSales(1,20,charts.ColorUtil.fromDartColor(Color(0xFF38D28D)),"One",),

     PieSales(2,20,charts.ColorUtil.fromDartColor(Color(0xFFF2CE28)), "Two", ),

     PieSales(3,20,charts.ColorUtil.fromDartColor(Color(0xFFFFA65B)), "Three",),

     PieSales(4,20, charts.ColorUtil.fromDartColor(Color(0xFFE5574D)), "Four",),

    PieSales(5, 20,charts.ColorUtil.fromDartColor(Color(0xFFDADADA)), "Five",),
      // new PieSales(3, random.nextInt(100)),
    ];//charts.ColorUtil.fromDartColor方法里面的颜色就是我们需要实现的饼状图的颜色。有多个 PieSales就会把整个圆分成几个部分,这里也是可以设置的。PieSales的第二个参数就是我们这个部分占圆的百分比

    return [
      new charts.Series<PieSales, int>(
        id: 'Sales',
        domainFn: (PieSales sales, _) => sales.initstatus,
        measureFn: (PieSales sales, _) => sales.intsales,
        colorFn: (PieSales sales, _) => sales.color,
        data: data,
        // Set a label accessor to control the text of the arc label.
        labelAccessorFn: (PieSales row, _) => row.statusInfo,
      )
    ];
  }
  // EXCLUDE_FROM_GALLERY_DOCS_END

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(),
      body: Container(
        width: 120,
        height: 120,
        child: charts.PieChart(
         seriesList,
         animate:true,
        defaultRenderer:new charts.ArcRendererConfig(
        arcWidth: 15,//这里是弧的宽度,当我们的宽高较小,而这个较大的时候,我们的饼状图就是实心的,否则就是空心的。可以自己设置
        arcRendererDecorators: [
        charts.ArcLabelDecorator(
        labelPosition: charts.ArcLabelPosition.inside,
        insideLabelStyleSpec:
           charts.TextStyleSpec(fontSize:8, color: charts.Color.white),
        )
        ]),
    )
      ),
    );
  }

 
}

下面是效果:
实心的效果:
在这里插入图片描述

空心的效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Flutter实现一个五边形表图,可以使用CustomPainter类来自定义绘制方法。下面是一个简单的示例代码: ```dart import 'package:flutter/material.dart'; import 'dart:math'; class PentagonChart extends StatelessWidget { final List<double> data; PentagonChart({required this.data}); @override Widget build(BuildContext context) { return CustomPaint( painter: _PentagonChartPainter(data), ); } } class _PentagonChartPainter extends CustomPainter { final List<double> data; _PentagonChartPainter(this.data); @override void paint(Canvas canvas, Size size) { final double radius = min(size.width, size.height) / 2; final double centerX = size.width / 2; final double centerY = size.height / 2; final double angle = 2 * pi / 5; final double rotation = -pi / 2; final List<Offset> points = []; // 计算五个顶点的坐标 for (int i = 0; i < 5; i++) { final double x = centerX + radius * cos(angle * i + rotation); final double y = centerY + radius * sin(angle * i + rotation); points.add(Offset(x, y)); } // 绘制多边形 final Paint polygonPaint = Paint() ..color = Colors.blue ..style = PaintingStyle.fill; final Path polygonPath = Path() ..moveTo(points[0].dx, points[0].dy); for (int i = 1; i < 5; i++) { polygonPath.lineTo(points[i].dx, points[i].dy); } polygonPath.close(); canvas.drawPath(polygonPath, polygonPaint); // 绘制数据 final Paint dataPaint = Paint() ..color = Colors.white ..style = PaintingStyle.stroke ..strokeWidth = 2; final Path dataPath = Path(); for (int i = 0; i < 5; i++) { final double x = centerX + radius * data[i] * cos(angle * i + rotation); final double y = centerY + radius * data[i] * sin(angle * i + rotation); if (i == 0) { dataPath.moveTo(x, y); } else { dataPath.lineTo(x, y); } } dataPath.close(); canvas.drawPath(dataPath, dataPaint); } @override bool shouldRepaint(CustomPainter oldDelegate) { return true; } } ``` 在这个示例中,我们定义了一个PentagonChart类来表示五边形表图,并使用CustomPainter类来自定义绘制方法。在paint()方法中,我们首先计算出五个顶点的坐标,然后使用Path类来绘制多边形和数据。最后,我们将PentagonChart类作为一个自定义的Widget来使用。 你可以通过以下代码来使用PentagonChart类: ```dart PentagonChart(data: [0.2, 0.5, 0.8, 0.4, 0.6]) ``` 这个示例代码中的data参数表示五个数据点的值,它们将被用来绘制五边形表图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值