将数据标签对准右边。水平条形图

问题描述 投票:0回答:0

我试图将数据标签对齐到画布的右边。有什么办法可以把标签移到右边吗?我是这样配置标签的。

plugins: {
      datalabels: {
        formatter: (value, ctx) => {
          let sum = 0;
          let dataArr = ctx.chart.data.datasets[0].data;
          dataArr.map(data => {
            sum += data;
          });
          let percentage = ((value * 100) / sum).toFixed(2) + "%";
          return percentage;
        },
        color: "#000",
        align: "right",
        anchor: "end",
        offset: "70"
      },
      drawBorder: true
    },

Horizontal Bar chart

javascript charts chart.js
0
投票

插件核心API 提供了一系列可用于执行自定义代码的钩子。您可以使用 afterDraw 钩子,直接在画布上使用 CanvasRenderingContext2D.fillText() 如下所示。

plugins: [{
  afterDraw: chart => {
    var ctx = chart.chart.ctx;
    ctx.save();
    ctx.textAlign = 'left';
    ctx.textBaseline = 'middle';
    ctx.font = "12px Arial";
    let sum = chart.config.data.datasets[0].data.reduce((v, s) => v + s, 0);
    var xAxis = chart.scales['x-axis-0'];
    var yAxis = chart.scales["y-axis-0"];
    yAxis.ticks.forEach((v, i) => {
      var label = chart.data.labels[i];
      var value = chart.data.datasets[0].data[i];
      var x = xAxis.getPixelForValue(value) + 5;
      var y = yAxis.getPixelForTick(i);
      let percent = (value * 100 / sum).toFixed(2);
      ctx.fillText(label + ' (' + percent + '%)', x, y);
    });
    ctx.restore();
  }
}],

你可能还需要定义一些 padding 在图表右侧的 "标签",以确保所有的标签都显示在 "图表 "上。canvas.

options: {
  layout: {
    padding: {
      right: 110
    }
  },

请看一下下面的可运行代码片段。

const chart = new Chart(document.getElementById('myChart'), {
  type: 'horizontalBar',
  plugins: [{
    afterDraw: chart => {
      var ctx = chart.chart.ctx;
      ctx.save();
      ctx.textAlign = 'left';
      ctx.textBaseline = 'middle';
      ctx.font = "12px Arial";
      let sum =  chart.config.data.datasets[0].data.reduce((v, s) => v + s, 0);
      var xAxis = chart.scales['x-axis-0'];
      var yAxis = chart.scales["y-axis-0"];
      yAxis.ticks.forEach((v, i) => {
        var label = chart.data.labels[i];
        var value = chart.data.datasets[0].data[i];
        var x = xAxis.getPixelForValue(value) + 5;
        var y = yAxis.getPixelForTick(i);             
        let percent = (value * 100 / sum).toFixed(2);
        ctx.fillText(label + ' (' + percent + '%)', x, y);
      });
      ctx.restore();
    }
  }],
  data: {
    labels: ['Traffic source 1', 'Traffic source 2', 'Traffic source 3', 'Traffic source 4'],
    datasets: [{
      label: 'By Dataset',
      data: [15, 8, 12, 6],
      backgroundColor: ['red', 'lightblue', 'green', 'orange'],
      barPercentage: 1,
      categoryPercentage: 1
    }]
  },
  options: {
    layout: {
      padding: {
        right: 110
      }
    },
    legend: {
      display: false
    },
    scales: {
      yAxes: [{
        ticks: {
          display: false
        },
        gridLines: {
          drawTicks: false
        }
      }],
      xAxes: [{
        ticks: {
          beginAtZero: true
        },
        gridLines: {
          display: false
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="80"></canvas>
© www.soinside.com 2019 - 2024. All rights reserved.