D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jtr13
Full window
Github gist
d3.json
Built with
blockbuilder.org
forked from
jtr13
's block:
d3.csv
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v5.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> const w = 525; const h = 400; const svg = d3.select("body").append("svg").attr("width", w).attr("height", h); const xScale = d3.scaleLinear().domain([0,1]).range([0,w-60]); const yScale = d3.scaleLinear().domain([0,1]).range([h,0]); const colors = d3.scaleOrdinal(d3.schemeTableau10); d3.json("https://data.ct.gov/resource/y6p2-px98.json?category=Fruit").then(function (data) { console.log(data); xScale.domain([d3.min(data.map(d => d.location_1.coordinates[0])), d3.max(data.map(d => d.location_1.coordinates[0]))]); yScale.domain([d3.min(data.map(d => d.location_1.coordinates[1])), d3.max(data.map(d => d.location_1.coordinates[1]))]); svg.selectAll("circle") .data(data) .enter() .append("circle") .attr("cx", d => xScale(d.location_1.coordinates[0])) .attr("cy", d => yScale(d.location_1.coordinates[1])) .attr("r", "3") .attr("fill", d => colors(d.item)) .on("mouseover", function(d) { // IDVW2, p. 270 var xPos = +d3.select(this).attr("cx"); var yPos = +d3.select(this).attr("cy"); d3.select(this).style("cursor", "pointer"); svg.append("text") .attr("id", "tooltip") .attr("x", function() { if(xPos < w/2) { return xPos + 10 } else { return xPos - 10 } }) .attr("y", yPos) .attr("font-size", "11px") .attr("text-anchor", function() { if(xPos < w/2) { return "start" } else { return "end" } }) .attr("alignment-baseline", "middle") .text(d.item); // not a function (already in one) }) .on("mouseout", function() { d3.select("#tooltip").remove(); }); }); </script> </body>
https://d3js.org/d3.v5.min.js