javascript - 如何在 Angular 6 和 TypeScript 中使用 Highcharts Solid Gauge?

标签 javascript angular typescript highcharts gauge

在我当前的项目中,我使用 Angular 6 和 typescript。我的计划是在我的一个组件上使用 Solidgauge。在我的能力制作过程中,我遇到了以下问题:

当我尝试在显示器上显示仪表时,没有任何显示,并且在控制台 Highcharts 中显示错误 #17。我怀疑问题的原因在于我在组件中导入 Highcharts-more 的方式。

我的thoughput.component.ts文件

import { Component, OnInit, OnDestroy, Input} from '@angular/core';
import { UsageService } from '../../services/UsageService';
import { Subscription } from 'rxjs';
import * as Highcharts from 'highcharts/highcharts';
import 'highcharts/modules/exporting';
import * as more from 'highcharts-more/more';
import * as solidgauge from 'highcharts/modules/solid-gauge';
more(Highcharts);

@Component({
  selector: 'app-throughput',
  templateUrl: './throughput.component.html',
  styleUrls: ['./throughput.component.css'],
})

export class ThroughputComponent implements OnInit, OnDestroy {
  // private updataDataEndRef: Subscription = null;
  public messageCount: number;
  public chart: any;
 // @Input() usageService: UsageService;

 constructor() {
   this.messageCount = 0;
 }

 ngOnInit(): void {
   this.initChart(this.buildGauge());
 }

 ngOnDestroy() {
   // this.updataDataEndRef.unsubscribe();
 }

 // noinspection JSMethodCanBeStatic
 private buildGauge(): any {
    return {
      chart: {
        type: 'solidgauge'
      },
      title: null,
      pane: {
        center: ['50%', '85%'],
        size: '140%',
        startAngle: -90,
        endAngle: 90,
        background: {
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) 
        || '#EEE',
        innerRadius: '60%',
        outerRadius: '100%',
        shape: 'arc'
       }
    },
    tooltip: {
      enabled: false
    },
    yAxis: {
      stops: [
        [0.1, '#55BF3B'],
        [0.5, '#DDDF0D'],
        [0.9, '#DF5353']
      ],
      lineWidth: 0,
      minorTickInterval: null,
      tickAmount: 2,
      title: {
        y: -70
      },
      labels: {
        y: 16
      }
    },
    plotOptions: {
      solidgauge: {
        dataLabels: {
          y: 5,
          borderWidth: 0,
          useHTML: true
        }
      }
    }
  };
}

private initChart(gaugeOptions: any) {
  this.chart = Highcharts.chart('gauge-container', 
              Highcharts.merge(gaugeOptions, {
   yAxis: {
     min: 0,
     max: 200,
     title: {
       text: 'Speed'
     }
   },
   credits: {
     enabled: false
   },
   series: [{
     name: 'Speed',
     data: [80],
     dataLabels: {
       format: '<div style="text-align:center"><span style="font- 
                   size:25px;color:' +
                   ((Highcharts.theme && Highcharts.theme.contrastTextColor) 
                    || 'black') + '">{y}</span><br/>' +
               '<span style="font-size:12px;color:silver">km/h</span></div>'
      },
      tooltip: {
        valueSuffix: ' km/h'
      }
    }]
  }));
}

我的thoughput.component.html文件

<div id='gauge-container' style="width: 300px; height: 200px"></div>

我的app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform- 
         browser/animations';
import { MatCardModule, MatButtonModule, MatGridListModule } from        
       '@angular/material';
import { RouterModule } from '@angular/router';
import { AngularFontAwesomeModule } from 'angular-font-awesome';

import { GatewayService } from './services/GatewayService';
import { AppComponent } from './components/app/app.component';
import { GatewayComponent} from './components/gateway/gateway.component';
import { GatewayControlComponent} from 
           './components/gatewaycontrol/gatewaycontrol.component';
import { TopmenuComponent} from './components/topmenu/topmenu.component';
import { SidemenuComponent} from './components/sidemenu/sidemenu.component';
import { GatewayDataDisplayComponent} from 
       './components/gatewaydatadisplay/gatewaydatadisplay.component';
import { ThroughputComponent } from 
       './components/throughputdisplay/throughput.component';

@NgModule({
  declarations: [
     AppComponent,
     GatewayComponent,
     TopmenuComponent,
     SidemenuComponent,
     GatewayControlComponent,
     GatewayDataDisplayComponent,
     ThroughputComponent
   ],
   imports: [
     BrowserModule,
     BrowserAnimationsModule,
     MatCardModule,
     MatButtonModule,
     MatGridListModule,
     RouterModule.forRoot([
       { path: '', redirectTo: 'home', pathMatch: 'full' },
       { path: 'home', component: AppComponent },
       { path: 'control', component: GatewayControlComponent},
       { path: 'gateways', component: GatewayComponent },
       { path: '**', redirectTo: 'home' }
     ])
   ],
   providers: [GatewayService],
   bootstrap: [AppComponent]
 })
 export class AppModule { }

请有人告诉我我在这里做错了什么吗?还是有什么我忘记做的事情?

诗。在当前版本的代码中,我尝试使用 npm 分别安装 highcharts-more 和 hightcharts。

最佳答案

包括所有:

  • Highcharts
  • highcharts-更多
  • 实体测量模块

例如:

import * as Highcharts from 'highcharts/highcharts';
import * as HighchartsMore from 'highcharts/highcharts-more';
import * as HighchartsSolidGauge from 'highcharts/modules/solid-gauge';

// Now init modules:
HighchartsMore(Highcharts);
HighchartsSolidGauge(Highcharts);

关于javascript - 如何在 Angular 6 和 TypeScript 中使用 Highcharts Solid Gauge?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52073315/

相关文章:

javascript - 如果谷歌地图绘图(如onDraw)会触发什么事件

javascript - 处理有效的模态解雇和真正的异常

javascript - JSXGraph - 交叉点和从交叉点创建的多边形

使用 Material Grid 的 Angular 7 响应式布局

javascript - 可以设置PipeTransform多个filter

javascript - 使用 jQuery 删除项目上的 OnDblClick 事件

angular - 如何在 Angular 2 的 UI-Router 中更改 href 生成?

angular - Angular 4 中的嵌套表单组

reactjs - TypeScript 连接 React-Redux 装饰器和有状态组件

typescript - Angular2 导入路径在运行时错误