• <legend id='TTEUN'><style id='TTEUN'><dir id='TTEUN'><q id='TTEUN'></q></dir></style></legend>
  • <small id='TTEUN'></small><noframes id='TTEUN'>

  • <tfoot id='TTEUN'></tfoot>

      <bdo id='TTEUN'></bdo><ul id='TTEUN'></ul>
    <i id='TTEUN'><tr id='TTEUN'><dt id='TTEUN'><q id='TTEUN'><span id='TTEUN'><b id='TTEUN'><form id='TTEUN'><ins id='TTEUN'></ins><ul id='TTEUN'></ul><sub id='TTEUN'></sub></form><legend id='TTEUN'></legend><bdo id='TTEUN'><pre id='TTEUN'><center id='TTEUN'></center></pre></bdo></b><th id='TTEUN'></th></span></q></dt></tr></i><div id='TTEUN'><tfoot id='TTEUN'></tfoot><dl id='TTEUN'><fieldset id='TTEUN'></fieldset></dl></div>

      1. 如何使用chart.js在水平条中设置标签左对齐?

        时间:2023-11-01

      2. <legend id='kDH0y'><style id='kDH0y'><dir id='kDH0y'><q id='kDH0y'></q></dir></style></legend>
          <tbody id='kDH0y'></tbody>

          <i id='kDH0y'><tr id='kDH0y'><dt id='kDH0y'><q id='kDH0y'><span id='kDH0y'><b id='kDH0y'><form id='kDH0y'><ins id='kDH0y'></ins><ul id='kDH0y'></ul><sub id='kDH0y'></sub></form><legend id='kDH0y'></legend><bdo id='kDH0y'><pre id='kDH0y'><center id='kDH0y'></center></pre></bdo></b><th id='kDH0y'></th></span></q></dt></tr></i><div id='kDH0y'><tfoot id='kDH0y'></tfoot><dl id='kDH0y'><fieldset id='kDH0y'></fieldset></dl></div>
            <bdo id='kDH0y'></bdo><ul id='kDH0y'></ul>
              • <tfoot id='kDH0y'></tfoot>

              • <small id='kDH0y'></small><noframes id='kDH0y'>

                1. 本文介绍了如何使用chart.js在水平条中设置标签左对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 chart.js 的代码

                  My code using chart.js

                  var ctx_1 = document.getElementById('non_200_pages').getContext('2d');
                  var myChart_1 = new Chart(ctx_1, {
                  type: 'horizontalBar',
                  data: {
                      labels: ["Total","301 Redirect","Broken Pages (4xx Errors)","Uncategorised HTTP Response Codes","5xx Errors","Unauthorised Pages","Non-301 Redirects"],
                      datasets: [{
                  
                          data: [ {{ $array_non_200_pages[0] }}, {{ $array_non_200_pages[1] }}, {{ $array_non_200_pages[2] }}, {{ $array_non_200_pages[3] }}, {{ $array_non_200_pages[4] }}, {{ $array_non_200_pages[5] }}, {{ $array_non_200_pages[6]}} ],
                          backgroundColor: [ 
                              'rgba(237, 56, 98, 1.0)',
                              'rgba(237, 56, 98, 1.0)',
                              'rgba(237, 56, 98, 1.0)',
                              'rgba(237, 56, 98, 1.0)',
                              'rgba(237, 56, 98, 1.0)',
                              'rgba(237, 56, 98, 1.0)',
                              'rgba(237, 56, 98, 1.0)'
                          ]
                  
                      }]
                  },
                  options: {
                      showAllTooltips: true,
                      tooltips: {
                            enabled: true,
                            displayColors: false,
                            yPadding: 20,
                            xPadding: 30,
                            caretSize: 10,
                            backgroundColor: 'rgba(240, 240, 240, 1)',
                            bodyFontSize: 16,
                            bodyFontColor: 'rgb(50, 50, 50)',
                            borderColor: 'rgba(0,0,0,1)',
                            borderWidth: 1,
                            cornerRadius: 0,
                            yAlign: 'bottom',
                            xAlign: 'center',
                            position: 'custom',
                            custom: function(tooltip) {
                              if (!tooltip) return;
                              // disable displaying the color box;
                              tooltip.displayColors = false;
                            },
                            callbacks: {
                              // use label callback to return the desired label
                              label: function(tooltipItem, data) {
                                return tooltipItem.yLabel + " : " + tooltipItem.xLabel ;
                              },
                              // remove title
                              title: function(tooltipItem, data) {
                                return;
                              }
                          }
                      },
                      responsive: false,
                      legend: { display: false },
                      scales: {
                          yAxes: [{
                              ticks: {
                                  beginAtZero: true,
                              },
                              gridLines: {
                                  display: false
                              },
                          }],
                          xAxes: [{
                              ticks: {
                                  stepSize:5,
                                  display: false
                              },
                              gridLines: {
                                  drawBorder: false,
                              }
                          }],
                      },
                      plugins: {
                          datalabels: {
                      align: 'end',
                      anchor: 'end',        
                      backgroundColor: function(context) {
                        return context.dataset.backgroundColor;
                      },
                      borderRadius: 4,
                      color: 'white',
                      formatter: Math.round
                    }
                  }
                  }
                  });
                  

                  我的工作

                  预期输出

                  我在如何将标签文本设置为左对齐时遇到问题.我想要做的是我希望我的标签与下一张图片相同,但我不知道 Options:{ 设置中文本对齐的位置.有没有人可以帮我解决它?我在 https://www.chartjs.org/docs 上找不到任何内容/latest/configuration/legend.html.提前谢谢你.

                  I'm having a problem on how to set the labels text align to left. What I want to do is I want my label same with the next picture but I don't know where in Options:{ settings for text align. Is there anyone can help me how to fix it? I can't find any on the https://www.chartjs.org/docs/latest/configuration/legend.html. Thank you in advance.

                  推荐答案

                  yAxis.ticks 标签可以通过定义 mirror: true 结合添加一些 padding.

                  The yAxis.ticks labels can be left aligned by defining mirror: true combined with adding some padding.

                  yAxes: [{
                    ticks: {
                      mirror: true,
                      padding: 220
                    },
                  

                  要使标签在图表区域可见,需要在 图表布局.

                  To make the labels visible on the chart area, the same padding needs to be defined left of the chart layout.

                  layout: {
                    padding: {
                      left: 220
                    }
                  },
                  

                  请在下面查看您更改的代码.

                  Please have a look at your changed code below.

                  new Chart('myChart', {
                    type: 'horizontalBar',
                    data: {
                      labels: ["Total", "301 Redirect", "Broken Pages (4xx Errors)", "Uncategorised HTTP Response Codes", "5xx Errors", "Unauthorised Pages", "Non-301 Redirects"],
                      datasets: [{
                        data: [16, 14, 1, 1, 0, 0, 0],
                        backgroundColor: ['rgba(237, 56, 98, 1.0)', 'rgba(237, 56, 98, 1.0)', 'rgba(237, 56, 98, 1.0)', 'rgba(237, 56, 98, 1.0)', 'rgba(237, 56, 98, 1.0)', 'rgba(237, 56, 98, 1.0)','rgba(237, 56, 98, 1.0)']
                      }]
                    },
                    options: {
                      responsive: false,
                      layout: {
                        padding: {
                          left: 220
                        }
                      },  
                      legend: {
                        display: false
                      },
                      scales: {
                        yAxes: [{
                          ticks: {
                            mirror: true,
                            padding: 220
                          },
                          gridLines: {
                            display: false
                          },
                        }],
                        xAxes: [{
                          ticks: {
                            display: false
                          },
                          gridLines: {
                            drawBorder: false,
                          }
                        }],
                      }
                    }
                  });

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
                  <canvas id="myChart" width="500" height="200"></canvas>

                  这篇关于如何使用chart.js在水平条中设置标签左对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Chart.js 中使用百分比刻度 下一篇:将 FirestoreCollection 转换为数组?

                  相关文章

                  <legend id='7gG3N'><style id='7gG3N'><dir id='7gG3N'><q id='7gG3N'></q></dir></style></legend>

                  1. <tfoot id='7gG3N'></tfoot>

                    <i id='7gG3N'><tr id='7gG3N'><dt id='7gG3N'><q id='7gG3N'><span id='7gG3N'><b id='7gG3N'><form id='7gG3N'><ins id='7gG3N'></ins><ul id='7gG3N'></ul><sub id='7gG3N'></sub></form><legend id='7gG3N'></legend><bdo id='7gG3N'><pre id='7gG3N'><center id='7gG3N'></center></pre></bdo></b><th id='7gG3N'></th></span></q></dt></tr></i><div id='7gG3N'><tfoot id='7gG3N'></tfoot><dl id='7gG3N'><fieldset id='7gG3N'></fieldset></dl></div>
                  2. <small id='7gG3N'></small><noframes id='7gG3N'>

                    • <bdo id='7gG3N'></bdo><ul id='7gG3N'></ul>