M
M
mspuz2018-12-26 22:24:18
Angular
mspuz, 2018-12-26 22:24:18

How to generate a graph when opening a page?

Good evening. I continue to learn Angular. I got to the graphical display of data. Because the template is developed on bootstrap, then I decided to use such a component
. The example uses variables that take values ​​already at the declaration. I understand that I'm asking stupidity, but how can I implement the display of the graph if I calculate the data dynamically.
Those. In my template I use:

<canvas mdbChart
            [chartType]="chartType"
            [datasets]="chartDatasets"
            [labels]="chartLabels"
            [colors]="chartColors"
            [options]="chartOptions"
            [legend]="true"
            (chartHover)="chartHovered($event)"
            (chartClick)="chartClicked($event)">
          </canvas>

I calculate all the listed variables in the code, using the function:
updateDatasetsAndLabels() {
      let dataSets: Array<number> = []
      let cLabels: Array<string> = []
      let tbackgroundColor: Array<string> = []
      let thoverBackgroundColor: Array<string> = []

      for(let i = 0; i < this.organization.length; i++) {
          dataSets.push(this.organization[i].summa)
          cLabels.push(this.organization[i].name)
          tbackgroundColor.push(this.abackgroundColor[i])
          thoverBackgroundColor.push(this.ahoverBackgroundColor[i])
      }

      this.chartDatasets  = [ { data: dataSets } ]
      this.chartLabels = cLabels
      this.chartColors = [{backgroundColor: tbackgroundColor, hoverBackgroundColor: thoverBackgroundColor, borderWidth: 2}]
    }

How and where should I execute this code so that my chart is displayed immediately when I go to the page. Now I managed to place a button and when pressed, this function works out, as a result, the graph is displayed. But I would like it to be displayed when the page is displayed. Tried to place this code in ngAfterViewInit functions, but didn't help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question