Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
Last active February 3, 2019 17:02
Show Gist options
  • Save AvocadoVenom/eb37ab3da74405ccb3e40316274588e7 to your computer and use it in GitHub Desktop.
Save AvocadoVenom/eb37ab3da74405ccb3e40316274588e7 to your computer and use it in GitHub Desktop.
Adding labels in the pie chart d3 angular component
private arcLabel: any;
private texts: any;
private draw() {
// ...
this.drawLabels();
}
private setArcs() {
// ...
this.arcLabel = d3.arc().innerRadius(this.radius * .8).outerRadius(this.radius * .8);
}
private drawLabels() {
this.texts = this.mainContainer.selectAll('text')
.remove().exit()
.data(this.pie(this.dataSource))
.enter().append('text')
.attr('text-anchor', 'middle').attr('transform', d => `translate(${this.arcLabel.centroid(d)})`).attr('dy', '0.35em');
this.texts.append('tspan').filter(d => (d.endAngle - d.startAngle) > 0.05)
.attr('x', 0).attr('y', 0).style('font-weight', 'bold')
.text(d => d.data.name);
this.texts.append('tspan').filter(d => (d.endAngle - d.startAngle) > 0.25)
.attr('x', 0).attr('y', '1.3em').attr('fill-opacity', 0.7)
.text(d => d.data.value);
}
private resize() {
// ...
this.drawLabels();
}
private repaint() {
// ...
this.drawLabels();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment