[Chartjs]-Tick label overlaps axis label

0👍

In the end I had to compromise and shrink the font size, removing the x and y labels and updating the legend to provide the detail needed.

enter image description here

var ctx = document.getElementById("myChart").getContext("2d");
var chart = new Chart(ctx, {
  // The type of chart we want to create
  type: "bar",

  // The data for our dataset
  data: {
    labels: [
      "0k-40k",
      "40k-80k",
      "80k-100k",
      "100k-120k",
      "120k-160k",
      "160k-180k",
      "180k-220k"
    ],
    datasets: [
      {
        label: "My dataset x vs y",
        backgroundColor: "rgb(255, 99, 132)",
        borderColor: "rgb(255, 99, 132)",
        data: [0, 10, 5, 2, 20, 30, 45]
      }
    ]
  },

  // Configuration options go here
  options: {
    legend: {
      display: true
    },
    scales: {
      xAxes: [
        {
          ticks: {
            autoSkip: false,
            fontSize: 11,
            fontStyle: "bold",
            precision: 2,
            suggestedMin: 0
          }
        }
      ],
      yAxes: [
        {
          ticks: {
            fontSize: 11,
            fontStyle: "bold",
            precision: 2,
            suggestedMin: 0
          }
        }
      ]
    }
  }
});

https://codepen.io/afisher88/pen/XoNVNe

0👍

var ctx = document.getElementById("myChart").getContext("2d");
var chart = new Chart(ctx, {
  // The type of chart we want to create
  type: "bar",

  // The data for our dataset
  data: {
    labels: [
      "0k-40k",
      "40k-80k",
      "80k-100k",
      "100k-120k",
      "120k-160k",
      "160k-180k",
      "180k-220k"
    ],
    datasets: [
      {
        label: "My First dataset",
        backgroundColor: "rgb(255, 99, 132)",
        borderColor: "rgb(255, 99, 132)",
        data: [0, 10, 5, 2, 20, 30, 45]
      }
    ]
  },

  // Configuration options go here
  options: {
    legend: {
      display: false
    },
    scales: {
      xAxes: [
        {
          ticks: {
            autoSkip: false,
            fontSize: 16,
            fontStyle: "bold",
            precision: 2,
            suggestedMin: 0
          },
          scaleLabel: {
            display: true,
            fontSize: 16,
            fontStyle: "bold",
            labelString: "Chart Label X",
            padding:{
              bottom: -5,
            }
          }
        }
      ],
      yAxes: [
        {
          ticks: {
            fontSize: 16,
            fontStyle: "bold",
            precision: 2,
            suggestedMin: 0
          },
          scaleLabel: {
            display: true,
            fontSize: 16,
            fontStyle: "bold",
            labelString: "Chart Label Y",
          }
        }
      ]
    }
  }
});

-1👍

Different browsers can render the canvas image in many different ways. Small screen devices are not an exception. You can try your code with different browsers on normal screen to assert your claim.

chart1

Chart.js have many options you can use for your code. i.e. You can use “Chart.bundle.js” or “Chart.js” depending on your environment. The CSS you use can also affect how your chart is rendered on a canvas. The bundled build includes Moment.js in a single file. You should use this version if you require time axes and want to include single file.

For example:

<!DOCTYPE html>
<!--
pcharts.html
-->
<html>
    <head>
        <title>P Charts</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../public/css/main.css">
        <script src="../lib/charts/Chart.bundle.js"></script>
        <script src="../lib/jquery/jquery.js"></script>
        <script src="../js/appcharts.js"></script>
    </head>
    <body>
        <h1>Charts</h1>
        <hr>
        <br>
        <div id="myCan">
            <canvas id="myChart" width="100" height="100"></canvas>
        </div>
        <br>
        <br><br><hr><br><br>
    </body>
</html>

appcharts.js

/* 
 * appcharts.js
 * 
 */

var ctx;
var myChart;
var cdata = [0, 10, 5, 2, 20, 30, 45];
var lblX = ["0k-40k","40k-80k","80k-100k","100k-120k","120k-160k","160k-180k","180k-220k"];


function init(){
    render();
};

function render(){
    ctx = document.getElementById("myChart").getContext('2d');
    myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: lblX,
        datasets: [{
            label: 'My demo2 dataset.',
            data: cdata,
            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: {
        scales: {
            xAxes: [{
                ticks:{
                    beginAtZero:true        
                },
                scaleLabel:{
                    display: true,
                    labelString: "Chart Label X."
                }
            }],
            yAxes: [{
                ticks: {
                    beginAtZero:true
                },
                scaleLabel: {
                    display: true,
                    labelString: "Chart Label Y."
                }
            }]
        }
    }
});
};

//
//$(document).ready(function(){
//    $("#myCan").hide("slow");
//    $("#myCan").show("slow");
//});
//

window.onload = function(){
  init();  
};
//
//

chart2


Squeezing the big image in a small canvas is like squeezing big tomatoes in a small bottle. What will come out of that bottle is the tomato sauce and tomato seeds. Of course you can replant the tomato seeds if only and only if they are still OK. Orthographic projection do just that. You can render a big
image in a small screen by using orthographic projection technique. e.g. Positioning of the camera or distortion determines what the viewer will see or look at.

When you are using charts you are actually drawing an image and rendering it on a screen for the viewer to look at. You can get the latest version of chart.js by using: npm install chart.js --save or download it from: https://github.com/chartjs/Chart.js/releases/latest It comes with docs and samples.

To create a chart, we need to instantiate the Chart class. To do this, we need to pass in the node, jQuery instance, or 2d context of the canvas of where we want to draw the chart. Once you have the element or context, you’re ready to instantiate a pre-defined chart-type or create your own.

Good luck.


Leave a comment