简体   繁体   中英

Angular, svg - dynamically create circles in an svg

I've been using this articale to create an svg pie charts

https://medium.com/@heyoka/scratch-made-svg-donut-pie-charts-in-html5-2c587e935d72

I have an exmapke here using angular to create 3 separate pie charts from the one array of data.

https://stackblitz.com/edit/svg-donuts-uskvrn?file=src/app/donuts.component.ts

I now need to create one pie chart with all the sections on the one chart.

Is it possible to create circles in the svg using Angular, something like.

<circle 
  *ngFor="let dount of dountData; let i = index;"
  class="donut-segment" 
  cx="21" 
  cy="21" 
  r="15.91549430918954" 
  fill="transparent" 
  [attr.stroke]="donut.color" 
  stroke-width="3" 
  [attr.stroke-dasharray]="donut.percent + ' ' + (100 - donut.percent)"
  [attr.stroke-dashoffset]=strokeDashOffset>
</circle>

Sure, draw all the circles in the same svg instead of three separate svgs. Here's your code for donuts.template.html modified to draw them all in the same svg:

<div>
  <svg width="100%" height="100%" viewBox="0 0 42 42" 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>

    <g *ngFor="let donut of donutData; let i = index;">
    {{updatePercent(i, donut)}} 

      <circle class="donut-segment" 
          cx="21" 
          cy="21" 
          r="15.91549430918954" 
          fill="transparent" 
          [attr.stroke]="donut.color" 
          stroke-width="3" 
          [attr.stroke-dasharray]="donut.percent + ' ' + (100 - donut.percent)"
          [attr.stroke-dashoffset]=strokeDashOffset></circle>
    </g>
  </svg>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM