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

前端 未结 3 1941
慢半拍i
慢半拍i 2020-12-16 00:54

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<

相关标签:
3条回答
  • 2020-12-16 01:31

    okay maby a bit late ;)

    how can we show the last tick in the second x-axis row?

    with the code from above, we return a empty string. i want to see the label of the last point.

          ticks:{
            callback:function(label){
              var month = label.split(";")[0];
              var year = label.split(";")[1];
              if(month === "February"){
                return year;
              }else{
                return "";  **<==== ???**
              }
    

    thx for the help.

    EDIT

    i change it a bit but not complete like i will i don't want the label at 30 and 31 just the last day w.a.w 31 label , 30 not a label

    month ended at 30 => label

                return month;
              }else if 
                (Nbrday === "31"){
                return month;
              }else if 
                (Nbrday === "30"){
                return month;
              }
                else{
                  // return month;
                return "";
    
    0 讨论(0)
  • 2020-12-16 01:49

    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>

    0 讨论(0)
  • 2020-12-16 01:56
    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",
          }],
        }, 
      },
    });
    

    0 讨论(0)
提交回复
热议问题