【问题标题】:Chart js cut the title and the legendsChart js 剪掉了标题和图例
【发布时间】:2021-09-16 14:50:11
【问题描述】:

我写了一个chart.js 图形代码设置标题和轴X 和Y 图例。当我更改版本时,两者都消失在图形中。有人有同样的问题吗?

我实际上使用的是 chat.js min.js 版本 3.3.2。

选项代码:

                   options : {
                            responsive : true,
                            hoverMode : 'index',
                            stacked : false,
                            title : {
                                display : true,
                                text : 'Título do Gráfico'
                            },
                            scales : {
                                yAxes: [{
                                    display: true,
                                    scaleLabel: {
                                        display: true,
                                        labelString: 'Unidade do Gráfico'
                                    }
                                }]
                            }

【问题讨论】:

    标签: charts chart.js chart.js2 chart.js3


    【解决方案1】:

    您的选项对象错误,chart.js 版本 3 有一些重大的重大更改,请阅读所有这些更改的迁移指南:https://www.chartjs.org/docs/master/getting-started/v3-migration.html

    对于标题和图例,它们已被移至插件部分,并且刻度不再是数组,而只是对象

    例子:

    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
        }]
      },
      options: {
        plugins: {
          title: {
            display: true,
            text: 'title'
          },
          legend: {
            position: 'bottom'
          }
        },
        scales: {
          y: {
            ticks: {
              font: {
                size: 20
              }
            }
          },
          x: {
            ticks: {
              font: {
                size: 20
              }
            }
          }
        }
      }
    }
    
    var ctx = document.getElementById('chartJSContainer').getContext('2d');
    new Chart(ctx, options);
    <body>
      <canvas id="chartJSContainer" width="600" height="400"></canvas>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.0/chart.js"></script>
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多