css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div id="container"></div>

<script src="https://d3js.org/d3.v4.min.js"></script>
                text {
   font-size: 12px;

  .title {
    font-weight: bold;

  .grid-line {
    stroke: black;
    opacity: 0.2;

  .lollipop-line {
    stroke: black;
    stroke-width: 1px;

  circle {
    stroke-width: 1px;
    stroke: black;

  circle.lollipop-start {
    fill: #D770AD;

  .lollipop-end {
    fill: #4A89DC;

  .lollipop-median {
    fill: white;

  div.tooltip {
    position: absolute;
    text-align: center;
    color: white;
    padding: 8px;
    font: 12px sans-serif;
    border-radius: 6px;
    pointer-events: none;

              //filter the data for just the rows (occupations) we want
function filterData(data){
	const selectedFields = ["Driver/sales workers and truck drivers", "Elementary and middle school teachers", "Registered nurses", "Retail salespersons", "Accountants and auditors", "Janitors and building cleaners", "Sales representatives, wholesale and manufacturing", "Construction laborers", "Cashiers", "Customer service representatives"]
	return data.filter((fieldName)=>{
     		return selectedFields.includes(fieldName[0])

//transform the data into an array of objects so we can pass it to the draw function. We'll also sort the data.
function mapData(result){
  const formattedData = [];
    let mappedData = {}
    mappedData["name"] = val[0]
    mappedData["max"] = val[11]
    mappedData["min"] = val[13]
  return formattedData.sort(function(a, b) {
    if(parseInt(a["max"]) < parseInt(b["max"])) return 1 * 1;
    if(parseInt(a["max"]) > parseInt(b["max"])) return -1 * 1;
    return 0;

//draw the visualization
function drawSVG(data) {

//make margins and svg; append svg to container
  let margin = {top: 90, right: 15, bottom: 50, left: 350},
  width = 960 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;

  let svg = d3.select("#container").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//construct the scales and bands of the dots
  let y = d3.scaleBand()
    .range([0, height])

  let x = d3.scaleLinear()
    .range([0, width]);

  let lineGenerator = d3.line();

  let axisLinePath = function(d) {
    return lineGenerator([[x(d) + 0.5, 0], [x(d) + 0.5, height]]);

  let lollipopLinePath = function(d) {
    return lineGenerator([[x(d.min), 0], [x(d.max), 0]]);

//set the ranges for the chart
  y.domain(data.map(function(d) { return d.name }));
  x.domain([0, d3.max(data, function(d) { return d.max })]);

//make the axes
  let yAxis = d3.axisLeft().scale(y)

  let xAxis = d3.axisTop().scale(x)
    .tickFormat(function(d,i) {
    if (i == 0) {
      return "$0"
    } else {
      return d3.format(".2s")(d);

  let yAxisGroup = svg.append("g")
    .attr("class", "y-axis-group");

    .attr("class", "y-axis")
    .attr("transform", "translate(-20, 0)")
    .attr("opacity", 0);

 let xAxisGroup = svg.append("g")
    .attr("class", "x-axis-group");

    .attr("class", "x-axis")

  let axisLines = svg.append("g")
    .attr("class", "grid-lines");

    .attr("class", "grid-line")
    .attr("d", axisLinePath);

//make the lollipops (circles)
  let lollipopsGroup = svg.append("g").attr("class", "lollipops");

  let lollipops = lollipopsGroup.selectAll("g")
    .attr("class", "lollipop")
    .attr("transform", function(d) {
    return "translate(0," + (y(d.name) + (y.bandwidth() / 2)) + ")";

    .attr("class", "lollipop-line")
    .attr("d", lollipopLinePath);

  let startCircles = lollipops.append("circle")
    .attr("class", "lollipop-start")
    .attr("r", 5)
    .attr("cx", function(d) {
    return x(d.min);
    .on("mouseover", showLabel)
    .on("mousemove", moveLabel)
    .on("mouseout", hideLabel);

  let endCircles = lollipops.append("circle")
    .attr("class", "lollipop-end")
    .attr("r", 5)
    .attr("cx", function(d) {
    return x(d.max);
    .on("mouseover", showLabel)
    .on("mousemove", moveLabel)
    .on("mouseout", hideLabel);

//make the legend
  let legendLabels = [
    {label: "Women", class: "lollipop-start"},
    {label: "Men", class: "lollipop-end"}

  let legendX = 300,
    legendY = -50,
    spaceBetween = 70,
    titleOffset = -150;

// position legend
  let legend = svg.append("g")
    .attr("transform", "translate(" + [legendX, legendY] + ")");

    .attr("class", "title")
    .attr("x", titleOffset)
    .text("Median Earnings ($)")

  // add circles to legend
    .attr("cx", function(d, i) {
    return spaceBetween * i;
    .attr("cy", -4)
    .attr("r", 5)
    .attr("class", function(d) { return d.class   });

  // add labels to legend
    .attr("x", function(d, i) {
    return spaceBetween * i + 10;
    .text(function(d) { return d.label });

//make tooltips
  let div = d3.select("body").append("div")
  .attr("class", "tooltip")
  .style("opacity", 0);

  let classToPos = {
    "lollipop-start": "min",
    "lollipop-end": "max"

//define tooltip functions
  function showLabel() {
    let selection = d3.select(this);
    let pos = classToPos[selection.attr("class")];
    let data = d3.select(this.parentNode).datum();

      .style("opacity", 1)
    div.html("$" + (selection.datum()[pos]))
      .style("left", (d3.event.pageX - 30) + "px")
      .style("top", (d3.event.pageY - 40) + "px")
      .style("background-color", "rgba(0, 0, 0, 0.75)")

  function moveLabel() {
    div.style("left", (d3.event.pageX - 30) + "px")
      .style("top", (d3.event.pageY - 40) + "px")

  function hideLabel() {
      .style("opacity", 0);

//here's where we fetch the data and call the functions we define above
  .then((resp) => resp.json()) 
  .then((data) => {
    return data.table_rows.rows;
  .catch((error) => console.log(error));

// This chart is based on Cale Tilford’s Connected Dot Plot, found here: https://bl.ocks.org/tlfrd/e1ddc3d0289215224a69405f5e538f51. 
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................