戳這裏 鏡心的小樹屋javascript
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>d3 | bar chart</title> <link rel="stylesheet" href=""> <script src="https://d3js.org/d3.v3.min.js"></script> </head> <body> <div class="chart"></div> <script> var data = [4,8,15,16,23,42,12]; var x = d3.scale.linear() .domain([0, d3.max(data)]) .range([0, 420]); /* 下面代碼的運行結果是生成一個條狀圖,可是沒有對條狀圖的長度進行控制,下面採用scale.linear方法對數據長度進行設置。 */ d3.select(".chart") .selectAll("div") .data(data) //data方法用於對選中的結果集綁定數據。 .enter().append("div") //enter方法和append方法表示因爲此時div元素還不存在,必須根據數據的個數將它們創造出來。 .style({'width': function (data) {return x(data) + "px";} ,"background": "black","border": "2px solid #fff","color": "#fff"}) .text(function(data) {return data;}); //style方法和text方法的參數是函數,表示函數的運行結果就是設置網頁元素的值 /*reference*/ // http://www.danasilver.org/github-language-stats/ // http://javascript.ruanyifeng.com/library/d3.html </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>linechart</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <div id="container"></div> <script src="https://d3js.org/d3.v4.js"></script> <script src="js/index.js"></script> </body> </html>
var data = [1,6,4,3,3,8,5,4,3,7], bar_height = 50, bar_padding = 10, height = (bar_height + bar_padding)*data.length, width = 600; var scale = d3.scaleLinear() .domain([0,d3.max(data)]) .range([0,width]); var svg = d3.select("#container") .append("svg").attr("width",width).attr("height",height) var bar = svg.selectAll("g").data(data).enter().append("g").attr("transform",function(d,i){ return "translate(0,"+i*(bar_height+bar_padding)+")"; }) bar.append("rect").attr("width",function(d){return scale(d);}) .attr("height",bar_height ).style("fill","steelblue") bar.append("text").text(function(d){return d;}).attr("x",function(d){return scale(d+0.1);}).attr("y",bar_height/2)
d3.v3版本 : d3.scale.linear()
d3.v4版本 : d3.scaleLinear()
參考
https://bost.ocks.org/mike/
http://www.imooc.com/video/4743css
http://javascript.ruanyifeng....
http://www.danasilver.org/git...
D3.js柱狀圖例子html