0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Chart.js のスケールタイトルを回転させる方法

Last updated at Posted at 2019-10-04

追記
xのpositionにright指定してたら狂いますね、そのうち直します

きっかけ

???「y軸が2つあるグラフのスケールタイトルは両方とも同じ向きなんですよね」

コード

Chart.js 自体のコードを改変します。
まず下記の部分を探します。

if (scaleLabel.display) {
    // Draw the scale label
    var scaleLabelX;
    var scaleLabelY;
    var rotation = 0;
    var halfLineHeight = scaleLabelFont.lineHeight / 2;

    if (isHorizontal) {
        scaleLabelX = me.left + ((me.right - me.left) / 2); // midpoint of the width
	scaleLabelY = position === 'bottom'
	? me.bottom - halfLineHeight - scaleLabelPadding.bottom
	: me.top + halfLineHeight + scaleLabelPadding.top;
    } else {
        var isLeft = position === 'left';
	scaleLabelX = isLeft
	? me.left + halfLineHeight + scaleLabelPadding.top
        : me.right - halfLineHeight - scaleLabelPadding.top;
	scaleLabelY = me.top + ((me.bottom - me.top) / 2);
	rotation = isLeft ? -0.5 * Math.PI : 0.5 * Math.PI;
    }


    context.save();
    context.translate(scaleLabelX, scaleLabelY);
    context.rotate(rotation);
    context.textAlign = 'center';
    context.textBaseline = 'middle';
    context.fillStyle = scaleLabelFontColor; // render in correct colour
    context.font = scaleLabelFont.string;
    context.fillText(scaleLabel.labelString, 0, 0);
    context.restore();
}

position = left の処理しか書いてないので,right を追加します。

if (scaleLabel.display) {
    // Draw the scale label
    var scaleLabelX;
    var scaleLabelY;
    var rotation = 0;
    var halfLineHeight = scaleLabelFont.lineHeight / 2;

    if (isHorizontal) {
        scaleLabelX = me.left + ((me.right - me.left) / 2); // midpoint of the width
	scaleLabelY = position === 'bottom'
	? me.bottom - halfLineHeight - scaleLabelPadding.bottom
	: me.top + halfLineHeight + scaleLabelPadding.top;
    } else {
        var isLeft = position === 'left';
	scaleLabelX = isLeft
	? me.left + halfLineHeight + scaleLabelPadding.top
        : me.right - halfLineHeight - scaleLabelPadding.top;
	scaleLabelY = me.top + ((me.bottom - me.top) / 2);
	rotation = isLeft ? -0.5 * Math.PI : 0.5 * Math.PI;
    }

    /*------------ ここを追加 ------------*/
    if (position === "right") {
        rotation += Math.PI;
    }
    /*----------------------------------*/

    context.save();
    context.translate(scaleLabelX, scaleLabelY);
    context.rotate(rotation);
    context.textAlign = 'center';
    context.textBaseline = 'middle';
    context.fillStyle = scaleLabelFontColor; // render in correct colour
    context.font = scaleLabelFont.string;
    context.fillText(scaleLabel.labelString, 0, 0);
    context.restore();
}

結果

スクリーンショット 2019-10-04 15.14.17.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?