如何设置ChartJS Y轴标题?

emi*_*mir 51 javascript jquery charts chart.js

我使用Chartjs来显示图表,我需要设置y轴的标题,但在文档中没有关于它的信息.

我需要像在图片上一样设置y轴,或者在y轴的顶部设置,所以有人可以现在是什么参数

我在官方网站上看过,但没有关于它的信息

and*_*sit 161

在Chart.js版本2.0中,这是可能的:

options = {
  scales: {
    yAxes: [{
      scaleLabel: {
        display: true,
        labelString: 'probability'
      }
    }]
  }
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅秤文档.

  • 这对我不起作用 - 也许还有另一个步骤也与让标签显示相关? (2认同)

pot*_*ngs 29

您可以调整选项并扩展图表类型来执行此操作,就像这样

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();
    }
});
Run Code Online (Sandbox Code Playgroud)

这样称呼它

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: "          <%=value%>"
});
Run Code Online (Sandbox Code Playgroud)

注意标签值之前的空格,这为我们提供了写入y轴标签的空间,而没有弄乱太多的Chart.js内部


小提琴 - http://jsfiddle.net/wyox23ga/


在此输入图像描述


小智 19

对于x和y轴:

     options : {
      scales: {
        yAxes: [{
          scaleLabel: {
            display: true,
            labelString: 'probability'
          }
        }],
        xAxes: [{
          scaleLabel: {
            display: true,
            labelString: 'hola'
          }
        }],
      }
    }
Run Code Online (Sandbox Code Playgroud)


Raj*_*jiv 10

chart.js通过默认检查链接来支持此功能. chartjs

您可以在options属性中设置标签.

options对象看起来像这样.

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"
                        }
                    }   
                ]
            }
        };
Run Code Online (Sandbox Code Playgroud)


小智 8

对于 Chart.js 3.x

options: {
  scales: {
    y: {
      title: {
        display: true,
        text: 'Y axis title'
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

对我来说,它是这样工作的:

    options : {
      scales: {
        yAxes: [{
          scaleLabel: {
            display: true,
            labelString: 'probability'
          }
        }]
      }
    }
Run Code Online (Sandbox Code Playgroud)