是否有现有的 js 方法/库将常规 json 对象转换为 google 可视化类型的 js 对象?

发布于 2024-11-24 08:30:22 字数 136 浏览 0 评论 0原文

我们现有的其余 Web 服务可以生成 json 响应。现在我们想使用谷歌图表来显示这些数据。谷歌可视化 api 似乎需要它自己的 json 格式。是否有任何现有的 js 方法/库可以将常规 json 对象转换为 google 可视化类型的 js 对象?谢谢。

we have existing rest web services that generates json response. now we want to use google charts to show those data. google visualization api seems to expect its own json format. Is there any exiting js method/library to convert regular json objects to google visualization type of js objects? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-12-01 08:30:22

这实际上取决于您的目标是什么、数据是什么样的,以及您想要使用的谷歌图表。在使用 REST 数据和 google 图表时,我通常会执行以下操作。

在此示例中,我使用 jQuery,但您使用的 js 库并不那么相关。

假设您有以下数据集以及在区域图中显示的内容:

{"events":[{"event":{"timestamp":"1310926588423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926578423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926568423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926558423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926548423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926538423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926528423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926518423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926508423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926498423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}}]}

使用 JQuery 和 JSONQuery (这可以帮助您轻松选择 JSON 数据中的特定内容)您可以执行以下操作:

  // use the getJSON jQuery operation to get the REST data
  $.getJSON(restURL, function(data) {

    // use jsonquery to get all the 'event's from the JSON data     
    var query1 = "..event";
    var rootEvent = JSONQuery(query1,data);

    // manually create a datatable and fill it in the required
    // way for this chart           
    var data2 = new google.visualization.DataTable();
    data2.addColumn('string', 'Number of queries / per 10 seconds');
    data2.addColumn('number', '# queries');

    // each row is added based on information from the json event
    // by simply iterating over the array
    data2.addRows(rootEvent.length);
    for (i = 0; i < rootEvent.length; i++) {
      var date = new Date(parseInt(rootEvent[i]['timestamp']));
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();

      var time = '';
      if (hours < 10) time=time+'0'; time=time+hours+':';
      if (minutes < 10) time=time+'0'; time=time+minutes+':';
      if (seconds < 10) time=time+'0'; time=time+seconds;

      data2.setCell(i,0,time);
      data2.setCell(i,1,parseInt(rootEvent[i]['countAll3Sec_EsperEventProcessor']));
    }


    chart.draw(data2, {width: 400, height: 240, title: 'Requests per 10 seconds',
                       hAxis: {title: 'Time', titleTextStyle: {color: '#FF0000'}}
    });
 });

It really depends on what you're aiming for and what data looks like, and the google chart you want to use. I usually do the following when working with REST data and google charts.

In this example I use jQuery, but the js library you use isn't that relevant.

Say you've got the following set of data and what to show it in an areachart:

{"events":[{"event":{"timestamp":"1310926588423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926578423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926568423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926558423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926548423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926538423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926528423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926518423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926508423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}},{"event":{"timestamp":"1310926498423","service":"EsperEventProcessor.service","countAll3Sec_EsperEventProcessor":"0","server":"EsperServer"}}]}

To do this with JQuery and JSONQuery (which can help in easy selecting specific content in your JSON data) you can do something like this:

  // use the getJSON jQuery operation to get the REST data
  $.getJSON(restURL, function(data) {

    // use jsonquery to get all the 'event's from the JSON data     
    var query1 = "..event";
    var rootEvent = JSONQuery(query1,data);

    // manually create a datatable and fill it in the required
    // way for this chart           
    var data2 = new google.visualization.DataTable();
    data2.addColumn('string', 'Number of queries / per 10 seconds');
    data2.addColumn('number', '# queries');

    // each row is added based on information from the json event
    // by simply iterating over the array
    data2.addRows(rootEvent.length);
    for (i = 0; i < rootEvent.length; i++) {
      var date = new Date(parseInt(rootEvent[i]['timestamp']));
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();

      var time = '';
      if (hours < 10) time=time+'0'; time=time+hours+':';
      if (minutes < 10) time=time+'0'; time=time+minutes+':';
      if (seconds < 10) time=time+'0'; time=time+seconds;

      data2.setCell(i,0,time);
      data2.setCell(i,1,parseInt(rootEvent[i]['countAll3Sec_EsperEventProcessor']));
    }


    chart.draw(data2, {width: 400, height: 240, title: 'Requests per 10 seconds',
                       hAxis: {title: 'Time', titleTextStyle: {color: '#FF0000'}}
    });
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文