I'm not sure what this is called, but passing an ArrayList to a chart variable. Can you please check it out?

let categList = [];
let correctRateList = [];
let categAveList = [];
let ctx = document.getElementById("myChart");
  let data = {
    labels: [categList[0], categList[1], categList[2], categList[3], categList[4]],
    datasets: [
        {
            label: "Individual",
            backgroundColor: "rgba(255,99,132,0.2)",
            borderColor: "rgba(255,99,132,1)",
            pointBackgroundColor: "rgba(255,99,132,1)",
            pointBorderColor: "#fff",
            pointHoverBackgroundColor: "#fff",
            pointHoverBorderColor: "rgba(255,99,132,1)",
            data: [correctRateList[0], correctRateList[1], correctRateList[2], correctRateList[3], correctRateList[4]]
        },
        {
            label: "SCMS",
            backgroundColor: "rgba(255,153,0,0.2)",
            borderColor: "rgba(255,153,0,1)",
            pointBackgroundColor: "rgba(255,153,0,1)",
            pointBorderColor: "#fff",
            pointHoverBackgroundColor: "#fff",
            pointHoverBorderColor: "rgba(255,153,0,1)",
            data: [categAveList[0], categAveList[1], categAveList[2], categAveList[3], categAveList[4]]
        }
    ]
  };

The number of items on the list can vary, so it's important to make the implementation more flexible instead of assuming there are always 5 items. One way to achieve this is by passing the entire list variable in the label and data tag. When attempting: labels: [categList] it doesn't display the desired outcome. Is there a way to achieve this?

Answer №1

hmm, simply set the entire variable like this:

Substitute:

labels: [categList[0], categList[1], categList[2], categList[3], categList[4]]

With

labels: categList

The same principle can be applied to the data property

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

Is it possible to utilize "this" in mapMutations spread within Vue instance methods?

Trying to define Vuex mutations like this: export default { props: { store: String }, methods: { ...mapMutations({ changeModel: `${this.store}/changeModel` }) } } Encountering an error message: An er ...

Anticipating the arrival of the requested data while utilizing Ajax sending

Greetings, I'm currently utilizing JavaScript to send a request and receive a response from the server. xxmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); myotherMethod(); I am looking for a way to ensure that the next set of instructions ar ...

What is the method to adjust the time of print's font size?

When trying to print a datatable using the following JavaScript code: <script type="text/javascript"> function data(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML ...

What is the method to effectively conduct a testing procedure for JavaScript files that have been exported using

I have a JavaScript file called "sum.js" which contains a simple function: // sum.js function sum(a, b) { return a + b; } export default { sum }; Now I want to write a test for this file using Jest. Here is my "sum.test.js" file in the same folder: // ...

Mastering the art of typing Vuex modules in Vue 3 with TypeScript

I've been grappling with how to properly type vuex modules in a Vue 3 TypeScript project. The official documentation doesn't provide much guidance on this topic. Let's say I have a setup like this: import { createStore, useStore as baseUseS ...

Why won't the state change when using Angular's ui-router $state.go method?

I have developed a factory that is responsible for detecting state changes and checking if a form has been modified. When the form is dirty, a modal window like a confirmation prompt should appear. However, I am encountering an issue where the $state.go() ...

Executing the slideToggle() function in JQuery with javascript

Trying to create a script that expands and collapses when users click on either the text or image. The image is a basic arrow that switches to an arrow pointing upwards when the div is expanded. The issue I'm facing is that the text disappears when t ...

Using JSON within a GraphQL type definition in a Node.js environment

Utilizing Node.js, I am making requests to a report API that returns JSON responses. The API accepts different IDs as parameters, resulting in varying responses. For instance: https://report.domain.io/report/1 https://report.domain.io/report/2 Each r ...

A structure array pointer in C pointing to a structure, within another structure

Struggling with pointer syntax here. I have an array of structures and am attempting to create a pointer to it from within another structure contained in an array. struct foo array* = malloc(sizeof(foo)*10); bar_arr[i]->foo_ptr = &array; After rea ...

Send information from the textbox

I am trying to extract data from an input field and use it to create a QR code. While passing the value as text works, I am facing issues with passing the actual input value. Does anyone have a straightforward example of this process? import React, {Comp ...

Creating Shortened Links with Bit.ly API Solely Using Javascript

Lately, I have been experimenting with Javascript. I was trying to shorten some URLs using bit.ly for tweeting purposes when an idea struck me: why not create an automated process that utilises their API to shrink any URLs I need? However, upon checking ...

Can anyone tell me how to retrieve the value of {{org}} in this situation?

<head> <title>My Unique Page</title> </head> <body> <input type="text" ng-model="selectedOrg" ng-show="!isSuperAdmin" readonly /> <select id="nameOrg" ng-model="selectedOrg" ng-cha ...

Instructions for activating column resizing in MUI DataGrid

Is there a way to enable column resizing for users in MUI DataGrid? It's enabled by default on XGrid, but I would like to enable it on Datagrid as well. Any assistance is appreciated. <DataGrid className={classes.table} ...

The functionality of displaying an animated gif will not work when submitting a CakePHP ajax form before or after completion

I have exhaustively tested all possible scenarios using prototype, but I am unable to get my busy indicator to show no matter what I do. Despite cross-browser testing, I've had no luck. <?php echo $ajax->submit('Submit', array(' ...

Refresh the JavaScript graph with new data from the AJAX request

Seeking assistance in updating my javascript chart data using ajax data retrieved from a database. The specific chart being referenced is an apex chart. After submitting a form via ajax, the returned result is as follows: type: "POST",crossDomain ...

Testing AG Grid's TypeScript in-line cell editing using Jest

I am currently working on writing Jest tests to evaluate the functionality of my ag-grid table. So far, I have created tests to check the default data in the grid and to test a button that adds an additional row of data to the grid. I am now attempting t ...

Retrieve the div element by calling a scriptlet with JavaScript

I am facing an issue with a web page that includes a scriptlet like this: <div id="flash_chart"> <%=content_data['report_text']%> </div> The variable content_data['report_text'] contains a lengthy string ...

Engaging JavaScript Navigation

I am looking to create an interactive JavaScript menu or image map where users can press down, highlight, and hit enter on four different items to reveal hidden messages. However, I struggle with JavaScript and could really use some assistance. I have alr ...

Angular array: Cannot access property of undefined - Type Error

Having trouble fetching data from a web service? I keep encountering the same error message that says "ERROR TypeError: Cannot read property 'bu' of undefined", even though the data I want to show is appearing correctly. Check out the response an ...

Display DataTable in a JavaScript alert message

... { foreach (ListEntry.Custom listrow in mySpread.Elements) { DC = myTable.Columns[listrow.LocalName] ?? myTable. Columns.Add(listrow.LocalName); myDR[DC] = listrow.Value; ...