首页 > 其他分享 >Flutter实战 -- fl_chart(条形图)

Flutter实战 -- fl_chart(条形图)

时间:2024-04-10 21:32:46浏览次数:12  
标签:10 colors return -- toInt chart width Colors fl

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博客

标签:10,colors,return,--,toInt,chart,width,Colors,fl
From: https://blog.csdn.net/weixin_53468073/article/details/137611105

相关文章

  • 在eclipse上写第一个java项目,输出hello world
    第一步:创建一个JavaProject第二步:创建一个java类第三步:执行代码,输出helloworld......
  • 【软考】计算机组成与体系结构 - 系统可靠性分析与设计(串联系统与并联系统可靠度计算)
    一、并联系统1.1并联系统的结构注:少数子系统的失效将不会影响整个系统对于并联系统,可以使用以下公式来计算其可靠度:R=1-(1-R1)(1-R2)...(1-Rn)其中,R1,R2,…,Rn是各个组件的可靠度。1.2并联系统可靠性的计算通过计算失效率来求得可靠性,即各个子系统的失......
  • 【异常】Outlook一直提示正在启动
    一、异常内容二、异常说明安全模式可以帮助您解决Outlook的一些问题,如崩溃、卡顿、无法登录等。三、异常解决要在安全模式下打开Outlook,可以按照以下步骤操作,请确保在进行这些操作之前备份您的Outlook数据,以防万一出现不可预料的问题导致数据丢失。3.1使用运行命令......
  • 【异常】写了很多单元测试用例,但是Sonar上显示的单元测试覆盖率依旧为 0.0%
    一、异常内容写了很多单元测试用例,但是Sonar上显示的单元测试覆盖率依旧为0.0%二、异常说明在SonarQube中显示单元测试覆盖率为0%,通常意味着SonarQube没有正确地接收到测试覆盖率报告。三、异常解决要解决这个问题,您可以按照以下步骤操作:3.1确保测试覆盖率报告已......
  • 【异常】FATAL ERROR in native method: JDWP loaded classes, jvmtiError=JVMTI_ERRO
    一、异常内容IDEA启动微服务之后,提示FATALERRORinnativemethod:JDWPloadedclasses,jvmtiError=JVMTI_ERROR_OUT_OF_MEMORY(110)FATALERRORinnativemethod:JDWPloadedclasses,jvmtiError=JVMTI_ERROR_OUT_OF_MEMORY(110) atsun.misc.Unsafe.defineAnonym......
  • Bonnie++ 工具学习记录
    Bonnie++工具学习记录文章目录Bonnie++工具学习记录主要特点如何下载安装Bonnie++使用Bonnie++常见使用方式:基本使用:测试并生成报告。测试结果分析:主要使用场景Bonnie++是一款专门用于测试硬盘和文件系统性能的开源工具。它通过模拟各种文件操作来评估存储......
  • 2024年flstudio优惠码在哪用?flstudio优惠码在哪用?flstudio是否有必要购买以及如何升级
    现在已经是4月份了,FLStudio21问世已经有一年多了,但是很多朋友依然还在使用低版本的FLStudio20,甚至还有人在使用FLStudio12的,难道你们不想在21新世纪体验最新版FLStudio21版本的么。也有很多朋友在后台私信我,能不能出个FLStudio21购买教程,这不它来了~很多打算入手正版FLStu......
  • 常用集合算法
    算法简介:set_intersection//求两个容器的交集set_union//求两个容器的并集set_difference//求两个容器的差集一、set_intersection:求两个容器的交集函数原型:set_intersection(iteratorbeg1,iteratorend1,iteratorbeg2,iteratorend2,iteratordest);//be......
  • RISCV
    RISCVriscv通用寄存器riscv通用寄存器寄存器调用名字用途x0zero常数0x1ra返回地址x2sp栈指针x3gp全局指针x4tp线程指针x5-x7t0-t2临时存储x8s0/fp保存用寄存器/帧指针(配合栈指针界定一个函数的栈)x9s1保存用寄存器x10-x11a0-a1函数参数/返回值x12-x17a2-a7函数参数x......
  • 【华为笔试题汇总】2024-04-10-华为春招笔试题-三语言题解(Python/Java/Cpp)
    ......