mac5562
Posts: 9
Joined: Thu Aug 25, 2022 6:59 am

Highcharts map with series data from API problem in Angular

Hi !

For practice i started Highcharts map project in Angular, and i have a issue with data processing and probably thanks to that showing nothing in page. Data relatively large (3 MB) and coming from a API, so when page is loaded it will be not load the map part right away. The data is automatically converting to JSON and loading to a variable.

I share my code:

map.service.ts:

Code: Select all

import { Injectable } from '@angular/core';
import { Subject,firstValueFrom } from 'rxjs';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { JsonConvert, OperationMode, ValueCheckingMode } from 'json2typescript';

const headers= new HttpHeaders()
.set('Content-Type', 'text/html;charset=UTF-8')
//.set('Access-Control-Allow-Origin', "*")


@Injectable({
  providedIn: 'root'
})
export class MapService {

  jsonConvert: JsonConvert;
  mapSubject: Subject<any> = new Subject();
 

  constructor(private httpClient: HttpClient) {
    this.jsonConvert = new JsonConvert();
    this.jsonConvert.operationMode = OperationMode.ENABLE;
    this.jsonConvert.ignorePrimitiveChecks = false;
    this.jsonConvert.valueCheckingMode = ValueCheckingMode.DISALLOW_NULL;
  }

  getMap(){
    var result = "";
    let params = new HttpParams().set('inputdate', '2023-02-03')
    .set('currpage', 'machine_map.jsp');
    firstValueFrom(this.httpClient.get<string>('url',{params:params}))
    .then(res=>{

      /*let data=res;
      console.log(data)*/
      this.mapSubject.next(res);
    })
         
    }
}
map.component.ts

Code: Select all

import { Component, OnInit } from '@angular/core';
import * as Highcharts from 'highcharts/highmaps';
import {Subscription } from 'rxjs';
import { MapService } from 'src/app/map.service';


var worldMap = require("@highcharts/map-collection/custom/world.geo.json");



@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit {
  isHighcharts = typeof Highcharts === 'object'; // new
  title = 'test';
  Highcharts: typeof Highcharts = Highcharts;
  chartConstructor: string = 'mapChart';
  chartOptions: Highcharts.Options = {};
  private subscriptions: Subscription = new Subscription();
  data: any;

  constructor(private mapService: MapService) { }

  ngOnInit(): void {

    this.mapService.getMap();
    this.mapService.mapSubject.subscribe(response => {
      this.data = response;
      this.showMap(this.data);
    },
    );
  }

  showMap(data: any): void {
    console.log(data);
    this.chartOptions = {
      /*chart: {
         map: worldMap
       
       },*/
      title: {
        text: "Highmaps basic demo"
      },
      subtitle: {
        text:
          'Source map: <a href="http://code.highcharts.com/mapdata/custom/world.js">World, Miller projection, medium resolution</a>'
      },
      mapNavigation: {
        enabled: true,
        buttonOptions: {
          alignTo: "spacingBox"
        }
      },
      legend: {
        enabled: true
      },
      colorAxis: {
        min: 0
      },
      series: data
      
    };
  }
}

map.component.html

Code: Select all

<highcharts-chart
  id="container" 
  [Highcharts]="Highcharts"
  [constructorType]="chartConstructor"
  [options]="chartOptions"
  style="width: 100%; height: 100%; display: block;"
></highcharts-chart>
What am i doing wrong ?
User avatar
dawid.d
Posts: 832
Joined: Thu Oct 06, 2022 11:31 am

Re: Highcharts map with series data from API problem in Angular

Hi,

Thanks for contacting us with your question!

Could you create a demo of your problem in e.g. Stackblitz? It will be easier for me to help then. You can use this template that implements Highcharts Maps example: https://stackblitz.com/edit/highcharts- ... mponent.ts

I'm waiting for your reply
Best regards!
Dawid Draguła
Highcharts Developer
mac5562
Posts: 9
Joined: Thu Aug 25, 2022 6:59 am

Re: Highcharts map with series data from API problem in Angular

dawid.d wrote: Tue Feb 07, 2023 5:38 pm Hi,

Thanks for contacting us with your question!

Could you create a demo of your problem in e.g. Stackblitz? It will be easier for me to help then. You can use this template that implements Highcharts Maps example: https://stackblitz.com/edit/highcharts- ... mponent.ts

I'm waiting for your reply
Best regards!
Hi !

Sorry for the wait. I recreated the basic in StackBlitz. I hope it helps

https://stackblitz.com/edit/angular-bdp ... mponent.ts
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Highcharts map with series data from API problem in Angular

In the constructor put this.chartOptions and then you can get data, here is a simplified managing data service example.
https://stackblitz.com/edit/angular-bdp ... service.ts

In order to have communication with the data and the chart, the best solution is to put it in the constructor and then with the help of the return function make changes to the data. Well, unless you put it directly from the service as in the first example, but in this option you would have to rewrite data from JSON.

For process the data and add data, you can use setData or update for each series separately.
https://api.highcharts.com/class-refere ... es#setData
https://api.highcharts.com/class-refere ... ies#update

https://stackblitz.com/edit/angular-bdp ... mponent.ts




Let me know how are you going with this
Best regards
Sebastian Hajdus
Highcharts Developer
mac5562
Posts: 9
Joined: Thu Aug 25, 2022 6:59 am

Re: Highcharts map with series data from API problem in Angular

sebastian.h wrote: Tue Feb 14, 2023 1:27 pm In the constructor put this.chartOptions and then you can get data, here is a simplified managing data service example.
https://stackblitz.com/edit/angular-bdp ... service.ts

In order to have communication with the data and the chart, the best solution is to put it in the constructor and then with the help of the return function make changes to the data. Well, unless you put it directly from the service as in the first example, but in this option you would have to rewrite data from JSON.

For process the data and add data, you can use setData or update for each series separately.
https://api.highcharts.com/class-refere ... es#setData
https://api.highcharts.com/class-refere ... ies#update

https://stackblitz.com/edit/angular-bdp ... mponent.ts




Let me know how are you going with this
Best regards
Hi !

Firstly thank you for your help. I checked your solutions but i have problems with them.

My problem with the first solution is i trying to load the from resource file, but still showing nothing. I modified few things:
https://stackblitz.com/edit/angular-bdp ... mponent.ts

In the case of the second solution, i tried to use the setData method but it's not working and with the update method, i don't know how to gave the data here:
https://stackblitz.com/edit/angular-bdp ... mponent.ts
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Highcharts map with series data from API problem in Angular

Hi,

To do this you need to change the order so that the chart is only done after the data is loaded in the component above, for example, or do the update after the data is already loaded.
This looks like an Angular problem and doesn't look too much like a Highcharts problem - more the order of loading data into the component which should wait asynchronously for a response before setting all inputs in the component or use update when changing inputs dynamically.

Best regards
Sebastian Hajdus
Highcharts Developer

Return to “Highcharts Maps”