问题
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