Creating an array of data from JSON using JavaScript and displaying it in a chart using ApexChart

I am attempting to generate a chart displaying the value of Bitcoin in Euro. The data is fetched from JSON and converted into an array for the ApexChart series data (ApexData['xbtToEuro']) along with a list of dates.

Despite my console indicating that everything is working fine, I am only seeing the first value on the chart. What could be the issue?

Click here to view a screenshot showing the ApexChart not functioning as expected.

<HTML>
    <HEAD>
        <TITLE>test</TITLE>
    </HEAD>
    <BODY>
        <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
        <div id="myChart"></div>
        <script type="text/javascript">

/*  AJAX SECTION */
var dataApex = [];
var list_date = [];
var catalogue = [];
var series_forged = [];

loadData();


        function loadData() {
            if (catalogue.length === 0) {
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        catalogue = JSON.parse(xhr.responseText);
                        Object.keys(catalogue).forEach(function(date_day){
                            Object.keys(catalogue[date_day]).forEach(function(hours){
                                list_date.push(new Date(hours * 1000));
                                Object.keys(catalogue[date_day][hours]).forEach(function(monneys){
                                    var value = catalogue[date_day][hours][monneys];
                                        if (dataApex.indexOf(monneys) === -1) {
                                            dataApex[monneys] = new Array();
                                        }
                                })
                            })
                        });

                        processChart();
                    }
                }
                    var d = new Date();
                    var $month = d.getMonth();
                    var $years = d.getFullYear();
                xhr.open("GET", "../data/monney_market/" + $years + "_" + ($month + 1 * 1) + "_monney.json", true);
                xhr.send();
            } else {
                Object.keys(catalogue).forEach(function(date_day){
                    Object.keys(catalogue[date_day]).forEach(function(hours){
                        list_date.push(new Date(hours * 1000));
                        Object.keys(catalogue[date_day][hours]).forEach(function(monneys){
                            var value = catalogue[date_day][hours][monneys];
                                if (dataApex.indexOf(monneys) === -1) {
                                    dataApex[monneys] = new Array();
                                }
                        })
                    })
                });

                processChart();
            }
        }

        function processChart () {
            Object.keys(catalogue).forEach(function(date_day){
                Object.keys(catalogue[date_day]).forEach(function(hours){
                    Object.keys(catalogue[date_day][hours]).forEach(function(monneys){
                        var value = catalogue[date_day][hours][monneys];
                            dataApex[monneys].push(value);
                    })
                })
            });

            console.log(dataApex["xbtToEuro"]);
            console.log(list_date);

            series_forged = new Array;
            series_forged.push({
                name: "xbtToEuro",
                data: dataApex["xbtToEuro"],
                type: "line"
            });
            createChart("#myChart");

        }

        function createChart ($idCSS){
          /* Additional Chart Configuration Here */

            var options = {
                          /* Chart Options Here */
            };

            var chart = new ApexCharts(document.querySelector($idCSS), options);
            chart.render();
        }
        </script>
    </BODY>
</HTML>

Your assistance would be greatly appreciated. Thank you!

Answer №1

Resolved! Here is an example of the correct code. It seems like the issue with the previous code was related to the date array.


        <HTML>
            <HEAD>
                <TITLE>testing</TITLE>
            </HEAD>
            <BODY>
                // Script and chart setup
            </BODY>
        </HTML>

Example JSON Data:

{"19":{ ... }{"20":{ ... }}

Folder Structure:

  • /your_project/data/monney_market/ => place your JSON files here
  • /your_project/folder/demo.html

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

Activate button through input using Bootstrap

I am struggling to achieve the desired functionality with my "sendit" button. I want it to be enabled as soon as there are any characters entered in the box, but I have tried multiple solutions without success. Part of HTML: <input type="password ...

What is the best way to reference the className within nested SCSS when working with Next.js and React.js?

I am working on customizing a navbar that includes a hamburger button. My goal is to change the style, specifically the bar color and the appearance of the hamburger button, upon tapping. I have already attempted using &:active and .activeBar in the SCSS f ...

Can someone guide me on implementing Node.js clusters in my basic Express application?

— I have successfully developed a basic application that retrieves data (50 items) from a Redis DB and displays it on localhost. After running an ApacheBench test with parameters c = 100, n = 50000, I am achieving around 150 requests/sec on my aging dual ...

instructions for executing this javascript within the <p> tag

This is the code I have written : <p onload=javascript:alert('sss')>www</p> Unfortunately, the code does not alert 'sss', Can someone please tell me what's wrong with my code? Thank you ...

"What is the process for ASP MVC to return JSON data with a custom variable

Within my application, I have defined a user class. This class includes properties for name, phone number, and address. public class User { public string name { get; set; } public string phone { get; set; } public string address { get; set; } ...

A curated collection saved in LocalStorage using React JS

I have implemented a LocalStorage feature to create a favorite list, but it only adds one item each time the page is reloaded. The items are retrieved from a JSON file. For a demonstration of how my code functions, check out this link: const [ storageIte ...

Exploring the functionality of streams on the iOS 5 platform

Hey there, I'm still learning the ropes as a new programmer. (so please bear with me) Currently, I'm working on setting up an inputStream and outputStream to write JSON objects in another part of my class... I've got them set up in -(id)in ...

When the button is clicked, avoid the onclick event and display a modal. If "yes" is clicked, execute the onclick function

I am facing an issue with a page containing multiple input elements that all have similar structure. I want to show a modal before executing the onclick event, and only run the function when the "yes" button in the modal is clicked. However, the problem is ...

Returning Props in Dynamic Components with Vue 3

Exploring the capabilities of Vue3's Dynamic Component <component>, I am currently working with this setup: Component 1: <template> <div> <h1> Name Input: </h2> <Input :model="props.name" /> ...

What is the best way to retrieve the name of a dynamic form in a Controller

How can I retrieve the dynamic form name in my controller? See below for the code snippet: HTML <form name="{{myForm}}" novalidate> <input type="text" ng-model="username" name="username" required/> <span ng-show="(submit & ...

How come my uvmapped texture is flipping vertically when using iewebgl + threejs?

Currently in the process of developing a 3D viewer for game pads with the aim of customizing the pad using various colors and materials. Initially, I created a simple ".bin .js" loader with sample materials using Threejs r62 to create a visualizer prototy ...

When setting the form action attribute in jQuery, the parameter being passed may be null

My goal is to redirect my form action to another page with a URL parameter. Here's the code I'm using: ui.setFormActionToTargetPage = function () { $('form').get(0).setAttribute('action', 'myTargetPage.html?id=' ...

What are the steps for establishing a connection to Heroku using node-mongodb-native?

I'm feeling lost when it comes to connecting to MongoLab on Heroku. I came across an example that was supposed to help, but it just left me more confused. You can check it out here. After looking at both the web.js and deep.js files, I noticed they b ...

Discovering the mean value within an array

Looking to determine the minimum, maximum, and average values in an array of numbers: Below is the code I am working with: $number = array(15,20,100,10,25,30); for ($i=0; $i<count($number); $i++){ //Find maximum number using the max function. ...

Developing a TypeScript NodeJS module

I've been working on creating a Node module using TypeScript, and here is my progress so far: MysqlMapper.ts export class MysqlMapper{ private _config: Mysql.IConnectionConfig; private openConnection(): Mysql.IConnection{ ... } ...

Is there a way to transform a .pcm file to a wav file using node.js?

I'm working on a project that generates a pcm file from a stream and saves it after the stream finishes. Now, I am looking for a way to convert this .pcm file to a .wav or another audio format using an npm package. Can anyone suggest a solution or poi ...

Is it possible for a web server to transmit information without being prompted by the client?

I'm looking to create a web application for a tool that can run anywhere from a minute to a couple of hours. I want users to be able to initiate the tool's run directly from the webpage, and receive real-time status updates as the process progres ...

When multiple instances are present, the functionality of dynamically generated jQuery functions ceases to operate effectively

I've developed a chat application similar to hangouts where clicking on a user generates the chat div. One feature I have is allowing users to press enter in a textarea to send text, but when multiple dynamically generated jQuery functions are present ...

A pop-up appears, prompting me to redirect to an external URL when clicking on a link that opens in a new tab on my WordPress website

Seeking assistance in dealing with a popup that appears when trying to open a link redirected to an external URL. The popup prompts for permission to open the link in a new tab. Upon inspecting the Element, the code snippet below is revealed: <div cla ...

Is the Jackson pendant designed for GsonFactory?

In my code, I implement the AuthorizationCodeFlow.Builder() method that includes a parameter for new GsonFactory(). Given that I am already utilizing the Jackson libraries in my project, I prefer to stick with Jackson and avoid adding Gson. Is there a way ...