D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
BenHeubl
Full window
Github gist
Coffee consumption vs. Diabetes
<!DOCTYPE html> <html> <meta charset="utf-8"> <!-- Example based on https://bl.ocks.org/mbostock/3887118 --> <!-- Tooltip example from https://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html --> <style> body { font: 12px futura; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .dot { stroke: none; } .tooltip { position: absolute; width: 200px; height: 20px; opacity: 0.3 font-size: 10px; font-family: futura; pointer-events: none; background: white; background-size: 15; background-stroke: 2px; } </style> <body> <script src="https://d3js.org/d3.v3.min.js"></script> <h1> Coffee Consumption (Kg per person per year) vs. <br>Diabetes Prevalence (% of Population) </br></h1> <h2> </h2> <script> var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 600 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; /* * value accessor - returns the value to encode for a given data object. * scale - maps value to a visual display encoding, such as a pixel position. * map function - maps from data value to display value * axis - sets up axis */ // setup x var xValue = function(d) { return d.diabetesPrevalence;}, // data -> value xScale = d3.scale.linear().range([0, width]), // value -> display xMap = function(d) { return xScale(xValue(d));}, // data -> display xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .tickFormat(d3.format("%")); // setup y var yValue = function(d) { return d["coffeeConsumption"];}, // data -> value yScale = d3.scale.linear().range([height, 0]), // value -> display yMap = function(d) { return yScale(yValue(d));}, // data -> display yAxis = d3.svg.axis().scale(yScale).orient("left"); // setup fill color var cValue = function(d) { return d.Country;}, color = d3.scale.category10(); // setup r // var dValue = function(d) { return d.GDP;}, // add the graph canvas to the body of the webpage 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 + ")"); // add the tooltip area to the webpage var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); // load data d3.csv("data2.csv", function(error, data) { // change string (from CSV) into number format data.forEach(function(d) { d.diabetesPrevalence = +d.diabetesPrevalence; d["coffeeConsumption"] = +d["coffeeConsumption"]; // console.log(d); }); // don't want dots overlapping axis, so add in buffer to data domain xScale.domain([d3.min(data, xValue), d3.max(data, xValue)]); yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]); // x-axis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("Diabetes Prevalence (% of Population)"); // y-axis svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Coffee Consumption (Kg per capita, per year"); // draw dots svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class", "dot") .attr("r", function(d) { return d.GDP * 10}) .attr("cx", xMap) .attr("cy", yMap) .style("fill", function(d) { return color(cValue(d));}) .on("mouseover", function(d) { tooltip.transition() .duration(20) .style("opacity", .9); tooltip.html(d["typeGDP"] + " ," + d["Country"]) .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 28) + "px"); }) .on("mouseout", function(d) { tooltip.transition() .duration(500) .style("opacity", 0); }); // draw legend // var legend = svg.selectAll(".legend") // .data(color.domain()) // .enter().append("g") // .attr("class", "legend") // .attr("transform", function(d, i) { return "translate(0," + i * 10 + ")"; }); // draw legend colored rectangles // legend.append("rect") // .attr("x", width ) // .attr("width", 10) // .attr("height", 10) // .style("fill", color); // draw legend text legend.append("text") .attr("x", width - 20) .attr("y", 4) .attr("dy", ".35em") .style("text-anchor", "rigth") .text(function(d) { return d;}) }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js