javascript-D3:在多行折线图中跳过空值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-D3:在多行折线图中跳过空值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1879字,纯文字阅读大概需要3分钟。
内容图文
我有一个动态数组来显示带有多条线的线图.例:
var data =
[[{x:2005, y:100}, {x:2007, y:96.5}, {x:2009, y:100.3}, {x:2011, y:102.3}],
[{x:2005, y:100}, {x:2007, y:105}, {x:2009, y:102}, {x:2011, y:104}]]
我的脚本的这一部分将划清界线:
graph.selectAll("path.line")
.data(data)
.enter().append("path")
.attr("class", "line")
.style("stroke", function(d, i) { return d3.rgb(z(i)); })
.style("stroke-width", 2)
.attr("d", d3.svg.line()
.y(function(d) { return y(d.y); })
.x(function(d,i) { return x(i); }));
(我使用的脚本基于http://cgit.drupalcode.org/d3/tree/libraries/d3.linegraph/linegraph.js)
我的问题是:数据数组是动态的,我事先不知道其中的内容.有时,2005年的y值将为null:
var data =
[[{x:2005, y:100}, {x:2007, y:96.5}, {x:2009, y:100.3}, {x:2011, y:102.3}],
[{x:2005, y:null}, {x:2007, y:105}, {x:2009, y:102}, {x:2011, y:104}]]
如何使第二行忽略第一个对象,并从2007年开始?
根据答案1,这就是我现在所拥有的,仍然显示了整行:
data =
[[{x:2005, y:100}, {x:2007, y:96.5}, {x:2009, y:100.3}, {x:2011, y:102.3}],
[{x:2005, y:null}, {x:2007, y:105}, {x:2009, y:102}, {x:2011, y:104}]];
var validatedInput = function(inptArray) {
return inptArray.filter(function(obj) {
return obj.y != null;
});
};
graph.selectAll("path.line")
.data(data, validatedInput)
.enter().append("path")
.attr("class", "line")
.style("stroke", function(d, i) { return d3.rgb(z(i)); })
.style("stroke-width", 2)
.attr("d", d3.svg.line()
.y(function(d) { return y(d.y); })
.x(function(d,i) { return x(i); }));
解决方法:
应该这样做:
.data(data, function(inptArray) {
return inptArray.filter(function(obj) {
return obj.y != null;
})
});
最好这样写:
var validatedInput = function(inptArray) {
return inptArray.filter(function(obj) {
return obj.y != null;
});
.data(data, validatedInput);
或者,您可以在将数据对象提供给D3之前对其进行格式化:
var data = data.map(function(obj){
return obj.filter(function(obj) {
return obj.y != null;
})
})
内容总结
以上是互联网集市为您收集整理的javascript-D3:在多行折线图中跳过空值全部内容,希望文章能够帮你解决javascript-D3:在多行折线图中跳过空值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。