简体   繁体   English

谷歌图表中的实时图表

[英]Live Chart in Google Chart

I don't know if it's already too much to ask in StackOverflow but I'm just so exhausted and disappointed with my own capacity.我不知道在 StackOverflow 中要问是否已经太多了,但我对自己的能力感到非常疲惫和失望。 I've been doing this chart for 3 months and not successful.我已经做了这个图表 3 个月了,但没有成功。 Anyways I'm trying again google chart I'm asking for help out there.无论如何,我再次尝试使用谷歌图表寻求帮助。

I have my livedata.php(the time_stamp(named ts),ph,moist are data values in my database) in json format showing:我有我的 livedata.php(time_stamp(命名为 ts),ph,moist 是我数据库中的数据值)以 json 格式显示:

[{"ts":"12","ph":736,"moist":92},{"ts":"27","ph":0,"moist":0},{"ts":"21","ph":192,"moist":24},{"ts":"15","ph":0,"moist":0}]

that is "ts"(time stamp('d')) shows a specific day of a month(this example the month is august).即 "ts"(time stamp('d')) 显示一个月中的特定日期(这个例子是八月)。 "ph" and "moisture" are the overall value of that specific day. “ph”和“水分”是特定日期的总体值。 Obviously I am making charts here and I know I am a dumb full of ambitions I am trying to create a live chart that looks like this显然我在这里制作图表,我知道我是一个充满野心的笨蛋我正在尝试创建一个看起来像这样的实时图表只是来自我正在查找的网站的示例

Now my problem is with this code below I found which is working to their example but trying to my PHP JSON fails.现在我的问题是我发现下面这段代码对他们的示例有效,但尝试我的 PHP JSON 失败。

function drawChart() {
    var jsonData = $.ajax({
        url: 'livedata.php',
        dataType: 'json',
        async: false
    })

    .done(function (results) {
        var data = new google.visualization.DataTable(jsonData);
        data.addColumn('datetime', 'time_stamp');
        data.addColumn('number', 'ph');
        data.addColumn('number', 'moist');
        $.each(results, function (i, row) {
            data.addRow([
                (new Date(row.time_stamp)),
                parseFloat(row.ph),
                parseFloat(row.moist)]);
        });
        var chart = new google.visualization.LineChart($('#chart').get(0));
        chart.draw(data, {
            title: 'Soil Analysis'
        });
    });

}
// load chart lib
google.load('visualization', '1', {
    packages: ['corechart']
});
// call drawChart once google charts is loaded
google.setOnLoadCallback(drawChart);

there are several issues here, I'll try to help...这里有几个问题,我会尽力帮助...

for starters, use the current version of google charts...对于初学者,请使用当前版本的谷歌图表...

use this --> <script src="https://www.gstatic.com/charts/loader.js"></script>使用这个 --> <script src="https://www.gstatic.com/charts/loader.js"></script>

jsapi is an old version... jsapi是旧版本...

not this --> <script src="https://www.google.com/jsapi"></script>不是这个 --> <script src="https://www.google.com/jsapi"></script>

this will only change the load statement这只会改变load语句

google.charts.load('current', {
  callback: drawChart,
  packages: ['corechart']
});

as for ajax async: false has been deprecated, remove it...至于 ajax async: false已被弃用,将其删除...


next, the contructor for the data table only accepts json data in a specific format接下来,数据表的构造函数只接受特定格式的json数据
found here --> Format of the Constructor's JavaScript Literal data Parameter在这里找到 --> 构造函数的 JavaScript 文字数据参数的格式

just leave the argument blank只需将参数留空

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

finally, the posted json example does not contain a property for time_stamp最后,发布的 json 示例不包含time_stamp的属性

looks like ts instead, you could try using the current month instead看起来像ts ,您可以尝试使用当前月份

recommend something similar to the following...推荐类似以下的东西...

google.charts.load('current', {
  callback: drawChart,
  packages: ['corechart']
});

function drawChart() {
  $.ajax({
    url: 'livedata.php',
    dataType: 'json'
  }).done(function (results) {
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'time_stamp');
    data.addColumn('number', 'ph');
    data.addColumn('number', 'moist');

    $.each(results, function (i, row) {
      var today = new Date();

      data.addRow([
        new Date(today.getFullYear(), today.getMonth(), row.ts),
        parseFloat(row.ph),
        parseFloat(row.moist)
      ]);
    });

    var chart = new google.visualization.LineChart($('#chart').get(0));
    chart.draw(data, {
      title: 'Soil Analysis'
    });
  });
}

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

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