Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
Last active February 3, 2019 16:53
Show Gist options
  • Save AvocadoVenom/8fc4de04d1723ba670fbcdc67b102047 to your computer and use it in GitHub Desktop.
Save AvocadoVenom/8fc4de04d1723ba670fbcdc67b102047 to your computer and use it in GitHub Desktop.
Adding labels to d3 bar chart
private labels: any; // Labels
private draw() {
// ...
this.drawLabels();
}
private repaint() {
// ...
this.drawLabels();
}
private drawLabels() {
this.labels = this.mainContainer.selectAll('.label')
.remove().exit()
.data(this.dataSource)
.enter().append('text').attr('class', 'label')
.attr('x', d => this.xScale(d.name) + (this.xScale.bandwidth() / 2))
.attr('y', d => this.yScale(d.value) - 5))
.text(d => Math.floor(d.value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment