首页 > 其他分享 >vuejs+echarts实现时间轴

vuejs+echarts实现时间轴

时间:2023-12-28 16:27:17浏览次数:30  
标签:option show vuejs myChart params 时间轴 true echarts

1、效果图

2、具体需求描述

①可以设置时间轴起始值;

②时间轴可以缩放、左右拖动;

③鼠标移入时间轴显示当前刻度信息;

④点击时间轴时添加蓝色图标,鼠标移入图标显示此时图标信息且隐藏刻度信息,按住图标可以拖动图标;

3、实现

①结构代码

<div id="timeAxisEchart" style="width:100%;height: 50px;"></div> ②配置图表 data () {     return {       myChart: null,       option: {         grid: {           show: true,           top: 0,           left: 30,           right: 30,           bottom: 0,           borderWidth: 0,           cursor: 'pointer',         },         xAxis: {           type: 'time',           maxInterval: 3600 * 1000 * 24 * 30 * 12,           minInterval: 1000 * 60 * 2,           axisLine: {             // 轴线             show: true,             lineStyle: {               width: 2,             },           },           axisTick: {             // 轴刻度             show: true,             alignWithLabel: true,           },           offset: -26,           axisPointer: {             // 坐标轴指示器配置项             show: true,             label: {               show: true,               margin: -26,             },           }         },         yAxis: {           show: false,         },         tooltip: {           show: true,           formatter: (value) => {             return value.data ? this.dateFormatter("yyyy-MM-dd HH:mm:ss", new Date(parseInt(value.data))) : '';           },           padding: [2, 4],           transitionDuration: 0,           position: function(point, params, dom, rect, size) {               //其中point为当前鼠标的位置,size中有两个属性:viewSize和contentSize,分别为外层div和tooltip提示框的大小               var x = point[0]; //               var y = point[1];               var boxWidth = size.contentSize[0];               var boxHeight = size.contentSize[1];               var posX = 0; //x坐标位置               var posY = 0; //y坐标位置               if (x < boxWidth) { //左边放不开                   posX = 5;               } else { //左边放的下                   posX = x - boxWidth - 10;               }               if (y < boxHeight) { //上边放不开                   posY = -26;               } else { //上边放得下                   posY = y - boxHeight;               }               return [posX, posY];           }         },         series: [           {             type: 'scatter',             symbol: 'pin',             symbolSize: 26,             symbolOffset: [0, 26],             itemStyle: {
            },             data: [],             z:999,           }         ],         dataZoom: [           {             type: 'inside',             xAxisIndex: 0,           },         ],       },     };   }, ③初始化图表     initEchart (time) {       let that = this;       let draggerStatus = false;       this.myChart = null;       this.myChart = this.$echarts.init(document.getElementById("timeAxisEchart"));       this.option.xAxis.min = new Date(time[0]).getTime();       this.option.xAxis.max = new Date(time[1]).getTime();       this.option.series[0].data = [new Date(time[0]).getTime()];       this.myChart.setOption(this.option, true);       window.addEventListener("resize", () => {         that.myChart.resize();       });       // 鼠标移入item不显示axisPointer       that.myChart.on('mouseover', params => {         that.option.xAxis.axisPointer.show = false;         that.myChart.setOption({ xAxis: that.option.xAxis });       });       that.myChart.on('mouseout', params => {         // draggerStatus = false;         that.option.xAxis.axisPointer.show = true;         that.myChart.setOption({ xAxis: that.option.xAxis });       });       // 拖动series数据       that.myChart.on('mousedown', params => {         draggerStatus = true;    that.option.dataZoom[0].moveOnMouseMove = false;//拖动时禁止时间轴拖动(看具体需求是否需要)         that.myChart.setOption({ dataZoom: that.option.dataZoom });       });       that.myChart.getZr().on('mousemove', params => {         const pointInPixel = [params.offsetX, params.offsetY]         if (that.myChart.containPixel('grid', pointInPixel) && draggerStatus) {           const xIndex = that.myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0];           that.option.series[0].data = [new Date(parseInt(xIndex)).getTime()];           that.myChart.setOption({ series: that.option.series });         }       });       window.onmouseup = ()=> {         if(draggerStatus){           //逻辑操作         }         draggerStatus = false;    that.option.dataZoom[0].moveOnMouseMove = true;//时间轴可拖动         that.myChart.setOption({ dataZoom: that.option.dataZoom });       };       // 时间轴点击事件       that.myChart.getZr().on('click', params => {         const pointInPixel = [params.offsetX, params.offsetY]         if (that.myChart.containPixel('grid', pointInPixel)) {           const xIndex = that.myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0];           that.option.series[0].data = [new Date(parseInt(xIndex)).getTime()];           that.myChart.setOption({ series: that.option.series });    //逻辑操作         }       });     }, 4、目前拖动色块时不会紧随鼠标移动,待优化。

标签:option,show,vuejs,myChart,params,时间轴,true,echarts
From: https://www.cnblogs.com/wangyan0926/p/17932924.html

相关文章

  • vuejs+echarts实现x轴为时间轴且数据区域可缩放
    1、效果图2、具体功能描述①选择的时间为时间轴的开始和结束时间;②鼠标可以左右拖动x轴时间轴;③鼠标放入图表中可以缩放数据,x轴会相应的更改当前坐标轴间隔值,最小间隔值为1分钟,最大间隔值为1年,且在缩放时可以获取到数据窗口范围的起始数值;④点击y轴名称会对相应数据显示隐......
  • vue3横向时间轴展示
    架子是用的vue3+elementPlus,要用到时间轴展示,但element组件只有竖着的,想要横着的,找了一圈没有合适的,终于找到个合适的,文章原址https://blog.csdn.net/m0_62949703/article/details/127800712数据结构: {date:'2023-09-27’,isShow:true,children:[......
  • wpf + LiveCharts.wpf 做个漂亮的图表
    十年河东,十年河西,莫欺少年穷学无止境,精益求精参考:WPFLiveChart图表详解接着上一篇博客: wpf+MaterialDesign+Prism8实现导航功能 1、项目引入图表包 2、定义用户控件IndexView的IndexViewModel,如下usingLiveCharts;usingPrism.Mvvm;usingSystem;usingSystem.Col......
  • echarts在vue中不显示中国地图
    遇到的情况是在开发中,中国地图是正常显示的但是打包之后,放到服务器,地图就不显示了,经过搜索,说是因为低版本的echarts需要手动添加map首先找到这个文件然后手动写上这个最后重新打包试试吧......
  • eCharts记录一柱形图案例
     option={color:'#8AE6C7',grid:{left:'50',right:'50',bottom:'50',containLabel:true},textStyle:{color:'rgba(0,0,0,.58)'},xAxis:{type:'catego......
  • wpf + LiveCharts.wpf 做个漂亮的图表
    十年河东,十年河西,莫欺少年穷学无止境,精益求精参考:WPFLiveChart图表详解接着上一篇博客: wpf+MaterialDesign+Prism8实现导航功能 1、项目引入图表包 2、定义用户控件IndexView的IndexViewModel,如下usingLiveCharts;usingPrism.Mvvm;usingSystem;using......
  • echarts画桑基图,并根据选择的分析因子画图
    最近要在系统中增加一个桑基图,要求桑基图可以根据选择的分析因子重新绘图。仔细看了下echarts的示例,桑基图本身很简单,困难的是如何根据数据资料整理成桑基图的数据格式,并且实现选择分析因子重新绘图。研究了几天的数据资料,特将方法写个demo记录下。<scriptsrc="./plugins/ech......
  • echarts柱形图给X轴坐标类目添加点击事件
    在项目中遇到这么个需求要在柱形图上的x轴添加点击事件,当点击对应x轴文字的时候要弹出模态框展示子图表根据echarts的Api给图表实例绑定点击事件myChartInstance?.on('click','xAxis.category',(params)=>{if(params.value==='其他变动成本'){set......
  • v-echarts 使用折线图
    <ve-linestyle="top:-40px"height="100%"width="100%":loading="yearChartLoading":data="yearChartData":extend="chartExtend":legend-visible="false":settings="yearSetting......
  • echarts设置多条折线不是你想的那样简单
    简单的多条折线图小伙伴写过多条折线图的都知道,常见的折线图是xAxis配置项下的data属性上设置时间或者日期。series配置项下是对应的legend中的数据以及该条折线的数据。<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"con......