D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
alandunning
Full window
Github gist
Custom Positioned Scatter V4
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <div id="scatter"></div> <script> var margin = {top: 20, right: 20, bottom: 30, left: 50}; var width = 960 - margin.left - margin.right; var height = 600 - margin.top - margin.bottom; var domainwidth = width - margin.left - margin.right; var domainheight = height - margin.top - margin.bottom; var svg = d3.select("#scatter").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .call(responsivefy) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var radius = d3.scaleLinear() .domain([0, 280]) .range([10, 100]); var color = d3.scaleOrdinal(["#FA8334", "#AFFC41", "#19647E", "#7FDDDD", "#949396", "#DCF763", "#00C6D0", "#C1C1C1", "#666666", "#FDCDAE"]); var x = d3.scaleLinear() .range(padExtent([0, domainwidth])); var y = d3.scaleLinear() .range(padExtent([domainheight, 0])); d3.json("data.json", function(error, data) { console.log(data) data.sort(function(x, y){ return d3.ascending(x.total, y.total); }) console.log(data) if (error) throw error; data.forEach(function(d) { d.consequence = +d.consequence; d.value = +d.value; d.total = +d.total; }); x.domain(padExtent([0, 100])); y.domain(padExtent([0, 100])); svg.selectAll("circle") .data(data) .enter() .append("circle") .attr("class", "dot") .style('fill', function (d, i) { return color(d.industry); }) .attr("r", function(d) { return radius(d.total); }) .attr("cx", function(d) { return x(d.xPos); }) .attr("cy", function(d) { return y(d.yPos); }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + y.range()[0] + ")") .call(d3.axisBottom(x).ticks(5)); svg.append("g") .attr("class", "y axis") .call(d3.axisLeft(y).ticks(5)); }); function padExtent(e, p) { if (p === undefined) p = 1; return ([e[0] - p, e[1] + p]); } function responsivefy(svg) { var container = d3.select(svg.node().parentNode); var width = parseInt(svg.style("width")); var height = parseInt(svg.style("height")); var aspect = width / height; svg.attr("viewBox", "0 0 " + width + " " + height) .attr("perserveAspectRatio", "xMinYMid") .call(resize); function resize() { var targetWidth = parseInt(container.style("width")); svg.attr("width", targetWidth); svg.attr("height", Math.round(targetWidth / aspect)); } d3.select(window).on("resize." + container.attr("id"), resize); } </script> </body>
https://d3js.org/d3.v4.min.js