2010-07-16 25 views
0

嘿,大家好,我正在创建一个小班来帮助我使用Google可视化API。你可以看到它是如何工作在这里......使用Google可视化API时,'预期'对象'

http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html

下面是谷歌的实现。

google.load('visualization', '1', {'packages':['annotatedtimeline']}); 
     google.setOnLoadCallback(drawChart); 
     function drawChart() { 
     var data = new google.visualization.DataTable(); 
     data.addColumn('date', 'Date'); 
     data.addColumn('number', 'Sold Pencils'); 
     data.addColumn('string', 'title1'); 
     data.addColumn('string', 'text1'); 
     data.addColumn('number', 'Sold Pens'); 
     data.addColumn('string', 'title2'); 
     data.addColumn('string', 'text2'); 
     data.addRows([ 
      [new Date(2008, 1 ,1), 30000, undefined, undefined, 40645, undefined, undefined], 
      [new Date(2008, 1 ,2), 14045, undefined, undefined, 20374, undefined, undefined], 
      [new Date(2008, 1 ,3), 55022, undefined, undefined, 50766, undefined, undefined], 
      [new Date(2008, 1 ,4), 75284, undefined, undefined, 14334, 'Out of Stock','Ran out of stock on pens at 4pm'], 
      [new Date(2008, 1 ,5), 41476, 'Bought Pens','Bought 200k pens', 66467, undefined, undefined], 
      [new Date(2008, 1 ,6), 33322, undefined, undefined, 39463, undefined, undefined] 
     ]); 

     var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div')); 
     chart.draw(data, {displayAnnotations: true}); 

这是我做的,我有问题的类。 该类使得向图中添加数据更容易,更好地实现我所要做的。基本上,不是使用一堆未定义的值创建列,而是为您完成这些工作,而不必跟踪每列的位置/值。

function GraphManager(dataTable) 
{ 
    this.graphData = new Array(); 
    this.dataTable = dataTable; 

    this.titleFinder = new Object(); // used to keep track of the indices 

    this.dataTable.addColumn('date', 'Date'); 

    this.addSet = function(title) 
    { 

     var setCount = (this.dataTable.getNumberOfColumns() -1)/3; //used for the column name 
     var place = this.dataTable.getNumberOfColumns(); 

     this.titleFinder[title] = place; //the title of the column and its location 

     this.dataTable.addColumn('number', title); 
     this.dataTable.addColumn('string', 'title' + setCount); 
     this.dataTable.addColumn('string', 'text' + setCount); 

    } 

    this.addPoint = function(title, date, number) 
    { 
     //this function finds the location of the category 
     //and inserts a column with data at the location 


     var place = titleFinder[title]; //get the location 

     var column = new Array(dataTable.getNumberOfColumns()); 
     column[0] = date; 
     column[place] = number; 

     for (var i = 0;i<place; i++) 
     { 
      column[i] = undefined; 
     } 

     for (var i = place + 1; i<dataTable.getNumberOfColumns(); i++) 
     { 
      column[i] = undefined; 
     } 

     var next = this.graphData.length; 
     this.graphData[next] = column; 
     data.addRows(graphData); 

    } 


} 

然后这段代码可以用少量的代码做同样的事情。 function printGraph() var data = new google.visualization.DataTable();

 var gm = new GraphManager(data); 
    var title = "testcategory"; 


    gm.addSet(title); 
    gm.addPoint(title, new Date[2010, 5, 10], 100); 
    gm.addPoint(title, new Date[2010, 6, 10], 200); 


    var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div')); 
    chart.draw(gm.dataTable, {displayAnnotations: true}); 

}

有了这个HTML体

<input type="button" onclick="printGraph()" value="Draw Graph"> 
<div id='chart_div' style='width: 800px; height: 350px;'></div> 

问题:我正在上线gm.addSet(标题)错误的 “预期目标”。基本上,我不能使用类GraphManager。我在这里做错了什么?

+0

哪里的代码部分的上方包含在HTML页面? – 2010-07-16 17:52:42

+0

我在下面添加了以供参考,谢谢 – imagineblue 2010-07-16 20:44:14

回答

0

好吧,我想出了问题。基本上我遗漏了一堆“这个”陈述,当我创建一个新的日期时,我使用括号而不是括号。我也意识到,当我添加一组新数据时,我需要浏览旧数据以添加空列。如果任何人有兴趣,这里是完成的代码...如果你在不同的日期添加数据或者你不知道你将拥有多少数据集,这是非常有用的。

function GraphManager(adataTable) 
{ 
    this.graphData = new Array(); 
    this.dataTable = adataTable; 

    this.titleFinder = new Object(); // used to keep track of the indices 

    this.dataTable.addColumn('date', 'Date'); 

    this.addSet = function(title) 
    { 

     var setCount = (this.dataTable.getNumberOfColumns() -1)/3; //used for the column name 
     var pointsCount = this.graphData.length; 
     var place = this.dataTable.getNumberOfColumns(); 

     this.titleFinder[title] = place; //the title of the column and its location 

     this.dataTable.addColumn('number', title); 
     this.dataTable.addColumn('string', 'title' + setCount); 
     this.dataTable.addColumn('string', 'text' + setCount); 

     var newCount = this.dataTable.getNumberOfColumns(); 



     for (var i = 0; i<pointsCount; i++) 
     { 
      for (var j=place; j<newCount; j++) 
      { 
       this.graphData[i][j] = undefined; 
      } 


     } 

    } 

    this.addPoint = function(title, date, number) 
    { 
     //this function finds the location of the category 
     //and inserts a column with data at the location 


     var place = this.titleFinder[title]; //get the location 

     var column = new Array(this.dataTable.getNumberOfColumns()); 
     column[0] = date; 
     column[place] = number; 

     for (var i = 1;i<place; i++) 
     { 
      column[i] = undefined; 
     } 

     for (var i = place + 1; i<this.dataTable.getNumberOfColumns(); i++) 
     { 
      column[i] = undefined; 
     } 

     var next = this.graphData.length; 
     this.graphData[next] = column; 
     this.dataTable.addRows(this.graphData); 

    } 


} 

而且它的那么容易,因为这样使用:

 var data = new google.visualization.DataTable(); 
    var gm = new GraphManager(data); 

    var title = "testcategory"; 
    var title2 = "cat"; 

    gm.addSet(title); 
    gm.addPoint(title, new Date(2010, 5, 10), 100); 
    gm.addPoint(title, new Date(2010, 6, 10), 200); 
    gm.addPoint(title, new Date(2010, 2, 10), 300); 


    gm.addSet(title2); 
    gm.addPoint(title2, new Date(2010, 6, 10), 100); 
    gm.addPoint(title2, new Date(2010, 2, 10), 500); 

    var chart = newgoogle.visualization.AnnotatedTimeLine(document.getElementById('chart_div')); 
    chart.draw(gm.dataTable, {displayAnnotations: true}); 
0

不应该是“dataTable”而不是“tableData”?

for (var i = place + 1; i<tableData.count; i++) 
{ 
    column[i] = undefined; 
} 
+0

是的,愚蠢的错误。但是,这并没有帮助我的问题。 – imagineblue 2010-07-16 16:55:22

0

我不知道,按:

http://code.google.com/apis/visualization/documentation/reference.html#DataTable 

计数不是一个属性,但我看到你提到它很多地方在你的代码:

var column = new Array(dataTable.count) 

有是dataTable.getNumberOfColumns()然而

+0

谢谢,没错,我已经解决了这个问题,但我仍然有“对象预期”错误 – imagineblue 2010-07-16 17:27:48

相关问题