estoy realizando un grafico que se actualiza directamente sin tener que recargar toooda la pagina. Este grafico lee los datos desde una base de datos MYSQL. el problema que tengo es que el grafico se va actualizando automaticamente cuando coje valores aleatorios, pero cuando le hago leer desde la base de datos, solo accede una sola vez y el resto ya no vuelve a leer.
Os pongo el codigo que es mas ilustrativo:
Cita:
Inicialmente dentro del timer esta la funcion: <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>Visifire Test Page</title>
<script type="text/javascript" src="../../../../Silverlight Binaries/Visifire.js"></script>
<script type="text/javascript">
var count=0;
var chart; // A global Chart variable
var dataPointIndex = 0; // Variable for DataPoint Indexing
/*
Function updates YValue property of a DataPoint
*/
function updateYValue(newYValue, dataPointIndex)
{
if (chart != null)
{
try
{
chart.Series[0].DataPoints[dataPointIndex].SetPropertyFromJs("YValue", newYValue);
}
catch (e)
{
}
}
};
/*
Timer function allows updating chart data with a time interval.
*/
function timer()
{
<? include("datos.php");?>
var newYValue1 =Math.abs(<? echo $valor ?>);
//var newYValue = Math.abs(Math.random() * 100 - 10 * dataPointIndex);
var newYValue2 =Math.abs(Math.random() * 100 - 10 * dataPointIndex);
// Update a YValue property of a DataPoint
updateYValue(newYValue1, 0);
updateYValue(newYValue2, 1);
// Update dataPointIndex
dataPointIndex = (dataPointIndex > 4) ? 0 : dataPointIndex + 1;
// Set timeout for timer() function
setTimeout(timer, 1000);
};
</script>
</head>
<body>
<div id="VisifireChart0"; align="center" >
<script type="text/javascript">
// Create Visifire object
var vChart = new Visifire('../../../../Silverlight Binaries/SL.Visifire.Charts.xap', "MyChart", 500, 300);
// vChart1.setLogLevel(0); // If you want to disable logging.
// Chart XML
var chartXml = '<vc:Chart xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Char ts" Width="500" Height="300" AnimationEnabled="False" BorderThickness="0.5" Background="White" BorderBrush="Black" Padding="3">'
+ ' <vc:Chart.Titles>'
+ ' <vc:Title Text="Do you like this song?" FontSize="14"/>'
+ ' </vc:Chart.Titles>'
+ ' <vc:Chart.Series>'
+ ' <vc:DataSeries RenderAs="Column" LabelEnabled="True">'
+ ' <vc:DataSeries.DataPoints>'
+ ' <vc:DataPoint AxisXLabel="YES" YValue="35"/>'
+ ' <vc:DataPoint AxisXLabel="NO" YValue="32"/>'
+ ' </vc:DataSeries.DataPoints>'
+ ' </vc:DataSeries>'
+ ' </vc:Chart.Series>'
+ ' </vc:Chart>';
// Set Chart Data XML
vChart.setDataXml(chartXml);
/*
On loaded event, array of charts is received as event arguments. It fires once the chart is loaded.
Events can be attached to chart elements. And required customization can be done here.
*/
vChart.loaded = function(args)
{
chart = args[0]; // Chart reference.
// Start Timer
timer();
}
// Render chart
vChart.render("VisifireChart0");
</script>
</div>
</body>
</html>
<head>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>Visifire Test Page</title>
<script type="text/javascript" src="../../../../Silverlight Binaries/Visifire.js"></script>
<script type="text/javascript">
var count=0;
var chart; // A global Chart variable
var dataPointIndex = 0; // Variable for DataPoint Indexing
/*
Function updates YValue property of a DataPoint
*/
function updateYValue(newYValue, dataPointIndex)
{
if (chart != null)
{
try
{
chart.Series[0].DataPoints[dataPointIndex].SetPropertyFromJs("YValue", newYValue);
}
catch (e)
{
}
}
};
/*
Timer function allows updating chart data with a time interval.
*/
function timer()
{
<? include("datos.php");?>
var newYValue1 =Math.abs(<? echo $valor ?>);
//var newYValue = Math.abs(Math.random() * 100 - 10 * dataPointIndex);
var newYValue2 =Math.abs(Math.random() * 100 - 10 * dataPointIndex);
// Update a YValue property of a DataPoint
updateYValue(newYValue1, 0);
updateYValue(newYValue2, 1);
// Update dataPointIndex
dataPointIndex = (dataPointIndex > 4) ? 0 : dataPointIndex + 1;
// Set timeout for timer() function
setTimeout(timer, 1000);
};
</script>
</head>
<body>
<div id="VisifireChart0"; align="center" >
<script type="text/javascript">
// Create Visifire object
var vChart = new Visifire('../../../../Silverlight Binaries/SL.Visifire.Charts.xap', "MyChart", 500, 300);
// vChart1.setLogLevel(0); // If you want to disable logging.
// Chart XML
var chartXml = '<vc:Chart xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Char ts" Width="500" Height="300" AnimationEnabled="False" BorderThickness="0.5" Background="White" BorderBrush="Black" Padding="3">'
+ ' <vc:Chart.Titles>'
+ ' <vc:Title Text="Do you like this song?" FontSize="14"/>'
+ ' </vc:Chart.Titles>'
+ ' <vc:Chart.Series>'
+ ' <vc:DataSeries RenderAs="Column" LabelEnabled="True">'
+ ' <vc:DataSeries.DataPoints>'
+ ' <vc:DataPoint AxisXLabel="YES" YValue="35"/>'
+ ' <vc:DataPoint AxisXLabel="NO" YValue="32"/>'
+ ' </vc:DataSeries.DataPoints>'
+ ' </vc:DataSeries>'
+ ' </vc:Chart.Series>'
+ ' </vc:Chart>';
// Set Chart Data XML
vChart.setDataXml(chartXml);
/*
On loaded event, array of charts is received as event arguments. It fires once the chart is loaded.
Events can be attached to chart elements. And required customization can be done here.
*/
vChart.loaded = function(args)
{
chart = args[0]; // Chart reference.
// Start Timer
timer();
}
// Render chart
vChart.render("VisifireChart0");
</script>
</div>
</body>
</html>
//var newYValue = Math.abs(Math.random() * 100 - 10 * dataPointIndex);
y yo lo que hago dentro del timer es acceder siempre a la base de datos "datos.php" donde hago las consultas MYSQL y luego lo pongo en var newYValue:
var newYValue1 =Math.abs(<? echo $valor ?>);
El problema como he dicho que esto solo lo hace la primera vez y luego deja el valor fijo, no accede cada vez a la base de datos :s
la consulta php que hago es la siguiente:
Cita:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';
$database = 'embracelet';
$table = 'eventos';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
$consulta = mysql_query("SELECT * FROM eventos WHERE id='1'") or die ("Error en la consulta");
$row = mysql_fetch_array( $consulta );
// Print out the contents of the entry
$valor = $row['yes'];
echo $valor;
mysql_free_result($result);
?>
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';
$database = 'embracelet';
$table = 'eventos';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
$consulta = mysql_query("SELECT * FROM eventos WHERE id='1'") or die ("Error en la consulta");
$row = mysql_fetch_array( $consulta );
// Print out the contents of the entry
$valor = $row['yes'];
echo $valor;
mysql_free_result($result);
?>
Este es el grafico en cuestion que utilizo: [URL="http://visifire.com/popup.php?url=Column2D1.xml?version=15&width=600&h eight=375"]http://visifire.com/popup.php?url=Column2D1.xml?version=15&width=600&h eight=375[/URL]