selection.call() method in D3 can aid in code organization and flexibility by eliminating the need to use chained method calls for every operation.

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="../bower_components/underscore/underscore-min.js"></script>
<script src="../ventor/d3.min.js"></script>
<style type="text/css"> body {
padding-top: 50px;
padding-left: 100px; } #chartArea {
width: 400px;
height: 300px;
background-color: #CCC;
} .bar {
display: inline-block;
width: 20px;
height: 75px; /* Gets overriden by D3-assigned height below */
margin-right: 2px;
/* fill: teal; *//* SVG doesn't have background prop, use fill instead*/
z-index: 99;
} .bubble, .center {
display: inline-block;
fill: purple;
fill-opacity: 0.5;
stroke: black;
stroke-weight: 1px;;
z-index: 15;
} .center {
z-index: 10;
} .active {
fill: magenta;
fill-opacity: 0.5;
stroke-width: 3px;
} .axis path, .axis line {
fill: none;
stroke: #000;
stroke-width: 1px;
shape-rendering: crispEdges;
} </style>
</head>
<body>
<button onclick="update()">Update</button>
<section id="chartArea"></section>
<script> function planTrasition(selection, duration, color){ selection
.transition()
.duration(duration)
.style('fill', color)
.attr('cx', function(each_data, index) {
return xScale(each_data.x);
})
.attr('cy', function(each_data) {
return yScale(each_data.y);
})
.attr('r', function(each_data, i) {
return each_data.r;
});
} function stepTransition(selection) { selection.transition()
.duration(600)
.style('fill', "lightblue")
.attr('cx', function(each_data, index) {
return xScale(each_data.x);
})
.transition()
.duration(600)
.attr('cy', function(each_data) {
return yScale(each_data.y);
})
.transition()
.duration(600)
.attr('r', function(each_data, i) {
return each_data.r;
});
} function newData(d){
d.x = Math.round(Math.random() * 100);
d.y = Math.round(Math.random() * 100);
d.r = Math.round(5 + Math.random() * 10);
} function update(){ //Only the data which x < 50 will get update
svg.selectAll('circle')
.filter(function(d) {
return d.x < 50;
})
.each( newData)
.call(stepTransition); svg.selectAll('circle')
.filter(function(d) {
return d.x >= 50;
})
.each( newData)
.call(planTrasition, 2000, "red");
} var dataset = _.map(_.range(30), function(num) {
return {
x: Math.round(Math.random() * 100),
y: Math.round(Math.random() * 100),
r: Math.round(5 + Math.random() * 10)
};
}), //reandom generate 15 data from 1 to 50
margin = {top: 20, right: 20, bottom: 40, left: 40},
w = 400 - margin.left - margin.right,
h = 300 - margin.top - margin.bottom; var svg = d3.select('#chartArea').append('svg')
.attr('width', w + margin.left + margin.right)
.attr('height', h + margin.top + margin.bottom)
.append('g') //The last step is to add a G element which is a graphics container in SBG.
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')'); //Then offset that graphic element by our left and top margins. var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) {
return d.y; //tell the max function just need to care about y prop
})])
.range([h, 0]); var yAxis = d3.svg.axis()
.scale(yScale)
.orient('left')
.ticks(10)
.innerTickSize(10)
.outerTickSize(10)
.tickPadding(10);
svg.append('g')
.attr('class', 'y axis')
.attr('transform', 'translate(0,0)')
.call(yAxis); var xScale = d3.scale.linear()
.domain([0, 100])
.range([0, w]); var xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom')
.ticks(10)
.innerTickSize(6)
.outerTickSize(12)
.tickPadding(12); svg.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0, ' + h + ')')
.call(xAxis); svg.selectAll('circle')
.data(dataset)
.enter()
.append('circle')// svg doesn't have div, use rect instead
.attr('class', "bubble")
.attr('cx', function(each_data, index) {
return xScale(each_data.x);
})
.attr('cy', function(each_data) {
return yScale(each_data.y);
})
.attr('r', function(each_data, i) {
return each_data.r;
})
.on('mouseover', function() {
d3.select(this).classed('active', true)
})
.on('mouseleave', function() {
d3.select(this).classed('active', false)
})
.on('mousedown', function(d) {
var p_cx = d.x, p_cy = d.y, p_r = d.r;
d3.select(this).transition().duration(500).attr('r', d.r * 1.5);
svg.append('circle')
.attr('class', "center")
.attr('cx', function() {
return xScale(p_cx);
})
.attr('cy', function() {
return yScale(p_cy);
})
.attr('r', function() {
return p_r / 4;
})
.style('fill', 'red');
})
.on('mouseup', function(d) { d3.select(this).transition().duration(250).delay(100).attr('r', d.r)
});
</script>
</body>
</html>

[D3] 13. Cleaner D3 code with selection.call()的更多相关文章

  1. D3笔记01——D3简介与安装

    1 D3简介 发布于2011年,全称Data-Driven Documents,直译为“数据驱动的文档”. 简单概括为一句话:D3是一个Javascript的函数库,是用来做数据可视化的.文档指DOM ...

  2. d3.js:数据可视化利器之 selection:选择集

    选择集/selection 选择集/selection是d3中的核心对象,用来封装一组从当前HTML文档中选中的元素: d3提供了两个方法用来创建selection对象: select(selecto ...

  3. A better way to learn D3 js - iLearning D3.js

    iLearning D3.js Basic is an iPad app to learn and code with D3. In 1.1 version, new tutorial is prov ...

  4. [D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. [D3] Creating a D3 Force Layout in React

    Learn how to leverage d3's layout module to create a Force Layout inside of React. We'll take a look ...

  6. D3中selection之使用

    1. 极为重要的reference: [1] How selections works. http://bost.ocks.org/mike/selection/ [2] Nested selecti ...

  7. D3中动画(transition函数)的使用

    关于transition的几个基本点: 1. transition()是针对与每个DOM element的,每个DOM element的transition并不会影响其他DOM element的tra ...

  8. 精通D3.js学习笔记(1)基础的函数

    买了本吕大师的d3可视化.最近来学习一下,做个笔记.   1.选择元素  select(第一元素) 和selectAll(全部的元素)      类似css的选择器.也可以是dom选中的. var i ...

  9. 使用JavaScript和D3.js实现数据可视化

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由独木桥先生发表于云+社区专栏 介绍 D3.js是一个JavaScript库.它的全称是Data-Driven Documents(数据 ...

随机推荐

  1. 如何使用Github仓库创建网站

    官方文档:https://help.github.com/categories/github-pages-basics/ 1.创建一个仓库 2.额外建立一个gh-pages分支 3.添加CNAME文件 ...

  2. CTSC && APIO 总结

    先说CTSC吧,第一试其实不难但是下意识觉得CTSC不就只能骗分吗,然后就全上暴力了.然而第二题再一次看漏了条件,即答案总和小于等于1e6.第三题现在回想起来要不然就是没有思考,要不然就是没学过数学, ...

  3. Android 给ListView设置Adapter

    Adapter: class MyAdapter extends BaseAdapter { private List<Person> personList; public MyAdapt ...

  4. SpringSecurity简单应用(二)

    这里我首先对我上一篇博文的第三个实例做一下讲解,下面是applicationContext-security.xml内容如下: <?xml version="1.0" enc ...

  5. 子查询解嵌套in改写为exists

    SELECT * FROM (SELECT pubformdat0_.id id332_, pubformdat0_.domain_id domain2_332_, pubformdat0_.proc ...

  6. linux使用crontab -e 遇到No space left on device

    今天用linux的crontab -e编辑定时脚本的时候.遇到No space left on device的错误. 网上找了半天终于知道原因了,昨天晚上我的一个任务因为路径没写对,到时crontab ...

  7. 漫谈CSS的渲染效率

    总结了部分所学.所听.所看.所问的一些CSS写作经验,书写高效的CSS - 漫谈CSS的渲染效率,它们与渲染效率及所占用消耗的资源有一定的关 联.部分为自己理解所写,不排除会有错漏,欢迎提供更好的意见 ...

  8. SQL中and与or优先级比较

    刚刚在项目中遇到这样一个问题,SQL语句如下: select * from LOAN_BACK_LIBRARY where LIBRARY_ID=1 or LIB_ID=1 and STATUS=3 ...

  9. 计数方法,博弈论(扫描线,树形SG):HDU 5299 Circles Game

    There are n circles on a infinitely large table.With every two circle, either one contains another o ...

  10. VS2010+Oracle11+Entity Framework4.1环境搭建及常见问题(转)

    一,开场白: 在微软的实体数据模型中存在四种查询方式:SQL字符串:Linq:Linq to SQL:Linq to Entity(ESQL) 对于Linq SQL目前微软虽然仍在支持,但微软已经声明 ...