欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Bootstrap标签页(Tab)插件切换echarts不显示问题的解决

程序员文章站 2023-11-09 10:31:34
问题: 1.  在echarts跟bootstrap选项卡整合的时候,默认第一个选中选项卡可以正常加载echarts图表,但是切换其他选项的时候不能渲染出其他选...

问题:

1.  在echarts跟bootstrap选项卡整合的时候,默认第一个选中选项卡可以正常加载echarts图表,但是切换其他选项的时候不能渲染出其他选项卡echarts图表。

2.  echarts自适应怎么实现?

解决办法:

//解决tab切换不显示问题 在加载窗口后重新渲染。
 $('a[data-toggle="pill"]').on('shown.bs.tab', function(e) {
 for(var i = 0; i < charts.length; i++) {
 charts[i].resize();
 }
 }); //data-toggle="pill还是data-toggle="tab" 根据前面所用标签页的不同来选择
//echarts自适应
 var charts = [];
 var chart1 = echarts.init(document.getelementbyid("left1"));
 var chart2 = echarts.init(document.getelementbyid("left2"));
 var chart3 = echarts.init(document.getelementbyid("right11"));
 var chart4 = echarts.init(document.getelementbyid("right12"));
 var chart5 = echarts.init(document.getelementbyid("right21"));
 var chart6 = echarts.init(document.getelementbyid("right22"));
 chart1.setoption(option1);
 chart2.setoption(option1);
 chart3.setoption(option2);
 chart4.setoption(option2);
 chart5.setoption(option1);
 chart6.setoption(option1);
 charts.push(chart1);
 charts.push(chart2);
 charts.push(chart3);
 charts.push(chart4);
 charts.push(chart5);
 charts.push(chart6);
 $(window).resize(function() {
 for(var i = 0; i < charts.length; i++) {
 charts[i].resize();
 }
 })

下面看一个实现的demo:

            demo 完成内容:

                                      1. bootstrap实现响应式布局

                                      2. echats实现自适应

                                      3. 标签页切换、缩放正常显示

<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
 <title></title>
 <meta http-equiv="x-ua-compatible" content="ie=edge">
 <!--设置视口的宽度(值为设备的理想宽度),页面初始缩放值<理想宽度/可见宽度>-->
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" rel="external nofollow" />
 <link rel="stylesheet" href="../css/main.css" rel="external nofollow" />
 <!-- 可选的bootstrap主题文件(一般不用引入) -->
 <link rel="stylesheet" href="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/css/bootstrap-theme.min.css" rel="external nofollow" >
 <title></title>
 <!-- jquery文件。务必在bootstrap.min.js 之前引入 -->
 <script src="../js/jquery-3.1.1.js"></script>
 <!-- 最新的 bootstrap 核心 javascript 文件 -->
 <script src="../js/bootstrap.min.js"></script>
 <!-- 最新的echarts核心 javascript 文件 -->
 <script src="../js/echarts.js"></script>
 <script src="../js/main.js" type="text/javascript" charset="utf-8"></script>
 </head>
 
 <body>
 <div class="container">
 <div class="row">
 <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 col-left">
  <div class="css_height1" id="left1">
  </div>
  <div class="css_height" id="left2">
  </div>
 </div>
 <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 col-right">
  <div class="right_title">
  <ul class="nav nav-pills">
  <li class="active">
  <a href="#right1" rel="external nofollow" data-toggle="pill">1</a>
  </li>
  <li>
  <a href="#right2" rel="external nofollow" data-toggle="pill">2</a>
  </li>
  <li>
  <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-toggle="pill">3</a>
  </li>
  <li>
  <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-toggle="pill">4</a>
  </li>
  <li>
  <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-toggle="pill">5</a>
  </li>
  </ul>
  </div>
  <div class="tab-content">
  <div class="tab-pane fade in active" id="right1">
  <div class="css_height" id="right11">
 
  </div>
  <div class="css_height" id="right12">
 
  </div>
  </div>
  <div class="tab-pane fade in" id="right2">
  <div class="css_height" id="right21">
 
  </div>
  <div class="css_height" id="right22">
 
  </div>
  </div>
  </div>
 </div>
 </div>
 </div>
 </body>
</html>
<script type="text/javascript">
 var charts = [];
 var chart1 = echarts.init(document.getelementbyid("left1"));
 var chart2 = echarts.init(document.getelementbyid("left2"));
 var chart3 = echarts.init(document.getelementbyid("right11"));
 var chart4 = echarts.init(document.getelementbyid("right12"));
 var chart5 = echarts.init(document.getelementbyid("right21"));
 var chart6 = echarts.init(document.getelementbyid("right22"));
 chart1.setoption(option1);
 chart2.setoption(option1);
 chart3.setoption(option2);
 chart4.setoption(option2);
 chart5.setoption(option1);
 chart6.setoption(option1);
 charts.push(chart1);
 charts.push(chart2);
 charts.push(chart3);
 charts.push(chart4);
 charts.push(chart5);
 charts.push(chart6);
 $(window).resize(function() {
 for(var i = 0; i < charts.length; i++) {
 charts[i].resize();
 }
 });
 //解决tab切换不显示问题 在加载窗口后重新渲染。
 $('a[data-toggle="pill"]').on('shown.bs.tab', function(e) {
 for(var i = 0; i < charts.length; i++) {
 charts[i].resize();
 }
 });
</script>
.css_height{
 height: 400px;
 margin-top: 30px;
}
.css_height1{
 height: 400px;
 margin-top: 72px;
}
.right_title{
 width: 90%;
 margin-left: 10%;
}
.nav>li>a{
 padding: 5px 18px;
 margin-top: 10px;
}
body{
 background-color:#009688;
}
.container{
 background-color:#dcedf5;
 margin-top: 25px;
 border-radius: 15px;
}
option1= {
 title: {
  text: '未来一周气温变化',
  subtext: '纯属虚构'
 },
 tooltip: {
  trigger: 'axis'
 },
 xaxis: {
  type: 'category',
  boundarygap: false,
  data: ['周一','周二','周三','周四','周五','周六','周日']
 },
 yaxis: {
  type: 'value',
  axislabel: {
   formatter: '{value} °c'
  }
 },
 series: [  {   name:'最高气温',   type:'line',   data:[11, 11, 15, 13, 12, 13, 10],
   markpoint: {
    data: [
     {type: 'max', name: '最大值'},
     {type: 'min', name: '最小值'}
    ]
   },
   markline: {
    data: [
     {type: 'average', name: '平均值'}
    ]
   }
  },
  {
   name:'最低气温',
   type:'line',
   data:[1, -2, 2, 5, 3, 2, 0],
   markpoint: {
    data: [
     {name: '周最低', value: -2, xaxis: 1, yaxis: -1.5}
    ]
   },
   markline: {
    data: [
     {type: 'average', name: '平均值'},
     [{      symbol: 'none',      x: '90%',      yaxis: 'max'     }, {      symbol: 'circle',      label: {       normal: {        position: 'start',        formatter: '最大值'       }      },      type: 'max',      name: '最高点'     }]
    ]
   }
  }
 ]
};
option2 = {
 title: {
  text: '大规模散点图'
 },
 legend: {
  data:['sin','cos']
 },
 xaxis : [  {   type : 'value',   scale:true  } ],
 yaxis : [  {   type : 'value',   scale:true  } ],
 series : [  {   name:'sin',   type:'scatter',   large: true,   symbolsize: 3,   data: (function () {    var d = [];
    var len = 10000;
    var x = 0;
    while (len--) {
     x = (math.random() * 10).tofixed(3) - 0;
     d.push([
      x,
      //math.random() * 10
      (math.sin(x) - x * (len % 2 ? 0.1 : -0.1) * math.random()).tofixed(3) - 0
     ]);
    }
    //console.log(d)
    return d;
   })()
  },
  {
   name:'cos',
   type:'scatter',
   large: true,
   symbolsize: 2,
   data: (function () {
    var d = [];
    var len = 20000;
    var x = 0;
    while (len--) {
     x = (math.random() * 10).tofixed(3) - 0;
     d.push([
      x,
      //math.random() * 10
      (math.cos(x) - x * (len % 2 ? 0.1 : -0.1) * math.random()).tofixed(3) - 0
     ]);
    }
    //console.log(d)
    return d;
   })()
  }
 ]
};

demo效果链接: 

github源码:   | 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。