Customize Your Chartjs Background Color Settings

I'm working on an angular application that includes a chartjs line graph, and I'm trying to get the line chart to shade the background red for negative numbers. The issue I'm facing is figuring out how to calculate the bottom of the chart.

      const canvasBackgroundColor = {

    id: 'canvasBackgroundColor',

    beforeDraw(chart: any, args: any, pluginOptions: any) {

      const { ctx, chartArea: { top, bottom, left, right, width, height }, scales: { x, y } } = chart;

      ctx.fillStyle = 'rgba(255 26, 104, 0.2)';

      ctx.fillRect(left, y.getPixelForValue('0'), width, canvasBottom);

    }

  }

  if (this.chart == null) {

    this.chart = new Chart("solarChart", {

      type: 'line',

      data: {// values on X-Axis

        labels: this.createXLabels(),

        datasets: [

          {

            label: "Solar",

            data: paybackArray,

            showLine: true,

            pointStyle: 'crossRot',

          }

        ]

      },

      options: {

        scales: {

          x: {

            grid: {

              display: false

            },

            title: {

              display: true,

              text: 'Year',

              font: {

                size: 16

              }

            }

          },

          y: {

            display: true,

            title: {

              display: true,

              text: 'Dollars',

              font: {

                size: 16

              }

            },

          }

        }

      },

      plugins: [canvasBackgroundColor]

    });

  }

I am aiming to make the negative quadrant red, but the color sometimes doesn't reach the x-axis properly, leaving white space in-between.

Answer №1

drawRectangle(left, chart.centerYValue('0'), width, bottomEdge - chart.centerYValue('0'));

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

Tips for keeping a video background stationary while allowing the text to move smoothly

When I added the video for the background, it only appears at the top of the page. However, I want it to be visible as the rest of the page is scrolled. <div class="hero"> <video autoplay loop muted plays-inline class="back-video&qu ...

Tips for waiting until all data has been loaded within Angular 2's asynchronous http calls

Currently, I am developing a tool that involves extracting data from Jira. While I have come across numerous examples of chaining multiple http calls one after another using data from the previous call, I am facing a challenge in making the outer call wait ...

CSS Safari link not aligned properly

My issue involves Safari. When viewed in other browsers, the input and link on the second row appear on the same line, but Safari displays them differently. Any thoughts on why this might be happening? HTML <div id="searchWrapper01"> <d ...

Eliminating the margin caused by Twitter Bootstrap's span

Utilizing bootstrap, my aim is to display two spans side by side without any white margin between them. However, when I implement the code, there seems to be a white margin between each span. This is the code in question: <li class="span4" > &l ...

Utilizing union type return values in Typescript

Looking to incorporate shelljs (via DefinitelyTyped) into my Typescript 1.5-beta project. I want to utilize the exec function with the specified signature: export function exec(command: string, options: ExecOptions): ExecOutputReturnValue | child.ChildPro ...

Stacked on top of one another are in-line block items

I've been working on this code for a while now, but I can't seem to get it right. My goal is to create a horizontal line of navigation links with the first link aligned to the left and the rest following in sequence like a text line. However, a ...

Insert a blank row at the top of the grid in Wijmo for Angular 2

I am attempting to insert a new empty row at the start of the grid when an external button is clicked. The grid is displaying correctly. <wj-flex-grid #flex [itemsSource]="data" [isReadOnly]="true" [headersVisibility]="'Column' ...

Switch up the Thumbnail Image based on the selected category

While testing a shopping site I created, I encountered an issue with changing the banners at the top of the page based on the category the user is viewing. This site is hosted on a server on my localhost, not using wordpress by the way. <div class=" ...

How can I customize icons within the <mat-nav-list> using Angular?

Is it possible to change the icons as follows? If the list is expanded, display icon "arrow1" Otherwise, show icon named "arrow2". How can I determine if the list is expanded and implement this in HTML? <mat-nav-list> <mat-list-item> ...

There was an issue found at line 18 in the backend.service.d.ts file of the node_modules/angular-in-memory-web-api, showing error TS1086 which states that an accessor cannot be declared

I encountered this issue right after installing 'angular-in-memory-web-api'. import { InMemoryDbService } from "angular-in-memory-web-api"; and subsequently, the following error messages appeared: ERROR in node_modules/angular-in-memory-we ...

Exploring the Application of CSS Styles in Selenium

I need help with a webpage that contains a configurable field. The values of this field can be set through a configuration panel, and it can be marked as required by checking a checkbox which applies a specific style to the field label. My question is, how ...

Having trouble implementing an onClick event to change a button's CSS to href in Vue.js

When working with a SinglePageApp and using UIKit (UKit), I have found that I need to use a <button> tag instead of an <a> tag. Previously, the <a> tag was written like this: <a type="button" class="uk-button uk-button-link" href="#e ...

What is the process for adjusting the color of the standard Chromium music player?

On my blog, I am looking to modify the color of the default chromium music player that is currently being displayed for my visitors. The existing player can be seen here: https://i.sstatic.net/73ewd.png I would like to change it to a different color so th ...

Utilizing session management in Angular while handling CORS origin requests

Everything was going smoothly with my session until I decided to enable CORS. Server: app.use(require('express-session')({ secret: 'some key', resave: true, saveUninitialized: true, proxy: true, cookie: { s ...

Is there a way to seamlessly transition between different Angular components without having to refresh the entire webpage?

I'm currently working on implementing a navigation bar that allows users to switch between three components without having the navbar reload. The goal is for only the new component to load when the user clicks on a different section of the navbar, kee ...

Error encountered while upgrading Angular to version 16 and working with Ant Design: `nzComponentParams` is not found in the type `ModalOptions`

Currently, I am in the process of upgrading several Angular applications from version 12 to version 16. However, I have encountered an error related to either TypeScript or Ant Design. TS2345: Argument of type '{ nzTitle: string; nzContent: typeof Con ...

Loop through values from various variables

I am trying to display values inside ngFor that are stored in another variable, but I am not sure if this is possible. Currently, I have the following: Component import { Component, OnInit, OnDestroy } from '@angular/core'; import { ListAffilia ...

Tips for distinguishing between mobile devices and desktop computers on a webpage without using media queries

After examining a list of resolutions for phones and tablets used by CSS designers, I came to the realization that some devices have larger resolutions than most computers. This got me thinking about how design elements on my webpage might be affected. Th ...

Can a search engine algorithm be developed to display an iframe based on the keywords entered in the search query?

Is it possible to create a search engine algorithm in HTML, CSS, and JavaScript that takes keywords from a search bar input and displays the best solution (or solutions) through iframes? html, css, javascript Below is the code for the iframes and search ...

The type '(props: Props) => Element' cannot be assigned to the type 'FunctionComponent<FieldRenderProps<any, HTMLElement>>' in React-final-form

I'm fairly new to using TypeScript, and I am currently working on developing a signUp form with the help of React-Final-Form along with TypeScript. Here is the code snippet that describes my form: import React from "react"; import Button from "@mater ...