[D3] 12. Basic Transitions with D3
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="../bower_components/underscore/underscore-min.js"></script>
<script src="../ventor/d3.min.js"></script>
<style type="text/css"> body {
padding-top: 50px;
padding-left: 100px; } #chartArea {
width: 400px;
height: 300px;
background-color: #CCC;
} .bar {
display: inline-block;
width: 20px;
height: 75px; /* Gets overriden by D3-assigned height below */
margin-right: 2px;
/* fill: teal; *//* SVG doesn't have background prop, use fill instead*/
z-index: 99;
} .bubble, .center {
display: inline-block;
fill: purple;
fill-opacity: 0.5;
stroke: black;
stroke-weight: 1px;;
z-index: 15;
} .center {
z-index: 10;
} .active {
fill: magenta;
fill-opacity: 0.5;
stroke-width: 3px;
} .axis path, .axis line {
fill: none;
stroke: #000;
stroke-width: 1px;
shape-rendering: crispEdges;
} </style>
</head>
<body>
<button onclick="update()">Update</button>
<section id="chartArea"></section>
<script> function update(){
console.log("update");
_.each(dataset, function(d) {
d.x = Math.round(Math.random() * 100);
d.y = Math.round(Math.random() * 100);
d.r = Math.round(5 + Math.random() * 10);
}); svg.selectAll('circle')
.transition()
.duration(600)
.style('fill', "lightblue")
.attr('cx', function(each_data, index) {
return xScale(each_data.x);
})
.attr('cy', function(each_data) {
return yScale(each_data.y);
})
.transition()
.duration(600)
.attr('r', function(each_data, i) {
return each_data.r;
});
} var dataset = _.map(_.range(30), function(num) {
return {
x: Math.round(Math.random() * 100),
y: Math.round(Math.random() * 100),
r: Math.round(5 + Math.random() * 10)
};
}), //reandom generate 15 data from 1 to 50
margin = {top: 20, right: 20, bottom: 40, left: 40},
w = 400 - margin.left - margin.right,
h = 300 - margin.top - margin.bottom; var svg = d3.select('#chartArea').append('svg')
.attr('width', w + margin.left + margin.right)
.attr('height', h + margin.top + margin.bottom)
.append('g') //The last step is to add a G element which is a graphics container in SBG.
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')'); //Then offset that graphic element by our left and top margins. var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) {
return d.y; //tell the max function just need to care about y prop
})])
.range([h, 0]); var yAxis = d3.svg.axis()
.scale(yScale)
.orient('left')
.ticks(10)
.innerTickSize(10)
.outerTickSize(10)
.tickPadding(10);
svg.append('g')
.attr('class', 'y axis')
.attr('transform', 'translate(0,0)')
.call(yAxis); var xScale = d3.scale.linear()
.domain([0, 100])
.range([0, w]); var xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom')
.ticks(10)
.innerTickSize(6)
.outerTickSize(12)
.tickPadding(12); svg.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0, ' + h + ')')
.call(xAxis); svg.selectAll('circle')
.data(dataset)
.enter()
.append('circle')// svg doesn't have div, use rect instead
.attr('class', "bubble")
.attr('cx', function(each_data, index) {
return xScale(each_data.x);
})
.attr('cy', function(each_data) {
return yScale(each_data.y);
})
.attr('r', function(each_data, i) {
return each_data.r;
})
.on('mouseover', function() {
d3.select(this).classed('active', true)
})
.on('mouseleave', function() {
d3.select(this).classed('active', false)
})
.on('mousedown', function(d) {
var p_cx = d.x, p_cy = d.y, p_r = d.r;
d3.select(this).transition().duration(500).attr('r', d.r * 1.5);
svg.append('circle')
.attr('class', "center")
.attr('cx', function() {
return xScale(p_cx);
})
.attr('cy', function() {
return yScale(p_cy);
})
.attr('r', function() {
return p_r / 4;
})
.style('fill', 'red');
})
.on('mouseup', function(d) { d3.select(this).transition().duration(250).delay(100).attr('r', d.r)
});
</script>
</body>
</html>
[D3] 12. Basic Transitions with D3的更多相关文章
- [D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [D3] 10. Creating Axes with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [D3] Create Chart Axes with D3 v4
Most charts aren’t complete without axes to provide context and labeling for the graphical elements ...
- [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] 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 ...
- [D3] Basic Interactivity with D3 v4
Data visualizations are a lot more interesting when they’re interactive. Whether it’s clicks, roll o ...
- [D3 + AngularJS] 15. Create a D3 Chart as an Angular Directive
Integrating D3 with Angular can be very simple. In this lesson, you will learn basic integration as ...
- d3可视化实战00:d3的使用心得和学习资料汇总
最近以来,我使用d3进行我的可视化工具的开发已经3个月了,同时也兼用其他一些图表类库,自我感觉稍微有点心得.之前我也写过相关文章,我涉及的数据可视化的实现技术和工具,但是那篇文章对于项目开发而言太浅了 ...
随机推荐
- Excel下拉框选项切换行颜色切换
选择行颜色变化范围 开始-条件格式-新创建规则-"使用公式-" 录入:=$104B="确认" 点击"格式(F)-"->填充,选择填充颜 ...
- Quartz2D 备忘 + 学习
Quartz2D Quartz2D是支持iOS和Mac系统的二维绘制引擎,它可以绘制: 绘制图形(图形,线条,圆等) 绘制文字 绘制/生成图片 读取/生成PDF 截图 Quartz2D主要功能就是以画 ...
- SVN - 基础知识
1. 术语 $ svn checkout URL [PATH] ----- 下载服务器所有文件 (clone) 到本地[path] --- 只需一次 $ svn checkout http: ...
- VS2010皮肤控件介绍
在我们平时使用的各种工具中,如QQ,迅雷,以及各种空间等,都提供了一些换肤功能,可以让我们选择各种我们喜欢的界面.本文就对VS中常用的窗口程序做一个简单的换肤,利用一个dll文件来进行实现. 首先我们 ...
- cf 219D
树形dp; 思想: 把正向边赋值为0:反向边赋值为1:然后求出点到其他点的最小距离: 两次dfs: 第一次是从下往上:记录每个点到所有子树中需要改变的边的条数: 第二次是自上往下:由父节点求子节点到所 ...
- Unreal Engine4 蓝图入门
微信公众号:UE交流学习 UE4开发群:344602753 蓝图是Unreal Engine的特点,用C++编程固然好,但是效率要低很多,主要是国内资料比较少,所以不太容易学习,用蓝图编程可以节 ...
- 分析ECMall的注册与登录机制
ecmall的注册流程index.php?app=member&act=register. 首先app是member,act是register方法. index.php中.通过ecmall的s ...
- 不可忽视的 .NET 应用5大性能问题
[编者按]本文系国内 ITOM 管理平台 OneAPM 翻译自 Steven Haines 的文章.Steven Haines 是 Pisksel 技术架构师,目前在奥兰多迪士尼乐园工作.他是在线教育 ...
- Android 向系统添加一个联系人信息contact
private void writeContacts() { Uri rawContacts = Uri.parse("content://com.android.contacts/raw_ ...
- OA学习笔记-004-Spring2.5配置
一.jar包 (1)spring.jar (2)Aop包 aspectjrt.jaraspectjweaver.jar (3)动态代理 cglib-nodep-2.1_3.jar (4)日志 comm ...