D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
devdiva8
Full window
Github gist
test 2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bar Chart</title> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script> <style type="text/css"> body { background-color: pink; font-family: Helvetica, Arial, sans-serif; font-size: 8px; } svg { background-color: white; } rect { shape-rendering: crispEdges; } p {margin:15px;} .headline{ font-size: 12px; color:#3B444B; // #d95f0e; } .axis path, .axis line { fill:none; stroke:gray; stroke-width: 0.5px; shape-rendering: crispEdges;; } .axis text { font-family: Arial, sans-serif; font-size: 8px; } .y.axis path { display: none; } </style> </head> <body> <script type="text/javascript"> d3.select("body") .append("p") .text("Data by Task") .attr("class", "headline") ; var w = 700; var h = 400; var padding = [ 5, 10, 80, 100]; //Top, right, bottom, left var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeBands([ padding[0], h - padding[2] ]); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("Food.csv", function(data) { data.sort(function(a, b) { return d3.descending(a.ScoreReversedHighGood, b.ScoreReversedHighGood); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.ScoreReversedHighGood; }) ]); heightScale.domain(data.map(function(d) { return d.Country; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.Country); }) .attr("width", function(d) { return widthScale(d.ScoreReversedHighGood); }) .attr("height", .7*heightScale.rangeBand()) .attr("fill", "PaleGoldenRod") .attr("stroke", "white") .attr("stroke-width", 1) .append("title") .text(function(d) { return d.Country + "'s Index score is " + d.ScoreReversedHighGood; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis) ; svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + 100 + ",0)") .call(yAxis) ; }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js