Most basic lollipop chart in d3.js

This post describes how to build a very basic lollipop chart with d3.js. Only one category is represented, to simplify the code as much as possible. You can see many other examples in the lollipop section of the gallery. Learn more about the theory of lollipop chart in

Lollipop section


  • The Html part of the code just creates a div that will be modified by d3 later on.

  • The first part of the javascript code set a svg area. It specify the chart size and its margin. Read more.

  • Data shows the amount of sold weapon per country. See data-to-viz if interested.

  • First step consists to make a numeric (Y axis) and a categoric scale (X axis). Read here to understand how scales and axis work.

  • Lollipop is then constituted of a line and a circle.
<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src=""></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>


// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 90, left: 40},
    width = 460 - margin.left - margin.right,
    height = 500 - - margin.bottom;

// append the svg object to the body of the page
var svg ="#my_dataviz")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + + margin.bottom)
          "translate(" + margin.left + "," + + ")");

// Parse the Data
d3.csv("", function(data) {

// X axis
var x = d3.scaleBand()
  .range([ 0, width ])
  .domain( { return d.Country; }))
  .attr("transform", "translate(0," + height + ")")
    .attr("transform", "translate(-10,0)rotate(-45)")
    .style("text-anchor", "end");

// Add Y axis
var y = d3.scaleLinear()
  .domain([0, 13000])
  .range([ height, 0]);

// Lines
    .attr("x1", function(d) { return x(d.Country); })
    .attr("x2", function(d) { return x(d.Country); })
    .attr("y1", function(d) { return y(d.Value); })
    .attr("y2", y(0))
    .attr("stroke", "grey")

// Circles
    .attr("cx", function(d) { return x(d.Country); })
    .attr("cy", function(d) { return y(d.Value); })
    .attr("r", "4")
    .style("fill", "#69b3a2")
    .attr("stroke", "black")


Related blocks →

  • The most basic barplot with d3.js - link

  • Lollipop chart - link