View unanswered posts | View active topics It is currently Thu Mar 28, 2024 12:40 pm



Reply to topic  [ 2 posts ] 
Subcategory Extension 
Author Message
Regular pChart user
Regular pChart user

Joined: Fri Feb 04, 2011 11:23 am
Posts: 15
Post Subcategory Extension
This extension allows you to added a category tag on your barcharts.


Create a new file called "pSubcategorie.class.php" in the pChart2 class directory:

Code:
<?php

class pSubcategorie
{
    private $pChartObject;
   
    /* Class creator */
    function   __construct($pChartObject)
    {
   $this->pChartObject = $pChartObject;
    }
       
    /* Draw an indicator */
    function draw($X,$Y,$Size,$Margin, $SerieLinked, $Color = null)
    {   
   $Data = $this->pChartObject->DataSet->getData();
   if ($Color == null)
       $Color      = array("R"=>0,"G"=>103,"B"=>197,"Alpha"=>90);
   foreach($Data["Series"] as $SerieName => $Serie)
       {   
      if ( $Serie["isDrawable"] == TRUE && $SerieName == $Data["Abscissa"] )
          {   
         if (array_key_exists($SerieLinked, $Data["Series"]) === true)
             {
            $assoc = $Data["Series"][$SerieLinked]["Data"];         
            foreach($Serie["Data"] as $Key => $Value)
                {               
               $MyData[] = array("key"      => $assoc[$Key],
                       "name"   => $Value);
                }
             }      
         break;
          }      
       }

   foreach ( $Data["Axis"] as $AxisId => $val)
       {
      if ($val['Identity'] ==  AXIS_X)
          break;
       }
   
   $Height = ($this->pChartObject->GraphAreaY2 - $this->pChartObject->GraphAreaY1);
   $Width = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1);
   $StepY = $Height /count($MyData);
   $StepX = $Width /count($MyData);

   $LastY = $Y;
   $LastX = $X;
   $i = 0;
   foreach ($MyData as $Key => $Value)
       {
      $i++;
      if ($Value['key'] == $MyData[$Key + 1]['key'])
          continue;   
      if ($Value['name'] == VOID)
          {
         $LastY = $Y  + $StepY*$i;
         $LastX = $X  + $StepX*$i;
         continue;
          }

      if ($Data["Axis"][$AxisId]["Position"] ==  AXIS_POSITION_BOTTOM)
          {
         $YPos  = $Y - $Margin;
         $XPos  = $X  + $StepX*$i;
         $this->pChartObject->drawFilledRectangle($LastX,$YPos,$XPos,$YPos+$Size,$Color);
         
         $this->pChartObject->drawText($XPos - (($XPos - $LastX)/2),$YPos+($Size/2),$Value["key"],
                        array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"R"=>255,"G"=>255,"B"=>255));
         
          }
      else if ($Data["Axis"][$AxisId]["Position"] ==   AXIS_POSITION_LEFT)
          {
         $YPos  = $Y  + $StepY*$i;
         $XPos  = $X - $Margin;

         $this->pChartObject->drawFilledRectangle($XPos,$LastY,$XPos+$Size,$YPos,$Color);
         
         $this->pChartObject->drawText($XPos - 5,$YPos-(($YPos - $LastY)/2),$Value["key"],
                     array("Align"=>TEXT_ALIGN_MIDDLERIGHT));
          }
             
      $LastY = $YPos;
      $LastX = $XPos;
       }
    }
}

?>


Don't forget to include pSubcategorie.class.php in your source code

Code:
<?php

/* pChart library inclusions */
include("pData.class.php");
include("pDraw.class.php");
include("pPie.class.php");
include("pImage.class.php");
include("pSubcategorie.class.php");

$Chart = new myChart($X, $Y);

$used = array(25,256,VOID, 988,36);
$Category = array("Category 1", "Category 1","", "Category 2","Category 2");
$abs = array("a","b",VOID, "c","d");

// Dataset definition
$DataSet = new pData;
$DataSet->addPoints($used, "Used Space");
$DataSet->setPalette("Used Space", array("R"=>255,"G"=>12,"B"=>12,"Alpha"=>90));

$DataSet->addPoints($Category, "Category");
$DataSet->setPalette("Category", array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>90));

$DataSet->addPoints($abs, "Labels");
$DataSet->setPalette("Labels", array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>90));

$DataSet->setAxisName(0, "Size (Giga)");
$DataSet->setAbscissa("Labels");

$DataSet->setSerieDrawable("Category",FALSE);

/* Set the default font properties */
$myPicture->setFontProperties(array("FontName"=>"tahoma.ttf","FontSize"=>8));

$myPicture->setGraphArea(220,60,$X - 10,$Y - 10);

$myPicture->drawScale(array("Factors"=>array(2.5), "DrawSubTicks"=>TRUE,"Mode"=>SCALE_MODE_ADDALL_START0,"CycleBackground"=>TRUE,"DrawXLines"=>TRUE,"Pos"=>SCALE_POS_TOPBOTTOM,
                 "GridR"=>160,"GridG"=>160,"GridB"=>160));
   
   
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
$myPicture->drawStackedBarChart(array("DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_MANUAL, "Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayR"=>0,"DisplayG"=>0,"DisplayB"=>0,"DisplaySize"=>8,"BorderR"=>100,"BorderG"=>100,"BorderB"=>100));

$myPicture->setShadow(FALSE);

$Categorie = new pSubcategorie($myPicture);
$Categorie->draw(90,60,15,-5,"Category");
   
/* Render the picture (choose the best way) */
$myPicture->autoOutput();



Attachments:
Sans titre.png
Sans titre.png [ 101.95 KiB | Viewed 10039 times ]
Mon Sep 26, 2011 1:59 pm
Profile
Regular pChart user
Regular pChart user

Joined: Fri Feb 04, 2011 11:23 am
Posts: 15
Post Re: Subcategory Extension
New version with a lot of bug fixes.


Code:
<?php

class pSubcategorie
{
    private $pChartObject;
   
    /* Class creator */
    function   __construct($pChartObject)
    {
   $this->pChartObject = $pChartObject;
    }

    function scaleGetXSettings($Data)
    {
   foreach($Data["Axis"] as $AxisID => $Settings)
       {
      if ( $Settings["Identity"] == AXIS_X )
          {
         $Rows = $Settings["Rows"];
         
         return(array($Settings["Margin"],$Rows));
          }
       }
    }
   
    /* Draw an indicator */
    function draw($X,$Y,$Size,$Margin, $SerieLinked, $Color = null)
    {   
   $Data = $this->pChartObject->DataSet->getData();
   if ($Color == null)
       $Color      = array("R"=>0,"G"=>103,"B"=>197,"Alpha"=>90);
   foreach($Data["Series"] as $SerieName => $Serie)
       {   
      if ( $Serie["isDrawable"] == TRUE && $SerieName == $Data["Abscissa"] )
          {   
         if (array_key_exists($SerieLinked, $Data["Series"]) === true)
             {
            $assoc = $Data["Series"][$SerieLinked]["Data"];         
            foreach($Serie["Data"] as $Key => $Value)
                {               
               $MyData[] = array("key"      => $assoc[$Key],
                       "name"   => $Value);
                }
             }      
         break;
          }      
       }

   foreach ( $Data["Axis"] as $AxisId => $val)
       {
      if ($val['Identity'] ==  AXIS_X)
          break;
       }
   list($XMargin,$XDivs) = $this->scaleGetXSettings($Data);

   $Interleave = 0.2;
   $XStep = ($this->pChartObject->GraphAreaX2-$this->pChartObject->GraphAreaX1-$XMargin*2)/$XDivs;
   $YStep = ($this->pChartObject->GraphAreaY2-$this->pChartObject->GraphAreaY1-$XMargin*2)/$XDivs;
   $XBarSize = ($XStep-2);
   $YBarSize = ($YStep-4);

   $LastY = $Y+$XMargin;
   $LastX = $X+$XMargin;

   $i = -1;
   foreach ($MyData as $Key => $Value)
       {
      $i++;
      if ($Value['key'] == $MyData[$Key + 1]['key'])
          {
         continue;
          }
      if ($Value['name'] == VOID )
          {
         $LastY += $YStep*2;
         $LastX += $XStep*2;
         $i--;
         continue;
          }
      if ($Data["Axis"][$AxisId]["Position"] ==  AXIS_POSITION_BOTTOM)
          {
         $YPos  = $Y - $Margin;
         $XPos  = $LastX  + ($XStep * $i);
         $this->pChartObject->drawFilledRectangle($LastX-$XBarSize,$YPos,$XPos+$XBarSize,$YPos+$Size,$Color);
         
         $this->pChartObject->drawText($XPos - (($XPos - $LastX)/2),$YPos+($Size/2),$Value["key"],
                        array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"R"=>255,"G"=>255,"B"=>255));
         $i = -1;
          }
      else if ($Data["Axis"][$AxisId]["Position"] ==   AXIS_POSITION_LEFT)
          {
         $YPos  = $LastY + ($YStep * $i);
         $XPos  = $X - $Margin;

         $this->pChartObject->drawFilledRectangle($XPos,$LastY-$YBarSize,$XPos+$Size,$YPos+$YBarSize,$Color);
         
         $this->pChartObject->drawText($XPos - 5,$YPos-(($YPos - $LastY)/2),$Value["key"],
                     array("Align"=>TEXT_ALIGN_MIDDLERIGHT));
         $i = -1;
          }
             
      $LastY = $YPos;
      $LastX = $XPos;
   
       }
    }
}

?>



Fri Oct 21, 2011 3:36 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron