D3——Axes
使用d3.svg.axis() 创建一个 axis function:
var xAxis = d3.svg.axis();
an axis function is called, it doesn’t return a value, but generates the visual elements of the axis, including lines, labels, and ticks.
xAxis.scale(xScale);
设置label相对axis的位置,默认为bottom, 对horizontal axes(横轴)来说可以设置的值为 top and bottom. For vertical axes(纵轴), use leftand right:
xAxis.orient("bottom");
链式写法:
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
将横轴添加到画布svg上(to actually generate the axis and insert all those little lines and labels into our SVG, we must call the xAxis function)
svg.append("g")
.call(xAxis);
例子:
<script type="text/javascript">
//Width and height
var w = ;
var h = ;
var padding = ;
var dataset = [
[, ], [, ], [, ], [, ], [, ],
[, ], [, ], [, ], [, ], [, ],
[, ]
];
//Create scale functions
var xScale = d3.scale.linear()
.domain([, d3.max(dataset, function(d) {
return d[]; })])
.range([padding, w - padding * ]);
var yScale = d3.scale.linear()
.domain([, d3.max(dataset, function(d) {
return d[]; })])
.range([h - padding, padding]);
var rScale = d3.scale.linear()
.domain([, d3.max(dataset, function(d) {
return d[]; })])
.range([, ]);
//Define X axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create circles
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d[]);
})
.attr("cy", function(d) {
return yScale(d[]);
})
.attr("r", function(d) {
return rScale(d[]);
});
//Create labels
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d[] + "," + d[];
})
.attr("x", function(d) {
return xScale(d[]);
})
.attr("y", function(d) {
return yScale(d[]);
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "red");
//Create X axis
svg.append("g")
.call(xAxis);
</script>

只创建了横纵轴的例子
var w = ;
var h = ;
var padding = ; var dataset = [
[, ], [, ], [, ], [, ], [, ],[, ], [, ], [, ], [, ], [, ],[, ]
]; //create xScale,yScale
var xScale = d3.scale.linear()
.domain([, d3.max(dataset, function(d) { return d[]; })])
.range([padding, w - padding * ]);
var yScale = d3.scale.linear()
.domain([, d3.max(dataset, function(d) { return d[]; })])
.range([h - padding, padding]); //define X axis , Y axis
var xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var yAxis = d3.svg.axis().scale(yScale).orient("right"); //Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h); //Create X axis , Y axis svg.append("g").call(xAxis);
svg.append("g").call(yAxis);

为axes设置样式
svg.append("g")
.attr("class", "axis") //Assign "axis" class
.call(xAxis);
// set css style .axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
} .axis text {
font-family: sans-serif;
font-size: 11px;
}

将X axis 向下平移
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);

上面的例子中我们并没有指定坐标轴的ticks个数,如果没有特别指定,D3会根据我们的scale (eg: xScale)和其他信息自动为我们设置合适个数的ticks。
设置ticks个数:
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(); //Set rough # of ticks

添加上Y axis:
//reset padding= 30;
var padding = ; //Define Y axis
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(); //Create Y axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);

Formatting Tick Labels
var formatAsPercentage = d3.format(".1%");
xAxis.tickFormat(formatAsPercentage);
formatAsPercentage(.); //"36.5%"
formatAsPercentage(1.2); //"120.0%"
formatAsPercentage(-.); //"-50.0%"
D3——Axes的更多相关文章
- [D3] Create Chart Axes with D3 v4
Most charts aren’t complete without axes to provide context and labeling for the graphical elements ...
- [D3] 10. Creating Axes with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- D3.js学习(七)
上一节中我们学会了如何旋转x轴标签以及自定义标签内容,在这一节中,我们将接触动画(transition) 首先,我们要在页面上添加一个按钮,当我们点击这个按钮时,调用我们的动画.所以,我们还需要在原来 ...
- D3 learning notes
D3 https://d3js.org/ 数据驱动文档显示, 利用 SVG HTML CSS技术. D3.js is a JavaScript library for manipulating doc ...
- D3、EChart、HighChart绘图demol
1.echarts: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- d3浅谈
d3是一个及其庞大的库,有20个模块,大小也达到了216kb,是JQ1.x的2倍多,JQ3.x的3倍多,JQ本来就挺笨重的一个库,d3更是如此,但是它的功能确实很强悍~ d3的定位是一个科学计算库,并 ...
- D3.js 入门学习(二) V4的改动
//d3.scan /* 新的d3.scan方法对数组进行线性扫描,并根据指定的比较函数返回至少一个元素的索引. 这个方法有点类似于d3.min和d3.max. 而d3.scan可以得到极值的索引而不 ...
- 软件项目技术点(1)——d3.interpolateZoom-在两个点之间平滑地缩放平移
AxeSlide软件项目梳理 canvas绘图系列知识点整理 软件参考d3的知识点 我们在软件中主要用到d3.js的核心函数d3.interpolateZoom - 在两个点之间平滑地缩放平移.请 ...
- 【D3 API 中文手冊】
[D3 API 中文手冊] 声明:本文仅供学习所用,未经作者同意严禁转载和演绎 <D3 API 中文手冊>是D3官方API文档的中文翻译. 始于2014-3-23日,基于VisualCre ...
随机推荐
- 转自IBM:Apache HTTP Server 与 Tomcat 的三种连接方式介绍
http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/index.html 整合 Apache Http Server ...
- 【css】清除浮动的几种方式
[css]清除浮动的几种方式 因为浮动框不在普通的文档流中,所以它不占据空间.如下面的代码: .news { background-color:gray; border:1px solid bla ...
- [Linux]C语言Linux系统编程创建进程
1.进程ID 每一个进程都由一个唯一的标识符表示,即进程ID,简称pid.系统保证在某时刻每个pid都是唯一的. 1.1分配进程ID 缺省情况下,内核将进程ID的最大值限制为32768,可以在此处设置 ...
- Spring中三个重要概念 IOC AOP Bean
Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...
- 撩课-Web大前端每天5道面试题-Day15
1.请描述一下 cookies,sessionStorage 和 localStorage 的区别? cookie是网站为了标示用户身份而储存在用户本地终端(Client Side)上的数据(通常经过 ...
- 【说文解字】Unix与Linux
历史 Unix操作系统是由Ken Thompson和Dennis Ritchie于1969-1970年发明. 它的部分技术来源可以追溯到Multics工程,后者因为过于庞大复杂而失败. 研究人员吸取教 ...
- BZOJ4650: [Noi2016]优秀的拆分(hash 调和级数)
题意 题目链接 Sol NOI的题都这么良心么.. 先交个\(n^4\)暴力 => 75 hash优化一下 => 90 然后\(90\)到\(100\)分之间至少差了\(10\)难度台阶= ...
- Java中的数据类型转换
先来看一个题: Java类Demo中存在方法func0.func1.func2.func3和func4,请问该方法中,哪些是不合法的定义?( ) public class Demo{ float fu ...
- iOS各种profile文件
iOS是一个非常封闭的系统.授权文件(.mobileprovision)和签名证书文件(.cer)的存在就是为了验证身份信息.一般情况下,比如ssh登陆或者scp需要私钥.公钥对即可,iOS也是基本采 ...
- SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis)
SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis) 如果你使用的是 Intellij IDEA,请查看: SSM的配置流程详细的写了出来,方便很少 ...