0

我已经阅读了很多关于同一项目的参考资料,但我仍然感到困惑。我已经添加了我的代码。

在随附的代码中,我有两个图。plot_1使用页面中定义的数据并且plot_2相同,但使用 JSON 导入数据。plot_1工作正常,plot_2但没有。在这个论坛上有很多不同版本的导入 JSON 数据,我尝试模拟其中的一些但没有成功。我在嵌入式系统上使用此代码。如果有人可以提供帮助,我将不胜感激。

<script>
$(function () { 
        $('#plot_1').highcharts({
        chart: {
        borderWidth: 2,
        borderRadius: 3,
        borderColor: '#a1a1a1',
                type: 'line',
        marginBottom: 30
        },
        title: {
                text: 'Battery Voltage and Current'
        },
        xAxis: {
                type: 'datetime',
        pointStart: Date.UTC(2013, 2, 26),      // Jan = 0
        pointInterval: 60 * 1000 * 15,      // 15 minutes
        labels: {
        align: 'center',
        x:  0,
        y:  16
        }
        },

        yAxis: [{               // Primary axis (left)
        startOnTick: true,
                title: {
                text: 'Volts',
        style: {
            color: '#89A54E'
        }
                },
        labels: {
        align: 'right',
        x: -3,
        y: 0,
        style: {
            color: '#89A54E'
        }
        },
        min: 6,
        max: 16

        },{                 // Secondary axis (right)
        opposite: true,
        gridLineWidth: 1,
            title: {
                text: 'Amps',
        style: {
            color: '#4572A7'
        }
                },
        labels: {
        align: 'left',
        x: 3,
        y: 0,
        style: {
            color: '#4572A7'
        }
        },
        min: -0.5,
        max:  0.5
    }],

    legend: {
        enabled: false
    },

    plotOptions: {
        series : {
        marker: {
            enabled: false
        }
        }
    },

        series: [{
            name: 'Volt',
        color: '#89A54E',
                data:  [12.4,   12.4,    12.4,    12.2,    12.0,    11.9,    11.9,    11.8,    11.6,    11.4,    11.1,    10.9,     11.4,     11.5,     11.7,     11.9,     12.2,     12.4,     12.4,     12.4]
        }, {
                name: 'Amp',
        color: '#4572A7',
        yAxis: 1,
                data: [-0.1, -0.2, -0.1, -0.2, -0.3, -0.3, -0.4, -0.3, -0.4, -0.4, -0.3, -0.3,  0.3,  0.3,  0.4,  0.5,  0.4,  0.4,  0.4,  0.1]
        }]
        });
});
    </script>   


    <script>
$(function () {

    var data1 = [], data2 = [];

    function requestData1() {
        $.ajax({
            url: "sig0.jsn",
            dataType: "json",
            success: function(data1) {
                alert (data1);
                this.series[0].setData(data1);
            },
            cache: false
        });
    }

    function requestData2() {
        $.ajax({
            url: "sig2.jsn",
            dataType: "json",
            success: function(data2) {
                alert (data2);
                this.series[0].setData(data2);
            },
            cache: false
        });
    }


        $('#plot_2').highcharts({
        chart: {
        borderWidth: 2,
        borderRadius: 3,
        borderColor: '#a1a1a1',
                type: 'line',
        marginBottom: 30,
        events: {
        load: requestData1,
        load: requestData2
        }
        },
        title: {
                text: 'Received Signal Strength'
        },
        xAxis: {
                type: 'datetime',
        pointStart: Date.UTC(2013, 2, 26),      
        pointInterval: 60 * 1000 * 15,
        labels: {
        align: 'center',
        x:  0,
        y:  16
        }
        },

        yAxis: {                // Primary axis (left)
        startOnTick: true,
                title: {
                text: 'Strength (db)'
                },
        labels: {
        align: 'right',
        x: -3,
        y: 0

        },
        min: -40,
        max: 0

    },

    legend: {
        align: 'left',
        verticalAlign: 'top',
        floating: true,
        x: 50,
        y: 45
    },

    plotOptions: {
        series : {
        marker: {
            enabled: false
        }
        }
    },

        series: [{
            name: 'Device A',
        color: '#89A54E',
                data: data1
        },{
            name: 'Device B',
        color: '#4572A7',
                data: data2
        }]

        });

});
    </script>   

JSON 文件数据如下所示。

{
"data" : [-25, -23, -15, -16, -26, -24, -26, -28, 29, -16, -22, -24, -22, -17, -21, -25, -21, -22, -23, -22]
}

最后,我将 x 轴时间数据与 y 轴数据配对,因为我很可能会错过数据记录中的一些时间间隔,所以如果你能在答案中考虑这一点。总的来说,我想绘制来自 4 个传感器的数据。

4

1 回答 1

0

您应该在 ajax 调用中移动图表 inilazize,因为在您的情况下,ajax 和 highcharts 定义是“同时”运行的。

结果应该是这样的:

 $.ajax({
        url: "sig2.jsn",
        dataType: "json",
        success: function(data2) {
            alert (data2);
            this.series[0].setData(data2);
            $('#plot_2').highcharts({
                            //parameters
            });
        },
        cache: false
    });
于 2013-04-15T09:51:37.880 回答