如何在 Angular 上使用 Highchart?

How to use Highchart on Angular?

我想在 angular 上使用 Highchart 制作图表,但由于缺少有关 [=23= 的信息,我不得不使用它]Highchart用法见Highchart官网Angular

这是队列中的 highchart 库-dashboard.component.ts:

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { CountryunitService } from '../../countryunit.service';
import * as Highcharts from 'highcharts';
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { map } from 'lodash';

这是ngOnInit()队列里面的内容-dashboard.component.ts

ngOnInit() {

  var chart = Highcharts.chart('container', {

    title: {
      text: 'Chart.update'
    },

    subtitle: {
      text: 'Plain'
    },

    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    series: [{
      type: 'column',
      colorByPoint: true,
      data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
      showInLegend: false
    }]

  });

调用这个queue里面的chart怎么办-dashboard.component.html?

<div eds-tile class="xl-6" style="height: 350px">
<eds-tile-title>Count of Ticket ID by CU ID</eds-tile-title>
<!-- Highchart On This -->
</div>

有多种方法可以使用 HTML 呈现图表。将尝试解释其中的一些

1.We 可以通过在图表配置对象中指定元素名称来在元素上呈现图表。 因此,假设您想在 HTML 文件中提到的 div 上呈现图表。

<div eds-tile class="xl-6" style="height: 350px">
<eds-tile-title>Count of Ticket ID by CU ID</eds-tile-title>
<div id="chartContainer"></div>
<!-- Highchart On This -->
</div>

您可以通过在元素上渲染来配置图表对象

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { CountryunitService } from '../../countryunit.service';
import * as Highcharts from 'highcharts';
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { map } from 'lodash';

导出class YourComponentName 实现 OnInit{

setTimeout( ()=>{
  this.Highcharts.chart({
   chart: {
      type: 'column',
      renderTo:'chartContainer',
  },
  title: {
      text: 'Chart.update'
  },

  subtitle: {
      text: 'Plain'
  },

  xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  },

  series: [{
      type: 'column',
      colorByPoint: true,
      data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
      showInLegend: false
  }]

})},3000);
  1. 另一种方法是您可以动态创建图表配置对象并绑定到您尝试绘制的容器内。

    this.chartConfig = {
     title: {
      text: 'Chart.update'
      },
    
     subtitle: {
      text: 'Plain'
      },
    
     xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',    'Oct', 'Nov', 'Dec']
      },
    
     series: [{
      type: 'column',
      colorByPoint: true,
      data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
      showInLegend: false
      }]
    }
    

并且在您的 HTMl 中您可以动态绑定图表(指令)

 <div eds-tile class="xl-6" style="height: 350px">
<eds-tile-title>Count of Ticket ID by CU ID</eds-tile-title>
<div id="chartContainer">
<chart [options]="chartConfig"></chart>
 </div>
<!-- Highchart On This -->
</div>

有可能 css 的 highcharts 没有全高和 width.To 添加你可以添加这个 css 在你的组件 css/scss.

chart {
 width: 100% !important;
 margin: 0 auto;
 display: block;
}

.highcharts-root {
  width: 100% !important;
  display: block;
  } 

 .highcharts-container {
   width: 100% !important;
  }