Home Forums Chart Support Data from Ajax is not updating Chart

Data from Ajax is not updating Chart

Viewing 6 posts - 1 through 6 (of 6 total)
  • #9780
    <!DOCTYPE HTML>
    <html>
    <head>
    <script src="js/jquery.js"></script> 
    <script src="../library/canvasjs/canvasjs.min.js"></script>
    
    	<script type="text/javascript">
    	window.onload = function () {
    
          var dps = [{x: 1, y: 10}];   //dataPoints. 
    
          var chart = new CanvasJS.Chart("chartContainer",{
          	title :{
          		text: "Live Data"
          	},
          	axisX: {						
          		title: "Axis X Title"
          	},
          	axisY: {						
          		title: "Units"
          	},
          	data: [{
          		type: "line",
          		dataPoints : dps
          	}]
          });
    
          chart.render();
          var xVal = dps.length + 1;
          var yVal = 20;	
          var updateInterval = 1000;
    
    var updateChart = function () {
    $.getJSON('ajax/mikrotikTraffic.php', function (result) {
    
    dps.splice(0, dps.length);
    
    $.each(result, function (index, value) {
    dps.push(value);
    console.log(result);
    });
    });
    
    chart.render();
    };
    
    /*
    			var updateChart = function () {
              $.getJSON('ajax/mikrotikTraffic.php', function (result) {
               console.log(result);
              chart.options.data[0].dataPoints = result;
      });
    
    chart.render();
    };
    */
    
    /*      //var updateChart = function () {
          var ip = '10.26.29.50';	
          	
          //	yVal = yVal +  Math.round(5 + Math.random() *(-5-5));
             // yVal = bandwidthCheck(ip);  
            
          	dps.push({x: xVal,y: yVal});
          	
          	xVal++;
          	if (dps.length >  10 )
          	{
          		dps.shift();				
          	}
    
          	chart.render();		
    
    	// update chart after specified time. 
    
    };
     */
    
    function bandwidthCheck(ip) {
    //show the cards associated with this set
    
        //var ajax_load = "<img src='../../images/processing.gif' width='20' alt='loading...' />";
        //$('#apResults'+equip_id).html(ajax_load);
       
       $.getJSON('ajax/mikrotikTraffic.php', function (result) {
    
          chart.options.data[0].dataPoints = result;
     });
     
     /*  
        $.ajax({
            type: "POST",
            url: "ajax/mikrotikTraffic.php",
            data:{ip: ip},
            //dataType: 'html',
            success: function(responseText) {
              //$('#apResults'+equip_id).html(responseText).addClass('equipresults');
              console.log(responseText);
              //handleData(responseText);
           }
        });
        //return false;
      */
    }
    
    setInterval(function(){updateChart()}, updateInterval); 
    }
    </script>
    <script type="text/javascript" src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
    </head>
    <body>
    	<div id="chartContainer" style="height: 300px; width: 100%;">
    	</div>
    </body>
    </html>

    I have tried several ways to update my chart. I can see that the data is coming over in the console, but the chart is not rendering the new data.

    Here is my script running: https://alohab.com/customer/crm_admin/realtimeTraffic.php

    #9781

    We just looked into your JSON API and its returning 0 every time. If you can make it return some data we would be able help you in updating the chart.

    #9782

    Thank you for looking at this.
    Sorry, it is pulling data from a Router that I had turned off over night. I have verified that when the router is on, it is not 0. I can leave the router on though.

    • This reply was modified 8 years, 10 months ago by elbowwilham.
    #9784

    I got it to work.
    I was also able to get it to work with 2 lines.
    Let me know if you want me to show my code.

    #20774

    Hi elbowwilham,

    Could you tell me please the lines you added in order to mak it work? Thx

    #20775

    Hi elbowwilham,

    Could you tell me please the lines you added in order to mak it work? I tried a lot of things but i can’t make it update. Txh a lot.

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

You must be logged in to reply to this topic.