Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda