pChart 2.x forum
http://wiki.pchart.net/forum/

Subcategory Extension
http://wiki.pchart.net/forum/viewtopic.php?f=4&t=2190
Page 1 of 1

Author:  theodamien [ Mon Sep 26, 2011 1:59 pm ]
Post subject:  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 10899 times ]

Author:  theodamien [ Fri Oct 21, 2011 3:36 pm ]
Post subject:  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;
   
       }
    }
}

?>


Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/