Running into an issue in my angular-cli 6 project where I'm using highcharts to create a solid-gauge. The error message I received can be found here. To resolve this, I had to include the highcharts-more.js file in my component.
For highcharts, I have installed the following npm packages:
- npm install highcharts
- npm install --save-dev @types/highcharts (recommended by VS Code when trying to import highcharts)
Below is the code snippet for my component along with the necessary imports:
import {
AfterViewInit,
Component,
ElementRef,
Injector,
OnInit,
ViewChild
} from '@angular/core';
import * as Highcharts from 'highcharts';
import { chart } from 'highcharts';
import highchartsMore from 'highcharts/highcharts-more';
import { AbstractDashboardCard } from '../../models/abstract-dashboard-card';
import { DashboardCard } from '../../models/dashboard-card';
highchartsMore(Highcharts);
@Component({
selector: 'app-luchtkwaliteit',
templateUrl: './luchtkwaliteit.component.html',
styleUrls: ['./luchtkwaliteit.component.css']
})
export class LuchtkwaliteitComponent extends AbstractDashboardCard
implements OnInit, AfterViewInit {
@ViewChild('chartTarget') chartTarget: ElementRef;
chart: Highcharts.ChartObject;
constructor(private injector: Injector) {
super(
injector.get(DashboardCard.metadata.NAME),
injector.get(DashboardCard.metadata.SOURCE),
injector.get(DashboardCard.metadata.COLS),
injector.get(DashboardCard.metadata.ROWS)
);
}
ngOnInit() {}
ngAfterViewInit() {
const gaugeOptions = {
chart: {
type: 'solidgauge'
},
title: null,
pane: {
center: ['50%', '85%'],
size: '140%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: '#EEE',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
// the value axis
yAxis: {
stops: [
[0.1, '#55BF3B'], // green
[0.5, '#DDDF0D'], // yellow
[0.9, '#DF5353'] // red
],
lineWidth: 0,
minorTickInterval: null,
tickAmount: 2,
min: 0,
max: 200,
title: {
y: -70,
text: 'Speed'
},
labels: {
y: 16
}
},
plotOptions: {
solidgauge: {
dataLabels: {
y: 5,
borderWidth: 0,
useHTML: true
}
}
},
credits: {
enabled: false
},
series: [
{
name: 'Speed',
data: [80],
dataLabels: {
format:
'<div style="text-align:center"><span style="font-size:25px;color: black' +
'">{y}</span><br/>' +
'<span style="font-size:12px;color:silver">km/h</span></div>'
},
tooltip: {
valueSuffix: ' km/h'
}
}
]
};
this.chart = chart(this.chartTarget.nativeElement, gaugeOptions);
}
}
I've tried various ways to add highcharts-more but encountered roadblocks. Here's what I tried:
- npm install highcharts-more (deprecated so avoided using it) Link
- import * as highchartsMore from 'highcharts/highcharts-more'; Received TS error "node_modules/@types/highcharts/highcharts-more" resolves to a non-module entity and cannot be imported."
- import * as highchartsMore from 'highcharts'; TS Error on highchartsMore(Highcharts); Cannot invoke an expression whose type lacks a call signature.
- highcharts-angular (not used due to compatibility issues with angular 6) Link