【问题标题】:How to make dynamic graph with rickshaw without refreshing the browser?如何在不刷新浏览器的情况下用人力车制作动态图?
【发布时间】:2015-06-19 14:56:40
【问题描述】:

我想制作动态图,它总是提供新鲜的数据而不刷新整个页面。在以下示例中,他们根据随机数据构建图表。但是在这个问题中,数据不是新鲜的,只有刷新浏览器才会得到新鲜的数据。 Please see this link 用于 ajax 请求,我使用这里的参考 please see it

我复制了下面的整个代码。我想制作可移动的图形,例如实时更新一些像flot,但不想使用flot,而是使用人力车。

    <!doctype>
<head>
        <title>rickShaw greaph examples</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link type="text/css" rel="stylesheet" href="../src/css/graph.css">
        <link type="text/css" rel="stylesheet" href="../src/css/legend.css">
        <link type="text/css" rel="stylesheet" href="../src/css/detail.css">
    <link type="text/css" rel="stylesheet" href="css/lines.css">

    <script src="../vendor/d3.v3.js"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

    <script src="../rickshaw.js"></script>
</head>
<body>

<div id="chart_container" style="margin:0px auto;width:660px;">
    <div id="chart"></div>
        <div id="legend_container">
            <div id="smoother" title="Smoothing"></div>
            <div id="legend"></div>
        </div>
        <div id="slider"></div>
</div>

<script>
       // set up our data series with 50 random data points
        var seriesData = [ [], [], [] ];
        var random = new Rickshaw.Fixtures.RandomData(150);

        for (var i = 0; i < 150; i++) {
            random.addData(seriesData);
        }

var ajaxGraph = new Rickshaw.Graph.Ajax( {

    element: document.getElementById("chart"),
    width: 400,
    height: 200,
    renderer: 'line',
    series: [
        {
            name: 'New York',
                        data: seriesData[0],
            color: '#c05020',
        }, {
            name: 'London',
                        data: seriesData[0],
            color: '#30c020',
        }, {
            name: 'Tokyo',
                        data: seriesData[0],
            color: '#6060c0'
        }
    ]
} );
ajaxGraph.render();

var hoverDetail = new Rickshaw.Graph.HoverDetail( {
            graph: graph
        } );

        var legend = new Rickshaw.Graph.Legend( {
            graph: graph,
            element: document.getElementById('legend')

        } );

        var shelving = new Rickshaw.Graph.Behavior.Series.Toggle( {
            graph: graph,
            legend: legend
        } );


        var axes = new Rickshaw.Graph.Axis.Time( {
            graph: graph
        } );
        axes.render();
</script>

</body>

【问题讨论】:

  • @thisOneGuy,感谢您的回复。你能告诉我如何在这里使用 Ajax

标签: javascript jquery d3.js rickshaw


【解决方案1】:

而不是使用

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

for (var i = 0; i < 150; i++) {
    random.addData(seriesData);
}

在某个时间间隔内执行 ajax 调用并更新图表的数据系列。有点像这样(取自他们使用 ajax 的其他示例)这是我使用的示例 http://www.flotcharts.org/flot/examples/ajax/index.html

data = [];

$.plot("#placeholder", data, options);

function fetchData() {

  function onDataReceived(series) {

    // Load all the data in one pass; if we only got partial
    // data we could merge it with what we already have.

    data = [ series ];
    $.plot("#placeholder", data, options);
  }

  $.ajax({
    url: "data-eu-gdp-growth-" + iteration + ".json",
    type: "GET",
    dataType: "json",
    success: onDataReceived
  });

  setTimeout(fetchData, 1000);
}

setTimeout(fetchData, 1000);

希望这会有所帮助!

【讨论】:

  • 感谢回复,我看到了你给的链接,它不是获取实时数据。而且没有源代码..
  • 你可以查看页面源代码,所有代码都在里面
猜你喜欢
  • 2021-01-13
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
相关资源
最近更新 更多