[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个月了,同时也兼用其他一些图表类库,自我感觉稍微有点心得.之前我也写过相关文章,我涉及的数据可视化的实现技术和工具,但是那篇文章对于项目开发而言太浅了 ...
随机推荐
- 一些Swift编程语言的相关资料
苹果官方Swift文档<The Swift Programming Language> 苹果开发者Swift文档及介绍 中文版Apple官方Swift教程(Github协作翻译中) Git ...
- Python join()方法
描述 Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. 语法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的 ...
- POJ 2409 Let it Bead(Polya定理)
点我看题目 题意 :给你c种颜色的n个珠子,问你可以组成多少种形式. 思路 :polya定理的应用,与1286差不多一样,代码一改就可以交....POJ 1286题解 #include <std ...
- [转贴]C++开源库
C++在“商业应用”方面,曾经是天下第一的开发语言,但这一 桂冠已经被java抢走多年.因为当今商业应用程序类型,已经从桌面应用迅速转移成Web应 用.当Java横行天下之后,MS又突然发力,搞出C# ...
- WebView 和Animation冲突
当有WebView时,可能会出现 动画没有执行过程. 解决方法: child.setLayerType(View.LAYER_TYPE_HARDWARE, null); child.setAnimat ...
- WordPress Cart66 Lite插件HTML注入漏洞
漏洞名称: WordPress Cart66 Lite插件HTML注入漏洞 CNNVD编号: CNNVD-201310-525 发布时间: 2013-10-23 更新时间: 2013-10-23 危害 ...
- Linux kernel 内存损坏漏洞
漏洞名称: Linux kernel 内存损坏漏洞 CNNVD编号: CNNVD-201310-143 发布时间: 2013-10-11 更新时间: 2013-10-11 危害等级: 中危 漏洞类 ...
- STM32F0308开发环境的选择--CooCox CoIDE篇
STM32的开发环境有很多总,官方手册也提供了IAR Embedded Workbench.MDK-ARM和TrueSTUDIO这3种.今天我试用了CooCox CoIDE,是免费的集成开发环境,同T ...
- ZOJ --- 3516 Tree of Three
Tree of Three Time Limit: 2 Seconds Memory Limit: 65536 KB Now we have a tree and some queries ...
- ASCII,Unicode和UTF-8字符编码
ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte).也就是 ...