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

echarts一个图例控制多个图表

因为业务需求,需要实现多个环状图由一个实例控制,其实可以使用echarts的encode图表设置实现。但设计图的样式用encode图表的样式无法实现,所以图例和图表需要分开实现
效果如图所示
在这里插入图片描述

第一步,先创建一个父组件,引入图例的组件和图表的组件
在这里插入图片描述
第二步,创建子组件,图表的组件BgPieLabel.vue

<template><div ref="chartRef" style="width: 100%; height: 100%"></div>
</template><script setup>
import { onMounted, ref, watch, onUnmounted } from "vue";
import * as echarts from "echarts";const chartRef = ref(null);
let resizeObserver = null; //监听页面大小变化const props = defineProps({dataV: Object,title: String,legend: Boolean,center: Array,label: Boolean,selectedName: Array, // 当前选中的 namehighlightName: String, // 当前高亮的名称,undefined表示无
});// 键名替换映射
// 解析数据并生成 ECharts 配置
const render = (data) => {const option = {tooltip: {},title: {text: props.title,left: "center", // 标题水平居中bottom: "0",textStyle: {fontWeight: 700,fontSize: "16px",color: "#333333",},},grid: {left: "4%",right: "4%",bottom: "2%",containLabel: true,},legend: {orient: "vertical",top: "center",left: "5%",show: props.legend,},color: ["#6488FE", "#FF6F6F", "#FEE177", "#2AF0CF", "#0099F4"],series: [{radius: ["45%", "55%"],center: props.center,type: "pie",label: {show: false,},labelLine: {show: true,},animation: false,tooltip: {show: false,},itemStyle: {color: "rgba(186, 207, 255, 0.2)",},data: [1],},{type: "pie",radius: ["25%", "45%"],center: props.center,padAngle: 5,label: {formatter: "{name|{b}}\n{time|{c}%}",minMargin: 0,edgeDistance: 10,lineHeight: 15,fontSize: 12,padding: [0, -70],alignTo: "labelLine",color: "#000",rich: {time: {fontSize: 12,color: "#000",},},},itemStyle: {shadowBlur: 10,shadowColor: "rgba(184,184,184,1)",shadowOffsetX: [10, -10],shadowOffsetY: [10, -10],},labelLine: {length: 25,length2: 70,maxSurfaceAngle: 70,lineStyle: {color: "#CECECE", // 线条颜色},},data: data,},],};return option;
};onMounted(() => {const chart = echarts.init(chartRef.value);// 假设 props.dataV 已经包含了解析好的数据const option = render(props.dataV);chart.setOption(option);watch(() => props.dataV,(newData) => {chart.setOption(render(newData));},{ deep: true });// 监听高亮状态watch([() => props.highlightName],([name]) => {chart.dispatchAction({ type: "downplay", seriesIndex: 1 }); // 先全部取消if (name !== undefined) {chart.dispatchAction({type: "highlight",seriesIndex: 1,name: name,});}},{ immediate: true });watch(() => props.selectedName,(arr) => {// arr 为当前需要“显示”的系列名集合chart.dispatchAction({type: "legendAllSelect",});// 先全部选中(防止脏状态)arr.map((name) => {chart.dispatchAction({type: "legendUnSelect",name: name,});});},{ immediate: true, deep: true });// 监听当容器大小变化时随之变化resizeObserver = new ResizeObserver((entries) => {for (let entry of entries) {if (entry.target === chartRef.value) {if (chart != null) {chart.resize();}}}});resizeObserver.observe(chartRef.value);const handleResize = () => chart.resize();// window.addEventListener('resize', handleResize);onUnmounted(() => {window.removeEventListener("resize", handleResize);chart.dispose();});
});
</script>

第三步,图例的组件 BgPieLegend.vue

<template><div ref="pieChart" class="line-box" style="height: 60px"></div>
</template><script setup>
import { onMounted, ref, watch, onUnmounted } from "vue";
import * as echarts from "echarts";
const pieChart = ref(null);
let myChart = null;
let resizeObserver = null; //监听页面大小变化const props = defineProps({pieData: Object,
});
const emit = defineEmits(["legendSelect", "legendHighlight", "legendDownplay"]);const initChart = () => {myChart = echarts.init(pieChart.value);const option = {legend: {orient: "horizontal",top: "center",right: "5%",type: "scroll",},color: ["#6488FE", "#FF6F6F", "#FEE177", "#2AF0CF", "#0099F4"],series: [{type: "pie",radius: ["0%", "1%"],  //设置图表的大小不会展示在页面上center: ["0%", "0%"],data: props.pieData,label: {show: false,},},],};myChart.setOption(option);// 点击图例显示和隐藏对应图例的图表数据myChart.on("legendselectchanged", (params) => {const selectedArr = [];Object.keys(params.selected).map((item, index) => {if (!params.selected[item]) {selectedArr.push(item);}});// 把“真正被选中”的名字状态为false的数组emit("legendSelect", selectedArr);});// hover图例的时候高亮显示对应图例的数据myChart.on("highlight", (params) => {emit("legendHighlight", params.name);});// 取消高亮myChart.on("downplay", (params) => {emit("legendDownplay", "");});
};onMounted(() => {initChart();watch(() => props.pieData,() => {if (myChart) {myChart.setOption({series: [{data: props.pieData,},],});}},{ deep: true });// 监听当容器大小变化时随之变化resizeObserver = new ResizeObserver((entries) => {for (let entry of entries) {if (entry.target === pieChart.value) {if (myChart != null) {myChart.resize();}}}});resizeObserver.observe(pieChart.value);const handleResize = () => myChart.resize();// window.addEventListener('resize', handleResize);onUnmounted(() => {window.removeEventListener("resize", handleResize);myChart.dispose();});
});
</script>

代码如上所示,通过父子传参、emit事件,实现分开的图例和图表,数据可以联动,实现图例hover图表对应高亮及图例点击可以显示和隐藏对应的图表数据。

http://www.dtcms.com/a/309601.html

相关文章:

  • Git 进阶使用
  • ansible 在EE 容器镜像中运行
  • C primer plus (第六版)第十章 编程练习第7,8,9,10,11题
  • Linux基本服务——web服务解析
  • 如何管理数据足迹,实现SAP S/4HANA的无缝迁移
  • Solana: 逐行解读 solana-test-validator 输出, 本地节点日志完全指南
  • oracle备库主机断电重启后IO异常报错
  • 【C#学习Day16笔记】XML文件、 事件Event 、Json数据
  • Sqlserver备份恢复指南-完整备份恢复
  • 从零到英雄:掌握神经网络的完整指南
  • Qt Quick 自定义组件开发
  • 江协科技STM32 11-4 SPI通信外设
  • Android SDK 版本差异与兼容方案:从适配到实践
  • gitlab 开发人员无法创建分支,管理员配置分支权限
  • flutter-boilerplate-project 学习笔记
  • 嵌入式学习笔记-MCU阶段--DAY09
  • STM32-ESP8266Wi-Fi模块使用USART实现通信/创建AP和STA模式配置教程(寄存器版)
  • 从0开始学习R语言--Day64--决策树回归
  • 流式编程的中间操作
  • 机器学习sklearn:随机森林的决策树
  • 低通滤波器的原理以及作用
  • C# 引用外部项目
  • 切比雪夫不等式
  • 网页从点击到显示:前端开发视角下的旅程
  • 在SQL SERVER 中如何用脚本实现每日自动调用存储过程
  • 大模型开发框架LangChain之构建知识库
  • 高速公路桥梁安全监测系统解决方案
  • 技术栈:基于Java语言的搭子_搭子社交_圈子_圈子社交_搭子小程序_搭子APP平台
  • 安全专家发现利用多层跳转技术窃取Microsoft 365登录凭证的新型钓鱼攻击
  • 【C#学习Day14笔记】泛型、集合(数组列表Arraylist、列表list)与字典