D3——Updates, Transitions, and Motion
<script type="text/javascript">
var w = ;
var h = ;
var barPadding = ; var dataset = [ , , , , , , , , , ,, , , , , , , , , ]; //create svg
var svg = d3.select("body").append("svg")
.attr("width", w).attr("height", h); svg.selectAll("rect")
.data(dataset).enter()
.append("rect")
.attr("x",function(d,i){ return i * (w/dataset.length);})
.attr("y", function(d){ return h - (d*);})
.attr("width", function(d,i){return w/dataset.length - barPadding;})
.attr("height", function(d){return d*;})
.attr("fill", function(d){return "rgb(0,0," +(d*)+ ")";}) //set label
svg.selectAll("text")
.data(dataset).enter()
.append("text")
.text(function(d){return d;})
.attr("text-anchor","middle")
.attr("x",function(d,i){
return i*(w/dataset.length)+(w/dataset.length-barPadding)/;})
.attr("y",function(d){return h-(d*)+;})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill","rgb(255,255,255)") </script>
前面的直方图使用的数据都是static datasets, 但现实世界中数据大多都是随时间变化的。有时我们希望图表能反映那些变化。在D3中,通过updates可以更新数据,changes可视化可以通过transitions,motion表现出来
有序的数据(ordinal data: typically categories with some inherent order to them),例如:
- freshman, sophomore, junior, senior
- grade B, grade A, grade AA
- strongly dislike, dislike, neutral, like, strongly like
We don’t have true ordinal data for use with this bar chart. Instead, we just want the bars to be drawn from left to right using the same order in which values occur in our dataset.
对这类数据需要用ordinal scale(序数比例尺);
var
xScale
=
d3
.
scale
.
ordinal
();
线性比例尺的定义域需要设置一个有两个值得数组,分别是最小值和最大值,例如var xScale = d3.scale.linear().domain([0,100]);
而序数比例尺的定义域的设置和线性比例尺不一样,可以指定an array with the category names,例如:.domain(["freshman","sophomore","junior","senior"])
上面直方图中数据设置序数比例尺:
.domain(d3.range(dataset.length)) //等价于
.domain([, , , , , , , , , ,
, , , , , , , , , ]) //d3.range(10)返回一个数组[0,1,2,3,4,5,6,7,8,9]
ordinal scales 使用离散 ranges, meaning输出值是预先定义好的,可以数值化或不数值化;
可以使用range()函数
定义值域, 或者使用更平滑的 rangeBands()函数
, 它利用最小值和最大值,根据domain的length自动分割成均匀的chunks or “bands。 For example:
.rangeBands([, w])
/*
this says “calculate even bands starting at 0 and ending at w, then set this scale’s range to those bands.” In our case, we specified 20 values in the domain, so D3 will calculate: (w - 0) / xScale.domain().length
(600 - 0) / 20
600 / 20
30 */
In the end, each band will be 30
“wide.”
也可以加入第二个参数,以设置每个band之间的间隔; Here, I’ve used 0.2
, meaning that 20 percent of the width of each band will be used for spacing in between bands:
.rangeBands([, w], 0.2)
还可以使用 rangeRoundBands()
, 类似于 rangeBands()
, 只是此函数的输出值是舍入后最接近的整数, so 12.3456 becomes just 12, for example. This is helpful for keeping visual elements lined up precisely on the pixel grid, for clean, sharp edges.
.rangeRoundBands([, w], 0.05); //This gives us nice, clean pixel values, with a teensy bit of visual space between them.
Interaction via Event Listeners
<p>Click on this text to update the chart with new data values (once).</p>
//On click, update with new data
d3.select("p")
.on("click", function() { //New values for dataset
dataset = [ , , , , , , , , , ,
, , , , , , , , , ]; //Update all rects
svg.selectAll("rect")
.data(dataset)
.attr("y", function(d) {
return h - yScale(d);
})
.attr("height", function(d) {
return yScale(d);
});
.
attr
(
"fill"
,
function
(
d
)
{
// <-- Down here!
return
"rgb(0, 0, "
+
(
d
*
10
)
+
")"
;
});
});
设置新的label text
svg.selectAll("text")
.data(dataset)
.text(function(d) {
return d;
})
.attr("x", function(d, i) {
return xScale(i) + xScale.rangeBand() / ;
})
.attr("y", function(d) {
return h - yScale(d) + ;
});
Transitions
.transition() //过渡效果
Without transition()
, D3 evaluates every attr()
statement immediately, so the changes in height and fill happen right away. When you add transition()
, D3 introduces the element of time. Rather than applying new values all at once, D3 interpolates between the old values and the new values, meaning it normalizes the beginning and ending values, and calculates all their in-between states. D3 is also smart enough to recognize and interpolate between different attribute value formats. For example, if you specified a height of 200px
to start but transition to just 100
(without the px
). Or if a blue
fill turns rgb(0,255,0)
. You don’t need to fret about being consistent; D3 takes care of it.
//Update all rects
svg.selectAll("rect")
.data(dataset)
.transition() // <-- This is new! Everything else here is unchanged.
.duration() // <-- Now this is new!
.attr("y", function(d) {
return h - yScale(d);
})
.attr("height", function(d) {
return yScale(d);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d * ) + ")";
});
//Update all labels
svg.selectAll("text")
.data(dataset)
.transition() // <-- This is new,
.duration() // and so is this.
.text(function(d) {
return d;
})
.attr("x", function(d, i) {
return xScale(i) + xScale.rangeBand() / ;
})
.attr("y", function(d) {
return h - yScale(d) + ;
});
duration() 定义过渡时间
The duration()
must be specified after the transition()
, and durations are always specified in milliseconds, so duration(1000)
is a one-second duration.
ease()定义过渡类型
ease()
must also be specified after transition()
, but before the attr()
statements to which the transition applies. ease()
can come before or after duration()
, but this sequence makes the most sense to me:
… //Selection statement(s)
.transition()
.duration()
.ease("linear")
… //attr() statements
其他内置的 easing functions有,例如:
circle:
Gradual ease in and acceleration until elements snap into place.
elastic:
The best way to describe this one is “sproingy.”
bounce:
Like a ball bouncing, then coming to rest.
delay()设置过渡什么时候开始,可以指定一个值, 毫秒级,也可以指定一个函数
…
.transition()
.delay() //1,000 ms or 1 second
.duration() //2,000 ms or 2 seconds
…
…
.transition()
.delay(function(d, i) {
return i * ;
})
.duration()
…
D3——Updates, Transitions, and Motion的更多相关文章
- [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] Animate Transitions in D3 v4
D3 makes it easy to add meaningful animations to your data visualizations. Whether it’s fading in ne ...
- 数据可视化(8)--D3数据的更新及动画
最近项目组加班比较严重,D3的博客就一拖再拖,今天终于不用加班了,赶紧抽点时间写完~~ 今天就将D3数据的更新及动画写一写~~ 接着之前的博客写~~ 之前写了一个散点图的例子,下面可以自己写一个柱状图 ...
- unity3d 学习笔记_____Native2d 刚体、冲击、联合使用
Mass Mass of the rigidbody. Linear Drag Drag coefficient affecting positional movement. Angular Drag ...
- Android 5.0自定义动画
材料设计中的动画对用户的操作给予了反馈,并且在与应用交互时提供了持续的可见性.材料主题提供了一些按钮动画和活动过渡,Android 5.0允许你自定义动画并且可以创建新的动画: Touch Feedb ...
- Host–Parasite(主从关系): Graph LSTM-in-LSTM for Group Activity Recognition
This article aims to tackle the problem of group activity recognition in the multiple-person scene. ...
- [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 an ...
- [D3] 12. Basic Transitions with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- D3中动画(transition函数)的使用
关于transition的几个基本点: 1. transition()是针对与每个DOM element的,每个DOM element的transition并不会影响其他DOM element的tra ...
随机推荐
- SQLite数据类型(学习必备)
最近在学习android端的数据库,以下知识点作为备用- 一.存储种类和数据类型: SQLite将数据值的存储划分为以下几种存储类型: NULL: 表示该值为NULL值. INTEGE ...
- TCP/IP提供网络传输速率
丢包(超时)->减少超时时间->ECN(有网络设备通知终端,有丢包发生)->DCTCP(优化快恢复) 丢包是超时的充分条件,但不是必要条件,因此也可通过其他方式获得丢包是否发生,比如 ...
- 使用 pjax 实现无刷新切换页面
一.目的 1.当打开链接的时候,页面是淡入显示,并且页面顶部会显示加载进度条,页面显示完成时,进度条加载满并且消失. 2.点击页面上的 a 标签时,显示加载进度条,并且当前页面淡出消失,当前页面淡出消 ...
- 数据集DataSet
ADO.NET数据访问技术的一个突出的特点就是支持离线访问,而实现这种离线访问技术的核心就是DateSet对象,该对象通过将数据驻留在内存来实现离线访问. DataSet对象由一组DataTable对 ...
- Transfer-Encoding:chunked 返回数据过长导致中文乱码
最近在写一个项目的后台时,前端请求指定资源后,返回JSON格式的数据,突然发现在返回的字节数过大时,最后的message中文数据乱码了,对于同一个接口的请求:当数据小时不会乱码,当数据量大了中文就乱码 ...
- Hbase配置指南
注意点 Hbase 需要zookeeper. Hbase 需要在各个节点的机器上配置. 集群中的启动顺序是Hadoop.zookeeper 和Hbase 搭建步骤 解压安装文件并配置环境变量. exp ...
- ckeditor添加自定义按钮整合swfupload实现批量上传图片
ckeditor添加自定义按钮整合swfupload实现批量上传图片给ckeditor添加自定义按钮,由于ckeditor只能上传一张图片,如果要上传多张图片就要结合ckfinder,而ckfinde ...
- 【SSH网上商城项目实战22】获取银行图标以及支付页面的显示
转自: https://blog.csdn.net/eson_15/article/details/51452243 从上一节的小demo中我们搞清楚了如何跟易宝对接以及易宝的支付流程.这一节 ...
- 【SSH网上商城项目实战19】订单信息的级联入库以及页面的缓存问题
转自: https://blog.csdn.net/eson_15/article/details/51433247 购物车这一块还剩最后两个问题,就是订单信息的级联入库和页面缓存,这里的 ...
- Golang把所有包括底层类库,输出到stderr的内容, 重新定向到一个日志文件里面?
不论应用是如何部署的,我们都期望能扑捉到应用的错误日志, 解决思路: 自己写代码处理异常拦截,甚至直接在main函数中写异常拦截. stderr重定向到某个文件里 使用 syscall.Dup2 第一 ...