I am struggling to make Angular Charts display labels the way I want them to

Struggling to customize an angular chart for my project. The x axis should display dates and the mouse over should show client names, all retrieved from an array of resource objects in a loop.

The loop code snippet is as follows:

angular.forEach(charts, function(chart, key) {
             var d = new Date(chart.appointment_date).toDateString();

             $scope.labels.push(d);
             $scope.total_earnings += chart.cost.dollars;
               $scope.data[0].push(chart.cost.dollars);
               if (!chart.refundObj[0]){
                 $scope.data[1].push(0);
                } else {
                 $scope.data[1].push((chart.refundObj[0].amount/100));
                }
            });

Currently, the date property is set on both the x axis and in the mouseover. However, when trying to include the client name using:

$scope.labels.push({date: d, name: clientName});

the output only shows [Object, Object].

Using this documentation as the foundation for the charts:

Answer №1

angular-chart is built on the foundation of Chart.js, which requires an array of strings for labels. If you input an object instead, Chart.js will convert it to a string using toString, resulting in [Object, Object] if toString is not defined.

Creating the desired output is straightforward with the correct object construction and option setting.

HTML

<div ng-app="app">
    <div ng-controller="ctrlr">
        <canvas id="line" class="chart chart-line" data="data"
                labels="labels" options="options"></canvas>
    </div>
</div>

JS

In this example, we define a unique object SpecialLabel that provides the axis label when toString is invoked. We also customize the tooltipTemplate to display tooltipLabel while constructing the tooltip.

var app = angular.module('app', ['chart.js']);

app.controller('ctrlr', ['$scope', function ($scope) {

    var SpecialLabel = function (axisLabel, tooltipLabel) {
        this.axisLabel = axisLabel;
        this.tooltipLabel = tooltipLabel;
    }
    SpecialLabel.prototype.toString = function () {
        return this.axisLabel
    }

    $scope.labels = [
    new SpecialLabel("10-Jan", "Client 1"),
    new SpecialLabel("11-Jan", "Client 2"),
    new SpecialLabel("12-Jan", "Client 3"),
    new SpecialLabel("13-Jan", "Client 4"),
    new SpecialLabel("14-Jan", "Client 5"),
    new SpecialLabel("15-Jan", "Client 6"),
    new SpecialLabel("16-Jan", "Client 7")];

    $scope.data = [
        [65, 59, 80, 81, 56, 55, 40]
    ];

    $scope.options = {
        tooltipTemplate: "<%if (label){%><%=label.tooltipLabel%>: <%}%><%= value %>"
    }
}])

Fiddle - http://jsfiddle.net/xg2pd1cu/

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

Symfony's DataTables is encountering an issue with the JSON response, showing

I have encountered an issue while trying to fetch data from a response within my template table (using DataTables). The error message I receive is as follows: DataTables warning: table id=example - Invalid JSON response. For more information about this ...

Terminating a function during execution in JavaScript/TypeScript

Currently, I am in the process of developing a VSCODE extension using TypeScript. Within this extension, there is a particularly large function that is frequently called, but only the final call holds significance. As a solution, I am attempting to elimina ...

The confusion arises from the ambiguity between the name of the module and the name of

In my current scenario, I am faced with the following issue : module SomeName { class SomeName { } var namespace = SomeName; } The problem is that when referencing SomeName, it is pointing to the class instead of the module. I have a requireme ...

What is the best way to split an array into smaller chunks?

My JavaScript program fetches this array via ajax every second, but the response time for each request is around 3 to 4 seconds. To address this delay, I attempted to split the array into chunks, however, encountered difficulties in completing the task: { ...

Achieving CSS Buttons That Change to Green When Clicked and STAY Green

I've been tasked with working on a front-end project for a website that involves buttons changing color when clicked and staying that color. Here is the CSS <style> code I have written for these specific buttons: .button { background-color ...

I am having trouble with my jQuery wrap function and it is not functioning correctly

I'm having trouble with implementing the jQuery wrap function. (function ($) { $.fn.customWrap = function () { applyWrapper(this); return this; }; function applyWrapper($element) { var $input = $('<input&g ...

Displaying Image Preview in Angular 2 After Uploading to Firebase Storage

At the moment, I am facing an issue where the uploaded image is not being displayed after the uploadTask is successful. This problem arises due to the asynchronous loading nature of the process, causing the view to attempt to display the image before the u ...

Obtain ng-model data upon sending a POST request to an Express.js endpoint

I am currently developing a Node.js application that incorporates AngularJS. My goal is to execute a simple POST request using Angular. This POST request should send a set of values to my server, where I can view them using the console.log function. With ...

Use the keyboard to interact with the user interface

Imagine you have the following HTML markup - <ul id="list"> <li class="list-item" tabindex="0">test 1</li> <li class="list-item" tabindex="1">test 2</li> <li class="list-item" tabindex="2">test 3</li> ...

Facing issues with Heroku and Express/Nodejs crashing?

I have been working on a React/Express application that I am attempting to deploy on Heroku. While trying to do so, I encountered the following errors in my logs: 2020-01-13T03:39:48.733455+00:00 heroku[router]: at=error code=H10 desc="App crashed" method ...

best practices for implementing authentication in MEAN stack

Currently, I am exploring the most effective approach to develop an authentication system that is straightforward (relying on a user_name and password). After researching best practices, I came across several projects; however, most of them are outdated a ...

What are the steps to bring in a module from a URL?

I have integrated a Vue.js npm package into my application. However, due to certain reasons, I now need to directly use the CDN URL. Let's assume that this is the CDN link for the library: https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-prot ...

Posting Form Data with Ajax in CodeIgniter

Incorporating the CodeIgniter framework along with the jQuery Form plugin available at http://malsup.com/jquery/form/ Encountering challenges in ensuring proper functionality of a form. View <div class="row"> <div class="well c ...

``Is there a way to retrieve the file path from an input field without having to submit the form

Currently, I am looking for a way to allow the user to select a file and then store the path location in a JavaScript string. After validation, I plan to make an AJAX call to the server using PHP to upload the file without submitting the form directly. Thi ...

Implementing ng-show in the controller using $index variable

I have a list of items displayed using ng-repeat and I want to show the "Status" text next to an item when a certain event occurs, such as a click. I know I can use $index for this, but I'm struggling to understand how to implement it in the controlle ...

Ways to create a URL path that is not case-sensitive in NextJs using JavaScript

I am currently working on a project using NextJs and I have encountered an issue with URL case sensitivity. The paths fetched from an API all start with a capital letter, causing inconsistency in the URLs. For instance, www.mysite.com/About. I would like t ...

Is there a way to stream an mp3 file in a Node.js REPL on Replit?

I have an MP3 file that I want to play when a button is clicked. However, I suspect that I am not correctly serving the file to the server. The following code snippet is from my project on Replit.com: const app = require('express')(); const http ...

The data list is failing to retain previous elements as new ones are incorporated

I have a list for uploads, and whenever I upload new data, the old data in the list gets removed. However, I want to display all the data together. How can I resolve this issue? const [fileList, setFileList] = useState<AddedFile[]>([]); const beg ...

What is the best way to transfer Express.js variables to MongoDB operations?

I have been developing a blogging application using Express, EJS, and MongoDB. Feel free to check out the GitHub repository for more details. One of the features I've implemented is a simple pager for the posts within the application. Within the pos ...

Exploring the World of JSON Nested Arrays with Unknown Titles and Organizing Them for Storage

Apologies if this question has already been addressed elsewhere, but I couldn't find it. I have a JSON object with dynamic keys: { "main": [ { "Example1": [ { "Example1_Var02": "5", ...