Skip to content

Instantly share code, notes, and snippets.

Created November 20, 2020 17:33
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
How to build a pictogram grid in D3.js. Demo with SVG circles.
<!DOCTYPE html>
<meta charset="utf-8">
<script src=""></script>
<div id='grid-container'>
<div id='grid-chart'></div>
var drawGraph = function(){
//number of circles to color in to visualize percent
var percentNumber = 92;
//variables for the font family, and some colors
var fontFamily = "helvetica";
var twitterFill = "#4D908E";
var twitterFillActive = "#adf7b6";
var svgBackgroundColor = '#264653';
//width and height of the SVG
const width = 500;
const height = 500;
//create an svg with width and height
var svg ='#grid-chart')
.attr("width", width)
.attr("height", height)
.style('background-color', svgBackgroundColor);
//10 rows and 10 columns
var numRows = 10;
var numCols = 10;
//x and y axis scales
var y = d3.scaleBand()
var x = d3.scaleBand()
.range([0, 250])
//the data is just an array of numbers for each cell in the grid
var data = d3.range(numCols*numRows);
//container to hold the grid
var container = svg.append("g")
.attr("transform", "translate(135,130)");
.attr("id", function(d){return "id"+d;})
.attr('cx', function(d){return x(d%numCols);})
.attr('cy', function(d){return y(Math.floor(d/numCols));})
.attr('r', 12)
.attr('fill', function(d){return d < percentNumber ? twitterFillActive : twitterFill;})
.style('stroke', 'black');
//call function to draw the graph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment