Chartjs-Vertical line annotation on horizontal bar in chart js react not showing

1👍

If I’m not wrong, there are some errors in chart configuration.

  1. plugin is not the correct node name but is plugins
  2. The plugin annotation is related to x scale which is not defined, because you defined xAxes.

Furthermore it seems you are using CHART.JS 3 but the annotation plugin version 0.x.
From plugin README:

For Chart.js 3.0.0 to 3.6.2 support, use version 1.4.0 of this plugin For Chart.js 2.4.0 to 2.9.x support, use version 0.5.7 of this plugin

I have attached here a sample, fixing the config, using CHART.JS 3.9.1 and ANNOTATION 2.0.1:

const ctx = document.getElementById("myChart");
const myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['January', 'Fabruary', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'user 1 online',
            data: [50, 35, 45, 47, 10, 3, 27],
            backgroundColor: 'rgba(40, 139, 170, 1)',
            borderWidth: 0,
            borderSkipped: false,
        }]
    },
    options: {
      indexAxis: 'y',
      scales: {
        y: {
           display: true,
        },
        x: {
            max: 100,
            min: 0,
            display: true,
        }
      },
      plugins: {
         annotation: {
           annotations: [
             {
               type: 'line',
               scaleID: 'x',
               value: 80,
               borderColor: 'black',
               borderWidth: 2,
              },
           ],
        },
     },
   },
});
.myChartDiv {
  max-width: 600px;
  max-height: 400px;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation@2.0.1/dist/chartjs-plugin-annotation.min.js"></script>
<div class="myChartDiv">
  <canvas id="myChart" width="600" height="400"></canvas>
</div>

Leave a comment