score:0

Accepted answer

i found the solution myself. here it is

<div class="row">
    <div class="col-12 border">
        <canvas id="mychart"></canvas>
    </div>
</div>

<script src="./assets/charts/dist/chart.js"></script>
<!----------------------mychart---------------------->
<script>
var canvas = document.getelementbyid("mychart");
var ctx = canvas.getcontext("2d");

var horizonallineplugin = {
  afterdraw: function(chartinstance) {
    var yscale = chartinstance.scales["y-axis-0"];
    var canvas = chartinstance.chart;
    var ctx = canvas.ctx;
    var index;
    var line;
    var style;

    if (chartinstance.options.horizontalline) {
      for (index = 0; index < chartinstance.options.horizontalline.length; index++) {
        line = chartinstance.options.horizontalline[index];

        if (!line.style) {
          style = "rgba(169,169,169, .6)";
        } else {
          style = line.style;
        }

        if (line.y) {
          yvalue = yscale.getpixelforvalue(line.y);
        } else {
          yvalue = 0;
        }

        ctx.linewidth = 3;

        if (yvalue) {
          ctx.beginpath();
          ctx.moveto(0, yvalue);
          ctx.lineto(canvas.width, yvalue);
          ctx.strokestyle = style;
          ctx.stroke();
        }

        if (line.text) {
          ctx.fillstyle = style;
          ctx.filltext(line.text, 0, yvalue + ctx.linewidth);
        }
      }
      return;
    };
  }
};
chart.pluginservice.register(horizonallineplugin);

var data = {
  labels: ["january", "february", "march", "april", "may", "june", "july"],
  datasets: [{
    label: "my first dataset",
    fill: false,
    linetension: 0.1,
    backgroundcolor: "rgba(75,192,192,0.4)",
    bordercolor: "rgba(75,192,192,1)",
    bordercapstyle: 'butt',
    borderdash: [],
    borderdashoffset: 0.0,
    borderjoinstyle: 'miter',
    pointbordercolor: "rgba(75,192,192,1)",
    pointbackgroundcolor: "#fff",
    pointborderwidth: 1,
    pointhoverradius: 5,
    pointhoverbackgroundcolor: "rgba(75,192,192,1)",
    pointhoverbordercolor: "rgba(220,220,220,1)",
    pointhoverborderwidth: 2,
    pointradius: 4,
    pointhitradius: 10,
    data: [65, 59, 80, 81, 56, 55, 48],
  }]
};

var mychart = new chart(ctx, {
  type: 'line',
  data: data,
  options: {
    "horizontalline": [{
      "y": 82,
      "style": "rgba(255, 0, 0, .4)",
      "text": "max"
    }, {
      "y": 50,
      "style": "#00ffff",
      "text": "min"
    }]
  }
});
</script>

Related Query

More Query from same tag