I am using chart.js.
Similar to this Question, I would like to rotate my x-axis labels 90 degrees. Currently my labels are rotated about 80 degrees with default settings.
Could somebody help me adapt the bar-chart solution for rotating labels, so that I can use it on line-charts?
for x axis use this
and can filter the label with a
for
loop:If you are using chart.js 2.x, just set
maxRotation: 90
andminRotation: 90
in ticks options. It works for me! And if you want to all x-labels, you may want to setautoSkip: false
. The following is an example.Here's a slightly more hackier version (Quince's answer is better - the following may break if a future implementation of Chart.js does
calculateXLabelRotation
differently)The label rotation is calculated by progressively rotating the labels so that they fit between the vertical grid lines - the space between them is calculated using
scale.calculateX(1) - scale.calculateX(0)
. We jump in at the right point to force the result of this calculation to be 0 (by makingscale.calculateX
return the same value) - this in turn forces the rotation to progress to it's maximum (i.e. 90 degrees)Preview
Script
and then
Fiddle - http://jsfiddle.net/gc5gdg7e/
This answer is for chartjs 1.X for an answer covering 2.X take a look at the great answer from @tabetomo https://stackoverflow.com/a/39706986/2737978
Using the same method as in the previous answer the only thing that needs to changed is the extension of the graph type. This time it is extending the line chart and the set-up is a little different as the Line charts scale is created in the build scale so this time it is
buildScale
is overridden so that the custom scale is used and the new optionoverrideRotation
can be passed in.initialize
is also overridden but only so that the super initialize can be called and get the ball rolling on building the graph.