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的更多相关文章

  1. jQuery学习--Code Organization Concepts

    jQuery官方文档:  http://learn.jquery.com/code-organization/concepts/ Code Organization Concepts(代码组织概念) ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

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

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

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

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

  7. D3.js data() 方法详解

    Binding data(数据绑定) D3各种图表的作用体现在将数据(Data)转换成可视化的过程. 比如将一个月的气温数据,通过树形图来展现,能够直观的看到气温走势,下个月还需不需要穿秋裤 :) 我 ...

  8. D3——基本知识点

    选择器: d3.select - 从当前文档中选择一个元素 d3.selectAll - 从当前文档中选择多个元素 selection.append - 创建并追加一个新元素 selection.at ...

  9. D3.js学习(一)

    从今天开始我将和大家一起学习D3.js(Data-Driven Documents),由于国内关于D3的学习资料少之又少,所以我觉得很有必要把自己学习过程记录下来,供同学们参考,如果文章有有哪些表达有 ...

随机推荐

  1. UVa 10101 - Bangla Numbers

    题目:将数字数转化成数字加单词的表示形式输出. 分析:数论.简单题.直接分成两部分除10000000的商和余数,分别输出就可以. 说明:注意输入为数字0的情况,还有long long类型防止溢出. # ...

  2. Android控件ToggleButton的使用方法

    ToggleButton(开关button)是Android系统中比較简单的一个组件,是一个具有选中和未选择状态双状态的button.而且须要为不同的状态设置不同的显示文本. ToggleButton ...

  3. 基于TI Davinci架构的多核/双核开发高速扫盲(以OMAP L138为例),dm8168多核开发參考以及达芬奇系列资料user guide整理

    基于TI Davinci架构的双核嵌入式应用处理器OMAPL138开发入门 原文转自http://blog.csdn.net/wangpengqi/article/details/8115614 感谢 ...

  4. 1.实用:Google Chrome 键盘快捷键大全

    转自:https://www.cnbeta.com/articles/soft/64070.htm 窗口和标签页快捷方式 Ctrl+N 打开新窗口 按住 Ctrl‎ 键,然后点击链接 在新标签页中打开 ...

  5. Spark scheduler

    触发Spark scheduler的入口是调用者代码中的action操作,如groupByKey,first,take,foreach等操作.这些action操作最终会调用SparkContext.r ...

  6. Laravel输出sql语句

    $queries = DB::getQueryLog();

  7. flask使用第三方云通讯平台时,出现{'172001':'网络错误'}解决方法

    问题描述:flask使用第三方云通讯平台时,出现{'172001':'网络错误'} 解决方法: 在sms.py文件中添加如下代码 import ssl # 取消证书验证ssl._create_defa ...

  8. 关于client浏览器界面文字内容溢出用省略号表示方法

    在实际的项目中,因为client浏览器文字内容的长度不确定性和页面布局的固定性,难免会出现文字内容超过div(或其它标签,下同)区域的情况.此时比較好的做法就是当文字超过限定的div宽度后自己主动以省 ...

  9. 将NSTimer加入至RunLoop中的两种方法差别

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  10. Oracle APEX 4.2公布RESTful Webservice

    Purpose This tutorial covers creating a RESTful Web Service and accessing the Web Service through an ...