Tips for developing strong writing habits

I'm currently working on building a chart with chart.js and I want to populate the chart with data from the backend. However, before that, I need to call a function that is located in my controller. My goal is to invoke this function directly from index.html.

INDEX:

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: $scope.getChartLabels(),
        datasets: [{
            label: '# of Votes',
            data: $scope.getChartData(),
            backgroundColor: [
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        responsive: true,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

CONTROLLER:

(...)

.controller('DashboardController', function ($scope) {
    $scope.getChartLabels = function() {
        return ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"];
    }

    $scope.getChartData = function() {
        return [12, 19, 3, 5, 2, 3];
    }
}

(...)

Is there a way to properly call the Angular functions getChartLabels() or getChartData() from index.html without resorting to bad practices?

Answer №1

It is recommended to utilize javascript methods and angularJs for chart drawing.

.controller('DashboardController', function ($scope) {
    $scope.getChartLabels = function() {
        return ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"];
    }

    $scope.getChartData = function() {
        return [12, 19, 3, 5, 2, 3];
     }

    drawCharts($scope.getChartLabels,$scope.getChartData);
}

    function drawCharts(d1,d2){
      // implement chart drawing logic here.
    }

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

Angular 6: encountering difficulty in setting up component routes (from root to child) when creating separate modules

As a newcomer to Angular, I'm encountering some difficulties in defining child routes in Angular. I'm not sure where I'm going wrong. When I try to create a separate module for the child components, I run into issues when defining the routes ...

building a reactive grid in ReactJS

I'm looking for an explanation of the code inside the constructor that sets the gridFull state. As a newcomer to JavaScript, I'm struggling to understand this particular line of code. constructor() { super(); this.speed = 100; ...

Utilize Angular Js to play a hidden sound in the background without any visible controls

Is there a method to incorporate a background sound without visible controls using Angular JS? I am currently working on an app using Cordova Visual Studio tools. ...

Are JavaScript Object notation and proper JSON the same thing?

When I execute valid JSON in the Chrome console: {"aaa":"bbb"} I encounter this error: SyntaxError: Unexpected token : But if I try something like this instead: {aaa:"bbb"} No error is thrown. Additionally, when running the following code ...

The serverSideTranslations function require an initial locale argument to be passed in order to properly set up localization data

Decided to post here as per the recommendation of next-18next developers. I'm encountering an issue with next-i18next after updating both i18next and nextjs. The error message states: Initial locale argument was not passed into serverSideTranslation ...

Clicking on the checkbox will trigger an AJAX request to cache the

Encountering a problem with an old system I am currently updating: In the <div id='list'>, there is a checkbox list. Upon clicking a checkbox, it triggers an ajax request that returns JavaScript to execute. The JavaScript in the Ajax requ ...

After attempting to host a build file on Firebase, Ionic encountered an error

While attempting to upload my Ionic web app on the browser, I ran the command ionic build browser --prod --release which successfully built the code. I then deployed the app using firebase deploy which also uploaded successfully. However, upon trying to lo ...

Error message: Issue encountered while rendering Angular Bootstrap accordion template

Despite my attempts to find a solution before reaching out for help, I am struggling to get the accordion feature to work in my Angular and Bootstrap application utilizing ui.angular.js. The error message in the developer tools indicates an issue with the ...

Retrieving the necessary key values from a JSON dictionary

I'm attempting to retrieve keys from a dictionary using JavaScript. The user uploads a .json file containing the dictionary, and I want to display the keys from that uploaded dictionary. It's important to note that only .json dictionaries can be ...

What could be causing the responsive grid to not stack items properly?

I'm struggling to make my page responsive on mobile devices. The elements are not stacking as intended, but rather aligning side by side. How can I fix this issue? My attempts to adjust spacing, padding, and flex-grow values have not yielded the desi ...

Feeling uncertain about how to utilize AJAX using the XMLHttpRequest object

The JavaScript function is: function loadData() { const xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { const data = xhttp.responseXML.documentElement.getElement ...

Trouble with Exec Command InsertHTML: Content not Going to Correct Position in Content Editable Div Tag

Need to add an image using the insertHtml execCommand within a content editable div. Take a look at the following code snippet. function createSelection(node, chars, range) { if (!range) { range = document.createRange() range.selec ...

What is the best way to retrieve the ng-model value from a radio button selection?

Is there a way to retrieve the value of a checked checkbox in the controller? An array of radio buttons is used to select from multiple options. <label class="item item-input" ng-show="item.types !=''"> <div ng-repeat="type in ite ...

Creating array keys dynamically based on their respective lengths

Here is an array presented in a specific format: [ [{ name: "name", value: "My-name" }, { name: "qty", value: "1" }, { name: "url", value: "test.com" }, { name: "comment", value: "m ...

Deeply copy a recursive jQuery object using $.extend function

How can I understand the purpose of the first parameter in $.extend function? It mentions deep copy, but what exactly does that mean? I experimented with it, but I didn't notice any significant changes in the output Check out this JSFiddle link for ...

Retrieve the location within the parent mesh

In my scenario, I have a series of meshes organized in a hierarchy as follows: Scene -scene.add(SpaceMesh) -SpaceMesh.add(ShipMesh) SpaceMesh is currently in motion within the scene. However, ShipMesh remains stationary. When I request th ...

Selecting an object dynamically to add a new property in JavaScript

I am faced with a scenario where I have two objects and I need to add new properties to them. The challenge is that I want to be able to choose which object to work with first before adding the new properties. Here's the proposed logic : let customiz ...

Trouble uploading an audio blob as a base64 string using Google Drive API with the drive.files.create method - File ID could not be located

My current challenge involves sending an audio blob to a specific Google Drive folder. To accomplish this, I convert the blob into a file before initiating the transfer process. However, I have encountered an error from the beginning: Error: File not fo ...

Issue arises when component is mistakenly displayed in the header upon deployment on Vercel platform

When deploying my NextJS app to Vercel, I encounter an issue where state-based modals that should be hidden are displaying. Additionally, the modals are rendering in the header instead of center-screen as expected. Surprisingly, these problems do not occur ...

Obscure Promise Structure - Accomplish, Flop, Achieved

I came across the following code block and I'm struggling to understand it. While I have a good grasp on promises in the context of: deferred.then(successCb, errorCb); This code snippet appears to have three callbacks (complete, fail, done>) whic ...