【问题标题】:Displaying a circle inside an SVG with D3.js使用 D3.js 在 SVG 内显示一个圆圈
【发布时间】:2021-12-26 13:56:08
【问题描述】:

在我的 Angular 应用程序中,我已经完成了以下工作。 但是,圆圈​​不会在 svg 上绘制。 我做错了什么,圆圈没有显示?

  svg: any;
  margin = 50;
  width = 350 - (this.margin * 2);
  height = 300 - (this.margin * 2);

  ngOnInit(): void {
    this.createSvg();
    this.createCircle();
  }

  createSvg(): void {
    this.svg = d3
      .select("figure#zoom-pan")
      .append("svg")
      .attr("width", "100%")
      .attr("height", "100%")
      .append("g")
      .attr("transform", "translate(" + this.margin + "," + this.margin + ")");
  }

  createCircle(): void {
      this.svg
        .append("circle")
        .attr("cx", document.body.clientWidth / 2)
        .attr("cy", document.body.clientHeight / 2)
        .attr("r", 50)
        .style("fill", "#B8DEE6")
  }

  ngAfterViewInit() {
    let svg = d3
      .select("svg")
      .call(d3.zoom().on("zoom", (event) => {
        svg.attr("transform", event.transform);
      }))
      .append("g");
  }

我的html 模板和css 代码非常简单:

<h3 class="center">Zoom Pan</h3>
<figure id="zoom-pan" class="center"></figure>
<ng-content></ng-content>

.center {
    display: flex;
    justify-content: center;
}

figure#zoom-pan {
    margin: 0;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

我只得到“缩放平移”和一个空白区域......我错过了什么?

【问题讨论】:

    标签: javascript angular typescript d3.js


    【解决方案1】:

    我制作了这个代码的一个工作示例,显示了圆圈。首先它需要是一个类和一个角度组件。

    Check out the working example at stackblitz

    这是组件的打字稿:

    import { Component, OnInit } from '@angular/core';
    import * as d3 from 'd3';
    
    @Component({
      selector: "ng-content",
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit  {
      svg: any;
      margin = 50;
      width = 350 - this.margin * 2;
      height = 300 - this.margin * 2;
    
      ngOnInit(): void {
        this.createSvg();
        this.createCircle();
      }
    
      createSvg(): void {
        this.svg = d3
          .select("figure#zoom-pan")
          .append("svg")
          .attr("width", "100%")
          .attr("height", "100%")
          .append("g")
          .attr("transform", "translate(" + this.margin + "," + this.margin + ")");
      }
    
      createCircle(): void {
        this.svg
          .append("circle")
          .attr("cx", document.body.clientWidth / 2)
          .attr("cy", document.body.clientHeight / 2)
          .attr("r", 50)
          .style("fill", "#B8DEE6");
      }
    
      ngAfterViewInit() {
        let svg = d3
          .select("svg")
          .call(
            d3.zoom().on("zoom", (event) => {
              svg.attr("transform", event.transform);
            })
          )
          .append("g");
    
      }
    }
    

    还有模板:

    <h3 class="center">Zoom Pan</h3>
    <figure id="zoom-pan" class="center"></figure>
    

    我建议始终提供minimal working example

    【讨论】:

    • 但是代码的哪一部分出错了?
    • 也许你需要在组件模板里面有图。
    【解决方案2】:

    如果您没有以像素为单位设置 svg 的高度和宽度,根据我的经验,您需要使用 viewBox。

    this.svg = d3
      .select("figure#zoom-pan")
      .append("svg")
      .attr("width", "100%")
      .attr("height", "100%")
      .attr("viewBox", "0 0 " + this.width + " " + this.height)
    

    【讨论】:

    • 感谢您的回答,但没有任何改变;仍然没有显示。我想我的 createCircle() 方法可能有问题?
    【解决方案3】:

    当使用 angular 时,我建议你放弃 D3 选择器而只使用 angular。 Angular 已经有 DOM 操作标记,所以你不需要使用 D3 来做。

    // component
    createCircle() { 
      this.circle = {
        cx: document.body.clientWidth / 2,
        cx: document.body.clientHeight / 2,
        r: 50
      }
    }
    
    // component template
    <figure>
      <svg
        [height]="height + (margin * 2)"
        [width]="width + (margin * 2)"
      >
        <g [attr.transform]="'translate(' + margin + ',' + margin + ')">
          <circle *ngIf="circle"
            [attr.cx]="circle.cx"
            [attr.cy]="circle.cx"
            [attr.r]="circle.r" />
        </g>
      </svg>
    </figure>
          
    

    【讨论】:

    • 感谢您的回答。这是一个很好的方法,但就我而言,我不能偏离项目中的 d3 使用;我必须继续将它与所有可视化一起使用。您是否看到我在原始问题中做错了什么,所以我可以修复现有代码?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多