标签:概率 变量 随机 uml 先来 常用 函数 ctr 次方
1 var numList = [22,44,33,11,66];
2 numList.sort(d3.ascending); //注意 : 此处的sort()方法使JavaScript的数组方法对象(Array)的方法,不是D3的selection.sort()方法。
3 console.log(numList) //[11,22,33,44,66]
d3.descending(a,b); //递减函数。如果a大于b,则返回-1;如果a小于b,返回1;如果a等于b,返回0;再举个例子 :
1 var numList = [22,44,33,11,66];
2 numList.sort(d3.descending); //注意 : 此处的sort()方法使JavaScript的数组方法对象(Array)的方法,不是D3的selection.sort()方法。
3 console.log(numList) //[66,44,33,22,11]
1 //定义数组
2 var numbers = [30,20,50,10,80,60]
3
4 //求最小值和最大值
5 var min = d3.min(numbers)
6 var max = d3.max(numbers)
7 var extent = d3.extent(numbers)
8
9 //输出结果
10 console.log(min) //10
11 console.log(max) //80
12 console.log(extent) //[10,80]
13
14 //使用accessor,在求值之前先出来了数据
15 var minAcc = d3.min(numbers,function(d){return d*3})
16 var maxAcc = d3.max(numbers,function(d){return d-1})
17 var extentAcc = d3.extent(numbers,function(d){return d%7})
18
19 //输出结果
20 console.log(minAcc) //30
21 console.log(maxAcc) //79
22 console.log(extentAcc) //[1,6]
1 //数组定义
2 var numbers = [30,20,undefined,50,10,80,60,NaN]
3
4 //求总和与平均值
5 var sum = d3.sum(numbers,function(d){return d-10})
6 var mean = d3.mean(numbers)
7
8 //输出结果
9 console.log(sum) //190
10 console.log(mean) //41.666666666666664
1 //数组定义
2 var numbers = [2,8,15]
3 numbers.sort(d3.ascending);
4 console.log(d3.quantile(numbers,0)) //返回2
5 console.log(d3.quantile(numbers,0.25)) //返回5
6 console.log(d3.quantile(numbers,0.5)) //返回8
7 console.log(d3.quantile(numbers,0.75)) //返回11.5
8 console.log(d3.quantile(numbers,0.9)) //返回13.599999999999998
9 console.log(d3.quantile(numbers,1.0)) //返回15
1 //定义数组
2 var numbers1 = [3,5,9,undefined,11,NaN];
3 console.log(d3.median(numbers1)) //返回7
4 var numbers2 =[3,5,9,undefined,11,NaN,17];
5 console.log(d3.median(numbers2)) //返回9
1 //定义数组
2 var numbers1 = [1,9,7,9,5,8,9,10]
3 console.log(d3.mean(numbers1)) //返回平均值7.25
4 console.log(d3.variance(numbers1)) //返回方差值 约8.79
5 console.log(d3.deviation(numbers1)) //返回标准差值 约2.96
6
7 var numbers2 = [7,8,6,7,7,8,8,7];
8 console.log(d3.mean(numbers2)) //返回平均值7.25
9 console.log(d3.variance(numbers2)) //返回方差值 约0.5
10 console.log(d3.deviation(numbers2)) //返回标准差值 约0.71
1 //定义数组
2 var dataList = ["China","America","Japan","France"]
3 //在数组索引为1的位置处,删除0个项后,插入字符串Germany
4 dataList.splice(1,0,"Germany");
5 //再来打印一下新数组
6 console.log(dataList) //输出 ["China", "Germany", "America", "Japan", "France"]
7 //在数组索引为3的位置处,删除一个项后,插入两个字符串Britain和Russis
8 dataList.splice(3,1,"Britain","Russis")
9 //再来打一下新数组
10 console.log(dataList) //输出["China", "Germany", "America", "Britain", "Russis", "France"]
1 //定义数组
2 var numbers = [10,13,16,19,22,25]
3
4 //获取16左边在数组中的位置
5 var iLeft = d3.bisectLeft(numbers.sort(d3.ascending),16);
6 console.log(iLeft) //2
7
8 // 在iLeft位置处,删除0项,出入66
9 numbers.splice(iLeft,0,66)
10 //打印新数组
11 console.log(numbers) [10, 13, 66, 16, 19, 22, 25]
1 //定义数组
2 var numbers = [10,13,16,19,22,25]
3 //18在数组中不存在,返回介于16和19之间的位置
4 var iLeft = d3.bisectLeft(numbers.sort(d3.ascending),18)
5 console.log(iLeft) //返回值为3
6 numbers.splice(iLeft,0,77);
7
8 //打印新数组
9 console.log(numbers) //输出[10, 13, 16, 77, 19, 22, 25]
22:20:16
D3.js的基础部分之数组的处理 数组的排序和求值(v3版本)
标签:概率 变量 随机 uml 先来 常用 函数 ctr 次方
原文地址:https://www.cnblogs.com/littleSpill/p/10793275.html