[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:有帮你封装一下图形接口 ...
随机推荐
- Scrapy框架之日志等级
一.日志等级 CRITICAL:严重错误 ERROR:一般错误 WARNING:警告 INFO: 一般信息 DEBUG:调试信息 [注意:默认的日志等级是DEBUG] 二.日志等级设置 修改setti ...
- JAVA工程命名规范
Java推荐的包声明命名约定是反向域名. 例如 - com.abysm.myproject
- ArcGIS Engine能够做什么?
转自原文ArcGIS Engine能够做什么? ArcGIS Engine是一组跨平台的嵌入式ArcObjects,它是ArcGIS软件产品的底层组件,用来构建定制的GIS和桌面制图应用程序,或是向原 ...
- [Python] Manage Dependencies with Python Virtual Environments
Virtual Environments ensure that dependencies from one Python application don’t overwrite the depend ...
- poj2486--Apple Tree(树状dp)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7789 Accepted: 2606 Descri ...
- K短路 spfa + A*
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> ...
- BootStrap_Table 学习
https://blog.csdn.net/heting90/article/details/52248729 $("#realTime_Table").bootstrapTabl ...
- HDU——T 3746 Cyclic Nacklace
http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- 洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving
https://www.luogu.org/problem/show?pid=2984 题目描述 Farmer John is distributing chocolates at the barn ...
- 洛谷 P1881 绳子对折
P1881 绳子对折 题目描述 FJ 有一个长度为L(1<= L <= 10,000)的绳子. 这个绳子上有N(1 <= N <= 100)个结,包括两个端点. FJ想将绳子对 ...