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

手动实现legend 与 echarts图交互 通过js事件实现图标某项的高亮 显示与隐藏

通过html实现legend的样式 提供调用echarts的api实现与echarts图表交互的效果

实现饼图element实现类似于legend与echartstu表交互效果

效果图

在这里插入图片描述

配置代码

<template><div style="height: 400px; width: 500px;background-color: #CCC;"><v-chart:option="options"autoresizestyle="width: 100%; height: 100%"ref="chart"/><div style="display: flex; margin-right: 20px"><divv-for="item in dataList":key="item.name"style="display: flex;align-items: center; margin-top: 10px; margin-right: 20px;cursor: pointer"@mouseenter="handleMouseenter(item)"@mouseleave="handleMouseleave(item)"@click="handleClick(item)"><i :style="{'background-color': item.isShow ? item.itemStyle.color: '#CCC'}" style="width: 10px;height:10px;border-radius: 50%;margin-right: 5px"></i><span style="margin-right:10px;">{{item.name}}</span><span>{{item.value}}</span></div></div></div>
</template><script>export default {name: 'AboutView',data() {return{dataList: [{ value: 1048, name: 'Search Engine',isShow: true,itemStyle: {color: '#006699'} },{ value: 735, name: 'Direct',isShow: true,itemStyle: {color: '#009966'}  },{ value: 580, name: 'Email',isShow: true,itemStyle: {color: '#FFCC00'}  },]}},computed: {options() {return {tooltip: {trigger: 'item'},legend:{show: false},series: [{name: 'Access From',type: 'pie',radius: ['45%', '50%'],center: ['50%', '38.5%'],avoidLabelOverlap: false,label: {show: false,position: 'center'},labelLine: {show: false},data: this.dataList}]}}},methods: {handleMouseenter(item){this.$refs.chart.dispatchAction({type: 'highlight',name: item.name,// seriesIndex: 1// dataIndex: 1// // 用 index 或 id 或 name 来指定系列。// // 可以使用数组指定多个系列。// seriesIndex?: number | number[],// seriesId?: string | string[],// seriesName?: string | string[],// // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项// dataIndex?: number | number[],// // 可选,数据项名称,在有 dataIndex 的时候忽略// name?: string | string[],})},handleMouseleave(item){this.$refs.chart.dispatchAction({type: 'downplay',name: item.name,// seriesIndex: 1// dataIndex: 1// // 用 index 或 id 或 name 来指定系列。// // 可以使用数组指定多个系列。// seriesIndex?: number | number[],// seriesId?: string | string[],// seriesName?: string | string[],// // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项// dataIndex?: number | number[],// // 可选,数据项名称,在有 dataIndex 的时候忽略// name?: string | string[],})},handleClick(item){item.isShow = !item.isShowthis.$refs.chart.dispatchAction({type: 'legendToggleSelect',name: item.name,// // 用 index 或 id 或 name 来指定系列。// // 可以使用数组指定多个系列。// seriesIndex?: number | number[],// seriesId?: string | string[],// seriesName?: string | string[],// // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项// dataIndex?: number | number[],// // 可选,数据项名称,在有 dataIndex 的时候忽略// name?: string | string[],})}}}
</script><style lang="scss" scoped></style>

通过js实现柱形图同一组数据时 隐藏某一个柱子

效果图

在这里插入图片描述

配置代码

<template><div style="height: 400px; width: 500px;background-color: #CCC;"><v-chart:option="options"autoresizestyle="width: 100%; height: 100%"ref="chart"/><div style="display: flex; margin-right: 20px"><divv-for="item in dataList":key="item.name"style="display: flex;align-items: center; margin-top: 10px; margin-right: 20px;cursor: pointer"@click="handleClick(item)"@mouseenter="handleMouseenter(item)"@mouseleave="handleMouseleave(item)"><i :style="{'background-color': item.isShow ? item.itemStyle.color: '#CCC'}" style="width: 10px;height:10px;border-radius: 50%;margin-right: 5px"></i><span style="margin-right:10px;">{{item.name}}</span><span>{{item.value}}</span></div></div></div>
</template><script>export default {name: 'AboutView',data() {return{dataList: [{ value: 1048, name: 'Search Engine',isShow: true,itemStyle: {color: '#006699'} },{ value: 735, name: 'Direct',isShow: true,itemStyle: {color: '#009966'}  },{ value: 580, name: 'Email',isShow: true,itemStyle: {color: '#FFCC00'}  },]}},computed: {options() {return {tooltip: {trigger: 'item'},legend:{show: false},xAxis: {type: 'category',data: this.computedSeriesData.map(item => item.name)},yAxis: {type: 'value'},series: [{name: 'Access From',type: 'bar',radius: ['45%', '50%'],center: ['50%', '38.5%'],avoidLabelOverlap: false,label: {show: false,position: 'center'},labelLine: {show: false},data: this.computedSeriesData}]}},computedSeriesData() {return this.dataList.filter(item => item.isShow)}},methods: {handleClick(item){item.isShow = !item.isShow},handleMouseenter(item){this.$refs.chart.dispatchAction({type: 'highlight',name: item.name,// seriesIndex: 1// dataIndex: 1// // 用 index 或 id 或 name 来指定系列。// // 可以使用数组指定多个系列。// seriesIndex?: number | number[],// seriesId?: string | string[],// seriesName?: string | string[],// // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项// dataIndex?: number | number[],// // 可选,数据项名称,在有 dataIndex 的时候忽略// name?: string | string[],})},handleMouseleave(item){this.$refs.chart.dispatchAction({type: 'downplay',name: item.name,// seriesIndex: 1// dataIndex: 1// // 用 index 或 id 或 name 来指定系列。// // 可以使用数组指定多个系列。// seriesIndex?: number | number[],// seriesId?: string | string[],// seriesName?: string | string[],// // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项// dataIndex?: number | number[],// // 可选,数据项名称,在有 dataIndex 的时候忽略// name?: string | string[],})},}}
</script><style lang="scss" scoped></style>

通过js实现柱形图一组数据的显示隐藏 达到legend的效果

效果图

在这里插入图片描述
在这里插入图片描述

配置代码

<template><div style="height: 400px; width: 500px;background-color: #CCC;"><v-chart:option="options"autoresizestyle="width: 100%; height: 100%"ref="chart"/><div style="display: flex; margin-right: 20px"><divv-for="item in legendList":key="item.name"style="display: flex;align-items: center; margin-top: 10px; margin-right: 20px;cursor: pointer"@click="handleClick(item)"@mouseenter="handleMouseenter(item)"@mouseleave="handleMouseleave(item)"><i :style="{'background-color': item.isShow ? item.color: '#CCC'}" style="width: 10px;height:10px;border-radius: 50%;margin-right: 5px"></i><span style="margin-right:10px;">{{item.name}}</span><span>{{item.value}}</span></div></div></div>
</template><script>export default {name: 'AboutView',data() {return{legendList: [{ name: '测试1', isShow: true, color: '#00BFFF' },{ name: '测试2', isShow: true, color: '#FFD700'},],dataList1: [{ value: 1048, name: 'Search Engine' },{ value: 735, name: 'Direct'},{ value: 580, name: 'Email'},],dataList2: [{ value: 800, name: 'Search Engine' },{ value: 335, name: 'Direct'},{ value: 1580, name: 'Email'},]}},computed: {options() {return {tooltip: {trigger: 'item'},legend:{show: false},xAxis: {type: 'category',data: this.dataList1.map(item => item.name)},yAxis: {type: 'value'},series: [{name: '测试1',type: 'bar',radius: ['45%', '50%'],center: ['50%', '38.5%'],avoidLabelOverlap: false,label: {show: false,position: 'center'},labelLine: {show: false},itemStyle:{color: '#00BFFF'},data: this.dataList1},{name: '测试2',type: 'bar',radius: ['45%', '50%'],center: ['50%', '38.5%'],avoidLabelOverlap: false,label: {show: false,position: 'center'},labelLine: {show: false},itemStyle:{color: '#FFD700'},data: this.dataList2}]}},},methods: {handleClick(item){item.isShow = !item.isShowthis.$refs.chart.dispatchAction({type: 'legendToggleSelect',// 图例名称name: item.name,// // 下列参数自 v5.6.0 起开始支持// // 图例组件ID// legendId: string,// // 图例组件索引// legendIndex: number,// // 图例组件名称// legendName: string})},handleMouseenter(item){this.$refs.chart.dispatchAction({type: 'highlight',seriesName: item.name// seriesIndex: 1// dataIndex: 1// // 用 index 或 id 或 name 来指定系列。// // 可以使用数组指定多个系列。// seriesIndex?: number | number[],// seriesId?: string | string[],// seriesName?: string | string[],// // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项// dataIndex?: number | number[],// // 可选,数据项名称,在有 dataIndex 的时候忽略// name?: string | string[],})},handleMouseleave(item){this.$refs.chart.dispatchAction({type: 'downplay',seriesName: item.name// seriesIndex: 1// dataIndex: 1// // 用 index 或 id 或 name 来指定系列。// // 可以使用数组指定多个系列。// seriesIndex?: number | number[],// seriesId?: string | string[],// seriesName?: string | string[],// // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项// dataIndex?: number | number[],// // 可选,数据项名称,在有 dataIndex 的时候忽略// name?: string | string[],})},}}
</script><style lang="scss" scoped></style>

相关文章:

  • Vivado22 Vcs18仿真联调原语缺失
  • DNS实验
  • STM32F407使用ESP8266实现阿里云OTA(中)
  • Python学习之路(五)-接口API
  • Java Thread类深度解析:构造方法与核心方法全攻略
  • 运算符重载 (Operator Overloading)
  • RPCRT4!NDRSContextUnmarshall2函数分析
  • IEEE综述 | 车道拓扑推理20年演进:从程序化建模到车载传感器
  • 什么是CMMI认证?CMMI评估内容?CMMI认证能带来哪些好处?
  • 通过4种方法来重置UOS操作系统中的用户密码
  • 4.3 工具调用与外部系统集成:API调用、MCP(模型上下文协议)、A2A、数据库查询与信息检索的实现
  • 简易学生成绩管理系统(C语言)
  • 动手试一试 Spring Security入门
  • 服务器上安装node
  • Ubuntu服务器上如何监控Oracle数据库
  • JCP官方定义的Java技术体系组成部分详解
  • 操作系统---经典同步问题
  • 高功率激光输出稳定性不足?OAS 光学软件来攻克
  • 【Python网络爬虫实战指南】从数据采集到反反爬策略
  • ActiveMQ 快速上手:安装配置与基础通信实践(一)
  • 最新研究挑战男性主导说:雌性倭黑猩猩联盟对付雄性攻击,获得主导地位
  • 唐仁健违规收受礼品、礼金被点名!十起违反中央八项规定精神典型问题被通报
  • 低轨卫星“千帆星座”已完成五批次组网卫星发射,未来还有这些计划
  • 凯撒旅业:2024年营业收入约6.53亿元,同比增长12.25%
  • 嫦娥五号月球样品将借给这些国家机构
  • 一季度全国纪检监察机关共处分18.5万人,其中省部级干部14人