How to set ChartJS Y axis title?

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

I am using Chartjs for showing diagrams and I need to set title of y axis, but there are no information about it in documentation.

I need y axis to be set like on picture, or on top of y axis so someone could now what is that parameter

I have looked on official website but there was no information about it

回答1:

You can tweak the options and extend the chart type to do this, like so

Chart.types.Line.extend({     name: "LineAlt",     draw: function () {         Chart.types.Line.prototype.draw.apply(this, arguments);          var ctx = this.chart.ctx;         ctx.save();         // text alignment and color         ctx.textAlign = "center";         ctx.textBaseline = "bottom";         ctx.fillStyle = this.options.scaleFontColor;         // position         var x = this.scale.xScalePaddingLeft * 0.4;         var y = this.chart.height / 2;         // change origin         ctx.translate(x, y);         // rotate text         ctx.rotate(-90 * Math.PI / 180);         ctx.fillText(this.datasets[0].label, 0, 0);         ctx.restore();     } }); 

calling it like this

var ctx = document.getElementById("myChart").getContext("2d"); var myLineChart = new Chart(ctx).LineAlt(data, {     // make enough space on the right side of the graph     scaleLabel: "          " }); 

Notice the space preceding the label value, this gives us space to write the y axis label without messing around with too much of Chart.js internals


Fiddle - http://jsfiddle.net/wyox23ga/




回答2:

In Chart.js version 2.0 this is possible:

options = {   scales: {     yAxes: [{       scaleLabel: {         display: true,         labelString: 'probability'       }     }]   } } 

See scales documentation for more details.



回答3:

For x and y axes:

     options : {       scales: {         yAxes: [{           scaleLabel: {             display: true,             labelString: 'probability'           }         }],         xAxes: [{           scaleLabel: {             display: true,             labelString: 'hola'           }         }],       }     } 


回答4:

chart.js supports this by defaul check the link. chartjs

you can set the label in the options attribute.

options object looks like this.

options = {             scales: {                 yAxes: [                     {                         id: 'y-axis-1',                         display: true,                         position: 'left',                         ticks: {                             callback: function(value, index, values) {                                 return value + "%";                             }                         },                         scaleLabel:{                             display: true,                             labelString: 'Average Personal Income',                             fontColor: "#546372"                         }                     }                    ]             }         }; 


回答5:

For me it works like this:

    options : {       scales: {         yAxes: [{           scaleLabel: {             display: true,             labelString: 'probability'           }         }]       }     } 


回答6:

Consider using a the transform: rotate(-90deg) style on an element. See http://www.w3schools.com/cssref/css3_pr_transform.asp

Example, In your css

.verticaltext_content {   position: relative;   transform: rotate(-90deg);   right:90px;   //These three positions need adjusting   bottom:150px; //based on your actual chart size   width:200px; } 

Add a space fudge factor to the Y Axis scale so the text has room to render in your javascript.

scaleLabel: "      " 

Then in your html after your chart canvas put something like...

Y Axis Label

It is not the most elegant solution, but worked well when I had a few layers between the html and the chart code (using angular-chart and not wanting to change any source code).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!