��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - how to add dynamic data in a highchart? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add dynamic data in a highchart? - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share Improve this question edited Jan 30, 2012 at 23:15 doug 70.1k25 gold badges171 silver badges201 bronze badges asked Jan 25, 2012 at 7:05 cobolstinkscobolstinks 7,15318 gold badges73 silver badges103 bronze badges 2
  • Did you try using console.info or console.log statements to log what you're doing? Perhaps using a debugger like Chrome Dev tools or Firebug will help show you what you're doing wrong. Also, post a link to the site so we can see what's happening. Thanks! – jamesmortensen Commented Jan 25, 2012 at 7:58
  • To answer this question, it is required to know the structure of JSON? What is inside rtndata? As it is not possible for us to know what is returned by JSON – Umesh Patil Commented Jan 25, 2012 at 8:03
Add a ment  | 

2 Answers 2

Reset to default 3

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}},

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;
发布评论

评论列表(0)

  1. 暂无评论