gpt4 book ai didi

jquery - HighCharts 通过 Json 加载数据

转载 作者:行者123 更新时间:2023-12-01 06:45:07 24 4
gpt4 key购买 nike

我正在尝试从我的 Json 数据制作 HighCharts 图表。这是我的 Json 数据

[{"ReadData":"99","Time":"07\/09\/2015 00:00:07"},{"ReadData":"101","Time":"07\/09\/2015 00:01:07"},{"ReadData":"113","Time":"07\/09\/2015 00:02:07"},{"ReadData":"115","Time":"07\/09\/2015 00:03:07"},{"ReadData":"96","Time":"07\/09\/2015 00:04:07"},{"ReadData":"103","Time":"07\/09\/2015 00:05:07"}]

我的问题是当图表无法加载时,我做错了什么?这是我的 HTML 代码。

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script>

$(document).ready(function() {

var options = {
chart: {
renderTo: 'container',
type: 'spline'
},
series: [{}]
};

$.getJSON('data.json', function(data) {
options.xAxis.categories = json[0]['Time'];
options.series[0] = json[0]['ReadData'];
var chart = new Highcharts.Chart(options);
});

});
</script>
</head>
</html>

最佳答案

有几个问题,请参阅工作演示:http://jsfiddle.net/ux74929j/5/

让我解释一下:

  • 您的格式与 Highcharts 不兼容,并且您尝试以错误的方式使用它,请先解析您的数据:

            var categories = [],
    points = [];

    $.each(JSON, function(i, el) { // Assuming that JSON is data from getJSON()
    categories.push(el.Time);
    points.push(parseFloat(el.ReadData)); // Another issue - data should be number, not string
    });
    options.xAxis.categories = categories;
    options.series[0].data = points;
    var chart = new Highcharts.Chart(options);
  • 您的选项中没有 xAxis,但您仍尝试分配类别:

       var options = {
    chart: {
    renderTo: 'container',
    type: 'spline'
    },
    xAxis: {},
    series: [{}]
    };

总而言之,您的代码应如下所示:

       var options = {
chart: {
renderTo: 'container',
type: 'spline'
},
xAxis: {},
series: [{}]
};

$.getJSON('data.json', function (data) {
var categories = [],
points = [];

$.each(data, function(i, el) {
categories.push(el.Time);
points.push(parseFloat(el.ReadData));
});
options.xAxis.categories = categories;
options.series[0].data = points;
var chart = new Highcharts.Chart(options);
});

关于jquery - HighCharts 通过 Json 加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32478776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com