JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.canvasjs.com/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", {
title:{
text: "Basic Column Chart"
},
toolTip: {
contentFormatter: function (e) {
let largestDecimalDigits = 2;
const color = e.entries[0].dataSeries.lineColor;
const x = e.entries[0].dataPoint.x;
const y = e.entries[0].dataPoint.y;
if (typeof y === "object") return null;
const xWithCorrectDecimalSeparator = x
.toFixed(largestDecimalDigits);
const yWithCorrectDecimalSeparator = y
.toFixed(largestDecimalDigits);
const xUnit = "mm"
const yUnit = "N"
const str = `${xWithCorrectDecimalSeparator} ${xUnit} | ${yWithCorrectDecimalSeparator} ${yUnit}`;
return `<text style="color: ${color}">${str}</text>`;
},
},
data: [{
type: "line",
id: "main",
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55},
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14}
]
}]
});
let counter = 0;
function fakePoll() {
for (let i = chart.options.data.length - 1; i >= 0; i--) {
const data = chart.options.data[i];
if (data.id === "main") continue;
chart.options.data.splice(i, 1);
}
chart.options.data.push({
type: "line",
lineThickness: 4,
toolTipContent: null,
markerType: "none",
indexLabelPlacement: "outside",
indexLabelFontSize: 14,
dataPoints: [{x:20, y:40},{x:30, y:40},{x:30, y: 60},{x: 20, y: 60},{x: 20, y: 40}],
});
chart.options.data.push({
type: "rangeArea",
lineThickness: 0,
dataPoints: [],
toolTipContent: null,
markerType: "none",
fillOpacity: 0.2,
color: "#9c9C9C",
dataPoints: [{x:20, y:[40, 60]},{x:30, y:[40, 60]}],
});
chart.render();
counter++;
if (counter < 100) {
setTimeout(() => {
fakePoll();
}, 200);
}
}
fakePoll();