Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create two x-axes label using chart.js

Tags:

There is a way to create two label for y-axes. But how do you make a multiple x-axes label in chart.js? eg: example as in this picture: How to group (two-level) axis labels

like image 627
Alexander Dischberg Avatar asked Mar 21 '17 17:03

Alexander Dischberg


People also ask

How do you stagger X axis labels?

One way to solve this problem is to right-click the axis on the chart and select Format Axis from the shortcut menu. Then in the Format Axis task pane, under Labels, set the Label Position to Low. 21.

What are the labels on the horizontal and vertical axes?

Horizontal axis labels represent the X axis. They do not apply to pie, funnel, or gauge charts. Vertical axis labels represent the Y1 axis in a single axis chart. They represent a numeric scale, usually located on the left side of a vertical chart.


2 Answers

For v2 only (v3 see @LeeLenalee's answer)

This question has already been answered on github here

Here is a working JSFiddle

var ctx = $("#c");
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ["January;2015", "February;2015", "March;2015", "January;2016", "February;2016", "March;2016"],
    datasets: [{
      label: '# of Votes',
      xAxisID:'xAxis1',
      data: [12, 19, 3, 5, 2, 3]
    }]
  },
  options:{
    scales:{
      xAxes:[
        {
          id:'xAxis1',
          type:"category",
          ticks:{
            callback:function(label){
              var month = label.split(";")[0];
              var year = label.split(";")[1];
              return month;
            }
          }
        },
        {
          id:'xAxis2',
          type:"category",
          gridLines: {
            drawOnChartArea: false, // only want the grid lines for one axis to show up
          },
          ticks:{
            callback:function(label){
              var month = label.split(";")[0];
              var year = label.split(";")[1];
              if(month === "February"){
                return year;
              }else{
                return "";
              }
            }
          }
        }],
      yAxes:[{
        ticks:{
          beginAtZero:true
        }
      }]
    }
  }
});
<body>
<canvas id="c" width="400" height="300"></canvas>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
</body>
like image 102
Antoine Thiry Avatar answered Oct 05 '22 10:10

Antoine Thiry


var myChart = new Chart(ctx, {
  type: "line",
  data: {
    datasets: [{
      data: [20, 50, 100, 75, 25, 0],
      label: "Left dataset",

      // This binds the dataset to the left y axis
      yAxisID: "left-y-axis",
    }, {
      data: [0.1, 0.5, 1.0, 2.0, 1.5, 0],
      label: "Right dataset",

      // This binds the dataset to the right y axis
      yAxisID: "right-y-axis",
    }],
    labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
  },
  options: {
    scales: {
      yAxes: [{
        id: "left-y-axis",
        type: "linear",
        position: "left",
      }, {
        id: "right-y-axis",
        type: "linear",
        position: "right",
      }],
    }, 
  },
});

enter image description here

like image 28
Aqeel Bahoo Avatar answered Oct 05 '22 11:10

Aqeel Bahoo