D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jtfallon
Full window
Github gist
Tables 2A and 2J
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> /* set the CSS */ .bar { fill: steelblue; } .axis text { font-family: sans-serif; font-size: 11px; } </style> <body> <!-- load the d3.js library --> <script src="//d3js.org/d3.v4.min.js"></script> <script> function a() { padding = -100; // set the dimensions and margins of the graph var margin = {top: 40, right: 20, bottom: 40, left: 70}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; // set the ranges var x = d3.scaleBand() .range([0, width]) .padding(0.1); var y = d3.scaleLinear() .range([height, 0]); // append the svg object to the body of the page // append a 'group' element to 'svg' // moves the 'group' element to the top left margin var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // get the data d3.csv("Table2A.csv", function(error, data) { if (error) throw error; // Scale the range of the data in the domains x.domain(data.map(function(d) { return d.HighestLevelCompleted; })); y.domain([0, d3.max(data, function(d) { return d.NumberOfPeople; })]); // append the rectangles for the bar chart svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.HighestLevelCompleted); }) .attr("width", x.bandwidth()) .attr("y", function(d) { return y(d.NumberOfPeople); }) .attr("height", function(d) { return height - y(d.NumberOfPeople); }); // add the x Axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // add the y Axis svg.append("g") .call(d3.axisLeft(y)); svg.append("text") .attr("text-anchor", "middle") // this makes it easy to centre the text as the transform is applied to the anchor .attr("transform", "translate("+ (padding/2) +","+(height/2)+")rotate(-90)") // text is drawn off the screen top left, move down and out and rotate .text("Number of people"); svg.append("text") .attr("text-anchor", "middle") // this makes it easy to centre the text as the transform is applied to the anchor .attr("transform", "translate("+ (width/2) +","+(height-(padding/3))+")") // centre below axis .text("Highest level completed"); svg.append("text") .attr("x", (width / 2)) .attr("y", 0 - (margin.top / 2)) .attr("text-anchor", "middle") .style("font-size", "16px") .style("text-decoration", "underline") .text("Highest Level of Education Completed vs. Number of People 2009"); }); } a(); function b() { var margin = {top: 100, right: 20, bottom: 110, left: 70}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scaleLinear().range([0, width]), y = d3.scaleLinear().range([height, 0]); var xAxis = d3.axisBottom(x), yAxis = d3.axisLeft(y); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom); svg.append("defs").append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); var focus = svg.append("g") .attr("class", "focus") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var cValue = function(d) { return d.HighestLevelCompleted;}, color = d3.scaleOrdinal(d3.schemeCategory10); d3.csv("Table2A.csv", function(error, data) { if (error) throw error; x.domain(d3.extent(data, function(d) { return d.MonthsWorked; })); y.domain([0, d3.max(data, function(d) { return d.TotalIncome$; })]); // append scatter plot to main chart area var dots = focus.append("g"); dots.attr("clip-path", "url(#clip)"); dots.selectAll("dot") .data(data) .enter().append("circle") .attr('class', 'dot') .attr("r",10) .style("opacity", .5) .style("fill", function(d) { return color(cValue(d));}) .attr("cx", function(d) { return x(d.MonthsWorked); }) .attr("cy", function(d) { return y(d.TotalIncome$); }) focus.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(xAxis); focus.append("g") .attr("class", "axis axis--y") .call(yAxis); focus.append("text") .attr("transform", "rotate(-90)") .attr("y", 0 - margin.left) .attr("x",0 - (height / 2)) .attr("dy", "1em") .style("text-anchor", "middle") .text("Total Income ($)"); svg.append("text") .attr("transform", "translate(" + ((width + margin.right + margin.left)/2) + " ," + (height + margin.top + 50) + ")") .style("text-anchor", "middle") .text("Months Worked"); svg.append("text") .attr("x", ((width+100) / 2)) .attr("y", 120 - (margin.top / 2)) .attr("text-anchor", "middle") .style("font-size", "16px") .style("text-decoration", "underline") .text("Months Worked vs. Total Income ($) 2009"); // draw legend var legend = svg.selectAll(".legend") .data(color.domain()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(-70," + i * 20 + ")"; }); // draw legend colored rectangles legend.append("rect") .attr("x", width - 18) .attr("width", 18) .attr("height", 18) .style("fill", color); // draw legend text legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d;}) }) } b(); </script> </body>
https://d3js.org/d3.v4.min.js