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

235
Visualizações
ChartJS: single bar of bar chart. I want a border around entire bar, including the max Y-value

I want to create a single bar using a bar chart. The setup is that the user can select different choices (representing the colors). In this example (codesandbox link below) I have a bar that has a max value of 90.000. The three chosen values are 20.000, 30.000 and 15.000, totaling 65.000.

Now, my goal is to have a border around this entire bar, not just the colors. In the image below this is represented with the red border. Currently I do this by putting a container element around my canvas element, but I would like to do this in the canvas itself, without using a container element. Someone has an idea on how to do this?

Codesandbox link

enter image description here

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to define different dataset properties such as borderSkipped, borderRadius, borderWidth to achieve what you're looking for.

Don't know why but I also had to define the data of the dataset at the bottom as a floating bar in order to see the rounded border.

data: [[0, 20000]]

Please take a look at the runnable code below and see how it could work.

new Chart('chart', {
  type: 'bar',
  data: {
    labels: [''],
    datasets: [{
        label: "currentAmount",
        data: [[0, 20000]],
        backgroundColor: "#bbb",
        borderColor: "#f00",
        borderWidth: 2,
        borderSkipped: 'top',
        borderRadius: 30,
        barPercentage: 0.5
      },
      {
        label: "amount 1",
        data: [30000],
        backgroundColor: "orange",
        borderColor: "#f00",
        borderWidth: { left: 2, right: 2 },
        barPercentage: 0.5
      },
      {
        label: "amount 2",
        data: [15000],
        backgroundColor: "green",
        borderColor: "#f00",
        borderWidth: { left: 2, right: 2 },
        barPercentage: 0.5
      },
      {
        label: "remaining",
        data: [25000],
        backgroundColor: "#fff",
        borderColor: "#f00",
        borderWidth: 2,
        borderSkipped: 'bottom',
        borderRadius: 30,
        barPercentage: 0.5
      },
    ]
  },
  options: {
    plugins: {
      legend: {
        display: false
      },
      tooltip: {
        displayColors: false
      }
    },
    scales: {
      y: {
        display: false,
        stacked: true,
        beginsAtZero: true
      },
      x: {
        display: false,
        stacked: true
      }
    }
  }
});
canvas {
  max-width: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<canvas id="chart"></canvas>

about 4 years ago · Santiago Trujillo 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