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);
...
});
}