Skip to content

Instantly share code, notes, and snippets.

@mauroSolis
Created March 28, 2013 04:26
Show Gist options
  • Save mauroSolis/5260608 to your computer and use it in GitHub Desktop.
Save mauroSolis/5260608 to your computer and use it in GitHub Desktop.
Practice of “Attribute Explorer” with html/javascript and d3.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<style type="text/css">
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<script type="text/javascript">
//Width and height
var w = 1000;
var h = 300;
var padding = 60;
var baseCuadrado = 225;
//Static dataset
var dataset = [
{x: 15, bar: 2}, {x: 16, bar: 3},
{x: 17, bar: 8}, {x: 18, bar: 12},
{x: 19, bar: 10}, {x: 20, bar: 8},
{x: 21, bar: 6}, {x: 22, bar: 7},
{x: 23, bar: 8}, {x: 24, bar: 5},
{x: 25, bar: 6}, {x: 26, bar: 2},
{x: 27, bar: 0}, {x: 28, bar: 2},
{x: 29, bar: 6}, {x: 30, bar: 1},
{x: 31, bar: 2}, {x: 32, bar: 1},
{x: 33, bar: 0}, {x: 34, bar: 0},
{x: 35, bar: 0}, {x: 36, bar: 0},
{x: 37, bar: 0}, {x: 38, bar: 1},
{x: 39, bar: 0}, {x: 40, bar: 0},
{x: 41, bar: 1}, {x: 42, bar: 0},
{x: 43, bar: 0}, {x: 44, bar: 0},
{x: 45, bar: 0}, {x: 46, bar: 1},
];
var xDomain = d3.extent(dataset, function(i) { return i.x; });
var barDomain = d3.extent(dataset, function(i) { return i.bar; });
//Create scale functions
var xScale = d3.scale.linear()
.domain(xDomain)
.range([padding, w - padding * 2]);
var barScale = d3.scale.linear()
.domain(barDomain)
.range([padding, w - padding * 2]);
/*var yScale = d3.scale.linear()
.domain([0, maxY])
.range([h - padding, padding]);*/
//Define X axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(30);
//Define Y axis
/*var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);*/
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create circles
var repeticion = 0;
svg.selectAll(".bar")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d) {return xScale(d.x);})
.attr("y", baseCuadrado)
.attr("width", 10)
.attr("height", 15)
.attr("fill", "white")
.attr("stroke", "black");
//Create circles
/*svg.selectAll(".y2")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d.x);
})
.attr("cy", function(d) {
return yScale(d.y2);
})
.attr("class", "y2")
.attr("r", 2);*/
//Create X axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
//Create Y axis
/*svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);*/
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment