Skip to content
Advertisement

Donut chart d3.js labels

I’m new to d3.js and I’m trying to change this code. What I actually need is to have each slice’s name on it. The problem is that labels need to change for each button. For example, if you click on culture/Media the labels are – German, English, History but for medicine are – Dentist, Pharmacist…

donut chart

any help is highly appreciated 🙂

var transitionSpeed = 2000;

var hareReturn = "";
var mixReturn = "";
var tortoiseReturn = "";

var currentData = [];
var dataset = [{
    "label": "Mathematics",
    "count": 16500
  },
  {
    "label": "Computer Science",
    "count": 58000
  },
  {
    "label": "Physics",
    "count": 35000
  },
  {
    "label": "Chemistry",
    "count": 12700
  },
  {
    "label": "Biology",
    "count": 13500
  },
];





var tortoiseData = [{
    "label": "English / American Studies",
    "count": 5800
  },
  {
    "label": "History",
    "count": 7900
  },
  {
    "label": "German",
    "count": 11000
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  }
];

var mixData = [{
    "label": "Electro technology",
    "count": 17500
  },
  {
    "label": "Civil Engineering",
    "count": 13700
  },
  {
    "label": "Architecture",
    "count": 8850
  },
  {
    "label": "Mechanical Engineering / Process Engineering",
    "count": 37000
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
];


var socialData = [{
    "label": "Social Studies",
    "count": 25400
  },
  {
    "label": "Social Sciences",
    "count": 8200
  },
  {
    "label": "Pedagogic",
    "count": 10500
  },
  {
    "label": "Psychology",
    "count": 10800
  },
  {
    "label": "",
    "count": 0
  }

];
var lawData = [{
    "label": "Economics",
    "count": 102000
  },
  {
    "label": "Business Engineering",
    "count": 19900
  },
  {
    "label": "Jurisprudence",
    "count": 32000
  },
  {
    "label": "Administration & Public Management",
    "count": 20000
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
];
var MedData = [{
    "label": "Medicine",
    "count": 16500
  },
  {
    "label": "Pharmacy",
    "count": 15100
  },
  {
    "label": "Dentist",
    "count": 14800
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  }
];









var width = 360;
var height = 360;
var radius = Math.min(width, height) / 2;
var donutWidth = 75;
var legendRectSize = 18;
var legendSpacing = 4;

var color = d3.scale.category20b();

var svg = d3.select('#chart')
  .append('svg')
  .attr('width', width)
  .attr('height', height)
  .append('g')
  .attr('transform', 'translate(' + (width / 2) +
    ',' + (height / 2) + ')');

var arc = d3.svg.arc()
  .innerRadius(radius - donutWidth)
  .outerRadius(radius);

var pie = d3.layout.pie()
  .value(function(d) {
    return d.count;
  })
  .sort(null);

var tooltip = d3.select('#chart')
  .append('div')
  .attr('class', 'tooltip');

tooltip.append('div')
  .attr('class', 'label');

tooltip.append('div')
  .attr('class', 'count');


currentData = dataset;

var path = svg.selectAll('path')
  .data(pie(currentData))
  .enter()
  .append('path')
  .attr('d', arc)
  .attr('fill', function(d, i) {
    return color(d.data.label);
  })
  .each(function(d) {
    this._current = d;
  });

path.on('mouseover', function(d) {
  var total = d3.sum(currentData.map(function(d) {
    return d.count; // UPDATED
  }));
  var percent = Math.round(transitionSpeed * d.data.count / total) / 10;
  tooltip.select('.label').html(d.data.label);
  tooltip.select('.count').html(d.data.count);

  tooltip.style('display', 'block');
});

path.on('mouseout', function() {
  tooltip.style('display', 'none');
});

svg.append("text")
  .attr("class", "return")
  .text(hareReturn + "%")
  .attr("y", ".3em")
  .attr("text-anchor", "middle")
  .style('fill', 'white')

var tortoise = d3.select("#tortoise")
  .on("click", function(e) {
    //alert("tortoise");
    d3.select(".return")
      .transition()
      .duration(transitionSpeed)
      .tween('text', textTween(tortoiseReturn));
    currentData = tortoiseData;
    path = path.data(pie(currentData));
    path.transition()
      .duration(transitionSpeed)
      .attrTween('d', arcTween);
  });

var mix = d3.select("#mix")
  .on("click", function(e) {
    //alert("tortoise");
    d3.select(".return")
      .transition()
      .duration(transitionSpeed)
      .tween('text', textTween(mixReturn));

    path = path.data(pie(mixData));
    path.transition()
      .duration(transitionSpeed)
      .attrTween('d', arcTween);
  })

var hare = d3.select("#hare")
  .on("click", function(e) {
    //alert("tortoise");
    d3.select(".return")
      .transition()
      .duration(transitionSpeed)
      .tween('text', textTween(hareReturn));

    path = path.data(pie(dataset));
    path.transition()
      .duration(transitionSpeed)
      .attrTween('d', arcTween);
  })
var social = d3.select("#socialStudies")
  .on("click", function(e) {

    d3.select(".return")
      .transition()
      .duration(transitionSpeed)
      .tween('text', textTween(mixReturn));

    path = path.data(pie(socialData));
    path.transition() // NEW
      .duration(transitionSpeed) // NEW
      .attrTween('d', arcTween);
  })
var ecolaw = d3.select("#law")
  .on("click", function(e) {
    //alert("tortoise");
    d3.select(".return")
      .transition()
      .duration(transitionSpeed);

    path = path.data(pie(lawData));
    path.transition() // NEW
      .duration(transitionSpeed) // NEW
      .attrTween('d', arcTween);
  })

var Med = d3.select("#med")
  .on("click", function(e) {
    //alert("tortoise");
    d3.select(".return")
      .transition()
      .duration(transitionSpeed);

    path = path.data(pie(MedData));
    path.transition() // NEW
      .duration(transitionSpeed) // NEW
      .attrTween('d', arcTween);
  })

function textTween(newValue) {
  return function() {
    // get current value as starting point for tween animation
    var currentValue = +this.textContent;
    // create interpolator and do not show nasty floating numbers
    var i = d3.interpolate(this.textContent, newValue);

    return function(t) {

      this.textContent = d3.round(i(t), 2) + "%";
    };
  }
}

// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(d) {
  var interpolate = d3.interpolate(this._current, d); // NEW
  this._current = interpolate(0); // NEW
  return function(t) { // NEW
    return arc(interpolate(t)); // NEW
  };
}

d3.interpolators.push(function(a, b) {
  var re = /^(dd.dd)%$/,
    ma, mb, f = d3.format('05.2f');

  if ((ma = re.exec(a)) && (mb = re.exec(b))) {

    a = parseFloat(ma[1]);
    b = parseFloat(mb[1]) - a;

    return function(t) {
      return f(a + b * t) + '%';
    };
  }
});
#left {
  float: left;
  width: 250px;
  overflow: hidden;
  margin-right: 130px;
}

h1,
h2,
h3,
h4 {
  font-weight: 400;
  letter-spacing: 0.3rem;
  text-transform: uppercase;
  color: #777;
}

.buttons {
  margin-left: 250px;
  margin-bottom: 70px;
  margin-top: 80px;
  width: 805px;
}

#chart {
  overflow: hidden;
  height: 360px;
  margin: 0 auto;
  /* NEW */
  position: relative;
}

.tooltip {
  background: #eee;
  box-shadow: 0 0 5px #999999;
  color: #333;
  display: none;
  font-size: 12px;
  left: 130px;
  padding: 10px;
  position: Absolute;
  text-align: center;
  top: 95px;
  width: 80px;
  z-index: 10;
}

.legend {
  font-size: 12px;
}

rect {
  cursor: pointer;
  /* NEW */
  stroke-width: 2;
}

rect.disabled {
  /* NEW */
  fill: transparent !important;
  /* NEW */
}


/* NEW */

h1 {
  /* NEW */
  font-size: 14px;
  /* NEW */
  text-align: center;
  /* NEW */
}

.return {
  font-size: 3.8rem;
}

.tooltip {
  text-align: left;
}

.container {
  width: 1200px;
  height: 500px;
  margin: 25px auto 25px auto;
  padding: 50px 50px 50px 50px;
  background-color: white;
  box-shadow: 0 0 20px #ccc;
  font-family: 'Source Sans Pro', sans-serif;
}

p {
  color: #777;
  font-size: 16px;
  line-height: 28px;
  word-spacing: 1px;
  letter-spacing: 1px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<div class="container">
  <div id="left">
    <h2> Description </h2>
    <p> This Donut Diagram shows you the number of enrolled students in 2018 in each Major and Specialization just click on your desired major and hover on each piece of donut for more information!</p>
    <h2> Category:</h2>
    <p>
      Academic Data</p>
  </div>
  <div id="chart"></div>


  <div class='buttons'>
    <button id="tortoise">Culture / Media
</button>
    <button id="mix">Engineering</button>
    <button id="hare">Mathematics and Natural Science</button>
    <button id="socialStudies">Social Studies</button>
    <button id="law">Economy and law </button>
    <button id="med">Medicine </button>
  </div>


</div>

Advertisement

Answer

I’ve refactored your code a bit, since you kept doing the same thing a lot. Look at the update function for that.

You also didn’t account for more/fewer slices depending on the selection, so I added that as well.

The texts do not animate yet, but I think you should be able to manage that.

var transitionSpeed = 2000;

var hareReturn = "";
var mixReturn = "";
var tortoiseReturn = "";
var socialReturn = "";
var lawReturn = "";
var medReturn = "";

var dataset = [{
    "label": "Mathematics",
    "count": 16500
  },
  {
    "label": "Computer Science",
    "count": 58000
  },
  {
    "label": "Physics",
    "count": 35000
  },
  {
    "label": "Chemistry",
    "count": 12700
  },
  {
    "label": "Biology",
    "count": 13500
  },
];

var tortoiseData = [{
    "label": "English / American Studies",
    "count": 5800
  },
  {
    "label": "History",
    "count": 7900
  },
  {
    "label": "German",
    "count": 11000
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  }
];

var mixData = [{
    "label": "Electro technology",
    "count": 17500
  },
  {
    "label": "Civil Engineering",
    "count": 13700
  },
  {
    "label": "Architecture",
    "count": 8850
  },
  {
    "label": "Mechanical Engineering / Process Engineering",
    "count": 37000
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
];

var socialData = [{
    "label": "Social Studies",
    "count": 25400
  },
  {
    "label": "Social Sciences",
    "count": 8200
  },
  {
    "label": "Pedagogic",
    "count": 10500
  },
  {
    "label": "Psychology",
    "count": 10800
  },
  {
    "label": "",
    "count": 0
  }

];
var lawData = [{
    "label": "Economics",
    "count": 102000
  },
  {
    "label": "Business Engineering",
    "count": 19900
  },
  {
    "label": "Jurisprudence",
    "count": 32000
  },
  {
    "label": "Administration & Public Management",
    "count": 20000
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  },
];
var MedData = [{
    "label": "Medicine",
    "count": 16500
  },
  {
    "label": "Pharmacy",
    "count": 15100
  },
  {
    "label": "Dentist",
    "count": 14800
  },
  {
    "label": "",
    "count": 0
  },
  {
    "label": "",
    "count": 0
  }
];

var width = 360;
var height = 360;
var radius = Math.min(width, height) / 2;
var donutWidth = 75;
var legendRectSize = 18;
var legendSpacing = 4;

var color = d3.scale.category20b();

var svg = d3.select('#chart')
  .append('svg')
  .attr('width', width)
  .attr('height', height)
  .append('g')
  .attr('transform', 'translate(' + (width / 2) +
    ',' + (height / 2) + ')');

var arc = d3.svg.arc()
  .innerRadius(radius - donutWidth)
  .outerRadius(radius);

var pie = d3.layout.pie()
  .value(function(d) {
    return d.count;
  })
  .sort(null);

var tooltip = d3.select('#chart')
  .append('div')
  .attr('class', 'tooltip');

tooltip.append('div')
  .attr('class', 'label');

tooltip.append('div')
  .attr('class', 'count');

svg.append("text")
  .attr("class", "return")
  .text(hareReturn + "%")
  .attr("y", ".3em")
  .attr("text-anchor", "middle")
  .style('fill', 'white')

function textTween(newValue) {
  return function() {
    // get current value as starting point for tween animation
    var currentValue = +this.textContent;
    // create interpolator and do not show nasty floating numbers
    var i = d3.interpolate(this.textContent, newValue);

    return function(t) {

      this.textContent = d3.round(i(t), 2) + "%";
    };
  }
}

// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(d) {
  var dummy = {
    endAngle: 0,
    padAngle: 0,
    startAngle: 0,
    value: 0
  };

  var old = this._current || dummy;
  var _new = d || dummy;

  var interpolate = d3.interpolate(old, _new); // NEW
  this._current = interpolate(0); // NEW
  return function(t) { // NEW
    return arc(interpolate(t)); // NEW
  };
}

d3.interpolators.push(function(a, b) {
  var re = /^(dd.dd)%$/,
    ma, mb, f = d3.format('05.2f');

  if ((ma = re.exec(a)) && (mb = re.exec(b))) {

    a = parseFloat(ma[1]);
    b = parseFloat(mb[1]) - a;

    return function(t) {
      return f(a + b * t) + '%';
    };
  }
});

update(hareReturn, dataset);

function update(currentReturn, currentData) {
  d3.select(".return")
    .transition()
    .duration(transitionSpeed)
    .tween('text', textTween(currentReturn));

  var path = svg.selectAll('path')
    .each(function(d) {
      this._current = d; // set current before updating
    })
    .data(pie(currentData));

  path
    .enter()
    .append('path')
    // We use a dummy value in arcTween to make new slices appear
    .attr('fill', function(d, i) {
      return color(d.data.label);
    });

  path
    .exit()
    .transition()
    .duration(transitionSpeed)
    .attrTween('d', function() {
      // We use a dummy value in arcTween to make old slices disappear
      return arcTween();
    });

  path
    .transition()
    .duration(transitionSpeed)
    .attrTween('d', arcTween);

  /* Add labels here */
  svg.selectAll('.pie-label')
    .data(pie(currentData))
    .enter()
    .append('text')
    .classed('pie-label', true);

  svg.selectAll('.pie-label')
    .attr('x', function(d) {
      return arc.centroid(d)[0];
    })
    .attr('y', function(d) {
      return arc.centroid(d)[1];
    })
    .text(function(d, i) {
      return d.data.label;
    });

  path.on('mouseover', function(d) {
    var total = d3.sum(currentData.map(function(d) {
      return d.count; // UPDATED
    }));
    var percent = Math.round(transitionSpeed * d.data.count / total) / 10;
    tooltip.select('.label').html(d.data.label);
    tooltip.select('.count').html(d.data.count);

    tooltip.style('display', 'block');
  });

  path.on('mouseout', function() {
    tooltip.style('display', 'none');
  });
}

var tortoise = d3.select("#tortoise")
  .on("click", function(e) {
    update(tortoiseReturn, tortoiseData);
  });

var mix = d3.select("#mix")
  .on("click", function(e) {
    update(mixReturn, mixData);
  })

var hare = d3.select("#hare")
  .on("click", function(e) {
    update(hareReturn, dataset);
  })
var social = d3.select("#socialStudies")
  .on("click", function(e) {
    update(socialReturn, socialData);
  })
var ecolaw = d3.select("#law")
  .on("click", function(e) {
    update(lawReturn, lawData);
  })

var Med = d3.select("#med")
  .on("click", function(e) {
    update(medReturn, MedData);
  })
#left {
  float: left;
  width: 250px;
  overflow: hidden;
  margin-right: 130px;
}

h1,
h2,
h3,
h4 {
  font-weight: 400;
  letter-spacing: 0.3rem;
  text-transform: uppercase;
  color: #777;
}

.buttons {
  margin-left: 250px;
  margin-bottom: 70px;
  margin-top: 80px;
  width: 805px;
}

#chart {
  overflow: hidden;
  height: 360px;
  margin: 0 auto;
  /* NEW */
  position: relative;
}

.tooltip {
  background: #eee;
  box-shadow: 0 0 5px #999999;
  color: #333;
  display: none;
  font-size: 12px;
  left: 130px;
  padding: 10px;
  position: Absolute;
  text-align: center;
  top: 95px;
  width: 80px;
  z-index: 10;
}

.legend {
  font-size: 12px;
}

rect {
  cursor: pointer;
  /* NEW */
  stroke-width: 2;
}

rect.disabled {
  /* NEW */
  fill: transparent !important;
  /* NEW */
}


/* NEW */

h1 {
  /* NEW */
  font-size: 14px;
  /* NEW */
  text-align: center;
  /* NEW */
}

.return {
  font-size: 3.8rem;
}

.tooltip {
  text-align: left;
}

.container {
  width: 1200px;
  height: 500px;
  margin: 25px auto 25px auto;
  padding: 50px 50px 50px 50px;
  background-color: white;
  box-shadow: 0 0 20px #ccc;
  font-family: 'Source Sans Pro', sans-serif;
}

p {
  color: #777;
  font-size: 16px;
  line-height: 28px;
  word-spacing: 1px;
  letter-spacing: 1px;
}


/* Styles to make the labels nicer */

text.pie-label {
  text-anchor: middle;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<div class="container">
  <div id="left">
    <h2> Description </h2>
    <p> This Donut Diagram shows you the number of enrolled students in 2018 in each Major and Specialization just click on your desired major and hover on each piece of donut for more information!</p>
    <h2> Category:</h2>
    <p>
      Academic Data</p>
  </div>
  <div id="chart"></div>


  <div class='buttons'>
    <button id="tortoise">Culture / Media
</button>
    <button id="mix">Engineering</button>
    <button id="hare">Mathematics and Natural Science</button>
    <button id="socialStudies">Social Studies</button>
    <button id="law">Economy and law </button>
    <button id="med">Medicine </button>
  </div>


</div>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement