我正在按角度重现其中一个有向图,并修复了多个错误之后,我陷入了以下一个我似乎无法自行解决的错误:错误错误:“未捕获(承诺):TypeError :_amcharts_amcharts4_core__WEBPACK_IMPORTED_MODULE_3 __。useTheme不是函数
我更新了无法修复的amcharts模块。我从amcharts网站下载了该模块,并替换了模块中的文件,但仍无法修复。我认为我的进口货很好。
<div id="chartdiv" style="width: 100%; height: 500px"></div>
import { CapabilitiesService } from './../../services/capabilities.service';
import { Component, OnInit, NgZone } from '@angular/core';
import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
import am4themes_animated from "@amcharts/amcharts4/themes/animated";
import am4plugins_forceDirected from "@amcharts/amcharts4/plugins/forceDirected";
@Component({
selector: 'app-directed-graph',
templateUrl: './directed-graph.component.html',
styleUrls: ['./directed-graph.component.css']
})
export class DirectedGraphComponent implements OnInit {
allSystemsData = new Array < {} > ();
linksBetweenSystems = new Array < {} > ();
connectedSystems = new Array < {} > ();
constructor(
private capabilitiesService : CapabilitiesService
) { }
async ngOnInit() {
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4plugins_forceDirected.ForceDirectedTree);
var networkSeries = chart.series.push(new am4plugins_forceDirected.ForceDirectedSeries())
networkSeries.dataFields.linkWith = "linkWith";
networkSeries.dataFields.name = "name";
networkSeries.dataFields.id = "name";
networkSeries.dataFields.value = "value";
networkSeries.dataFields.children = "children";
networkSeries.maxRadius = am4core.percent(1);
networkSeries.nodes.template.label.text = "{name}"
networkSeries.fontSize = 8;
networkSeries.linkWithStrength = 0.1;
var nodeTemplate = networkSeries.nodes.template;
nodeTemplate.tooltipText = "{name}";
nodeTemplate.adapter.add("tooltipText", (text,target) => {
if(target.dataItem) {
switch(target.dataItem.level){
case 0:
return "name : {name}";
case 1:
return "name : {name}\nparent : {parent.name}";
}
}
return text;
})
nodeTemplate.fillOpacity = 1;
nodeTemplate.label.hideOversized = true;
nodeTemplate.label.truncate = true;
var linkTemplate = networkSeries.links.template;
linkTemplate.strokeWidth = 2;
var linkHoverState = linkTemplate.states.create("hover");
linkHoverState.properties.strokeOpacity = 1;
linkHoverState.properties.strokeWidth = 2;
nodeTemplate.events.on("over", function (event) {
var dataItem = event.target.dataItem;
dataItem.childLinks.each(function (link) {
link.isHover = true;
})
})
nodeTemplate.events.on("out", function (event) {
var dataItem = event.target.dataItem;
dataItem.childLinks.each(function (link) {
link.isHover = false;
})
})
networkSeries.maxLevels = 1;
networkSeries.data = [
{
"name":"Phoebe",
"value":102,
"children":[
{
"name":"Paolo2",
"value":1
},
{
"name":"Pete",
"value":10
},
{
"name":"Chip",
"value":1
},
{
"name":"Timothy (Burke)",
"value":1
},
{
"name":"Emily",
"value":17
},
{
"name":"Dr. Roger",
"value":3
}
]
},
{
"name":"Ross",
"value":216,
"children":[
{
"name":"Carol",
"value":10
},
{
"name":"Celia",
"value":2
},
{
"name":"Julie",
"value":6
},
{
"name":"Chloe",
"value":1
},
{
"name":"Bonnie",
"value":4
},
{
"name":"Messy Girl (Cheryl)",
"value":5
},
{
"name":"Jill",
"value":1
},
{
"name":"Charlie",
"value":13
}
]
},
{
"name":"Chandler",
"value":167,
"children":[
{
"name":"Aurora",
"value":2
},
{
"name":"Jill Goodacre",
"value":1
},
{
"name":"Janice",
"value":12
},
{
"name":"Mrs Bing",
"value":6
},
{
"name":"Janine",
"value":9
},
{
"name":"Erin",
"value":1
},
{
"name":"Cecilia",
"value":3,
"linkWith":[
"Gunther"
]
}
]
}
];
}
}
输出应该与以下内容非常相似:https://codepen.io/pen/?&editable=true
答案 0 :(得分:0)
不确定是什么导致了您的错误,但是我认为您需要将am4core.useTheme(am4themes_animated);
放在类之外-将其放在导入内容的下方。
另外,我给您的印象是您必须使用NgZone并在angular之外运行。他们在这里有很好的文档。
https://www.amcharts.com/docs/v4/getting-started/integrations/using-angular2/
答案 1 :(得分:0)
这里am4plugins_forceDirected
没有被导入,因此只需导入即可。
您可以在本Git问题https://github.com/amcharts/amcharts4/issues/1473
使用此导入
import * as am4plugins_forceDirected
from"@amcharts/amcharts4/plugins/forceDirected";