[英]angular d3 not rendering uniquely with *ngFor
i am trying to implement angular-d3-donut to my web application.我正在尝试在我的 Web 应用程序中实现angular-d3-donut 。 each has one SVG(donut) .
每个都有一个SVG(donut) 。 so if i generate using *ngFor there'll be multiple with one SVG(donut).
因此,如果我使用 *ngFor 生成,将有多个带有一个 SVG(甜甜圈)。 but problem is that, if i try to generate multiple by *ngFor, only one is creating and multiple SVG(donut) nesting inside the single .
但问题是,如果我尝试通过 *ngFor 生成多个,则只有一个正在创建,并且多个 SVG(甜甜圈)嵌套在单个. so i am getting design mismatching and different result.
所以我得到了设计不匹配和不同的结果。
HTML HTML
<div class="row">
<div class="col-md-3" *ngFor="let x of [1,2,3,4]; let myIndex = index">
<angular-d3-donut [data]="donutChartData" [outerRadius]=60 [innerRadius]=20 [spreadSlice]=true [iconWidth]=20 [iconHeight]=20 ></angular-d3-donut>
</div>
</div>
Component.ts组件.ts
public donutChartData = [{
id: 0, // number
label: 'A', // string
value: 70, // number
color: '#ccc', // string,
iconImage: 'https://avatars.discourse.org/v2/letter/a/a8b319/45.png' // string
}, {
id: 1, // number
label: 'B', // string
value: 30, // number
color: '#DA4F49', // string,
iconImage: 'https://avatars.discourse.org/v2/letter/a/a8b319/45.png' // string
}
]
I think the problem is that you are plotting in the same "dountChart" div each time.我认为问题在于您每次都在同一个“dountChart”div 中进行绘图。 If you want to create a new div, you need to change the id of each "dountChart".
如果你想创建一个新的div,你需要改变每个“dountChart”的id。 For instance, let's suppose I want to plot multiple circles (your dount charts) so I have this HTML:
例如,假设我想绘制多个圆圈(你的圆点图),所以我有这个 HTML:
<div *ngFor="let item of options.items; index as i">
<angular-d3-donut [index]="i"></angular-d3-donut>
</div>
Let's say that item
is my dummy data.假设该
item
是我的虚拟数据。 If you pass the index i
to your angular-d3-donut
component you can use it to identify your multiple DOM elements如果将索引
i
传递给angular-d3-donut
组件,则可以使用它来识别多个 DOM 元素
@Component
({
selector: 'angular-d3-donut',
templateUrl: './angular-d3-donut.component.html',
styleUrls: ['./angular-d3-donut.component.css']
})
export class AngularD3Donut implements OnInit
{
private circle: any; // Is Circle class
private container: any; // div
@Input() public index: number = 0;
@Input() public height: number = 20;
public color: string = 'black';
constructor() {}
ngOnInit(): void
{
this.container = d3.select('#dountChart') // First select the original id
.attr('id', 'dountChart' + this.index) // Now, change the id to make it unique
.style('height', this.height.toString() + "px")
.style('background-color', 'red');
this.circle = new Circle(10, this.color, 10, 10, this.index); // Circle(radius, 'color', coor_x, coor_y, index)
}
}
class Circle
{
private container: any; //svg
private circle: any;
constructor(private radius: number, private color: string, private x: number, private y: number, private index: number)
{
this.container = d3.select('#circle') // First, select the original id
.attr('x', this.x)
.attr('y', this.y)
.attr('height', this.radius * 2)
.attr('id', 'circle'+this.index); // Now, change the id to make it unique
this.circle = this.container.append('circle')
.attr('cx', this.x)
.attr('cy', this.y)
.attr('r', this.radius)
.style('fill', this.color);
}
}
And the html component of AngularD3Donut is: AngularD3Donut 的 html 组件是:
<div id='dountChart'>
<svg id='circle'></svg>
</div>
If you change the id each time, then the original id always will be the next element in the iteration如果每次都更改 id,那么原始 id 将始终是迭代中的下一个元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.