首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >很难将这些JSON数据放到高级图表中

很难将这些JSON数据放到高级图表中
EN

Stack Overflow用户
提问于 2017-10-31 23:08:50
回答 2查看 125关注 0票数 1

我有下面的代码片段,它从JSON中获取数据并输入到MariaDB中。

现在,我想把这些数据从数据库中取出(因为数据库记录了JSON的时间),然后将其放入一个图表中,但是我很难将数据从JSON URL中提取到高级图表中.我的数据如下:

代码语言:javascript
复制
[{"time":"1509488314","hashrate":"34096322642","minersTotal":"99"},
{"time":"1509490093","hashrate":"34096645609","minersTotal":"101"},
{"time":"1509490201","hashrate":"34096374421","minersTotal":"101"},
{"time":"1509490321","hashrate":"34138925733","minersTotal":"101"},
{"time":"1509490441","hashrate":"34062086317","minersTotal":"101"},
{"time":"1509490561","hashrate":"34116887228","minersTotal":"101"},
{"time":"1509490681","hashrate":"34053449517","minersTotal":"103"},
{"time":"1509490801","hashrate":"34060600882","minersTotal":"103"},
{"time":"1509490921","hashrate":"34065888457","minersTotal":"103"},
{"time":"1509491041","hashrate":"34093378965","minersTotal":"105"}]

我希望基本上绘制跨越X轴的时间,哈希速率作为一条线,minersTotal作为一条条。

我已经做了PHP / MariaDB部分,但是做这个部分对我来说是一件很困难的事情。

我的php代码:

代码语言:javascript
复制
$mysqli = new mysqli($servername, $username, $password, $dbname);
$myArray = array();
if ($result = $mysqli->query("SELECT * FROM hashrates LIMIT 100")) {

    while($row = $result->fetch_object()) {
            $myArray[] = $row;
    }
    echo json_encode($myArray);
}

$result->close();
$mysqli->close();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-03 23:56:47

我就这样结束了

代码语言:javascript
复制
$(function () {
    var hashrates = new Array();
    var minersTotal = new Array();

    function refreshdata() {
        $.getJSON("data.json", function(data) {
            var hashrates = new Array();
            var minersTotal = new Array();
            for (i = 0; i < data.length; i++) {
                var object = data[i];
                var time = object.time * 1000;
                hashrates.push([time, parseFloat(object.hashrate) / 1000000000]);
                minersTotal.push([time, parseFloat(object.minersTotal)]);
            }
            $('#container').highcharts().series[0].setData(minersTotal, true);
            $('#container').highcharts().series[1].setData(hashrates, true);
            $('#container').highcharts().redraw();
        });
    }

    $('#container').highcharts({
        chart: {
            backgroundColor: "rgba(0,0,0,0)",
            color: "#FF0000",
            alignTicks: false,
            events: {
                load: function () {
                    setInterval(function () {refreshdata();}, 60000);
                }
            }
        },
        title: {
            text: "Pool performance"
        },
        subtitle: {
            text: "3 days (15 min intervals)"
        },
        tooltip: {
            shared: true,
            valueDecimals: 2
        },
        legend: {
            layout: "horizontal"
        },
        xAxis: {
            title: {
                text: "Time (UTC)"
            },
            type: "datetime",
            showFirstLabel: true
        },
        yAxis: [{
            title: {
                text: "Hashrate (GH/s)"
            },
            labels: {
                style: {
                    color: "#434348"
                },
                formatter: function() {
                    return this.axis.defaultLabelFormatter.call(this).toString().substr(0,4);
                }
            },
            gridLineColor: "#434348",
            tickInterval: 10,
            min: 0
        },{
            title: {
                text: "Miners",
                style: {
                    color: "#95CEFF"
                },
            },
            labels: {
                style: {
                    color: "#95CEFF"
                }
            },
            opposite: true,
            tickInterval: 40,
            gridLineColor: "#95CEFF"
        }],

        series: [{
            name: "Miners",
            type: "spline",
            data: minersTotal,
            yAxis: 1
        },{
            name: "Hashrate",
            data: hashrates,
            type: "spline"
        }]
    });
    refreshdata();
});

在这里可以看到:https://metaverse.farm/

票数 0
EN

Stack Overflow用户

发布于 2017-11-01 00:02:21

根据这个演示(高图表演示>双轴、直线和列)。数据必须是一个值数组,例如:["Item1", "Item2", "Item3"]

使用您的数据,您可以使用Array#map()

代码语言:javascript
复制
var times = data.map(function(x) {
  return new Date(x.time * 1000);
});
var hashrates = data.map(function(x) {
  return x.hashrate * 1;
});
var minersTotals = data.map(function(x) {
  return x.minersTotal * 1;
});

你可以这样做:

代码语言:javascript
复制
(function() {
  var data = [{
      "time": "1509488314",
      "hashrate": "34096322642",
      "minersTotal": "99"
    },
    {
      "time": "1509490093",
      "hashrate": "34096645609",
      "minersTotal": "101"
    },
    {
      "time": "1509490201",
      "hashrate": "34096374421",
      "minersTotal": "101"
    },
    {
      "time": "1509490321",
      "hashrate": "34138925733",
      "minersTotal": "101"
    },
    {
      "time": "1509490441",
      "hashrate": "34062086317",
      "minersTotal": "101"
    },
    {
      "time": "1509490561",
      "hashrate": "34116887228",
      "minersTotal": "101"
    },
    {
      "time": "1509490681",
      "hashrate": "34053449517",
      "minersTotal": "103"
    },
    {
      "time": "1509490801",
      "hashrate": "34060600882",
      "minersTotal": "103"
    },
    {
      "time": "1509490921",
      "hashrate": "34065888457",
      "minersTotal": "103"
    },
    {
      "time": "1509491041",
      "hashrate": "34093378965",
      "minersTotal": "105"
    }
  ];
  var times = data.map(function(x) {
    return new Date(x.time * 1000);
  });
  var hashrates = data.map(function(x) {
    return x.hashrate * 1;
  });
  var minersTotals = data.map(function(x) {
    return x.minersTotal * 1;
  });

  Highcharts.chart("container", {
    chart: {
      zoomType: "xy"
    },
    title: {
      text: "Data"
    },
    subtitle: {
      text: "Subtitle"
    },
    xAxis: [{
      categories: times,
      crosshair: true
    }],
    yAxis: [{ // Primary yAxis.
      labels: {
        format: "{value}",
        style: {
          color: Highcharts.getOptions().colors[1]
        }
      },
      title: {
        text: "Hashrate",
        style: {
          color: Highcharts.getOptions().colors[1]
        }
      }
    }, { // Secondary yAxis.
      title: {
        text: "MinersTotal",
        style: {
          color: Highcharts.getOptions().colors[0]
        }
      },
      labels: {
        format: "{value}",
        style: {
          color: Highcharts.getOptions().colors[0]
        }
      },
      opposite: true
    }],
    tooltip: {
      shared: true
    },
    legend: {
      layout: "vertical",
      align: "left",
      x: 120,
      verticalAlign: "top",
      y: 100,
      floating: true,
      backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || "#FFFFFF"
    },
    series: [{
      name: "MinersTotal",
      type: "column",
      yAxis: 1,
      data: minersTotals,
      tooltip: {
        valueSuffix: ""
      }
    }, {
      name: "Hashrate",
      type: "line",
      data: hashrates,
      tooltip: {
        valueSuffix: ""
      }
    }]
  });
})();
代码语言:javascript
复制
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="height: 400px; margin: 0 auto; min-width: 310px;"></div>

如果这对你有用的话请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47045803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档