1

在第二个选项卡上显示我的 Echarts 时遇到问题。该图表仅显示在第一个选项卡上,但在导航到第二个选项卡时不显示

                                <ul class="nav nav-tabs" style="margin-bottom: 15px;">
                                    <li class="active">
                                        <a href="#campaign" data-toggle="tab">Campaigns</a>
                                    </li>
                                    <li>
                                        <a href="#subscribers" data-toggle="tab">Subscribers</a>
                                    </li>
                            </ul>
                            <div id="myTabContent" class="tab-content">
                                <div class="tab-pane fade active in" id="campaign">
                                    <div id="pieChart" style="height:500px;border:1px solid #ccc;padding:10px;"></div>

                                </div>
                                <div class="tab-pane fade" id="subscribers">
                                    <div id="barChart" style="height:500px;border:1px solid #ccc;padding:10px;"></div>
                                </div>
                            </div>

然后这是显示图表的js

    var myChart = echarts.init(document.getElementById('pieChart'));
myChart.setTheme({color:['#6CC66C','#418BCA','#ff6600']});
pieChartOption =  option = {
    title : {
        text: 'Campaign Analysis',
        subtext: 'Jackpot',
        x:'center'
    },
    tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    legend: {
        orient : 'vertical',
        x : 'left',
        data:['Sent','Pending','Not Delivered']
    },
    toolbox: {
        show : true,
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            magicType : {
                show: true, 
                type: ['pie', 'funnel'],
                option: {
                    funnel: {
                        x: '25%',
                        width: '50%',
                        funnelAlign: 'left',
                        max: 1548
                    }
                }
            },
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    calculable : true,
    series : [
        {
            name:'Access Source',
            type:'pie',
            radius : '55%',
            center: ['50%', '60%'],
            data:[
                {value:{{ $no}}, name:'Sent'},
                {value:135, name:'Pending'},
                {value:155, name:'Not Delivered'}
            ]
        }
    ]
};
myChart.setOption(pieChartOption);



/*#########################         BARCHART            ##################################*/

var myBarChart = echarts.init(document.getElementById('barChart'));

var barChartOption = {
  title: {
    text: '某地区蒸发量和降水量',
    subtext: '纯属虚构'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['2014', '2015']
  },
  toolbox: {
    show: true,
    feature: {
      mark: {
        show: true
      },
      dataView: {
        show: true,
        readOnly: false
      },
      magicType: {
        show: true,
        type: ['line', 'bar']
      },
      restore: {
        show: true
      },
      saveAsImage: {
        show: true
      }
    }
  },
  calculable: true,
  xAxis: [{
    type: 'category',
    data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  }],
  yAxis: [{
    type: 'value'
  }],
  series: [{
    name: '2014',
    type: 'bar',
    data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
  }, {
    name: '2015',
    type: 'bar',
    data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
  }]
};

myBarChart.setOption(barChartOption);
/*#########################         BARCHART            ##################################*/

$(function() {
      $( "#tabs" ).tabs({
        select: function(event, ui) {
          console.log('Calling chart.invalidateSize()');
          chart.invalidateSize();
        }
      });

解决这个问题的方法是什么?

});
4

2 回答 2

4

ECharts 文档已经提到

有时图表可能会放置在多个选项卡中。隐藏标签中的那些可能由于对容器宽度和高度的忽略而无法初始化。因此,在切换到相应的选项卡时,应手动调用 resize 以获得正确的宽度和高度,或者在 opts 中明确指定宽度/高度。

所以每次切换到新标签时,只需在图表实例上调用 resize 函数即可:

chartInstance.resize()
于 2018-12-11T07:12:14.270 回答
2

答案有点晚了,我不确定你是否找到了解决方案,但解决这个问题的方法是结合 echartssetOption和 Bootstrap 的 Tab 事件。

喜欢-

$('#myTabItem').on('shown.bs.tab', function (e) {
         myChart.setOption(options);
})

显示 Bootstrap 选项卡后,上述代码将mychart使用 echarts回调重新加载图表。setOption可以在此处找到有关 Bootstrap Tab 事件的更多信息。

于 2017-05-03T03:44:21.097 回答