how to create pie chart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumaabey
    New Member
    • Jun 2007
    • 29

    how to create pie chart

    my requirement is
    1.To create a pie chart
    2.the value will be updating from the database

    How can i do this ?
  • nitinpatel1117
    New Member
    • Jun 2007
    • 111

    #2
    You can use a PHP extension (GD library) to create images.

    see this for more info:


    once you get the GD library configured properly you can either do the coding yourselve, or do a search on google for frree libaries that draw graphs for you. NOTE: these libraries will still need the GD extension on your server.

    Comment

    • sumaabey
      New Member
      • Jun 2007
      • 29

      #3
      please help me.gd library is installed in my server.i need some sample code

      Comment

      • nitinpatel1117
        New Member
        • Jun 2007
        • 111

        #4
        look at this library, its what i used a while back, its good for creating simple graphs .

        Has good documentation with sample code.

        Also i think it's opensource

        Download PHPlot for free. A PHP class for creating graphs, plots, charts. PHPlot is a PHP graphics class for creating charts and plots. It works with PHP5 and the PHP GD extension to produce PNG, GIF, or JPEG images.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, sumaabey.

          The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. You have not asked a question. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

          Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

          Then when you are ready post a new question in this thread.

          MODERATOR

          Also, take a look at this thread. Note how the other poster offered to try it first.

          Comment

          • Rhishabh07
            New Member
            • Jul 2007
            • 21

            #6
            http://192.168.1.60/dlf/

            Comment

            • Rhishabh07
              New Member
              • Jul 2007
              • 21

              #7
              http://192.168.1.60/dlf/.

              Comment

              • sumaabey
                New Member
                • Jun 2007
                • 29

                #8
                thanks everybody i solved the problem

                Comment

                • Rhishabh07
                  New Member
                  • Jul 2007
                  • 21

                  #9
                  <?php
                  /*************** *************** **
                  2D Pie Chart Version 1.0
                  Programer: Xiao Bin Zhao
                  E-mail: love1001_98@yah oo.com
                  Date: 03/31/2001
                  All Rights Reserved 2001.
                  *************** *************** **/

                  /*************Co nfiguration Starts Here*********** *******/

                  $chartTitle = "Percentage of Products in Database"; //pie chart name

                  /*************** **********End** *************** ***********/

                  /*************** **For Programers Only*********** *********/
                  $imageWidth = 600; //image width
                  $imageHeight = 400; //image height
                  $diameter = 250; //pie diameter
                  $centerX = 225; //pie center pixels x
                  $centerY = 225; //pie center pixels y
                  $labelWidth = 10; //label width, no need to change
                  /*************** **********End** *************** ***********/

                  //db connection info.
                  $dbuser = 'db';

                  $dbhost = 'localhost';

                  $dbpass = '';

                  $dbname = 'rhishabh';

                  $mysql_link = mysql_connect($ dbhost,$dbuser, $dbpass) or die ("Could not connect"); //connect to db

                  mysql_select_db ($dbname) or die ("DB select failed"); //select db

                  $query1 = "select productname, count(*) as groupcount from product group by productname"; //get product count by groups of products

                  $query2 = "select count(*) as countammount from product"; //get the ammount of products in the db

                  $result1 = mysql_db_query( $dbname,$query1 ); //queryresult1

                  $result2 = mysql_db_query( $dbname,$query2 ); //queryresult2

                  $result3 = mysql_db_query( $dbname,$query1 ); //queryresult3

                  $num_products = mysql_num_rows( $result1); //count number of products

                  while ($row = mysql_fetch_ass oc ($result2)) //put total ammount of products in db from query in a var
                  {
                  $dataTotal = $row[countammount];
                  }

                  function circlePoint( $deg, $dia )
                  {
                  $x = cos( deg2rad( $deg ) ) * ( $dia / 2 );
                  $y = sin( deg2rad( $deg ) ) * ( $dia / 2 );
                  return array( $x, $y );
                  }

                  $im = ImageCreate( $imageWidth, $imageHeight ); //make image area

                  //color array for chart. addmore colors if you have more data than colors below.
                  $color[] = ImageColorAlloc ate( $im, 255, 0, 0 ); //red
                  $color[] = ImageColorAlloc ate( $im, 255, 204, 0 );//yellow
                  $color[] = ImageColorAlloc ate( $im, 153, 204, 0 );//green
                  $color[] = ImageColorAlloc ate( $im, 153, 51, 255 );//purple
                  $color[] = ImageColorAlloc ate( $im, 0, 128, 255 );//blue
                  $color[] = ImageColorAlloc ate( $im, 255, 0, 128 );//pink
                  $color[] = ImageColorAlloc ate( $im, 153, 51, 255 );//purple
                  $color[] = ImageColorAlloc ate( $im, 192, 192, 192 );//grey
                  $color[] = ImageColorAlloc ate( $im, 204, 204, 0 );
                  $color[] = ImageColorAlloc ate( $im, 64, 128, 128 );
                  $color[] = ImageColorAlloc ate( $im, 204, 102, 153 );
                  $white = ImageColorAlloc ate( $im, 255, 255, 255 );
                  $black = ImageColorAlloc ate( $im, 0, 0, 0 );
                  $grey = ImageColorAlloc ate( $im, 215, 215, 215 );

                  ImageFill( $im, 0, 0, $white ); //make image background

                  $degree = 0;

                  /*************** *************** ****
                  make the pie chart with percentages
                  *************** *************** ****/

                  $i=-1; //set color array counter

                  while ($row = mysql_fetch_ass oc ($result1))
                  {

                  $i < sizeof( $row ); $i++; //counter for color array

                  $startDegree = round( $degree );
                  $degree += ( $row[groupcount] / $dataTotal ) * 360;
                  $endDegree = round( $degree );

                  $currentColor = $color[ $i % ( count( $color ) ) ];

                  ImageArc( $im, $centerX, $centerY, $diameter, $diameter, $startDegree, $endDegree, $currentColor );

                  list( $arcX, $arcY ) = circlePoint( $startDegree, $diameter );
                  ImageLine( $im, $centerX, $centerY, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor );

                  list( $arcX, $arcY ) = circlePoint( $endDegree, $diameter );
                  ImageLine( $im, $centerX, $centerY, ceil( $centerX + $arcX ), ceil( $centerY + $arcY ), $currentColor );

                  $midPoint = round( ( ( $endDegree - $startDegree ) / 2 ) + $startDegree );
                  list( $arcX, $arcY ) = circlePoint( $midPoint, $diameter / 1.5 );
                  ImageFillToBord er( $im, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor, $currentColor );
                  //joe: i added -6 to center %'s better
                  ImageString( $im, 2, floor( $centerX + $arcX - 6 ), floor( $centerY + $arcY - 6 ), intval( round( $row[groupcount] / $dataTotal * 100 ) ) . "%", $white );

                  }

                  /*************** *************** ****
                  setup for the menu and print title
                  *************** *************** ****/

                  $labelX = $centerX + $diameter / 2 + 10;
                  $labelY = $centerY - $diameter / 4;
                  $titleX = $labelX - $diameter / 4;
                  $titleY = $centerY - $diameter / 2;
                  //ImageString( $im, 3, $titleX + 1, $titleY + 1, $chartTitle, $grey );
                  ImageString( $im, 3, $titleX, $titleY, $chartTitle, $black ); //print chart title
                  ImageString( $im, 1, $labelX, $titleY + 14, date( "Y-m-d H:i:sa" ), $black ); //print date

                  /*************** *************** ****
                  make the menu,lables,tot als
                  *************** *************** ****/

                  $i=-1; //set color array counter

                  while ($row1 = mysql_fetch_ass oc ($result3))
                  {

                  $i < sizeof( $row1 ); $i++; //counter for color array

                  $currentColor = $color[ $i % ( count( $color ) ) ];
                  ImageRectangle( $im, $labelX, $labelY, $labelX + $labelWidth, $labelY + $labelWidth, $black );
                  ImageFilledRect angle( $im, $labelX + 1, $labelY + 1, $labelX + $labelWidth, $labelY + $labelWidth, $currentColor );
                  ImageString( $im, 2, $labelX + $labelWidth + 5, $labelY, $row1[productname], $black );
                  //ImageString( $im, 2, $labelX + $labelWidth + 60, $labelY, $row1[groupcount], $black );
                  $labelY += $labelWidth + 2;
                  }

                  /*************** *************** ****
                  make the total
                  *************** *************** ****/

                  ImageString( $im, 3, $labelX, $labelY, "Total:", $black );
                  ImageString( $im, 3, $labelX + $labelWidth + 60, $labelY, $dataTotal, $black );
                  //ImageString( $im, 2, $labelX, $labelY + 15, $logo, $black );

                  Header( "Content-type: image/jpeg" ); //output image
                  Imagejpeg( $im );
                  ImageDestroy( $im ); //remove image from memory
                  ?>

                  Comment

                  Working...