2013-10-10 21 views
0

我正在嘗試做一個簡單的儀表板類型的頁面。我正在使用Highcharts進行可視化。爲了實現佈局,我使用jQueryUI可排序,特別是像例子那樣的portlet。我需要能夠調整一個特定的「portlet」,並調整圖表大小以適應。我用highcharts的setSize函數來做這件事。問題是我可以在頁面上擁有無限量的這些「portlet」,並且需要根據哪個「portlet」大小發生更改來調整大小。調整具體的圖表

我的HTML:

<html> 
<body> 
     <div class="column"> 
      <div class="portlet"> 
      <div class="portlet-header">Bar</div> 
      <div class="portlet-content" id="b_container"></div> 
      </div> 

      <div class="portlet"> 
      <div class="portlet-header">Pie</div> 
      <div class="portlet-content" id="Pie_Container"></div> 
      </div> 
     </div> 
     <div class="column"> 
      <div class="portlet"> 
      <div class="portlet-header">Line</div> 
      <div class="portlet-content" id="L_container"></div> 
      </div> 
     </div> 
     <div class="column"> 
      <div class="portlet" > 
      <div class="portlet-header">Gauge</div> 
      <div class="portlet-content" id="g_container"></div> 
      </div> 
      <div class="portlet"> 
      <div class="portlet-header">Time Chart</div> 
      <div class="portlet-content" id="t_container"></div> 
      </div> 
     </div> 
</body> 
    </html> 

和JS填補一些小工具:

$(function() { 
    $(".column").sortable({ 
     connectWith: ".column" 
    }); 
    $(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all") 
     .find(".portlet-header") 
     .addClass("ui-widget-header ui-corner-all") 
     .prepend("<span class='ui-icon ui-icon-minusthick'></span>") 
     .end() 
     .find(".portlet-content"); 

    $(".portlet-header .ui-icon").click(function() { 
     $(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick"); 
     $(this).parents(".portlet:first").find(".portlet-content").toggle(); 
    }); 

    $(".column").disableSelection(); 

    //pie chart 
    $('#Pie_Container').highcharts({ 
     chart: { 
      plotBackgroundColor: null, 
      plotBorderWidth: null, 
      plotShadow: false 
     }, 
     title: { 
      text: 'Browser market shares at a specific website, 2010' 
     }, 
     tooltip: { 
      pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' 
     }, 
     plotOptions: { 
      pie: { 
       allowPointSelect: true, 
       cursor: 'pointer', 
       dataLabels: { 
        enabled: false, 
        color: '#232323', 
        connectorColor: '#000000', 
        format: '<b>{point.name}</b>: {point.percentage:.1f} %' 
       }, 
       showInLegend: true 
      } 
     }, 
     series: [{ 
      type: 'pie', 
      name: 'Browser share', 
      data: [ 
       ['Firefox', 45.0], 
       ['IE',  26.8], 
       { 
        name: 'Chrome', 
        y: 12.8, 
        sliced: true, 
        selected: true 
       }, 
       ['Safari', 8.5], 
       ['Opera',  6.2], 
       ['Others', 0.7] 
      ] 
     }] 
    }); 
    //line chart 
    $('#L_container').highcharts({ 
     title: { 
      text: 'Monthly Average Temperature', 
      x: -20 //center 
     }, 
     subtitle: { 
      text: 'Source: WorldClimate.com', 
      x: -20 
     }, 
     xAxis: { 
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
       'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
     }, 
     yAxis: { 
      title: { 
       text: 'Temperature (°C)' 
      }, 
      plotLines: [{ 
       value: 0, 
       width: 1, 
       color: '#808080' 
      }] 
     }, 
     tooltip: { 
      valueSuffix: '°C' 
     }, 
     legend: { 
      layout: 'vertical', 
      align: 'right', 
      verticalAlign: 'middle', 
      borderWidth: 0 
     }, 
     series: [{ 
      name: 'Tokyo', 
      data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] 
     }, { 
      name: 'New York', 
      data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5] 
     }, { 
      name: 'Berlin', 
      data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0] 
     }, { 
      name: 'London', 
      data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8] 
     }] 
    }); 
    //gauge 
     var chart = new Highcharts.Chart({ 

     chart: { 
     type: 'gauge', 
     renderTo: 'g_container', 
     plotBackgroundColor: null, 
     plotBackgroundImage: null, 
     plotBorderWidth: 0, 
     plotShadow: false 
    }, 

    title: { 
     text: 'Speedometer' 
    }, 

    pane: { 
     startAngle: -150, 
     endAngle: 150, 
     background: [{ 
      backgroundColor: { 
       linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
       stops: [ 
        [0, '#FFF'], 
        [1, '#333'] 
       ] 
      }, 
      borderWidth: 0, 
      outerRadius: '109%' 
     }, { 
      backgroundColor: { 
       linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
       stops: [ 
        [0, '#333'], 
        [1, '#FFF'] 
       ] 
      }, 
      borderWidth: 1, 
      outerRadius: '107%' 
     }, { 
      // default background 
     }, { 
      backgroundColor: '#DDD', 
      borderWidth: 0, 
      outerRadius: '105%', 
      innerRadius: '103%' 
     }] 
    }, 

    // the value axis 
    yAxis: { 
     min: 0, 
     max: 200, 

     minorTickInterval: 'auto', 
     minorTickWidth: 1, 
     minorTickLength: 10, 
     minorTickPosition: 'inside', 
     minorTickColor: '#666', 

     tickPixelInterval: 30, 
     tickWidth: 2, 
     tickPosition: 'inside', 
     tickLength: 10, 
     tickColor: '#666', 
     labels: { 
      step: 2, 
      rotation: 'auto' 
     }, 
     title: { 
      text: 'km/h' 
     }, 
     plotBands: [{ 
      from: 0, 
      to: 120, 
      color: '#55BF3B' // green 
     }, { 
      from: 120, 
      to: 160, 
      color: '#DDDF0D' // yellow 
     }, { 
      from: 160, 
      to: 200, 
      color: '#DF5353' // red 
     }]   
    }, 

    series: [{ 
     name: 'Speed', 
     data: [80], 
     tooltip: { 
      valueSuffix: ' km/h' 
     } 
    }] 

    }, 
    // Add some life 
    function(chart) { 
     setInterval(function() { 
      var point = chart.series[0].points[0], 
       newVal, inc = Math.round((Math.random() - 0.5) * 20); 

      newVal = point.y + inc; 
      if (newVal < 0 || newVal > 200) { 
       newVal = point.y - inc; 
      } 

      point.update(newVal); 

     }, 3000); 

    }); 
    //bar chart 
    var colors = Highcharts.getOptions().colors, 
     categories = ['MSIE', 'Firefox', 'Chrome', 'Safari', 'Opera'], 
     name = 'Browser brands', 
     data = [{ 
       y: 55.11, 
       color: colors[0], 
       drilldown: { 
        name: 'MSIE versions', 
        categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'], 
        data: [10.85, 7.35, 33.06, 2.81], 
        color: colors[0] 
       } 
      }, { 
       y: 21.63, 
       color: colors[1], 
       drilldown: { 
        name: 'Firefox versions', 
        categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'], 
        data: [0.20, 0.83, 1.58, 13.12, 5.43], 
        color: colors[1] 
       } 
      }, { 
       y: 11.94, 
       color: colors[2], 
       drilldown: { 
        name: 'Chrome versions', 
        categories: ['Chrome 5.0', 'Chrome 6.0', 'Chrome 7.0', 'Chrome 8.0', 'Chrome 9.0', 
         'Chrome 10.0', 'Chrome 11.0', 'Chrome 12.0'], 
        data: [0.12, 0.19, 0.12, 0.36, 0.32, 9.91, 0.50, 0.22], 
        color: colors[2] 
       } 
      }, { 
       y: 7.15, 
       color: colors[3], 
       drilldown: { 
        name: 'Safari versions', 
        categories: ['Safari 5.0', 'Safari 4.0', 'Safari Win 5.0', 'Safari 4.1', 'Safari/Maxthon', 
         'Safari 3.1', 'Safari 4.1'], 
        data: [4.55, 1.42, 0.23, 0.21, 0.20, 0.19, 0.14], 
        color: colors[3] 
       } 
      }, { 
       y: 2.14, 
       color: colors[4], 
       drilldown: { 
        name: 'Opera versions', 
        categories: ['Opera 9.x', 'Opera 10.x', 'Opera 11.x'], 
        data: [ 0.12, 0.37, 1.65], 
        color: colors[4] 
       } 
      }]; 

    function setChart(name, categories, data, color) { 
     chart.xAxis[0].setCategories(categories, false); 
     chart.series[0].remove(false); 
     chart.addSeries({ 
      name: name, 
      data: data, 
      color: color || 'white' 
     }, false); 
     chart.redraw(); 
    } 

    var chart = $('#b_container').highcharts({ 
     chart: { 
      type: 'column' 
     }, 
     title: { 
      text: 'Browser market share, April, 2011' 
     }, 
     subtitle: { 
      text: 'Click the columns to view versions. Click again to view brands.' 
     }, 
     xAxis: { 
      categories: categories 
     }, 
     yAxis: { 
      title: { 
       text: 'Total percent market share' 
      } 
     }, 
     plotOptions: { 
      column: { 
       cursor: 'pointer', 
       point: { 
        events: { 
         click: function() { 
          var drilldown = this.drilldown; 
          if (drilldown) { // drill down 
           setChart(drilldown.name, drilldown.categories, drilldown.data, drilldown.color); 
          } else { // restore 
           setChart(name, categories, data); 
          } 
         } 
        } 
       }, 
       dataLabels: { 
        enabled: true, 
        color: colors[0], 
        style: { 
         fontWeight: 'bold' 
        }, 
        formatter: function() { 
         return this.y +'%'; 
        } 
       } 
      } 
     }, 
     tooltip: { 
      formatter: function() { 
       var point = this.point, 
        s = this.x +':<b>'+ this.y +'% market share</b><br/>'; 
       if (point.drilldown) { 
        s += 'Click to view '+ point.category +' versions'; 
       } else { 
        s += 'Click to return to browser brands'; 
       } 
       return s; 
      } 
     }, 
     series: [{ 
      name: name, 
      data: data, 
      color: 'white' 
     }], 
     exporting: { 
      enabled: false 
     } 
    }) 
    .highcharts(); // return chart 
    //time chart 
    $('#t_container').highcharts({ 
     chart: { 
      type: 'spline' 
     }, 
     title: { 
      text: 'Snow depth at Vikjafjellet, Norway' 
     }, 
     subtitle: { 
      text: 'Irregular time data in Highcharts JS' 
     }, 
     xAxis: { 
      type: 'datetime', 
      dateTimeLabelFormats: { // don't display the dummy year 
       month: '%e. %b', 
       year: '%b' 
      } 
     }, 
     yAxis: { 
      title: { 
       text: 'Snow depth (m)' 
      }, 
      min: 0 
     }, 
     tooltip: { 
      formatter: function() { 
        return '<b>'+ this.series.name +'</b><br/>'+ 
        Highcharts.dateFormat('%e. %b', this.x) +': '+ this.y +' m'; 
      } 
     }, 

     series: [{ 
      name: 'Winter 2007-2008', 
      // Define the data points. All series have a dummy year 
      // of 1970/71 in order to be compared on the same x axis. Note 
      // that in JavaScript, months start at 0 for January, 1 for February etc. 
      data: [ 
       [Date.UTC(1970, 9, 27), 0 ], 
       [Date.UTC(1970, 10, 10), 0.6 ], 
       [Date.UTC(1970, 10, 18), 0.7 ], 
       [Date.UTC(1970, 11, 2), 0.8 ], 
       [Date.UTC(1970, 11, 9), 0.6 ], 
       [Date.UTC(1970, 11, 16), 0.6 ], 
       [Date.UTC(1970, 11, 28), 0.67], 
       [Date.UTC(1971, 0, 1), 0.81], 
       [Date.UTC(1971, 0, 8), 0.78], 
       [Date.UTC(1971, 0, 12), 0.98], 
       [Date.UTC(1971, 0, 27), 1.84], 
       [Date.UTC(1971, 1, 10), 1.80], 
       [Date.UTC(1971, 1, 18), 1.80], 
       [Date.UTC(1971, 1, 24), 1.92], 
       [Date.UTC(1971, 2, 4), 2.49], 
       [Date.UTC(1971, 2, 11), 2.79], 
       [Date.UTC(1971, 2, 15), 2.73], 
       [Date.UTC(1971, 2, 25), 2.61], 
       [Date.UTC(1971, 3, 2), 2.76], 
       [Date.UTC(1971, 3, 6), 2.82], 
       [Date.UTC(1971, 3, 13), 2.8 ], 
       [Date.UTC(1971, 4, 3), 2.1 ], 
       [Date.UTC(1971, 4, 26), 1.1 ], 
       [Date.UTC(1971, 5, 9), 0.25], 
       [Date.UTC(1971, 5, 12), 0 ] 
      ] 
     }, { 
      name: 'Winter 2008-2009', 
      data: [ 
       [Date.UTC(1970, 9, 18), 0 ], 
       [Date.UTC(1970, 9, 26), 0.2 ], 
       [Date.UTC(1970, 11, 1), 0.47], 
       [Date.UTC(1970, 11, 11), 0.55], 
       [Date.UTC(1970, 11, 25), 1.38], 
       [Date.UTC(1971, 0, 8), 1.38], 
       [Date.UTC(1971, 0, 15), 1.38], 
       [Date.UTC(1971, 1, 1), 1.38], 
       [Date.UTC(1971, 1, 8), 1.48], 
       [Date.UTC(1971, 1, 21), 1.5 ], 
       [Date.UTC(1971, 2, 12), 1.89], 
       [Date.UTC(1971, 2, 25), 2.0 ], 
       [Date.UTC(1971, 3, 4), 1.94], 
       [Date.UTC(1971, 3, 9), 1.91], 
       [Date.UTC(1971, 3, 13), 1.75], 
       [Date.UTC(1971, 3, 19), 1.6 ], 
       [Date.UTC(1971, 4, 25), 0.6 ], 
       [Date.UTC(1971, 4, 31), 0.35], 
       [Date.UTC(1971, 5, 7), 0 ] 
      ] 
     }, { 
      name: 'Winter 2009-2010', 
      data: [ 
       [Date.UTC(1970, 9, 9), 0 ], 
       [Date.UTC(1970, 9, 14), 0.15], 
       [Date.UTC(1970, 10, 28), 0.35], 
       [Date.UTC(1970, 11, 12), 0.46], 
       [Date.UTC(1971, 0, 1), 0.59], 
       [Date.UTC(1971, 0, 24), 0.58], 
       [Date.UTC(1971, 1, 1), 0.62], 
       [Date.UTC(1971, 1, 7), 0.65], 
       [Date.UTC(1971, 1, 23), 0.77], 
       [Date.UTC(1971, 2, 8), 0.77], 
       [Date.UTC(1971, 2, 14), 0.79], 
       [Date.UTC(1971, 2, 24), 0.86], 
       [Date.UTC(1971, 3, 4), 0.8 ], 
       [Date.UTC(1971, 3, 18), 0.94], 
       [Date.UTC(1971, 3, 24), 0.9 ], 
       [Date.UTC(1971, 4, 16), 0.39], 
       [Date.UTC(1971, 4, 21), 0 ] 
      ] 
     }] 
    }); 
    $(".portlet").resizable({ 
    resize: function() { 
     chart.setSize(
      $(this).width() - 10, 
      $(this).height() - 60, 
      false 
     ); 
    }, 
    maxWidth: 301, 
    minWidth: 301 
    }); 
}); 

在JS的最後,你能看到我試圖建立的阻力和調整,但它並不完全正確。無論哪個小部件,我只拖動左上角的小部件大小。如何設置它,以便只調整大小的特定小部件更改?

+1

這裏是一個小提琴看到http://jsfiddle.net/XHLdp/ –

+0

當任何'.portlet's的被調整,你可以在同一個圖表上調用'setSize()' - 最後指定給'chart'變量的圖表。你需要做的是確定哪個圖表駐留在當前被調整大小的'.portlet'中,並且調用該特定圖表的'setSize()'。 –

+0

我遵循你所說的話,但我不確定如何編碼,任何提示?即使這是我需要搜索的東西? –

回答

1

正如我在評論中所說,您必須確定正確的圖表來調整大小,具體取決於當前調整的.portlet。我有Highcharts沒有經驗,但是這似乎在你的jsfiddle工作:

$(".portlet").resizable({ 
    resize: function() { 
     $(this).find('.portlet-content').highcharts().setSize(
      $(this).width() - 10, 
      $(this).height() - 60, 
      false 
     ); 
    }, 
    maxWidth: 301, 
    minWidth: 301 
});