D3.js学习笔记(1)

1.filter

selection.filter(function(d,i){
if(d>20)return true;
else return false;
})
这个函数返回结果是过滤掉绑定数据中所有小于20的数

2.each()

selection.each(function(d,i){
d:每一个标签
i:对应的下角标
})

3.call()

function myfun(selection){
selection.attr(“name”,”value”);
}
selection.call(myfun);
等价于
myfun(selection);
这里区别得于原生js得点在于,这里selection调用函数 既是函数this也可以当参数。

4.sort()

arr.sort(d3.ascending);
[1,2,3]
升序排列
arr.sort(d3.descending);
[3,2,1]
降序排列
sort()默认是d3.ascending

5.求值

d3.min(arr);输出最大值
d3.max(arr);输出最小值
d3.extend(arr);输出一个数组 里面有最大和最小值
d3.sum(arr);求和如果为空则返回0;
d3.mean(arr);求平均值如果数组为空则返回undefined
d3.median(arr);求数组的中间值,如果数组为空,则返回undefined

6.操作数组

d3.shuffle(arr);重新排列数组
d3.merge(arr,arr);合并数组

7.Map

d3.map([object],[key]);构建映射
map.has(key);如果指定的key存在返回true,反之则返回false;
map.get(key);如果指定的key存在,则返回value,否则返回undefined
map.set(key,value)设置指定的key ,存在则覆盖没有则添加
map.remove(key)
map.size()
map.empty()不是空的返回false

8.Set

d3.set(arr);使用数组构建集合,如果数组里有重复的元素,则只添加其中一项
set.has(value);
set.add(value);没有才添加
set.remove(value);
set.values();以数组形式返回集合中所有的元素
set.forEach(funtion(d){d:set中每一个元素});
set.empty();
set.size();

猜你喜欢

转载自blog.csdn.net/qq_26383604/article/details/72641789