Modifying the chart width in Chart.js: A step-by-step guide

After creating a chart using Chart Js, I encountered an issue where the chart did not fit within the specified width. Adjusting the attributes of canvas proved to be ineffective, specifically with regards to the width attribute. Despite changing the value, the width remained unchanged. While setting maintainAspectRatio to false and responsive to true, these adjustments did not resolve the problem.

How can I ensure that the chart fits within the designated width and height?

Any suggestions or advice would be greatly appreciated.

<canvas id="monthly-sales" width="300" height="200"></canvas>

JS

var monthlySales = document.getElementById("monthly-sales").getContext('2d');
        var monthlySalesChart = new Chart(monthlySales, {
            type: 'bar',
            data: {
                labels: listOfMonths.slice(10,2),
                datasets: [{
                    label: 'Sales',
                    data: [
                        monthlyFees['sales'][10],
                        monthlyFees['sales'][11]
                    ],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                    ],
                    borderColor: [
                        'rgba(255,99,132,1)',
                        'rgba(54, 162, 235, 1)',
                    ],
                    borderWidth: 1,
                }]
            },
            options: {
                title: {
                    display: true,
                    text: 'Sales'
                },
                legend: {
                    display: false,
                },
                maintainAspectRatio: false,
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero:true,
                            userCallback: function userCallback(tick) {
                                return (tick / 100000000);
                            }
                        }
                    }]
                },
                tooltips: {
                    callbacks: {
                        label: function label(tooltipItem, data) {
                            return numeral(tooltipItem.yLabel).format('0,0');
                        }
                    }
                }
            }
        });

Html

<div class="col-sm-12">
    <div class="col-sm-4">
        <div class="card">
            <div class="card-body">
                <h4 class="card-title">This month</h4>
            </div>
            <div class="card-body">
                <div class="col-sm-4">
                    <div class="card-chart">
                        <canvas id="monthly-sales" width="200" height="200"></canvas>
                    </div>
                    <div>
                        Sales Changed: <span id="monthly-sales-changes"></span>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="card-chart">
                        <canvas id="monthly-profits" width="200" height="150"></canvas>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="card-chart">
                        <canvas id="monthly-orders" width="200" height="150"></canvas>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

https://i.stack.imgur.com/ZjyK9.png

Answer №1

After some investigation, the issue has been identified. Due to incorrect definitions of labels, the chart was not displaying correctly.

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

Decrease the heaviness of a Glyphicon

How can I make Glyphicons appear lighter in weight? I have utilized the "ok" Glyphicon <span class="glyphicon glyphicon-ok"></span> which currently displays as follows: Is there a method to decrease the weight of the icon in order to create a ...

Is there a way to adjust the pivot point of the object's rotation?

I have incorporated a 3D hamburger model on my website using Three.js. However, I am facing an issue where the rotation behaves unexpectedly. The burger is supposed to rotate around itself at a 45-degree angle, but instead, it rotates from the upper left c ...

Is the ID "nodeName" specifically designated as reserved in the HTML5 language specifications?

I have an element with the following id: <span id="nodeName"></span> In my HTML code. Then, when using jQuery to do the following: $("#nodeName").html("someString"); I am getting an error in the console that says: Uncaught TypeError: Objec ...

Acquiring div elements from a collection of divs using selenium in Java

Currently, I am attempting to retrieve a list of all elements using the XPath below. Unfortunately, no matter what I try (class, xpath, etc.), I'm always getting a result size of 1. My objective is to search for records and then loop through them, but ...

The conditional rendering logic in the ng-if directive does not seem to be synchronizing

Currently, I am delving into AngularJS and working on a basic application to gain familiarity with it. Within my app, there are four tabs: List, Create, Update, and Delete. However, my goal is to only display the Update and Delete tabs when I press the b ...

When the height of a sibling element restricts the inner content and scroll bar due to the parent element's height being defined in vh

I'm currently working on creating a sidebar that occupies a fixed percentage of the viewport. Inside this sidebar, I want to have an element that remains fixed at the top while allowing the rest of the content to scroll if it exceeds the height of the ...

How to use Express Validator to validate both email and username within a single field?

I am currently developing an application using the Express (Node.js framework) and I want to allow users to log in with either their email address or username. My question is, how can I implement validation for both types of input on the same field using e ...

Four-pointed star design with rounded edges

My goal is to create a pixel-perfect star using CSS. I have attempted to achieve this so far, but the star currently has 5 points and I would like it to only have 4 points. Additionally, I am looking for a way to make the corners of the star more rounded. ...

How can the name of an element be passed as an argument to a function called by that element within a Vue component?

I'm currently developing an interactive SVG map inspired by the one found on the CDC page. In my SVG map component, I have implemented multiple path elements that each contain a unique name attribute representing the corresponding state. Additionally, ...

Determining the screen width of mobile devices across the board

While using the Google Chrome inspector to simulate the Galaxy S5 (360px) screen, I am encountering issues with detecting the correct screen width. The CSS for 360px is being overridden by the 768px CSS instead. Is there a more effective solution to this ...

Changing styles with the use of bootstrap.css themes on the fly

I'm experimenting with toggling between a custom dark and light theme in my MVC application. On the _Layout.cshtml page, the default theme is loaded by the following code: <link id="theme" rel="stylesheet" href="~/lib/bootstrap/dist/css/Original. ...

Attempting to reduce the width of the dig when it reaches 400

I have a square element with a click event that increases its size by 50 pixels on each click. However, once it reaches a size of 400 pixels, the size decreases by 50 pixels with every click instead. Additionally, when the size of the square reaches 100 p ...

jquery target descendants with specific names

In the provided HTML code snippet, there is a main div with the class name of cxfeeditem feeditem, and it contains multiple child elements with similar class names and structure. My query pertains to extracting values from specific children within all the ...

How come there is such a large space for clicking between my logo and the navigation bar, and what can be done to eliminate it?

* { box-sizing: border-box; padding: 0; margin: 0; } ul { list-style-type: none; } .nav-links, .logo { text-decoration: none; color: #000; } .logo { max-width: 80%; width: 20%; height: 20%; } .navbar img { width: 10%; height: auto; di ...

Content in Slider Exceeding Container Bounds

After successfully creating a slider using jQuery with the help of some Stack Overflow users, everything was working perfectly. However, when attempting to change the layout of the slider, it somehow broke and now all slides are being pushed out of the con ...

Tips on implementing CSS to the subpar class in Vuejs

I am working on an HTML file that operates with button in Vue.js. The v-bind:class can be utilized for a single tag as shown below. It disappears based on the boolean value of bool data. <h3 v-bind:class="{active: bool}">{{counter.document}}&l ...

What is the best way to refresh a single component in my application without causing the other components to reload?

I've been working on a review application with Vue.js that fetches random facts from an API (https://uselessfacts.jsph.pl/random.json?language=en) and allows users to provide feedback through radio buttons and text inputs. Once submitted, the feedback ...

What is the best way to insert identical text into several lines at once using emacs?

My HTML file contains numerous links to a database, but the URLs are formatted as: <a href=/bw/examplefile.html> Although I attempted to use wget with the --base parameter to download all the links, it didn't work as expected. Instead of trou ...

What steps can be taken to resolve the error message "AttributeError: 'WebElement' object does not have the attribute 'find_element_class_name'?"

try: main = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "main")) ) articles = main.find_elements_by_tag_name("article") for article in articles: header = article.find_element_c ...

How to animate a left border shifting to the center using JavaScript

As I'm modifying my current div, I need to add a vertical line in the center of it. I've come across various solutions where a left border is added and then shifted using the left property by 50% (which effectively places it in the middle). Here ...