【问题标题】:padding not working in my chartjs line-graph填充在我的 chartjs 折线图中不起作用
【发布时间】:2021-08-04 11:09:29
【问题描述】:

我正在使用最新版本的 chart.js (3.2.1)。如果我将其设置为正值,则填充有效。 但是填充 0(或负值)不会将折线图完全设置在画布的边界

options: {

        layout: {
            padding:0
        },
        interaction: {
            intersect: false,
        },
        plugins: {
            tooltip: {
                backgroundColor: 'transparent',
                displayColors: false,
                bodyFontSize: 14,
                titleColor: 'rgba(83,255,228,1)',
                callbacks: {
                    label: function (tooltipItems, data) {
                        return 'BMI: ' + tooltipItems.raw;
                    }
                }
            },
            legend: {
                display: false,
            },
        },
        elements: {
            point: {
                radius: 6,
                hitRadius: 6,
                hoverRadius: 6
            }
        },
        scales: {
            xAxes: {
                display: false,
            },
            yAxes: {
                display: false,
                
            },
        }
    }

【问题讨论】:

    标签: chart.js linegraph chart.js3


    【解决方案1】:

    图表配置中的填充不适用于将画布样式设置为其他元素,填充所做的唯一事情是根据您提供的图像在画布边缘和线条之间添加一些空间似乎它走到了画布的尽头,而空白空间是画布下方的另一个元素。

    正如您在此示例中所见,画布背景为灰色,padding: 0 线条按预期触及画布末端

    var options = {
      type: 'line',
      data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            borderWidth: 1
          },
          {
            label: '# of Points',
            data: [7, 11, 5, 8, 3, 7],
            borderWidth: 1
          }
        ]
      },
      options: {
        plugins: {
          tooltip: {
            backgroundColor: 'transparent',
            displayColors: false,
            bodyFontSize: 14,
            titleColor: 'rgba(83,255,228,1)',
            callbacks: {
              label: function(tooltipItems, data) {
                return 'BMI: ' + tooltipItems.raw;
              }
            }
          },
          legend: {
            display: false,
          },
        },
        interaction: {
          intersect: false,
        },
        layout: {
          padding: 0
        },
        elements: {
          point: {
            radius: 0
          }
        },
        scales: {
          y: {
            display: false
          },
          x: {
            display: false
          }
        }
      }
    }
    
    var ctx = document.getElementById('chartJSContainer').getContext('2d');
    new Chart(ctx, options);
    canvas {
      background-color: #eee;
    }
    <body>
      <canvas id="chartJSContainer" width="600" height="400"></canvas>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.0/chart.js" integrity="sha512-opXrgVcTHsEVdBUZqTPlW9S8+99hNbaHmXtAdXXc61OUU6gOII5ku/PzZFqexHXc3hnK8IrJKHo+T7O4GRIJcw==" crossorigin="anonymous"></script>
    </body>

    【讨论】:

    • 我的画布和你的一样 canvas { background-color: #eee; } 和它同样的问题
    • 你能分享一个可重现的样本吗,因为你可以通过一个简约的例子看到它只是工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2021-07-12
    相关资源
    最近更新 更多