没有静态id的jqPlot目标

时间:2012-01-16 18:19:28

标签: javascript jquery charts jqplot

我只是玩jqplot几个小时,但我找不到如何以更具体的jquery方式指定目标。 例如,如果我有html代码:

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

我可以使用

创建图表
 $.jqplot("chart", [], {});

它将在元素上创建一个图表,其中包含id:chart。

我想要的是使用这样的东西:

$("#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

我正在寻找什么解决方案?  感谢

2 个答案:

答案 0 :(得分:5)

通过查看code,您确实可以看到$.jqplot仅接受目标元素的ID作为第一个参数,因此您对此非常正确。

但是,$.fn.jqplot也已定义,这意味着您可以使用$(".multiple_charts").jqplot();$("<div></div>").jqplot();。请注意,jqplot为jquery对象中的每个元素创建一个唯一的id,如果它尚未存在的话。

哦,看起来我看的版本还没有,但你可以抓住最新的代码并制定解决方法。

答案 1 :(得分: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' 
            }
        }
    );