單元課程5:D3.js資料圖形化設計應用
(參考資料:http://d3js.org /
https://www.oxxostudio.tw/articles/201410/svg-d3-js.html /
https://ithelp.ithome.com.tw/articles/10158546)
(參考資料:https://abgne.tw/d3/d3-getting-stared/d3-draw-svg-shape-text.html
/
https://bl.ocks.org/mbostock/1062544
)
1.資料視覺化的設計,有若干方法,其中經由javascript D3.js的外掛資料庫引入設計,是很重要的方法之一。
這種方法主要是結合 HTML+javascript的互動設計,然後引入 D3.js的相關應用程式介面(API)指令,以處理
文字資料轉為圖形化的程序。
(1)D3.js官方網站:http://d3js.org/
(2)首先引入 d3.js的外掛資料庫(目前有 v3 和 v5版本)
<script src="http://d3js.org/d3.v3.min.js"></script>
(2)在javascript程式內,使用d3.js語法,將之前的 SVG 語法,引入程式之中。
如:
d3.select('body')
.append('svg')
.attr({
'width':200,
'height':200
});
【說明】d3.js以物件屬性與方法的模式,撰寫程式。單元課程4:HTML5 SVG繪圖基本設計應用 課程中
A.HTML svg 的標籤為 <svg width="寬度" height="高度"> ...</svg>
B.以 d3.js的語法則是
d3.select('body') => 將會找到 HTML 中 <body>的標籤
d3.select('body')
.append('svg') => 將會在<body>標籤內,加入 <svg> 標籤
d3.select('body')
.append('svg')
.attr({
'width':200,
'height': 200
}); => 將會在<body>標籤內,的<svg>標籤內加入 width="寬度" 和 height="高度"
C.如此便是以 d3.js 程式建立一個 HTML SVG 圖形區,以便後續圖形呈現之用。
2.javascript + d3.js 程式化圓形設計
(1) HTML網頁程式參考
|
HTML程式碼 |
執行結果 |
|
<!DOCTYPE html>
<script src="http://d3js.org/d3.v3.min.js"></script> |
![]() |
(2)程式設計說明
A.引入 d3.js <script src="http://d3js.org/d3.v3.min.js"></script>
B.在<body>...</body>內的<script>...</script>
* 建立 SVG圖形區:
d3.select('body')
.append('svg')
.attr({
'width':200,
'height':200
});
* 繪製圓形
d3.select('svg')
.append('circle')
.attr({
'cx':50,
'cy':50,
'r':30,
'fill':'yellow',
'stroke':'blue',
'stroke-width':'5px'
});
3.javascript + d3.js 程式化線段設計
(1)HTML 網頁程式參考
|
HTML程式碼 |
執行結果 |
|
<!DOCTYPE html>
|
![]() |
(2)程式設計說明
A.引入 d3.js <script src="http://d3js.org/d3.v3.min.js"></script>
B.在<body>...</body>內的<script>...</script>
* 建立 data[]陣列資料來源:
var data = [
{x:10,y:10},
{x:50,y:100},
{x:60,y:50},
{x:100,y:30}
];
* 建立 line()函數:
var line = d3.svg.line()
.x(function(d) {
return d.x;
})
.y(function(d) {
return d.y;
});
* 以SVG path 方式建立線段圖形:
svg.append('path')
.attr({
'd': line(data),
'y': 0,
'stroke': '#000',
'stroke-width': '5px',
'fill': 'none'
});
3.javascript + d3.js 程式化長條圖設計
(1)HTML 網頁程式參考
|
HTML程式碼 |
執行結果 |
|
<!DOCTYPE html> |
![]() |
(2)程式設計說明
A.引入 d3.js <script src="http://d3js.org/d3.v3.min.js"></script>
B.在<body>...</body>內的<script>...</script>
* 建立 dataset[]陣列資料來源:
var dataset = [30,26,27,31,13,10,20,20,24,25,25,21,17,21,6,7,13,23,27,31];
* 建立 bar 的CSS格式:
.bar {
display:
inline-block;
width: 20px;
height: 75px;
margin-right:
3px;
background-color: RoyalBlue;
}
* 以d3.select().data().enter().append().attr().style() 建立長條圖
d3.select('.demo').selectAll('div') //選取 class內的div
.data(dataset) // 將資料加入至 div
.enter() // return data
.append('div') // 這邊加入的div已經有包含data
.attr('class','bar') //套用class
.style('height', function(d){
//將data的值取出作為高
return (d*3) + 'px'
})
4.SVG路徑圖形與文字結合應用
(1)HTML網頁程式參考
|
HTML程式碼 |
執行結果 |
|
<!doctype html> <html> <head> <script src="https://d3js.org/d3.v3.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </head> <body> <h1>D3js+path+pathtext Demo</h1> <script> $(function(){ // 在 body 中插入一個 svg var svg = d3.select('body').append('svg').attr({'width':500,'height': 500}); // 先在 svg 中插入一個 path svg.append('path').attr({ id: 'mypath', d: 'M50 100Q350 50 350 250Q250 50 50 250' }).style({ fill: 'none', stroke: 'green', 'stroke-width': 10 }); // 接著在 svg 中插入一個 text // 並在 text 中插入一個 textPath svg.append('text').attr({ x: 10, y: 20 }).style({ fill: 'red', 'font-size': '42px' }).append('textPath').attr({ 'xlink:href': '#mypath' }).text('國立台中教育大學數位系'); }); </script> </body> </html> |
|
(2)程式說明
A.引入jquery與d3.js
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
B.建立 SVG的大小,會影響後續圖文的出現效果
var svg = d3.select('body').append('svg').attr({'width':500,'height':
500});
5.javascript + d3.js 程式化資料導向的圓形圖設計
(1)HTML網頁程式參考
|
HTML程式碼 |
執行結果 |
|
<!doctype html> |
![]() |
(2)程式設計說明
A.d3.max(),d3.min():取得資料的最大值、最小值。
B.d3.hsl():依參數取得顏色值。