javascript - ChartJS xAxis label position - Stack Overflow

I have a ChartJS that display the label as slanted when you resize the window to smaller size.What I wa

I have a ChartJS that display the label as slanted when you resize the window to smaller size.

What I want to do is to lower the X-labels down a bit vertically so they are not as close to the base of the graph if possible.

After googling around, it looks like I can disable the tick display for x-Axis and use the option's animation to do this manually. I tried to implement this in the following fiddle.

    animation: {
      duration: 1,
      onComplete: function() {
        var chartInstance = this.chart;
        this.data.datasets.forEach(function(dataset, i) {
          var meta = chartInstance.controller.getDatasetMeta(i);
          meta.data.forEach(function(bar, index) {

            var label = bar._model.label;
            var xOffset = bar._model.x;
            var yOffset = bar._model.y;
            ctx.fillText(label, xOffset, 420);
          });
        });
      }
    },

However, I can't get the label to scale properly when I resize the window. Can you help?

I have a ChartJS that display the label as slanted when you resize the window to smaller size.

What I want to do is to lower the X-labels down a bit vertically so they are not as close to the base of the graph if possible.

After googling around, it looks like I can disable the tick display for x-Axis and use the option's animation to do this manually. I tried to implement this in the following fiddle.

    animation: {
      duration: 1,
      onComplete: function() {
        var chartInstance = this.chart;
        this.data.datasets.forEach(function(dataset, i) {
          var meta = chartInstance.controller.getDatasetMeta(i);
          meta.data.forEach(function(bar, index) {

            var label = bar._model.label;
            var xOffset = bar._model.x;
            var yOffset = bar._model.y;
            ctx.fillText(label, xOffset, 420);
          });
        });
      }
    },

However, I can't get the label to scale properly when I resize the window. Can you help?

Share Improve this question asked Oct 2, 2019 at 20:42 FylixFylix 2,7258 gold badges53 silver badges86 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

Chart.js implements a padding property in the ticks object for this:

Padding between the tick label and the axis. When set on a vertical axis, this applies in the horizontal (X) direction. When set on a horizontal axis, this applies in the vertical (Y) direction.

Here's a working example with the x-axis labels offset 20px down from the line:

new Chart(document.getElementById("chart"), {
  type: "bar",
  data: {
    labels: ["Blue", "Red", "Green", "Orange", "Purple"],
    datasets: [{
      data: [0, 1, 2, 3, 4]
    }]
  },
  options: {
    maintainAspectRatio: false,
    scales: {
      xAxes: [{
        ticks: {
          padding: 20
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<div style="height:200px;width:200px">
  <canvas id="chart"></canvas>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744169133a4561447.html

相关推荐

  • javascript - ChartJS xAxis label position - Stack Overflow

    I have a ChartJS that display the label as slanted when you resize the window to smaller size.What I wa

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信