-1

I want to make a chart with Highchart on my angular, but I'm stuck to work with it because of lack of information regarding Highchart usage in Angular on the official website of Highchart.

This is highchart library on queue-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';

And this is the content inside 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
    }]

  });

What should I do, if want to make a call the chart inside this 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>
Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
aldi
  • 463
  • 1
  • 4
  • 19

1 Answers1

-1

There are multiple ways to render chart using HTML. Will try to explain few of them

1.We can render the chart on element by specifying the element name in chart config object. So suppose you want to render the chart on div you mentioned in HTML file.

<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>

You can configure the chart object by rendering on the element

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';

export class YourComponentName implements 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. Another way is you can create dynamically chart config object and bind inside the container you are trying to plot.

    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
      }]
    }
    

and in your HTMl you can dynamically bind on chart(directive)

 <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>

There could be possibility of css of highcharts of not taking full height and width.To add on you can add this css in your component css/scss.

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

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

 .highcharts-container {
   width: 100% !important;
  }
shikhar
  • 110
  • 10
  • let me know for any errors you face, will you help you out or give good links that can help you out – shikhar Feb 15 '19 at 18:01
  • wait @shikhar, on going try it – aldi Feb 15 '19 at 18:02
  • Hey sir @shikhar, the first solution that u gave actually works, but still got an error "error TS2304: Cannot find name 'require'." – aldi Feb 15 '19 at 18:08
  • @aldi you can directly import, sorry to mention the require method and for the second method you need to install npm package named "angular-highcharts" Refer this for the same https://www.npmjs.com/package/angular-highcharts – shikhar Feb 15 '19 at 18:21