首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >带角度的实时图表2-highcharts

带角度的实时图表2-highcharts
EN

Stack Overflow用户
提问于 2017-02-25 21:45:19
回答 5查看 4.7K关注 0票数 5

我想在我的angular2应用程序中插入实时图表,我正在使用angular2-highcharts。

代码语言:javascript
代码运行次数:0
运行
复制
npm install angular2-highcharts --save

app.component.ts

代码语言:javascript
代码运行次数:0
运行
复制
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'app works!';
  options: Object;
  constructor() {
    this.options = {
      title : { text : 'simple chart' },
      series: [{
        data: [29.9, 71.5, 106.4, 129.2],
      }]
    };
  }

}

app.component.html

代码语言:javascript
代码运行次数:0
运行
复制
<chart [options]="options"></chart>

app.module.ts

代码语言:javascript
代码运行次数:0
运行
复制
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import {ChartModule} from "angular2-highcharts";

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

在运行我的angular2应用程序时,我遇到了这个错误:"No provider for HighchartsStatic!“提前谢谢。

EN

回答 5

Stack Overflow用户

发布于 2017-04-10 09:10:31

我遇到了这个问题。来自matthiaskomarekThis solution为我做到了这一点:

代码语言:javascript
代码运行次数:0
运行
复制
import { ChartModule } from 'angular2-highcharts';
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';

declare var require: any;
export function highchartsFactory() {
  return require('highcharts');
}

@NgModule({
  imports: [
      ChartModule
  ],
  declarations: [ AppComponent ],
  exports: [],
  providers: [
    {
      provide: HighchartsStatic,
      useFactory: highchartsFactory
    },
  ],
  bootstrap: [AppComponent]
})
export class AppModule

我不确定为什么这个问题被否决了,因为这真的是一个问题。我在这里遇到了完全相同的事情!

highchart文档表明,ChartModule.forRoot(require('highcharts')是所需的全部内容(当然,填充文档中缺少的))。对于angular-cli项目,我永远不能让require工作,所以declare var require: any;通常可以做到这一点。除了这里,在forRoot的上下文中调用它似乎会导致:

静态解析符号值时遇到错误,出现

错误。对本地(非导出的)符号“require”的引用。考虑导出符号(原始.ts文件中的位置18:14 ),在...中解析symbol AppModule ...app/app.module.ts

我的猜测是matthiaskomarek的变通方法避免了这一点。

票数 11
EN

Stack Overflow用户

发布于 2017-02-25 21:55:35

您需要在导入中使用ChartModule.forRoot()

代码语言:javascript
代码运行次数:0
运行
复制
@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    ChartModule.forRoot(require('highcharts'))  // <-- HERE
  ],
  // ...
})
export class AppModule { }
票数 7
EN

Stack Overflow用户

发布于 2017-04-21 23:17:31

如果Marc Brazeau的解决方案对你不起作用(它对我不起作用),另一个解决办法是执行以下操作:

代码语言:javascript
代码运行次数:0
运行
复制
import {ChartModule} from 'angular2-highcharts';
import {HighchartsStatic} from 'angular2-highcharts/dist/HighchartsService';
import * as highcharts from 'highcharts';

@NgModule({
    declarations: [AppComponent],
    imports: [ChartModule],
    providers: [
    {
        provide: HighchartsStatic,
        useValue: highcharts
    }],
    bootstrap: [AppComponent]
})
export class AppModule {
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42456855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档