Built with blockbuilder.org
Last active
May 31, 2017 07:25
-
-
Save alansmithy/75da00c0030f360dc66dd661602e2ed6 to your computer and use it in GitHub Desktop.
log bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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