简体   繁体   English

准备动态数据以通过Google Chart API显示

[英]Preparing dynamic data for displaying via Google Chart API

I have an amount of data coming as a JSON string via an API that look like this: 我通过一个API将大量数据作为JSON字符串输入,如下所示:

someData1: {0: {_id: {date: "2015-11-11"}, count: 1}, length: 1}
    0: {_id: {date: "2015-11-11"}, count: 1}
        _id: {date: "2015-11-11"}
        date: "2015-11-11"
        count: 1
    length: 1
someData2: {0: {_id: {date: "2015-11-12"}, count: 5}, 1: {_id: {date: "2015-11-11"}, count: 3}, length: 2}
    0: {_id: {date: "2015-11-12"}, count: 5}
        _id: {date: "2015-11-12"}
        count: 5
    1: {_id: {date: "2015-11-11"}, count: 3}
        _id: {date: "2015-11-11"}
        date: "2015-11-11"
        count: 3
    length: 2
someDataN: ...

However, I need this data to be displayed in a Google Chart (Area chart exactly). 但是,我需要将此数据显示在Google图表中(确切地是面积图)。

From what I have understood, in a Google chart, the first column can be a date/string and the rest have to be numbers, so I am having trouble formatting the data in such a way to make it useable for Google charts. 据我了解,在Google图表中,第一列可以是日期/字符串,而其余列必须是数字,因此我在格式化数据时遇到麻烦,无法使其可用于Google图表。

So in my case someData1 and someData2 have to be columns, basically after the first date column. 因此,在我的情况下, someData1someData2必须是列,基本上在第一个date列之后。

So columns would be: [date, someData1, someData2] 因此,列将为: [date, someData1, someData2]

  1. obviously the first column takes a date. 显然,第一列需要一个日期。 How do I use the dates in my provided object in this case? 在这种情况下,如何使用提供的对象中的日期?
  2. How do I create rows in their own column using the data above as an example? 如何使用上面的数据作为示例在自己的列中创建行?

It's easy to set the columns in this case, but setting the rows is what has confused me. 在这种情况下设置列很容易,但是设置行却让我感到困惑。 How do I make sure the count of someData1 will be under the someData1 column and so on? 如何确定someData1计数someData1列下,依此类推?

How can I restructure this object to make it suit my needs? 如何重组该对象以使其适合我的需求?

Thank you. 谢谢。

I wrote a javascript function to do this exact thing: 我编写了一个javascript函数来完成此操作:

var GenerateChart = function(element, columns, data, title, type) {
var dataArr = [];

for (var i = 0; i < data.length; i++) {
  var cell = [];
  for (var n = 0; n < data[i].length; n++) {
    if (n === 0) {
      cell.push(data[i][n]);
    } else {
      cell.push(parseInt(data[i][n], 10));
    }

  }
  dataArr.push(cell);
}
var data = new google.visualization.DataTable();

for (var i = 0; i < columns.length; i++) {
  if (i === 0) {
    data.addColumn('string', columns[i].label);
  } else {
    data.addColumn('number', columns[i].label);
  }
}
data.addRows(dataArr);

var options;
var chart;
switch (type) {
  case 'line':
    options = {
      title: title,
      curveType: 'function',
      legend: {
        position: 'bottom'
      }
    }
    chart = new google.visualization.LineChart(element);
    break;
  case 'bar':
    options = {
      title: title,
      trendlines: {
        0: {}
      },
      legend: {
        position: 'bottom'
      },
    }
    chart = new google.visualization.ColumnChart(element);
    break;
  case 'pie':
    options = {
      title: title
    }
    chart = new google.visualization.PieChart(element);
    break;
  case 'bubble':
    var hAxis;
    var vAxis;
    for (var i = 0; i < 3; i++) {
      if (i === 1) {
        hAxis = columns[i].label;
      } else if (i === 2) {
        vAxis = columns[i].label;
      }
    }
    options = {
      title: title,
      hAxis: hAxis,
      vAxis: vAxis,
      buble: {
        textStyle: {
          fontSize: 11
        }
      }
    }
    chart = new google.visualization.BubbleChart(element);
    break;
  }
chart.draw(data, options);
  }

You would just need to add an 'area' case to the switch statement. 您只需要在switch语句中添加一个“ area”大小写即可。 But, most of their graphs are treated with very similar json structures. 但是,他们的大多数图形都使用非常相似的json结构进行处理。 You would call that function above like this: 您可以像上面这样调用该函数:

var element = document.getElementById('graph');
var columns = result[key].columns;
var data = result[key].data;
GenerateChart(element, columns, data, "Graph Title", "area");

And the 'result' variable above would be your json object. 上面的“结果”变量将是您的json对象。 I will put an example of a json object I would use for the function above shortly. 我将在上面简短介绍一个将用于上述功能的json对象的示例。 Example of a bar chart: 条形图示例:

{
    "title":"Feeds Today",
    "type":"bar",
    "columns":[
            {"label":"Feed Type"},
            {"label":"Successes"},
            {"label":"Fails"}],
    "data":[
         ["ListHub","507",2],
         ["Reliance","270",0],
         ["Realogy","88",0],
         ["DDF","36",0],
         ["RETS","231",7],
         ["IDX","23",0],
         ["XML","26",3]]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM