문제

I've an array for Y-axis and i want to display them in graph...and want to reload that graph after every second... i got this graph using an AJAX and display in HOME PAGE...

Chart code is as below..

    function graph1()
{
    $dt=array();    
    $q=mysql_query("select * from pricee ") or die (mysql_error());
    while($data=mysql_fetch_object($q))
    {
        array_push($dt,$data->price);
    }       
        $datay = $dt;


        for( $i=0; $i < sizeof($datay); ++$i )
        {
            $data[$i] = $datay[$i];         
        }

        // Create the new graph
        $graph = new Graph(540,300,auto);

        // Slightly larger than normal margins at the bottom to have room for
        // the x-axis labels
        $graph->SetMargin(40,40,30,130);

        // Fix the Y-scale to go between [0,100] and use date for the x-axis
        $graph->SetScale('datlin',0,max($datay));       

        // Adjust the start time for an "even" 5 minute, i.e. 5,10,15,20,25, ...
        $graph->xaxis->scale->SetTimeAlign(SECADJ_1);

        // Force labels to only be displayed every 1 second
        $graph->xaxis->scale->ticks->Set(1);

        // Use hour:minute format for the labels
        $graph->xaxis->scale->SetDateFormat('H:i:s');

        $graph->title->Set("Example on Date scale");

        // Set the angle for the labels to 90 degrees
        $graph->xaxis->SetLabelAngle(90);

        $line = new LinePlot($data,$xdata);
        $line->SetLegend('Merc Price');
        $line->SetFillColor('lightblue@0.5');
        $graph->Add($line);
        return $graph->Stroke();
}

Thank you friends

도움이 되었습니까?

해결책

Here you can see working example. Pretty good solution http://www.wattnotions.com/making-updating-graph-using-javascript-jpgraph/

The main idea is summarizing from "Rizwan Shamsher Kaim Khani" and official doc http://jpgraph.net/download/manuals/chunkhtml/ch05s05.html

If you very simplify code, you can get very short js code into your HTML. php file remains as it is.

<!DOCTYPE html>
<html>
<body>
    <script>
        function refresh() {
            setInterval(function() {
                var image = document.getElementById('myImage');
                image.src = "index.php?" + new Date().getTime();
            }, 1000);
        }
        refresh();
    </script>
    <img id="myImage" src="index.php"/>

</body>
</html>

To see refresh tries realtime add this into your index.php */

/* NOTE! $today variable is mandatory usage */
$today = date("Y-m-d H:i:s");
$graph->title->Set('Title'.'     '.$today);

다른 팁

The Graph by default, I think could not be refreshed but you need to refresh that area where your graph is displaying.

First You need to make a file where your graph code is written.

Suppose you made a file i.e. graph.php

<?php // content="text/plain; charset=utf-8"

function Graph1() {

require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');

$datay = array(0,25,12,47,27,27,0);

// Setup the graph
$graph = new Graph(350,250);
$graph->SetScale("intlin",0,$aYMax=50);

$theme_class= new UniversalTheme;
$graph->SetTheme($theme_class);

$graph->SetMargin(40,40,50,40);

$graph->title->Set('Inverted Y-axis');
$graph->SetBox(false);
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// For background to be gradient, setfill is needed first.
$graph->ygrid->SetFill(true,'#FFFFFF@0.5','#FFFFFF@0.5');
$graph->SetBackgroundGradient('#FFFFFF', '#00FF7F', GRAD_HOR, BGRAD_PLOT);

$graph->xaxis->SetTickLabels(array('G','F','E','D','C','B','A'));
$graph->xaxis->SetLabelMargin(20);
$graph->yaxis->SetLabelMargin(20);

$graph->SetAxisStyle(AXSTYLE_BOXOUT);
$graph->img->SetAngle(180); 

// Create the line
$p1 = new LinePlot($datay);
$graph->Add($p1);

$p1->SetFillGradient('#FFFFFF','#F0F8FF');
$p1->SetColor('#aadddd');

// Output line
$graph->Stroke();

}

echo Graph1();

?>

Now You want to refresh your Graph, So you can do it by given below code.

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
    $(document).ready(function() {
        setInterval(function() {
            $('.details').html('<img src="graph.php" />');
        }, 1000);
    });
</script>
</head>
<body>
    <div class="details">Graph Value</div> <!-- Position where you display your JPgraph !>
</body>
</html>

Hope It will help you.

Thanks

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top