question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pie Chart is not fitting with in Canvas

See original GitHub issue

Expected behavior

The pie chart should fit in the canvas. The pie chart should display the legend at the bottom, it display it at the top The pie chart is not showing the title , when I provided it n the configurations.

How should I display values on the pie chart without having to hover over it.

Current behavior

The pie chart exceeds the canvas The legend is on top when I specified it ot be in the bottom. The title is not appearing

Reproducible sample

I dont have one

Optional extra steps/info to reproduce

This is my code in components.ts

let chartCanvas = document.getElementById('chart-sector') as HTMLCanvasElement;
        let chartData = {
            labels: Array.from(this.sector_data.keys()),
            datasets: [{
                label: 'Reports By Sector',
                data: Array.from(this.sector_data.values()),
                backgroundColor: this.generateRandomColors(_.size(this.sector_data)),
                options: {
                    responsive: false,
                    plugins: {
                        legend: {
                            position: 'bottom',
                        },
                        title: {
                            display: true,
                            text: 'Reports By Sector'
                        }
                    }
                },
            }],
        };

        this.sectorChart = new Chart(chartCanvas.getContext("2d"), {
            type: 'pie',
            data: chartData
        });

This is the code on the html

<div class=" col-md-4">
            <div class=" card">
                <div class=" card-body">
                    <div class=" col-auto">
                        <div class=" chart row">
                            <canvas class=" chart-canvas" id="chart-sector"></canvas>

                        </div>
                    </div>

                </div>
            </div>
        </div>

This is how I imported Chartjs

import { Chart, registerables  } from "chart.js";
Chart.register(...registerables);

This is how my chart appears chatr js pie chart

Possible solution

No response

Context

No response

chart.js version

v3.9.1

Browser name and version

Chrome 104.0.5112.81 and microsoft edge 104.0.1293.47

Link to your project

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
OKELLOMARVINcommented, Aug 11, 2022

@stockiNail , what was making my other graphs dissapear was something else, so your solution has worked! Thanks

1reaction
OKELLOMARVINcommented, Aug 11, 2022

Got the solution to my probem

 The expected type comes from property 'options' which is declared here on type 'ChartConfiguration<"pie", unknown[], unknown> | ChartConfigurationCustomTypesPerDataset<"pie", unknown[], unknown>'

here: https://stackoverflow.com/questions/67058279/chart-js-types-of-property-type-are-incompatible-type-string-is-not-assigna

let chartCanvas = document.getElementById('chart-sector') as HTMLCanvasElement;
        let chartData = {
            labels: Array.from(this.sector_data.keys()),
            datasets: [{
                label: 'Reports By Sector',
                data: Array.from(this.sector_data.values()),
                backgroundColor: this.generateRandomColors(_.size(this.sector_data)),
            }],
        };
        let chartOptions = {
            responsive: false,
            plugins: {
                legend: {
                    position: 'bottom',
                },
                title: {
                    display: true,
                    text: 'Reports By Sector'
                }
            }
        };

        this.sectorChart = new Chart(chartCanvas.getContext("2d"), {
            type: 'pie',
            data: chartData,
            options: chartOptions
        } as any);

I added as any at the end of th chart creation because TypeScript compiler is complaining because the chart type property is a specific type rather than a string

Read more comments on GitHub >

github_iconTop Results From Across the Web

Charts.js graph not scaling to canvas size - Stack Overflow
The width and height property that you set for the canvas only work if the Chartjs' responsive mode is false (which is true...
Read more >
Show data in a line, pie, or bar chart in canvas apps
Add a pie chart​​ On the Insert tab, select Charts, and then select Pie Chart. Move the pie chart under the Import data...
Read more >
Chart Container - Concepts - Handbook - Apache ECharts
If the height and width of the container do not exist, or you wish the chart size not equal to the container, you...
Read more >
Pie chart - MATLAB pie - MathWorks
This MATLAB function draws a pie chart using the data in X. ... If you specify a conversion character that does not fit...
Read more >
Responsive Charts - Chart.js
Furthermore, these sizes are independent from each other and thus the canvas render size does not adjust automatically based on the display ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found