Utilize anychart.js to define the axis using JSON data

I'm relatively new to using anychart js and encountering some obstacles. I have a json file that is being fetched from an API, containing data on NBA players' statistics. You can find the json file here:

My goal is to display the date data on the X axis of the chart. How can I achieve this?

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

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

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

    });

  });

}

retrieveData();

Answer №1

The necessary data is stored in the item.game.date key, specifically within the date property. By utilizing the Internationalization API, you can easily convert this date into a month format that suits your locale.

I have adjusted the code to align with the coding patterns illustrated in Anychart's documentation. This revised version should generate an array containing nested arrays holding the [month, fga] values, as shown in this example from the docs.

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

anychart.onDocumentReady(function() {
  fetchData().then(data => {
    var chart = anychart.line();
    var series = chart.line(data);
    chart.yScale().minimum(0);
    chart.yScale().maximum(50);
    chart.container("container");
    chart.draw();
  });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transitioning React Hover Navbar Design

I'm currently revamping a click-to-open navbar into a hover bar for a new project. I have successfully implemented the onMouseEnter and onMouseLeave functions, allowing the navbar to open and close on mouse hover. However, I am facing an issue with ad ...

Error encountered while creating SvelteKit: The module 'intl-messageformat' that was requested is in CommonJS format

Encountering an error during production build. Here's the output of npm run build executed on Node v16.20.1 npm run build > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7a6c6a317a757278796e5c2c322c322d">[email&# ...

Troubleshooting MongoDB and Node.js: Issue with updating variables when inserting documents in a loop

As a newcomer to MongoDB, I'm facing a puzzling issue that has left me confused. In my dataset, I have an array of Employee objects structured like this: { "Name" : "Jack Jackson", "Title" : "Senior Derp Engineer", "Specialties" : [ "Kicki ...

Filter out the selection choice that begins with 'aa' in the drop-down menu

Here is a select field with various options: <select id="sel"> <option value="1">aadkdo</option> <option value="2">sdsdf</option> <option value="3">aasdfsddkdo</option> <option value="4"> ...

Disregard the Field File when utilizing jQuery validation

I have created a form with jQuery validation, but I am facing an issue where the file upload field is showing a message "enter no more than 3 characters." I believe this problem is due to the maxlength="3" property in the file field. How can I remove the ...

Experiencing difficulties with mocha and expect while using Node.js for error handling

I'm in the process of developing a straightforward login module for Node. I've decided to take a Test-Driven Development (TDD) approach, but since I'm new to it, any suggestions or recommended resources would be greatly appreciated. My issu ...

Error 56 EROFS encountered when trying to save a file in Node.js filesystem every 2 seconds

I've set up a node.js environment on my raspbian system and I'm attempting to save/update a file every 2/3 seconds using the code below: var saveFileSaving = false; function loop() { mainLoop = setTimeout(function() { // update data ...

Unusual express middleware usage in NodeJS

app.use(function(req,res,next){ console.log('middleware executed'); next(); }); app.get('/1',function(req,res){ console.log('/1'); res.end(); }); app.get('/2',function(req,res){ console.log('/2'); res.end() ...

Make changes to an array in Javascript without altering the original array

I currently have an array : let originalArr = ['apple', 'plum', 'berry']; Is there a way to eliminate the item "plum" from this array without altering the originalArr? One possible solution could be: let copyArr = [...origin ...

Is it possible for images underneath to receive focus when hovering over them?

I'm struggling with a layout of thumbnails on my page. We'll refer to them as A, B, C, etc. They are currently displayed like this: A, B, C, D, E, F, G, H, I, J, K, L, M, N... and so on. When you hover over one thumbnail, it enlarges by 2.5 t ...

What's the most effective method: Utilizing generic code generation or hardcoding API calls?

Currently, I find myself in a situation where I am contemplating the best practice for handling my work on an Ecommerce website that involves orders, shipments, invoices, and more. In addition to creating the UI for my application, I also have the capabil ...

What is the method for providing a date format choice in JSON?

I am encountering an issue in my PHP script where I use JSON to pass parameters. When I pass the date as "09-09-2015", it functions correctly. However, when I try to pass the date as $date, it does not work. How can I resolve this problem? $item1 = "test ...

Event that signifies a change in the global state of the Angular 2 router

Is there a universal event that can be utilized for state change/start across all components, similar to the Component Lifecycle Hooks ? For example, in UI-router: $rootScope.$on("$stateChangeStart", function() {}) ...

Update the content of a div element with the data retrieved through an Ajax response

I am attempting to update the inner HTML of a div after a certain interval. I am receiving the correct response using Ajax, but I am struggling to replace the inner HTML of the selected element with the Ajax response. What could be wrong with my code? HTM ...

Using and accessing Ajax response across all routes in an application

I am developing a Node.js Express API application that requires several AJAX calls at the start of the application for global data access in all requests. At the beginning of my app.js file, I include: var users = require('./modules/users'); I ...

Angular 6 and the intricacies of nested ternary conditions

I need help with a ternary condition in an HTML template file: <div *ngFor="let $m of $layer.child; let $childIndex=index" [Latitude]="$m.latitude" [Longitude]="$m.longitude" [IconInfo]="$childIndex== 0 ? _iconInfo1:$c ...

Having trouble retrieving the latest value of a scope variable when dealing with multiple partial HTML files controlled by a single Angular controller

In my Angular controller, I have an HTML file that includes two partials. Here is a simplified version: HTML: <!DOCTYPE html> <html> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> ...

Issue encountered while attempting to transform a POJO Class into a JSON String using GSON

I've been attempting to convert a POJO class to Json using Gson, but I keep encountering an error for which I don't have a clear solution. My Java version is 19 and here is my class: public class PlayerModel { String player; UUID uuid; ...

Which is better for toggling between images/icons: a switch statement or a ternary operator?

I have a unique challenge where I am dealing with a dynamic list of thumbnails. Some of the items in this list are images, while others need to be represented by icons. My goal is to display either an image or an icon based on the contentType of each item. ...

sending jqgrid post request with JSON payload

My grid data read is set up to use json format. Here is the configuration: url:"devitem.json", mtype: "POST", datatype: "json", ajaxGridOptions: { type : 'post', async : false, error : function() { alert ...