使用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. mysql 最小配置 及 安装

    [mysqld] # 设置3306端口 port= # 设置mysql的安装目录 basedir=D:\-Installer\-MySQL\mysql--winx64 # 设置mysql数据库的数据的 ...

  2. MYSQL DATE_FORMAT() 函数时间大小比较

    DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. DATE_FORMAT(date,format) 可以使用的格式有: 格式 描述 %a 缩写星期名 %b 缩写月名 %c 月,数值 ...

  3. Cheatsheet: 2017 10.01 ~ 12.31

    Mobile Updating Your App for iOS 11 Get Started With Natural Language Processing in iOS 11 Getting S ...

  4. springboot手动配置数据源:

    @Configuration @EnableTransactionManagement @PropertySource(value = {"classpath:config/source.p ...

  5. FisherYates费雪耶兹随机置乱算法

    public class FisherYates { public static void main(String[] args) { int[] arr = new int[10]; // 初始有序 ...

  6. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

  7. Influxdb的存储引擎

    创建Influxdb数据库时,我们可以看到下面选项,每个选项的含义就是本文要描述的: Influxdb内部数据的存储可以使用不同的存储引擎.当前0.8.7版本支持的是LevelDB, RocksDB, ...

  8. Q:接口与抽象类

    博文回答一下两个问题: 接口和抽象类的区别 选用接口和抽象类的依据 对于问题1: 从java语法的角度上来说,接口的所有成员和方法都是public的,且其方法均为abstract的.直到jdk1.8之 ...

  9. JavaScript高级编程———数据存储(cookie、WebStorage)

    JavaScript高级编程———数据存储(cookie.WebStorage) <script> /*Cookie 读写删 CookieUtil.get()方法根据cookie的名称获取 ...

  10. UOJ#400. 【CTSC2018】暴力写挂

    传送门 看到要求两棵树的 \(lca\) 深度不太好操作 考虑枚举第二棵树的 \(lca\),这样剩下的都是只和第一棵树有关的 而注意到 \(dis(x,y)=d(x)+d(y)-2d(lca(x,y ...