selector: tag
, .class
, #id
var
vs let
Main difference is scoping rules. Variables declared by var
keyword are scoped to the immediate function body (hence the function scope) while let
variables are scoped to the immediate enclosing block denoted by { }
(hence the block scope).
//选中HTML元素
selection = d3.select(selector)//只选第一个
selection = d3.selectAll(selector)
//改变选中的元素
selection.text("new string")
selection.html("<b>new</b> string")
selection.attr(attr,value)
selection.style(attr,value)
//添加删除元素
selection.append(tag) .text(‘haha‘)
d3.csv(dataURL)
.then(callback)
.catch(errorHandler)
//filtering and sorting
array.filter(func) //return a new list
array.sort(comparator)//in-place
//comparator example:
function (a,b){if a<b return -1 / 1 /0}
array.map(func)
array.reduce(reducer, initialValue)
d3.max(data[,accessor])//min sum mean
let sel = d3.selectAll("li").data(data)
sel //update
sel.enter()
sel.exit() .remove()
//numeric scale
d3.scaleLinear().domain(domainExtent).range(rangeExtent)
d3.scaleSqrt().domain(domainExtent).range(rangeExtent)
d3.scaleOrdinal().domain(domain).range(range)
svg 有个path比较强大,有专门语法。d3可以帮我们转换成path的语法
d3.line().x(function).y(function)
//pie/donut chart
let arcGenerator = d3.arc()
let pieGen = d3.pie().value( d=> d.price)
let arcData = pieGen(data).attr("d", arcGenerator )
slection.on(event, handlerFunction)
//arrow funtion 不能用this
() => {}
//function function可以用this
function () {}
//选择的例子
body.selectAll("circle")
.on("click", function(d){
if(d3.event.shiftKey){
this.style.fill = "blue"
}
})
//鼠标晃过变色嗯例子
join.enter()
.append(‘rect‘)
.style(‘fill‘, ‘blue‘)
.style(‘stroke‘, ‘white‘)
.attr(width, height, ...)
.attr(‘transform‘, d => `translate ${scalePosition(d.height)}`)
.on(‘click‘, d => {
d3.select(‘#details‘).text(d.name)
})
.on(‘mouseover‘, function() => {
this.style.fill = ‘red‘
})
.on(‘mouseout‘, function() => {
this.style.fill = ‘blue‘
})
//mousemove rule barchart
用于选择。比如鼠标拖方块选择
https://www.d3-graph-gallery.com/graph/interactivity_brush.html
浮动的小块显示信息
https://www.d3-graph-gallery.com/graph/interactivity_tooltip.html
同一份数据,不同的看法
原文:https://www.cnblogs.com/like1tree/p/14742787.html