2013-06-18 42 views
1

我是新來的google.visualization.datasource,我想知道是否有一個簡單的例子,用一個助手類創建數據表並返回給客戶端,但沒有使用查詢和DataSourceServlet ??因爲我已經通過spring mvc控制器實現了我的http請求。春天mvc 3和谷歌datatable

我想實現下面的例子:https://developers.google.com/chart/interactive/docs/dev/dsl_get_started

這裏是我的代碼:

@Controller 
public class DBcontroller { 
private SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 


@SuppressWarnings({ "rawtypes", "unchecked" }) 
@RequestMapping(value = "/getHistoricalData", method = {RequestMethod.POST,RequestMethod.GET}) 
public @ResponseBody DataTable setLampsRequest(@RequestParam String Lampid 
     , @RequestParam String StartDate, @RequestParam String EndDate) throws ParseException{ 

    //converting time values to long 
    Long start = formater.parse(StartDate).getTime(); 
    Long end = formater.parse(EndDate).getTime(); 

    // Create a data table, 
    DataTable data = new DataTable(); 
    ArrayList cd = new ArrayList(); 
    cd.add(new ColumnDescription("name", ValueType.TEXT, "Animal name")); 
    cd.add(new ColumnDescription("link", ValueType.TEXT, "Link to wikipedia")); 
    cd.add(new ColumnDescription("population", ValueType.NUMBER, "Population size")); 
    cd.add(new ColumnDescription("vegeterian", ValueType.BOOLEAN, "Vegetarian?")); 

    data.addColumns(cd); 

    // Fill the data table. 
    try { 
     data.addRowFromValues("Aye-aye", "http://en.wikipedia.org/wiki/Aye-aye", 100, true); 
     data.addRowFromValues("Sloth", "http://en.wikipedia.org/wiki/Sloth", 300, true); 
     data.addRowFromValues("Leopard", "http://en.wikipedia.org/wiki/Leopard", 50, false); 
     data.addRowFromValues("Tiger", "http://en.wikipedia.org/wiki/Tiger", 80, false); 
    } catch (TypeMismatchException e) { 
     System.out.println("Invalid type!"); 
    } 


    return data; 
} 

} 

預先感謝您的任何幫助表示讚賞。

+0

問題,什麼你在運行?你有什麼嘗試?單獨創建dataTable沒有任何問題,只要最終的格式可以通過Google Visualization以JS對象的形式進行讀取即可。因此,如果它是一個適當的對象,那麼將它用作數據源應該不會對創建圖表造成任何問題。你的問題究竟在哪裏? – jmac

+0

@jmac我試圖通過google.gson轉換爲JSON對象 當我在瀏覽器中收到它並從它製作google.Visualization.Datatable並嘗試繪製它時,我在控制檯中得到此錯誤: 'Uncaught TypeError :對象#沒有方法'getNumberOfColumns')' –

回答

1

可以使用JsonRenderer寫的數據表爲JSON:

@RequestMapping(value = "/getHistoricalData", method = {RequestMethod.POST,RequestMethod.GET}) 
public @ResponseBody void setLampsRequest(
      @RequestParam String Lampid, 
      @RequestParam String StartDate, 
      @RequestParam String EndDate, 
      Writer output, 
      HttpServletRequest request) { 

    //converting time values to long 
    Long start = formater.parse(StartDate).getTime(); 
    Long end = formater.parse(EndDate).getTime(); 

    // Create a data table, 
    DataTable data = new DataTable(); 
    ArrayList cd = new ArrayList(); 
    cd.add(new ColumnDescription("name", ValueType.TEXT, "Animal name")); 
    cd.add(new ColumnDescription("link", ValueType.TEXT, "Link to wikipedia")); 
    cd.add(new ColumnDescription("population", ValueType.NUMBER, "Population size")); 
    cd.add(new ColumnDescription("vegeterian", ValueType.BOOLEAN, "Vegetarian?")); 

    data.addColumns(cd); 

    // Fill the data table. 
    try { 
     data.addRowFromValues("Aye-aye", "http://en.wikipedia.org/wiki/Aye-aye", 100, true); 
     data.addRowFromValues("Sloth", "http://en.wikipedia.org/wiki/Sloth", 300, true); 
     data.addRowFromValues("Leopard", "http://en.wikipedia.org/wiki/Leopard", 50, false); 
     data.addRowFromValues("Tiger", "http://en.wikipedia.org/wiki/Tiger", 80, false); 
    } catch (TypeMismatchException e) { 
     System.out.println("Invalid type!"); 
    } 
    PrintWriter writer = new PrintWriter(output); 
    writer.println(JsonRenderer.renderDataTable(data, true, false, false)); 
    writer.close(); 
}