如何设置ChartJS x轴标题

我们使用Chartjs来绘制图表,但是没有给出x轴标题,这里有y轴标题的解决方案,但是对于x轴我可以添加x轴名称但是无法创建使用y作为this.chart.height将文本与x轴标题重叠,在图表下方的空间正确放置它。

我看过chartjs v2.0,但它也不支持x-Axis标题。

我们根据x轴标签所需的高度设置图表的高度,然后在该空间中写入。

我们不必在draw覆盖中做任何事情,因为canvas清除也是基于高度(我们调整)

预习

在此处输入图像描述


脚本

 Chart.types.Line.extend({ name: "LineAlt", initialize: function (data) { this.chart.height -= 30; Chart.types.Line.prototype.initialize.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.chart.width / 2; var y = this.chart.height + 15 + 5; // change origin ctx.translate(x, y) ctx.fillText("My x axis label", 0, 0); ctx.restore(); } }); 

然后

 ... new Chart(ctx).LineAlt(data); 

小提琴 – http://jsfiddle.net/ct2h2wde/