Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label padding for pointLabels #4962

Closed
sashankQ opened this issue Nov 17, 2017 · 5 comments · Fixed by #5914
Closed

Label padding for pointLabels #4962

sashankQ opened this issue Nov 17, 2017 · 5 comments · Fixed by #5914
Milestone

Comments

@sashankQ
Copy link

I have been trying to generate a radar chart and the labels are covered with the graph. please suggest a solution to overcome such issue
1

@etimberg
Copy link
Member

@sashankQ do you have a fiddle that reproduces this?

@jhadenfeldt
Copy link

Here is a fiddle that has the same issue: https://jsfiddle.net/ks5h2syL/

Any way to increase the distance of the label ("anger" in the screenshot or the months in the fiddle) from the graph would probably OK as a solution.

@filipesperandio
Copy link

I am also digging to find how to accomplish this

@Uranbold
Copy link

Uranbold commented Oct 8, 2018

Also having this issue. How to resolve this?

@filipesperandio
Copy link

This is very specific to my case, but I believe you can adapt:

  function renderLabels(chart) {
    var scale = chart.scale;
    var ctx = scale.ctx;
    var opts = scale.options.pointLabels;

    ctx.fillStyle = opts.fontColor || DARK;
    ctx.font = opts.fontSize + "px " + opts.fontFamily;
    ctx.textAlign =  "center";

    for(var i = 0; i < data.axisLabels.length; i++) {
      var outerDistance = scale.getDistanceFromCenterForValue(scale.max);
      var pointLabelPosition = scale.getPointPosition(i, outerDistance);

      var width = $(canvas).width();
      var xCenter = width / 2;
      if(pointLabelPosition.x > xCenter) {
        ctx.textAlign = "left";
        pointLabelPosition.x = pointLabelPosition.x + (0.0175 * width);
      } else if(pointLabelPosition.x < xCenter) {
        ctx.textAlign = "right";
        pointLabelPosition.x = pointLabelPosition.x - (0.014 * width);
      } else {
        ctx.textAlign = "center";
      }

      var height = $(canvas).height();
      var yCenterBase = (height / 2) + 30;
      if(i === 0) {
        pointLabelPosition.y = pointLabelPosition.y - (0.095 * height);
        ctx.textAlign = "center";
      } else if(pointLabelPosition.y > yCenterBase) {
        pointLabelPosition.y = pointLabelPosition.y + (0.02 * height);
      } else {
        pointLabelPosition.y = pointLabelPosition.y - (0.03 * height);
      }

      ctx.fillText(data.axisLabels[i], pointLabelPosition.x, pointLabelPosition.y);
    }

    ctx.restore();
  }

add this callback into a beforeDraw plugin:

  new Chart(canvas, {
    ... 
    plugins: [{ beforeDraw: renderLabels }]
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants