score:11

Accepted answer

as per chartjs documentation:

detecting when the canvas size changes can not be done directly from the canvas element. chart.js uses its parent container to update the canvas render and display sizes. however, this method requires the container to be relatively positioned and dedicated to the chart canvas only. responsiveness can then be achieved by setting relative values for the container size

source: https://www.chartjs.org/docs/latest/general/responsive.html

you should wrap your canvas into div and add width,height into it.

here is the change i did

<div style="width:240px;height:240px">
    <canvas id="layanan"></canvas>
    </div>
    <div style="width:240px;height:240px">
    <canvas id="layanan_subbagian" ></canvas>
    </div>

$(function () {
            var ctx = document.getelementbyid("layanan").getcontext('2d');
            var data = {
                datasets: [{
                    data: [10, 20, 30],
                    backgroundcolor: [
                        '#3c8dbc',
                        '#f56954',
                        '#f39c12',
                    ],
                }],
                labels: [
                    'request',
                    'layanan',
                    'problem'
                ]
            };
            var mydoughnutchart = new chart(ctx, {
                type: 'doughnut',
                data: data,
                options: {
                    maintainaspectratio: false,
                    legend: {
                        position: 'bottom',
                        labels: {
                            boxwidth: 12
                        }
                    }
                }
            });

            var ctx_2 = document.getelementbyid("layanan_subbagian").getcontext('2d');
            var data_2 = {
                datasets: [{
                    data: [10, 20, 30],
                    backgroundcolor: [
                        '#3c8dbc',
                        '#f56954',
                        '#f39c12',
                    ],
                }],
                labels: [
                    'request',
                    'layanan',
                    'problem'
                ]
            };
            var mydoughnutchart_2 = new chart(ctx_2, {
                type: 'doughnut',
                data: data_2,
                options: {
                    maintainaspectratio: false,
                    legend: {
                        position: 'bottom',
                        labels: {
                            boxwidth: 12
                        }
                    }
                }
            });
        });
<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.7.3/chart.js"></script>

    <title>document</title>
    

    <div style="width:240px;height:240px">
    <canvas id="layanan"></canvas>
    </div>
    <div style="width:240px;height:240px">
    <canvas id="layanan_subbagian" ></canvas>
    </div>

score:2

i found two solutions:

  1. you have to put the charts in a container such as a div. <canvas> is an element semantically dedicated for drawing graphics dynamically via scripting. <div> is a general-purpose container. the important point is: width and height properties are not the size in px but the ratio between them. <canvas id="layanan" width="240px" height="240px"></canvas> would result into a 1:1 ratio, but you need a parent container to work with. in the example below, i put a div around each canvas.

  2. you can disable this feature by setting maintainaspectratio to false. removing the divs from my code and setting this into yours gives the same result :)

cheers!

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.7.3/chart.js"></script>

    <title>document</title>
    <script>
        $(function () {
            var ctx = document.getelementbyid("layanan").getcontext('2d');
            var data = {
                datasets: [{
                    data: [10, 20, 30],
                    backgroundcolor: [
                        '#3c8dbc',
                        '#f56954',
                        '#f39c12',
                    ],
                }],
                labels: [
                    'request',
                    'layanan',
                    'problem'
                ]
            };
            var mydoughnutchart = new chart(ctx, {
                type: 'doughnut',
                data: data,
                options: {
                    responsive: false,
                    maintainaspectratio: false,
                    legend: {
                        position: 'bottom',
                        labels: {
                            boxwidth: 12
                        }
                    }
                }
            });

            var ctx_2 = document.getelementbyid("layanan_subbagian").getcontext('2d');
            var data_2 = {
                datasets: [{
                    data: [10, 20, 30],
                    backgroundcolor: [
                        '#3c8dbc',
                        '#f56954',
                        '#f39c12',
                    ],
                }],
                labels: [
                    'request',
                    'layanan',
                    'problem'
                ]
            };
            var mydoughnutchart_2 = new chart(ctx_2, {
                type: 'doughnut',
                data: data_2,
                options: {
                    responsive: false,
                    maintainaspectratio: false,
                    legend: {
                        position: 'bottom',
                        labels: {
                            boxwidth: 12
                        }
                    }
                }
            });
        });

    </script>
</head>

<body>
  <div>
      <canvas id="layanan" width="240px" height="240px"></canvas>
  </div>

  <div>
      <canvas id="layanan_subbagian" width="240px" height="240px"></canvas>
  </div>
</body>

</html>

score:3

try to add

options: {

            responsive: false
        }

Related Query

More Query from same tag