如何在Chart.js中绘制水平参考线?

发布于 2025-02-11 16:06:40 字数 782 浏览 3 评论 0原文

我想在特定值中绘制水平参考线。

数据是格式:

{
      "Close": 15638.8,
      "Date": "2022-06-21T10:00:00.000Z",
      "High": 15707.25,
      "Low": 15419.85,
      "Open": 15455.95,
      "Volume": 0,
      "id": 36
    }

这是我目前已经想象的。我不确定如何使用回流属性。

 var dataReqdFormat = {
    labels: [],
    datasets: [{
      data: [],
      label: "Nifty",
      fill: true,
      borderColor: (ctx) => {
        const data = ctx.chart.data.datasets[ctx.datasetIndex].data;
        return data[0] >= data[data.length - 1] ? 'red' : 'green'
      }
    }]
  };

任何帮助将不胜感激。

I want to draw a horizontal reference line at a particular value.

enter image description here

The data is of the format:

{
      "Close": 15638.8,
      "Date": "2022-06-21T10:00:00.000Z",
      "High": 15707.25,
      "Low": 15419.85,
      "Open": 15455.95,
      "Volume": 0,
      "id": 36
    }

This is what I have figured as of now. I am unsure how to use the refLines attribute.

 var dataReqdFormat = {
    labels: [],
    datasets: [{
      data: [],
      label: "Nifty",
      fill: true,
      borderColor: (ctx) => {
        const data = ctx.chart.data.datasets[ctx.datasetIndex].data;
        return data[0] >= data[data.length - 1] ? 'red' : 'green'
      }
    }]
  };

Any help is appreciated.

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

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

发布评论

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

评论(1

薯片软お妹 2025-02-18 16:06:40

您可以使用注释插件为此:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderColor: 'pink'
    }]
  },
  options: {
    plugins: {
      annotation: {
        annotations: {
          line: {
            type: 'line',
            yMin: 16,
            yMax: 16,
            borderWidth: 2,
            borderColor: 'red'
          }
        }
      }
    }
  }
}

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.8.0/chart.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/1.4.0/chartjs-plugin-annotation.js"></script>
</body>

You can use the annotation plugin for this:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderColor: 'pink'
    }]
  },
  options: {
    plugins: {
      annotation: {
        annotations: {
          line: {
            type: 'line',
            yMin: 16,
            yMax: 16,
            borderWidth: 2,
            borderColor: 'red'
          }
        }
      }
    }
  }
}

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.8.0/chart.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/1.4.0/chartjs-plugin-annotation.js"></script>
</body>

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