javascript Angular 2 Fusion Chart集成

sshcrbum  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(83)

我刚刚开始使用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中包含fusionchartcdn,或者fusionchart.js存在于app文件夹中,则该错误已修复。虽然fusionchart.js代码在应用程序包中,但它总是在根应用程序文件夹中查找fusionchart.js。甚至我尝试通过指向node_modules导入一些类似这样的东西“***import '../../node_modules/fusioncharts/fusioncharts.charts.js';”““没有效果。在html中通过script标签包含js是正确的方法吗?有没有其他方法可以解决这个错误?

k4emjkb1

k4emjkb11#

我也遇到过同样的问题。您可以通过更新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正确用法的更新文档

sc4hvdpw

sc4hvdpw2#

你可以尝试执行下面的代码:
//在app.module.ts中需要设置

import { NgModule, enableProdMode } from '@angular/core'
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { FusionChartsModule } from 'angular-fusioncharts';

// Load FusionCharts
import * as FusionCharts from 'fusioncharts';
// Load Charts module
import * as Charts from 'fusioncharts/fusioncharts.charts';
// Load fusion theme
import * as FusionTheme from 'fusioncharts/themes/fusioncharts.theme.fusion';

// Add dependencies to FusionChartsModule
FusionChartsModule.fcRoot(FusionCharts, Charts, FusionTheme)

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FusionChartsModule
  ],
  providers: [
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

//在app.component.ts中

import { Component } from '@angular/core';

@Component({
    selector: 'app',
    templateUrl: 'app.component.html'
})
export class AppComponent { 
    dataSource: Object;
    chartConfig: Object;

    constructor() {
         this.chartConfig = {
            width: '700',
            height: '400',
            type: 'column2d',
            dataFormat: 'json',
        };

        this.dataSource = {
            "chart": {
              "caption": "Countries With Most Oil Reserves [2017-18]",
              "subCaption": "In MMbbl = One Million barrels",
              "xAxisName": "Country",
              "yAxisName": "Reserves (MMbbl)",
              "numberSuffix": "K",
              "theme": "candy",
            },
            "data": [{
              "label": "Venezuela",
              "value": "290"
            }, {
              "label": "Saudi",
              "value": "260"
            }, {
              "label": "Canada",
              "value": "180"
            }, {
              "label": "Iran",
              "value": "140"
            }, {
              "label": "Russia",
              "value": "115"
            }, {
              "label": "UAE",
              "value": "100"
            }, {
              "label": "US",
              "value": "30"
            }, {
              "label": "China",
              "value": "30"
            }]
          };

    }
}

//在app.component.html中

<fusioncharts
    [chartConfig]=chartConfig
    [dataSource]=dataSource >
</fusioncharts>

文件参考-https://www.fusioncharts.com/angular2-js-charts?framework=angular2

相关问题