[D3] 10. Creating Axes 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 {
display: inline-block;
fill: purple;
fill-opacity: 0.5;
stroke: black;
stroke-weight: 1px;
} .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;
});
</script> <!--
1. svg should use 'fill' prop instead 'background-color'
2. svg width & height no need 'px'
3. attr(function(data_val, index){})
4. create svg, d3.select('selector').append('svg').attr('width', xxx).attr('height', xx)
5. svg should use 'rect' instead of 'div'
-->
</body>
</html>
[D3] 10. Creating Axes with D3的更多相关文章
- [D3] Create Chart Axes with D3 v4
Most charts aren’t complete without axes to provide context and labeling for the graphical elements ...
- [D3] 12. Basic Transitions with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [D3] Create DOM Elements with D3 v4
Change is good, but creating from scratch is even better. This lesson shows you how to create DOM el ...
- d3可视化实战00:d3的使用心得和学习资料汇总
最近以来,我使用d3进行我的可视化工具的开发已经3个月了,同时也兼用其他一些图表类库,自我感觉稍微有点心得.之前我也写过相关文章,我涉及的数据可视化的实现技术和工具,但是那篇文章对于项目开发而言太浅了 ...
- [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] Modify DOM Elements with D3 v4
Once you can get hold of DOM elements you’re ready to start changing them. Whether it’s changing col ...
- [D3] Select DOM Elements with D3 v4
Before you can create dazzling data driven documents, you need to know how D3 accesses the DOM. This ...
- [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 ...
- D3、EChart、HighChart绘图demol
1.echarts: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
随机推荐
- VOIP概述
简介 VoIP(Voice over Internet Protocol)就是将模拟声音讯号(Voice)数字化,以数据封包(Data Packet)的型式在 IP 数据网络 (IP Network) ...
- [原博客] BZOJ 2242 [SDOI2011] 计算器
题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...
- Discuz! 7.2 SQL注入exp
已经有人写出一些工具了,但是感觉不怎么好用,就自己写了个. 参数:1.可直接getshell2.爆管理账号密码3.爆表前缀如果表前缀不是默认的cdb_ 只需更改代码中的 $table即可,方便快捷. ...
- hdu 3394 Railway
这是一道用tarjin求双连通分量的题: 其中,不需要修的道路就是桥的数目: 在图的每个极大环中,如果点的数目小于边的数目,显然这个环中含有子环,并且这个环的边数就是这个环中有冲突的边的数目: 如果点 ...
- 设计模式之策略模式Strategy
/** * 策略设计模式 * 策略模式:定义一系列的算法族,使他们之间可以相互转换,动态的改变其行为. * 问题:设计一个鸭子模拟游戏. * 现在有一群鸭子: * ①这些鸭可以有飞的行为(分为快和慢) ...
- 【网络流24题】No.16 数字梯形问题 (不相交路径 最大费用流)
[题意] 给定一个由 n 行数字组成的数字梯形如下图所示. 梯形的第一行有 m 个数字.从梯形的顶部的 m 个数字开始,在每个数字处可以沿左下或右下方向移动, 形成一条从梯形的顶至底的路径.规则 1: ...
- Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性
1.XML文件中的OnClick 属性可以指定在Activity中处理点击事件的方法,Activity中必须定义该属性指定的值作为方法的名字且有一个View类型的参数,表示此物件被点击. 2.使用se ...
- greenDaoMaster的学习研究
最近一直在研究一个第三方的开源框架,greenDaoMaster是一个移动开发的ORM框架,由于网上一直查不到使用资料,所以自己摸索总结下用法. 首先需要新建一个JAVA项目用来自动生成文件.需要导入 ...
- Process.StandardInput属性
获取用于写入应用程序输入的流. 命名空间:System.Diagnostics程序集:System(在 system.dll 中) 语法 C# C++ VB public StreamWr ...
- javascript循环
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...