0

I have a dashboard component with shows charts on the page. The charts library needs data in order to render the chart and display on dashboard. Currently the data is static and works fine, but i need the data to be given by typescript variable which will be initialized by API call.

Dashboard HTML:

        <div class="panel panel-white">
          <div class="panel-heading clearfix">
            <h4 class="panel-title">Series Chart</h4>
          </div>
          <div class="panel-body">
            <canvas id="series-chart"></canvas>
          </div>
        </div>

Dashboard Component:

export class DashboardComponent implements OnInit {

  totalSymbol = 0;
  chartLabels = ['A', 'B', 'C'];

  constructor(private http: HttpClient, private apiUrl: ApiUrlService) {
  }

  ngOnInit() {
    this.fetchTotalSymbol();
  }

  fetchTotalSymbol() {
    this.http.get(this.apiUrl.getBaseUrl() + 'symbol/count').subscribe(data => {
      this.totalSymbol = +data;
    });
  }

}

dashboard.js

$(document).ready(function () {

  "use strict";
  new Chart(document.getElementById("series-chart"),
    {
      "type": "doughnut",
      "data": {
        "labels": ["Red", "Blue", "Yellow"],
        "datasets": [{
          "label": "My First Dataset",
          "data": [300, 50, 100],
          "backgroundColor": ["rgb(236, 94, 105)", "rgb(0, 112, 224)", "rgb(241, 194, 5)"]
        }]
      }
    });

});

I need to replace Labels Red,Blue,Yellow in dashboard.js to A,B,C from dashboard component chartLabels after API call fetchTotalSymbol().

I am including dashboard.js in .angular-cli.json under scripts array.

5
  • new Chart(document.getElementById("series-chart"), you are doing it wrong. Use refs, nativeElement, but never DOM directly. Post component annotations and how dashboard.js is included. Commented Mar 11, 2018 at 10:24
  • Why do you use separate dashboard.js? It should be possible to integrate it into angular typescript code Commented Mar 11, 2018 at 10:27
  • @piotrgajow how can integrate it in typescript code? Commented Mar 11, 2018 at 10:29
  • @dfsq can i get link to any blog where i can study that. Commented Mar 11, 2018 at 10:29
  • If I understand correctly that you are using Chart.js library, you could have a look at this question: stackoverflow.com/questions/29885809/… or this example github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/… Commented Mar 11, 2018 at 20:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.