1

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.

user460567
  • 133
  • 9

1 Answers1

2

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>
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69