Angular v12,try 使用d3,nvd3(而不是ng2—nvd3).编译正常,但运行时出错.

default—node_modules_d3_src_index_js. js:2 Error Error:Uncatch(in promise):ReferenceError:d3未定义ReferenceError:d3未定义 定义 at nv.d3.js:18:7 在75301(nv.d3.js:15671:2) 在webpack_require( bootstrap :19:1) 在89275(src_app_mypage_modules_ts. js:15:62) 在webpack_require( bootstrap :19:1) 在13111(pageone. component. html:2:39) 在webpack_require( bootstrap :19:1) 在Function.webpack_require( bootstrap :19:1) at_ZoneDelegate. invoke(zone. js:368:26) at resolvePromise(zone. js:1193:31) at resolvePromise(zone. js:1147:17) at zone. js:1260:17 at_ZoneDelegate. invokeTask(zone. js:402:31) 在核心. mjs:25948:55 at AsyncStackTaggingZoneSpec. onnkkeTask(core. mjs:25948:36) at_ZoneDelegate. invokeTask(zone. js:401:60) at Object. onjkeTask(core. mjs:26249:33) at_ZoneDelegate. invokeTask(zone. js:401:60) at Zone. runTask(zone. js:173:47)

以下是我们所做的:

npm install d3
npm install nvd3
npm install @types/d3 --save-dev
npm install @types/nvd3 --save-dev

组件.html:

<div id='chart' class='span4'>
    <svg style='height:500px;width:100%'> </svg>
</div>

TS:

import * as d3 from 'd3';  
import * as nv from 'nvd3';  

ngOnInit(): void 
{
    this.myservice.subscribe(resp=> {
        this.chartdata = resp.data;
        this.drawchart();
    });
}
  
drawchart()
{
    nv.addGraph(()=> {
    var chart = nv.models.pieChart()
      .x((d) => { return d.label })
      .y((d) => { return d.value })
      .showLabels(true)
      .donut(true)         
      .labelThreshold(0.001) 
      .labelType("key").donutRatio(0.60); 

    d3.select("#chart svg")
        .datum(this.chartdata)
        .call(chart as any);
    ...
    });
}

推荐答案

确保您使用以下命令安装了d3

npm i d3@^3.5.17

因为这是nvd3似乎支持的版本

如果这不起作用.包括脚本导入,如下所示!

<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://cdn.rawgit.com/novus/nvd3/v1.8.1/build/nv.d3.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.rawgit.com/novus/nvd3/v1.8.1/build/nv.d3.css"
/>

然后我们可以使用下面的代码,创建一个基本的图表

import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <div id='chart' class='span4'>
        <svg style='height:500px;width:100%'> </svg>
    </div>
  `,
})
export class App {
  chartdata!: { label: string; value: number }[];

  height = 350;
  width = 350;
  chart2: any;

  testdata = [
    { key: 'One', y: 5 },
    { key: 'Two', y: 2 },
    { key: 'Three', y: 9 },
    { key: 'Four', y: 7 },
    { key: 'Five', y: 4 },
    { key: 'Six', y: 3 },
    { key: 'Seven', y: 0.5 },
  ];

  ngOnInit(): void {
    this.chartdata = [
      { label: 'test', value: 20 },
      { label: 'test2', value: 20 },
      { label: 'test3', value: 20 },
      { label: 'test4', value: 20 },
    ];
    this.drawchart();
  }

  drawchart() {
    const nv = (window as any)['nv'];
    nv.addGraph(() => {
      const chart2 = nv.models
        .pieChart()
        .x(function (d: any) {
          return d.key;
        })
        .y(function (d: any) {
          return d.y;
        })
        //.labelThreshold(.08)
        //.showLabels(false)
        .width(this.width)
        .height(this.height)
        .donut(true)
        .id('donut2')
        .titleOffset(-30)
        .title('woot');

      // MAKES IT HALF CIRCLE
      chart2.pie
        .startAngle(function (d: any) {
          return d.startAngle / 2 - Math.PI / 2;
        })
        .endAngle(function (d: any) {
          return d.endAngle / 2 - Math.PI / 2;
        });
      const d3 = (window as any)['d3'];
      d3.select('#chart svg')
        //.datum(historicalBarChart)
        .datum(this.testdata)
        .transition()
        .duration(1200)
        .call(chart2);

      return chart2;
    });
  }
}

bootstrapApplication(App);

Stackblitz Demo

Angular相关问答推荐

Angular v12:Uncatch(in promise):ReferenceError:d3 is not defined ReferenceError:d3 is not defined

Angular路由不响应子路由的子路由

Angular 和PrimeNg文件Uploader:渲染元件时如何显示选定文件的列表?

我如何更新此Ionic应用程序以解决路由问题?

Swiper 11角17 SSR Swiper断点不工作

Angular 如何渲染ngFor?

阶段JS画布覆盖 bootstrap 模式

Angular 信号 在 effect() 中使用 debounce

在ngOninit方法中直接VS初始化属性

如果Angular 数据为空,如何设置N/A

Angular 等待对话框未显示

无法将项目从 Angular 13 更新到 14

为什么 Angular2 routerLinkActive 将活动类设置为多个链接?

无法获得 angular-material md-sidenav 的 100% 高度

ngx-toastr,Toast 未在 Angular 7 中显示

Angular 2 组件不是任何 NgModule 的一部分

@HostBinding 与 Angular 中的变量类

找不到模块angular2/core

Angular 2 输出到router-outlet

在 Angular 2 中使用逗号作为列表分隔符