0

有一个很小的问题,但我不知道,我正在尝试使用 angular2-highchart、rxjs 和 Angular 4 从我的饼图中的 Json 中注入“operating_rate”值,这是我的代码(我认为这是一个格式问题,因为当我使用“operating_details”这样做时,它就完成了工作):

饼图.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();
}
}
}

饼图.html:

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

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

这是我的service.ts:

   getData() {

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

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



 } 
4

1 回答 1

1

我相信你想要一些类似的东西

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

根据 highcharts 文档http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/pie-basic/

于 2017-06-28T13:47:29.613 回答