Drawing a pie chart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rhishabh07
    New Member
    • Jul 2007
    • 21

    Drawing a pie chart

    how to use pie chart?

    plz give me suggestion?
  • Rhishabh07
    New Member
    • Jul 2007
    • 21

    #2
    piechart

    how to make pie chart using php?

    plz suggest me.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Rhishabh07.

      Did you mean how to create a pie chart in PHP?

      Check out the PHP Pie Chart class.

      Comment

      • Rhishabh07
        New Member
        • Jul 2007
        • 21

        #4
        hi pbmods

        thanx at first i try then reply u back.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Rhishabh.

          Sounds good. If you run into any trouble, post back anytime :)

          Comment

          • Rhishabh07
            New Member
            • Jul 2007
            • 21

            #6
            thanx a lot.i did it.

            here is the code:


            <?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...