Ensure that the chart.js doughnut remains centered in its container with a fixed width and height

I'm currently utilizing the aurelia plugin for chart js to create a dynamic Doughnut chart. I have disabled the responsive feature for the chart so that it maintains a fixed width and height regardless of screen or device size. However, I am facing an issue with centering the chart as the canvas element exceeds its container. When I try setting a max-width on the canvas element, it centers the chart but distorts the doughnut image by squishing it. Refer to the images below:

Initially, the chart renders properly at a large screen size like this:

https://i.sstatic.net/a8G1k.png

As the viewport size decreases, the canvas element overflows its container, causing the chart to be centered within the canvas but not correctly within the container:

https://i.sstatic.net/lEPol.png

If I apply a max-width to the canvas, it manages to center the chart but severely distorts the doughnut shape:

https://i.sstatic.net/CBKNM.png

I have refrained from using the responsive option in chart.js for the doughnut chart because it makes the chart too small at certain screen sizes. How can I maintain the fixed width and height while ensuring the chart is correctly centered? Below are my chart.js options:


this.doughnutOptions = {
    cutoutPercentage: 80,
    responsive: false,
    tooltips: {
        enabled: false
    }
}

Here's how I'm rendering the chart using Aurelia...

<chart id="dynamic-doughnut-chart" type="doughnut" should-update="true" throttle="2000" data.bind="dynamicDoughnutData" native-options.bind="doughnutOptions"></chart>

Any assistance on resolving this issue would be greatly appreciated!

Answer №1

According to this source, Chart.js Doughnut Chart Sizing, the latest ChartJS guideline states that the elementRatio should be set at aspectRatio: 2, requiring adjustment of the aspectRatio: 1 within the options object.

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

JSON input in react.js sadly ended abruptly

I am encountering a problem when I attempt to submit the form. Unhandled Rejection (SyntaxError): Unexpected end of JSON input Whenever I press the submit button, this error occurs, here is the code snippet: onButtonSubmit = () => { this.setState({ ...

Enhance Your Website with HTML and JavaScript

Here is the code snippet I am working with: sTAT **javascript** var acc = document.getElementsByClassName("item-wrapper"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function(){ this.classList.toggle("selected"); this.nextElementS ...

The ASP.NET JSON response unexpectedly contained undefined data

I am working on a straightforward web application that features a HouseUnitController containing an action that delivers a JSONResult. public JsonResult GetHouseUnits() { var houseUnits = db.HouseUnits.Include(h => h.HouseModel).Include(h = ...

Ways to revert all modifications to styles implemented using JavaScript

Hey there, just checking in to see how you're doing. I was wondering if there's a way to reset all the styles that have been changed using JavaScript? I'm referring to the styles shown in this photo: https://i.sstatic.net/Zawjt.png Thanks ...

Extracting the value of *data* from my HTML and displaying it in the console with Javascript

I'm struggling to figure out what's going wrong with this code. I've done some research online and it seems like I should just include the window.onload = function() at the beginning of my code. However, no matter what I try, the value alway ...

There was an error: Unable to access the 'rotation' property of an undefined object

Currently learning three.js and following the documentation step by step. As I progress, I reach the following code snippet: function animate(renderer, scene, camera, cube) { requestAnimationFrame( animate); ...

I am having trouble getting the pagination to function properly on my custom materialize projects

I am experiencing issues with the list of sub-pages on the official demo sites: Materialized Materialize(css) The navigation bar does not update the active page number or content when clicked. Both websites are using materialize css. If you click ...

What is the best way to combine 4 small triangle pieces in order to create one large triangle?

Is there a way to create an image background with triangle shapes by combining small triangles together? I am interested in making this collection of triangle image backgrounds. Can someone guide me on how to do it?! .block { width: 0; height: ...

Conflict arises between entities due to the speed of one

I am working on creating a scenario in my world where a ball enters a tube with multiple sections. To achieve this, I have constructed the tube using a series of box shapes that I manipulate by moving and rotating them to form the complete structure of the ...

box tick does not alter appearance

I've been struggling with this seemingly simple issue for an hour now. I have a radio button set up with two buttons: <input class="form-control icheck" id="cash_prize" name="cash_prize" type="radio" value="1" style="position: absolute; opacity: 0 ...

Implementing a Popover Notification When Clicked

I'm a beginner at this. I came across an example of a popover message box in the link provided below. I attempted to implement it, but for some reason, it's not working. Could I be overlooking something? Alternatively, is there a simpler way to ...

What is the best way to transfer data received from a controller to Express' router?

Seeking assistance in creating a versatile function to handle data retrieval for my Author endpoint. The goal is to return a complete list of authors if the URL passed to the endpoint has no query parameters. If the URL includes firstName and lastName para ...

Combining array elements into sets of n

I am facing a challenge with an array manipulation task. let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; My goal is to divide this array into a specified number of smaller arrays such that each new array contains the same number of elements ...

Distributing elements evenly across the parent width using Angular FlexLayout for a responsive design

I need to create 3 div squares: div-left, div-middle, and div-right, each with a width of 300px. These squares should be arranged horizontally within the parent div main-section, which will adjust its size based on the screen width, being 1300px if the scr ...

Tips for implementing JWT in a Node.js-based proxy server:

I am a Node.js beginner with a newbie question. I'm not sure if this is the right place to ask, but I need ideas from this community. Here's what I'm trying to do: Server Configurations: Node.js - 4.0.0 Hapi.js - 10.0.0 Redis Scenario: ...

Bootstrap dropdown menu experiencing functionality issues

I am facing an issue with implementing the javascript bootstrap file in my project. The dropdown menu is not working as expected even though I have saved bootstrap to my project folder. I have tried looking at other example codes and even copied and paste ...

There appears to be an issue with displaying the graph

I am having trouble understanding how to transfer the values entered in the input to the graph that is displayed successfully. Although the update seems correct, nothing changes when I try to update it and the data remains stagnant. Page.vue <script&g ...

Repainting can be an issue if the child element has not been transformed

My parent map has a large number of children pins, and I am experiencing significant slowdown while panning the map due to continuous repainting. Strangely, this issue is resolved when tapping on a child's pin and transforming the thumbnail inside it. ...

Transform jQuery scroll script into vanilla JavaScript

While I am quite familiar with jQuery, I find myself struggling when it comes to using pure JavaScript. Could anyone provide guidance on how I can convert my jQuery code into vanilla JavaScript? Your help is greatly appreciated! Below is the code that I ...

Adding JavaScript code to Rails erb snippets

I have a complex select block created using Rails erb, and I now need to convert it to jQuery using append. Here is the original select block: <select id="mission_reward" name="mission_reward" class="select_reward"> <option value="0"><%= ...