1

I'm new in anychart js and I'm facing some difficulties. I have a json file which is loaded from an API. This json file contains informations about nba players stats. Here is my json file :

https://www.balldontlie.io/api/v1/stats

What I want is to get the date data on th X axis. How can I solve that ?

function getData() {
  axios.get("https://www.balldontlie.io/api/v1/stats").then(response => {
    var tab = [];
    for (item of response.data.data) {
      tab.push([item.date, item.fga]);
    }

    anychart.onDocumentReady(function() {
      var month = [];
      for (item of response.data.data) {
        month.push(item.date.split('-'));
      }

      var data = tab;
      var chart = anychart.line();
      var serie = chart.line(data);
      chart.yScale().minimum(0);
      chart.yScale().maximum(50);
      chart.container("container");
      chart.draw();

    });

  });

}

getData();
Emiel Zuurbier
  • 19,095
  • 3
  • 17
  • 32

1 Answers1

1

The date property can be found in the item.game.date key. To make a month out of it you can use the Internationalization API to convert it to the proper way for your locale.

I've modified the code to resemble the code examples in the documentation of Anychart. This example should give you an array with arrays that have the [month, fga] values in them. According to this example in the docs that should work.

function getData() {
  return axios.get("https://www.balldontlie.io/api/v1/stats").then(response => {
    var tab = response.data.data.map((item) => {
      var date = new Date(item.game.date);
      var month = date.toLocaleString('en-US', { month: 'long' });
      return [month, item.fga];
    });
    return tab;
  });
}

anychart.onDocumentReady(function() {
  getData().then(data => {
    var chart = anychart.line();
    var serie = chart.line(data);
    chart.yScale().minimum(0);
    chart.yScale().maximum(50);
    chart.container("container");
    chart.draw();
  });
});
Emiel Zuurbier
  • 19,095
  • 3
  • 17
  • 32