score:8

Accepted answer

it is certainly possible to achieve a 'right' aligned scale tick label instead of the original 'center' aligned scale tick label, but unfortunately it is not very straight forward to implement. let me walk you through how to do it and then provide an example.

1) first, since there is no configuration option to control this, we have to look at doing some sort of custom implementation. it turns out that the scale tick labels in a bar chart are rendered as part of the category scale's draw method. therefore, we must somehow overwrite this draw method to change to a new alignment.

2) according to the api there is a documented way to create new scale types, so we should be able to use a similar approach to extend the category scale type and overwrite it's draw method.

since all scales are wrapped up in the scaleservice we have to use the below approach to extend an existing scale type.

var categoryrightaligned = chart.scaleservice.getscaleconstructor('category').extend({});

3) now its just a matter of figuring out what part of the draw method we need to modify. after looking it over, it looks like we need to change the logic for calculating labelx (the pixel position to render the tick label). here would be the new logic.

// current logic for getting pixel value of each label (we will use the logic below to 
// adjust if necessary)
labelx = me.getpixelfortick(index, gridlines.offsetgridlines) + optionticks.labeloffset;

// get a reference to the bar chart controller so we can determine the chart's bar width
var meta = me.chart.getdatasetmeta(0);

// use the bart chart controller to calculate the bar width
var barwidth = meta.controller.calculatebarwidth(meta.controller.getruler());

// if the labels are rotated, then move the pixel location from the middle of the bar 
// to the far right of the bar
if (labelrotationradians != 0) {
  labelx += barwidth / 2;
}

4) now we just need to register our new scale and configure the chart to use it (instead of the bar chart default category scale).

chart.scaleservice.registerscaletype('categoryrightaligned', categoryrightaligned, {position: 'bottom'});

xaxes: [{
  type: 'categoryrightaligned',
  gridlines: {
    display : false,
    offsetgridlines: true
  },
  ticks: {
    beginatzero:true,
  }
}]

refer to this jsfiddle example to see it in action and to see how everything fits together.


Related Query

More Query from same tag