te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Live updating Rickshaw graph - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Live updating Rickshaw graph - Stack Overflow

programmeradmin4浏览0评论

I'm trying to use Rickshaw to create a nice placeholder graph that shows live updating with random data, like so:

var series = [[], []];
var random = new Rickshaw.Fixtures.RandomData(150);

for(var i = 0; i < 80; i++) {
    random.addData(series);
}

var throughput = new Rickshaw.Graph( {
    element: document.querySelector("#throughput_chart"),
    width: "300",
    height: "200",
    renderer: "line",
    series: [{
        color: "gold",
        data: series[0]
    }]
});

var alerts = new Rickshaw.Graph( {
    element: document.querySelector("#alerts_chart"),
    width: "300",
    height: "200",
    renderer: "line",
    series: [{
        color: "red",
        data: series[1]
    }]
});

throughput.render();
alerts.render();

setInterval(function() {
    random.addData(series);
    throughput.update();
    alerts.update();
    /* XXX: This causes the data to stop drawing properly
    series.forEach(function(s) {
        s.shift();
    });
    */
}, 1000);

This works, except for the mented out part. As it is, it keeps adding data, which means the graph gets more and more crowded. I'd like to be able to remove the first item each time I add a new item to the end, in order to keep the graph with the same number of data points, but when I put that code in it quickly causes the graph not to display any data, even though console.log shows that the array is still full of data.

How can I make this work, so that I am only displaying a fixed number of data points at a time?

I'm trying to use Rickshaw to create a nice placeholder graph that shows live updating with random data, like so:

var series = [[], []];
var random = new Rickshaw.Fixtures.RandomData(150);

for(var i = 0; i < 80; i++) {
    random.addData(series);
}

var throughput = new Rickshaw.Graph( {
    element: document.querySelector("#throughput_chart"),
    width: "300",
    height: "200",
    renderer: "line",
    series: [{
        color: "gold",
        data: series[0]
    }]
});

var alerts = new Rickshaw.Graph( {
    element: document.querySelector("#alerts_chart"),
    width: "300",
    height: "200",
    renderer: "line",
    series: [{
        color: "red",
        data: series[1]
    }]
});

throughput.render();
alerts.render();

setInterval(function() {
    random.addData(series);
    throughput.update();
    alerts.update();
    /* XXX: This causes the data to stop drawing properly
    series.forEach(function(s) {
        s.shift();
    });
    */
}, 1000);

This works, except for the mented out part. As it is, it keeps adding data, which means the graph gets more and more crowded. I'd like to be able to remove the first item each time I add a new item to the end, in order to keep the graph with the same number of data points, but when I put that code in it quickly causes the graph not to display any data, even though console.log shows that the array is still full of data.

How can I make this work, so that I am only displaying a fixed number of data points at a time?

Share Improve this question edited Mar 5, 2016 at 21:12 Peter 3,9561 gold badge24 silver badges44 bronze badges asked Jun 5, 2013 at 15:38 singpolymasingpolyma 11.3k5 gold badges51 silver badges75 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

Following the lead of the example on the documentation for Rickshaw (http://code.shutterstock./rickshaw/examples/fixed.html), I've knocked together something similar to what you need:

JS FIDDLE

var tv = 50;

var throughput = new Rickshaw.Graph({
    element: document.querySelector("#throughput_chart"),
    width: "300",
    height: "200",
    renderer: "line",
    series: new Rickshaw.Series.FixedDuration([{
        name: 'one', color: 'gold'
    }], undefined, {
        timeInterval: tv,
        maxDataPoints: 100,
        timeBase: new Date().getTime() / 1000
    })
});

var alerts = new Rickshaw.Graph({
    element: document.querySelector("#alerts_chart"),
    width: "300",
    height: "200",
    renderer: "line",
    series: new Rickshaw.Series.FixedDuration([{
        name: 'one', color: 'red'
    }], undefined, {
        timeInterval: tv,
        maxDataPoints: 100,
        timeBase: new Date().getTime() / 1000
    })
});

for(var i = 0; i < 100; i++){
    addRandomData(throughput);
    addRandomData(alerts);
}

throughput.render();
alerts.render();

setInterval(function () {

    addRandomData(throughput);
    addRandomData(alerts);
    throughput.render();
    alerts.render();

}, tv);

function addRandomData(chart) {
    var data = {
        one: Math.floor(Math.random() * 40) + 120
    };
    chart.series.addData(data);
}

The FixedDuration class does not work with RandomData, as far as I know. What I do, is remove the first datapoint in the series arrays so that the graph does not get crowded and it streams to the left as more data is added:

setInterval( function() {
    random.addData(seriesData);
    for (i=0; i < seriesData.length; i++){
        var series0 = seriesData[i][seriesData[i].length-1];
        seriesData[i].shift();
        seriesData[i].push(series0);
    }
    graph.update();
}, 3000 );

I am not entirely sure why you need to add the last added datapoint back on to the end of the array (series[x].push(seriesX)), but it does not work otherwise. It does not have the effect of doubling the last datapoint, only one is added. Hope this works for you!

发布评论

评论列表(0)

  1. 暂无评论