Angular / svg - pass data into svg

徘徊边缘 提交于 2021-02-19 08:51:08

问题


I have a stackblitz here - https://stackblitz.com/edit/angular-vkwets?file=src%2Fapp%2Fdonuts.template.html

I have an Angular component that creates an svg donut chart for each data point of an array passed to the component.

I can access the data passed into the component but is it possible to use this in the svg.

I would like to update the 'stroke-dasharray' in the svg using the data passed to the component.

something like stroke-dasharray=donut.percent 100-donut.perecent

<div>
  <ul>
    <li *ngFor="let donut of donutData">
      {{donut.percent}}
      <svg width="20%" height="20%" 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>

        <circle class="donut-segment" 
                cx="21" 
                cy="21" 
                r="15.91549430918954" 
                fill="transparent" 
                stroke="green" 
                stroke-width="3" 
                stroke-dasharray="60 40"
                stroke-dashoffset="25"></circle>
      </svg>

    </li>
  </ul>
</div>

回答1:


Convert the values to a string and use attribute binding:

[attr.stroke-dasharray]="donut.percent + ' ' + (100 - donut.percent)"

See this stackblitz for a demo.



来源:https://stackoverflow.com/questions/52703983/angular-svg-pass-data-into-svg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!