解决 echarts myChart.resize不被调用问题

 $(function(){
    drawUnitBar();
   });
   
   $(window).resize(function(){
    //alert("width:"+$("#main-unitID").width()+"---"+"height:"+$("#main-unitID").height());
    //alert($(window).width()+":"+$(window).height());
    $("#main-unitID").width($(window).width()*0.75);
    $("#main-unitID").height($(window).height()*0.38);
    drawUnitBar();

   });





function drawUnitBar(){
    // 路径配置
       require.config({
           paths: {
               echarts: '../js/echarts/build/source'
           }
       });
       
       // 请求
       $.post("<%=request.getContextPath()%>/userUnitStatistics/userUnit.c", {}, function(dataStr){
              var data = dataStr;
              //alert(data);
              var data_yAxis = [];
              var series_data = [];
              for(var o in data){
                  data_yAxis.push(data[o].unitName);
                  series_data.push(data[o].count)
              }
              
              //alert(data_yAxis);
              //alert(series_data);
              // 使用
              require(
                  [
                      'echarts',
                      'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
                  ],
                  function (ec) {
                      // 基于准备好的dom,初始化echarts图表
                      var myChart = ec.init(document.getElementById('main-unitID')); 
                      
                      var option = {
                              title : {
                                  text: '',
                                  subtext: ''
                              },
                              tooltip : {
                                  trigger: 'axis'
                              },
                              legend: {
                                  data:['用户数']
                              },
                              toolbox: {
                                  show : false,
                                  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 : 'value',
                                      boundaryGap : [0, 0.01]
                                  }
                              ],
                              yAxis : [
                                  {
                                      type : 'category',
                                      data : data_yAxis
                                  }
                              ],
                              series : [
                                  {
                                      name:'用户数',
                                      type:'bar',
                                      data:series_data,
                                      itemStyle:{
                                          normal: {
                                               color:'#68BC31'
                                           }
                                      }
                                  }
                              ]
                          };
                          // 为echarts对象加载数据 
                          window.onresize = myChart.resize;
                          myChart.setOption(option); 
                      }
                  );
       });
   }

posted on 2015-06-15 11:56  _故乡的原风景  阅读(1620)  评论(0编辑  收藏  举报