Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda