Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chartjs 2: Multi level/hierarchical category axis in chartjs

Is it possible to define a bar chart with a multi level/category axis?

For instance I'd like to display the Region/Province categories like in this Excel chart: enter image description here

I found this example using multiple xAxes.

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 "";
          }
        }
      }
    }
  ]

The problem is it seems that the two axes are not really linked and the alignment of second axis is based on values instead of aligning in middle of lower level category. this case cause issues

Is there a clean way to achieve this in chart.js?

Update: I ended up creating a feature request on chartjs.

like image 428
Sebastien Avatar asked Oct 27 '17 14:10

Sebastien


People also ask

What are the possible values of the axis of a chart?

Possible values are: 'x', 'y'. If not set, this is inferred from the first character of the ID which should be 'x' or 'y'. If true, extra space is added to the both edges and the axis is scaled to fit into the chart area.

What are the possible values for the scale of a chart?

Possible values are: 'x', 'y'. If not set, this is inferred from the first character of the ID which should be 'x' or 'y'. If true, extra space is added to the both edges and the axis is scaled to fit into the chart area. This is set to true for a bar chart by default. Scale title configuration. more... Type of scale being employed.

What is the use of stack axis?

Axes at the same position with same stack are stacked. Weight of the scale in stack group. Used to determine the amount of allocated space for the scale within the group. Which type of axis this is. Possible values are: 'x', 'y'.

What is the best tool to implement bar charts with sub Groupes?

ZingChart is a commercial tool and can be used to implement bar charts with sub groupes. But I prefer D3 over this library. because D3 comes under BSD License.


1 Answers

you can provide value separately for different axis via datesets and provide an object with different configuration option (borderColor, pointBackgroundColor, pointBorderColor) etc, i hope It'll help.

here is the link for the with an update (fiddle you shared) Updated Fiddle

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]
},

// here i added another data sets for xAxisID 2 and it works just fine
{
  label: '# of Potatoes',
  xAxisID:'xAxis2',
  data: [9, 17, 28, 26, 29, 9]
}]

}

I hope that solves your problem :)

like image 128
ishahadathb Avatar answered Oct 16 '22 02:10

ishahadathb