angular chartjs not showing data properly





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm using Angular 6 and trying to implement a Chart.js line chart. Basically, I'm calling my API for two arrays: a weight_data array and a weight_date array and using them for the chart.



I store the arrays I got from the API request in two instances: this.weight_date and this.weight_data.



Here's the code for the chart:



//   code for chart here
this.chart = new Chart(this.chartRef.nativeElement, {
type: 'line',
data: {
labels: this.weight_date, // your labels array
datasets: [
{
data: this.weight_data, // your data array
borderColor: '#e0573a',
fill: true
}
]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: true
}],
yAxes: [{
display: true
}],
},
title: {
display: true,
text: 'Weight Tracker',
fontFamily: "'Montserrat', sans-serif",
fontColor: '#272727',
fontSize: 18,
padding: 12
},
layout: {
padding: {
left: 10,
right: 20,
top: 0,
bottom: 0
}
},
gridLines: {
drawOnChartArea: true
},
}
});


It's works okay when I use pre-coded arrays (["11/02/18", "11/03/18", "11/04/18"] for dates and [65, 66, 67] for weight). But when I try to use the two instances, the chart comes up blank. There's no error of sorts, it's just blank.



I'm doing the API call first then initializing the chart, respectively, in ngOnInit. What am I missing here?










share|improve this question























  • Are you sure that you have the data before initializing the chart? Retrieving the data will be asynchronous, and should therefore probably be handled by subscribing to an observable.

    – Jeff Fohl
    Nov 23 '18 at 23:45











  • @JeffFohl I see, this might be the case. But would you mind posting your solution on how to construct the observable below?

    – Lee Merlas
    Nov 23 '18 at 23:52











  • Sure - see below for a simple example

    – Jeff Fohl
    Nov 24 '18 at 0:39


















0















I'm using Angular 6 and trying to implement a Chart.js line chart. Basically, I'm calling my API for two arrays: a weight_data array and a weight_date array and using them for the chart.



I store the arrays I got from the API request in two instances: this.weight_date and this.weight_data.



Here's the code for the chart:



//   code for chart here
this.chart = new Chart(this.chartRef.nativeElement, {
type: 'line',
data: {
labels: this.weight_date, // your labels array
datasets: [
{
data: this.weight_data, // your data array
borderColor: '#e0573a',
fill: true
}
]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: true
}],
yAxes: [{
display: true
}],
},
title: {
display: true,
text: 'Weight Tracker',
fontFamily: "'Montserrat', sans-serif",
fontColor: '#272727',
fontSize: 18,
padding: 12
},
layout: {
padding: {
left: 10,
right: 20,
top: 0,
bottom: 0
}
},
gridLines: {
drawOnChartArea: true
},
}
});


It's works okay when I use pre-coded arrays (["11/02/18", "11/03/18", "11/04/18"] for dates and [65, 66, 67] for weight). But when I try to use the two instances, the chart comes up blank. There's no error of sorts, it's just blank.



I'm doing the API call first then initializing the chart, respectively, in ngOnInit. What am I missing here?










share|improve this question























  • Are you sure that you have the data before initializing the chart? Retrieving the data will be asynchronous, and should therefore probably be handled by subscribing to an observable.

    – Jeff Fohl
    Nov 23 '18 at 23:45











  • @JeffFohl I see, this might be the case. But would you mind posting your solution on how to construct the observable below?

    – Lee Merlas
    Nov 23 '18 at 23:52











  • Sure - see below for a simple example

    – Jeff Fohl
    Nov 24 '18 at 0:39














0












0








0








I'm using Angular 6 and trying to implement a Chart.js line chart. Basically, I'm calling my API for two arrays: a weight_data array and a weight_date array and using them for the chart.



I store the arrays I got from the API request in two instances: this.weight_date and this.weight_data.



Here's the code for the chart:



//   code for chart here
this.chart = new Chart(this.chartRef.nativeElement, {
type: 'line',
data: {
labels: this.weight_date, // your labels array
datasets: [
{
data: this.weight_data, // your data array
borderColor: '#e0573a',
fill: true
}
]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: true
}],
yAxes: [{
display: true
}],
},
title: {
display: true,
text: 'Weight Tracker',
fontFamily: "'Montserrat', sans-serif",
fontColor: '#272727',
fontSize: 18,
padding: 12
},
layout: {
padding: {
left: 10,
right: 20,
top: 0,
bottom: 0
}
},
gridLines: {
drawOnChartArea: true
},
}
});


It's works okay when I use pre-coded arrays (["11/02/18", "11/03/18", "11/04/18"] for dates and [65, 66, 67] for weight). But when I try to use the two instances, the chart comes up blank. There's no error of sorts, it's just blank.



I'm doing the API call first then initializing the chart, respectively, in ngOnInit. What am I missing here?










share|improve this question














I'm using Angular 6 and trying to implement a Chart.js line chart. Basically, I'm calling my API for two arrays: a weight_data array and a weight_date array and using them for the chart.



I store the arrays I got from the API request in two instances: this.weight_date and this.weight_data.



Here's the code for the chart:



//   code for chart here
this.chart = new Chart(this.chartRef.nativeElement, {
type: 'line',
data: {
labels: this.weight_date, // your labels array
datasets: [
{
data: this.weight_data, // your data array
borderColor: '#e0573a',
fill: true
}
]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: true
}],
yAxes: [{
display: true
}],
},
title: {
display: true,
text: 'Weight Tracker',
fontFamily: "'Montserrat', sans-serif",
fontColor: '#272727',
fontSize: 18,
padding: 12
},
layout: {
padding: {
left: 10,
right: 20,
top: 0,
bottom: 0
}
},
gridLines: {
drawOnChartArea: true
},
}
});


It's works okay when I use pre-coded arrays (["11/02/18", "11/03/18", "11/04/18"] for dates and [65, 66, 67] for weight). But when I try to use the two instances, the chart comes up blank. There's no error of sorts, it's just blank.



I'm doing the API call first then initializing the chart, respectively, in ngOnInit. What am I missing here?







javascript angular chart.js angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 23:39









Lee MerlasLee Merlas

9611




9611













  • Are you sure that you have the data before initializing the chart? Retrieving the data will be asynchronous, and should therefore probably be handled by subscribing to an observable.

    – Jeff Fohl
    Nov 23 '18 at 23:45











  • @JeffFohl I see, this might be the case. But would you mind posting your solution on how to construct the observable below?

    – Lee Merlas
    Nov 23 '18 at 23:52











  • Sure - see below for a simple example

    – Jeff Fohl
    Nov 24 '18 at 0:39



















  • Are you sure that you have the data before initializing the chart? Retrieving the data will be asynchronous, and should therefore probably be handled by subscribing to an observable.

    – Jeff Fohl
    Nov 23 '18 at 23:45











  • @JeffFohl I see, this might be the case. But would you mind posting your solution on how to construct the observable below?

    – Lee Merlas
    Nov 23 '18 at 23:52











  • Sure - see below for a simple example

    – Jeff Fohl
    Nov 24 '18 at 0:39

















Are you sure that you have the data before initializing the chart? Retrieving the data will be asynchronous, and should therefore probably be handled by subscribing to an observable.

– Jeff Fohl
Nov 23 '18 at 23:45





Are you sure that you have the data before initializing the chart? Retrieving the data will be asynchronous, and should therefore probably be handled by subscribing to an observable.

– Jeff Fohl
Nov 23 '18 at 23:45













@JeffFohl I see, this might be the case. But would you mind posting your solution on how to construct the observable below?

– Lee Merlas
Nov 23 '18 at 23:52





@JeffFohl I see, this might be the case. But would you mind posting your solution on how to construct the observable below?

– Lee Merlas
Nov 23 '18 at 23:52













Sure - see below for a simple example

– Jeff Fohl
Nov 24 '18 at 0:39





Sure - see below for a simple example

– Jeff Fohl
Nov 24 '18 at 0:39












2 Answers
2






active

oldest

votes


















0














It looks like you want to subscribe to an observable which can provide the data, and once it comes in, you can then initialize your chart. Usually, we would want to have the HTTP call occur within a service, but to simplify, this is how we could do with within a component:



import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {Component, OnInit} from '@angular/core';


export class MyChart implements OnInit {

constructor(private http: HttpClient) { }

private getData() {
let url = '[SOME-URL]' // your path to the data API endpoint
return this.http.get(url) as Observable<Array<any>>; // you would want to use correct typings here, instead of <any>
}

ngOnInit() {
this.getData().subscribe((data: any) => {
console.log(data);
// assign your data here, and then initialize your chart.
});
}
}





share|improve this answer































    0














    I just found out what I was doing wrong: I'm using axios, which is promise-based, so taking @JeffFhol's suggestion into account, my two instances were blank since I was initializing the chart at the same time I was making the API request.



    The solution was to initialize the chart in the then clause of the axios request, which ensures that my instances aren't empty.



    Here's the snippet that worked:



    axios({
    method: 'post',
    url: 'url_to_api',
    headers: {
    'Content-Type' : 'application/json'
    // custom data headers, etc.
    },
    data: {
    // data to be sent
    }
    })
    .then(response => {
    this.weight_dates = response.data.weight_date;
    this.weight_data = response.data.weight_data;

    // code for chart here
    })
    .catch(error => {
    console.log(error)
    })





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453915%2fangular-chartjs-not-showing-data-properly%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      It looks like you want to subscribe to an observable which can provide the data, and once it comes in, you can then initialize your chart. Usually, we would want to have the HTTP call occur within a service, but to simplify, this is how we could do with within a component:



      import {HttpClient} from '@angular/common/http';
      import {Observable} from 'rxjs/Observable';
      import {Component, OnInit} from '@angular/core';


      export class MyChart implements OnInit {

      constructor(private http: HttpClient) { }

      private getData() {
      let url = '[SOME-URL]' // your path to the data API endpoint
      return this.http.get(url) as Observable<Array<any>>; // you would want to use correct typings here, instead of <any>
      }

      ngOnInit() {
      this.getData().subscribe((data: any) => {
      console.log(data);
      // assign your data here, and then initialize your chart.
      });
      }
      }





      share|improve this answer




























        0














        It looks like you want to subscribe to an observable which can provide the data, and once it comes in, you can then initialize your chart. Usually, we would want to have the HTTP call occur within a service, but to simplify, this is how we could do with within a component:



        import {HttpClient} from '@angular/common/http';
        import {Observable} from 'rxjs/Observable';
        import {Component, OnInit} from '@angular/core';


        export class MyChart implements OnInit {

        constructor(private http: HttpClient) { }

        private getData() {
        let url = '[SOME-URL]' // your path to the data API endpoint
        return this.http.get(url) as Observable<Array<any>>; // you would want to use correct typings here, instead of <any>
        }

        ngOnInit() {
        this.getData().subscribe((data: any) => {
        console.log(data);
        // assign your data here, and then initialize your chart.
        });
        }
        }





        share|improve this answer


























          0












          0








          0







          It looks like you want to subscribe to an observable which can provide the data, and once it comes in, you can then initialize your chart. Usually, we would want to have the HTTP call occur within a service, but to simplify, this is how we could do with within a component:



          import {HttpClient} from '@angular/common/http';
          import {Observable} from 'rxjs/Observable';
          import {Component, OnInit} from '@angular/core';


          export class MyChart implements OnInit {

          constructor(private http: HttpClient) { }

          private getData() {
          let url = '[SOME-URL]' // your path to the data API endpoint
          return this.http.get(url) as Observable<Array<any>>; // you would want to use correct typings here, instead of <any>
          }

          ngOnInit() {
          this.getData().subscribe((data: any) => {
          console.log(data);
          // assign your data here, and then initialize your chart.
          });
          }
          }





          share|improve this answer













          It looks like you want to subscribe to an observable which can provide the data, and once it comes in, you can then initialize your chart. Usually, we would want to have the HTTP call occur within a service, but to simplify, this is how we could do with within a component:



          import {HttpClient} from '@angular/common/http';
          import {Observable} from 'rxjs/Observable';
          import {Component, OnInit} from '@angular/core';


          export class MyChart implements OnInit {

          constructor(private http: HttpClient) { }

          private getData() {
          let url = '[SOME-URL]' // your path to the data API endpoint
          return this.http.get(url) as Observable<Array<any>>; // you would want to use correct typings here, instead of <any>
          }

          ngOnInit() {
          this.getData().subscribe((data: any) => {
          console.log(data);
          // assign your data here, and then initialize your chart.
          });
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 0:39









          Jeff FohlJeff Fohl

          1,4851422




          1,4851422

























              0














              I just found out what I was doing wrong: I'm using axios, which is promise-based, so taking @JeffFhol's suggestion into account, my two instances were blank since I was initializing the chart at the same time I was making the API request.



              The solution was to initialize the chart in the then clause of the axios request, which ensures that my instances aren't empty.



              Here's the snippet that worked:



              axios({
              method: 'post',
              url: 'url_to_api',
              headers: {
              'Content-Type' : 'application/json'
              // custom data headers, etc.
              },
              data: {
              // data to be sent
              }
              })
              .then(response => {
              this.weight_dates = response.data.weight_date;
              this.weight_data = response.data.weight_data;

              // code for chart here
              })
              .catch(error => {
              console.log(error)
              })





              share|improve this answer




























                0














                I just found out what I was doing wrong: I'm using axios, which is promise-based, so taking @JeffFhol's suggestion into account, my two instances were blank since I was initializing the chart at the same time I was making the API request.



                The solution was to initialize the chart in the then clause of the axios request, which ensures that my instances aren't empty.



                Here's the snippet that worked:



                axios({
                method: 'post',
                url: 'url_to_api',
                headers: {
                'Content-Type' : 'application/json'
                // custom data headers, etc.
                },
                data: {
                // data to be sent
                }
                })
                .then(response => {
                this.weight_dates = response.data.weight_date;
                this.weight_data = response.data.weight_data;

                // code for chart here
                })
                .catch(error => {
                console.log(error)
                })





                share|improve this answer


























                  0












                  0








                  0







                  I just found out what I was doing wrong: I'm using axios, which is promise-based, so taking @JeffFhol's suggestion into account, my two instances were blank since I was initializing the chart at the same time I was making the API request.



                  The solution was to initialize the chart in the then clause of the axios request, which ensures that my instances aren't empty.



                  Here's the snippet that worked:



                  axios({
                  method: 'post',
                  url: 'url_to_api',
                  headers: {
                  'Content-Type' : 'application/json'
                  // custom data headers, etc.
                  },
                  data: {
                  // data to be sent
                  }
                  })
                  .then(response => {
                  this.weight_dates = response.data.weight_date;
                  this.weight_data = response.data.weight_data;

                  // code for chart here
                  })
                  .catch(error => {
                  console.log(error)
                  })





                  share|improve this answer













                  I just found out what I was doing wrong: I'm using axios, which is promise-based, so taking @JeffFhol's suggestion into account, my two instances were blank since I was initializing the chart at the same time I was making the API request.



                  The solution was to initialize the chart in the then clause of the axios request, which ensures that my instances aren't empty.



                  Here's the snippet that worked:



                  axios({
                  method: 'post',
                  url: 'url_to_api',
                  headers: {
                  'Content-Type' : 'application/json'
                  // custom data headers, etc.
                  },
                  data: {
                  // data to be sent
                  }
                  })
                  .then(response => {
                  this.weight_dates = response.data.weight_date;
                  this.weight_data = response.data.weight_data;

                  // code for chart here
                  })
                  .catch(error => {
                  console.log(error)
                  })






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 2:09









                  Lee MerlasLee Merlas

                  9611




                  9611






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453915%2fangular-chartjs-not-showing-data-properly%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()