SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    9
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Create Bar Graph Php

    hello. i am still new in using php coding.i hope anybody can explain me detail how to solve my problem.i want to generate a bar graph. the value is automatically update from my total of registered user in a month.after do some research i have found a code how to build a bar graph.here is it

    Code:
    <?
    # ------- The graph values in the form of associative array
    $values=array(
    "Jan" => 110,
    "Feb" => 130,
    "Mar" => 215,
    "Apr" => 81,
    "May" => 310,
    "Jun" => 110,
    "Jul" => 190,
    "Aug" => 175,
    "Sep" => 390,
    "Oct" => 286,
    "Nov" => 150,
    "Dec" => 196
    );
    
    
    
    $img_width=450;
    $img_height=300;
    $margins=20;
    
    
    
    # ---- Find the size of graph by substracting the size of borders
    $graph_width=$img_width - $margins * 2;
    $graph_height=$img_height - $margins * 2;
    $img=imagecreate($img_width,$img_height);
    
    
    
    $bar_width=20;
    $total_bars=count($values);
    $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);
    
    
    
    # ------- Define Colors ----------------
    $bar_color=imagecolorallocate($img,0,64,128);
    $background_color=imagecolorallocate($img,240,240,255);
    $border_color=imagecolorallocate($img,200,200,200);
    $line_color=imagecolorallocate($img,220,220,220);
    
    # ------ Create the border around the graph ------
    
    
    imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
    imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);
    
    
    
    # ------- Max value is required to adjust the scale -------
    $max_value=max($values);
    $ratio= $graph_height/$max_value;
    
    
    
    # -------- Create scale and draw horizontal lines --------
    $horizontal_lines=20;
    $horizontal_gap=$graph_height/$horizontal_lines;
    
    
    for($i=1;$i<=$horizontal_lines;$i++){
    $y=$img_height - $margins - $horizontal_gap * $i ;
    imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
    $v=intval($horizontal_gap * $i /$ratio);
    imagestring($img,0,5,$y-5,$v,$bar_color);
    
    
    }
    
    
    # ----------- Draw the bars here ------
    for($i=0;$i< $total_bars; $i++){
    # ------ Extract key and value pair from the current pointer position
    list($key,$value)=each($values);
    $x1= $margins + $gap + $i * ($gap+$bar_width) ;
    $x2= $x1 + $bar_width;
    $y1=$margins +$graph_height- intval($value * $ratio) ;
    $y2=$img_height-$margins;
    imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
    imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
    imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
    }
    header("Content-type:image/png");
    imagepng($img);
    
    
    ?>
    i need a help.how i can get the value for x is month and y is total user that registered for each month. here is some detail that i can provide. help me please

    HTML Code:
    CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(50) NOT NULL DEFAULT '',
    `address` varchar(100) NOT NULL DEFAULT '',
    `day` int(10) NOT NULL,
    `month` varchar(15) NOT NULL,
    `years` int(10) NOT NULL,
    `number` varchar(15) NOT NULL,
    `gender` varchar(100) NOT NULL,
    `email` varchar(100) NOT NULL,
    `password` varchar(100) NOT NULL DEFAULT '',
    `time` time NOT NULL,
    `date` date NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=114 DEFAULT CHARSET=latin1
    Code:
    <?php
    
    
    
    
    $user = $fullname = $address = $day = $month =$years =$number = $gender = $email = $password = $repeatpassword = $time = $date ="";
    
    
    if(isset($_POST['fullname'])) //check for existance <- ni check isset utk 1 form je dah ckp. isset tu yg biasa guna utk cek variable kosong ke x
    {
    
    
    
    //form data
    $fullname= strip_tags($_POST['fullname']);
    $address= strip_tags($_POST['address']);
    $day=strip_tags($_POST['day']);
    $month=strip_tags($_POST['month']);
    $years=strip_tags($_POST['years']);
    $number=strip_tags($_POST['number']);
    $gender=strip_tags($_POST['gender']);
    $email= strip_tags($_POST['email']);
    $password= strip_tags($_POST['password']);
    $repeatpassword= strip_tags($_POST['repeatpassword']);
    $time = date("H:i",time());
    $date = date('y/m/d', time()); ///date for registered user
    
    // echo $fullname;
    
    
    //encrypt password
    
    //$repeatpassword=md5($repeatpassword); ni tak perlu sebab yg kau nak simpan dalam database password je so aku dah 'comment' kan. (boleh padam bila2)
    
    if($password==$repeatpassword)
    {
    
    //check char length of username and fullname
    if (strlen($email)>25||strlen($fullname)>25){
    echo "email or fullname is too long!";
    } else {
    //check password length
    if (strlen($password)>25||strlen($password)<6){
    echo "your password must be between 6 to 25 characters";
    } else {
    //register the user!
    
    //encrypt password
    $password=md5($password);
    
    //Open database
    $connect = mysql_connect("localhost","root","");
    mysql_select_db("phplogin"); //select database
    
    $queryreg = mysql_query("
    
    INSERT INTO users VALUES ('','$fullname','$address','$day','$month','$years','$number','$gender','$email','$password','$time','$date')
    
    ");
    
    echo("You have been registered! <a href='index.php'>Return to login page</a>");
    }
    }
    } else {
    echo "Your password do not match!";
    }
    
    }
    
    ?>

  2. #2
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You prefer to do it in 100% php? looks like your using GD to draw the graphs. there are few other ways to do it buy using JQuery / JavaScript graph frameworks. where you just have to worry about filling in the XML file . and it'll look better.
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •