【问题标题】:implementing HighCharts-xrange in angular4在 angular4 中实现 HighCharts-xrange
【发布时间】:2018-07-25 06:31:02
【问题描述】:

我正在尝试使用 xrange 类型的 HighCharts:

import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
import { HighchartsStatic } from '../../../../node_modules/angular2-highcharts/dist/HighchartsService';

export function highchartsFactory() {
  const hc = require('highcharts');
  const dd = require('highcharts/modules/xrange');
  dd(hc);

  return hc;
} 
@NgModule({
  imports: [
    /* case 1 */
    ChartModule 
    // in this case the code compiles but I get Error 17 in console

    /* case 2 */
    /* ChartModule.forRoot(require('highcharts'), require('highcharts/modules/xrange')) */
    // in this case the code doesn't compiles with the error:
    // "Error encountered resolving symbol values statically. 
    //  Calling function 'ChartModule', function calls are not supported."
  ],
  providers: [
    {
        provide: HighchartsStatic,
        useValue: highcharts,
        useFactory: highchartsFactory
    }
})

如这两种情况所示,我在创建图表时遇到了问题。有人告诉我,第一种情况是更好的方法,我想继续朝这个方向发展。

我看到很多帖子都使用这种方法,但都没有提到这个错误。

【问题讨论】:

  • 使用官方 highcharts npm 检查这个实现stackblitz.com/edit/angular4-highcharts-x-range?file=app/…
  • 你安装了 highchart typings 吗?
  • 我得到错误:xrange_1.default is not a function TypeError: xrange_1.default is not a function@Deep 3015
  • 是的,我安装了 highchart @ Chellappan
  • @AmichayAtias 你使用的是官方的 highcharts npm 而不是 angular2-highcharts

标签: angular highcharts


【解决方案1】:

我建议你像example那样使用官方的highcharts npm包,但你想使用angular2-highcharts。

更新你的应用模块

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';

import { ChartModule } from 'angular2-highcharts';
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';
import Highcharts from 'highcharts';
import Exporting from 'highcharts/modules/exporting';
import xrange from "highcharts/modules/xrange";
xrange(Highcharts);
Exporting(Highcharts);
export function highchartsFactory() {
  return Highcharts;
}
@NgModule({
  imports:      [ BrowserModule, FormsModule , ChartModule],
  declarations: [ AppComponent, HelloComponent ],
  providers: [
    { provide: HighchartsStatic, useFactory: highchartsFactory } // add as factory to your providers
  ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Stackblitz演示

【讨论】:

  • 当我导入这个时:import Highcharts from 'highcharts'; 被输入错误:has no default export. 如果我这样做import * as Highcharts from 'highcharts'; 被抛出这个错误TypeError: xrange_1.default is not a function TypeError: xrange_1.default is not a function
  • 你说我可以在没有angular2-highcharts. 的情况下使用它,如果可以,请告诉我我是如何尝试所有可能的方式而不是成功
  • 我刚刚创建了一个新项目,就像您发送给我的示例中一样,当我将鼠标悬停在导入 highcharts 上时出现此错误:[ts] Could not find a declaration file for module 'highcharts/modules/xrange'. 'c:/Users/User/my-app/node_modules/highcharts/modules/xrange.js' implicitly has an 'any' type. Try npm install @types/highcharts` 如果它存在或添加包含declare module 'highcharts'; 的新声明 (.d.ts) 文件。项目已编译但图表未构建,我在控制台中收到此消息:xrange_1.default is not a function@Deep 3015
  • 您使用 angular2-highcharts 或不使用哪个示例
  • @ Deep 3015 没有 angular2-highcharts
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多