D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Xychen1994
Full window
Github gist
bar chart
Built with
blockbuilder.org
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>D3 Page Template</title> <script src="https://d3js.org/d3.v4.min.js"></script> </head> <body> <style> .axis path, .axis line{ fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> <script type="text/javascript"> // Your beautiful D3 code will go here var width = 1200; //画布的宽度 var height = 600; //画布的高度 var svg = d3.select("body") //选择文档中的body元素 .append("svg") //添加一个svg元素 .attr("width", width) //设定宽度 .attr("height", height); //设定高度 var padding = {left:45, right:40, top:25, bottom:20}; d3.csv("file1.csv",function(csvdata) { var dataset = [] var employee = [] var average = 0 for (var i = 0; i < csvdata.length - 3; i++) { dataset.push(csvdata[i].Number_of_people) employee.push(csvdata[i].Highest_level_completed) } average = (csvdata[csvdata.length - 3].Number_of_people) var xScale = d3.scaleBand() .domain(employee) .range([0, width - padding.left - padding.right]) .paddingInner(0) .paddingOuter(0); var yScale = d3.scaleLinear() .domain([0,d3.max(dataset)]) .range([height - padding.top - padding.bottom, 0]); //定义x轴 var xAxis = d3.axisBottom(xScale) //定义y轴 var yAxis = d3.axisLeft(yScale) var rectPadding = 4; var rects = svg.selectAll(".MyRect") .data(dataset) .enter() .append("rect") .attr("class","MyRect") .attr("transform","translate(" + padding.left + "," + padding.top + ")") .attr("x", function(d,i){ return xScale(employee[i]) + rectPadding/2; } ) .attr("y",function(d){ return yScale(d); }) .attr("fill","steelblue") .attr("width", xScale.bandwidth() - rectPadding ) .attr("height", function(d){ return height - padding.top - padding.bottom - yScale(d); }); var texts = svg.selectAll(".MyText") .data(dataset) .enter() .append("text") .attr("class","MyText") .style("fill","white") .attr("transform","translate(" + padding.left + "," + padding.top + ")") .attr("x", function(d,i){ return xScale(employee[i]) + rectPadding/2 - 5; } ) .attr("y",function(d){ var min = yScale.domain()[0]; return yScale(min); }) .transition() .delay(function(d,i){ return i * 200; }) .duration(2000) .attr("y",function(d){ return yScale(d); }) .attr("dx",function(){ return (xScale.bandwidth())/2-20; }) .attr("dy",function(d){ return 15; }) .text(function(d){ return d; }); svg.append("line") .attr("x1",padding.left) .attr("y1", padding.top + yScale(average)) .attr("x2",width - padding.right) .attr("y2", padding.top + yScale(average)) .attr("stroke-width",3) .attr("stroke","red"); svg.append("g") .attr("class","axis") .attr("transform","translate(" + padding.left + "," + (height - padding.bottom) + ")") .call(xAxis); //添加y轴 svg.append("g") .attr("class","axis") .attr("transform","translate(" + padding.left + "," + padding.top + ")") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("fill","black") .text("Number of people");; var legend = svg.selectAll(".legend") .data(average) .enter().append("g") .attr("class", "legend") .attr("transform","translate(0,20)" ) legend.append("rect") .attr("x", width - 28) .attr("width", 28) .attr("height", 8) .style("fill", 'red'); legend.append("text") .attr("x", width-34) .attr("y", 2) .attr("dy", ".35em") .style("text-anchor", "end") .text('average number of people'); }); </script> </body> </html>
https://d3js.org/d3.v4.min.js