How to Center a Chart.js Graph

Viewed 63

I am looking to center a doughnut chart on a page using Chart.js. I have tried moving it using CSS and, changing its X and Y values under the options section of the script with no success. I am reading through their documentation and trying to understand where to place some of the configuration tools that are included in the library with little success. I have tried manipulating a container div and the canvas node itself.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .chart-container {
            display: flex;
            justify-content: center;
        }
    </style>
</head>
<body>

<!--create node-->
<div class="chart-container" style="position: relative; height: 30vw; width:20vw">
    <canvas id="myChart"></canvas>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>

    var ctx = document.getElementById("myChart").getContext("2d");

    var option = {
        responsive: true
    }

    var myChart = new Chart(ctx, {
        type: 'doughnut',
        options: option,
        data: {
            labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
            datasets: [{
                label: "Number of votes",
                data: [12, 19, 3, 5, 2, 3],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153, 102, 255, 0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
                borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ],
                borderWidth: 1

            }]
        },
        options: {
            responsive: true,
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            }
        }
    });


</script>


</body>
</html>

I am new to Chart.js so if there are any redundancies in my approach I would be grateful for the feedback.

1 Answers

I don't know Chart.js but I think this is the result you were looking for.

var ctx = document.getElementById("myChart").getContext("2d");

var myChart = new Chart(ctx, {
  type: 'doughnut',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: "Number of votes",
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1

    }]
  },
  options: {
    responsive: true,
    scales: {
      y: {
        ticks: {
          beginAtZero: true
        }
      }
    }
  }
});
.chart-container {
  display: flex;
  justify-content: center;
}

canvas {
  max-width: 400px;
  max-height: 400px;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<!--create node-->
<div class="chart-container">
    <canvas id="myChart"></canvas>
</div>

Related