2016-08-05 75 views
1

首先感謝HighCharts ..那麼,我正在使用jQuery Mobile中的HighCharts構建餅圖。Highcharts(PieChart) - 圖表寬度在第二次繪製時減少

我正在顯示一些根據響應數據在Ajax成功回調中動態創建的餅圖(最多6個圖表)。 Ajax最初在document.ready()中調用,並且還在按鈕單擊上。

在從的document.ready創建餅圖(),所有圖表顯示水平居中到屏幕上,這是一種預期的結果。但下次從按鈕單擊創建時,它向左移動一點,即其寬度比在document.ready()上顯示的寬度要小。

我正在爲餅圖動態創建每個容器<div id="container-x"></div>
這是我用來創建圖表的示例代碼。 dataArray是用於繪製餅圖的數據集。

$('#container' + i).highcharts({ 
chart: { 
    plotBackgroundColor: null, 
    plotBorderWidth: null, 
    plotShadow: false, 
    type: 'pie', 
    marginBottom: 150, 
    marginLeft: 20 
}, 
title: { 
    text: '' 
}, 
tooltip: { 
    pointFormat: '<b>{point.y:.0f}</b>' 
}, 
plotOptions: { 
    pie: { 
     allowPointSelect: true, 
     cursor: 'pointer', 
     showInLegend: true, 
     dataLabels: { 
      enabled: true, 
      useHTML: true, 
      formatter: function() { 
       return Math.round(this.percentage * 100)/100 + '%'; 
      }, 
      style: { 
       fontWeight: 'bold', 
       color: 'black' 
      } 
     } 
    } 
}, 
legend: { 
    layout: 'vertical', 
    align: 'left', 
    verticalAlign: 'bottom', 
    useHTML: true, 
    maxHeight: 135, 
    itemMarginTop: 2, 
    itemMarginBottom: 2, 
    labelFormatter: function() { 
     var words = this.name.split(/[\s]+/); 
     var numWordsPerLine = 4; 
     var str = []; 
     for (var word in words) { 
      if (word > 0 && word % numWordsPerLine == 0) { 
       str.push('<br>'); 
      } 
      str.push(words[word]); 
     } 
     return (str.slice(0, str.length - 2)).join(' '); 
    }, 
    navigation: { 
     activeColor: '#3E576F', 
     animation: true, 
     arrowSize: 12, 
     inactiveColor: '#CCC', 
     useHTML: true, 
     style: { 
      fontWeight: 'bold', 
      color: '#333', 
      fontSize: '12px' 
     } 
    } 
}, 
series: [{ 
    name: 'Brands', 
    colorByPoint: true, 
    data: dataArray 
}] 
}); 

附加以下

1.預期的兩個屏幕 - 最初加載餅圖
Expected and initially loaded pie chart

2.寬度減小 - 裝載在按鈕點擊
Width reduced

順便說一句,我有兩個div,一個有幾個過濾器和一個提交按鈕,另一個是顯示的圖表。當我需要顯示基於過濾器的圖表並顯示ajax響應時,我也隱藏顯示圖表的div。

JSFIDDLE

在此先感謝。

+0

了類似的回答,你試過將大小的餡餅?它可以幫助你解決你的問題:http://api.highcharts.com/highcharts#plotOptions.pie.size –

+0

@GrzegorzBlachliński:讓我試試看... – sadiqmc

+0

@GrzegorzBlachliński:它仍然是一樣的。:( – sadiqmc

回答