简体   繁体   English

Jhipster:数据加载可能太慢

[英]Jhipster : data loading may be too slow

I have some trouble making my chart work. 使图表无法正常工作。 I'm a creating a chart using ng2-charts. 我正在使用ng2-charts创建图表。 I load my datas using a service, then i display them into a chart. 我使用服务加载数据,然后将它们显示在图表中。 The problem is, datas are correctly displayed only if I add an alert() between the moment where i'm taking the datas from my service, and the moment where I set the chart datas. 问题是,只有在我从服务中获取数据的那一刻到设置图表数据的那一刻之间添加了alert(),数据才能正确显示。

lightData: LightData[];
public lineChartData = Array<any>();
public lineChartLabels = Array<any>();

ngOnInit() {
    this.lightData = []; // Array is empty
    this.loadAll(); // Load all the datas
    this.setChartData() // Set datas of my chart
}

loadAll() {
    this.lightDataService.query().subscribe(
        (res: Response) => this.onSuccess(res.json(), res.headers),
        (res: Response) => this.onError(res.json())
    );
}

public setChartData(){
    for(let i =0; i< this.lightData.length; i++)
    {
        this.lineChartLabels.push(this.lightData[i].id);
        this.lineChartData.push(this.lightData[i].value);
    }
}

private onSuccess(data, headers) {
    for (let i = 0; i < data.length; i++) {
        this.lightData.push(data[i]);
    }
}

private onError(error) {
    this.alertService.error(error.message, null, null);
}

This code doesn't work, chart is empty, but, if I add an alert like this : 此代码不起作用,图表为空,但是,如果我添加这样的警报:

ngOnInit() {
    this.lightData = []; // Array is empty
    this.loadAll(); // Load all the datas
    alert("blabla"); // alert to wait
    this.setChartData() // Set datas of my chart
}

It works ! 有用 !

I already had this problem before for other function and I never knew how to fix it. 我以前在其他功能上已经遇到过这个问题,但我不知道如何解决。

Edit 1 : Trying to put the setChartData call into the load all asynchronous method : 编辑1:尝试将setChartData调用放入装入所有异步方法中:

lightData: LightData[];
public lineChartData = Array<any>();
public lineChartLabels = Array<any>();

ngOnInit() {
    this.lightData = []; // Array is empty
    this.loadAll(); // Load all the datas

}

loadAll() {
   this.lightDataService.query().subscribe(
   (res: Response) => {
      this.onSuccess(res.json(), res.headers);
      this.setChartData();
      },
   (res: Response) => this.onError(res.json())
   );
 }

public setChartData(){
  for(let i =0; i< this.lightData.length; i++)
  {
    this.lineChartLabels.push(this.lightData[i].id);
    this.lineChartData.push(this.lightData[i].value);
  }
}

private onSuccess(data, headers) {
    for (let i = 0; i < data.length; i++) {
        this.lightData.push(data[i]);
    }
}
private onError(error) {
    this.alertService.error(error.message, null, null);
}

Still not working. 还是行不通。

The loading of the data is done asynchronously, your code does not stop and wait until it is loaded. 数据的加载是异步完成的,您的代码不会停止并等待直到加载它。 The way you are doing things now the order of the execution is. 现在您执行事务的方式是执行的顺序。

 this.loadAll(); // starts the request
 this.setChartData(); // set the empty list as data
 this.onSuccess(res.json(), res.headers); // later the request finishes

What you need to do is set the chart data after the data has been received. 您需要做的是在收到数据后设置图表数据。 You can do it by calling setChartData inside the success handler of the request. 您可以通过在请求的成功处理程序中调用setChartData来实现。

loadAll() {
  this.lightDataService.query().subscribe(
    (res: Response) => {
      this.onSuccess(res.json(), res.headers);
      this.setChartData();
    },
    (res: Response) => this.onError(res.json())
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM