1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

get cool graphs out your database

Discussion in 'PHP' started by falcondriver, Feb 16, 2007.

  1. #1
    ok, i was working on another project and came across this site for php/swf charts:
    http://www.maani.us/charts/index.php?menu=Gallery.

    looks cool and is quite easy to implement to your site. all you need is some data to display, the .swf and php includes here, unzip it to your webserver and you are ready to go.

    now create a test.php and place it in the same folder:

    
    <HTML>
    <BODY bgcolor="#FFFFFF">
    <?php
    
    //include charts.php to access the InsertChart function
    include "charts.php";
    
    echo InsertChart ( "charts.swf", "charts_library", "chartdata.php", 600, 600 );
    
    ?>
    
    </BODY>
    </HTML>
    
    Code (markup):
    this creates just the html code and parameters for the flash file, nothing magic here.
    now open chartdata.php and ad this functions on top:

    
    <?$
    //this file should contain a textfile with your sql query
    filename = 'my_query.sql';
    
    //read the textfile
    $sql = file_get_contents($filename);
    
    //get the result into an array
    $mychart = sql2array($sql);
    
    //here is how it works, change your server/username/password:
    function sql2array($sql) {
    	$data = array();
    	$x = 0;
    	$y = 1;
    	$handle = mysql_pconnect("localhost","username","password");
    	$res = mysql_query($sql, $handle);
    	while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
    	  $x=0;
    	  foreach ($row as $col => $value) {
    	      $data[$x][0] = $col;
    	      $data[$x][$y] = $value;
    	      $x++;
    	
    	  }
    	  $y++;
    	}
    	return $data;
    }
    
    //add the basic template for the chart:
    $chart[ 'axis_category' ] = array ( 'font'=>"arial", 'bold'=>true, 'size'=>11, 'color'=>"000000", 'alpha'=>50, 'skip'=>2 );
    $chart[ 'axis_ticks' ] = array ( 'value_ticks'=>false, 'category_ticks'=>true, 'major_thickness'=>2, 'minor_thickness'=>1, 'minor_count'=>3, 'major_color'=>"000000", 'minor_color'=>"888888" ,'position'=>"outside" );
    $chart[ 'axis_value' ] = array ( 'font'=>"arial", 'bold'=>true, 'size'=>10, 'color'=>"000000", 'alpha'=>50, 'steps'=>4, 'prefix'=>"", 'suffix'=>"", 'decimals'=>0, 'separator'=>"", 'show_min'=>true );
    
    $chart[ 'chart_border' ] = array ( 'color'=>"000000", 'top_thickness'=>1, 'bottom_thickness'=>2, 'left_thickness'=>0, 'right_thickness'=>0 );
    //tutorial data here, we dont need them any more
    //$chart[ 'chart_data' ] = array ( array ( "","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31" ), array ( "Region A",10,12,11,15,20,22,21,30,31,32,28,29,40,41,45,50,65,45,50,51,65,60,62,65,45,55,59,52,53,40,45 ), array ( "Region B",30,32,35,40,42,35,36,31,35,36,40,42,40,41,45,40,38,36,30,29,28,25,28,29,30,40,32,33,34,30,35 ) );
    // use our own data instead
    $chart[ 'chart_data' ] =  $mychart;
    $chart[ 'chart_grid_h' ] = array ( 'alpha'=>10, 'color'=>"0066FF", 'thickness'=>28 );
    $chart[ 'chart_grid_v' ] = array ( 'alpha'=>10, 'color'=>"0066FF", 'thickness'=>1 );
    $chart[ 'chart_rect' ] = array ( 'x'=> 20, 'y'=>50, 'width'=>550, 'height'=>550, 'positive_color'=>"FFFFFF", 'positive_alpha'=>40 );
    
    $chart[ 'draw' ] = array ( array ( 'type'=>"text", 'color'=>"ff6644", 'alpha'=>70, 'font'=>"arial", 'rotation'=>2, 'bold'=>true, 'size'=>53, 'x'=>5, 'y'=>0, 'width'=>600, 'height'=>600, 'text'=>"monthly report", 'h_align'=>"center" ),
                               array ( 'type'=>"text", 'color'=>"000000", 'alpha'=>60, 'font'=>"arial", 'rotation'=>0, 'bold'=>true, 'size'=>16, 'x'=>25, 'y'=>55, 'width'=>300, 'text'=>"June 2005", 'h_align'=>"left" ) );
    
    $chart[ 'legend_rect' ] = array ( 'x'=>-100, 'y'=>-100, 'width'=>10, 'height'=>10, 'margin'=>0 ); 
    
    $chart[ 'series_color' ] = array ( "dd6b66","7e6cee" );
    $chart[ 'series_gap' ] = array ( 'set_gap'=>0, 'bar_gap'=>0 );
    
    //return the data as xml for the .swf
    SendChartData ( $chart );
    ?>
    
    Code (markup):
    and thats it! go to your webserver, write a small sql query and save it under the path you specified in chartdata.php and then just call the test.php .
    hope i have no errors, works fine here.
    now you can just write a bunch of sql queries, make a selection field and display them as graphs.
    an easy way to impress your boss and the managers, because they just LOVE graphs, bars and everything they can put into meaningless powerpoint presentations :D

    let me know if you like it, maybe you can place a link to one of my sites for me :)
     
    falcondriver, Feb 16, 2007 IP
  2. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #2
    Hey, I took a look at your system, it isn't too bad but there are not enough features to convince me to stop using JpGraph (http://www.aditus.nu/jpgraph/).

    I know yours is geared towards a Flash market but I think of that as a limitation rather than a positive. Plus your license is too restrictive, I didn't like the fact that clicking a chart would direct a user to your web site.
     
    SilkySmooth, Feb 17, 2007 IP