[D3] Better Code Organization with selection.call() with D3 v4
Most of D3’s native selection APIs also return the selection (or a new selection), to enable multiple method calls to be chained together. Since the code you write isn’t on the selection prototype, chaining your methods would take some extra work. selection.call() will call any function reference you give it, providing the selection as the first parameter, and then it returns the selection for you to ensure chaining is supported.
var scores = [
{ name: 'Alice', score: 96 },
{ name: 'Billy', score: 83 },
{ name: 'Cindy', score: 91 },
{ name: 'David', score: 96 },
{ name: 'Emily', score: 88 }
]; var bar = d3.select('.chart')
.append('svg')
.attr('width', 225)
.attr('height', 300)
.selectAll('g')
.data(scores)
.enter()
.append('g')
.attr('transform', (d, i) => 'translate(0, ' + i * 33 + ')'); function scaleBar (selection, scale) {
selection.style('transform', 'scaleX(' + scale + ')');
} function setFill (selection, color) {
selection.style('fill', color);
} function fade (selection, opacity) {
selection.style('fill-opacity', opacity);
} bar.append('rect')
.style('width', d => d.score)
.attr('class', 'bar')
.on('mouseover', function (d, i, elements) {
d3.select(this)
.call(scaleBar, 2)
.call(setFill, 'orange'); d3.selectAll(elements)
.filter(':not(:hover)')
.call(fade, 0.5);
})
.on('mouseout', function (d, i, elements) {
d3.select(this)
.call(scaleBar, 1)
.call(setFill, 'lightgreen'); d3.selectAll(elements)
.call(fade, 1);
}); bar.append('text')
.attr('y', 20)
.text(function (d) {
return d.name;
});
[D3] Better Code Organization with selection.call() with D3 v4的更多相关文章
- jQuery学习--Code Organization Concepts
jQuery官方文档: http://learn.jquery.com/code-organization/concepts/ Code Organization Concepts(代码组织概念) ...
- [D3] Animate with the General Update Pattern in D3 v4
In D3, the General Update Pattern is the name given to what happens when a data join is followed by ...
- [D3] SVG Graphics Containers and Text Elements in D3 v4
SVG is a great output format for data visualizations because of its scalability, but it comes with s ...
- [D3] 13. Cleaner D3 code with selection.call()
selection.call() method in D3 can aid in code organization and flexibility by eliminating the need t ...
- d3.js:数据可视化利器之 selection:选择集
选择集/selection 选择集/selection是d3中的核心对象,用来封装一组从当前HTML文档中选中的元素: d3提供了两个方法用来创建selection对象: select(selecto ...
- D3中动画(transition函数)的使用
关于transition的几个基本点: 1. transition()是针对与每个DOM element的,每个DOM element的transition并不会影响其他DOM element的tra ...
- D3.js data() 方法详解
Binding data(数据绑定) D3各种图表的作用体现在将数据(Data)转换成可视化的过程. 比如将一个月的气温数据,通过树形图来展现,能够直观的看到气温走势,下个月还需不需要穿秋裤 :) 我 ...
- D3——基本知识点
选择器: d3.select - 从当前文档中选择一个元素 d3.selectAll - 从当前文档中选择多个元素 selection.append - 创建并追加一个新元素 selection.at ...
- D3.js学习(一)
从今天开始我将和大家一起学习D3.js(Data-Driven Documents),由于国内关于D3的学习资料少之又少,所以我觉得很有必要把自己学习过程记录下来,供同学们参考,如果文章有有哪些表达有 ...
随机推荐
- [Python] for.. not in.. Remove Deduplication
Write a function, remove_duplicates that takes a list as its argument and returns a new list contain ...
- linux杂谈(十八):DNSserver的配置(一)
1.DNSserver简单介绍 域名系统(英文:Domain Name System,縮寫:DNS)是因特网的一项服务. 它作为将域名和IP地址相互映射的一个分布式数据库,可以使人更方便的訪问互联网. ...
- Spark 性能相关參数配置具体解释-任务调度篇
作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 随着Spark的逐渐成熟完好, ...
- awk依照多个分隔符进行切割
我们知道awk能够进行类似于cut之类的操作.如一个文件data例如以下 zhc-123|zhang hongchangfirst-99|zhang hongchang-100|zhang 假设我们 ...
- Objective-C_类的扩展
一Category的定义和使用 二Extension的定义和使用 三Protocol的定义和使用 delegate的使用 一.Category的定义和使用 Category,分类或类目.主要作用是为没 ...
- ElasticSearch 在Hadoop生态圈的位置
它的位置非常清晰,直接贴图. 更详细点,见
- javafx drag
public class EffectTest extends Application { @Override public void start(Stage stage) { stage.setTi ...
- 使用Java开发微信公众平台(四)——消息的接收与响应
上一篇文章(http://www.jerehedu.com/fenxiang/171807_for_detail.htm )中,我们学习了使用Java语言开发微信公众平台的第一部分——环境搭建与开发接 ...
- 深入了解"网上邻居"原理
说到“网上邻居”,相信很多人都很熟悉.但是说起“网上邻居”的工作机制,可能大家就不太清楚了. 要说“网上邻居”的工作机制,不妨联系一下生活中的例子:比如我(A),要拜访一个远方的朋友(B),我要去他的 ...
- bootstrap之Orientation
Orientation 调整屏幕方向的操作. package io.appium.android.bootstrap.handler; import android.os.RemoteExceptio ...