JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
JavaScript
var chart = new CanvasJS.Chart("chartContainer", {
axisY: {
stripLines: [{
startValue: 50,
endValue: 70
},{
//value: 45,
label: "Stripline Label",
color: "transparent",
labelFontColor:"black"
}]
},
data: [{
type: "line",
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 },
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 155 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14 }
]
}]
});
chart.render();
var refStripline = chart.axisY[0].stripLines[0], currStripline = chart.axisY[0].stripLines[1];
updateStripLineValue(currStripline, refStripline)
function updateStripLineValue(currStripline, refStripline) {
var padding = 2; //In pixels
var font = currStripline.labelFontWeight + " " + currStripline.labelFontSize + "px " + currStripline.labelFontFamily;
var labelHeight = getHeight(currStripline.label, font);
currStripline.set("value", currStripline.parent.convertPixelToValue(currStripline.parent.convertValueToPixel(Math.min(refStripline.startValue,refStripline.endValue)) + labelHeight + padding));
}
function getHeight(txt, font){
var tempCanvas = document.createElement("canvas");
var tempCtx = tempCanvas.getContext("2d");
tempCtx.font = font;
return parseInt(tempCtx.font.match(/\d+/), 10);
}