Skip to content

Instantly share code, notes, and snippets.

@alansmithy
Last active May 31, 2017 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alansmithy/75da00c0030f360dc66dd661602e2ed6 to your computer and use it in GitHub Desktop.
Save alansmithy/75da00c0030f360dc66dd661602e2ed6 to your computer and use it in GitHub Desktop.
log bar
license: mit
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment