Sign in to follow this  
pimlinders

Update chart automatically every minute?

Recommended Posts

I'm trying to figure out how to have my charts updated every minute without the user having to do anything (such as clicking on a button/chart), Is this possible to do? My data is stored in a mysql database and I based my php code off of the "Combining FusionCharts, PHP & JavaScript (dataURL) method".

Share this post


Link to post
Share on other sites
Guest Rajroop

Hello,

 

 

 

You can achieve your requirements by using the setInterval function of JavaScript.

 

 

 

The setInterval method is available on window objects. It is used to add a delay to a function (usually one that is going to be executed multiple times). This is not a reserved word so you can declare your own variable or function called setInterval but if you do then you will not be able to use the method.

 

 

 

Ref: http://www.switchonthecode.com/tutorials/javascript-tutorial-using-setinterval-and-settimeout

 

 

 

For example:

 

 

 

<script type="text/javascript">

 

var myChart = new FusionCharts("Column2D.swf", "myChartId", "500", "400", "0", "1");

 

myChart.setDataURL("Data.xml");

 

myChart.render("chartdiv");

 

var intrv;

 

function FC_Rendered(DOMId){

 

intrv = window.setInterval(function(){updateChart();} , 1000);

 

}

 

 

 

I hope this helps. :)

Share this post


Link to post
Share on other sites

I'm generating my xml with PHP, I guess I should create a php function that gets called on load and call that function using setInterval. Does any one know of a way to do this, I assume this is done with ajax but I'm a little confused on how I could refresh only the chart and not the entire page. Again thanks for the help.

Share this post


Link to post
Share on other sites

Hi,

 

 

 

If you are using FusionCharts v3 you can do it just using setInterval without any 3rd party AJAX or native AJAX code.

 

 

 

FusionCharts v3 has inbuilt AJAX support.

 

 

 

Only you would need to specify the php page's URL which is generating the XML to charts's setDataURL() function.

 

 

 

Thats it!

 

 

 

so,

 

 

 

now your updateCharts() function will have a changed line :

 

 

 

getChartFromId('myChartid').setDataXML(YourPHPScriptURL);

 

 

 

Here, instead of 'myChartid' place your chart ID and pass your PHP URL.

 

 

 

The chart will load the XML form the PHP file and would redraw. No page refresh!

Share this post


Link to post
Share on other sites

I ended up creating a php file that generates xml files and took out the php code that generated the initial xml file on the main page and having javascript generate the chart, everything works now, thanks for the help!

Edited by Guest

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this