创建echart多个联动的示例代码
程序员文章站
2022-06-22 15:42:41
鼠标悬浮同时触发多个echart
效果如下
html代码
鼠标悬浮同时触发多个echart
效果如下
html代码
<div class="contain"> <div class="sel"> <div class="sel1"> <div class="top"> <span>选择时间间隔</span> <div class="show"> <span>one second</span> <i class="glyphicon glyphicon-chevron-right"></i> </div> </div> <div class="block"> <div leap="second">one second</div> <div leap="minute">one minute</div> <div leap="hour">one hour</div> <div leap="day">one day</div> <div leap="week">one week</div> <div leap="month">one month</div> <div class="active" leap="year">one year</div> </div> </div> <div class="sel2"> <div class="top"> <span>选择数据个数</span> <div class="show"> <span>5</span> <i class="glyphicon glyphicon-chevron-right"></i> </div> </div> <div class="block"> <div leap="5">5</div> <div leap="10">10</div> <div leap="15">15</div> <div leap="20">20</div> <div leap="25">25</div> <div leap="30">30</div> <div leap="35">35</div> </div> </div> <div class="zybtn">确定</div> </div> <!-- 为echarts准备一个具备大小(宽高)的dom --> <div id="main" style="width: 1000px;height:300px;margin-top:45px;"></div> <div id="main2" style="width: 1000px;height:300px;"></div> </div>
css代码
body { margin: 0; padding: 0; background-color: #eee; } .contain { padding: 10px; } .sel { position: absolute; height: 250px; z-index: 10; } .sel>div { float: left; margin-right: 10px; } .top span { display: inline-block; width: 100px; height: 30px; } .top .show { width: 125px; height: 25px; float: right; border: 1px solid #444; padding-left: 5px; position: relative; } .block { float: right; position: relative; border-radius: 5px; padding: 5px; width: 120px; box-shadow: 1px 1px 5px #444; display: none; background-color: #fff; } .block>div { height: 25px; line-height: 25px; padding-left: 5px; border-radius: 5px; cursor: pointer; } .block>div:hover { background-color: #d3e3e5; } .block>div.active { background-color: #087c90; color: #fff; } .zybtn { width: 80px; height: 30px; border-radius: 5px; background-color: #46ad08; line-height: 30px; text-align: center; color: #fff; cursor: pointer; } i.glyphicon { top: 3px; right: 5px }
js代码
function drawchart(timearr, dataarr) { // 基于准备好的dom,初始化echarts实例 var mychart1 = echarts.init(document.getelementbyid('main')); var mychart2 = echarts.init(document.getelementbyid('main2')); // 指定图表的配置项和数据 var option1 = { title: { text: 'chart1' }, tooltip: { show: true, trigger: 'axis', axispointer: { type: 'line' }, linestyle: { color: '#000', } }, legend: { data: ['销量1'] }, grid:{ y2:140 }, xaxis: [{ type: 'category', data: timearr, axislabel: { interval: 0, //横轴信息全部显示 rotate: -45, //-30度角倾斜显示 } }], yaxis: [{ type: 'value', }], series: [{ name: '销量1', type: 'line', data: dataarr }] }; var option2 = { title: { text: 'chart2' }, tooltip: { show: true, trigger: 'axis', axispointer: { type: 'line' }, linestyle: { color: '#000', } }, legend: { data: ['销量2'] }, grid:{ y2:140 }, xaxis: [{ type: 'category', data: timearr, axislabel: { interval: 0, //横轴信息全部显示 rotate: -45, //-30度角倾斜显示 } }], yaxis: [{ type: 'value', }], series: [{ name: '销量2', type: 'line', data: dataarr }] }; // 为echarts对象加载数据 mychart1.setoption(option1); mychart2.setoption(option2); //联动配置 // 分别设置每个实例的 group id mychart1.group = 'group1'; mychart2.group = 'group1'; echarts.connect('group1'); // 或者可以直接传入需要联动的实例数 // echarts.connect([mychart1,mychart2]); } // 获取x轴时间字符串 function gettimestr(tseconds) { var str = ''; var year = new date(tseconds).getfullyear(); var month = new date(tseconds).getmonth() + 1; var date = new date(tseconds).getdate(); var hour = new date(tseconds).gethours(); var minute = new date(tseconds).getminutes(); var second = new date(tseconds).getseconds(); if (month < 10) { month = "0" + month } if (date < 10) { date = "0" + date } if (hour < 10) { hour = "0" + hour } if (minute < 10) { minute = "0" + minute } if (second < 10) { second = "0" + second } str += year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second; return str; } getsel() // 获取两个数值 function getsel() { $("i").on("click", function () { var that = $(this); var block = that.parents(".top").next(); // 点击i触发函数,判断类型 if ($(this).hasclass("glyphicon-chevron-right")) { $(this).removeclass("glyphicon-chevron-right"); $(this).addclass("glyphicon-chevron-down") block.children("div").each(function () { $(this).removeclass("active") }); block.slidedown(); } else if ($(this).hasclass("glyphicon-chevron-down")) { $(this).removeclass("glyphicon-chevron-down"); $(this).addclass("glyphicon-chevron-right") block.slideup() } block.children("div").on("click", function () { $(this).addclass("active"); that.prev("span").html($(this).html()) that.removeclass("glyphicon-chevron-down"); that.addclass("glyphicon-chevron-right") block.slideup() }); }); var val1 = 1000; var val2 = 5; $(".zybtn").on("click", function () { switch ($(".sel1 .show span").html()) { case 'one second': val1 = 1000; break; case 'one minute': val1 = 1000 * 60; break; case 'one hour': val1 = 1000 * 3600; break; case 'one day': val1 = 1000 * 3600 * 24; break; case 'one week': val1 = 1000 * 3600 * 24 * 7; break; case 'one month': val1 = 1000 * 3600 * 24 * 30; break; case 'one year': val1 = 1000 * 3600 * 24 * 365; break; } switch ($(".sel2 .show span").html()) { case '5': val2 = 5; break; case '10': val2 = 10; break; case '15': val2 = 15; break; case '20': val2 = 20; break; case '25': val2 = 25; break; case '30': val2 = 30; break; case '35': val2 = 35; break; } changedata(val1, val2) }) changedata(val1, val2) } function changedata(sel1, sel2) { // 获取当前日期 var getdate = new date(); var tseconds = getdate.gettime(); var timearr = []; var dataarr = []; for (var i = 0; i < sel2; i++) { timearr.push(gettimestr(tseconds - sel1 * i)) dataarr.push(math.ceil(math.random() * 10)) } drawchart(timearr, dataarr) }
ps:echart多表联动
<!doctype html> <head> <meta charset="utf-8"> <title>echarts</title> </head> <body> <!-- 为echarts准备一个具备大小(宽高)的dom --> <div id="main1" style="height:200px"></div> <div id="main2" style="height:200px"></div> <div id="main3" style="height:200px"></div> <div id="main4" style="height:200px"></div> <!-- echarts单文件引入 --> <script src="http://echarts.baidu.com/build/dist/echarts.js"></script> <script type="text/javascript"> // 路径配置 require.config({ paths: { echarts: 'http://echarts.baidu.com/build/dist' } }); // 使用 require( [ 'echarts', 'echarts/chart/bar', // 使用柱状图就加载bar模块,按需加载 'echarts/chart/line' ], function (ec) { // 基于准备好的dom,初始化echarts图表 var mychart1 = ec.init(document.getelementbyid('main1')); var mychart2 = ec.init(document.getelementbyid('main2')); var mychart3 = ec.init(document.getelementbyid('main3')); var mychart4 = ec.init(document.getelementbyid('main4')); mychart1.settheme("macarons"); mychart2.settheme("macarons"); mychart3.settheme("macarons"); mychart4.settheme("macarons"); var option1 = { title : { text: '温度状况', subtext: '纯属虚构' }, tooltip : { trigger: 'axis' }, legend: { data:['设定温度','进水温度','出水温度','环境温度'] }, toolbox: { show : true, feature : { mark : {show: true}, dataview : {show: true, readonly: false}, magictype : {show: true, type: ['line', 'bar', 'stack', 'tiled']}, restore : {show: true}, saveasimage : {show: true} } }, xaxis : [ { type : 'category', //x轴为类目类型 axislabel:{ show:true, interval:0, rotate:45 }, data : ['00:00:00','00:05:00','00:10:00','00:15:00','00:20:00','00:25:00','00:30:00','00:35:00','00:40:00','00:45:00'] }], yaxis : [ { type : 'value' //y轴为值类型 } ], series : [{ name:'设定温度', type:'line', smooth:true, data:[55,55,55,55,55,55,55,55,55,55] }] } var option2 = { tooltip : { trigger: 'axis' }, legend: { y:-30, data:['设定温度','进水温度','出水温度','环境温度'] }, toolbox: { y : -30, show : true, feature : { mark : {show: true}, dataview : {show: true, readonly: false}, magictype : {show: true, type: ['line', 'bar', 'stack', 'tiled']}, restore : {show: true}, saveasimage : {show: true} } }, xaxis : [ { type : 'category', //x轴为类目类型 axislabel:{ show:true, interval:0, rotate:45 }, data : ['00:00:00','00:05:00','00:10:00','00:15:00','00:20:00','00:25:00','00:30:00','00:35:00','00:40:00','00:45:00'] }], yaxis : [ { type : 'value' //y轴为值类型 } ], series : [{ name:'进水温度', type:'line', smooth:true, data:[15,15,16,18,18,19,19,19,19,19] }] } var option3 = { tooltip : { trigger: 'axis' }, legend: { y : -30, data:['设定温度','进水温度','出水温度','环境温度'] }, toolbox: { y : -30, show : true, feature : { mark : {show: true}, dataview : {show: true, readonly: false}, magictype : {show: true, type: ['line', 'bar', 'stack', 'tiled']}, restore : {show: true}, saveasimage : {show: true} } }, xaxis : [ { type : 'category', //x轴为类目类型 axislabel:{ show:true, interval:0, rotate:45 }, data : ['00:00:00','00:05:00','00:10:00','00:15:00','00:20:00','00:25:00','00:30:00','00:35:00','00:40:00','00:45:00'] }], yaxis : [ { type : 'value' //y轴为值类型 } ], series : [{ name:'出水温度', type:'line', smooth:true, data:[20,25,30,35,38,44,46,48,53,56] }] } var option4 = { tooltip : { trigger: 'axis' }, legend: { y : -30, data:['设定温度','进水温度','出水温度','环境温度'] }, toolbox: { y : -30, show : true, feature : { mark : {show: true}, dataview : {show: true, readonly: false}, magictype : {show: true, type: ['line', 'bar', 'stack', 'tiled']}, restore : {show: true}, saveasimage : {show: true} } }, xaxis : [ { type : 'category', //x轴为类目类型 axislabel:{ show:true, interval:0, rotate:45 }, data : ['00:00:00','00:05:00','00:10:00','00:15:00','00:20:00','00:25:00','00:30:00','00:35:00','00:40:00','00:45:00'] }], yaxis : [ { type : 'value' //y轴为值类型 } ], series : [{ name:'环境温度', type:'line', smooth:true, data:[15,15,15,15,15,15,15,15,15,15] }] } // 为echarts对象加载数据 mychart1.setoption(option1); mychart2.setoption(option2); mychart3.setoption(option3); mychart4.setoption(option4); //联动配置 mychart1.connect([mychart2, mychart3,mychart4]); mychart2.connect([mychart1, mychart3,mychart4]); mychart3.connect([mychart2, mychart1,mychart4]); mychart4.connect([mychart2, mychart3,mychart1]); } ); </script> </body>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: NEC参加2012云计算世界峰会亚洲论坛
下一篇: 微信小程序带动画弹窗组件使用方法详解