高图:两行显示而不是一行

时间:2016-07-11 06:36:52

标签: javascript jquery highcharts frontend

我正在使用highcharts.js

问题是在创建图形时它向我显示两行而不是 一。

它应该只显示一行,因为它在两个值之间绘制图形 这是(时间戳和价值)。

请你检查一下为什么它向我显示两行而不是一行,我该如何解决这个问题:

(只需将代码复制并粘贴到文件中即可使用)。

<!DOCTYPE html>
<html>

<head>

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>

</head>

<body style="background:#212224;">

<div id="container" style="max-width: 1666px;  margin: 0 auto"></div>

<script type="text/javascript">

    $.getJSON('https://dl.dropboxusercontent.com/u/76618626/data4.json', function (data) {
        console.log("data size is : ");
        console.log(data);      

        var data3 = [];

                    $.each(data.data,function(i,d){
                    console.log(new Date(d.timestamp).getTime(), d.value);

                        data3.push([new Date(d.timestamp).getTime(),d.value]);

                    });

                    console.log("Data 3 is :");
                    console.log(data3);
                    var data4 = [];
                    Highcharts.each(data3, function(p, i) {
                        if (typeof p[0] !== 'number' || typeof p[1] !== 'number') {
                             console.log('Invalid data is :')
                             console.log(p);
                        } else {
                            data4.push(p);
                        }
                    });



        $('#container').highcharts({

            chart: {
            backgroundColor: '#000000',
                },

            title: {
                text: 'Test Graph',
                style: {
                color: '#FFFFFF',
                fontWeight: 'bold'
                }
            },
            xAxis: {
                type: 'datetime',
                title: {
                    text: 'Time Stamp'
                },
                gridLineColor: 'grey',
                gridLineWidth: 1,
                lineWidth:1

            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                gridLineColor: 'grey',
                gridLineWidth: 1,
                lineWidth:1
            },
            legend: {
                enabled: true
            },

            exporting: false,



            plotOptions: {
                line: {                 
                    lineColor: 'red',
                    fillOpacity: 1,                    
                    lineWidth: 2,
                    states: {
                        hover: {
                            lineWidth: 2
                        }
                    },
                    threshold: null,
                    marker: {
                        fillColor: '#e57255'
                        }


                },


            },


            series: [{
                type: 'line',
                name: 'test',
                data: data4
            }]
        });
    });

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我在jsFiddle重新编写了代码。它实际上是一个折线图。

查看console.log(new Date(d.timestamp).toString(), d.value);处的打印件。

您的数据时间戳从Tue Jul 05 2016 03:00:00开始,增加到Thu Jul 07 2016 03:00:00,然后从Jul 05 2016 03:00:00重新开始。

因此,这就是为什么你的图表看起来像一个双线图表。该行的底部部分被链接回顶行。

只需更新您的数据JSON就可以了。

<强>被修改

更新我的fiddle以使用.sort()安排时间戳。

相关问题