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

105
Visualizações
Chartjs small offset on top

HTML

<body>
    
    <div style = "width:100%; height:100%; display:flex; align-items:center; justify-content:center;">
            
            <div style = "width:300px; height:300px; display:flex; align-items:center; justify-content:center; background-color:blue">
                
                <canvas id="canvas" style = "width:300px; height:300px; background-color:green"></canvas>
            
            </div>
    
    </div>
    
</body>

JAVASCRIPT

<script>

var canvas  = document.getElementById("canvas");
var context = canvas.getContext("2d"); 
var radius  = 0.5;

var options = 
{
 title:
 {
  display:false,
 },
  
 legend:
 {
  display:false
 },  
  
 tooltip:
 {
  enabled:false
 }, 

 animation: 
 {
  duration: 0
 }
}

var data = 
{
 datasets: 
 [
  {
   data:            [50, 50],
   backgroundColor: ["blue", "red"],
   borderWidth:     0
  }
 ]
}


var chart = 
new Chart(context, 
{
 type:       "doughnut",
 
 cutout:     100 * radius,
 responsive: true,
  
 data:       data, 
 options:    options

}); 

</script>

FIDDLE

https://jsfiddle.net/t5a41rdj/

As per title, somehow this chart has a little offset on top which I can't seem to be able to get rid of, despite having disabled legend and title.

Anything obvious I am missing?

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

0

This is because you did not disable the legend, title or tooltip. If you hover your chart you can see the tooltip is still working, title is hidden by default. Why the legend does not show I dont really know, should still show 1 item with undefined as text. But if you disable it in the correct space with is the plugins namespace you see your extra padding goes away.

Also cutout and responsive have to be configured in the options not in the root of new Chart:

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var radius = 0.5;

var options = {
  plugins: {
    legend: {
      display: false
    },
    tooltip: {
      enabled: false
    }
  },
  animation: {
    duration: 0
  },
  cutout: 100 * radius,
  responsive: true,
}

var data = {
  datasets: [{
    data: [50, 50],
    backgroundColor: ["blue", "red"],
    borderWidth: 0,
  }]
}


var chart =
  new Chart(context, {
    type: "doughnut",
    data: data,
    options: options

  });
<body>
  <div style="width:100%; height:100%; display:flex; align-items:center; justify-content:center;">
    <div style="width:300px; height:300px; display:flex; align-items:center; justify-content:center; background-color:blue">
      <canvas id="canvas" style="width:300px; height:300px; background-color:green"></canvas>
    </div>
  </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

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