I am trying to modify an example of Rendering a Chart from User Input for CanvasJS. I want the x-axis to be a timeline. I feel I am on the right track but now stuck in setting the correct time input. The code is:
var dps = []; //dataPoints.
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "DataPoints from User Input"
},
axisX: {
valueFormatString: "HH:mm",
labelAngle: -50
},
axisY: {
valueFormatString: "###"
},
color: "#F08080",
data: [{
type: "line",
xValueFormatString: "HH:mm",
dataPoints: dps
}]
});
function addDataPointsAndRender() {
xValue = Number(document.getElementById("xValue").value);
yValue = Number(document.getElementById("yValue").value);
dps.push({
x: new Date(xValue),
y: yValue
});
chart.render();
}
var renderButton = document.getElementById("renderButton");
renderButton.addEventListener("click", addDataPointsAndRender);
I know it is a problem in using new Date()
but not sure what.
Here is a codepen
Any help would be appreciated!