Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Ability to rotate axis titles

Open NemanjaS2P opened this issue 3 years ago • 7 comments

Hello,

i would like to rotate the y axis tittle label but in the documentation there is nothing on this topic. The rotation is avalible only for axis ticks. Is there a way to rotate the label?

image

example in fiddle : https://jsfiddle.net/3yxwc0m1/

NemanjaS2P avatar Jan 26 '21 14:01 NemanjaS2P

There is no way to rotate the label

etimberg avatar Jan 26 '21 22:01 etimberg

Is there a chance this can be implemented in the future? Thank you

NemanjaS2P avatar Jan 27 '21 08:01 NemanjaS2P

Looks like the title is drawn on the canvas here, so you might be able to look at it and implement it yourself https://github.com/chartjs/Chart.js/blob/master/src/core/core.scale.js#L1622

LeeLenaleee avatar Jan 27 '21 09:01 LeeLenaleee

Would be nice if this could be implemented

wdmeest avatar Aug 06 '21 11:08 wdmeest

Any news on this?

ggutierrez123 avatar Jan 21 '22 18:01 ggutierrez123

I have got it working with a config option i called "flip". I just added some code to the drawTitle and titleArgs functions in the core.scale.js file. Will do a pull request after some testing. For now here is the code

  drawTitle() {
    const {ctx, options: {position, title, reverse, flip}} = this;

    if (!title.display) {
      return;
    }

    const font = toFont(title.font);
    const padding = toPadding(title.padding);
    const align = title.align;
    let offset = font.lineHeight / 2;

    if (position === 'bottom' || position === 'center' || isObject(position)) {
      offset += padding.bottom;
      if (isArray(title.text)) {
        offset += font.lineHeight * (title.text.length - 1);
      }
    } else {
      offset += padding.top;
    }

    const {titleX, titleY, maxWidth, rotation} = titleArgs(this, offset, position, align, title.flip);

    renderText(ctx, title.text, 0, 0, font, {
      color: title.color,
      maxWidth,
      rotation,
      textAlign: titleAlign(align, position, reverse),
      textBaseline: 'middle',
      translation: [titleX, titleY],
    });
  }
function titleArgs(scale, offset, position, align, flip) {
  const {top, left, bottom, right, chart} = scale;
  const {chartArea, scales} = chart;
  let rotation = 0;
  let maxWidth, titleX, titleY;
  const height = bottom - top;
  const width = right - left;

  if (scale.isHorizontal()) {
    titleX = _alignStartEnd(align, left, right);

    if (isObject(position)) {
      const positionAxisID = Object.keys(position)[0];
      const value = position[positionAxisID];
      titleY = scales[positionAxisID].getPixelForValue(value) + height - offset;
    } else if (position === 'center') {
      titleY = (chartArea.bottom + chartArea.top) / 2 + height - offset;
    } else {
      titleY = offsetFromEdge(scale, position, offset);
    }
    maxWidth = right - left;
  } else {
    if (isObject(position)) {
      const positionAxisID = Object.keys(position)[0];
      const value = position[positionAxisID];
      titleX = scales[positionAxisID].getPixelForValue(value) - width + offset;
    } else if (position === 'center') {
      titleX = (chartArea.left + chartArea.right) / 2 - width + offset;
    } else {
      titleX = offsetFromEdge(scale, position, offset);
    }
    titleY = _alignStartEnd(align, bottom, top);
    if (flip){
      rotation = position === 'left' ? - HALF_PI : -HALF_PI;
    }else{
      rotation = position === 'left' ? - HALF_PI : HALF_PI;
    }

  }
  return {titleX, titleY, maxWidth, rotation};
}

En1kay avatar Mar 31 '22 10:03 En1kay