Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

392
Vistas
Inserting a text on top of the rect component in a svg using d3

I've to put some text on top of the rectangles on a stacked bar chart in d3.js. I've to insert the text exactly as reported on the attached image below instead of the 'x' characters. How could I do?enter image description here

This is my code that creates the svg.

chartRealModel = {
  const svg = d3.create("svg")
      .attr("viewBox", [0, 0, width, height]);

  svg.append("g")
    .selectAll("g")
    .data(seriesRealModel)
    .join("g")
      .attr("fill", d => color(d.key))
    .selectAll("rect")
    .data(d => d)
    .join("rect")
      .attr("x", (d, i) => xRealModel(d.data.Week))
      .attr("y", d => yRealModel(d[1]))
      .attr("height", d => yRealModel(d[0]) - yRealModel(d[1]))
      .attr("width", xRealModel.bandwidth())
    .append("title")
      .text(d => `${d.key}
${formatValue(d.data[d.key])}`);

  svg.selectAll("rect")

  svg.append("g")
      .call(xAxisRealModel);

  svg.append("g")
      .call(yAxisRealModel);

  return svg.node();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you pretty much do the same thing as you did to create the bars, but instead with the text

svg.append("g")
  .selectAll("label")
  .data(seriesRealModel)
   .join('text')
   .text( d => `${d.key}${formatValue(d.data[d.key])}`);
    // positioning the text in the middle of the bar
    .attr('x', d  => xRealModel(d.data.Week) + xRealModel.bandwidth() /2)
    // positioning on the top of the bar 
    .attr('y',d => yRealModel(d[1]) -5)
    .attr("fill", "black")
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda