Home forums Using CanvasJS Dynamic Chart Using Data From JSON File

Tagged: , ,

This topic contains 3 replies, has 2 voices, and was last updated by  Vishwas R 3 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #12317

    sandonet
    Member

    Hi, i have a question how to make a dynamic chart but the data is coming from JSON file, this JSON data is updating every second.
    This is the example of the JSON data

    {"X":-2455, "Y":142, "Z":-110}

    I have searching everywhere but i didn’t find it, i only find dynamic chart with random data within.
    And one more thing, the X axis is showing the current time.

    Thanks for your attention, gladly waiting for an answer 🙂

    #12318

    Vishwas R
    Keymaster

    Sandonet,

    Please refer this link.

    #12319

    sandonet
    Member

    Vishwas, i try this but not work, i’m sorry my js knowledge is not good, so can you give the example base on my code ?

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8"/>
    <script type="text/javascript">
    window.onload = function() {
    	var dataPoints = [];
    	var chart;
    	$.getJSON("test.json", function(data) {  
    		$.each(data, function(key, value){
    			dataPoints.push({x: parseInt(value)[0], y: parseInt(value[1]), z: parseInt(value[2])});
    		});
    		chart = new CanvasJS.Chart("chartContainer",{
    			title:{
    				text:"Live Chart with dataPoints from External JSON"
    			},
    			data: [{
    				type: "line",
    				dataPoints : dataPoints,
    			}]
    		});
    		chart.render();
    		updateChart();
    	});
    	function updateChart() {
    	$.getJSON("test.json" + (dataPoints.length + 1) + (dataPoints[dataPoints.length - 1].y) + function(data) {
    		$.each(data, function(key, value) {
    			dataPoints.push({
    			x: parseInt(value[0]),
    			y: parseInt(value[1]),
    			z: parseInt(value[2])
    			});
    		});
    		chart.render();
    		setTimeout(function(){updateChart()}, 1000);
    	});
    	}
    }
    </script>
    <script type="text/javascript" src="http://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="http://canvasjs.com/assets/script/canvasjs.min.js"></script>
    </head>
    <body>
    <div id="chartContainer" style="height: 300px; width: 100%;"></div>
    </body>
    </html>

    This is my json
    [[-2518,69,-192]]

    You can see my chart in here http://sne.dlinkddns.com:6060/html/node/Node/public/canpas.html
    Thank you very much

    #12325

    Vishwas R
    Keymaster

    Sandonet,

    Replacing $.getJSON("test.json" + (dataPoints.length + 1) + (dataPoints[dataPoints.length - 1].y) + function(data) with $.getJSON("test.json", function(data) { in updateChart function should work fine.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.