Colorful legends in Chart.js graphs

I have successfully implemented a chart in my code. The background color for the legend is determined by $scope.colors, but in this example, I only have 3 static datasets. However, my issue arises when the data comes from the database.

Is it possible for the generated colors of the graph to also be used as the background color for the legends?

Check out the source link for the chart

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

app.controller('Ctrl', function ($scope) { 
   $scope.labels = ["A","B","C"];
   $scope.data = ["1","2","3"];
   $scope.colours = ['#bff0dd', '#ffa67b','#b1c2ff'];
   
   
});
<html ng-app="App" ng-controller="Ctrl">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-chart.js/0.10.2/angular-chart.js"></script>
    </head>
    <body>
     <canvas id="doughnut" class="chart chart-doughnut doughnut-year"
               chart-colours="colours"
                chart-data="data" chart-labels="labels">
               </canvas>   
               
               <h3>Legends</h3>
               <div class="legend-item" ng-repeat="a in colours">
                                                <label>
                                                   A
                                                </label>
                                            </div>
       </body>
    </html>

Answer №1

Below are the steps to generate color codes:

  Iterate through each item in $scope.data using AngularJS forEach function.
      Obtain a color code based on the index by calling getColorHex function.
      Push the obtained color code into $scope.colours array.

  Define a function called getColorHex which takes an index parameter and returns the corresponding color code in hexadecimal format after skipping black and white colors.

  Implement a function named decimalColorToHTMLcolor that converts decimal color values to HTML color values.

  Create functions getRGB, getElement, and getPattern to handle RGB value retrieval, element processing, and pattern generation respectively.

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

Error encountered when passing arguments from code-behind to a JavaScript function

When working in the code behind: string func = "displaySuccessMessage("+name+");"; ClientScript.RegisterStartupScript(this.GetType(), "success", func, true); If in a separate JavaScript file: function displaySuccessMessage(user) { ...

Interested in creating a Twitter bot that can automatically share images from a designated folder in sequential order. Leveraging the power of node.js

Recently, I've been following an online guide to create a Twitter bot that tweets images from a folder on my computer. While I have a vast collection of photos I'd like to share, the guide only demonstrates how to post them in a random sequence. ...

Steps for implementing a click event for a navigation drop-down using an HTML helper class

In my MVC 5 application, I have implemented a navigation dropdown. Here is the code snippet: <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="menu> Report ...

AngularJS: Issue with watching arrays and deep $watch functionality

I'm having trouble using the $watch function in AngularJS with an array of boolean values. I want to display a message when there's a change in the array, but it's not working as expected. Check out this code example where the array values ...

What is the method for importing jQuery from a local source?

My experience with CDNs has been seamless, but I've run into some issues when trying to use jquery/jquery mobile locally. It seems that certain classes work intermittently, while others don't work at all. The versions I am using are: jQuery - ...

Issue with Laravel 4 JavaScript Functionality

Currently working with Laravel 4 and have integrated a professional HTML template into my Laravel project. I am in the process of writing a script for it, but encountering an issue that is new to me. I have successfully included the .js and .css files as a ...

Adjust the text color when you select an element

I am trying to figure out how to change the color of my "footertext" to red when I click on either the "left" or "center" containers. Then, I want it to go back to white when I click on any of the containers (left, right, center). This is my current HTML: ...

Exploring discrepancies in JSON arrays using Lodash

Using lodash to find the difference between two arrays: c1Arr contains: [ { varName: 'city', varValue: 'cccccccc' }, { varName: 'country', varValue: 'dddddddd' } ] c2Arr contains: [ { varName: 'abc', ...

Tips for displaying a specific array in react JS using the map function

Hello everyone, I am currently working on creating a hotel webpage using React.js and I'm focusing on developing the facilities page. My goal is to display the description of the facilities based on the button that the user clicks. How can I achieve t ...

Utilizing HTML5 to Access and Update custom data attributes

I have implemented the following code: var activeFilter = $('<li></li>').data('input-id', 'mycustomId'); $('#container').append(activeFilter); Now, I am faced with the challenge of retrieving a specific ...

What is preventing me from dynamically generating a property?

As someone who is new to TypeScript, I have a question regarding defining properties. In JavaScript, we can define a property dynamically like this: class Rectangle { constructor(height, width) { this.height = height; this.width = width; } } ...

In Node.js, use the `[]` operator to select elements from an array of strings without including

In my situation, I am working with an array of strings which can sometimes be an array containing only one string. The issue is that when this happens, using array[0] to retrieve the value does not return the entire string but rather just the first charact ...

What is the best way to toggle button visibility upon clicking in AngularJS?

I have 4 buttons in total. Initially, only one button will be visible while the remaining three should be hidden. I attempted to create this scenario using an example fiddle, but the code was not ideal. I am looking to utilize a ternary operator and simp ...

What is the process for storing an array in AdonisJs migrations?

Having trouble saving an array into a PostgreSQL database! 'use strict' const Schema = use('Schema'); class UsersSchema extends Schema { up () { this.create('users', (table) => { table.increments(); t ...

Retrieving documents from Cloud Firestore using JavaScript in a Cloud Function

I created a test cloud function to trigger when a new like is added to a post in my Cloud Firestore database. Within the function, I intend to retrieve user tokens for sending notifications (though I haven't implemented that yet). However, I encounter ...

Issues with routing in IE9 with AngularJS and PHP posing compatibility problems

I am facing an issue with routing in my PHP application. Below is a simple example of the code I am using: $req=$_SERVER['REQUEST_URI']; if(strpos($req, '/items/') === 0){ include __DIR__.'/../views/items/index.php'; } el ...

Tips for using NodeJS with a MySQL database

Hello everyone, I'm new to this community so please bear with me if my question seems too simplistic. I've been tasked with a basic web project for one of my courses and will be using NodeJS+MySQL along with VS Code which has caught my eye. Howe ...

Rearrange the position of the customized google map marker to appear just above the latitude

Can you provide guidance on how to move a customized Google Map marker above a specific latitude and longitude point? You can view the code structure in this JSBIN: Link. I have used three object arrays for reference. Where should I insert the "anchor" i ...

Load elements beforehand without displaying them using a div

In order to efficiently manipulate my Elements using jQuery and other methods, I am exploring the idea of preloading them all first. One approach I have considered is creating a div with CSS display set to none, and placing all the elements I need for my w ...

Transferring a zipped file from the backend of SailsJS to the frontend of React Redux via

My SailsJS Backend is responsible for generating a zip File when requested by my React App with Redux on the Frontend. I am utilizing Sagas for asynchronous calls and fetch to make the request. In the backend, I have attempted various methods such as: //z ...