如何更新Chartjs中的注释值3.7.1

发布于 2025-02-09 07:07:56 字数 479 浏览 2 评论 0原文

找到了一堆旧帖子,但在较新版本上找不到任何东西。我正在使用ChartJS 3.7.1和Chartjs-Plugin-nototation 1.4.0

我可以通过以下方式添加新的注释:

myChart.options.plugins.annotation.annotations['newAnnotation'] = {
  type: 'line',
 xMin: 0,
 xMax: 11,
 yMin: 200000,
 yMax: 200000,
 borderColor: 'rgb(255, 99, 132)',
 borderWidth: 2
 };

 myChart.update();

但是如何动态访问现有注释以便以后更改值?

尝试这样的事情不起作用:

myChart.options.plugins.annotation.annotations[0].value = 200000;

Found a bunch of old posts but couldn't find anything on the newer versions. I'm using Chartjs 3.7.1 and chartjs-plugin-annotation 1.4.0

I can add a new annotation via:

myChart.options.plugins.annotation.annotations['newAnnotation'] = {
  type: 'line',
 xMin: 0,
 xMax: 11,
 yMin: 200000,
 yMax: 200000,
 borderColor: 'rgb(255, 99, 132)',
 borderWidth: 2
 };

 myChart.update();

but how do I access the the existing annotation dynamically in order to change the value afterwards?

Trying something like this didn't work:

myChart.options.plugins.annotation.annotations[0].value = 200000;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2025-02-16 07:07:56

这可以如下完成:

const annotation = myChart.options.plugins.annotation.annotations.newAnnotation;
annotation.yMin = newValue;
annotation.yMax = newValue;
myChart.update();

请查看下面的可运行代码。要查看其工作原理,请在INPUT元素上键入图表顶部,然后按 Enter

const myChart = new Chart('myChart', {
  type: 'line',
  data: {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [{
      label: 'My First Dataset',
      data: [65, 59, 80, 81, 56, 55, 40],
      fill: false,
      borderColor: 'rgb(75, 192, 192)',
      tension: 0.1
    }]
  },
  options: {
    plugins: {
      autocolors: false,
      annotation: {
        annotations: {
          newAnnotation: {
            type: 'line',
            yMin: 60,
            yMax: 60,
            borderColor: 'rgb(255, 99, 132)',
            borderWidth: 2
          }
        }
      }
    }
  }
});

const input = document.getElementById('myInput');
input.addEventListener('keypress', event => {
  if (event.key === 'Enter') {
    event.preventDefault();
    const annotation = myChart.options.plugins.annotation.annotations.newAnnotation;
    annotation.yMin = input.value;
    annotation.yMax = input.value;
    myChart.update();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/1.4.0/chartjs-plugin-annotation.min.js"></script>
<label for="salary">Enter new y-value and press enter:</label>
<input id="myInput" type="number">
<canvas id="myChart" height="90"></canvas>

This can be done as follows:

const annotation = myChart.options.plugins.annotation.annotations.newAnnotation;
annotation.yMin = newValue;
annotation.yMax = newValue;
myChart.update();

Please take a look at the runnable code below. To see how it works, type a new y-value into the input element top of the chart and press Enter

const myChart = new Chart('myChart', {
  type: 'line',
  data: {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [{
      label: 'My First Dataset',
      data: [65, 59, 80, 81, 56, 55, 40],
      fill: false,
      borderColor: 'rgb(75, 192, 192)',
      tension: 0.1
    }]
  },
  options: {
    plugins: {
      autocolors: false,
      annotation: {
        annotations: {
          newAnnotation: {
            type: 'line',
            yMin: 60,
            yMax: 60,
            borderColor: 'rgb(255, 99, 132)',
            borderWidth: 2
          }
        }
      }
    }
  }
});

const input = document.getElementById('myInput');
input.addEventListener('keypress', event => {
  if (event.key === 'Enter') {
    event.preventDefault();
    const annotation = myChart.options.plugins.annotation.annotations.newAnnotation;
    annotation.yMin = input.value;
    annotation.yMax = input.value;
    myChart.update();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/1.4.0/chartjs-plugin-annotation.min.js"></script>
<label for="salary">Enter new y-value and press enter:</label>
<input id="myInput" type="number">
<canvas id="myChart" height="90"></canvas>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文