【问题标题】:Remove padding from chartJs horizontal bar chart从chartJs水平条形图中删除填充
【发布时间】:2020-02-20 14:14:56
【问题描述】:

嘿 :) 提前感谢您在这个问题上帮助我。

我在 ChartJs 中有一个带有一些虚拟信息的水平图表。我目前想将其与上面的统计数据对齐。在 ChartJs 中,图表在 <canvas> 标签内呈现。我想删除图表中的间距。我假设这是填充,但它不在画布上,而是在图表本身内。我已经阅读了文档并尝试了一些选择。

只是一些额外的信息,我自定义了图表,现在看起来像这样:

  • 删除了图例
  • 翻转标签
  • 更改了标签颜色
  • 删除了网格线

HTML: <canvas id="myChart" width="400" height="150"></canvas>

    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'horizontalBar',
        data: {
            labels: ['1', '2'],
            datasets: [{
                label: 'Money in',
                data: [5, 19],
                backgroundColor: [
                    'rgb(0,51,160)',
                    'rgb(26,121,191)'
                ],
                borderWidth: 0
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        fontColor: 'white',
                        display: true,
                        position: top,
                        mirror: true,
                        beginAtZero: true,
                        fontSize: 17,
                        padding: -9,
                        z: 1
                    },
                    gridLines: {
                        display: false
                    }
                }],
                xAxes: [{
                    gridLines: {
                        display: false
                    },
                    ticks: {
                        beginAtZero: true,
                        display: false
                    }
                }]
            },
            legend: {
                display: false
            }
        }
    });

【问题讨论】:

    标签: javascript jquery css chart.js


    【解决方案1】:

    您可以使用负左填充定义layout,如下所示:

    options: {
        layout: {
          padding: {
            left: -10
          }
        },
        ...
    

    请看下面的代码 sn-p,其中图表画布用边框定义,以强调与前面文本的对齐。

    var myChart = new Chart(document.getElementById('myChart'), {
      type: 'horizontalBar',
      data: {
        labels: ['1', '2'],
        datasets: [{
          label: 'Money in',
          data: [5, 19],
          backgroundColor: ['rgb(0,51,160)', 'rgb(26,121,191)'],
          borderWidth: 0
        }]
      },
      options: {
        layout: {
          padding: {
            left: -10
          }
        },
        scales: {
          yAxes: [{
            ticks: {
              fontColor: 'white',
              mirror: true,
              beginAtZero: true,
              fontSize: 17,
              padding: -9,
              z: 1
            },
            gridLines: {
              display: false
            }
          }],
          xAxes: [{
            gridLines: {
              display: false
            },
            ticks: {          
              beginAtZero: true,
              display: false
            }
          }]
        },
        legend: {
          display: false
        }
      }
    });
    canvas{ 
      max-width: 300px;
      border: 1px solid #000;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 
    <p>Text here</p>
    <canvas id="myChart" width="400" height="150"></canvas>

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 2023-01-20
      • 2017-03-09
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2012-12-19
      相关资源
      最近更新 更多