1

我是 google.visualization.datasource 的新手,我想知道是否有一个简单的示例可以使用帮助程序类创建数据表并将其返回给客户端,但是不使用查询和 DataSourceServlet?因为我已经通过 spring mvc 控制器实现了我的 http 请求。

我试图实现以下示例: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;
 }  

 }

提前感谢您的任何帮助。

4

1 回答 1

1

您可以使用 aJsonRenderer将 DataTable 编写为 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();
}
于 2014-07-17T20:14:40.130 回答