Transition the numeric labels in a bar chart

Transition the numeric labels in a bar chart

By : dreko90
Date : January 12 2021, 01:40 AM
wish helps you You can get the current value for each text by different ways.
For instance, with vanilla JavaScript:
code :
var current = +(this.textContent);
var current = +(d3.select(this).text());
const data = [{
    key: 0,
    value: 50
    key: 1,
    value: 20
    key: 2,
    value: 100
    key: 3,
    value: 30
    key: 4,
    value: 40
    key: 5,
    value: 70
// const dataset = [50, 20, 100, 30, 40]

const svgWidth = 800;
const svgHeight = 400;

const xScale = d3.scaleBand()
  .rangeRound([0, svgWidth])

const yScale = d3.scaleLinear()
  .domain([0, d3.max(data, d => d.value)])
  .range([0, svgHeight]);

const svg = d3.select('body')
  .attr('width', svgWidth)
  .attr('height', svgHeight);

let bars = svg.selectAll('rect').data(data, d => d.key);
let labels = svg.selectAll('text').data(data);

  .each(function(d) {
    return this._old = d;
  .attr('width', xScale.bandwidth)
  .attr('height', d => yScale(d.value))
  .attr('fill', d => `rgb(${d.value}, ${d.value * 2}, ${d.value * 3})`)
  .attr('x', (d, i) => xScale(i))
  .attr('y', d => svgHeight - yScale(d.value))
  .attr('stroke', 'black')
  .attr('stroke-width', 3)

  .attr('x', (d, i) => xScale(i) + (xScale.bandwidth() / 2))
  .attr('y', d => svgHeight - yScale(d.value) + 20)
  .attr('font-size', 20)
  .attr('text-anchor', 'middle')
  .attr('fill', 'white')
  .text(d => d.value);

let asc = false;

d3.select('button').on('click', () => {
  if (!asc) {
    data.sort((a, b) => b.value - a.value);
  } else {
    data.sort((a, b) => a.value - b.value);

  asc = !asc;

  bars = svg.selectAll('rect').data(data, d => d.key);
  labels = svg.selectAll('text').data(data);

    .delay((d, i) => (i * 10))
    .each(function(d) {
      return this._old = d;
    .attr('x', (d, i) => xScale(i))
    .attr('height', d => yScale(d.value))
    .attr('y', d => svgHeight - yScale(d.value));

    .delay((d, i) => (i * 10))
    .tween("text", function(d) {
      var current = +(d3.select(this).text());
      var that = this;
      var i = d3.interpolate(current, d.value); // Number(d.percentage.slice(0, -1))
      return function(t) {
    .attr('y', d => svgHeight - yScale(d.value) + 20);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

Share : facebook icon twitter icon
Transition on labels in d3 pie/donut chart

Transition on labels in d3 pie/donut chart

By : Hiran
Date : March 29 2020, 07:55 AM
around this issue Add same transition effect to label group also
code :
  .attr("transform", function(d) {
    var c = arc.centroid(d);
  return "translate(" + c[0] +"," + c[1] + ")";
D3 exit mutli-series line chart labels on transition

D3 exit mutli-series line chart labels on transition

By : ahmer khan
Date : March 29 2020, 07:55 AM
will be helpful for those in need You are appending a new text element to each person every time you render and not removing the old ones. Presumably the code you posted gets run every time you want to draw the chart with new data, so you end up with more text elements every time, rather than updating the existing ones. You need to only append on the "enter" selection. You did this right on the path elements, so you just need to make the text work more like the path. I've updated your example with comments to highlight what I changed.
code :
var line = d3.svg.line()
  .x(function(d) { return x(d.Date); })
  .y(function(d) { return y(d.candidate); });

var person = svg_multi.selectAll(".candidate")
  // I added a key function here to make sure you always update the same
  // line for every person. This ensures that when you re draw with different
  // data, the line for Trump doesn't become the line for Sanders, for example.
  .data(people, function(d) { return d.name});

var personGroups = person.enter()
  .attr("class", "candidate");

// This isn't needed. The path and text get appended to the group above,
// so this one just sits empty and clutters the DOM
// person
//   .enter().append("g")
//   .attr("class", "candidate");

  .attr("class", "line")
  // You do this down below, so no need to duplicate it here
  // .attr("d", function(d) { return line(d.values); })
  .style("stroke", function(d) { return color(d.name); });
// Append the text element to only new groups in the enter selection  
  // Set any static attributes here that don't update on data
  .attr("x", 3)
  .attr("dy", ".35em");

var personUpdate = d3.transition(person);

  .attr("d", function(d) {
    return line(d.values);

  // You don't have to do this datum call because the text element will have
  // the same data as its parent, but it does make it easier to get to the last
  // value in the list, so you can do it if you want
  .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
  .attr("transform", function(d) { return "translate(" + x(d.value.Date) + "," + y(d.value.candidate) + ")"; })
  .text(function(d) { return d.name; });

// Remove this. You don't need it anymore since you are updating the text above
// person.selectAll("text").transition()
//   .attr("transform", function(d) { return "translate(" + x(d.value.Date) + "," + y(d.value.candidate) + ")"; });

How to display labels in pie transition chart using d3.js?

How to display labels in pie transition chart using d3.js?

By : Salik Bin Rashid
Date : March 29 2020, 07:55 AM
Hope this helps To display the text in the "exact location" (which I supposed is in the middle of the arc), you can use the centroid() function:
code :
var labels = arcs.append("text")
    .attr("transform", function(d) { 
        d.innerRadius = 120;
        return "translate(" + arc.centroid(d) + ")"; 
    .attr("dy", ".35em")
    .text(function(d) { return d.value; });
var w = 400,
h = 400,
r = Math.min(w, h) / 2,
data = [25,45],            // Data values
color = d3.scale.category20(),
arc = d3.svg.arc().outerRadius(r),
donut = d3.layout.pie();

var vis = d3.select("body").append("svg")  // Place the chart in 'pie-chart-div'
    .attr("width", w)
    .attr("height", h);

var arcs = vis.selectAll("g.arc")
    .attr("class", "arc")
    .attr("transform", "translate(" + r + "," + r + ")");

var paths = arcs.append("path")
    .attr("fill", function(d, i) { return color(i); });
var labels = arcs.append("text")
      .attr("transform", function(d) { d.innerRadius = 120; return "translate(" + arc.centroid(d) + ")"; })
      .attr("dy", ".35em")
      .text(function(d) { return d.value; });

    .attrTween("d", tweenPie);

    .delay(function(d, i) { return 2000 + i * 50; })
    .attrTween("d", tweenDonut);

function tweenPie(b) {
  b.innerRadius = 0;
  var i = d3.interpolate({startAngle: 0, endAngle: 0}, b);
  return function(t) {
    return arc(i(t));

function tweenDonut(b) {
  b.innerRadius = r * .6;
  var i = d3.interpolate({innerRadius: 0}, b);
  return function(t) {
    return arc(i(t));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Transition of labels in D3.js: Rename label names on change and transition smoothly

Transition of labels in D3.js: Rename label names on change and transition smoothly

By : user3112927
Date : March 29 2020, 07:55 AM
With these it helps The problem is that you are removing the DOM elements for the text and not updating them. If there is a need to remove them then you can fade out the text and remove them at the end as such d3.select("text").transition().duration(300).style("opacity","0").on("end", () => { d3.select("text").removeAll() });
but I suggest that you reuse the labels and just update their content using the same d3.select("").transition().duration(300) way
How to add direct labels to a bar chart in ggplot for numeric x axis

How to add direct labels to a bar chart in ggplot for numeric x axis

By : user3517532
Date : March 29 2020, 07:55 AM
To fix the issue you can do I think it will be easier to do a bit of data prep so you can put all the boxes in one facet with a shared x-axis. For instance, we can calc the cumulative sum of reduction Kg, and use that to define the starting x for each box.
EDIT -- added ylim = c(0, NA), xlim = c(0, NA), to keep ggrepel::geom_text_repel text within positive range of plot.
code :

Cost %>%
  arrange(desc(Average.of.Cost_Per_Kg_P_Removal.undiscounted..LOW_Oncost)) %>%
  mutate(Row.Labels = forcats::fct_inorder(Row.Labels),
         cuml_reduc = cumsum(Average.of.FS_Annual_P_Reduction_Kg),
         bar_start  = cuml_reduc - Average.of.FS_Annual_P_Reduction_Kg,
         bar_center = cuml_reduc - 0.5*Average.of.FS_Annual_P_Reduction_Kg) %>%
  ggplot(aes(xmin = bar_start, xmax = cuml_reduc,
             ymin = 0, ymax = Average.of.Cost_Per_Kg_P_Removal.undiscounted..LOW_Oncost)) +
  geom_rect(fill = "grey", colour = "black") +
  geom_text_repel(aes(x = bar_center, 
                      y = Average.of.Cost_Per_Kg_P_Removal.undiscounted..LOW_Oncost,
                      label = str_wrap(Row.Labels, 15)), 
                  ylim = c(0, NA), xlim = c(0, NA),  ## EDIT
                  size = 3, nudge_y = 1E4, nudge_x = 2, lineheight = 0.7, 
                  segment.alpha = 0.3) +
  scale_y_continuous(labels = scales::comma) +
  labs(x = "Measure code and average P reduction (kg/P/yr)",
       y = "Mean annual TOTEX (£/kg) of P removal (thousands)")
Related Posts Related Posts :
  • redux how to use state
  • All data in the db is lost when closing a express based node js server
  • How to convert javascript code for Angular
  • XPath select on child nodes
  • Idle event on mouse move - how to stop script permanently on mouse move
  • Why do I get logic is not defined in this code
  • When I enter a number in the input, the first function starts
  • ES6 imports and node_modules hell
  • How to get css selector of clicked-on element?
  • How to auto input MySQL data (input column 1 data to auto input column 2 data with same column) in php?
  • Appending to Datalist Via Javascript
  • Customizing Material-UI Components with Styled-Components
  • Populate elements with class name using Javascript
  • How to execute promises in batches (first resolving 10, then next 10 and so on)
  • Maximo JS automation script: "importPackage" is not defined
  • How to surround selected text inside textarea with <em> on key combination CTRL+i?
  • Your configured registry does not support audit requests ~ On npm audit
  • What's the Angular equivalent of Vue <slot/>?
  • Iconify icon not updating when "data-icon" set with setAttribute()
  • Javascript newbie stuck with XMLHttpRequest return data
  • How to add obj to to state in my example Redux React
  • Loading "grunt-karma.js" tasks...ERROR >> SyntaxError: Unexpected identifier
  • Why do I get log is not defined
  • get the multiple index of the same string
  • How can I iterate over an Array object by using For IN or FOR OF
  • Launch local executable from web browser
  • How to prepare array of json objects to d3 hierarchical tree structure
  • VueJS - event emitted - show HTML before other calculations
  • Export SASS/SCSS variables to Javascript without exporting them to CSS
  • How to attach an event handler only once continuously
  • How to make button open a link in new tab in JavaScript?
  • Find range from the javascript object
  • How can I use my jQuery code as JavaScript?
  • How to show 5 images in a 2 row slider with bxslider
  • How to convert stream into variables in typescript
  • Three.js custom shader error on Chrome GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs
  • My JavaScript doesn't work. How can I solve this problem?
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • Why several clicks fire for each instance of a Class?
  • cant grab value from div using attr()
  • Instantiate subclass without constructing
  • Outputing a created object in javascript to a server side JSON file
  • createBottomTabNavigator: hide just one tab from the tabBar
  • iife vs simple statement behavior with document.title
  • showDialogPopup Refresh Page after Click
  • Child component does not set the initial value passed from the parent: ReactJS
  • Nodejs javascript added to button not responding
  • Trying to define a promise.all
  • Python GEE to extract featurecollection timeseries from Landsat imageries
  • How to write negative binary number?
  • Unable to set property of individual object
  • Why Named Function Expression itself cannot assign Name to another Value?
  • How to reset scroll position on single page pagination
  • How to scroll to bottom of page, when a new message is sent - Socket.io
  • How to reconstruct audio blob from a base64 encoded String?
  • How to calculate numbers within an array of objects
  • Using JavaScript to change CSS properties of SVG circles for all circles that was not clicked
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org