【问题标题】:Draw a horizontal bar chart from right to left从右到左绘制水平条形图
【发布时间】:2020-07-03 18:34:43
【问题描述】:

我正在 Chart.js 中创建水平条形图,如您所见 in this jsfiddle

我想更改图表方向,使其从右向左绘制。我试过 dir = rtl 和 CSS direction=rtl 但它们不起作用。

这就是我想要的:

【问题讨论】:

    标签: javascript html css chart.js


    【解决方案1】:

    我在文档中找不到执行此操作的本地方式,但您可以通过相当简单的解决方法来否定数据,以便 Chart.js 绘制负值:

    1. 反转数据集中的值:

      data.datasets[0].data.map((currentValue, index, array) => {
        array[index] = currentValue * -1;
      });
      
    2. y 轴右对齐:

      scales: {
        yAxes: [{
          position: 'right'
          ...
      
    3. 重新格式化工具提示以供显示:

      options = {
        tooltips: {
        callbacks: {
          label: function(tooltipItem, data) {
            var label = data.datasets[tooltipItem.datasetIndex].label || '';
      
            if (label) {
              label += ': ';
            }
            label += tooltipItem.xLabel * -1;
            return label;
          }
        }
        ...
      
    4. 重新格式化刻度以供显示:

      xAxes: [{
        ticks: {
          callback: function(value, index, values) {
            return value * -1;
          }
      ...
      

    这是一个工作示例:

    var data = {
      labels: ["x1", "x2", "x3", "x4", "x5"],
      datasets: [{
        label: "Actual",
        backgroundColor: 'rgba(0, 0, 255, 0.5)',
        borderWidth: 1,
        data: [40, 150, 50, 60, 70],
        yAxisID: "bar-y-axis1"
      }]
    };
    
    // invert the sign of each of the values.
    data.datasets[0].data.map((currentValue, index, array) => {
      array[index] = currentValue * -1;
    });
    
    var options = {
      tooltips: {
        callbacks: {
          label: function(tooltipItem, data) {
            var label = data.datasets[tooltipItem.datasetIndex].label || '';
    
            if (label) {
              label += ': ';
            }
            label += tooltipItem.xLabel * -1; // invert the sign for display.
            return label;
          }
        }
      },
      scales: {
        yAxes: [{
          id: "bar-y-axis1",
          categoryPercentage: 0.5,
          barPercentage: 0.5,
          position: 'right' // right-align axis.
        }],
    
        xAxes: [{
          id: "bar-x-axis1",
          stacked: false,
          ticks: {
            callback: function(value, index, values) {
              return value * -1; // invert the sign for tick labelling.
            },
            beginAtZero: true
          }
        }]
      }
    };
    
    var ctx = document.getElementById("canvas").getContext("2d");
    var myBarChart = new Chart(ctx, {
      type: 'horizontalBar',
      data: data,
      options: options
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
    <canvas id="canvas"></canvas>

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 2017-05-24
      相关资源
      最近更新 更多