如何在highchart中添加动态数据?

发布于 2024-12-28 23:50:40 字数 4770 浏览 2 评论 0原文

我有一个 Highchart,它通过 AJAX 和 jQuery 接收 JSON 数据。但直到我将鼠标悬停在图表上时,我的图表才显示这些点。即便如此,这些点都位于图表的最顶部。我认为我没有正确地将我的观点添加到该系列中。请告诉我 jsonpcallback 函数中做错了什么。

谢谢!

        <script type="text/javascript">
    <!-- Begin Chart options-->

    // define the options
            var options = {
                            chart: {renderTo: 'container'},
                            title: {text: 'Brewery'},
                            subtitle: {text: ' '},
                            xAxis: {text: 'Time',type: 'datetime'},
                            yAxis: [{ // left y axis
                                        title: {text: 'Temperature (℉)'},
                                        labels: {align: 'left', x: 3, y: 16,
                                                formatter: function() {
                                        return Highcharts.numberFormat(this.value, 0);}
                                    },
                                    showFirstLabel: false}, 
                                    { // right y axis
                                        linkedTo: 0, gridLineWidth: 0, opposite: true,
                                        title: {text: 'Temperature (℉)'},
                                                labels: {align: 'right', x: -3, y: 16,
                                                    formatter: function() {
                                        return Highcharts.numberFormat(this.value, 0);}
                                    },
                                    showFirstLabel: false
                            }],
                            legend: {align: 'left', verticalAlign: 'top', y: 20,
                                    floating: true, borderWidth: 0},
                            tooltip: {shared: true, crosshairs: true},
                            plotOptions: {  series: {cursor: 'pointer',
                                                    point: {events: {
                                                    click: function() {
                                                    hs.htmlExpand(null, {
                                                    pageOrigin: {
                                                        x: this.pageX, 
                                                        y: this.pageY
                                                    },
                                                    headingText: this.series.name,
                                                    maincontentText: Highcharts.dateFormat('%A, %b %e, %Y', this.x) +':<br/> '+ 
                                                    this.y +'(℉)',
                                                    width: 200});
                                                    }}},
                            marker: {lineWidth: 1}}},
                            series: [   {name: 'Hot Liqour Tank'},
                                        {name: 'MashTun'},
                                        {name: 'Brew Kettle'},
                                        {name: 'Post Chiller'},
                                        {name: 'Control Box'}
                                    ]
            };
    <!-- End Chart Options -->
    var chart;
    //after DOM is loaded setup timeout to call the ajax method
    $(document).ready(function() {
        //call function to render the chart and setup the options
        renderChart();
    });

    //this function requests the data
    function reqData(){
        $.ajax({
            url: "http://192.168.0.11/"+Math.random(),
            dataType: "jsonp",              
            jsonp: "callback",
            jsonpCallback: "jsonpcallback"
        });
    }

    function jsonpcallback(rtndata) { 
        for(var i = 0; i < rtndata.length; i++){
            if(rtndata[i].sensor=="hlt")
            {
                chart.series[0].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="mt")
            {
                chart.series[1].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="bk")
            {
                chart.series[2].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="pc")
            {
                chart.series[3].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="box")
            {
                chart.series[4].addPoint([rtndata[i].time, rtndata[i].temp]);
            }       
        }
    }

    function renderChart(){
        chart = new Highcharts.Chart(options);
    }

    //continually poll for data
    setInterval(reqData, 5000);
    </script>

I have a Highchart that is receiving JSON data via AJAX and jQuery. But my chart doesn't show the points until I hover the mouse over the chart. Even then the points are all at the very top of the chart. I don't think I'm adding my points to the series correctly. Please tell me what I'm doing wrong in the jsonpcallback function.

Thanks!

        <script type="text/javascript">
    <!-- Begin Chart options-->

    // define the options
            var options = {
                            chart: {renderTo: 'container'},
                            title: {text: 'Brewery'},
                            subtitle: {text: ' '},
                            xAxis: {text: 'Time',type: 'datetime'},
                            yAxis: [{ // left y axis
                                        title: {text: 'Temperature (℉)'},
                                        labels: {align: 'left', x: 3, y: 16,
                                                formatter: function() {
                                        return Highcharts.numberFormat(this.value, 0);}
                                    },
                                    showFirstLabel: false}, 
                                    { // right y axis
                                        linkedTo: 0, gridLineWidth: 0, opposite: true,
                                        title: {text: 'Temperature (℉)'},
                                                labels: {align: 'right', x: -3, y: 16,
                                                    formatter: function() {
                                        return Highcharts.numberFormat(this.value, 0);}
                                    },
                                    showFirstLabel: false
                            }],
                            legend: {align: 'left', verticalAlign: 'top', y: 20,
                                    floating: true, borderWidth: 0},
                            tooltip: {shared: true, crosshairs: true},
                            plotOptions: {  series: {cursor: 'pointer',
                                                    point: {events: {
                                                    click: function() {
                                                    hs.htmlExpand(null, {
                                                    pageOrigin: {
                                                        x: this.pageX, 
                                                        y: this.pageY
                                                    },
                                                    headingText: this.series.name,
                                                    maincontentText: Highcharts.dateFormat('%A, %b %e, %Y', this.x) +':<br/> '+ 
                                                    this.y +'(℉)',
                                                    width: 200});
                                                    }}},
                            marker: {lineWidth: 1}}},
                            series: [   {name: 'Hot Liqour Tank'},
                                        {name: 'MashTun'},
                                        {name: 'Brew Kettle'},
                                        {name: 'Post Chiller'},
                                        {name: 'Control Box'}
                                    ]
            };
    <!-- End Chart Options -->
    var chart;
    //after DOM is loaded setup timeout to call the ajax method
    $(document).ready(function() {
        //call function to render the chart and setup the options
        renderChart();
    });

    //this function requests the data
    function reqData(){
        $.ajax({
            url: "http://192.168.0.11/"+Math.random(),
            dataType: "jsonp",              
            jsonp: "callback",
            jsonpCallback: "jsonpcallback"
        });
    }

    function jsonpcallback(rtndata) { 
        for(var i = 0; i < rtndata.length; i++){
            if(rtndata[i].sensor=="hlt")
            {
                chart.series[0].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="mt")
            {
                chart.series[1].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="bk")
            {
                chart.series[2].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="pc")
            {
                chart.series[3].addPoint([rtndata[i].time, rtndata[i].temp]);
            }
            else if(rtndata[i].sensor=="box")
            {
                chart.series[4].addPoint([rtndata[i].time, rtndata[i].temp]);
            }       
        }
    }

    function renderChart(){
        chart = new Highcharts.Chart(options);
    }

    //continually poll for data
    setInterval(reqData, 5000);
    </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2025-01-04 23:50:40

我想你已经快到了;我认为您缺少的唯一部分是从图表的加载事件中对您的 regData 函数的初始调用。

因此,在定义选项之后的第二行,将此行:更改

 chart: {renderTo: 'container'},

为:

chart: {renderTo: 'container', events: {load: regData}},

I think you are nearly there; the only piece i believe you are missing is an initial call to your regData function from the chart's load event.

So to your second line after define the options, change this line:

 chart: {renderTo: 'container'},

to this:

chart: {renderTo: 'container', events: {load: regData}},
じее 2025-01-04 23:50:40

尝试将时间字符串转换为 Javascript 日期时间对象。然后 Highcharts 将正确读取日期时间。例如,如果 JSON 日期的格式为“YYYY-MM-DD”,您可以使用以下代码:

    var dates = [];
    $.each(jsondata[0], function(itemNo, item) {
        year = parseInt(item.split('-')[0], 10);
        month = parseInt(item.split('-')[1], 10);
        day = parseInt(item.split('-')[2], 10);
        dt = Date.UTC(parseInt(year), parseInt(month), parseInt(day));
        series[0].data.push([dt, parseInt(jsondata[2][itemNo], 10)]);
        series[1].data.push([dt, parseInt(jsondata[1][itemNo], 10)]);
        dates.push(dt);
    });

    options.series = series;

Try converting the time strings to Javascript datetime objects. Highcharts will then read the datetimes correctly. For example, if the JSON date is in the format 'YYYY-MM-DD' you can use this code:

    var dates = [];
    $.each(jsondata[0], function(itemNo, item) {
        year = parseInt(item.split('-')[0], 10);
        month = parseInt(item.split('-')[1], 10);
        day = parseInt(item.split('-')[2], 10);
        dt = Date.UTC(parseInt(year), parseInt(month), parseInt(day));
        series[0].data.push([dt, parseInt(jsondata[2][itemNo], 10)]);
        series[1].data.push([dt, parseInt(jsondata[1][itemNo], 10)]);
        dates.push(dt);
    });

    options.series = series;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文