Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
How can I hide a chartjs chart?

I'm creating a dashboard with chartJS and gaugeJS, I added buttons to collapse divs with css (it hide everything but the button and the title), it work pretty good with gaugeJS, h2, p etc. but not with chartJS.

Here's the code :

<div class="case presence maximised">
    <canvas id="presence" height="200" width="250"></canvas>
</div>
div.minimised * {
    display: none;
}

div.minimised button {
    display: block;
}
graph1 = new Chart(document.getElementById("presence"),
    {
        type: "doughnut",
        data: {
            labels: [
                "a",
                "b",
                "c"
                "d"
            ],
            datasets: [{
                data: [1, 1, 1, 1],
                backgroundColor: [
                    "blue",
                    "brown",
                    "grey",
                    "darkcyan",
                ],
                datalabels: {
                    anchor: "start",
                    align: "start",
                    color: "#fff"
                },
                borderColor: function () {
                    return $(".case").css("background-color");
                }
            }]
        },
        options: {
            legend: {
                display: false
            },
            plugins: {
                datalabels: {
                    backgroundColor: function (context) {
                        return context.dataset.backgroundColor;
                    },
                    borderColor: "#fff",
                    borderRadius: 10,
                    display: function (context) {
                        return context.dataset.data[context.dataIndex] > 0;
                    },
                    formatter: function (value, context) {
                        return context.chart.data.labels[context.dataIndex];
                    },
                    offset: 5,
                    padding: 10,
                    font: {
                        weight: "bold",
                        size: 12
                    },
                    margin: 10
                },
                labels: {
                    render: "value",
                    fontColor: "#fff",
                }
            },
            cutout: "75%"
        }
    })

The button just add/remove the minimised class to the div with a js function

I tried to set canvas width/height to 0 or 1px in css, visibility: hidden but it doesn't change anything, the chart is still visible and its size didn't changed.

Edit : added js chart code

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I found the solution : I just needed to put my canvas inside div. I don't if it's because of CSS or ChartJS which can't hide canvas' display but with a div it now works (the div is hidden ofc, not only the canvas).

I went from

<div class="case presence maximised">
    <canvas id="presence" height="200" width="250"></canvas>
</div>

to

<div class="case presence maximised">
    <div>
        <canvas id="presence" height="200" width="250"></canvas>
    </div>
</div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!