javascript - Horizontal xAxis labels in Chart.js - Stack Overflow

I'm working with a chart-js bar chart that has to be placed inside a small div so I was searching

I'm working with a chart-js bar chart that has to be placed inside a small div so I was searching for a way to make the labels in the x-axis horizontal instead of diagonal. The point is to gain as much height as possible for the actual bars because right now the labels consume almost 40% of the available space.

This is my code:

let options = {
    responsive: true,
    maintainAspectRatio: false,
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }],
        xAxes: [{
            display: false
        }]
    }
};
let ctx = document.getElementById('barChartCtx');
let barChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: options
});

I have temporarily disabled the xAxis labels. It works but ideally I would like them to be horizontal. Thoughts?

I'm working with a chart-js bar chart that has to be placed inside a small div so I was searching for a way to make the labels in the x-axis horizontal instead of diagonal. The point is to gain as much height as possible for the actual bars because right now the labels consume almost 40% of the available space.

This is my code:

let options = {
    responsive: true,
    maintainAspectRatio: false,
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }],
        xAxes: [{
            display: false
        }]
    }
};
let ctx = document.getElementById('barChartCtx');
let barChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: options
});

I have temporarily disabled the xAxis labels. It works but ideally I would like them to be horizontal. Thoughts?

Share asked Apr 1, 2017 at 15:08 dimlucasdimlucas 5,1417 gold badges40 silver badges54 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use the scale ticks maxRotation and minRotation properties to control this.

Here is an example.

let options = {
    responsive: true,
    maintainAspectRatio: false,
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }],
        xAxes: [{
            ticks: {
                minRotation: 0,
                maxRotation: 0
            }
        }]
    }
};

Keep in mind that the reason they are diagonal is because chart.js calculated that they would not fit at their default horizontal orientation. If you find that they overflow on each other when horizontal then you can still use the above properties to tweak the rotation so they are only slightly diagonal.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745074080a4609727.html

相关推荐

  • javascript - Horizontal xAxis labels in Chart.js - Stack Overflow

    I'm working with a chart-js bar chart that has to be placed inside a small div so I was searching

    14小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信