javascript - 如何让 Highcharts 从外部 JSON 文件获取数据?

标签 javascript json highcharts

基本上,我有这个http://jsfiddle.net/TWF6N/

我不想让 JS 文件中包含数据系列,而是让 JS 检索 JSON 文件。

我不希望它出现在 JS 文件中:

data = {"aaData": [
    [1, "70.1700", "2008-12-29 11:23:00"],
    [2, "70.2600", "2008-12-29 16:22:00"],
    [3, "70.6500", "2008-12-30 11:30:00"],
    [4, "70.8700", "2008-12-30 16:10:00"],
    [5, "70.5500", "2009-01-02 11:09:00"],
    [6, "70.6400", "2009-01-02 16:15:00"]
]};

我该如何做这个?

谢谢。

最佳答案

直接来自the HighChart documentation :

您的 JSON 数据将如下所示:

[
    [1, "70.1700", "2008-12-29 11:23:00"],
    [2, "70.2600", "2008-12-29 16:22:00"],
    [3, "70.6500", "2008-12-30 11:30:00"],
    [4, "70.8700", "2008-12-30 16:10:00"],
    [5, "70.5500", "2009-01-02 11:09:00"],
    [6, "70.6400", "2009-01-02 16:15:00"]
]

你的 JS 看起来像这样:

$(function () {
    var chart;
    $.getJSON('data.json', function(jsonData) {
        chartOptions.series = jsonData;
        chart = new Highcharts.Chart(chartOptions);
    });

    var chartOptions = {
        chart: {
            renderTo: 'container'
        },
        xAxis: {
            type: 'datetime'
        },

        series: []
    };
});

关于javascript - 如何让 Highcharts 从外部 JSON 文件获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15001399/

相关文章:

javascript - jQuery 输入事件 - 触发 ('change' ) 不是吗

javascript - 如何在 Electron 的预加载js中运行knex?

java - 如何在Apache thrift中以JSON列表输出

javascript - HighStock 图表 : Extract time based on user selection from the Navigator window

javascript - 我该如何修复 'Error: <rect> attribute x: Expected length, "NaNNaN”

javascript - HighChart中导出的多个图表与屏幕上显示的不一样

javascript - 我怎么知道哪些处理程序在 promise 中抛出错误?

javascript - 我将如何使用 jquery click 从导航栏中的元素淡出 html 中的旁白部分?

php - 使用 Jquery、AJAX 和 PHP 从 MySQL 数据库中检索数据

javascript - 添加两个json对象并排序