[D3] Better Code Organization with selection.call() with D3 v4
code with call v4 D3 Better Selection organization
2023-09-14 09:00:51 时间
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; });
相关文章
- 微信绑定用户服务端代码-根据code获取openId然后绑定用户
- Entity Framework Code First属性映射约定
- code.google.com/p/log4go 下载失败
- 不要让“Clean Code”更难维护,请使用“Rule of Three”
- Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
- 在Code::Blocks试用与安装vim插件
- Visual Studio code快捷键
- [VSCode] Visually Organize Applications in VS Code with Window Color-Coding
- [Python] Optimizing code with numpy intersect1d, and Set
- [D3] Better Code Organization with selection.call() with D3 v4
- [D3] 13. Cleaner D3 code with selection.call()
- Mysql:Error Code 1235,This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
- svn Error: post-commit hook failed (exit code 127) with output
- [Tools] Scroll, Zoom, and Highlight code in a mdx-deck slide presentation with Code Surfer <🏄/>
- [D3] 13. Cleaner D3 code with selection.call()
- SAP Table function 执行报错 code CX_SQL_EXCEPTION feature not supported 该如何分析
- There is no source code available for the current location
- 成功解决“assert.h”: No such file or directory error: command failed with exit code 2
- No matching code signing identity found
- 问题解决:Job for docker.service failed because the control process exited with error code
- Visual Studio Code C++扩展更新:clang-tidy
- VS code创建虚拟环境并安装Python包
- 1.2、基于增量式生成遮挡与对抗抑制的行人再识别(Code Analysis)
- 【python问题解决】---- SyntaxError: Non-UTF-8 code starting with ‘xe4‘ in file