Shades and hues in Angular and ChartJS

Looking for assistance with Angular chart js. Is there a way to alternate the colors of the chart on every other row?

Here is the code snippet:

<canvas ng-if="hasStats" id="pie" class="chart chart-pie"
chart-type="Pie" 
chart-colours=colors 
chart-data="data" 
chart-labels="labels" 
chart-legend="true">
</canvas>

Below is the JavaScript code:

$scope.labels = ["label 1", "label 2", "label 3"];
$scope.data = [300, 200, 500];
$scope.colors = [{
              fillColor: 'rgba(59, 89, 152,0.2)',
              strokeColor: 'rgba(59, 89, 152,1)',
              pointColor: 'rgba(59, 89, 152,1)',
              pointStrokeColor: '#fff',
              pointHighlightFill: '#fff',
              pointHighlightStroke: 'rgba(59, 89, 152,0.8)'
            },
              {
                fillColor: 'rgba(0, 132, 180,0.2)',
                strokeColor: 'rgba(0, 132, 180,1)',
                pointColor: 'rgba(0, 132, 180,1)',
                pointStrokeColor: '#fff',
                pointHighlightFill: '#fff',
                pointHighlightStroke: 'rgba(0, 132, 180,0.8)'
              },
            {
                fillColor: 'rgba(233, 30, 99,0.2)',
                strokeColor: 'rgba(233, 30, 99,1)',
                pointColor: 'rgba(233, 30, 99,1)',
                pointStrokeColor: '#fff',
                pointHighlightFill: '#fff',
                pointHighlightStroke: 'rgba(233, 30, 99,0.8)'
              }];

The code is within a table and I want to change the color scheme for each row. Odd rows should have one color scheme while even rows have another.

Update:
This could be a solution:

$scope.colorsOdd = [{ // facebook blue
              ...
            },
              { // twitter blue
                ...
              },
            { // mav
                ...
              }];

  $scope.colorsEven = [{ // facebook blue
              ...
            },
              { // twitter blue
                ...
              },
            { // teal
                ...
              }];

Struggling to find the best approach to switch between them.

Update 2:

<td rowspan="5" width="200">
                    <div class="ng-scope" ng-controller="AmbasPieCtrl" ng-init="init(data)" style="width: 250px;">
                        <canvas ng-if="hasStats" id="pie" class="chart chart-pie"
                        chart-type="Pie" 
                        chart-colours=colors
                        chart-data="data" 
                        chart-labels="labels" 
                        chart-legend="true">
                        </canvas> 
                        <div ng-if="!hasStats" class="text-center">
                            <h4>No stats found</h4>
                        </div>
                    </div>

                </td>

Answer №1

Thanks to the assistance of @ewizard, I was able to successfully implement the changes.

Below is the updated code snippet:

<div class="ng-scope" ng-controller="AmbasPieCtrl" ng-init="init(data)" style="width: 250px;">
                        <div ng-if="hasStats">
                            <div ng-hide="$odd">
                                <canvas id="pie" class="chart chart-pie"
                                chart-type="Pie" 
                                chart-colours=colorsEven
                                chart-data="data" 
                                chart-labels="labels" 
                                chart-legend="true">
                                </canvas>
                            </div>
                            <div ng-hide="$even">
                                <canvas ng-if="hasStats" id="pie" class="chart chart-pie"
                                chart-type="Pie" 
                                chart-colours=colorsOdd
                                chart-data="data" 
                                chart-labels="labels" 
                                chart-legend="true">
                                </canvas>
                            </div>
                        </div>
                        <div ng-if="!hasStats" class="text-center">
                            <h4>No stats found</h4>
                        </div>
                    </div>

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

What is the best method for implementing free timeslots in fullcalendar using jquery?

I need help figuring out how to incorporate free timeslots in fullcalendar from 16:00 to 20:00 each day. These times should be available for users to book events but should not overlap with existing events fetched using a JSON call. The database query for ...

What could be causing the submit button to reactivate before all form fields have been completed?

I have implemented the code snippet below to validate each field in my contact form using bootstrap-validator and an additional check through Google reCAPTCHA. You can view and test the form here. The submit button is initially disabled with the following ...

ReactJS - Common Mistakes in Writing Code

I'm a beginner in Javascript and struggling to resolve this issue. When I declare my 'params' variable with var, I encounter an error stating "[js] Unexpected token. A constructor, method, accessor, or property was expected." You can take a ...

Problem with Closing Alerts in Angular UI Bootstrap

Utilizing Angular UI Bootstrap to create Alert boxes, the code used is as follows: <alert data-ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</alert> The output is as expected: Inquiring: Is there ...

AngularJS - Retrieving data dynamically inserted into the DOM

After the application has loaded, I am trying to find a way to extract initial data from the DOM. The server has already inserted the data into a view. Would using Module's value method be a good approach? Additionally, I am curious about how to retr ...

What strategies can be employed to preserve certain fields while dynamically populating others using JSON in a form?

Currently, I am utilizing jquery Populate to dynamically fill a field with the information from the previous Firstname and Surname fields within the same form. However, an issue arises when using the Populate javascript function: $(formname).populate(newfi ...

The attempt to run 'readAsBinaryString' on 'FileReader' was unsuccessful. The first parameter is not the expected type 'Blob'

I am currently working on parsing an xls file. You can find the file by clicking on the following link: However, I am encountering an error that says: 'Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of ...

A guide on how to efficiently retrieve a string within a function in a native module in a React Native

I'm currently tackling a function related to the native elements of iOS that is coded in Objective-C, My goal is to create a method that will output a string in Objective-C, However, I've hit a roadblock while trying to implement this method: T ...

What is the best way to ensure that the state is updated only when the user begins typing in a text

I am currently working on a text editor-related code and my main focus is to update the state of the editor only when the user starts typing in the editor. The state should be updated under the following scenarios: 1. Update the state when the user begin ...

Is it possible to send arguments to the functions executed by "jQuery then"?

Check out the complete code here: http://jsfiddle.net/BurFz/ http://jsbin.com/dagequha/1/edit?js,console /** * executed function chain */ func1('arg1').then(func2).then(func3).then(function () { console.log('execution comp ...

Why is npm attempting to compile a previous version of my code?

Being a complete newbie to npm and node.js, I hope you can bear with me if I'm lacking in providing the right details. I am working on developing a plugin for a website that utilizes an out-of-the-box framework within npm. Initially, everything was ru ...

Tips for transitioning from GWT to AngularJS

Since the development plugin has been discontinued, GWT development feels less enjoyable. Each small modification results in long recompilations and debugging a mix of Java and Javascript in the browser. I'm considering transitioning to AngularJS. Ca ...

Angular first renders a component before removing another one using ng-If

I have two components that are displayed one at a time using an ngif directive. <app-root> <first-Comp *ngIf="showFirst"></first-Comp> <second-Comp *ngIf="!showFirst"></second-Comp> </app-root> Here are some key ...

Is it possible for JavaScript/React to observe the movement of elements on the webpage?

I'm searching for a solution to detect if an element moves on the screen. Currently, I have an absolute positioned div (acting as a download overlay) that appears when a document is clicked on my website. However, I want it to disappear whenever the d ...

Angular 1 - navigating and interacting with external services

Attempting to develop a straightforward Single Page Application using the MEAN stack. All APIs implemented in Node.js are functioning as expected. Routes have been set up in the main controller of my application // create the module and name it var boar ...

The validation did not pass using the validate.min.js script

Hey everyone, I had this code working perfectly before but now it seems to have stopped working. Can anyone help me figure out what's going on? Below is the form I am referring to: <form action="CreateUser" method="POST" id="subscribe"> & ...

Troubleshooting issue with rendering <li></> using array.map: Map's return statement is producing undefined output

Trying to implement pagination in a React project and facing issues with rendering a set of li elements within an ul. The renderPageNumbers function is returning undefined in the console, even though I can see the array being passed and logging out each el ...

Updating the value of a Javascript variable from a separate file

Hello there! I have a file named map.php and I need to modify the center value in the code to be based on a different value provided by another JavaScript file called template.js. In the past, I was able to change other HTML values using setAttribute and q ...

Choose a navigation item from the list containing a nested span element

I've implemented selectnav from GitHub and it's functioning perfectly. However, my menu consists of list items with a description span inside each one, resulting in menu items structured as shown below: <li><a href="somelink.html">Ch ...

The dropdown consistently shows a value of zero

I am toggling the visibility of a div based on the selection from a Dropdown menu, but for some reason, the dropdown is consistently returning a value of 0. $(document).ready(function() { $('#rel_status').on('change', function() { ...