[D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)

<!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 {
display: inline-block;
fill: purple;
fill-opacity: 0.5;
stroke: black;
stroke-weight: 1px;
} .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>
<section id="chartArea"></section>
<script>
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) {
d3.select(this).attr('r', d.r * 1.2)
})
.on('mouseup', function(d) {
d3.select(this).attr('r', d.r)
});
</script>
</body>
</html>
[D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)的更多相关文章
- [D3] Build an Area Chart with D3 v4
Similar to line charts, area charts are great for displaying temporal data. Whether you’re displayin ...
- [D3] Build a Line Chart with D3 v4
Line charts are often used to plot temporal data, like a stock price over time. In this lesson we’ll ...
- [D3] 12. Basic Transitions with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- A better way to learn D3 js - iLearning D3.js
iLearning D3.js Basic is an iPad app to learn and code with D3. In 1.1 version, new tutorial is prov ...
- [D3] 13. Cleaner D3 code with selection.call()
selection.call() method in D3 can aid in code organization and flexibility by eliminating the need t ...
- D3笔记01——D3简介与安装
1 D3简介 发布于2011年,全称Data-Driven Documents,直译为“数据驱动的文档”. 简单概括为一句话:D3是一个Javascript的函数库,是用来做数据可视化的.文档指DOM ...
- 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本
使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...
- [D3] Build a Column Chart with D3 v4
Column and bar charts are staples of every visualization library. They also make a great project for ...
- [D3] Creating a D3 Force Layout in React
Learn how to leverage d3's layout module to create a Force Layout inside of React. We'll take a look ...
随机推荐
- Xcode 修改工程名称
总会遇到几个项目,在做到一半的时候被要求改项目名,网上找了下相关的资料,大多数是xcode5以前的版本,所以解决好了在这里mark一下,给需要的人. 目标为:将项目名XCD4改成xcd5. 先上结果图 ...
- TiDB:支持 MySQL 协议的分布式数据库解决方案
[编者按]TiDB 是国内 PingCAP 团队开发的一个分布式 SQL 数据库.其灵感来自于 Google 的 F1,TiDB 支持包括传统 RDBMS 和 NoSQL 的特性.在国内 ITOM 管 ...
- http://www.cnblogs.com/xdp-gacl/p/4040019.html
http://www.cnblogs.com/xdp-gacl/p/4040019.html
- 一起啃PRML - 1.2.2 Expectations and covariances 期望和协方差
一起啃PRML - 1.2.2 Expectations and covariances 期望和协方差 @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ ...
- spm_预处理实验记录
参考:<SPM8 MANNUAL> Chapter 28 Auditory fMRI data
- easyui datagrid pagesize设置无效
- 关于Session
转自:http://blog.csdn.net/wang379275614/article/details/9627755 Session理解: Session:在计算机中,尤其是在网络应用中,称 ...
- Android开发必知--使用View.setId的正确姿势
这两天在写一个柱状图的自定义控件,用的直接继承ViewGroup的方式实现的,我们都知道,这是自定义控件里面最简单的一种了,有时间写个总结分享一下.这里我想说的重点是,在写这个自定义控件的时候遇到了个 ...
- 【基于spark IM 的二次开发笔记】第二天 树形结构
MessageTreePlugin.java final MessageTreeTab messageTreeTab = new MessageTreeTab(); /** * Adds a tab ...
- Java学习感受
一个暑假,两本书,让我了解到了Java的神奇美妙,这个工具的年龄跟我一样大,然而日久弥新,随着电脑技术科学家,工程师的开发,Java的功能越来越完善.学习的时间不长,但我比较着学习,把Java跟C.C ...