如何在Angular上使用Highchart?

时间:2019-02-15 17:28:45

标签: javascript angular highcharts highcharts-ng

我想在角度上使用 Highchart 制作图表,但由于缺乏有关 Highchart 使用情况的信息,因此我坚持使用该图表Angular 在Highchart的官方网站上。

这是queue-dashboard.component.ts上的 highchart 库:

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() queue-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-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>

1 个答案:

答案 0 :(得分:-1)

有多种使用HTML呈现图表的方法。将尝试解释其中的几个

1。我们可以通过在图表配置对象中指定元素名称来在元素上呈现图表。 因此,假设您想在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';

导出类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>

highcharts的css可能无法完全占据高度和宽度。要添加,可以在组件css / scss中添加此css。

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

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

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