D3 - 以设置的延迟逐行加载和显示数据
D3 - Load in and display data, row by row with set delay
我正在尝试使用时间序列数据在地图上绘制气泡。我想做的是根据它们的日期慢慢绘制这些气泡,而不是一次全部绘制。
类似于:http://www.nytimes.com/interactive/2014/upshot/mapping-the-spread-of-drought-across-the-us.html?_r=0
下面是一些示例数据:
日期计数代码,国家,纬度,纬度,计数器16-1-28 3 安道尔,42.5,1.516667,0.5771212551/29/16,146,ARE,阿拉伯联合酋长国,24.466666667,54.366667,2.2643528561/30/16,13,AFG,阿富汗,34.51666667,69.183333,1.213943352
D3 地图示例
我已经看过MB关于路径转换的教程,Udacity关于D3的课程,以及许多关于Stack Overflow的问题。
我以前尝试过使用 setInterval
和 setTimeout
但大多数示例都是使用多个数据文件的。我想逐行使用一个数据文件。
法典:
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.center([0, 5 ])
.scale(200)
.rotate([-180,0]);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([0, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:white'>" + d.count + "</span>"+ "<br/>" + d.country;
})
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var path = d3.geo.path()
.projection(projection);
var g = svg.append("g");
// load and display the World
d3.json("world-110m2.json", function(error, topology) {
svg.call(tip)
// load and display the cities
d3.csv("cities2_or.csv", function(error, data) {
max = d3.max(data, function(d)
{return +d.counter})
coloring = d3.scale.linear()
.domain([0, max])
.range(["blue", "green"])
radiusing = d3.scale.linear()
.domain([0, max])
//.domain([0, 100])
.range([2, 30])
g.selectAll("circle")
.data(data)
.enter()
.append("circle")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr("cx", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("cy", function(d) {
return projection([d.lon, d.lat])[1];
})
.style("r", function(d){
return radiusing(+d.counter)
;})
//.style("opacity", .5)
.style("fill", function(d){
return coloring(+d.counter);
})
});
感谢您的任何帮助
我采纳了@adilapapaya建议并尝试使用延迟功能,但我只能绘制第一个点。我用以下内容代替了上面使用的g.selectAll
。但是,这只会在我的csv文件中绘制第一个点,然后停止。
g.append("circle")
.data(data)
.attr("cx", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("cy", function(d) {
return projection([d.lon, d.lat])[1];
})
.style("r", 20)
.transition()
.duration(1)
.delay(function(d, i) { return i*1; })
.style("r",30)
.style("fill","green");
感谢Andrew Guy帮助我找到了JS小提琴的例子。工作完美。我的问题不是事先使用enter
。这是最终代码。如果有人有任何问题,请回复。
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.center([0, 5 ])
.scale(200)
.rotate([-180,0]);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([0, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:white'>" + d.count + "</span>"+ "<br/>" + d.country;
})
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var path = d3.geo.path()
.projection(projection);
var g = svg.append("g");
// load and display the World
d3.json("world-110m2.json", function(error, topology) {
svg.call(tip)
// load and display the cities
d3.csv("cities2_or.csv", function(error, data) {
max = d3.max(data, function(d)
{return +d.counter})
coloring = d3.scale.linear()
.domain([0, max])
.range(["blue", "green"])
radiusing = d3.scale.linear()
.domain([0, max])
//.domain([0, 100])
.range([2, 30])
g.selectAll("circle")
.data(data)
.enter()
.append("circle")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr("cx", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("cy", function(d) {
return projection([d.lon, d.lat])[1];
})
// .style("r", function(d){
// return radiusing(+d.counter)
// ;})
//.style("opacity", .5)
.style("fill", function(d){
return coloring(+d.counter);})
.style("r",0)
.transition()
.duration(500)
.delay(function(d, i) { return i*200; })
.style("r",30);
});
g.selectAll("path")
.data(topojson.object(topology, topology.objects.countries)
.geometries)
.enter()
.append("path")
.attr("d", path)
});
// zoom and pan
var zoom = d3.behavior.zoom()
.on("zoom",function() {
g.attr("transform","translate("+
d3.event.translate.join(",")+")scale("+d3.event.scale+")");
g.selectAll("circle")
.attr("d", path.projection(projection));
g.selectAll("path")
.attr("d", path.projection(projection));
});
svg.call(zoom)
相关文章:
- 如何在生成下载文件时显示加载动画
- 如何在谷歌字体加载时显示加载图像
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何用ajax实现加载显示
- 使用javascript加载/显示图像并获得%下载
- 谷歌地图不会加载/显示KML文件
- 我的图片不是使用惰性加载显示的
- javascript d3可视化不加载/显示,直到点击HTML输入
- 导航标签动态加载/显示
- jQuery Select2远程数据加载:显示选项而不是占位符
- Console.log一些东西一旦异步Facebook喜欢按钮加载(显示)
- PHP停止表单重新加载显示错误Js
- 预加载显示/隐藏分区
- ReCaptcha不会在IE中加载/显示,但在Chrome和safari中可以完美运行
- dom事件——通过JavaScript加载显示一个又一个元素
- jQuery延迟加载-显示的问题:无
- Json.数据没有正确加载/显示
- 在每个页面加载显示不同的图像
- 如何检测PDF数据对象是否在html对象中加载/显示/有效
- Jquery添加页面加载显示请求的标签功能