【问题标题】:Highchart : Unable to populate data from JSONHighchart:无法从 JSON 填充数据
【发布时间】:2015-08-13 06:14:24
【问题描述】:

我正在尝试学习 highchart,但是当我尝试从 JSON 填充数据时,它无法正常工作。但是我硬编码了它工作正常的数据。所以,我认为我在调用 JSON 时犯了一些错误。

我的 JSON 看起来像:--

[{
"data": ["11-Aug 12:03", "11-Aug 12:45", "11-Aug 13:13", "11-Aug 13:53", "11-Aug 14:03", "11-Aug 14:33", "11-Aug 14:54", "11-Aug 15:17", "11-Aug 15:49", "11-Aug 16:07", "11-Aug 17:00", "11-Aug 17:33"]
}, {
 "data": [23987, 24784, 25899, 255, 25897, 25668, 24114, 23899, 224987, 25111, 25899, 23]
}, {
"data": [12.4, 4.56, 3.8, 1.2, 11, 12.3, 5.67, 7.65, 34.5, 12.78, 10.5, 2.8]
}]

添加整个代码:--

$(function () {
$('#container').highcharts({
    chart: {
        zoomType: 'xy'
    },
    title: {
        text: 'Job Status'
    },

    xAxis: [{
        categories: [],
        crosshair: true
    }],
    yAxis: [{ // Primary yAxis
        labels: {

            style: {
                color: Highcharts.getOptions().colors[2]
            }
        },
        title: {
            text: 'Transaction Count',
            style: {
                color: Highcharts.getOptions().colors[2]
            }
        },
        opposite: true

    }, { // Secondary yAxis
        gridLineWidth: 0,
        title: {
            text: 'Time',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        labels: {
            format: '{value} min',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        }

    }, { // Tertiary yAxis
        gridLineWidth: 0,
        title: {
            text: 'Sea-Level Pressure',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        labels: {

            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        opposite: true
    }],
    tooltip: {
        shared: true
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        x: 80,
        verticalAlign: 'top',
        y: 55,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    series: [{
        name: 'Rainfall',
        type: 'column',
        yAxis: 1,
        data: []

   },  {
        name: 'Temperature',
        type: 'spline',
        data: []

    }]

    $.getJSON("http://localhost:8080/TestQuartz/json/highdefault.json", function(json) {
        options.xAxis.categories = json[0]['data'];
        options.series[0] = json[1];
        options.series[1] = json[2];
        chart = new Highcharts.Chart(options);
    });
});
});

我无法创建 JSFiddle 来展示现场示例。

在从 JSON 调用数据时,您能帮我解决我做错的地方吗?

问候

【问题讨论】:

  • 您是否收到任何错误消息?
  • 它显示了调用 getJSON 函数的意外标识符。
  • getJSON 之前的上一行是什么?我想你最后忘记了昏迷。
  • 你能粘贴你使用的整个代码吗?
  • 嗨..我已经添加了完整的代码。

标签: javascript jquery json highcharts


【解决方案1】:

代码语法不正确。 Morefer 你运行 highcharts 两次。固定代码:

$(function () {
var options = {
    chart: {
        zoomType: 'xy',
        renderTo: 'container'
    },
    title: {
        text: 'Job Status'
    },

    xAxis: [{
        categories: [],
        crosshair: true
    }],
    yAxis: [{ // Primary yAxis
        labels: {

            style: {
                color: Highcharts.getOptions().colors[2]
            }
        },
        title: {
            text: 'Transaction Count',
            style: {
                color: Highcharts.getOptions().colors[2]
            }
        },
        opposite: true

    }, { // Secondary yAxis
        gridLineWidth: 0,
        title: {
            text: 'Time',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        labels: {
            format: '{value} min',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        }

    }, { // Tertiary yAxis
        gridLineWidth: 0,
        title: {
            text: 'Sea-Level Pressure',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        labels: {

            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        opposite: true
    }],
    tooltip: {
        shared: true
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        x: 80,
        verticalAlign: 'top',
        y: 55,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    series: [{
        name: 'Rainfall',
        type: 'column',
        yAxis: 1,
        data: []

    }, {
        name: 'Temperature',
        type: 'spline',
        data: []

    }]
};



    $.getJSON("http://localhost:8080/TestQuartz/json/highdefault.json", function (json) {
        options.xAxis[0].categories = json[0]['data'];
        options.series[0] = json[1];
        options.series[1] = json[2];
        chart = new Highcharts.Chart(options);
    });
});

【讨论】:

  • 您好,感谢您的帮助。它有两个问题。 X 轴采用默认值 1 2 3 而不是来自 JSON,例如 11-Aug 12:03 和其他两个图表都以 Line 形式出现的问题。一个应该是理想的酒吧。
  • 我更新了我的示例,在 xAxis 中您应该引用 xAxis[0],因为您定义了 xAxis 对象数组。
  • 非常感谢塞巴斯蒂安......很抱歉要求更多,但是否可以将图表之一作为条形图而不是两个作为折线图?说像 23987 这样在 JSON 中排名第二的值应该在 Bar 中
  • 添加一行例如:options.series[1].type = 'column';
  • 我尝试添加它,但如果我添加任何强制系列,它只会导致 1 个图表和其他图表消失 .$.getJSON("localhost:8080/TestQuartz/json/highdefault.json", function (json) { options.xAxis[0 ].categories = json[0]['data']; options.series[0] = json[1]; options.series[1] = json[2]; options.series[1].type = 'column' ; options.series[0].type = 'spline'; chart = new Highcharts.Chart(options); });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多