d3.js自动数据更新 - updateData

时间:2013-12-09 19:31:48

标签: javascript csv d3.js

我使用D3.js制作了一个简单的条形图。图表的数据来自csv文件。我希望图表每2.5秒更新一次,显示新数据。现在,在更新csv文件后,X轴和Y轴将更新,但条形图和鼠标悬停工具提示会显示旧值。我看了很多不同的例子。以下是我引用的一些内容。有谁知道发生了什么? http://mbostock.github.io/d3/tutorial/bar-2.html

http://www.d3noob.org/search?q=update

d3 update data and update graph

<!DOCTYPE html>
<head>
<meta charset="utf-8">

<title>Bar Chart</title>

<style type="text/css">

.bar {
  fill: steelblue;
}

.bar:hover {
  fill: brown;
}
div.tooltip {
    position: absolute;
    text-align: left;
    width: auto;
    height: auto;
    padding: 2px;
    font-family: sans-serif;
    font-size: 14px;
    background: white;
    border: 2;
    border-radius: 5px;
    pointer-events: none;
}


.axis {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}



</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = {top: 20, right: 20, bottom: 30, left: 45},
    width = 1000 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
    .range([height, 0]);

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

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickPadding(5);

//var data = [{"label":"Boone","values":0.8},{"label":"Story","values":0.2},{"label":"Polk","values":0.4}]

var div = d3.select("body").append("div")
    .attr("class", "tooltip")
    .style("position", "absolute")
    .style("opacity", 0);

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")


    d3.csv("data.csv", function(error, data) {
        data.forEach(function(d) {  
            d.label = d.label;
            d.values = +d.values;
        });

        x.domain(data.map(function(d) { return d.label; }));
        y.domain([0, d3.max(data, function(d) { return d.values; })]);

        svg.append("g")
        .attr("class", "y axis")
        .call(yAxis)
      .append("text")
        .attr("transform", "rotate(-0)")
        .attr("x", -10)
        .attr("y", -16)
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("Values");

        svg.selectAll(".bar")
            .data(data)
          .enter().append("rect")
        .on("mouseover", function(d) {
            div.transition()
                .duration(100)
                .style("opacity", 1)
                .style("left", (d3.event.pageX)+ "px")
                .style("top", (d3.event.pageY) + "px");
            div.html("<p>Label: " + d.label+ "<br>Value: " + d.values);
            d3.select("#tooltip").classed("hidden", false);
        })

        .attr("class", "bar")
        .attr("x", function(d) { return x(d.label); })
        .attr("width", x.rangeBand())
        .attr("y", function(d) { return y(d.values); })
        .attr("height", function(d) { return height - y(d.values); });

        svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis)
       .append("text")
        .attr("class", "label")
        .attr("x", width)
        .attr("y", 30)
        .style("text-anchor", "end")
        .text("Label");
    });

var inter = setInterval(function() {
    updateData();
},2500);

function updateData() {
    d3.csv("data.csv", function(error, data) {
        data.forEach(function(d) {  
            d.label = d.label;
            d.values = +d.values;
        });

        x.domain(data.map(function(d) { return d.label; }));
        y.domain([0, d3.max(data, function(d) { return d.values; })]);

        var svg = d3.select("body")

        var vis = svg.transition();
            vis.select(".x.axis")
                .duration(750)
                .call(xAxis)
            vis.select(".y.axis")
                .duration(750)
                .call(yAxis)
            vis.select(".rect")
                .duration(750)
                .attr(".x", function(d) { return x(d.label); })
                .attr(".y", function(d) { return y(d.values); })
    });
}

</script>
</body>
</html>

0 个答案:

没有答案