【问题标题】:How can I wait for data to be pushed into an array before loading charts?如何在加载图表之前等待数据被推入数组?
【发布时间】:2020-02-15 16:15:52
【问题描述】:

我正在尝试在我的应用程序上显示一些图表,但它们当前未显示,因为我在加载后将它们需要的数据推送到数组中,因此为空图表。在尝试渲染我的图表之前,有什么方法可以等待数组?

我尝试过使用 async 和 await,但我不知道我是否做错了,或者在这种情况下它是否对我没有帮助。我尝试在 this.games.getAllGames() 函数的末尾加上 then ,然后在其中调用 loadCharts() 函数,但这也不起作用。

export class SportsCardsComponent implements OnInit, AfterViewInit {
  currentPage = 1;
  chart: [];
  gamesArray = [];
  test = [];

  constructor(
    public games: GameService
  ) { }

  ngOnInit() {
    this.getGames();
  }

  async getGames() {
    const game = await this.games.getAllGames(this.currentPage);
    _.each(game.games, (gameData) => {
      this.gamesArray.push(gameData);
    });
  }

loadCharts() {
    _.each(this.gamesArray, (game) => {
      console.log('hello');
      this.chart = new Chart(game.id, {
        type: 'doughnut',
      data: {
        labels: [game.homeTeam.teamName, game.awayTeam.teamName],
        datasets: [
          {
            label: game.homeTeam.teamName + ' Vs ' + game.awayTeam.teamName,
            backgroundColor: [game.homeTeam.schoolPrimaryColor, game.awayTeam.schoolSecondaryColor],
            data: [game.homeTeam.totalBets, game.awayTeam.totalBets]
          }
        ]
      },
      options: {
        title: {
          display: true,
          text: game.homeTeam.teamName + ' Vs ' + game.awayTeam.teamName
        }
      }
      });
    });
  }

我想要加载我的图表,但每个人都在寻找的数组中没有任何数据,因此无法加载图表。

HTML:

<div class="col-lg-9 float-right" >
  <!-- <div *ngIf='loading'  class="d-flex justify-content-center">
    <div class="spinner-border" style="width: 3rem; height: 3rem;" role="status"></div>
  </div> -->
  <!-- <div *ngIf="!loading"> -->
      <div class="row">
          <div class="col-lg-6 card-background"
            *ngFor="let game of gamesArray"
          >
            <div class="card-surround shadow-sm">
              <div>
                  <h2>{{game.homeTeam.teamName}}</h2>
                  <h2>{{game.awayTeam.teamName}}</h2>
                  <canvas id="{{game.id}}"></canvas>
                  <hr>
                  <p>{{game.gameTime}}</p>
              </div>
            </div>
        </div>
      </div>
  <!-- </div> -->
  <ngb-pagination
  class="d-flex justify-content-end"
  [collectionSize]="gamesArray.length"
  [(page)]="currentPage"
  [maxSize]="5"
  [pageSize]='6'
  (pageChange)='onPageChange($event)'
  size="sm"
  [rotate]="true"
  [ellipses]="false"
  [boundaryLinks]="true"
  ></ngb-pagination>
</div>

【问题讨论】:

    标签: angular


    【解决方案1】:

    在您的模板中使用*ngIf

    <canvas id="{{game.id}}" *ngIf="gamesArray.length"></canvas>
    

    仅当数据在您的数组中时才会加载您的图表。

    您的图表未加载,因为您的画布没有对您的组件的引用。而且您正在使用循环,所以我建议您使用 chartjs 包装器为 Angular。 https://www.npmjs.com/package/angular2-chartjs

    【讨论】:

    • 这根本不会导致任何加载。
    • 我在原帖中添加了 HTML
    • 所以一切都加载了,但图表
    • @Chameleon 您的图表未加载,因为您的canvas 没有您的组件的引用。而且您正在使用循环,所以我建议您使用 Angular 的 chartjs 包装器。 npmjs.com/package/angular2-chartjs
    【解决方案2】:

    您可以使用@ViewChild 来触发您的图表

    例如,如果您在模板中有类似的东西

        <div class="col-lg-6 card-background"  *ngFor="let game of gamesArray;let i=index" >
    
            <canvas baseChart  [id]="i" [datasets]="ChartData" [labels]="ChartLabels" [options]="ChartOptions" [legend]="ChartLegend" [chartType]="ChartType" </canvas>
    
    </div>
    

    在 ViewModel 中你可以使用

      @ViewChild('#1') firstChartttt: BaseChart;  
    

    您现在有了可以在检查后在此处分配数据的对象,并且在 angular 是双向绑定时无需重绘

    注意:但不要忘记初始化图表数据

    【讨论】:

    • 这在我的组件文件中看起来如何?我不太熟悉@ViewChild 或如何触发它们,您是否有一个很好的资源供我阅读?
    • @ViewChild 太强大了,你可以在你的模板中触发你想要的一切请看这个blog.angular-university.io/angular-viewchild
    • 尽量在 ViewModel 中执行业务逻辑,避免使用 *ngIf 对模板收费,因为它会影响应用程序的首次渲染
    • @ViewChild('yourChart') yourChartttt: BaseChart; 这可能对他不起作用,因为他正在循环并创建多个动态图表。
    • 是的,如果它使用 *NgFor 生成许多没有 '#id' 的画布,他将很难触发它,所以他需要关心数据重复,所以触发器永远不会从 ModelView 完成
    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2022-07-21
    • 1970-01-01
    • 2012-01-28
    相关资源
    最近更新 更多