Skip to content

Instantly share code, notes, and snippets.



Created May 8, 2019
What would you like to do?
D3 Scatterplot without data join
license: mit

D3 Scatterplot Without Data Join

A somewhat convoluted demo of creating a simple scatterplot using ES6 template literals. The common-tags package is used for its handy html tag function that strips out line breaks and white space from the template literal string.

The point of this example is just to show you don't need to use d3-selection's methods to render SVG shapes from data.

Built with

<!DOCTYPE html>
<meta charset="utf-8">
<script src=""></script>
<script src=""></script>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
var xmlns = "";
var margin = { top: 20, left: 50, right: 10, bottom: 50 };
var width = 500;
var height = 500;
var body = document.querySelector("body");
// NOTE: creating an SVG element using vanilla JS requires document.createElementNS
var svg = body.appendChild(document.createElementNS(xmlns,"svg"))
svg.setAttribute("width", 500);
svg.setAttribute("height", 500);
var g = svg.appendChild(document.createElementNS(xmlns, "g"));
g.setAttribute("transform", `translate(${margin.left}, ${})`);
var xScale = d3.scaleLinear()
.range([0, width - margin.left - margin.right]);
var yScale = d3.scaleLinear()
.range([height - - margin.bottom, 0]);
var cScale = d3.scaleOrdinal(d3.schemeCategory10);
function xAxis(g) {
g.attr("transform", `translate(0, ${height - margin.bottom -})`)
function yAxis(g) {;
function parseRow(d) {
return {
sepal_length: +d.sepal_length,
sepal_width: +d.sepal_width,
petal_length: +d.petal_length,
petal_width: +d.petal_width,
species: d.species
function renderCircle(d) {
return commonTags.html`
function main(data) {
xScale.domain(d3.extent(data, d => d.sepal_width));
yScale.domain(d3.extent(data, d => d.sepal_length));
g.appendChild(, "g"))
g.appendChild(, "g"))
g.appendChild(document.createElementNS(xmlns, "g"))
.innerHTML = commonTags.html`${}`;
d3.csv("", parseRow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.