D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
TollyCoburn
Full window
Github gist
Scatter
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> <script> //Width and height var w = 700; var h = 400; var padding = 30; //Dynamic, random dataset var dataset = []; //Initialize empty array var numDataPoints = 50; //Number of dummy data points to create var xRange = Math.random() * 1000; //Max range of new x values var yRange = Math.random() * 1000; //Max range of new y values for (var i = 0; i < numDataPoints; i++) { //Loop numDataPoints times var newNumber1 = Math.floor(Math.random() * xRange); //New random integer var newNumber2 = Math.floor(Math.random() * yRange); //New random integer dataset.push([newNumber1, newNumber2]); //Add new number to array } //Create scale functions var xScale = d3.scaleLinear() .domain([0, d3.max(dataset, function(d) { return d[0]; })]) .range([padding, w - padding * 2]); var yScale = d3.scaleLinear() .domain([0, d3.max(dataset, function(d) { return d[1]; })]) .range([h - padding, padding]); var aScale = d3.scaleSqrt() .domain([0, d3.max(dataset, function(d) { return d[1]; })]) .range([0, 10]); //Define X axis var xAxis = d3.axisBottom(xScale) .ticks(5); //Define Y axis var yAxis = d3.axisLeft(yScale) .ticks(5) //Create SVG element var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Create circles svg.selectAll("circle") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScale(d[0]); }) .attr("cy", function(d) { return yScale(d[1]); }) .attr("r", function(d){ return Math.sqrt(h-d[1]); }) .attr("r", function(d){ return aScale(d[1]); }); // Create Text Element //svg.selectAll("text") // .data(dataset) // .enter() // .append("text") // .text(function(d){ // return d[0] + "," + d[1]; // }) // .attr("x", function(d){ // return xScale(d[0]); // }) // .attr("y", function(d){ // return yScale(d[1]); // }) // .attr("font-family", "sans-serif") // .attr("font-size", "11px") // .attr("fill", "red"); //Create X axis svg.append("g") .attr("class", "axis") .attr("transform", "translate(0,"+(h-padding)+")") .call(xAxis); //Create Y axis svg.append("g") .attr("class", "axis") .attr("transform", "translate(" + padding + ",0)") .call(yAxis); </script> </body>
https://d3js.org/d3.v4.min.js