之前说慢慢写有关D3的笔记,结果做完那个拓扑图就没写了,今天发现关于d3的用法有点遗忘。感觉有回顾一遍的必要。

之前的序对D3有一个简单的介绍,下面就做一些细节的东西。主要是贴代码,顺带注释和效果图。

<html>
<head>
<meta charset="utf-8">
<title>d3研究室</title>
<style>
.h-bar{
width:21px;
background-color: chartreuse;
text-align: start;
border:solid 1px black;
display: inline-block;
}
</style>
</head>
<body>
<script src="./d3.v3.min.js" charset="utf-8"></script>
<script> var data=[];
for(var i=0;i<10;i++){
data.push(Math.ceil(Math.random()*100));
} var render=function(){
//enter 计算数据与显示元素的差集,补充不足
d3.select("body").selectAll("div.h-bar")
.data(data)
.enter()
.append("div")
.attr("class",'h-bar')
.append("span"); //update 更新内容
d3.select("body").selectAll("div.h-bar")
.data(data)
.style("height",function(d,i){
console.log('item'+i+":"+d);
return (d*3)+"px";
})
.select("span")
.text(function(d){
return d;
});
//exit 删掉多于的元素
d3.select("body").selectAll("div.h-bar")
.data(data)
.exit()
.remove();
}; setInterval(function(){
data.shift();
data.push(Math.ceil(Math.random()*100))
render();
},1500);
</script>
</body>
</html>

将函数作为数据

上面那个示例中绑定的数据是一个纯数字,其实绑定什么类型的数据并没有严格的限定,完全取决于应用场景。甚至可以将函数作为数据绑定到视图上。


<html>
<head>
<meta charset="utf-8">
<title>d3研究室</title>
<style>
.h-bar{
width:15px;
background-color: chartreuse;
text-align: right;
border:solid 1px black;
display: inline-block;
margin-left:3px;
}
</style>
</head>
<body>
<script src="./d3.v3.min.js" charset="utf-8"></script>
<script> var data=[]; //生成将要绑定到视图的函数
var d_func=function(u){
return function(x){
return u+x*x;
}
}; //添加新的数据
var newData=function(){
data.push(d_func(Math.floor(Math.random()*20)));
return data;
}; //重新渲染视图
var render=function(){ var selection=d3.select("body").selectAll("div.h-bar")
.data(newData());//这里也可以直接传newData这个函数,d3自己会去执行 selection.enter()
.append("div")
.attr("class",'h-bar')
.append("span"); selection.exit()
.remove(); selection.attr("class","h-bar")
.style("height",function(d,i){
return d(i)+"px";//注意这里的d是一个函数
})
.select("span").text(function(d,i){
return d(i);
});
}; setInterval(function(){
render();
},1500);
</script>
</body>
</html>

一些数据处理工具

  • filter 过滤数据,在数据反映到视图前调用,.filter(function(){return true/false;})
  • sort 对数据进行排序,.sort(comparator);
  • nest 多级聚类
    var records = [
{quantity: 2, total: 190, tip: 100, type: "tab"},
{quantity: 2, total: 190, tip: 100, type: "tab"},
{quantity: 1, total: 300, tip: 200, type: "visa"},
{quantity: 2, total: 90, tip: 0, type: "tab"},
{quantity: 2, total: 90, tip: 0, type: "tab"},
{quantity: 2, total: 90, tip: 0, type: "tab"},
{quantity: 1, total: 100, tip: 0, type: "cash"},
{quantity: 2, total: 90, tip: 0, type: "tab"},
{quantity: 2, total: 90, tip: 0, type: "tab"},
{quantity: 2, total: 90, tip: 0, type: "tab"},
{quantity: 2, total: 200, tip: 0, type: "cash"},
{quantity: 1, total: 200, tip: 100, type: "visa"}
]; var nest = d3.nest()
.key(function (d) { // 第一级按type分类
return d.type;
})
.key(function (d) { // 第二级按tip分类
return d.tip;
})
.entries(records); // 执行分类策略
//分类的结果,即nest的值
{
"key" : "tab",
"values" : [{
"key" : "100",
"values" : [{
"quantity" : 2,
"total" : 190,
"tip" : 100,
"type" : "tab"
}, {
"quantity" : 2,
"total" : 190,
"tip" : 100,
"type" : "tab"
}
]
}, {
"key" : "0",
"values" : [{
"quantity" : 2,
"total" : 90,
"tip" : 0,
"type" : "tab"
}, {
"quantity" : 2,
"total" : 90,
"tip" : 0,
"type" : "tab"
}, {
"quantity" : 2,
"total" : 90,
"tip" : 0,
"type" : "tab"
}, {
"quantity" : 2,
"total" : 90,
"tip" : 0,
"type" : "tab"
}, {
"quantity" : 2,
"total" : 90,
"tip" : 0,
"type" : "tab"
}, {
"quantity" : 2,
"total" : 90,
"tip" : 0,
"type" : "tab"
}
]
}
]
}, {
"key" : "visa",
"values" : [{
"key" : "200",
"values" : [{
"quantity" : 1,
"total" : 300,
"tip" : 200,
"type" : "visa"
}
]
}, {
"key" : "100",
"values" : [{
"quantity" : 1,
"total" : 200,
"tip" : 100,
"type" : "visa"
}
]
}
]
}, {
"key" : "cash",
"values" : [{
"key" : "0",
"values" : [{
"quantity" : 1,
"total" : 100,
"tip" : 0,
"type" : "cash"
}, {
"quantity" : 2,
"total" : 200,
"tip" : 0,
"type" : "cash"
}
]
}
]
}
]

最新文章

  1. Smooth Mouse
  2. poj2389-Bull Math(大整数乘法)
  3. 安装SRILM
  4. ios view的frame和bounds之区别(位置和大小)
  5. openStack云平台虚拟桌面galera mysql 3节点集群实例实战
  6. pushViewController自定义动画
  7. [置顶] Adapter详解
  8. python2中的__init__.py文件的作用
  9. SQL语句检索数据排序及过滤
  10. Django积木块七——视频
  11. 开源项目商业模式分析(2) - 持续维护的重要性 - Selenium和WatiN
  12. docker-compose 案例
  13. Thymeleaf在IDEA中的使用
  14. C++ code:函数指针数组
  15. tab------左右布局
  16. L264 how cats are psychopaths
  17. WebDriver高级应用实例(6)
  18. PHP 协程最简洁的讲解
  19. python数据类型2
  20. 【CentOS 7】nginx配置web服务器

热门文章

  1. CopyU!v2 已经收录到腾讯软件管家!
  2. iOS开发——网络编程Swift篇&amp;(五)同步Post方式
  3. Android横竖屏切换处理
  4. 怎样用通用pe工具箱制作U盘启动盘
  5. css 之position用法详解
  6. sed程序
  7. 包加载失败 未能正确加载包“xxx”...
  8. IJKMediaFramework第三方库的使用
  9. Ubuntu14.04 Kylin下 GO语言环境搭建
  10. 【安卓面试题】在一个Activity启动另一个Activity和在Service中启动一个Activity有什么区别