创建echart多个联动的示例代码


Posted in Javascript onNovember 23, 2018
鼠标悬浮同时触发多个echart

效果如下

创建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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
HTML DOM的nodeType值介绍
Mar 31 Javascript
js点击事件链接的问题解决
Apr 25 Javascript
JavaScript设计模式之原型模式(Object.create与prototype)介绍
Dec 28 Javascript
基于JavaScript代码实现兼容各浏览器的设为首页和加入收藏
Jan 07 Javascript
详解javascript事件冒泡
Jan 09 Javascript
jQuery 调用WebService 实例讲解
Jun 28 Javascript
Bootstrap实现导航栏的2种方式
Nov 28 Javascript
Angular 表单控件示例代码
Jun 26 Javascript
浅谈js基础数据类型和引用类型,深浅拷贝问题,以及内存分配问题
Sep 02 Javascript
详解Webpack + ES6 最新环境搭建与配置
Jun 04 Javascript
微信小程序授权登录解决方案的代码实例(含未通过授权解决方案)
May 10 Javascript
jQuery - AJAX load() 实例用法详解
Aug 27 jQuery
详解Angular模板引用变量及其作用域
Nov 23 #Javascript
vue使用better-scroll实现下拉刷新、上拉加载
Nov 23 #Javascript
详解Vue2.0组件的继承与扩展
Nov 23 #Javascript
angular4自定义表单控件[(ngModel)]的实现
Nov 23 #Javascript
详解Angular中实现自定义组件的双向绑定的两种方法
Nov 23 #Javascript
Vue.js组件间通信方式总结【推荐】
Nov 23 #Javascript
vue-cli 2.*中导入公共less文件的方法步骤
Nov 22 #Javascript
You might like
Laravel 5框架学习之日期,Mutator 和 Scope
2015/04/08 PHP
laravel在中间件内生成参数并且传递到控制器中的2种姿势
2019/10/15 PHP
laravel框架分组控制器和分组路由实现方法示例
2020/01/25 PHP
javascript 模式设计之工厂模式详细说明
2010/05/10 Javascript
使用js对select动态添加和删除OPTION示例代码
2013/08/12 Javascript
js实现连续英文字符自动换行兼容ie6 ie7和firefox
2013/09/06 Javascript
Seajs的学习笔记
2014/03/04 Javascript
Nodejs为什么选择javascript为载体语言
2015/01/13 NodeJs
jQuery选择器源码解读(七):elementMatcher函数
2015/03/31 Javascript
AspNet中使用JQuery boxy插件的确认框
2015/05/20 Javascript
JS实现登录页面记住密码和enter键登录方法推荐
2016/05/10 Javascript
JS中sort函数排序用法实例分析
2016/06/16 Javascript
JS如何设置iOS中微信浏览器的title
2016/11/22 Javascript
不到200行 JavaScript 代码实现富文本编辑器的方法
2018/01/03 Javascript
vue element动态渲染、移除表单并添加验证的实现
2019/01/16 Javascript
vue实现路由监听和参数监听
2019/10/29 Javascript
vue-resource post数据时碰到Django csrf问题的解决
2020/03/13 Javascript
解决vscode进行vue格式化,会自动补分号和双引号的问题
2020/10/26 Javascript
[03:18]DOTA2放量测试专访820:希望玩家加入国服大家庭
2013/08/25 DOTA
[02:16]DOTA2英雄基础教程 干扰者
2014/01/15 DOTA
win7+Python3.5下scrapy的安装方法
2018/07/31 Python
OpenCV+python手势识别框架和实例讲解
2018/08/03 Python
Python math库 ln(x)运算的实现及原理
2019/07/17 Python
用python解压分析jar包实例
2020/01/16 Python
Python基于内置库pytesseract实现图片验证码识别功能
2020/02/24 Python
Python生成器generator原理及用法解析
2020/07/20 Python
canvas粒子动画背景的实现示例
2018/09/03 HTML / CSS
Html5应用程序缓存(Cache manifest)
2018/06/04 HTML / CSS
迪奥官网:Dior.com
2018/12/04 全球购物
阿拉伯书店:Jamalon
2019/07/24 全球购物
医生自荐信
2013/10/11 职场文书
中秋节主持词
2014/04/02 职场文书
汽车检测与维修专业求职信
2014/07/04 职场文书
教师三严三实心得体会
2014/10/11 职场文书
pandas数值排序的实现实例
2021/07/25 Python
vue 把二维或多维数组转一维数组
2022/04/24 Vue.js