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

Unable to submit form with Jquery

Having some trouble with my form submission using Jquery. The submit part of my code seems to be malfunctioning, and I can't pinpoint the issue. <?php if(!isset($_SESSION["useridentity"])){ die(header("Location:index.php")); } ...

The positioning of images on the fabricjs canvas seems to be unreliable and inconsistent

When trying to place a series of 4 images at specified coordinates in fabricjs, I am facing inconsistencies with their placement upon page load. Refreshing the page usually resolves the issue, but I want to prevent this from happening altogether. If anyon ...

Combining the Angular Material Design mdToolbar with the Ionic ion-nav-bar: A Match Made

Currently, I am in the process of creating an application using Ionic and Angular Material. However, I am facing a challenge with getting Ionics back button to function properly when it is used within the mdToolbar directive of Angular Material. To illust ...

Incorporating a dynamic ng-style value within an ng-repeat loop

Having trouble applying dynamic CSS using ng-style with ng-repeat. When I try to create a style variable with ng-repeat, ng-style does not work properly. However, if I replace the value of ng-style with a constant, it works fine. <span ng-repeat="val i ...

Guide on how to align h1 in the navigation bar

I've been attempting to center text on a nav bar but haven't been successful. What could I be overlooking? // include external modules import React, { Component } from "react"; import { Navbar } from "reactstrap"; import { Menu } from " ...

Using jQuery, effortlessly scroll a div to a specific vertical position of your choice

After referring to this previous question: Scrollpane on the bottom, css is hacky, javascript is hard I followed the same scrolling method as explained in the accepted answer. Now there's a new requirement to select a specific item (e.g., through a ...

Issues with Dependency Injection in Angular.js

I've been working on Dependency Injection and trying to make my code more modular in Angular.js. After researching some tutorials, I decided to try and rewrite it. Here's what I initially planned (although I may have misunderstood some concepts, ...

Fulfill a pledge after a function has been run five times

I'm struggling to understand why my promise is not working as expected and how to resolve the issue. Here is the code snippet: let count = 0; function onLoad(){ count++ console.log(count); return new Promise((resolve) => { ...

Retrieving data sent through an AJAX post request

My current project involves making a POST call from a basic HTML page to a Node.js and Express server that will then save the input values to a MongoDB collection. The issue I am facing is that when passing two POST parameters, namely 'name' and ...

Slider - incorporating a heading onto a video with HTML styling

Is there a way to display a title on a slider when one of the slides contains a video? Here is an example code snippet: <!-- Swiper--> <div data-height="100vh" data-min-height="480px" data-slide-effect="fade" class="swiper-container swiper-s ...

Problems encountered with React Image Magnifiers while attempting to zoom in with Next.js

When I try to use the react-image-magnifiers plugin to make an image zoom in on hover, it works fine without next.js. However, when I integrate it with next.js, the zoom functionality does not work. Could there be an issue in my next.config.js file? This ...

When the collapse button is clicked, I aim to increase the top value by 100. Subsequently, upon a second click, the top value should

https://i.stack.imgur.com/p9owU.jpghttps://i.stack.imgur.com/Kwtnh.jpg I attempted to utilize jQuery toggle, but unfortunately, it is not functioning as expected. <script> $(document).ready(function () { $(".sidebar ul li&quo ...

How come I can't capture discord.js promise rejections within event callbacks?

As I delve into creating a discord bot, I encountered an interesting problem. To simplify things, here is a snippet that encapsulates the issue at hand: const Discord = require('discord.js'); const client = new Discord.Client(); client.on(&apo ...

VueJS Unit Testing: Exploring the Content of Attributes- What to Test?

I'm currently facing some challenges with my initial VueJS unit tests using Jest. Although I grasp the concept and have already executed my first set of successful tests, I find myself pondering over the question of "What aspects should I test?" For ...

Encountering a connection error when trying to access a Google spreadsheet within a Next.js application

I am currently exploring Next.js and attempting to utilize Google Sheets as a database for my project. Although my application is functioning correctly, there is still an error message displaying that says "not forgot to setup environment variable". I have ...

Here are the steps to divide an array of objects into separate objects based on their keys

The data I currently have is formatted like this: [{ "Consumer": [{ "Associated ID": "JSUDB2LXXX / BIC 7503 / US", "Parent Consumer": "7503" }], "Owner": [{ &qu ...

What is the correct way to utilize browser actions for sending keys with the "?" symbol in Protractor?

I'm facing an issue in my tests with a particular line of code browser.actions().sendKeys(Key.chord(Key.CONTROL, '?')).perform(); Interestingly, it works fine with another symbol. For example: browser.actions().sendKeys(Key.chord(Key.CONT ...

Encountering problem with image file encoding while using res.download in Express.js

My node.js server with expressjs is set up for local development, where I store and retrieve various files through basic HTTP calls. Most of the time, everything works smoothly. However, on rare occasions, a small number of files return to the end-user sig ...

Ensure that the objection model aligns with the TypeScript interface requirements

I am currently working on implementing an API using express, objection.js, and TypeScript. I found a lot of inspiration from this repository: https://github.com/HappyZombies/brackette-alpha/tree/master/server/src Similar to the creator, I aim to have var ...

Am I implementing the Vue.JS onChange function correctly?

After selecting an option from mod_accs_Role_ID, how can I display the corresponding data stored in the database based on that selection? For example, if I select 1 in mod_accs_Role_ID, then the role_Name 'Hello' should be displayed. <div clas ...