[D3] Animate Chart Axis Transitions in D3 v4
When the data being rendered by a chart changes, sometimes it necessitates a change to the scales and axes of the chart as well. This lesson demonstrates how to animate and synchronize axis transitions on a column chart.
var svg = d3.select('.chart')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.call(responsivefy)
.append('g')
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');
/**
* Y axis
*/
const yScale = d3.scaleLinear()
.domain([
,
])
.range([height, ]);
const yAxis = svg
.append('g')
.call(d3.axisLeft(yScale));
/**
* x axis
*/
const xScale = d3.scaleBand()
.domain(data.map(d => d.name))
.range([, width])
.padding(0.2);
const xAxis = d3.axisBottom(xScale).tickSize().tickPadding();
svg.append('g')
.attr('transform', `translate(, ${height})`)
.call(xAxis)
.selectAll('text')
.attr('text-anchor', 'end')
.attr('transform', 'rotate(-45)');
// enter: which in the data, but not yet on the page
// upate: which in the data, and also in the page
// exit: which not in the data, but exist on the page
// end
function render(subject = 'math') {
// Define a resuable transation variable
var t = d3.transition().duration();
var update = svg.selectAll('rect')
.data(data.filter(d => d[subject]), d => d.name); //d => d.name is a uniq idientifier
// First: we want to remove the existing object which doesn't have any value
// Get rid of null value object
// Also animation y and height attr to produce
// fade out effect
update
.exit()
.transition(t)
.attr('y', height)
.attr('height', )
.remove();
// Update the y axis with animation
yScale.domain(
[, d3.max(data, d => d[subject])]
);
yAxis
.transition(t)
.delay()
.call(d3.axisLeft(yScale));
// Second, we want to animate the existing elements height
update
.transition(t)
.delay()
.attr('y', d => yScale(d[subject]))
.attr('height', d => height - yScale(d[subject]));
// Last, for the new data which is not in the page before
// We want to animate
update
.enter()
.append('rect')
.attr('y', height)
.attr('height', )
.attr('x', d => xScale(d.name))
.attr('width', d => xScale.bandwidth())
.transition(t)
.delay(update.exit().size() ? : ) // If page refresh, we don't want to wait 2000ms
.attr('y', d => yScale(d[subject]))
.attr('height', d => height - yScale(d[subject]));
}
render();
[D3] Animate Chart Axis Transitions in D3 v4的更多相关文章
- [D3] Animate Transitions in D3 v4
D3 makes it easy to add meaningful animations to your data visualizations. Whether it’s fading in ne ...
- [D3] Create Chart Axes with D3 v4
Most charts aren’t complete without axes to provide context and labeling for the graphical elements ...
- [D3] Reuse Transitions in D3 v4
D3 transitions start executing as soon as they’re created, and they’re destroyed once they end. This ...
- [D3] Build a Scatter Plot with D3 v4
Scatter plots, sometimes also known as bubble charts, are another common type of visualization. They ...
- d3 - bar chart
用 D3.js 做一个简单的柱形图. 做柱形图有很多种方法,比如用 HTML 的 div 标签,或用 svg . 推荐用 SVG 来做各种图形.SVG 意为可缩放矢量图形(Scalable Vecto ...
- [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] 12. Basic Transitions with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [D3] Load and Inspect Data with D3 v4
You probably use a framework or standalone library to load data into your apps, but what if that’s o ...
- d3可视化实战02:理解d3数据驱动的真正含义
前文中已经提到,SVG从诞生之初起就可以非常方便地使用javascript脚本语言来进行其DOM对象的控制.当然,控制的方法有很多,有直接控制SVG对象的方法,例如使用原生js:有帮你封装一下图形接口 ...
随机推荐
- CSS3新的UI方案
文本新增样式 一.opacity:指定了一个元素的透明度 0~1 二.新增颜色模式rgba:很好的解决了背景透明,字体颜色不透明的需求. 三.文字阴影:text-shadow用来为文字添加阴影,而且可 ...
- mysql源码安装(包括5.5和5.7)
1.mysql5.5源码安装 yum install -y cmake ncurses-devel ncurses cd /usr/src wget -c https://cdn.mysql.com/ ...
- 通用查询实现方案(可用于DDD)[附源码] -- 简介
原文:通用查询实现方案(可用于DDD)[附源码] -- 简介 [声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3985353.html). [ ...
- 实际运用中DataSet、DataTable、DataRow点滴
DataSet.DataTable都自带有序列化标记,但是DataRow没有, 所以如果是在CS程序中,Release版本程序DataTable才是最小的数据传输单元,如果使用DataRow则会报[未 ...
- 多路I/O转接之select模型
I/O复用使得程序可以同一时候监听多个文件描写叙述符.这对提高程序的性能至关重要.通常,网络程序同一时候处理或者监听多个socket文件描写叙述符的时候可以考虑使用I/O复用模型. 值得强调的是.I/ ...
- 手动脱FSG壳实战
作者:Fly2015 对于FSG壳.之前没有接触过是第一次接触.这次拿来脱壳的程序仍然是吾爱破解论坛破解培训的作业3的程序.对于这个壳折腾了一会儿,后来还是被搞定了. 1.查壳 首先对该程序(吾爱破解 ...
- Viewpager切换时pager页面的生命周期变化
总结1: 当我们把ViewPager和Fragment合用的时候,切换页面时生命周期会发生对应的变化.变化规律:载入当前页面.前一个页面和后一个页面.我们来看一个实际測试效果图 打开应用会载入第一个页 ...
- [转]Massive Model Rendering Techniques
Massive Model Rendering Techniques Andreas Dietrich Enrico Gobbetti Sung-Eui Yoon Abstract We presen ...
- 实现外网訪问局域网内的SVN——花生壳+visiualSVN实现外网訪问局域网内的SVN(三)
经过前两篇文章.到眼下为止,我们已经获取了外网域名而且搭建好了SVN server.接下来,我们就总结一下怎样实践实现一下訪问局域网. 1.安装VisiualSVN Server(可见:http:// ...
- Java程序猿的JavaScript学习笔记(6——面向对象模拟)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...