介绍

D3.js是一个JavaScript库,它可以通过数据来操作文档。D3可以通过使用HTML、SVG和CSS把数据鲜活形象地展现出来。D3严格遵循Web标准,因而可以让你的程序轻松兼容现代主流浏览器并避免对特定框架的依赖。同时,它提供了强大的可视化组件,可以让使用者以数据驱动的方式去操作DOM。

总的来说,D3是这样一个特殊的JavaScript库,它利用现有的Web标准,通过更简单的(数据驱动)方式来制作炫目的可视化效果。D3.js由Mike Bostock制作。之前他制作过一个叫Protovis的数据可视化JavaScript库,如今它已经被D3.js取代。如果想了解更多诸如D3.js制作过程、影响Protovis和D3.js的相关理论这类的信息,可以看看下面的链接。而本书将着眼于如何使用D3.js来增强可视化。D3使用JavaScript实现数据可视化的方式比较特别,因此刚开始时可能会让人觉得有些难懂。我希望通过本书中的大量实例,其中有基础的,也有高级的话题,能够帮助大家更好更高效地使用D3。一旦理解了原理,使用D3就可以让数据可视化的效率和丰富程度产生指数化的增长。

这是一个摘自Mike Bostock博客的一个数据可视化实例:

图一

<!-- index.html-->
<!DOCTYPE html>
<img src="topo.svg" width="960" height="680">
<!--legend.svg-->
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="960" height="600" viewBox="0 0 960 600" fill="none">
  <g transform="translate(540,640)" font-size="10" font-family="sans-serif" text-anchor="middle">
    <rect height="8" x="0" width="6" fill="#fff7ec"></rect>
    <rect height="8" x="6" width="13" fill="#fee8c8"></rect>
    <rect height="8" x="19" width="23" fill="#fdd49e"></rect>
    <rect height="8" x="42" width="42" fill="#fdbb84"></rect>
    <rect height="8" x="84" width="49" fill="#fc8d59"></rect>
    <rect height="8" x="133" width="56" fill="#ef6548"></rect>
    <rect height="8" x="189" width="78" fill="#d7301f"></rect>
    <rect height="8" x="267" width="110" fill="#b30000"></rect>
    <rect height="8" x="377" width="23" fill="#7f0000"></rect>
    <text x="0" y="-6" fill="#000" text-anchor="start" font-weight="bold">Population per square mile</text>
    <g transform="translate(6,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">1</text>
    </g>
    <g transform="translate(19,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">10</text>
    </g>
    <g transform="translate(42,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">50</text>
    </g>
    <g transform="translate(84,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">200</text>
    </g>
    <g transform="translate(133,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">500</text>
    </g>
    <g transform="translate(189,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">1,000</text>
    </g>
    <g transform="translate(267,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">2,000</text>
    </g>
    <g transform="translate(377,0)">
      <line stroke="#000" y2="13" x1="0.5" x2="0.5"></line>
      <text fill="#000" y="16" x="0.5" dy="0.71em">4,000</text>
    </g>
  </g>
</svg>

package.json

{   
  "private": true,  
  "license": "gpl-3.0",   

  "author": {   
    "name": "Mike Bostock",   
    "url": "https://bost.ocks.org/mike"
  },

  "scripts": {
    "prepublish": "bash prepublish"
  },

  "devDependencies": {   
    "d3-scale": "^1.0.4",
    "d3-scale-chromatic": "^1.1.0",
    "d3-geo-projection": "^1.2.1",
    "ndjson-cli": "^0.3.0",
    "shapefile": "^0.5.9",
    "topojson-server": "^2.0.0",
    "topojson-client": "^2.1.0",
    "topojson-simplify": "^2.0.0"    
  }
}

个人的D3.js学习日志 下载地址(Google云硬盘)open in new window 欢迎志同道合的同学下载阅读指正。

推荐书目D3.js in Actionopen in new window,这本书非常的棒结合D3.js的Sample一起使用效果更加!