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

357
Vistas
How do I add polylines to my simple svg donut chart? [NO LIBRARIES]

I want to build a simple svg donut chart, with labels and polylines connecting the sectors to the label text like this.

enter image description here

I know this is possible using d3.js like implemented here, but I am restricted to using a simple chart without any libraries.

This is the code I have now;

<div class="wrapper">
  <svg viewBox="0 0 42 42" preserveAspectRatio="xMidYMid meet" class="donut">
    <circle class="donut-hole" cx="21" cy="21" r="15.91549430918954" fill="#fff"></circle>
    <circle class="donut-ring" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#d2d3d4" stroke-width="3"></circle>
    <circle class="donut-segment" data-per="20" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#49A0CC" stroke-width="3" stroke-dasharray="50 50" stroke-dashoffset="25"></circle>
    <circle class="donut-segment" data-per="30" cx="21" cy="21" r="15.91549430918954" fill="transparent" stroke="#254C66" stroke-width="3" stroke-dasharray="50 50" stroke-dashoffset="75"></circle>   
   <!--   stroke-dashoffset formula:
     100 − All preceding segments’ total length + First segment’s offset = Current segment offset
   -->
  </svg>
</div>

Any tips on how to draw polylines and position them properly without overlap?

EDIT: I want to input dynamic data to the component so that it will draw the chart whenever the data is changed.

implementations in d3: impl1

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

My main doubt was calculating the points for the arcs that have to be drawn for a donut chart, which I then understood thanks to this amazing answer by enxaneta.

All I had to figure out was adding the third point for the polyline, if the text.xCoordinate was closer to either side of the svg, I moved it either left or right by a preset amount. I also had to split the labels into multiple <tspan>s as <text> elements would not break the long text and long labels would get clipped off by the SVG. It is also possible to append HTML within a <foreignObject> and position it correctly, to overcome the text wrapping issue.

Another option is to use <path> elements for generating arcs, but I am not sure how to calculate the centroid of each of the arcs.

Recommended reading:

  • Medium article for <circle> donut chart kinda like enxaneta's answer, with the attributes explained.
  • Codepen with another <circle> donut chart
  • <path> donut chart as mentioned above, implemented beautifully by Mustapha.
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