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

224
Visualizações
Chart JS - Title missing when clearing canvas if no data is available

EDIT - Answer below worked fine, all I did aside from removing chart.clear() was to hide the legend and the x/y axes.

Any ideas what may be causing the blurry text now, though?

I'm sure I'm doing something remarkably silly, but I can't seem to figure this out.

I have the following global plugin that will display a "No data to display" message when applicable:

Chart.plugins.register({
  afterDraw: function(chart, args, options) {
    if (chart.data.datasets.every(dataSetContainsNoData)) {
        debugger;
        var ctx = chart.chart.ctx;
        var width = chart.chart.width;
        var height = chart.chart.height;
        chart.clear();

        ctx.save();
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText('No data to display', width / 2, height / 2);
    }
}
});

However, I would like to keep the title of this chart showing at the top, and can't figure out how to do it. If I inspect chart.chart.options the title option has the text set, and the display flag is set to true.

Here are the options that are on the actual chart.

options: {
        title: { display: true, text: 'Chart Header Text' },
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }

Any help is greatly appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You will need to comment out chart.clear() and title should work.

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