D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
carlkra
Full window
Github gist
BarChart_Integer
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <script src= "https://cdnjs.com/libraries/d3-legend"></script> <link href='https://fonts.googleapis.com/css?family=Poiret+One' rel='stylesheet' type='text/css'> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } svg { width:100%; height:100%;} .bar {fill: steelblue;} .bar:hover {fill: red;} .axis path, .axis line { fill: black; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: none; } .my-text { font-size: 1em; font-family: 'Poiret One', cursive; } </style> </head> <body class="my-text"> <H1>Bar Chart - Visits Data</H1> <script> var margin = {top: 20, right: 20, bottom: 30, left:80 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); var y = d3.scale.linear() .range([height,0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(5); var svg = d3.select("body").append("svg") .attr("width",width+ margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("class","viewport") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); function render(data){ x.domain(data.map(function(d) { return d.period; })); y.domain([0, d3.max(data, function(d) { return d.visits; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("visits"); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.period); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.visits); }) .attr("height", function(d) { return height - y(d.visits); }); } function type(d){ d.visits = +d.visits; return d; } d3.csv("visits.csv", type, render); </script> </body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js