首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ChartJs中的可调整大小/可支出框注释

ChartJs中的可调整大小/可支出框注释
EN

Stack Overflow用户
提问于 2021-06-30 15:16:01
回答 1查看 87关注 0票数 0

我正在使用chartJs中的chartjs-plugin-annotation和ReactJs。但是在框注释中,有没有一种方法可以调整框的大小,以便我可以扩展注释的最小值和最大值。

我的选项代码:`

代码语言:javascript
复制
 options: {
        scales: {
          y: {
            beginAtZero: true
          }
        },
        plugins: {
          autocolors: false,
          annotation: {
            annotations: {
              box1: {
                type: "box",
                xMin: 1,
                xMax: 2,
                yMin: 5,
                yMax: 10,
                backgroundColor: "rgba(255, 99, 132, 0.25)"
              }
            }
          }
        }
      }

`

这是我的代码沙箱ReactJs-ChartJs

EN

回答 1

Stack Overflow用户

发布于 2021-06-30 18:26:01

是的,您可以深入研究图表对象的选项部分,调整注释的配置,然后调用chart.update(),这将更新注释。

示例:

代码语言:javascript
复制
const 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: {
      annotation: {
        annotations: {
          box1: {
            type: "box",
            xMin: 1,
            xMax: 2,
            yMin: 5,
            yMax: 10,
            backgroundColor: "rgba(255, 99, 132, 0.25)"
          }
        }
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);

document.getElementById("tt").addEventListener("click", () => {
  chart.options.plugins.annotation.annotations.box1.yMax = 16;
  chart.update();
});

document.getElementById("rr").addEventListener("click", () => {
  chart.options.plugins.annotation.annotations.box1.yMax = 8;
  chart.update();
});
代码语言:javascript
复制
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <button id="tt">Update annotation to 16</button>
  <button id="rr">Update annotation to 8</button>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.0/chart.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/1.0.2/chartjs-plugin-annotation.js"></script>
</body>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68190065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档