javascript - d3js多图自动更新

标签 javascript d3.js auto-update

我真的需要一些帮助来解决 d3js 相关的问题。我做了我所知道的一切并到处搜索,但仍然找不到解决方案。我有一个简单的数据库,如下所示。 执行代码时,它会读取所有数据,一切都很好,当它执行刷新时,它会更新温度图,但不会更新湿度...我尝试了很多方法,包括将 valueline2 添加到更新函数中,但它仍然不起作用。 ..任何帮助将不胜感激,谢谢。

dtg |温度|哼
2016-03-02 09:14:00 23 40
2016-03-02 09:10:00 22 45

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

body { font: 14px Arial;}

path { 
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}
/* Addon 5,6,7 - part 1*/
.grid .tick {
    stroke: lightgrey;
    stroke-opacity: 0.7;
    shape-rendering: crispEdges;
}
.grid path {
      stroke-width: 0;
}


</style>
<body>

<!-- load the d3.js library -->    
<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 60, bottom: 50, left: 50},
    width = 600 - margin.left - margin.right,
    height = 300 - margin.top - margin.bottom;

// Parse the date / time
/* var parseDate = d3.time.format("%d-%b-%y").parse; */
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(6);

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(8);

// Define the line
var valueline = d3.svg.line()
     .interpolate("basis")  
    .x(function(d) { return x(d.dtg); })
    .y(function(d) { return y(d.temperature); });

// Define the 2nd line -- Addon 9 part 1
var valueline2 = d3.svg.line()
    .interpolate("basis")   
    .x(function(d) { return x(d.dtg); })
    .y(function(d) { return y(d.hum); });

// Adds the svg canvas
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 + ")");

// Addon 5,6,7 - part 2
function make_x_axis() {        
    return d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(8)
}

function make_y_axis() {        
    return d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(8)
}


// Get the data
d3.json("data.php", function(error, data) {
    data.forEach(function(d) {
    d.dtg = parseDate(d.dtg);
    d.temperature = +d.temperature;
        d.hum = +d.hum; // Addon 9 part 3
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.dtg; }));
    y.domain([0, d3.max(data, function(d) { return Math.max(d.temperature, d.hum)+5; })]);
    // y.domain([0, d3.max(data, function(d) { return d.temperature; })]);

    // Add the valueline path.
    svg.append("path")
    .attr("class", "line")
    .attr("d", valueline(data));

    // Add the valueline2 path - Addon 9 part 2
    svg.append("path")      
    .attr("class", "line")
    .style("stroke", "red")
    .attr("d", valueline2(data));   

    // Add the X Axis
    svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

    // Add the Y Axis
    svg.append("g")
    .attr("class", "y axis")
    .call(yAxis);

// Addon 4
    svg.append("g")              // Add the Y Axis
    .attr("class", "y axis")
    .call(yAxis);

// Addon 5,6,7 - part 3
  svg.append("g")           
    .attr("class", "grid")
    .attr("transform", "translate(0," + height + ")")
    .call(make_x_axis()
        .tickSize(-height, 0, 0)
        .tickFormat("")
    )

    svg.append("g")         
    .attr("class", "grid")
    .call(make_y_axis()
        .tickSize(-width, 0, 0)
        .tickFormat("")
    )

});


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

// ** Update data section (Called from the onclick)
function updateData() {

    // Get the data again
    d3.json("data.php", function(error, data) {
    data.forEach(function(d) {
    d.dtg = parseDate(d.dtg);
    d.temperature = +d.temperature;
    d.hum = +d.hum; 
    });

   // Scale the range of the data again 
    x.domain(d3.extent(data, function(d) { return d.dtg; }));
    y.domain([0, d3.max(data, function(d) { return Math.max(d.temperature, d.hum) + 5; })]); // Addon 9 part 4

    // Select the section we want to apply our changes to
    var svg = d3.select("body").transition();

    // Make the changes
    svg.select(".line").duration(750).attr("d", valueline(data));
    svg.select("x.axis").duration(750).call(xAxis);
    svg.select("y.axis").duration(750).call(yAxis);


    });
}

</script>
</body>

最佳答案

您需要为不同的线路指定不同的名称。现在两条线只有相同的类:“.line”。

// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("id", "line1") //new id
.attr("d", valueline(data));

// Add the valueline2 path - Addon 9 part 2
svg.append("path")      
.attr("class", "line")
.attr("id", "line2") //new id
.style("stroke", "red")
.attr("d", valueline2(data));   

然后在更新中,您需要实际更新这两行:

// Make the changes
svg.select("#line1").duration(750).attr("d", valueline(data)); //update line 1
svg.select("#line2").duration(750).attr("d", valueline2(data)); //update line 2
svg.select("x.axis").duration(750).call(xAxis);
svg.select("y.axis").duration(750).call(yAxis);

另一种更“D3 友好”的方法是将温度数据提供给第一个路径,将湿度数据提供给第二个路径,然后在每个路径上调用 valueline 的变体,每个路径一个及其各自的数据。

关于javascript - d3js多图自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35751425/

相关文章:

android - Listview Widget 不会自动更新

javascript - 在 AngularJS 控件中调用 jQuery 函数

javascript - XHR 从单独的目录加载资源

javascript - d3.js scaleTime 返回未定义

javascript - 如何使用 d3 更新/覆盖 map 和图例内容

java - 我可以强制我的 Android 应用程序从 Play 商店更新吗?

android - 如何让你的安卓应用自动更新

javascript - JSON 中的变量

javascript - 根据数据中的附加列更改 d3 Sankey 图中的节点颜色

javascript - 如何正确给d3饼图添加标签?