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

echarts-事件

echarts部分事件
在这里插入图片描述

添加点击事件

添加点击事件:

let options = {tooltip: {},xAxis: {type: "category",data: ["d1", "d2", "d3", "d4"],},yAxis: {},series: [{type: "line",data: d1,},{type: "bar",data: d2,},],};myEcharts.on("click", (value) => {console.log(value);});

bar:
在这里插入图片描述
line:
在这里插入图片描述

过滤项

事件主要有两种写法:
myecharts.on(事件,监听执行的方法,this指向?) 或
myecharts.on(事件,过滤条件,监听执行的方法,this指向?)
第二种写法可以添加过滤条件,让特定的图表触发。
由于echarts的事件是绑定整个echarts的,所以如果想给不同的图表绑定不同的点击事件,就需要使用过滤项。
在这里插入图片描述

  1. 按照图标类型
    series.类型
 myEcharts.on("click", "series.line", (value) => {console.log("line");});myEcharts.on("click", "series.bar", (value) => {console.log("bar");});
  1. 图标顺序 seriesIndex
 myEcharts.on("click",{seriesIndex: 0,},(value) => {console.log("line");});myEcharts.on("click",{seriesIndex: 1,},(value) => {console.log("bar");});
  1. name 是data中数据的name,可以针对某一条数据触发
series: [{type: "line",data: [{name: "lineD1",value: 10,},20,30,40,],},{type: "bar",data: d2,},],myEcharts.on("click",{name: "lineD1",},(value) => {console.log("line");});
  1. dataIndex data数组中的第几个数据
 myEcharts.on("click",{seriesIndex: 0,dataIndex: 1,},(value) => {console.log("line");});
  1. dataType 在关系图中有用,如果设为node就是节点触发,edge就是边触发。
 series: [{type: "line",data: [{name: "lineD1",value: 10,},20,30,40,],},{type: "bar",data: d2,},{type: "graph",data: [{name: "1",x: 10,y: 10,value: 10,},{name: "2",x: 100,y: 100,value: 20,},],links: [{source: "1",target: "2",},],},],myEcharts.on("click",{dataType: "node",},(value) => {console.log("line");});
  1. element 自定义图表, 规定自定义图表返回的那个节点绑定事件。
 {type: "custom",renderItem() {return {type: "circle",//  coordinateSystem: "none",name: "c1",shape: {cx: 20,cy: 10,r: 50,},style: {fill: "blue",},};},},myEcharts.on("click",{element:"c1"}, (value) => {console.log("c1");});

使用echarts之外的按钮去触发

事件触发 dispatchAction(obj),只能触发图表行为。

 let options = {tooltip: {},legend: {show: true,},xAxis: {type: "category",data: ["d1", "d2", "d3", "d4"],},yAxis: {},series: [{type: "line",name: "line",data: [{name: "lineD1",value: 10,},20,30,40,],},{type: "bar",name: "bar",data: d2,},],};
const cli = () => {myEcharts.dispatchAction({ type: "legendUnSelect", name: "line" });
};
<button @click="cli">点击图例</button>

在这里插入图片描述

例子 点击柱状图展开详细的折线图

let myEcharts;
let options;
const data = [{data: "2024-05-05",value: 20,detail: [{time: "07:00-09:00",value: 5,},{time: "12:00-13:00",value: 5,},{time: "14:00-18:00",value: 10,},],},{data: "2024-06-06",value: 10,detail: [{time: "08:00-09:00",value: 5,},{time: "12:50-13:00",value: 4,},{time: "16:00-18:00",value: 1,},],},{data: "2024-07-07",value: 15,detail: [{time: "08:00-10:00",value: 5,},{time: "11:00-132:00",value: 5,},{time: "15:00-15:50",value: 10,},],},{data: "2024-08-08",value: 5,detail: [{time: "07:00-09:00",value: 2,},{time: "12:00-13:00",value: 2,},{time: "14:00-18:00",value: 1,},],},
];
onMounted(() => {let canvas = document.getElementById("canvas");echarts.registerMap("china", china);myEcharts = echarts.init(canvas, null, {width: 800,height: 500,devicePixelRatio: window.devicePixelRatio,locale: "ZH",});options = {tooltip: {},legend: {show: true,},xAxis: {type: "category",data: data.map((item) => item.data),},yAxis: {},series: [{type: "bar",name: "bar",data: data.map((item) => item.value),},],};myEcharts.on("click", "series.bar", (value) => {//匹配出当前点击的对象const date = value.name;const item = data.find((item) => {console.log(item.data);if (item.data == date) {return item;}});const detail = item.detail;//将detail渲染成折线myEcharts.setOption({xAxis: {type: "category",data: detail.map((item) => item.time),},yAxis: {},series: [{type: "line",data: detail.map((item) => item.value),},],});});rednderEcharts(options);
});function rednderEcharts(options) {myEcharts.setOption(options);
}
const cli = () => {rednderEcharts(options);
};
</script><template><button @click="cli">返回</button><div id="canvas" width="400" height="400"></div>
</template>

在这里插入图片描述

例子 随着鼠标移动,legend显示具体的数值

 options = {tooltip: {},legend: {show: true,formatter: (value) => {console.log(value);if (value == "line") {return (value +":" +d1.reduce((pre, now) => {return pre + now;}));} else {return (value +":" +d2.reduce((pre, now) => {return pre + now;}));}},},xAxis: {type: "category",data: data.map((item) => item.data),},yAxis: {},series: [{type: "bar",name: "bar",data: d2,},{type: "line",name: "line",data: d1,},],};myEcharts.on("mouseout", "series.bar", (value) => {myEcharts.setOption({legend: {show: true,formatter: (value) => {console.log(value);if (value == "line") {return (value +":" +d1.reduce((pre, now) => {return pre + now;}));} else {return (value +":" +d2.reduce((pre, now) => {return pre + now;}));}},},});});myEcharts.on("mouseover", "series.bar", (value) => {console.log(value);let _data = value.data;let _index = value.dataIndex;let _linedata = d1[_index];myEcharts.setOption({legend: {formatter(value) {if (value == "line") {return value + ":" + _linedata;} else {return value + ":" + _data;}},},});});

在这里插入图片描述

动画

在这里插入图片描述

series: [{type: "bar",name: "bar",data: d2,animation: false,animationThreshold: 5,},{type: "line",name: "line",data: d1,},],
http://www.lryc.cn/news/357292.html

相关文章:

  • 备受推崇的公司文件加密文件推荐榜单
  • QT——QSlider实现,QT滑动控件的使用
  • 【网络协议Http】Http中get,post,put,delete区别
  • 软硬中断区别,磁盘块、扇区、页区别与之间的关系
  • 在线思维导图编辑!3个AI思维导图生成软件推荐!
  • 使用 Ubuntu + Docker + Vaultwarden + Tailscale 自建密码管理器
  • YOLOv7添加注意力机制和各种改进模块
  • 【OpenGL第一个程序】
  • GPT-4O神器来袭!自动生成Figma设计稿,移动端开发瞬间加速!
  • 清华大学提出IFT对齐算法,打破SFT与RLHF局限性
  • TS(TypeScript)中Array数组无法调出使用includes方法,显示红色警告
  • 基于Kafka的日志采集
  • 某烟草企业数字化转型物流信息化咨询项目规划方案(117页PPT)
  • 失落的方舟 命运方舟台服封号严重 游戏封IP怎么办
  • 2.10 mysql设置远程访问权限
  • C# 证件照替换底色与设置背景图---PaddleSegSharp
  • HCIA-HarmonyOS Device Developer 课程大纲
  • 洗地机哪个牌子最好用?十大名牌洗地机排行榜
  • Unity开发——XLua热更新之Hotfix配置(包含xlua获取与导入)
  • Qt 基于FFmpeg的视频转换器 - 转GIF动图
  • HTML新春烟花盛宴
  • 第十四届蓝桥杯c++研究生组
  • KDD 2024|基于隐空间因果推断的微服务系统根因定位
  • 白鹭群优化算法,原理详解,MATLAB代码免费获取
  • 【源码】2024完美运营版商城/拼团/团购/秒杀/积分/砍价/实物商品/虚拟商品等全功能商城
  • Java-数组内存解析
  • Spring Cache --学习笔记
  • NTP服务的DDoS攻击:原理和防御
  • 【面试干货】事务的并发问题(脏读、不可重复读、幻读)与解决策略
  • 函数式接口:现代编程的利器