D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
alansmithy
Full window
Github gist
log bar
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> // Feel free to change or delete any of the code you see in this editor! var margin = {left:100,right:20,top:50,bottom:20} var svg = d3.select("body").append("svg") .attr("width", 580) .attr("height", 500); var mydata = [ {name:"a",value:100}, {name:"b",value:50}, {name:"c",value:10}, {name:"d",value:5}, {name:"e",value:0.8}, ]; var names = d3.map(mydata,function(d){ return d.name; }) var xScale = d3.scaleLog() .domain([0.1,100]) .range([0,580-(margin.left+margin.right)]); var yScale = d3.scaleOrdinal() .domain(names) .range([0,500-(margin.top+margin.bottom)]) svg.append("g") .selectAll("rect") .data(mydata) .enter() .append("rect") .attr("y",function(d,i){ return margin.top+(i * 20); }) .attr("width",function(d){return xScale(d.value)}) .attr("height",17) .attr("x",margin.left) .attr("fill","black") svg.append("g") .selectAll("text") .data(mydata) .enter() .append("text") .attr("y",function(d,i){ return margin.top+(i * 20)+10; }) .attr("text-anchor","end") .attr("x",margin.left-4) .text(function(d){return d.name}) var xAxis = d3.axisBottom() .scale(xScale) .ticks(20, ",.1s"); svg.append("g").attr("transform","translate("+margin.left+","+(margin.top+100)+")") .call(xAxis) </script> </body>
https://d3js.org/d3.v4.min.js