如何使用人力车制作动态图表而不刷新浏览器?

时间:2015-06-19 14:56:41

标签: javascript jquery d3.js rickshaw

我想制作动态图表,它总是提供新数据而不刷新整个页面。在以下示例中,他们在随机数据上构建图形。但是在这个问题数据不新鲜的情况下,只有刷新浏览器才能获得新鲜数据。 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>

1 个答案:

答案 0 :(得分:0)

而不是使用

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);

希望这有帮助!