W
W
wjvcot2018-12-14 08:52:37
JavaScript
wjvcot, 2018-12-14 08:52:37

How to update the data in the chart in real time?

How to update the data on the chart when adding, deleting or changing information? Changes in the displayed information on the chart should occur in real time. And preferably without using a child component.
ts:

ngOnInit() {
    this.loadReport()
  }

  private loadReport() {
    this.reportService.fetch(this.report_id).subscribe((data: Report[]) => {
      this.reports = data
    })
  }


  ngAfterViewInit() {
    const incomeConfig: any = {
      label: 'Income',
      backgroundColor: '#009688',
      borderColor: '#00796b'
    }

    const consumptionConfig: any = {
      label: 'Consumption',
      backgroundColor: '#0d47a1',
      borderColor: '#2196f3'
    }

    const dateConfig: any = {
      datasets: [ incomeConfig, consumptionConfig]
    }

    this.reportService.fetch(this.report_id).subscribe((data: Report[]) => {
      incomeConfig.data = data.map(item => item.income)
      consumptionConfig.data = data.map(item => item.consumption)
      dateConfig.labels = data.map(item => item.date)
      const reportCtx = this.reportRef.nativeElement.getContext('2d')

      new Chart(reportCtx, {
        type: 'bar',
        data: dateConfig,
        options: {
          scales: {
            xAxes: [{
                gridLines: {
                    offsetGridLines: true
                }
            }]
          } 
        }
      })
    })
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-12-14
@wjvcot

Make the graph a property of the component: Accordingly, the graph data will become available as After making the necessary changes, update the graph: UPD. https://stackblitz.com/edit/angular-h83adp-chartjs...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question