0

got a very little issue but I can't figure out, I'm trying to inject "operating_rate" value from a Json in my pie chart using angular2-highchart, rxjs and Angular 4 here's my code (i think it's a format issue because when I did so with "operating_details" it do the work) :

pieChart.ts:

import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { UserService3 } from '../user3.service';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';

@Component({
  selector: 'my-daydetail',
  templateUrl: './my-daydetail.component.html',
  styleUrls: ['./my-daydetail.component.css']
})
export class MyDaydetailComponent implements OnDestroy, OnInit {



// Içi je crée un un import depuis le composent appliV2.html
    @Input() showMePartially: boolean;



    options: any;
    data: Object[];
    chart: any;
// Ici j'importe la variable Subscription de l'api Rxjs que je stock dans une variable
    dataSubscription: Subscription;


     constructor(public userService3: UserService3) {

           this.options = {
            chart: {  type: 'pie',
                 plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,  },
   //     legend: { enabled: false },
        credits: { enabled: false },
         tooltip: {
  //     pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
   },
   plotOptions: {
    pie: {
        allowPointSelect: false,
        cursor: 'pointer',
        dataLabels: {
            enabled: false,
     //      format: '<b>{point.name}</b>: {point.percentage:.1f} %',
        }
      }
    },


        series: [{
          name: 'Dispo',
          data: []
        }]
       };
   }



   saveInstance(chartInstance) {
    this.chart = chartInstance;
 //   console.log(chartInstance);
}

   public ngOnInit () {
    this.dataSubscription = 
this.userService3.getData().subscribe((data) => {
      this.options.series[0].data = data.data.operating_rate;
     console.log(data);
   });
}
    public ngOnDestroy() {
      if (this.dataSubscription){
this.dataSubscription.unsubscribe();
}
}
}

pieChart.html:

  <chart [options]="options" (load)="saveInstance($event.context)">

a part of my Json: {"status":"OK","data": {"operating_rate":88.14,"operating_duration":"20h02mn", "unoperating_durati on":"2h40mn","operating_details":[[1497837618,0],[1497837738,0], [1497837858,0],[1497837978,0],[1497838098,0],[1497838218,0], [1497838338,0],[1497838458,0],[1497838578,0],[1497838698,0], [1497838818,0],[1497838938,0],[1497839058,0],[1497839178,0], [1497839298,0],[1497839418,0],[1497839538,0],[1497839658,0], [1497839778,0],[1497839898,0],[1497840018,0]]}

Here's my service.ts:

   getData() {

     const tick3$ = Observable.timer(100, 60000);

     return tick3$.flatMap(() => this.http.get(usersURL)).map(res => 
res.json());   // .publishBehavior(<User3[]>[]).refCount();



 } 
Emile Cantero
  • 1,943
  • 3
  • 22
  • 47

1 Answers1

1

I believe you want something along the lines of

// series data of your highcharts options
this.options.series[0].data = [
    {
        name: 'Operating Duration',
        y: data.data.operating_rate // this is the response from your json call
    },
    {
        name: 'Non-Operating Duration',
        y: 100 - data.data.operating_rate
    }
];

as per the highcharts documentation http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/pie-basic/

LLai
  • 13,128
  • 3
  • 41
  • 45
  • hey my friend how are you? I make it work at the end last time :) so now it's just a pie but y: 100 - res.data.operating_rate doesn't work it says me cannot find 'res' – Emile Cantero Jun 28 '17 at 14:01
  • @EmileCantero I am good! can you post your code where you are getting res? – LLai Jun 28 '17 at 14:04
  • @EmileCantero I've updated my answer to use data instead of res. you are using "data" instead of "res" in your subscribe callback. – LLai Jun 28 '17 at 14:13
  • series: [{ name: 'Dispo', data: [ ===> Should I put your code here? ] }] – Emile Cantero Jun 28 '17 at 14:24
  • @EmileCantero correct! this will tell highcharts what to render in the piechart – LLai Jun 28 '17 at 14:26
  • youir code looks great but i still got an error with dat(highlighted red) ===> [ts] Cannot find name 'data'. Did you mean the instance member 'this.data'? – Emile Cantero Jun 28 '17 at 14:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147840/discussion-between-emile-cantero-and-llai). – Emile Cantero Jun 28 '17 at 14:33