使用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的更多相关文章

  1. [D3] Create Chart Axes with D3 v4

    Most charts aren’t complete without axes to provide context and labeling for the graphical elements ...

  2. [D3] 10. Creating Axes with D3

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. D3.js学习(七)

    上一节中我们学会了如何旋转x轴标签以及自定义标签内容,在这一节中,我们将接触动画(transition) 首先,我们要在页面上添加一个按钮,当我们点击这个按钮时,调用我们的动画.所以,我们还需要在原来 ...

  4. D3 learning notes

    D3 https://d3js.org/ 数据驱动文档显示, 利用 SVG HTML CSS技术. D3.js is a JavaScript library for manipulating doc ...

  5. D3、EChart、HighChart绘图demol

    1.echarts:   <!DOCTYPE html>   <html>   <head>   <meta charset="utf-8" ...

  6. d3浅谈

    d3是一个及其庞大的库,有20个模块,大小也达到了216kb,是JQ1.x的2倍多,JQ3.x的3倍多,JQ本来就挺笨重的一个库,d3更是如此,但是它的功能确实很强悍~ d3的定位是一个科学计算库,并 ...

  7. D3.js 入门学习(二) V4的改动

    //d3.scan /* 新的d3.scan方法对数组进行线性扫描,并根据指定的比较函数返回至少一个元素的索引. 这个方法有点类似于d3.min和d3.max. 而d3.scan可以得到极值的索引而不 ...

  8. 软件项目技术点(1)——d3.interpolateZoom-在两个点之间平滑地缩放平移

    AxeSlide软件项目梳理   canvas绘图系列知识点整理 软件参考d3的知识点 我们在软件中主要用到d3.js的核心函数d3.interpolateZoom - 在两个点之间平滑地缩放平移.请 ...

  9. 【D3 API 中文手冊】

    [D3 API 中文手冊] 声明:本文仅供学习所用,未经作者同意严禁转载和演绎 <D3 API 中文手冊>是D3官方API文档的中文翻译. 始于2014-3-23日,基于VisualCre ...

随机推荐

  1. vue+vuex+axios从后台获取数据存入vuex,组件之间共享数据

    在vue项目中组件间相互传值或者后台获取的数据需要供多个组件使用的情况很多的话,有必要考虑引入vuex来管理这些凌乱的状态,今天这边博文用来记录这一整个的过程,后台api接口是使用webpack-se ...

  2. [转]SAPUI5 (01) - OpenUI5环境搭建

    本文转自:http://blog.csdn.net/stone0823/article/details/53750094 版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn ...

  3. Data Warehouse Hardware - DW硬件需求计算

    1. Disk I/O, 硬盘IO速度 硬盘的IO速度一直都是数据库的瓶颈,所以有条件的情况下尽可能的使用高IO的磁盘. 可以使用微软的工具SQLIO测试磁盘的IOPS 2. CPU的主频, DW和传 ...

  4. 分享一个好用的dns

  5. C# 日历类

    using System; namespace DotNet.Utilities { /// <summary> /// 农历属性 /// </summary> public ...

  6. yum卸载

    完全卸载依赖 -- 正常安装 yum install sl -- 列出操作 yum history list sl -- 根据显示install操作的id进行删除 yum history undo { ...

  7. [LeetCode]Maximum Subarray题解

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  8. js画一棵树

    用纯js画一棵树.思路: 1.一棵树的图片,作为页面背景: 2.通过html5中的canvas画布进行遮罩: 3.定时每隔10ms,从下往上清除1px的遮罩: <!DOCTYPE html> ...

  9. 关于java赋值运算的一个小例子

    直接贴代码,这个也是做题目中遇见的,觉得很好奇,查了一波,然后自己编写代码看了一下,果真如此,哈哈哈...... public class 关于Boolean的赋值运算 { public static ...

  10. APM飞控系统详细介绍

    APM飞控系统详细介绍2013-04-05 12:28:24   来源:   评论:2 点击:10303 APM飞控系统是国外的一个开源飞控系统,能够支持固定翼,直升机,3轴,4轴,6轴飞行器.在此我 ...