如何从图表js 2中的负值开始从右侧0开始Y轴线?

时间:2017-07-07 12:54:00

标签: charts chart.js yaxis

我想知道是否有任何选项可以在图表js 2.6中设置,以便Y轴线从右到左从负值开始,在条形图中。

目前,这是出现负值的方式(小提示中的#2图表): https://jsfiddle.net/j6nqt4oo/1/

参考代码:

var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
    type: 'horizontalBar',
    data: {
        labels: ['A', 'B'],
            datasets: [{
                data: [-10, -20]
            }]
    },
    options: {
    responsive: false,
        showDatapoints: true,
            scales: {

                maintainAspectRatio: true,
                responsive: false,
                xAxes: [{

                    ticks: {
                       beginAtZero: true

                    },
                    gridLines: {
                        display: false
                    }
                }],
                yAxes: [{

                    gridLines: {
                        display: false
                    }
                }]
            },

            legend: {
                display: false
            }
    }

但这就是我想要的:( Y轴网格线为0,A和B位于右侧)

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要在第二张图表中将 position 属性设置为 right 以获取y轴刻度...

...
yAxes: [{
   position: 'right',
   gridLines: {
      display: false
   }
}]
...

以下是working demo on JSFiddle

相关问题