代码之家  ›  专栏  ›  技术社区  ›  ttmt

角度,SVG-在SVG中动态创建圆

  •  0
  • ttmt  · 技术社区  · 7 年前

    我一直在用这篇文章创建SVG饼图

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

    我这里有一个Exmapke,使用Angular从一个数据数组中创建3个单独的饼图。

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

    我现在需要创建一个饼图,其中所有部分都在一个图表上。

    是否可以在SVG中使用角度之类的方法创建圆?

    <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>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Ted    7 年前

    当然,在同一个SVG中绘制所有的圆,而不是三个单独的SVG。这是你的密码 donuts.template.html 修改后将它们全部绘制在同一个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>
    
    推荐文章