"Update your Chart.js to version 3.7.1 to eliminate the vertical scale displaying values on the left

https://i.sstatic.net/7CzRg.png

Is there a way to disable the scale with additional marks from 0 to 45000 as shown in the screenshot? I've attempted various solutions, including updating chartjs to the latest version, but I'm specifically interested in addressing this issue in version 3.7.1. Additionally, there's an issue where the label at the top (100%) is being cut off, and I haven't been able to find a solution for that yet.

Possible solutions:

export const chartOptions: ICustomChartOptions = {
  responsive: true,
  layout: {},
  elements: {
    bar: {
      borderRadius: 4,
    },
    line: {
      borderWidth: 3,
    },
    point: {
      backgroundColor: '#FFFFFF',
      borderWidth: 2,
      radius: 4,
    },
  },
  clip: false,
  scales: {
    x: {
      grid: {
        display: false,
      },
      ticks: {
        font: {
          size: 10,
        },
      },
      min: 0,
      max: 100000,
    },
    y: {
      position: 'left',
      grid: {
        drawBorder: false,
      },
      ticks: {
        callback: function (value) {
          return `${value}%`;
        },
        stepSize: 25,
        font: {
          size: 10,
        },
      },
      min: 0,
      max: 100,
      beginAtZero: true,
    },
  },
  plugins: {},
};

Update: This is the data used for the chart

export const chartData: ChartData<'line'> = {
  labels: chartLabels,
  datasets: [
    {
      type: 'line',
      // yAxisID: 'line',
      data: [100, 20, 49, 10, 97],
      order: 1,
      datalabels: {
        anchor: 'start',
        align: 'end',
        color: color['Purple/Purple 80'],
        formatter(value) {
          return `${value}%`;
        },
      },
    },
    {
      type: 'bar' as 'line',
      yAxisID: 'bar',
      data: [22000, 17500, 12000, 10000, 44000],
      order: 2,
      datalabels: {
        align: 'end',
      },
    },
  ],
};

Update 2: When displaying values, I utilize ChartDataLabels plugin.

Answer №1

It's uncertain how the second axis is generated without data, but it could be related to the type: "bar" section of a mixed chart. The potential data structure might look like this:

const data = {
   labels: [....],
   datasets: [
       {
          type: "line",
          label: "....",
          data: [.....]
       },
       {
          type: "bar",
          label: "....",
          data: [.....]
       },
   ]
}

To manage two separate axes for different parts of the chart, you can assign unique IDs and hide one axis using options:

const data = {
   labels: [....],
   datasets: [
       {
          type: "line",
          label: "....",
          data: [.....],
          yAxisID: "y"
       },
       {
          type: "bar",
          label: "....",
          data: [.....],
          yAxisID: "y2"
       }
   ]
};

const chartOptions = {
   scales:{
      x: { 
         // .... options for the x axis
      },
      y: { 
         // .... options for the y axis 
      },
      y2:{
         display: false,
         // ... other y2 options, if any
      }
   }
}

Here's an example snippet demonstrating the implementation:

const data = {
    labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    datasets: [
        {
            label: 'lines',
            data: [100, 20, 40, 50, 90, 44, 29],
            type: 'line',
            order: 1,
            yAxisID: 'y'
        },
        {
            label: 'bars',
            data: [10009, 2000, 4000, 5000, 9000, 4400, 2900],
            type: 'bar',
            order: 0,
            yAxisID: 'y2'
        }
    ]
};

const options = {
    scales:{
        y: {
            beginAtZero: false,
        },
        y2: {
            display: false,
        }
    },
    layout: {
        padding: {top: 10}
    }
}
new Chart(document.getElementById('chart1'), {data, options});
<div style="height: 100%; width:100%">
<canvas id="chart1"  style="height: 90vh; width:95vw"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js" integrity="sha512-QSkVNOCYLtj73J4hbmVoOV6KVZuMluZlioC+trLpewV8qMjsWqlIQvkn1KGX2StWvPMdWGBqim1xlC8krl1EKQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

If adjusting the label size, explore options like layout.padding.top or scales.y.grace, or adjust the maximum value (max) of the scale as needed based on data.

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

"Encountering issues with logging in through AngularJS and the $http request functionality

My goal is to login using $http and GET of REST web services. This is my approach: Retrieve data based on username and password using $http.get Store the result in $scope.getResult=res; Assign variables to the retrieved data: var result=$scope.getResult ...

Can we verify the availability of an Angular library during runtime?

I am in the process of creating a custom Angular library, let's name it libA, which has the capability to utilize another Angular library, named libB, for an optional feature. Essentially, if the Angular application does not include libB, then the fea ...

What is the best way for me to bring in this function?

Currently, I am in the process of developing a point-of-sale (POS) system that needs to communicate with the kitchen. My challenge lies in importing the reducer into my express server. Despite multiple attempts, I have been unable to import it either as a ...

Convert the existing jQuery function to Angular 9 syntax

I have just started learning Angular and am finding it challenging to rewrite a jQuery code that I use for loading the "classycountdown" script. Below is the jQuery function that I need to convert: $(document).ready(function() { var remainingSec = $(& ...

The data stored in LocalStorage disappears when the page is refreshed

I'm facing an issue with the getItem method in my localStorage within my React Form. I have added an onChange attribute: <div className = 'InputForm' onChange={save_data}> I have found the setItem function to save the data. Here is ...

"Adding an Image to Another Image in HTML or JavaScript: A Step-by-Step

Hello there! I'm currently working on creating a status bar where I can add an image after another image. Let me explain my idea clearly. So, I have two images in GIF format: one is white and 10x10px, and the other one is black and also 10x10px. On ...

Select objects from an array that are contained within another array of objects

I am dealing with an array of objects that contain various attributes such as weakness, id, and type. [ { weakness: [ "Fire", "Flying", "Ice", "Psychic" ], id: 1, type: [ "grass", "poison" ] }, ...

Differences in outcomes have been observed between the elementLocated and findElements functions

Currently, I am in the process of developing Webdriver automation for a specific web application. As part of this process, I have created a test that seems to be functioning well most of the time but occasionally encounters an issue. it('verifies pre ...

The buttons on the Bootstrap Carousel are unresponsive and the indicators are not displaying as they should

I am currently attempting to replicate a bootstrap carousel that includes indicators. However, in my code, I am unable to view the indicators, and the previous/next buttons are not functional. Although I came across a workaround that I could modify from th ...

Troubleshooting issues with cross-domain jQuery ajax requests

Struggling with this code and unable to make it work. This call consistently returns a "Failed to load resource: the server responded with a status of 401 (Unauthorized)" error message. $('#btnZendesk').click(function () { $.ajax({ ...

Tips for verifying the input field with specific requirements in Angular 6

There is an input field that needs to validate text based on certain logic conditions: No spaces should be allowed in the formula. The operators (and,or) must be lowercase and enclosed in curly brackets {}. The number of opening '(&apos ...

Replacing jQuery.ajax from a different directory/domain - problem with using relative URLs

We are incorporating scripts from various sources and domains, including one that contains the definition for jQuery.ajax function. Calls to jQuery.ajax are being made from different places and domains using relative URLs, such as jQuery.ajax("my/relativ ...

Secure your Express.js session cookies for enhanced protection

Struggling to figure out how to set a secure cookie in the expressjs framework. Any suggestions on where I can find an option for this? ...

Event listeners can only be removed within the useEffect hook

I have encountered an issue when trying to remove an event listener added inside the useEffect hook. The listener is set up to run once after the first rerender, thanks to the empty array passed as the second argument in the useEffect call. I attempted t ...

What could be causing the "function undefined" error in my HTML document?

Recently, I developed a small chat application that initially functioned well with all code contained in one HTML document. However, after separating the CSS, HTML, and JS into individual files, an issue arose when invoking the register_popup function from ...

Calculate the total of the temporary information entered into the input field

I'm totally new to all of this and definitely not an expert, but there's something that really bothers me. I found a website where you have to complete a captcha to log in. It goes like this: <input type="text" class="form-contr ...

Installing Yarn causes the download of an unconventional directory

Currently, I am facing an issue while trying to install yarn on my Macbook Pro (2017). The installation process seems to be downloading a folder called /react-praktis/ instead of completing successfully. Below is a screenshot for reference: https://i.stac ...

An External Force is Disrupting My Jquery

Something seems off because everything was working perfectly fine until the FallingLeavesChristmas.min.js file stopped activating on this specific page. I initially thought it was an issue with the JavaScript itself, but that doesn't seem to be the ca ...

Cease the audio from playing unless you are actively hovering over the image

Is there a way to make the sound stop playing when you are not actively hovering over the picture? Any suggestions on how to achieve this would be greatly appreciated! imgarr[i].onmouseout=function(){ this.setAttribute('src',this.getAttribu ...

Dealing with substantial ajax responses using JavaScript

Currently, I am in the process of developing a website that utilizes jQuery File Tree. However, there is an issue with the enormous size of the AJAX response from the server - 900 KB and containing approximately 70,000 'files' (which are not actu ...