Using JavaScript to access array INDEX values sequentially

I have created a Stacked Bar chart using the Js library dhtmlx, and here is the generated output:

The JSON data is structured as follows:

var data = [
    { "allocated":"20", "unallocated":"2", "day":"01/01/2014" },
    { "allocated":"12", "unallocated":"0", "day":"02/01/2014" },
    { "allocated":"2", "unallocated":"18", "day":"03/01/2014" },
    { "allocated":"22", "unallocated":"2", "day":"04/01/2014" },
    { "allocated":"3", "unallocated":"13", "day":"05/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"06/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"07/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"08/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"09/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"10/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"11/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"12/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"13/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"14/01/2014" },
];

To group my JSON data by WEEK, I used the following method:

var groupedByWeek = _.groupBy(data, function(item) {
    var dateMoment = moment(item.day,"DD/MM/YYYY");
    weeks = dateMoment.week();
    array = [weeks]
    return array;
});

After grouping, I obtained 3 array indexes from the data.

Object { 1: Array[4], 2: Array[7], 3: Array[3] }

My aim now is to set index 1 as the default value for my graph. Then, upon clicking the "NEXT" button, I want the array index to change incrementally one by one.

In essence, I wish to pass data week by week on the X-axis. Is there a way to achieve this by passing array indexes sequentially? Any help would be greatly appreciated.

The complete code of my HTML file is as follows:


<body>
<div id="chart1" style="width:1020px;height:300px;border:0px solid #000000;">
    <div><a href="#">Next</a></div>
</div>


<script>

var barChart1 =  new dhtmlXChart({
    view:"stackedBar",
    container:"chart1",
    value:"#allocated#",
    label:"#allocated#",
    color: "#a24689",
    // gradient:"falling",
    width:40,
    tooltip:{
        template:"#allocated#"
    },

    xAxis:{
        title:"Days",
        template:"#day#",

    },

    yAxis:{
        title:"Hours",
        start:0,
        step:4,
        end:24
    },

    legend:{
        values:[{text:"Completed Activities",color:"#a24689"},{text:"Undefined",color:"#F9D544"}],
        valign:"top",
        align:"center",
        width:110,
        layout:"x"
    }
});

barChart1.addSeries({
    value:"#unallocated#",
    color:"#F9D544",
    label:"#unallocated#",
    tooltip:{
        template:"#unallocated#"
    }
});

var groupedByWeek = _.groupBy(data, function(item) {
    var dateMoment = moment(item.day,"DD/MM/YYYY");
    weeks = dateMoment.week();
    array = [weeks]
    return array;
});
console.log(groupedByWeek)

barChart1.parse(data,"json");
// console.log(data)

</script>

Answer №1

After trying different approaches, this solution ultimately did the trick.

let weeksGrouped = _.groupBy(data, function(element) {
        let dateMoment = moment(element.day,"DD/MM/YYYY");
        return dateMoment.week();

The contents of weeksGrouped are stored in an array format. To make it work, pass the corresponding index value of the array to

barChart1.parse(weeksGrouped[1],"json");
instead of barChart1.parse(data,"json");

In the end, success was achieved!

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

How to integrate a While loop within an RXJS subscription

I have a piece of Angular code where I am attempting to subscribe to my first API and include a while loop within this subscription. Additionally, I need to subscribe to another API inside the while loop. The reason for this is that I need to subscribe t ...

Problem with utilizing Passport-Local while executing a Mongoose.save operation

I've been troubleshooting the following code snippet: router.post('/register', function(req, res) { User.register(new User({ username : req.body.username }), req.body.password, function(err, user) { if (err) { ...

Is it possible to implement hierarchical validation within reactflow nodes?

I am currently utilizing reactflow to establish a network of sequences, each possessing their own unique "levels." My primary objective is to restrict connections between sequences based on their respective levels. For instance, a level 5 sequence should ...

Mistake in Timestamp Separation - JavaScript

I have a Timestamp retrieved from MSSQL and displayed on a Webclient. The timestamp reads: Thu Jan 01 18:00:00 CET 1970, however, I am only interested in the time format: 18:00 or 18:00:00 (preferably the former). <script> var timestamp = ...

Creating a bezel design in CSS or Vue: A step-by-step guide

Embedding: https://i.sstatic.net/YfvQP.png Is there a CSS property that can be used to create the same angle as shown in the layout? I looked everywhere in the program but couldn't find this specific property. Tried searching here !: https://i.s ...

Error message: Attempting to access a property that is undefined (specifically 'ref') in MUI

Has anyone encountered this error when trying to customize themes in MUI v5 using custom variants? I keep seeing this error message: https://i.sstatic.net/D7F0e.png TypeError: Cannot read properties of undefined (reading 'ref') at Select (/va ...

Is there a way to link the output to the attributes of the constructor in C#?

Developing an interface with the following properties: public interface IDatasource { string Name { get; } string Description { get; } List<GraphPoint> Point { get; set; } } After implementing this interface, I crea ...

The invocation of $.ajax does not work as a function

I'm encountering an issue when running npm start and trying to access the browser, I keep getting this error message in the stack trace. Error: $.ajax is not functioning properly at findLocation (G:\CodeBase\ExpressApp\routes\ind ...

Deliver the Stripe API response from the backend to the frontend page

I'm struggling to retrieve the response object from Stripe after creating a subscription using npm. import Stripe from 'stripe'; const key = require("stripe")("XXXXXXXXXXXXXXXXX"); export function subscribe(cus, items) { key.subscription ...

Exploring the data nesting structure in Grails and mongo DB

I am working on a Grails project that involves interacting with mongo DB. I am seeking advice on the best approach for creating domain classes to represent nested data. Here is an example of the data: settings:{ user:{id:1234 , name:"john"} colors:{h ...

programming / mathematics - incorrect circular item rotation

I need help arranging a sequence of planes in a circular formation, all facing toward the center. However, I seem to be experiencing issues with the rotation after crossing the 180-degree mark. While my objects are correctly positioned around the circle, t ...

jQuery efficiently handles large amounts of text data transfer (gradual loading)

The issue at hand: My website relies on jQuery AJAX (post) for navigation, meaning the page doesn't refresh every time a user moves to a different section. This setup requires me to fetch data using AJAX and present it dynamically. While this works w ...

What is the process for converting blog content into JSON format for export?

Currently, I am immersing myself in the world of blogging and web development simultaneously. My focus now lies on mastering JSON for which I am striving to devise a method that will enable me to export my entire blog content first to JSON and then XML. Th ...

Is it possible to merge string and span elements to create a unified element?

In my current project using React, I am dealing with an array of strings and JSX Elements. For instance, the array has items like: [<span class="name">JCrew0</span>, "edited this document"]. I am attempting to display thes ...

"Troubleshooting: Issues with accessing data in jQuery AJAX

I am currently using a JavaScript piece of code that utilizes AJAX to fetch data from my database. Here's how the code looks: $.ajax({ url: 'getlog', data: { id: calEvent.id}, dataType: "json", success: function(data) { alert ...

What is the proper syntax for using .focus() with the nextElementSibling method in typing?

As I strive to programmatically shift focus in my form using nextElementSibling, I encounter a challenge with typing variables/constants due to working with Typescript... I have managed to achieve success without typing by implementing the following: myF ...

Rendering a Nativescript component once the page has been fully loaded

I'm currently working on integrating the WikitudeArchitectView into my TypeScript code. I've successfully registered the element in the main.ts TypeScript file: var architectView = require("nativescript-wikitudearchitectview"); registerElement ...

React Js: Error encountered while parsing JSON due to an unexpected token < at the beginning

I am having issues fetching my JSON file in React JS using the fetch method. An error is popping up that says: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 I am unable to figure out what the error could be, even after va ...

Exploring the functionality of className using materialUI

Attempting to test whether my component has a specific class is proving challenging. This difficulty stems from the fact that the class is generated using MaterialUI. For instance, I am looking for a class named spinningIconCenter, but in reality, it appea ...

In what way can an array be assigned to a new key within the same key along with additional objects?

My goal is to transform the existing key value into a new format within the same key. It may be difficult for me to explain clearly through words, but the following data will help clarify. The data is currently structured as follows: const sampelData = [{ ...