Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created May 26, 2011 19:47
Show Gist options
  • Select an option

  • Save mbostock/993912 to your computer and use it in GitHub Desktop.

Select an option

Save mbostock/993912 to your computer and use it in GitHub Desktop.

Revisions

  1. mbostock revised this gist Jul 4, 2012. 2 changed files with 4 additions and 67 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    This example has moved to <http://bl.ocks.org/3048450>.
    70 changes: 3 additions & 67 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -1,68 +1,4 @@
    <!DOCTYPE html>
    <script src="http://mbostock.github.com/d3/d3.js?2.7.1"></script>
    <script src="http://mbostock.github.com/d3/d3.layout.js?2.7.1"></script>
    <style>

    body {
    font: 10px sans-serif;
    }

    rect {
    fill: steelblue;
    stroke: white;
    }

    line {
    stroke: black;
    shape-rendering: crispEdges;
    }

    </style>
    <body>
    <script>

    var n = 10000, // number of trials
    m = 10, // number of random variables
    data = [];

    // Generate an Irwin-Hall distribution.
    for (var i = 0; i < n; i++) {
    for (var s = 0, j = 0; j < m; j++) {
    s += Math.random();
    }
    data.push(s);
    }

    var histogram = d3.layout.histogram()
    (data);

    var width = 960,
    height = 500;

    var x = d3.scale.ordinal()
    .domain(histogram.map(function(d) { return d.x; }))
    .rangeRoundBands([0, width]);

    var y = d3.scale.linear()
    .domain([0, d3.max(histogram.map(function(d) { return d.y; }))])
    .range([0, height]);

    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

    svg.selectAll("rect")
    .data(histogram)
    .enter().append("rect")
    .attr("width", x.rangeBand())
    .attr("x", function(d) { return x(d.x); })
    .attr("y", function(d) { return height - y(d.y); })
    .attr("height", function(d) { return y(d.y); });

    svg.append("line")
    .attr("x1", 0)
    .attr("x2", width)
    .attr("y1", height)
    .attr("y2", height);

    </script>
    <script>
    top.location = "http://bl.ocks.org/3048450";
    </script>
  2. mbostock revised this gist Jan 12, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -61,8 +61,8 @@

    svg.append("line")
    .attr("x1", 0)
    .attr("x2", w)
    .attr("y1", h)
    .attr("y2", h);
    .attr("x2", width)
    .attr("y1", height)
    .attr("y2", height);

    </script>
    </script>
  3. mbostock revised this gist Jan 9, 2012. 1 changed file with 22 additions and 34 deletions.
    56 changes: 22 additions & 34 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,7 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Histogram</title>
    <script type="text/javascript" src="https://github.com/mbostock/d3/raw/v1.17.0/d3.js"></script>
    <script type="text/javascript" src="https://github.com/mbostock/d3/raw/v1.17.0/d3.layout.js"></script>
    <style type="text/css">
    <script src="http://mbostock.github.com/d3/d3.js?2.7.1"></script>
    <script src="http://mbostock.github.com/d3/d3.layout.js?2.7.1"></script>
    <style>

    body {
    font: 10px sans-serif;
    @@ -20,10 +17,9 @@
    shape-rendering: crispEdges;
    }

    </style>
    </head>
    <body>
    <script type="text/javascript">
    </style>
    <body>
    <script>

    var n = 10000, // number of trials
    m = 10, // number of random variables
    @@ -36,45 +32,37 @@
    }
    data.push(s);
    }

    var w = 400,
    h = 400;


    var histogram = d3.layout.histogram()
    (data);

    var width = 960,
    height = 500;

    var x = d3.scale.ordinal()
    .domain(histogram.map(function(d) { return d.x; }))
    .rangeRoundBands([0, w]);
    .rangeRoundBands([0, width]);

    var y = d3.scale.linear()
    .domain([0, d3.max(histogram, function(d) { return d.y; })])
    .range([0, h]);
    .domain([0, d3.max(histogram.map(function(d) { return d.y; }))])
    .range([0, height]);

    var vis = d3.select("body").append("svg:svg")
    .attr("width", w)
    .attr("height", h)
    .append("svg:g")
    .attr("transform", "translate(.5)");
    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

    vis.selectAll("rect")
    svg.selectAll("rect")
    .data(histogram)
    .enter().append("svg:rect")
    .attr("transform", function(d) { return "translate(" + x(d.x) + "," + (h - y(d.y)) + ")"; })
    .enter().append("rect")
    .attr("width", x.rangeBand())
    .attr("y", function(d) { return y(d.y); })
    .attr("height", 0)
    .transition()
    .duration(750)
    .attr("y", 0)
    .attr("x", function(d) { return x(d.x); })
    .attr("y", function(d) { return height - y(d.y); })
    .attr("height", function(d) { return y(d.y); });

    vis.append("svg:line")
    svg.append("line")
    .attr("x1", 0)
    .attr("x2", w)
    .attr("y1", h)
    .attr("y2", h);

    </script>
    </body>
    </html>
    </script>
  4. mbostock created this gist May 26, 2011.
    80 changes: 80 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Histogram</title>
    <script type="text/javascript" src="https://github.com/mbostock/d3/raw/v1.17.0/d3.js"></script>
    <script type="text/javascript" src="https://github.com/mbostock/d3/raw/v1.17.0/d3.layout.js"></script>
    <style type="text/css">

    body {
    font: 10px sans-serif;
    }

    rect {
    fill: steelblue;
    stroke: white;
    }

    line {
    stroke: black;
    shape-rendering: crispEdges;
    }

    </style>
    </head>
    <body>
    <script type="text/javascript">

    var n = 10000, // number of trials
    m = 10, // number of random variables
    data = [];

    // Generate an Irwin-Hall distribution.
    for (var i = 0; i < n; i++) {
    for (var s = 0, j = 0; j < m; j++) {
    s += Math.random();
    }
    data.push(s);
    }

    var w = 400,
    h = 400;

    var histogram = d3.layout.histogram()
    (data);

    var x = d3.scale.ordinal()
    .domain(histogram.map(function(d) { return d.x; }))
    .rangeRoundBands([0, w]);

    var y = d3.scale.linear()
    .domain([0, d3.max(histogram, function(d) { return d.y; })])
    .range([0, h]);

    var vis = d3.select("body").append("svg:svg")
    .attr("width", w)
    .attr("height", h)
    .append("svg:g")
    .attr("transform", "translate(.5)");

    vis.selectAll("rect")
    .data(histogram)
    .enter().append("svg:rect")
    .attr("transform", function(d) { return "translate(" + x(d.x) + "," + (h - y(d.y)) + ")"; })
    .attr("width", x.rangeBand())
    .attr("y", function(d) { return y(d.y); })
    .attr("height", 0)
    .transition()
    .duration(750)
    .attr("y", 0)
    .attr("height", function(d) { return y(d.y); });

    vis.append("svg:line")
    .attr("x1", 0)
    .attr("x2", w)
    .attr("y1", h)
    .attr("y2", h);

    </script>
    </body>
    </html>