我只是从Angular2开始,我正在尝试将Fusion charts与Angular2集成。我已经安装了所有必需的模块(@types/fusioncharts和fusioncharts)。以下是Fusionchart组件代码。
import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import * as FusionCharts from "FusionCharts";
import '../../node_modules/fusioncharts/fusioncharts.charts.js';
@Component({
moduleId: module.id.toString(),
templateUrl: 'fusioncharts2.component.html'
})
export class FusionChartsComponent2 {
revenueChart: any;
constructor() {
}
ngAfterViewInit() {
this.chartConfig()
}
public chartConfig() {
this.revenueChart = new FusionCharts({
type: 'column3d',
renderAt: 'chart-container',
width: '500',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Monthly revenue for last year",
"subCaption": "Harry's SuperMart",
"xAxisName": "Month",
"yAxisName": "Revenues (In USD)",
"paletteColors": "#0075c2",
"valueFontColor": "#ffffff",
"baseFont": "Helvetica Neue,Arial",
"captionFontSize": "14",
"subcaptionFontSize": "14",
"subcaptionFontBold": "0",
"placeValuesInside": "1",
"rotateValues": "1",
"showShadow": "0",
"divlineColor": "#999999",
"divLineIsDashed": "1",
"divlineThickness": "1",
"divLineDashLen": "1",
"divLineGapLen": "1",
"canvasBgColor": "#ffffff"
},
"data": [
{
"label": "Jan",
"value": "420000"
},
{
"label": "Feb",
"value": "810000"
},
{
"label": "Mar",
"value": "720000"
},
{
"label": "Apr",
"value": "550000"
},
{
"label": "May",
"value": "910000"
},
{
"label": "Jun",
"value": "510000"
},
{
"label": "Jul",
"value": "680000"
},
{
"label": "Aug",
"value": "620000"
},
{
"label": "Sep",
"value": "610000"
},
{
"label": "Oct",
"value": "490000"
},
{
"label": "Nov",
"value": "900000"
},
{
"label": "Dec",
"value": "730000"
}
]
}
});
this.revenueChart.render();
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { InputTextModule, DataTableModule, ButtonModule, DialogModule } from 'primeng/primeng';
// Import angular2-fusioncharts
import { FusionChartsModule } from 'angular2-fusioncharts';
// Import FusionCharts library
import * as FusionCharts from 'fusioncharts';
// Import FusionCharts Charts module
import Charts = require('fusioncharts/fusioncharts.charts');
// used to create fake backend
import { fakeBackendProvider } from './_helpers/index';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { BaseRequestOptions } from '@angular/http';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { AlertComponent } from './_directives/index';
import { AuthGuard } from './_guards/index';
import { AlertService, AuthenticationService, UserService,
HeroSearchService, HeroService, InMemoryDataService } from
'./_services/index';
import { CarService } from './cars/carservice';
import { HomeComponent } from './home/index';
import { DashboardComponent, HeroDetailComponent, HeroSearchComponent,
HeroesComponent } from './dashboard/index';
import { AppChildComponent, ParentComponent } from
'./parentchild/index';
import { ContentComponent } from './content/index';
import { DataTableComponent } from './datatable/index';
import { FusionChartsComponent } from './fusioncharts/index';
import { FusionChartsComponent2 } from './fusioncharts2/index';
import { JqDataTableComponent } from './jqdatatable/index';
import { JsTreeComponent } from './jstree/index';
import { LoginComponent } from './login/index';
import { RegisterComponent } from './register/index';
import { HashLocationStrategy, LocationStrategy } from
'@angular/common';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService, {
passThruUnknownUrl: true }),
routing,
InputTextModule,
DataTableModule,
ButtonModule,
DialogModule,
FusionChartsModule.forRoot(FusionCharts, Charts)
],
declarations: [
AppComponent,
AlertComponent,
HomeComponent,
DashboardComponent,
FusionChartsComponent,
FusionChartsComponent2,
HeroDetailComponent, HeroSearchComponent, HeroesComponent,
AppChildComponent, ParentComponent,
ContentComponent,
DataTableComponent,
JqDataTableComponent,
JsTreeComponent,
LoginComponent,
RegisterComponent
],
providers: [
AuthGuard,
AlertService,
AuthenticationService,
UserService,
HeroSearchService,
HeroService,
CarService,
// providers used to create fake backend
fakeBackendProvider,
MockBackend,
BaseRequestOptions,
{ provide: LocationStrategy, useClass: HashLocationStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule { }
我执行代码时遇到错误。
vendor.js:28488 [HMR] Waiting for update signal from WDS...
vendor.js:4073 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
app.js:27495 GET http://localhost:9966/fusioncharts.charts.js (404)
vendor.js:28300 [WDS] Hot Module Replacement enabled.
如果我通过脚本将index.html中的Fusionchart CDN包含在内,或者如果app文件夹中存在Fusionchart.js,则该错误已修复。尽管Fusionchart.js代码位于应用程序捆绑包中,但它始终会在根应用程序文件夹中查找Fusionchart.js。甚至我也尝试通过指向node_modules来进行导入,例如“ import'../../node_modules/fusioncharts/fusioncharts.charts.js';”。仍然没有效果。通过html中的脚本标签包含js的正确方法吗?还有其他方法可以解决此错误吗?
最佳答案
我遇到了同样的问题。您可以通过更新app.module.ts来解决此问题
从更新这部分
// Import FusionCharts Charts module
import Charts = require('fusioncharts/fusioncharts.charts');
至
// Import FusionCharts Charts module
import * as Charts from 'fusioncharts/fusioncharts.charts';
FusionChartsModule.fcRoot(FusionCharts, Charts);
是的,这将不起作用“ import'../../node_modules/fusioncharts/fusioncharts.charts.js';”因为这不是导入FusionCharts的正确方法
您也可以参考正确用法的更新文档
https://www.fusioncharts.com/dev/getting-started/angular/angular/configure-your-chart-using-angular