【D3.V3.js系列教程】--(十二)坐标尺度

1、多种类型的缩放尺度

Quantitative Scales
Linear Scales
Identity Scales
Power Scales

可以参考https://github.com/mbostock/d3/wiki/Quantitative-Scales

我们一般采用线性缩放

2、定义域和值域

定义域范围domain([100, 500])

值域范围.range([10, 350])

var scale = d3.scale.linear()
.domain([100, 500])
.range([10, 350]);

3、坐标轴的缩放

返回所有坐标中X值最大的X

d3.max(dataset, function(d) {    //Returns 480
return d[0]; //References first value in each sub-array
});

X轴缩放

var xScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[0]; })])
.range([0, w]);

Y轴缩放

var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[1]; })])
.range([0, h]);

4、设定圆心的坐标(注意使用和坐标同样缩放尺度的坐标值)

.attr("cx", function(d) {
return d[0];
})

缩放后的坐标X值

.attr("cx", function(d) {
return xScale(d[0]);
})

Y值同样如此:

.attr("cy", function(d) {
return d[1];
})

修改后就是:

.attr("cy", function(d) {
return yScale(d[1]);
})

5、设定文本坐标值(同上)

.attr("x", function(d) {
return d[0];
})
.attr("y", function(d) {
return d[1];
})

变成:

.attr("x", function(d) {
return xScale(d[0]);
})
.attr("y", function(d) {
return yScale(d[1]);
})

6、源码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>testD3-10-scale.html</title>
<script type="text/javascript" src="http://localhost:8080/spring/js/d3.v3.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
//Width and height
var w = 500;
var h = 100; var dataset = [
[5, 20], [480, 90], [250, 50], [100, 33], [330, 95],
[410, 12], [475, 44], [25, 67], [85, 21], [220, 88]
]; //Create scale functions
var xScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[0]; })])
.range([0, w]); var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[1]; })])
.range([0, h]); //Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h); svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", function(d) {
return Math.sqrt(h - d[1]);
}); svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d[0] + "," + d[1];
})
.attr("x", function(d) {
return xScale(d[0]);
})
.attr("y", function(d) {
return yScale(d[1]);
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "red");
</script> </body>
</html>

7、效果

注:点大小与圈大小成正比,想把大的放在下面,只要改变Y轴值域倒转即可: .range([h , 0]);

注:为了SVG边缘不被截断可以设置边距: .range([h - padding, padding]);

注:自定义半径:

var rScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[1]; })])
.range([2, 5]);

然后,这样设置半径

.attr("r", function(d) {
return rScale(d[1]);
});

注:将点变得更紧凑些,设置最大的坐标范围:,
       [600, 150]

注:SVG的纵向还是很拥挤,调整SVG纵坐标:

注:其他方法和缩放尺度:

Other Methods

d3.scale.linear() has several other handy methods that deserve a brief mention here:

  • nice() — This tells the scale to take whatever input domain that you gave torange() and expand both ends to the nearest round value. From the D3 wiki: “For example, for a domain of [0.20147987687960267, 0.996679553296417], the nice domain is [0.2, 1].” This is useful for normal people, who find it hard to read numbers like 0.20147987687960267.
  • rangeRound() — Use rangeRound() in place of range() and all values output by the scale will be rounded to the nearest whole number. This is useful if you want shapes to have exact pixel values, to avoid the fuzzy edges that may arise with antialiasing.
  • clamp() — By default, a linear scale can return values outside of the specified range. For example, if given a value outside of its expected input domain, a scale will return a number also outside of the output range. Calling .clamp(true) on a scale, however, forces all output values to be within the specified range. Meaning, excessive values will be rounded to the range’s low or high value (whichever is nearest).

Other Scales

In addition to linear scales (discussed above), D3 has several other scale methods built-in:

  • identity — A 1:1 scale, useful primarily for pixel values
  • sqrt — A square root scale
  • pow — A power scale (good for the gym)
  • log — A logarithmic scale
  • quantize — A linear scale with discrete values for its output range, for when you want to sort data into “buckets”
  • quantile — Similar to above, but with discrete values for its input domain (when you already have “buckets”)
  • ordinal — Ordinal scales use non-quantitative values (like category names) for output; perfect for comparing apples and oranges

【D3.V3.js系列教程】--(十二)坐标尺度的更多相关文章

  1. 【D3.V3.js系列教程】--(十四)有路径的文字

    [D3.V3.js系列教程]--(十四)有路径的文字 1. 在 svg 中插入一個 text // 在 body 中插入一個 svg var svg = d3.select('body').appen ...

  2. 【D3.V3.js系列教程】--(十五)SVG基本图形绘制

    [D3.V3.js系列教程]--(十五)SVG基本图形绘制 1.path <!DOCTYPE html> <html> <head> <meta charse ...

  3. webpack4 系列教程(十二):处理第三方JavaScript库

    教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十二):处理第三方 JavaScript 库>原文地址.或者来我的小站看更多内容:godbm ...

  4. CRL快速开发框架系列教程十二(MongoDB支持)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  5. Unity3D脚本中文系列教程(十二)

    http://dong2008hong.blog.163.com/blog/static/4696882720140313545332/ GameObject类,继承自Object Unity场景中所 ...

  6. Spring Boot系列教程十二:Spring boot集成Redis

    一.创建项目 项目名称为 "springboot_redis",创建过程中勾选 "Web","Redis",第一次创建Maven需要下载依赖 ...

  7. Spring Boot2 系列教程 (十二) | 整合 thymeleaf

    前言 如题,今天介绍 Thymeleaf ,并整合 Thymeleaf 开发一个简陋版的学生信息管理系统. SpringBoot 提供了大量模板引擎,包含 Freemarker.Groovy.Thym ...

  8. Spring Boot2 系列教程(十二)@ControllerAdvice 的三种使用场景

    严格来说,本文并不算是 Spring Boot 中的知识点,但是很多学过 SpringMVC 的小伙伴,对于 @ControllerAdvice 却并不熟悉,Spring Boot 和 SpringM ...

  9. Go语言系列教程(十二)之函数完结篇

    Hello,各位小伙伴大家好,我是小栈君.上一期我们讲到了关于函数的有参.无参.匿名函数,本期我们分享一下关于go语言函数类型.匿名函数和闭包的概念和实战.闲话不多说,立马开始分享. 在Go语言中,函 ...

随机推荐

  1. python初学笔记(二)

    注释: 任何时候,我们都可以给程序加上注释.注释是用来说明代码的,给自己或别人看,而程序运行的时候,Python解释器会直接忽略掉注释,所以,有没有注释不影响程序的执行结果,但是影响到别人能不能看懂你 ...

  2. echarts实现上海地域PM值(map、timeline)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 1001. 害死人不偿命的(3n+1)猜想

    /* * Main.c * 1001. 害死人不偿命的(3n+1)猜想 * Created on: 2014年8月27日 * Author: Boomkeeper *********测试通过***** ...

  4. LINUX常用命令--基础篇(一)

    1. man 对你熟悉或不熟悉的命令提供帮助解释 eg:man ls 就可以查看ls相关的用法 注:按q键或者ctrl+c退出,在linux下可以使用ctrl+c终止当前程序运行. 2. ls 查看目 ...

  5. LINUX查看硬件配置命令

    LINUX查看硬件配置命令   系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinf ...

  6. 谈谈文件增量同步算法:RSYNC和CDC

    谈谈文件增量同步算法:RSYNC和CDC 分类: 数据同步 增量备份 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在研究文件的增量同步问题,着重研究了文件差异编码部分,因为这个其实是文件 ...

  7. Oracle EBS-SQL (BOM-3):检查期间新增Bom数量.sql

    --本周系统BOM汇总记录 SELECT         ITM.SEGMENT1  物料编码, ITM.DESCRIPTION   物料描述, bom2.CREATION_DATE   创建日期, ...

  8. HDU 5716 带可选字符的多字符串匹配(ShiftAnd)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5716 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串 ...

  9. <%=id%>是什么意思

    <%=% > 这里可以绑定后台的一个PUBLIC变量 <% % > 如果没有等号  可以在里面写C#语句

  10. 2014.9.3数据库CRUD

    CRUD 增删改查 DCL 数据控制语言:备份,grant DML 数据操作语言: CRUD DDL 数据定义语言:create drop alter 自增长列不能赋值 增: Insert into  ...