首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的D3 svg没有显示在屏幕上?

为什么我的D3 svg没有显示在屏幕上?
EN

Stack Overflow用户
提问于 2022-03-16 18:59:00
回答 1查看 92关注 0票数 0

这是我的密码!我使用角和d3库来制作一个简单的形状,但是屏幕上什么也没有显示。

饼形图TS文件:

代码语言:javascript
复制
@Component({
  selector: 'pie-chart',
  templateUrl: './pie-chart.component.html',
  styleUrls: ['./pie-chart.component.css'],
})

export class PieChartComponent {
  svg = d3
    .select('#canvasarea')
    .append('svg')
    .attr('width', 400)
    .attr('height', 100);

  circle = this.svg
    .append('circle')
    .attr('cx', 200)
    .attr('cy', 200)
    .attr('r', 50)
    .attr('fill', 'blue');
}

饼图HTML文件:

代码语言:javascript
复制
<div id="canvasarea"></div>

app-component.html:

代码语言:javascript
复制
<pie-chart></pie-chart>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-16 22:18:29

如前所述,您确实需要添加带有r属性的半径。您还需要在角组件中呈现视图,以添加svg并对其执行操作。

代码语言:javascript
复制
import { AfterViewInit, Component } from '@angular/core';
import * as d3 from 'd3';

@Component({
  selector: 'pie-chart',
  templateUrl: './pie-chart.component.html',
  styleUrls: ['./pie-chart.component.css'],
})
export class PieChartComponent implements AfterViewInit {
  public svg: any = null;

  constructor() { }

  ngAfterViewInit(): void {
    this.svg = d3
      .select('#canvasarea')
      .append('svg')
      .attr('width', 400)
      .attr('height', 100);

    this.svg
      .append('circle')
      .attr('cx', 50)
      .attr('cy', 50)
      .attr('r', 20)
      .style('fill', 'blue');
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71502897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档