Step-by-step guide on populating an array with variables in Vue.Js for creating a bar chart

I am attempting to dynamically add values from variables to an array in order to create a bar chart using Vue.js

My initial attempt involved adding values like this: series[0]=ServiceArea1;.

This is my current progress:

barChart: {
    data: {
        series [0] : ServiceArea1Total,
        series [1] : ServiceArea2Total,                    
        series [2] : ServiceArea3Total,
        series [3] : ServiceArea4Total,
        series [4] : ServiceArea5Total,
        labels: ['Western', 'Northern', 'Eastern', 'Southern', 'Central'],
        series: [ ]                
    }
}

When I input the values as follows:

series: [[542, 443, 320, 780, 553],]

The output displays correctly. However, since I need to fetch values from a database, I cannot hardcode them.

Below is the relevant HTML code:

<div class="row">
    <div class="col-md-6">
        <chart-card :chart-data="barChart.data"
                    :chart-options="barChart.options"
                    :chart-responsive-options="barChart.responsiveOptions"
                    chart-type="Bar">
            <template slot="header">
                <h4 class="card-title">Statewide Service Area Kids</h4>
                <p class="card-category">Click on a service area to view more information</p>
            </template>
            <template slot="footer">
                <div class="legend">
                    <i class="fa fa-circle text-info"></i> Service Area

                </div>
                <hr>
                <div class="stats">
                    <i class="fa fa-check"></i> Data information certified
                </div>
            </template>
        </chart-card>
    </div>

Answer №2

If you have a model structured as follows:

let barChart = {
  services: [
    ServiceArea1Total,
    ServiceArea2Total,                    
    ServiceArea3Total,
    ServiceArea4Total,
    ServiceArea5Total,
  ],
  labels: ['Western', 'Northern', 'Eastern', 'Southern', 'Central'],
  series: []
}

Your services are stored in barChart.services, labels in

barChart.labels</code (not used here), and total series in <code>barChart.series
. To retrieve data from your services and save it in
barChart.series</code, you would need to do the following:</p>

<pre><code>barChart.services.forEach( function( service ){
    barChart.series.push( service() );
});

This code will execute all functions within services and insert the data into the series array for each one.

Have I misunderstood your intentions?

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

issue with leaflet vue3 component showing incorrect marker popup when clicked

I am dynamically loading markers based on the bounding box from an API using a Pinia store. These markers are stored in the itemsList property of my map wrapper component map-view. The markers are rendered as circlemarkers inside a Vue-for loop. As I navi ...

Using JQuery to delete data from a cookie

I have a delicious cookie that I want to savor before removing one element from it based on the widget ID. if (thisWidgetSettings.removable) { $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) { ...

Update the label for the weekday on Material UI picker

Is there a way to change the weekday display in Material UI DatePicker from single initial (M, T, W, T, F, S, S) to three-letter initials (MON, TUE, WED, etc)? I want to customize it in my component like this: <DatePicker disablePast disableTool ...

Experiencing troubles when trying to execute npm run serve with vue/cli

I started a project with the following commands: vue create test After navigating to my repository: cd test When I tried to launch it using: npm run serve I encountered an issue: INFO Starting development server... ERROR Error: No valid exports m ...

I am interested in creating a table that can toggle between show/hide mode with a plus/minus feature

$(document).ready(function() { $("#t1").hide(); // hide table by default $("#close").hide(); //hide the minus button as well if you only want one button to display at a time $('#sp1').on('click', function() { //when p ...

Is there a better alternative to using nested async callbacks?

Imagine I need to execute asynchronous actions like sending an email and updating the database. Usually, I would write code like this: send_email(function(err, id){ if(err){ console.log("error"); }else{ update_database(id,function( ...

The static files for the icon CSS (404 Error) from Flaticon and Font-mfizz are failing to load properly

When I was designing a website, I needed an icon of Python, so I turned to Flaticon and found what I was looking for. This snippet shows the HTML code I used: {% load static %} <!DOCTYPE html> <html lang="en"> <head> <li ...

Looking for a new slider option to replace the traditional Conveyor belt slideshow?

I have successfully used the Conveyor belt slideshow script from http://www.dynamicdrive.com/dynamicindex14/leftrightslide.htm. Now, I am looking to find another script that works similar to this one. Does anyone have any recommendations for a tool that ...

Anticipate the middleware function to either invoke the next function or return a HTTP 400 status code

I am eager to delve into unit testing and am looking to test my Node API. I am utilizing Express with Typescript and Jest for testing. Prior to invoking the controller middleware, I apply the route input validation middleware to verify the validity of the ...

Order Typescript by Segment / Category

Suppose we start with this original array of objects: {vendor:"vendor1", item:"item1", price:1100, rank:0}, {vendor:"vendor1", item:"item2",price:3200, rank:0}, {vendor:"vendor1", item:"item3", price:1100, rank:0}, {vendor:"vendor2", item:"item1", price: ...

What is the best way to show a nested div element within a v-for loop in Vue.js?

One interesting feature of my coding project is that I have an array nested within another array, and I iterate through them using two v-for loops. The challenge arises when I try to display a specific div in the second loop only upon clicking a button. ...

Attempting to engage with the like button on a Facebook page through Python's Selenium seems to be problematic

Here is the code for adding a (Facebook page like button): WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div[4]/div/div[1]/div/div[1]/div[2]/div[2]/div/div/div[3]/div/div[1]'))).click() The clic ...

"Send the response in ExpressJS before making a request to loop through the

I am currently working with a postgres database that contains records with a column format in JSON, which refers to other similar records. The challenge I am facing is retrieving linked records through asynchronous methods due to the nested structure and ...

Limit access to route in ExpressJS only to internal redirects

I'm managing an ExpressJS application that includes specific routes which I intend to only function when redirected to from my code, rather than input directly into the URL. Essentially, if a user attempts to enter "myapp.com/url" it should not be ac ...

Retrieving, storing, and implementing the classes of child elements directly from an array using jQuery

With the help of jQuery, you can retrieve child elements of the #parent element that have the class .west. In this case, we are not interested in fetching the content of these child elements but instead their class names. For example: <div id="parent" ...

Using Traefik for HTTPS redirection with Docker Compose, the Vue+Nginx application is encountering a 404

Hello everyone, I am a newbie developer and excited to share that I am deploying my first full stack project. I'm currently working on running a vuejs+nginx client app in a docker-compose setup which includes mysql, a nodejs backend, and traefik for ...

What is the best way to add HTML tags both before and after a specified keyword within a string using JavaScript?

Imagine I have a string similar to this, and my goal is to add html tags before and after a specific keyword within the string. let name = "George William"; let keyword = "geo"; Once the html tags have been appended, the desired result should look like ...

Retrieving Django view information using AJAX

Currently, I am in the process of developing an application that updates the main page's data via AJAX. After reading through a helpful response on this Stackoverflow post, I implemented AJAX to refresh my page. In addition to this, I have a specific ...

Incorporate an HTTP Header into a Wicket Ajax Request

I am trying to include a custom HTTP header in all Ajax (XHR) requests made by Wicket. I attempted the following: $.ajaxSetup({ beforeSend: function(xhr) { xhr.setRequestHeader('X-My-Header', 'value'); } }); and $(doc ...

The directive attribute in AngularJS fails to connect to the directive scope

I have been attempting to pass an argument to a directive through element attributes as shown in the snippet below: directive app.directive('bgFluct', function(){ var _ = {}; _.scope = { data: "@ngData" } _.link = function(scope, el ...