当前位置: 首页 > news >正文

Echarts+VUE饼图的使用(基础使用、多个饼图功能、单组饼图对应颜色使用)

安装:npm install echarts --save
配置:main.js

// 引入echarts
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts

一、基础饼图(直接拷贝就能出效果)

 <div class="big-box" ref="demoEhart"></div>
mounted() {this.demoChart()
}
demoChart(){var myChart = this.$echarts.init(this.$refs.demoEhart);var option = {title: {text: 'Referer of a Website',subtext: 'Fake Data',left: 'center'},tooltip: {trigger: 'item'},legend: {orient: "horizontal",icon: "circle",bottom: 0,x: "center",textStyle: {color: "#fff"}},series: [{name: 'Access From',type: 'pie',radius: '50%',data: [{ value: 1048, name: 'Search Engine' },{ value: 735, name: 'Direct' },{ value: 580, name: 'Email' },{ value: 484, name: 'Union Ads' },{ value: 300, name: 'Video Ads' }],}]
};myChart.setOption(option);
}

二、饼图(多组动态显示饼图数量的数据)

//获取到的数据
ceshilist:[{name:'/dev',free:30,used:520},{name:'/d456',free:30,used:520},{name:'/d88',free:30,used:520},{name:'/d78978',free:30,used:520},{name:'/sdasd8',free:30,used:520},{name:'/d8sad456asd8',free:30,used:520},{name:'/sasss8',free:30,used:520},{name:'/dwqewunn8',free:30,used:520},{name:'/dev336',free:30,used:520}],
//最终需要的数据格式
data:[
[{value:ceshilist[i].free,name:'可用量',typename:ceshilist[i].mountPath},
{value:ret[i].used,name:'已用量',typename:ret[i].fs_type_name}],
[{value:ceshilist[i].free,name:'可用量',typename:ceshilist[i].mountPath},
{value:ret[i].used,name:'已用量',typename:ret[i].fs_type_name}]
]

步骤:

 <div class="big-box" ref="pieEhart"></div>
  data() {return {source:[],setData:new Map//实时刷新map对象}}
mounted() {this.pieEhartclick()
}
getDiskData(ret){let source=[]//将获取到的数据变成以下数据格式for (let i = 0; i < ret.length; i++) {source.push([{value:ret[i].free,name:'可用量',typename:ret[i].mountPath},{value:ret[i].used,name:'已用量',typename:ret[i].fs_type_name}])}this.source=sourcethis.pieEhartclick()//重新渲染图表方法,不是实时数据可以不加此代码
}
 // 磁盘饼图pieEhartclick(){var myChart = this.$echarts.init(this.$refs.pieEhart);var datas=this.source// let that = this;var option = {{text:'磁盘使用情况',subtext:'将鼠标移动对应饼图上以查看对应信息'},//每个饼图对应的标题名称title:datas.map(function(data,idx){var numcol=4  //定义列var numrow=Math.ceil(datas.length/numcol) //定义行 有多少条数据除以列就是行数var top=Math.floor(idx/numcol)*100/numrow+10//距离上面的距离var left=(idx%numcol)*100/numcol+12//距离左边的距离return{subtext:data[0].typename,top:top+'%',left:left+'%',textAlign:'center',subtextStyle:{color:'#ffffff'//字体颜色}}}),legend: {},color:["#3F60C6","#9A60B4"],tooltip: {trigger:'item',formatter:'{b}:{c}({d}%)'//鼠标悬浮显示数据},series:datas.map(function(data,idx){var numcol=4 //定义列var numrow=Math.ceil(datas.length/numcol) //定义行 有多少条数据除以列就是行数var top=Math.floor(idx/numcol)*100/numrow//距离上面的距离var left=(idx%numcol)*100/numcol//距离左边的距离return{type:'pie',// radius:[20,60],top:top+'%',left:left+'%',height:100/numrow+'%',width:100/numcol+'%',itemStyle:{// borderColor:'#fff',// borderWidth:1,// color:function(data,idx){//   if(data[idx].value>95){//     return 'red'//   }// }},label:{show:false},data:data}}),};myChart.setOption(option);},

三、饼图(单组数据)
在这里插入图片描述

扩展功能:一般自定义颜色是按照数据的顺序依次对应,现在想要指定字段对应某个颜色

 <div class="big-box" ref="oneEhart"></div>
cdata: {xData: ["水文", "森林", "气象", "地质",  "其他"],seriesData: [{ value: 35, name: "水文" ,itemStyle: {color:"#9fe6b8"}},{ value: 15, name: "森林" },{ value: 15, name: "气象" },{ value: 25, name: "地质" },{ value: 40, name: "其他" }]},
coloritem:{"森林":"#9fe6b8","气象":"#0099ff","水文":"#32c5e9","地质":"#e7bcf3","其他":"#fb7293"}
mounted() {this.getOnechart()
}
 methods: {getBar(){this.$axios.post('******').then((res)=>{let items=res.data //接收到的数据this.cdata.seriesData=this.getData(items) //变成想要的数据格式方法console.log(this.cdata.seriesData)})},//对应颜色处理getData(data) {let that=thisreturn data.map(function (item) {return {value: item.value,name: item.name,itemStyle: {color: that.coloritem[item.name] // 使用颜色映射表中对应的颜色}};});}}
getOnechart(){
var myChart = this.$echarts.init(this.$refs.oneEhart);
let newdata=this.cdata
var option = {title: {text: 'Referer of a Website',subtext: 'Fake Data',left: 'center'},tooltip: {trigger: 'item',formatter: "{c} ({d}%)"},toolbox: {show: true},calculable: true,//默认方块显示颜色标签// legend: {// orient: 'vertical',// left: 'left'//},  legend: {//圆圈orient: "horizontal",icon: "circle",bottom: 0,x: "center",data: newData.xData,textStyle: {color: "#fff"}},       series: [{name: 'Access From',type: 'pie',radius: '50%',label: {//echarts饼图内部显示百分比设置formatter: "{b}\n{d}%",lineHeight: 15,//  color: "#ffffff", //颜色fontSize: 12 //字体大小},data: [{ value: 1048, name: 'Search Engine' },{ value: 735, name: 'Direct' },{ value: 580, name: 'Email' },{ value: 484, name: 'Union Ads' },{ value: 300, name: 'Video Ads' }],data: newData.seriesData}]
};myChart.setOption(option);
}
http://www.lryc.cn/news/490484.html

相关文章:

  • 刘铁猛C#入门 026 重写与多态
  • 《筑牢安全防线:培养 C++安全编程思维习惯之道》
  • 《TCP/IP网络编程》学习笔记 | Chapter 16:关于 I/O 流分离的其他内容
  • 单片机学习笔记 5. 数码管静态显示
  • ValueError: not enough values to unpack (expected 2, got 1) 解决方案
  • java基础知识(常用类)
  • Selenium+Java(19):使用IDEA的Selenium插件辅助超快速编写Pages
  • 决策树分类算法【sklearn/决策树分裂指标/鸢尾花分类实战】
  • 深入理解 Spring Boot 的 WebApplicationType
  • 摄影:相机控色
  • Python网络爬虫技术及其应用
  • 鸿蒙学习笔记:ArkUI概述
  • Selenium 在自动化测试中的应用
  • python3 Flask应用 使用 Flask-SQLAlchemy操作MySQL数据库
  • Python学习——猜拳小游戏
  • 递归-迭代
  • 恋爱通信史之完整性
  • Docker 容器的初始化设置
  • 密码编码学与网络安全(第五版)答案
  • C++初阶(十四)--STL--vector的模拟实现
  • 贴代码框架PasteForm特性介绍之query,linkquery
  • 高防IP如何构建安全高效的数字政务新生态
  • 数据结构与算法——1122—复杂度总结检测相同元素
  • HTML通过JavaScript获取访问连接,IP和端口
  • 自动化测试过程操作细节
  • AR智能眼镜|AR眼镜定制开发|工业AR眼镜方案
  • 从〇开始深度学习(0)——背景知识与环境配置
  • 实验室管理技术革新:Spring Boot系统
  • C语言 蓝桥杯某例题解决方案(查找完数)
  • Prompting LLMs to Solve Complex Tasks: A Review