Flutter实战 -- fl_chart(条形图)

1-引入依赖

fl_chart: ^0.35.0  # 折线图

2-条形图具体实现

条形图

代码:

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

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Container(
            height: 200,
            width:230,
            margin: EdgeInsets.only(top: 20),
            padding: const EdgeInsets.all(30),
            child: BarChart(
              BarChartData(
                alignment: BarChartAlignment.spaceAround,// 柱状图的对齐方式
                maxY:10,//Y轴的最大值
                // 点击可出现提示框
                barTouchData: BarTouchData(
                  enabled: true,
                  // 修改提示框的样式和展示文字
                  touchTooltipData: BarTouchTooltipData(
                     tooltipBgColor:const Color.fromRGBO(255, 237, 142, 1),// 设置弹出框的背景色
                     tooltipRoundedRadius: 8,//圆角边框  
                     tooltipPadding: const EdgeInsets.all(5), // 内边距
                     tooltipMargin: 2, // 外边距
                     maxContentWidth:100, // 提示框的最大宽度
                     direction:TooltipDirection.top, // 提示框的位置
                     // 设置弹出框内容
                     getTooltipItem: (group, groupIndex, rod, rodIndex) {
                         String myData ="";
                         if(group.x.toInt()==0){
                             myData = "第一条数据" ;                        
                         }else if(group.x.toInt()==1){
                             myData = "第二条数据" ;                        
                         }else if(group.x.toInt()==2){
                             myData = "第三条数据" ;                        
                         }
                         // rod.y.toInt() -- 当前y轴的数值
                         return BarTooltipItem("$myData ${rod.y.toInt()}", const TextStyle(color: Color.fromRGBO(180, 172, 130, 1)));                     
                     }                 
                  )
                ),
                titlesData: FlTitlesData(
                    // 纵轴
                  leftTitles: SideTitles(
                    showTitles: true, // 是否展示
                    interval: 5,
                    margin: 8,
                    reservedSize: 30,
                    getTextStyles: (value)=> const TextStyle(
                      color: Colors.black,
                      fontSize: 15
                    ) ,
                    getTitles: (value){
                      if(value.toInt()==0){
                        return "0";
                      }else if(value.toInt() == 5){
                        return "5";
                      }else{
                        return "";
                      }                      
                    },                    
                  ),
                  // 横轴
                  bottomTitles: SideTitles(
                    showTitles: true,
                    interval: 5,
                    margin: 8,
                    reservedSize: 30,
                    getTextStyles: (value)=> const TextStyle(
                      color: Colors.black,
                      fontSize: 15
                    ) ,
                    getTitles: (value){
                      if(value.toInt()==0){
                        return "1";
                      }else if(value.toInt() == 1){
                        return "2";
                      }else if(value.toInt() == 2){
                        return "3";
                      }else{
                        return "";
                      }                      
                    }, 
                  )
                ),
                // 是否展示边框
                borderData: FlBorderData(
                  show: true,
                  border:Border.all(
                    color: Colors.black12,
                      width: 1,
                      style: BorderStyle.solid
                  ),
                ),
                groupsSpace: 12, // 调整组与组之间的间隔
                // 条形图的数据
                barGroups: [
                    BarChartGroupData(
                        x: 0, // x轴坐标
                        barRods: [
                            // [Colors.black,Colors.blue]:渐变
                            // 一个值为纯色
                          BarChartRodData(y: 1,colors: [Colors.black,Colors.blue],width: 10)
                        ]
                  ) , 
                  BarChartGroupData(
                        x: 1,
                        barRods: [
                          BarChartRodData(y: 6,colors: [Colors.black,Colors.blue],width: 10)
                        ]
                  ) ,  
                  BarChartGroupData(
                        x: 2,
                        barRods: [
                          BarChartRodData(y: 8,colors: [Colors.black,Colors.blue],width: 10)
                        ]
                  ) ,              
                ],      
              ),
              swapAnimationDuration: const Duration(microseconds: 500),
              swapAnimationCurve: Curves.easeInOut,
            ),
          )      
    );
  }
}

组合条形图

                // 条形图的数据
                barGroups: [
                    BarChartGroupData(
                        x: 0, // x轴坐标
                        barsSpace: 0, // 一个坐标中条形之间的间隔
                        barRods: [
                            // [Colors.black,Colors.blue]:渐变
                            // 一个值为纯色
                          BarChartRodData(y: 3,colors: [Colors.black12,Colors.blue],width: 10),
                          BarChartRodData(y: 4,colors: [Colors.yellow,Colors.green],width: 10)
                        ]
                  ) , 
                  BarChartGroupData(
                        x: 1,
                        barsSpace: 0, // 一个坐标中条形之间的间隔
                        barRods: [
                          BarChartRodData(y: 6,colors: [Colors.black12,Colors.blue],width: 10),
                          BarChartRodData(y: 7,colors: [Colors.yellow,Colors.green],width: 10)
                        ]
                  ) ,  
                  BarChartGroupData(
                        x: 2,
                        barsSpace: 0, // 一个坐标中条形之间的间隔
                        barRods: [
                          BarChartRodData(y: 8,colors: [Colors.black12,Colors.blue],width: 10),
                          BarChartRodData(y: 6,colors: [Colors.yellow,Colors.green],width: 10)
                        ]
                  ) ,              
                ],  

fl_charts:折线图Flutter实战-折线图(fl_chart)-CSDN博客

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用fl_chart库创建饼图的简单示例: ```dart import 'package:flutter/material.dart'; import 'package:fl_chart/fl_chart.dart'; class PieChartSample extends StatefulWidget { @override _PieChartSampleState createState() => _PieChartSampleState(); } class _PieChartSampleState extends State<PieChartSample> { int touchedIndex; @override Widget build(BuildContext context) { return AspectRatio( aspectRatio: 1, child: PieChart( PieChartData( pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) { setState(() { if (pieTouchResponse.touchInput is FlLongPressEnd || pieTouchResponse.touchInput is FlPanEnd) { touchedIndex = -1; } else { touchedIndex = pieTouchResponse.touchedSectionIndex; } }); }), borderData: FlBorderData(show: false), sectionsSpace: 0, centerSpaceRadius: 40, sections: getSections(), ), ), ); } List<PieChartSectionData> getSections() { return List.generate(4, (i) { final isTouched = i == touchedIndex; final double fontSize = isTouched ? 25 : 16; final double radius = isTouched ? 60 : 50; switch (i) { case 0: return PieChartSectionData( color: Colors.red, value: 40, title: '40%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); case 1: return PieChartSectionData( color: Colors.green, value: 30, title: '30%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); case 2: return PieChartSectionData( color: Colors.blue, value: 15, title: '15%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); case 3: return PieChartSectionData( color: Colors.yellow, value: 15, title: '15%', radius: radius, titleStyle: TextStyle( fontSize: fontSize, fontWeight: FontWeight.bold, color: const Color(0xffffffff), ), ); default: return null; } }); } } ``` 在这个例子中,我们创建了一个PieChartSample类,它继承自StatefulWidget。在build()方法中,我们创建了一个AspectRatio小部件,它将PieChart小部件的宽高比设置为1:1。然后,我们创建了一个PieChart小部件,它接收一个PieChartData对象作为参数。PieChartData对象包含所有的饼图数据,包括饼图的颜色、半径、标题、值等。 在PieChartData对象中,我们使用了PieTouchData对象来处理用户的触摸事件。我们还使用了FlBorderData对象来隐藏边框,使用sectionsSpace属性来设置饼图部分之间的间距,使用centerSpaceRadius属性来设置中心空间的半径。 在getSections()函数中,我们生成了饼图的各个部分。每个部分都由一个PieChartSectionData对象表示,它包括颜色、半径、值、标题和标题样式等属性。我们返回一个包含所有部分的列表,然后将它们传递给PieChartData对象。 最后,我们在PieTouchData对象的touchCallback属性中处理触摸事件。如果用户停止触摸饼图,我们将touchedIndex设置为-1,否则,我们将touchedIndex设置为当前触摸部分的索引。根据touchedIndex的值,我们可以设置饼图部分的半径和标题样式,以突出显示当前触摸的部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值