Chart.js different x axis and tooltip format settings - Javascript Chart.js

Javascript examples for Chart.js:Chart Tooltip

Description

Chart.js different x axis and tooltip format settings

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from w  w  w .ja  v  a 2 s  .c  o m*/
var example2 = [229, 113, 109];
var labels2 = ["Presentation", "Super long label that will be trimmed", "Careers Fair"];
var barChartData2 = {
    labels: labels2,
    datasets: [{
        label: 'Student Count',
        backgroundColor: '#ccece6',
        data: example2
    }]
};
function drawChart(el, data, title) {
    var ctx = document.getElementById(el).getContext("2d");
    var bar = new Chart(ctx, {
        type: 'bar',
        data: data,
        options: {
            elements: {
                rectangle: {
                    borderWidth: 2,
                    borderColor: '#98c4f9',
                    borderSkipped: 'bottom'
                }
            },
            responsive: true,
            legend: {
                position: 'bottom',
            },
            title: {
                display: true,
                text: title
            },
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: 0,
                        min: 0
                    }
                }],
                xAxes: [{
                    ticks: {
                        autoSkip: false,
                        callback: function(tick) {
                            var characterLimit = 20;
                            if (tick.length >= characterLimit) {
                                return tick.slice(0, tick.length).substring(0, characterLimit - 1).trim() + '...';;
                            }
                            return tick;
                        }
                    }
                }]
            },
         tooltips: {
               callbacks: {
                   title: function(tooltipItem){
                       return this._data.labels[tooltipItem[0].index];
                    }
                }
            }
        }
    });
    console.log(bar);
};
drawChart('canvas0', barChartData2, 'example title');
    }

      </script> 
   </head> 
   <body> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.js"></script> 
      <div id="container"> 
         <canvas id="canvas0"></canvas> 
         <canvas id="canvas1"></canvas> 
         <canvas id="canvas2"></canvas> 
      </div>  
   </body>
</html>

Related Tutorials