自动刷新文本d3js
Auto refresh text d3js
我需要自动刷新使用d3js显示的文本。Source是一个json文件。我有下面的d3js代码(基于mbostock的自动刷新代码图表)。文本不是每次刷新,因为它应该。
我试图改变json文件中的值,但发现显示的文本不刷新。在updateData()中,我将值打印到控制台并正确显示。不确定为什么显示的文本没有显示正确的值。
jsoncir.html
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
</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: 100, right: 20, bottom: 30, left: 50},
width = 900 - margin.left - margin.right,
height = 570 - margin.top - margin.bottom;
// Set the ranges
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// 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 + ")");
// Get the data
d3.json("final_json_file.json", function(error, data) {
data.forEach(function(d) {
d.close = d.cnt;
});
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("class", "point")
.attr("r", 40.5)
.attr("fill", "#BADBDA")
svg.selectAll("text")
.data(data)
.enter().append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.close })
});
var inter = setInterval(function() {
updateData();
}, 12000);
// ** Update data section (Called from the onclick)
function updateData() {
// Get the data again
d3.json("final_json_file.json", function(error, data) {
data.forEach(function(d) {
d.close = d.cnt;
console.log(d.close);
});
// Select the section we want to apply our changes to
var svg = d3.select("body").transition();
// console.log('ehllo');
// Make the changes
svg.select("text") // change the line
.duration(12000)
.text(function(d) { return d.close +1});
});
}
</script>
</body>
输入json文件final_json_file。json
[{"问":49976年,"st_time":"2016-09-01"}]
不更新有界数据到text
元素。起初,您使用svg.selectAll("text").(data)
将数据绑定到元素,但在updateData
函数中,您没有更新数据。你应该将新数据绑定到元素,然后更新它们的文本。
// Select the section we want to apply our changes to
var svg = d3.select("body");
// Make the changes
svg.selectAll("text")
.data(data)
.transition()
.duration(1000)
.text(function (d) {
return d.close + 1
});
相关文章:
- 如何使用jquery在填充自动完成的值后使文本框只读
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- d3js文本传输-示例代码不起作用
- 在鼠标悬停d3js上更改SVG文本的文本
- D3JS SVG 文本和形状在同一行上
- d3js换行符在使用html的文本中不起作用
- 在d3js中的svg圆圈内添加svg文本
- 在d3js Force Layout中向节点添加箭头和文本
- 在带有D3js的SVG中选择带有tspan说明符的文本元素的内容
- d3js-更新多折线图中的文本元素时出现问题
- D3JS Sunburst不更新文本
- 文本不显示在强制布局d3js,但目前在视图
- 自动刷新文本d3js
- 如何将angularjs动态文本输入与d3js相结合,创建svg圆圈进行过滤
- d3js-svg文本选择id
- 如何更改d3js图形中图例的文本颜色
- 如何使d3js图形上的文本与图形一起缩放
- D3JS嵌套插入.SVG示例:文本进入组
- 文本阻塞圆圈点击方法d3js