2012-01-16 12 views
2

我只是玩jqplot幾個小時,但我找不到如何以更具體的jQuery方式指定目標。 例如,如果我有HTML代碼:沒有靜態ID的jqPlot目標

<div id="chart"></div> 

我可以創建使用

$.jqplot("chart", [], {}); 

圖表,它會創建一個ID的元件上的圖表:圖表。

我想要的是用這樣的:

$("#chart").jqplot([], {}); 

$(".multiple_charts").jqplot([], {}); 

var myChart=$("<div></div>"); 
myChart.jqplot([], {}); 

我看到這個問題在2009年已經在這裏建議:https://bitbucket.org/cleonello/jqplot/issue/114/jqplot-target-should-accept-any-element

有什麼我正在尋找的解決方案? 謝謝

回答

5

從看code,你的確可以看到,$.jqplot只接受目標元素的id作爲第一個參數,所以你是對的。

但是$.fn.jqplot也被定義,這意味着你可以使用$(".multiple_charts").jqplot();$("<div></div>").jqplot();。請注意,jqplot爲jquery對象中的每個元素創建一個唯一的id,如果它尚不存在。

哦,它看起來像我看的版本還沒有出來,但你可以抓住最新的代碼,並制定一個解決方法。

+0

如何在使用'$(「...」).jqplot();而不是'$ .jqplot(...)'方法時獲得相同的'jqplot'對象?我需要訪問'plugins'屬性和方法,比如'destroy()'等等。 – apdevelop

1

只是跟進

HTML

<div id="chart2" class="test2" style="margin-top:20px; margin-left:20px; width:460px; height:300px;"></div> 

數據

var data1 = [ 
      ['Verwerkende industrie', 9], 
      ['Retail', 3], 
      ['Primaire producent', 4], 
      ['Out of home', 2], 
      ['Groothandel', 7], 
      ['Grondstof', 9], 
      ['Consument', 3], 
      ['Bewerkende industrie', 2] 
     ]; 

所以不是這樣

var plot2 = $.jqplot($('chart2'), [ data1 ], { 
      title: ' ', 
      seriesDefaults: { 
       shadow: false, 
       renderer: jQuery.jqplot.PieRenderer, 
       rendererOptions: { 
        startAngle: 180, 
        sliceMargin: 4, 
        showDataLabels: true 
       } 
      }, 
      legend: { 
       show:true, location: 'w' 
      } 
     } 
    ); 

你可以做到這一點

$(".test2").jqplot([data1] , { 
      title: ' ', 
      seriesDefaults: { 
       shadow: false, 
       renderer: jQuery.jqplot.PieRenderer, 
       rendererOptions: { 
        startAngle: 180, 
        sliceMargin: 4, 
        showDataLabels: true 
       } 
      }, 
      legend: { 
       show:true, location: 'w' 
      } 
     } 
    );