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",
{
title:{
text: "Multi-Series Line Chart"
},
data: [
{
type: "line",
dataPoints: [
{ x: 1, y: 30 },
{ x: 3, y: 40 },
{ x: 4, y: 50 },
{ x: 6, y: 60 },
{ x: 7, y: 90 },
{ x: 8, y: 20 },
{ x: 9, y: 50}
]
}, {
type: "line",
dataPoints: [
{ x: 1, y: 10 },
{ x: 2, y: 20},
{ x: 3, y: 30 },
{ x: 4, y: 40 },
{ x: 5, y: 50 },
{ x: 6, y: 60 },
{ x: 7, y: 70 },
{ x: 8, y: 80 },
{ x: 9, y: 90}
]
}
]
});
addDataPoint(chart.options.data[0].dataPoints, 1, 2);
chart.render();
function addDataPoint(dataPoints, index, xValue) {
var yValue = dataPoints[index - 1].y + ((xValue - dataPoints[index - 1].x) / (dataPoints[index].x - dataPoints[index - 1].x)) * (dataPoints[index].y - dataPoints[index - 1].y);
dataPoints.splice(index, 0, {
x: xValue,
y: yValue,
markerColor: "red",
//Uncomment below two lines to disable marker & highlighting
markerSize: 1,
highlightEnabled: false
});
}