繁体   English   中英

Chart.js 如何更改 y 轴标题位置

[英]Chart.js How to change y axis title position

我正在使用 chart.js 库来创建图表,我快完成了。 最后一件事是将 yAxis 的标题定位到图例下方的顶部。

我目前有这样的事情。

https://i.stack.imgur.com/FD5s7.png

我正在寻找的结果是这样的。

https://i.stack.imgur.com/4T5l3.png

我真的很感激任何帮助。

代码: https : //jsfiddle.net/w2qzjhcb/2/

HTML

<canvas id="myChart"></canvas>

JavaScript

    var maxValue = 180000;
var ctx = document.getElementById("myChart").getContext('2d');

Chart.defaults.global.defaultFontFamily = "Arial";
Chart.defaults.global.defaultFontSize = 18;
Chart.defaults.global.defaultFontColor = 'black';
Chart.defaults.global.plugins.datalabels.font.size = '15';
Chart.defaults.global.plugins.datalabels.font.weight = 'bold';

function converToSpaced(item){
    tisice = (Math.floor(item/1000)).toString();
    stovky = (item-(Math.floor(item/1000))*1000).toString();

    switch (stovky.length){
        case 1:
            stovky = "00" + stovky;
            break;
        case 2:
            stovky = "0" + stovky;
            break;
        case 3:                                
            break;                                
    }
    return tisice + ' ' + stovky;
};


var data = {
    labels: ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018"],
    datasets: [
        {
            label: 'zisk Veolia, a.s. v mil. Kč',
            data: [
                61743,
                95242,
                125991,
                131192,
                130616,
                149573,
                155744,
                139540,
                128046,
                113326
            ],
            backgroundColor: 'black'
        },
        {            
            label: 'zisk VHS OL',
            data:[
                633,
                3425,
                10636,
                12130,
                13708,
                17095,
                23804,
                17787,
                21820,
                20179
          ],
            backgroundColor: '#000e4b'
        },
        {
            label: 'zisk VHS OL',
            data:[
            9123,
                9123,
                6748,
                6435,
                -12623,
                5732,
                7304,
                11167,
                9149,
                20437
          ],
            backgroundColor: '#009de0'
        },
        {
            label: 'zisk VHS OL',
            data:[
                5202,
                6153,
                1803,
                1480,
                1845,
                2403,
                3258,
                3020,
                3343,
                3726
          ],
            backgroundColor: '#55d9ff'
        },
        {
            label: 'zisk VHS OL',
            data:[
                14958,
                12393,
                19187,
                20045,
                2930,
                25230,
                34366,
                31974,
                34312,
                44342
          ],
            backgroundColor: 'white',
            borderWidth: 1,
            borderColor: 'black'
        }
    ]
};

var stovky;
var tisice;

var myChart = new Chart(ctx, {
    type: 'bar',
    data: data,

    options: {       
        scales:{
            yAxes: [{
                ticks: {
                    min: 0,
                    stepSize: 10000,
                    max: maxValue,
                    userCallback: function(item){
                        return converToSpaced(item); 
                    }
                },
                scaleLabel:{
                    display: true,
                    labelString: "Kč | m" + ('\u00B3')
                }
            }],
            xAxes: [{
                barPercentage: 1,
                categoryPercentage: 1,              
            }]
        },
        legend:{
          display: true,
          position:'top',
          labels:{
            fontColor:'#000',
              fontStyle: 'bold' 
          }
        },  
        tooltips:{
            enabled: true,  
            callbacks: {
                label: function(tooltipItems, data) { 
                    if(tooltipItems.yLabel >= 1000){
                        tisice = (Math.floor(tooltipItems.yLabel/1000)).toString();
                        stovky = (tooltipItems.yLabel-(Math.floor(tooltipItems.yLabel/1000))*1000).toString();

                        switch (stovky.length){
                            case 1:
                                stovky = "00" + stovky;
                                break;
                            case 2:
                                stovky = "0" + stovky;
                                break;
                            case 3:                                
                                break;                                
                        }
                                    return tisice + ' ' + stovky + ' Kč';
                                }
                            else if(tooltipItems.yLabel <= -1000){
                                tisice = (Math.ceil(tooltipItems.yLabel/1000)).toString();
                                stovky = (Math.abs(tooltipItems.yLabel)-Math.abs((Math.ceil(tooltipItems.yLabel/1000))*1000)).toString();

                                switch (stovky.length){
                            case 1:
                                stovky = "00" + stovky;
                                break;
                            case 2:
                                stovky = "0" + stovky;
                                break;
                            case 3:                                
                                break;                                
                        }
                                return tisice + ' ' + stovky + ' Kč';
                            }
                                else{
                                    return tooltipItems.yLabel + ' Kč';
                                } 
                    },
                title: function(tooltipItems, data){                                  
                    return data.datasets[tooltipItems[0].datasetIndex].label + " za rok " + tooltipItems[0].xLabel;//                    
                }
            }            
        },

        plugins: {
                    datalabels: {
                        display: true,
                        align: 'end',
                        anchor: 'end',
                        rotation: 270,

                        color: function(value){
                            if(value.dataset.data[value.dataIndex] <= 0){
                                    return 'red';
                                }
                                else{
                                    return 'black';
                                }
                        },

                        formatter: function(value){ 
                            if(value >= 1000){
                                    tisice = (Math.floor(value/1000)).toString();
                        stovky = (value-(Math.floor(value/1000))*1000).toString();
                        switch (stovky.length){
                            case 1:
                                stovky = "00" + stovky;
                                break;
                            case 2:
                                stovky = "0" + stovky;
                                break;
                            case 3:                                
                                break;                                
                        }
                                    return tisice + ' ' + stovky;
                                }
                            else if(value <= -1000){
                                tisice = (Math.ceil(value/1000)).toString();
                                stovky = (Math.abs(value)-Math.abs((Math.ceil(value/1000))*1000)).toString();

                                switch (stovky.length){
                            case 1:
                                stovky = "00" + stovky;
                                break;
                            case 2:
                                stovky = "0" + stovky;
                                break;
                            case 3:                                
                                break;                                
                        }
                                return tisice + ' ' + stovky;
                            }
                                else{
                                    return value;
                                } 
                        }                        

                    }
                }        
    }
});

您可以使用options.scales.x.title.padding参数使用自定义样式将轴标签移动到您想要的位置,如下例所示:

 options: {
                    
             scales: {
                        x: {
                            beginAtZero: true,
                            display: true,
                            title: {
                                display: true,
                                text: 'count',
                                padding: {top: -5, left: 0, right: 0, bottom: 0}
                            },
                            ticks: {
                                stepSize: 1,
                            }
                        },
                        
                 
                    }

不幸的是,至少目前,这是不可能的。 比例标题选项不支持此类位置更改。 更多信息请参见此处的文档:
https://www.chartjs.org/docs/latest/axes/labelling.html

也许在 Chart.js github 上提出功能请求会有意义?
https://github.com/chartjs/Chart.js/issues

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM