【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])

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

3、坐标轴的缩放

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

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

X轴缩放

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

Y轴缩放

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

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

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

缩放后的坐标X值

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

Y值同样如此:

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

修改后就是:

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

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

  1. .attr("x", function(d) {
  2. return d[0];
  3. })
  4. .attr("y", function(d) {
  5. return d[1];
  6. })

变成:

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

6、源码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>testD3-10-scale.html</title>
  6. <script type="text/javascript" src="http://localhost:8080/spring/js/d3.v3.js"></script>
  7. <style type="text/css">
  8. </style>
  9. </head>
  10. <body>
  11. <script type="text/javascript">
  12. //Width and height
  13. var w = 500;
  14. var h = 100;
  15.  
  16. var dataset = [
  17. [5, 20], [480, 90], [250, 50], [100, 33], [330, 95],
  18. [410, 12], [475, 44], [25, 67], [85, 21], [220, 88]
  19. ];
  20.  
  21. //Create scale functions
  22. var xScale = d3.scale.linear()
  23. .domain([0, d3.max(dataset, function(d) { return d[0]; })])
  24. .range([0, w]);
  25.  
  26. var yScale = d3.scale.linear()
  27. .domain([0, d3.max(dataset, function(d) { return d[1]; })])
  28. .range([0, h]);
  29.  
  30. //Create SVG element
  31. var svg = d3.select("body")
  32. .append("svg")
  33. .attr("width", w)
  34. .attr("height", h);
  35.  
  36. svg.selectAll("circle")
  37. .data(dataset)
  38. .enter()
  39. .append("circle")
  40. .attr("cx", function(d) {
  41. return xScale(d[0]);
  42. })
  43. .attr("cy", function(d) {
  44. return yScale(d[1]);
  45. })
  46. .attr("r", function(d) {
  47. return Math.sqrt(h - d[1]);
  48. });
  49.  
  50. svg.selectAll("text")
  51. .data(dataset)
  52. .enter()
  53. .append("text")
  54. .text(function(d) {
  55. return d[0] + "," + d[1];
  56. })
  57. .attr("x", function(d) {
  58. return xScale(d[0]);
  59. })
  60. .attr("y", function(d) {
  61. return yScale(d[1]);
  62. })
  63. .attr("font-family", "sans-serif")
  64. .attr("font-size", "11px")
  65. .attr("fill", "red");
  66. </script>
  67.  
  68. </body>
  69. </html>

7、效果

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

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

注:自定义半径:

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

然后,这样设置半径

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

注:将点变得更紧凑些,设置最大的坐标范围:,
       [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. C#BASE64 UTF8字符串加密解密

    base 64 解码 base64 bb = new base64(); string orgStr= Encoding.Default.GetString(bb.GetDecoded("b ...

  2. 简易 Ajax 入门案例

    AJAX = 异步 JavaScript 及 XML(Asynchronous JavaScript and XML) AJAX 不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的 Web ...

  3. 百度地图Label 样式:label.setStyle

    创建文本标注对象设置样式的时候,其中的backgroundColor属性居然还支持透明啊,不过改变数值好像对效果没有影响 var numLabel = new BMap.Label(num); num ...

  4. matlab 相关性分析

    Pearson相关系数 考察两个事物(在数据里我们称之为变量)之间的相关程度,简单来说就是衡量两个数据集合是否在一条线上面.其计算公式为: 或或 N表示变量取值的个数. 相关系数r的值介于–1与+1之 ...

  5. Office 2010 SP2简体中文正式版下载

    此次发布的SP2包含最新的更新,提高安全性.性能和稳定性,此外SP2还是之前发布的所有更新.累积性更新的汇总.SP2还改善了Office 2010与IE10.Windows 8.Windows Ser ...

  6. qutIm编译

    官网:http://www.qutim.org/ 原文地址:http://wiki.qutim.org/en/building_from_git 依赖: Qt4-dev 4.7:http://qt-p ...

  7. 今天在发布IIS站点的时候遇到了一些问题

    1.HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 分析:一般5XX的错误都是服务器的问题,这里把应用程序池 ...

  8. oracle解锁表

    select b.owner,b.object_name,a.session_id,a.locked_mode,c.serial#,c.sid||','||c.serial# from v$locke ...

  9. 雕爷牛腩这样判断零售未来-搜狐IT

    雕爷牛腩这样判断零售未来-搜狐IT 雕爷牛腩这样判断零售未来

  10. javascript获取url地址问好后面的值,包括问号

    javascript获取url地址问好后面的值,包括问号 <!DOCTYPE html> <html lang="en"> <head> < ...