无法获取jqPlot图表来显示来自Variable的行

时间:2012-07-10 20:19:21

标签: javascript jqplot

我正在使用jqPlot在我的webApp中绘制一些点,所以我正在尝试这个:

var plot10 = $.jqplot ('heightChartDiv', [[3,7,9,1,5,3,8,2,5]]);

它工作正常,我这个确切的图表here

但是当我拿出它时,给它一个值,就像这样:

$(document).ready(function(){
var serie1 = [[3,7,9,1,5,3,8,2,5]];
}

function doGraph(){
 var plot10 = $.jqplot ('heightChartDiv', serie1);
}

它不起作用。我声明变量错了吗?请帮帮忙!

〜MYY

1 个答案:

答案 0 :(得分:1)

您的变量范围全部关闭。变量serie1具有$(document).ready事件中定义的匿名函数的局部范围。阅读javascript范围herehere

也许是这样的:

// the document ready will fire when the page is finished rendering
// inline javascript as you've done with your doGraph will fire as the page renders
$(document).ready(function(){

  // first define graph function
  // make the series an argument to the function
  doGraph = function(someSeries){
    var plot10 = $.jqplot ('heightChartDiv', someSeries);
  }

  // now call the function with the variable
  var serie1 = [[3,7,9,1,5,3,8,2,5]];
  doGraph(serie1);

}

对评论作出反应的编辑

见下面的例子:

$(document).ready(function(){

  var a = 1;

  someFunc = function(){
    var b = 2;
    alert(a);                   
  }

  someFunc();  // this works
  alert(b);  // this produces an error

});​

这里变量a被认为是函数someFunc的全局变量。但是,在someFunc中声明的变量不会在它之外持久存在。