您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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; })() } ] };
關于“Bootstrap標簽頁插件切換echarts不顯示怎么辦”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。