Home forums Using CanvasJS How can I plot values from .txt file ? Reply To: How can I plot values from .txt file ?

#6786

Anjali
Participant

Hi,

From what I can see you are trying to parse text file on the server side which is not required – we can do it on the client side itself. At the same time you have included PHP code inside javascript loop which will not work.

<?php echo $parts[i] ?>

Here is an example that I have created which parses your text file on the client side.

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="jquery-2.1.1.js"></script>
<script type="text/javascript">
window.onload = function () {
var dataPoints = [];

//Replace text file's path according to your requirement.
$.get("testgraph.txt", function(data) {
	var x = 0;
	var allLines = data.split('\n');
	if(allLines.length > 0) {
		for(var i=0; i< allLines.length; i++) {
			dataPoints.push({x: x , y: parseInt(allLines[i])});
			x += .25;
		}
	}
	var chart = new CanvasJS.Chart("chartContainer",{
		title :{
			text: "Chart using Text File Data"
		},
		data: [{
			type: "line",
			dataPoints : dataPoints,
		}]
	});
	chart.render();
});
}
</script>
<script type="text/javascript" src="/assets/script/canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
</body>
</html>


Anjali