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

vue---echarts环形图

1、完整代码直接可以cv 

<template><div id="main1"></div>
</template><script>
import * as echarts from 'echarts';
// import { mapState } from 'vuex';
// import { Alarm_Device } from '../utils/api.js';
export default {name: 'PieChart',data() {return {chart: null,data: [{ value: 1048, name: '人体探测器', itemStyle: { color: '#4f7ff7' } },{ value: 262, name: '生命体征探测器', itemStyle: { color: '#be71f2' } },{ value: 210, name: '防摔倒探测器', itemStyle: { color: '#7586f5' } },{ value: 140, name: '智能床垫', itemStyle: { color: '#6ee497' } },{ value: 90, name: '智能手表', itemStyle: { color: '#4ebfff' } }]};},computed: {// ...mapState(['login']),},mounted() {this.initChart();this.registerUser()},beforeDestroy() {if (this.chart) {this.chart.dispose();}},methods: {getItemStyleByName(name) {// 根据设备名称返回对应的颜色const colorMap = {'人体探测器': '#4f7ff7','生命体征探测器': '#be71f2','防摔倒探测器': '#7586f5','智能床垫': '#6ee497','智能手表': '#4ebfff'};return { color: colorMap[name] || '#ffffff' }; // 如果名称未找到,则默认为白色},initChart() {const chartDom = document.getElementById('main1');this.chart = echarts.init(chartDom);const options = this.getChartOptions();this.chart.setOption(options);},getChartOptions() {const total = this.data.reduce((sum, item) => sum + item.value, 0);const dataWithPercentage = this.data.map(item => ({value: item.value,name: item.name,percentage: `${(item.value / total * 100).toFixed(1)}%`,itemStyle: item.itemStyle}));return {title: {text: '总数量',subtext: '100%',left: 'center',top: '40%',left: '29%',textAlign: 'center',textStyle: {fontSize: 18,fontWeight: 'bold',color: '#ffffff'},subtextStyle: {fontSize: 17,color: '#ffffff'}},legend: {orient: 'vertical',right: '16%',top: 'center',textStyle: {color: '#fff',  // 图例文字颜色fontSize: 12,   // 图例文字大小},formatter: (name) => {const item = dataWithPercentage.find(d => d.name === name);return item ? `${item.name} ${item.percentage}` : name;},},tooltip: {trigger: 'item',},label: {show: true,color: '#fff',  // 设置文字颜色为白色position: 'outside',formatter: ({ data }) => `${data.percentage}` // 只显示百分比},emphasis: {label: {show: true,fontSize: 20,fontWeight: 'bold',color: '#fff'  // 确保在强调状态下文字依然为白色},},labelLine: {show: true,lineStyle: {color: '#fff'  // 设置指引线的颜色为白色}},series: [{name: '',type: 'pie',radius: ['40%', '70%'],center: ['30%', '50%'], // 将中心位置设置为垂直和水平的中点data: dataWithPercentage,// 移除startAngle和endAngle,以显示完整的圆形},],};},},
};
</script><style scoped>
#main1 {width: 560px;height: 180px;
}
</style>

 2、初始化

initChart() {// 获取 DOM 元素,通过其 ID 获取到的元素将作为 ECharts 实例的容器const chartDom = document.getElementById('main1');// 初始化 ECharts 实例,传入上面获取的 DOM 元素this.chart = echarts.init(chartDom);// 获取图表的配置项,这些配置项用于定义图表的类型、数据、样式等const options = this.getChartOptions();// 设置图表的配置项// 这一步将配置项应用到 ECharts 实例中,渲染出图表this.chart.setOption(options);
}

 所有的基本上都是先获取容器盒子,然后初始化echarts,最后配置options

 3、配置解释

getChartOptions() {// 计算数据总量,用于计算百分比const total = this.data.reduce((sum, item) => sum + item.value, 0);// 为每个数据项计算百分比,并保留原有的 itemStyleconst dataWithPercentage = this.data.map(item => ({value: item.value,name: item.name,percentage: `${(item.value / total * 100).toFixed(1)}%`, // 计算并格式化百分比itemStyle: item.itemStyle // 保留原有的样式}));return {// 图表标题配置title: {text: '总数量', // 主标题文本subtext: '100%', // 副标题文本left: 'center', // 主标题水平居中top: '40%', // 主标题垂直位置left: '29%', // 主标题水平位置textAlign: 'center', // 主标题文本对齐方式textStyle: {fontSize: 18, // 主标题字体大小fontWeight: 'bold', // 主标题字体粗细color: '#ffffff' // 主标题字体颜色},subtextStyle: {fontSize: 17, // 副标题字体大小color: '#ffffff' // 副标题字体颜色}},// 图例配置legend: {orient: 'vertical', // 图例垂直排列right: '16%', // 图例水平位置top: 'center', // 图例垂直居中textStyle: {color: '#fff',  // 图例文字颜色fontSize: 12,   // 图例文字大小},// 格式化图例文字,显示名称和百分比formatter: (name) => {const item = dataWithPercentage.find(d => d.name === name);return item ? `${item.name} ${item.percentage}` : name;},},// 提示框配置tooltip: {trigger: 'item', // 鼠标悬停在项上触发提示框},// 标签配置label: {show: true, // 显示标签color: '#fff',  // 标签文字颜色position: 'outside', // 标签位置在图形外部formatter: ({ data }) => `${data.percentage}` // 标签只显示百分比},// 高亮显示配置emphasis: {label: {show: true, // 高亮时显示标签fontSize: 20, // 高亮时标签字体大小fontWeight: 'bold', // 高亮时标签字体粗细color: '#fff'  // 高亮时标签字体颜色},},// 标签线配置labelLine: {show: true, // 显示标签线lineStyle: {color: '#fff'  // 标签线颜色}},// 系列数据配置series: [{name: '', // 系列名称type: 'pie', // 图表类型为饼图radius: ['40%', '70%'], // 饼图的半径范围,内环到外环center: ['30%', '50%'], // 饼图中心位置,水平和垂直的百分比位置data: dataWithPercentage, // 使用带有百分比的数据// 移除 startAngle 和 endAngle 使饼图显示完整的圆形},],};
}

http://www.lryc.cn/news/430277.html

相关文章:

  • 克服编程挫折:从Bug的迷宫中寻找出口与面对算法保持冷静的策略
  • Flink之SQL client使用案例
  • 实际开发中的模块化开发 - 应用到直播间
  • EmguCV学习笔记 VB.Net 第5章 图像变换
  • 【初阶数据结构】顺序表与链表的比较(附题)
  • git-20240822
  • 【时时三省】c语言例题----华为机试题< 数字颠倒>
  • 【前缀和算法】--- 一维和二维前缀和模板
  • 有些信息注定会丢失
  • c#中Task.Run 和使用 Task 构造函数创建任务的区别
  • 使用nginx做代理转发
  • Java前端与后端交互:JSON与XML数据交换 - 掌握现代Web开发的核心技能
  • 网络攻击原理及过程
  • day30(8/16)——ansible
  • fastadmin 安装
  • Unity动画模块 之 3D模型导入基础设置 Rig页签
  • ⭐️Python在Windows命令行(Command Prompt)运行Python脚本或交互式地执行Python代码详解
  • Python | Leetcode Python题解之第355题设计推特
  • D. Beard Graph
  • 使用预训练的 ONNX 格式的 YOLOv8n 模型进行目标检测,并在图像上绘制检测结果
  • mac安装xmind
  • MySQL分区表入门
  • StarRocks 存算分离数据回收原理
  • 【运维】Linux中的xargs指令如何使用?
  • 日志审计-graylog ssh登录超过6次告警
  • 4. kafka消息监控客户端工具
  • 鸿蒙环境和模拟器安装
  • 【图文并茂】ant design pro 如何对接后端个人信息接口
  • MySQL运维学习(1):4种日志
  • 代码随想录算法训练营第二十天(二叉树 七)