使用--AJAX更新d3图表的最佳方法是什么?

时间:2014-04-01 11:14:11

标签: javascript jquery ajax d3.js

我学会了几个d3块。我在jquery的帮助下制作了响应式d3直方图。

现在我想要使用ajax更新d3图表。

我刚刚进入jquery。

了解ajax的工作原理。

搜索了很长时间,但我无法在官方d3网站或其他任何地方获得任何有用的示例。

通过ajax获取更新d3图表的基本功能,对我来说,任何帮助都会很有成效。

提前致谢!!

3 个答案:

答案 0 :(得分:25)

我知道OP指定了jQuery,但对于那些不想要另一个框架的Google员工,使用requestjson有一种原生的D3方式:

d3.request(url, function(error, response) {
    // Now use response to do some d3 magic
});

d3.json(url, function(error, response) {
    // Now use response to do some d3 magic
});

答案 1 :(得分:15)

你只需要在你的ajax成功中调用你的d3函数:

    $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: 'url to your web servise',
            dataType: 'json',
            async: true,
            data: "{}", 
            success: function (data) {
               var pos_data = data;
               div_name = "div.histogram";

        draw_histogram(div_name, pos_data);

            },
            error: function (result) {



}
    })



//The drawing of the histogram has been broken out from the data retrial 
    // or computation. (In this case the 'Irwin-Hall distribution' computation above)

    function draw_histogram(reference, pos_data){

        $(reference).empty()

        //The drawing code needs to reference a responsive elements dimneions
        var width = $(reference).width();
        // var width = $(reference).empty().width(); we can chain for effeicanecy as jquery returns jquery.

        // var height = 230;  // We don't want the height to be responsive.

        var margin = {top: 10, right: 30, bottom: 40, left: 30},
        // width = 960 - margin.left - margin.right,
        height = 270 - margin.top - margin.bottom;


        var histogram = d3.layout.histogram() (pos_data);
        //var neg_histogram = d3.layout.histogram()(neg_data);

        var x = d3.scale.ordinal()
            .domain(histogram.map(function(d) { return d.x; }))
            .rangeRoundBands([0, width]);

        var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom");


        var y = d3.scale.linear()
            .domain([0, d3.max(histogram.map(function(d) { return d.y; }))])
            .range([0, height]);

        //var ny = d3.scale.linear()
        //    .domain([0, d3.max(neg_histogram.map(function(d) { return d.y; }))])
        //    .range([0, height]);

        var svg = d3.select(reference).append("svg")
            .attr("width", width)
            .attr("height", height + 20);



        svg.selectAll("rect")
            .data(histogram)
          .enter().append("rect")
            .attr("width", x.rangeBand())
            .attr("x", function(d) { return x(d.x); })
            .attr("y", function(d) { return height - y(d.y); })
            .attr("height", function(d) { return y(d.y); });


        svg.append("line")
            .attr("x1", 0)
            .attr("x2", width)
            .attr("y1", height)
            .attr("y2", height);

        svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + (height)  + ")")
            .call(xAxis);
    };

    //Bind the window resize to the draw method.
    //A simple bind example is

    //A better idom for binding with resize is to debounce
    var debounce = function(fn, timeout) 
    {
      var timeoutID = -1;
      return function() {
        if (timeoutID > -1) {
          window.clearTimeout(timeoutID);
        }
        timeoutID = window.setTimeout(fn, timeout);
      }
    };

    var debounced_draw = debounce(function() {
      draw_histogram(div_name, pos_data);
    }, 125);

    $(window).resize(debounced_draw);

答案 2 :(得分:4)

任何AJAX请求的想法是将请求发送到将生成HTML标记的页面或客户端可以使用的数据。如果希望通过AJAX更新下拉列表,请确保服务器将下拉项列表作为XML / JSON或HTML标记发送,并且调用者函数将HTML放在适当的位置。

如果您想要实时更新,请考虑定期向服务器询问数据,然后将数据与上一个副本进行匹配,并查看新数据是否已到达。如果有,重新渲染。

要了解如何使用AJAX实时动态更新内容,请查看我的Dynamic-table github project

在您的本地服务器上托管项目。 (可以是WAMP / LAMP / Tomcat的任何东西)并打开sample.html

现在,更改datafile.json中的内容。您将立即看到表格中呈现的更改。

您希望获得相同的功能,但使用d3图表。但是我如何获取内容并定期处理它的想法仍然是一样的。

希望有所帮助。