Skip to content
the poor coder

hide label chratjs

Poor Man
Published
Reading time
4 min read
On this page

How to Hide Label in Chartjs#

Introduction#

Chartjs is a popular open-source JavaScript library that is used to create interactive and customizable charts and graphs for web applications. It provides various types of charts like line, bar, pie, etc., with a rich set of customization options. One such customization is hiding labels in Chartjs charts.

Method 1: Using CSS#

One way to hide labels in Chartjs is by using CSS. This method involves targeting the specific label element and setting its display property to none. Here's how you can do it:

Text

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

<style>
  .chart-container #myChart .chartjs-size-monitor, 
  .chart-container #myChart .chartjs-size-monitor-expand,
  .chart-container #myChart .chartjs-size-monitor-shrink {
    position: absolute;
    direction: ltr;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    pointer-events: none;
    visibility: hidden;
    z-index: -1;
  }

  #myChart .chartjs-hidden-iframe {
    display: none;
  }

  #myChart .chartjs-tooltip {
    opacity: 1;
    transition: all .1s ease;
    position: absolute;
    background-color: rgba(0, 0, 0, .8);
    color: white;
    border-radius: 3px;
    -webkit-transform: translate(-50%, -75%);
    transform: translate(-50%, -75%);
    padding: .5em .7em;
    pointer-events: none;
    z-index: 99999999;
    font-size: .857em;
    font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
  }

  #myChart .chartjs-tooltip-key {
    display: inline-block;
    width: 10px;
    height: 10px;
    margin-right: .5em;
    border-radius: 1px;
  }

  #myChart .chartjs-tooltip-value {
    display: inline-block;
  }

  #myChart .chartjs-tooltip-value::before {
    display: inline-block;
    content: "";
    border-color: transparent transparent rgba(0, 0, 0, .8) transparent;
    border-style: solid;
    border-width: .4em .4em 0 .4em;
    position: absolute;
    bottom: -.4em;
    left: 50%;
    transform: translateX(-50%);
  }

  #myChart .chartjs-tooltip-callback {
    background-color: white;
    border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, .2);
    box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
    padding: .5em;
    font-size: .857em;
    font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
    line-height: 1.2em;
  }

  #myChart .chartjs-tooltip-callback:before {
    display: inline-block;
    content: "";
    border-color: transparent transparent white transparent;
    border-style: solid;
    border-width: .4em .4em 0 .4em;
    position: absolute;
    top: -.4em;
    left: 50%;
    transform: translateX(-50%);
  }

  #myChart .chartjs-tooltip-callback:after {
    display: inline-block;
    content: "";
    border-color: transparent transparent rgba(0, 0, 0, .2) transparent;
    border-style: solid;
    border-width: .4em .4em 0 .4em;
    position: absolute;
    top: -.4em;
    left: 50%;
    transform: translateX(-50%);
  }

  #myChart .chartjs-tooltip-callback-header {
    margin-bottom: .3em; 
    font-weight: bold;
  }

  #myChart .chartjs-tooltip-callback-body {
    margin-bottom: .3em; 
  }

  #myChart .chartjs-tooltip-callback-footer {
    text-align: right;
  }

  #myChart .chartjs-tooltip-callback-footer button {
    border: none;
    background-color: white;
    padding: .2em .5em;
    margin-left: .5em;
    border-radius: 3px;
    color: #1e90ff;
    cursor: pointer;
  }

  #myChart .chartjs-tooltip-callback-footer button:hover {
    background-color: #1e90ff;
    color: white;
  }

  #myChart .chartjs-tooltip-callback-error {
    color: red;
  }

  #myChart .chartjs-tooltip-callback-warning {
    color: orange;
  }

  #myChart .chartjs-tooltip-callback-success {
    color: green;
  }

  #myChart .chartjs-tooltip-filter {
    display: none; 
  }
</style>

In the code above, we have targeted the specific label element using the CSS selector and set its display property to none. This hides the label from the chart.

Method 2: Using Chartjs Options#

Another way to hide labels in Chartjs is by using the options provided by Chartjs. This method involves setting the display property of the label to false in the chart configuration. Here's how you can do it:

Text

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

<script>
  var ctx = document.getElementById('myChart').getContext('2d');
  var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
      datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3, 7],
        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)',
          'rgba(255, 99, 132, 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)',
          'rgba(255, 99, 132, 1)'
        ],
        borderWidth: 1
      }]
    },
    options: {
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true
          }
        }]
      },
      legend: {
        display: false
      }
    }
  });
</script>

In the code above, we have set the display property of the legend to false in the chart configuration. This hides the labels from the chart.

Conclusion#

Hiding labels in Chartjs is a simple and easy customization that can be done using either CSS or Chartjs options. Both methods are effective and can be used depending on your preference and requirements. By hiding labels, you can make your chart more compact and visually appealing.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC