1 var width = 600; 2 var height = 400; 3
4 var svg = d3.select("#body") 5 .append("svg") 6 .attr("width",width) 7 .attr("height",height) 8
9 //打印d3.svg.symbolTypes數組裏面的符號
10 console.log(d3.svg.symbolTypes) //輸出["circle", "cross", "diamond", "square", "triangle-down", "triangle-up"]
1 var width = 600; 2 var height = 400; 3
4 var svg = d3.select("#body") 5 .append("svg") 6 .attr("width",width) 7 .attr("height",height) 8
9 //數組長度
10 var n = 30
11
12 //數組
13 var dataList = [] 14
15 //給數組添加元素
16 for (var i = 0 ; i < n ; i++){ 17 dataList.push({ 18 size : Math.random() * 30 + 500 , //符號的大小
19 type : d3.svg.symbolTypes[Math.floor( 20 Math.random() * d3.svg.symbolTypes.length //符號的類型
21 )] 22 }) 23 }
1 //建立一個符號生成器
2 var symbol = d3.svg.symbol() 3 .size(function(d){return d.size}) 4 .type(function(d){return d.type})
1 //定義顏色
2 var color = d3.scale.category20b(); 3
4 //添加路徑
5 svg.selectAll() 6 .data(dataList) 7 .enter() 8 .append("path") 9 .attr("d",function(d){return symbol(d)}) 10 .attr("transform",function(d,i){ 11 var x = 100 + i % 5 * 50; 12 var y = 100 + Math.floor(i/5) * 50;
13 return "translate("+ x + "," + y + ")"
14 }) 15 .attr("fill",function(d,i){ 16 return color(i) 17 })