D3基本操作
< script src= "https://cdnjs.cloudflare.com/ajax/libs/d3/7.9.0/d3.min.js"
integrity= "sha512-vc58qvvBdrDR4etbxMdlTt4GBQk1qjvyORR2nrsPsFPyrs+/u5c3+1Ct6upOgdZoIl7eq6k3a1UPDSNAQi/32A=="
crossorigin= "anonymous" referrerpolicy= "no-referrer" > < / script>
< script>
var p = d3. select ( "body" )
. selectAll ( "p" )
. text ( "hello D3" )
p. style ( "color" , "red" )
< / script>
var svg = d3. select ( "body" )
. append ( "svg" )
. attr ( "width" , 500 )
. attr ( "height" , 500 )
svg. append ( "circle" )
. attr ( "cx" , 50 )
. attr ( "cy" , 50 )
. attr ( "r" , 50 )
. style ( "fill" , "red" )
选择集与方法
d3. select ( "body" )
d3. select ( "#main" )
d3. select ( ".content" )
d3. selectAll ( "p" )
d3. selectAll ( ".content" )
d3. selectAll ( "ul li" )
d3. select ( document. getElementById ( "important" ) )
d3. select ( "body" ) . selectAll ( "p" )
empty ( ) : 选择集为空返回false
node ( ) : 选择集为空, 返回null
size ( ) : 返回选择集个数
d3. select ( "p" ) . attr ( "id" , "para" )
d3. select ( "p" ) . attr ( "id" )
d3. select ( "p" ) . attr ( "class" , "red bigsize" )
d3. select ( "p" ) . classed ( "red" , true ) . classed ( "bigsize" , false )
d3. select ( "p" ) . classed ( { "red" : true , "bigsize" : false } )
d3. select ( "p" ) . classed ( { "red bigsize" : true )
d3. select ( "p" ) . style ( "color" , "red" ) . style ( "font-size" , "30px" )
d3. select ( "p" ) . style ( { "color" : "red" , "font-size" : 30px} )
d3. select ( "#fname" ) . property ( "value" )
d3. select ( "#fname" ) . property ( "value" , "lisi" )
d3. select ( "#fname" ) . text ( )
d3. select ( "#fname" ) . html ( )
append ( ) : 添加, 末尾插入
insert ( ) : 指定元素前插入
remove ( ) : 删除选择集中的元素
datum ( [ value] ) : 选择集上都绑定一个相同的数据value
d3. select ( "body" ) . selectAll ( "p" ) . datum ( "name" ) . text ( "1" )
d3. select ( "body" ) . selectAll ( "p" ) . datum ( "name" ) . append ( "span" ) . text ( "1" )
data ( values, key) : 选择集中每个元素分别绑定数组values的每一项
d3. select ( "body" ) . selectAll ( "p" ) . data ( [ 10 , 20 , 30 ] )
data ( ) 是按索引号顺序绑定的, 也可以不按顺序, 需要用到第二个参数
var p = d3. select ( "body" ) . selectAll ( "p" )
dataset = [
{ id : 1 , name : "张三" } ,
{ id : 2 , name : "李四" } ,
{ id : 6 , name : "王五" } ,
{ id : 4 , name : "赵六" }
]
p. data ( dataset) . text ( function ( d ) { return d. id + ":" + d. name } )
< p> 1 : 张三< / p>
< p> 2 : 李四< / p>
< p> 3 : 王五< / p>
< p> 4 : 赵六< / p>
var dataset = [ 3 , 6 , 9 ]
var p = d3. select ( "body" ) . selectAll ( "p" )
var update = p. data ( dataset)
var enter = update. emter ( )
update. text ( )
enter. append ( "p" ) . text ( function ( d ) { return d} )
filter ( ) : 根据条件获取选择集的一部分子集
sort ( ) : 排列
each ( ) : 遍历
call ( ) :
sort ( d3. ascending) : 默认参数, 递增
sort ( d3. descending) : 递减
var numbers = [ 10 , 20 , 30 ]
d3. min ( numbers)
d3. max ( numbers)
d3. extent ( numbers)
d3. sum ( numbers)
d3. mean ( numbers)
d3. median ( numbers)
d3. quantile ( numbers, p)
d3. variance ( ) : 方差
d3. deviation ( ) : 求标准差
d3. bisectLeft ( ) : 某数组项左边的位置
d3. bisect ( ) : 某数组项右边的位置
d3. bisectRight ( ) : 和bisect ( ) 一样
d3. shuffle ( ) : 随机排列
d3. merge ( ) : 合并
d3. pairs ( ) : 返回邻接的数组对
d3. range ( start, stop, step) : 返回等差数列
d3. permute ( ) : 根据指定的索引进行排列
d3. zip ( ) : 多个数组来制作数组的数组
d3. transpose ( ) : 求转置矩阵
d3. map ( ) : 构造映射
map. has ( key) : key存在返回true
map. get ( key) : key存在, 返回对应的value
map. set ( key, val) : 修改
map. remove ( key) : 移除
map. keys ( ) : 返回所有dekey
map. values ( ) : 返回所有的value
map. entries ( ) : 返回所有的key和value
map. forEach ( )
map. empty ( ) 映射为空, 返回true
map. size ( ) 映射的大小
d3. set ( ) : 构建集合, 不能有重复
set. has ( ) : 是否包含指定集合
set. add ( ) : 如果集合中没有指定元素, 则添加
set. remove ( ) : 有该元素, 删除并返回true
set. values ( ) : 以数组形式返回
set. forEach ( )
set. empty ( ) : 集合为空则返回true
set. size ( ) 返回集合大小
d3. nest ( )
nest. key ( ) 指定嵌套结构的键
nest. entries ( ) 将数组用于构造嵌套结构
nest. sortKeys ( )
nest. sortValues ( )
nest. rollup ( )
nest. map ( )