VCL
[ class tree: VCL ] [ index: VCL ] [ all elements ]

Source for file chart.inc.php

Documentation is available at chart.inc.php

  1. <?php
  2. /**
  3. *  This file is part of the VCL for PHP project
  4. *
  5. *  Copyright (c) 2004-2007 qadram software <support@qadram.com>
  6. *
  7. *  Checkout AUTHORS file for more information on the developers
  8. *
  9. *  This library is free software; you can redistribute it and/or
  10. *  modify it under the terms of the GNU Lesser General Public
  11. *  License as published by the Free Software Foundation; either
  12. *  version 2.1 of the License, or (at your option) any later version.
  13. *
  14. *  This library is distributed in the hope that it will be useful,
  15. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. *  Lesser General Public License for more details.
  18. *
  19. *  You should have received a copy of the GNU Lesser General Public
  20. *  License along with this library; if not, write to the Free Software
  21. *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  22. *  USA
  23. *
  24. */
  25.  
  26. use_unit("classes.inc.php");
  27. use_unit("controls.inc.php");
  28. use_unit("stdctrls.inc.php");
  29. use_unit('libchart/libchart/libchart.php');
  30.  
  31. //ChartTypes
  32. define('ctHorizontalChart','ctHorizontalChart');
  33. define('ctLineChart','ctLineChart');
  34. define('ctPieChart','ctPieChart');
  35. define('ctVerticalChart','ctVerticalChart');
  36.  
  37. /**
  38.  * SimpleChart class
  39.  *
  40.  * A class to generate chart graphics.
  41.  * The chart data currently can only be modified through code at run-time.
  42.  *
  43.  * The image is dynamically generated on each request.
  44.  */
  45. class SimpleChart extends GraphicControl
  46. {
  47.         protected $_onclick = null;
  48.  
  49.         protected $_chart = null;
  50.         protected $_charttype = ctVerticalChart;
  51.  
  52.         function __construct($aowner=null)
  53.         {
  54.                 //Calls inherited constructor
  55.                 parent::__construct($aowner);
  56.  
  57.                 $this->Width 400;
  58.                 $this->Height 250;
  59.  
  60.                 // Make sure the framework knows that this component dumps binary image data
  61.                 $this->ControlStyle="csImageContent=1";
  62.  
  63.                 $this->ControlStyle="csRenderOwner=1";
  64.                 $this->ControlStyle="csRenderAlso=StyleSheet";
  65.  
  66.                 // Create the chart
  67.                 $this->createChart();
  68.         }
  69.  
  70.         /**
  71.         * Creates a new chart with the defined ChartType and updates the protected chart variable.
  72.         * @return object Chart object.
  73.         */
  74.         function createChart()
  75.         {
  76.                 switch($this->_charttype)
  77.                 {
  78.                         case ctHorizontalChart$this->_chart = new HorizontalChart($this->Width$this->Height)break;
  79.                         case ctLineChart$this->_chart = new LineChart($this->Width$this->Height)break;
  80.                         case ctPieChart$this->_chart = new PieChart($this->Width$this->Height)break;
  81.                         case ctVerticalChart$this->_chart = new VerticalChart($this->Width$this->Height)break;
  82.                 }
  83.  
  84.                 return $this->_chart;
  85.         }
  86.  
  87.         /**
  88.         * Call fillDummyValues() to fill the chart with some dummy values.
  89.         * Only fill dummy values at design time.
  90.         */
  91.         function fillDummyValues()
  92.         {
  93.                 if (($this->ControlState csDesigning== csDesigning)
  94.                 {
  95.                         $this->_chart->setTitle("Preferred Web Language");
  96.  
  97.                         $this->_chart->addPoint(new Point("Perl"50));
  98.                         $this->_chart->addPoint(new Point("PHP"75));
  99.                         $this->_chart->addPoint(new Point("Java"30));
  100.                 }
  101.         }
  102.  
  103.         /**
  104.         * Clear all data of the chart (including titles, points, axes, ..)
  105.         */
  106.         function clearChart()
  107.         {
  108.                 // just call the reset function
  109.                 $this->_chart->reset();
  110.         }
  111.  
  112.         function init()
  113.         {
  114.                 parent::init();
  115.  
  116.                 $submitEventValue $this->input->{$this->getJSWrapperHiddenFieldName()};
  117.  
  118.                 if (is_object($submitEventValue))
  119.                 {
  120.                         // check if the a click event has been fired
  121.                         
  122.                         if ($this->_onclick != null && $submitEventValue->asString(== $this->getJSWrapperSubmitEventValue($this->_onclick))
  123.                         {
  124.                                 $this->callEvent('onclick'array());
  125.                         }
  126.                 }
  127.         }
  128.  
  129.         function dumpHeaderCode()
  130.         {
  131.                 parent::dumpHeaderCode();
  132.                 // only dump the header if not in design mode
  133.                 if (($this->ControlState csDesigning!= csDesigning)
  134.                 {
  135.                         // try to make the browser not to cache the image
  136.                         echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\" />\n";
  137.                 }
  138.         }
  139.  
  140.         /**
  141.          * Dump the chart graphic.
  142.          */
  143.         function dumpGraphic()
  144.         {
  145.                 // It's a graphic component that dumps binary data
  146.                 header("Content-type: image/png");
  147.  
  148.                 // try to make the browser not to cache the image
  149.                 header("Pragma: no-cache");
  150.                 header("Cache-Control: no-cache, must-revalidate")// HTTP/1.1
  151.                 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT")// Date in the past
  152.  
  153.                 $this->_chart->height $this->Height;
  154.                 $this->_chart->width $this->Width;
  155.  
  156.                 $this->_chart->render();
  157.         }
  158.  
  159.         function serialize()
  160.         {
  161.                 parent::serialize();
  162.  
  163.                 // serialize the chart manually; there is no support for serialization
  164.                 // by the libchart; so it's done externally
  165.                 $owner $this->readOwner();
  166.                 if ($owner != null)
  167.                 {
  168.                         $prefix $owner->readNamePath().".".$this->_name.".Chart.";
  169.  
  170.                         if (is_object($this->_chart->text))
  171.                         {
  172.                                 $_SESSION[$prefix."Text"$this->_chart->text;
  173.                         }
  174.                         if (is_array($this->_chart->point))
  175.                         {
  176.                                 $_SESSION[$prefix."Points"$this->_chart->point;
  177.                         }
  178.  
  179.                         $_SESSION[$prefix."Title"$this->_chart->title;
  180.                         $_SESSION[$prefix."LogoFileName"$this->_chart->logoFileName;
  181.                         $_SESSION[$prefix."Margin"$this->_chart->margin;
  182.                         $_SESSION[$prefix."LowerBound"$this->_chart->lowerBound;
  183.                         $_SESSION[$prefix."LabelMarginLeft"$this->_chart->labelMarginLeft;
  184.                         $_SESSION[$prefix."LabelMarginRight"$this->_chart->labelMarginRight;
  185.                         $_SESSION[$prefix."LabelMarginTop"$this->_chart->labelMarginTop;
  186.                         $_SESSION[$prefix."LabelMarginBottom"$this->_chart->labelMarginBottom;
  187.                 }
  188.         }
  189.  
  190.         function unserialize()
  191.         {
  192.                 parent::unserialize();
  193.  
  194.                 // first manually unserialize the chart
  195.                 $owner $this->readOwner();
  196.                 if ($this->_chart != null && $owner != null)
  197.                 {
  198.                         $prefix $owner->readNamePath().".".$this->_name.".Chart.";
  199.  
  200.                         if (is_object($_SESSION[$prefix."Text"]))
  201.                                 $this->_chart->text $_SESSION[$prefix."Text"];
  202.                         if(is_array($_SESSION[$prefix."Points"]))
  203.                                 $this->_chart->point $_SESSION[$prefix."Points"];
  204.  
  205.                         if (isset($_SESSION[$prefix."Title"]))
  206.                                 $this->_chart->title $_SESSION[$prefix."Title"];
  207.                         if (isset($_SESSION[$prefix."LogoFileName"]))
  208.                                 $this->_chart->logoFileName $_SESSION[$prefix."LogoFileName"];
  209.                         if (isset($_SESSION[$prefix."Margin"]))
  210.                                 $this->_chart->margin $_SESSION[$prefix."Margin"];
  211.                         if (isset($_SESSION[$prefix."LowerBound"]))
  212.                                 $this->_chart->lowerBound $_SESSION[$prefix."LowerBound"];
  213.                         if (isset($_SESSION[$prefix."LabelMarginLeft"]))
  214.                                 $this->_chart->labelMarginLeft $_SESSION[$prefix."LabelMarginLeft"];
  215.                         if (isset($_SESSION[$prefix."LabelMarginRight"]))
  216.                                 $this->_chart->labelMarginRight $_SESSION[$prefix."LabelMarginRight"];
  217.                         if (isset($_SESSION[$prefix."LabelMarginTop"]))
  218.                                 $this->_chart->labelMarginTop $_SESSION[$prefix."LabelMarginTop"];
  219.                         if (isset($_SESSION[$prefix."LabelMarginBottom"]))
  220.                                 $this->_chart->labelMarginBottom $_SESSION[$prefix."LabelMarginBottom"];
  221.                 }
  222.  
  223.                 $key md5($this->owner->Name.$this->Name.$this->Left.$this->Top.$this->Width.$this->Height);
  224.                 $bchart $this->input->bchart;
  225.  
  226.                 // check if the request is for this chart
  227.                 if ((is_object($bchart)) && ($bchart->asString(== $key))
  228.                 {
  229.                         $this->dumpGraphic();
  230.                 }
  231.         }
  232.  
  233.         function dumpContents()
  234.         {
  235.                 if (($this->ControlState csDesigning== csDesigning)
  236.                 {
  237.                         $this->fillDummyValues();
  238.                         $this->dumpGraphic();
  239.                 }
  240.                 else
  241.                 {
  242.                         $events $this->readJsEvents();
  243.                         // add or replace the JS events with the wrappers if necessary
  244.                         $this->addJSWrapperToEvents($events$this->_onclick,    $this->_jsonclick,    "onclick");
  245.  
  246.                         $hint $this->getHintAttribute();
  247.                         $alt htmlspecialchars($this->_chart->title);
  248.                         $style "";
  249.                         if ($this->Style=="")
  250.                         {
  251.                                 // add the cursor to the style
  252.                                 if ($this->_cursor != "")
  253.                                 {
  254.                                         $cr strtolower(substr($this->_cursor2));
  255.                                         $style .= "cursor$cr;";
  256.                                 }
  257.                         }
  258.  
  259.                         $class ($this->Style != """class=\"$this->StyleClass\""";
  260.  
  261.                         if ($style != ""$style "style=\"$style\"";
  262.  
  263.                         if ($this->_onshow != null)
  264.                         {
  265.                                 $this->callEvent('onshow'array());
  266.                         }
  267.  
  268.                         $key md5($this->owner->Name.$this->Name.$this->Left.$this->Top.$this->Width.$this->Height);
  269.                         $url $GLOBALS['PHP_SELF'];
  270.                         // Output an image generated by an URL that request to this script
  271.                         echo "<img src=\"$url?bchart=$key\" width=\"$this->Width\" height=\"$this->Height\" id=\"$this->_name\" name=\"$this->_name\" alt=\"$alt\" $hint $style $class $events />";
  272.  
  273.                         // add a hidden field so we can determine which event for the chart was fired
  274.                         if ($this->_onclick != null)
  275.                         {
  276.                                 $hiddenwrapperfield $this->getJSWrapperHiddenFieldName();
  277.                                 echo "<input type=\"hidden\" id=\"$hiddenwrapperfield\" name=\"$hiddenwrapperfield\" value=\"\" />";
  278.                         }
  279.                 }
  280.         }
  281.  
  282.         /**
  283.         * Write the Javascript section to the header
  284.         */
  285.         function dumpJavascript()
  286.         {
  287.                 parent::dumpJavascript();
  288.  
  289.                 if ($this->_onclick != null && !defined($this->_onclick))
  290.                 {
  291.                         // only output the same function once;
  292.                         // otherwise if for example two buttons use the same
  293.                         // OnClick event handler it would be outputted twice.
  294.                         $def=$this->_onclick;
  295.                         define($def,1);
  296.  
  297.                         // output the wrapper function
  298.                         echo $this->getJSWrapperFunction($this->_onclick);
  299.                 }
  300.         }
  301.  
  302.         /*
  303.         * Publish the events for the component
  304.         */
  305.  
  306.         /**
  307.         * Occurs when the user clicks the control.
  308.         * @return mixed Returns the event handler or null if no handler is set.
  309.         */
  310.         function getOnClick                     (return $this->_onclick}
  311.         /**
  312.         * Occurs when the user clicks the control.
  313.         * @param mixed $value Event handler or null to unset.
  314.         */
  315.         function setOnClick                     ($value$this->_onclick=$value}
  316.         function defaultOnClick                 (return null}
  317.  
  318.  
  319.         /*
  320.         * Publish the JS events for the Chart component
  321.         */
  322.         function getjsOnClick                   (return $this->readjsOnClick()}
  323.         function setjsOnClick                   ($value$this->writejsOnClick($value)}
  324.  
  325.         function getjsOnDblClick                (return $this->readjsOnDblClick()}
  326.         function setjsOnDblClick                ($value$this->writejsOnDblClick($value)}
  327.  
  328.         function getjsOnMouseDown               (return $this->readjsOnMouseDown()}
  329.         function setjsOnMouseDown               ($value$this->writejsOnMouseDown($value)}
  330.  
  331.         function getjsOnMouseUp                 (return $this->readjsOnMouseUp()}
  332.         function setjsOnMouseUp                 ($value$this->writejsOnMouseUp($value)}
  333.  
  334.         function getjsOnMouseOver               (return $this->readjsOnMouseOver()}
  335.         function setjsOnMouseOver               ($value$this->writejsOnMouseOver($value)}
  336.  
  337.         function getjsOnMouseMove               (return $this->readjsOnMouseMove()}
  338.         function setjsOnMouseMove               ($value$this->writejsOnMouseMove($value)}
  339.  
  340.         function getjsOnMouseOut                (return $this->readjsOnMouseOut()}
  341.         function setjsOnMouseOut                ($value$this->writejsOnMouseOut($value)}
  342.  
  343.         function getjsOnKeyPress                (return $this->readjsOnKeyPress()}
  344.         function setjsOnKeyPress                ($value$this->writejsOnKeyPress($value)}
  345.  
  346.         function getjsOnKeyDown                 (return $this->readjsOnKeyDown()}
  347.         function setjsOnKeyDown                 ($value$this->writejsOnKeyDown($value)}
  348.  
  349.         function getjsOnKeyUp                   (return $this->readjsOnKeyUp()}
  350.         function setjsOnKeyUp                   ($value$this->writejsOnKeyUp($value)}
  351.  
  352.  
  353.         /*
  354.         * Publish the properties for the component
  355.         */
  356.  
  357.         /**
  358.         * Chart object
  359.         * Please have a look at the libchart class in order to understand the functionallity
  360.         * of the chart object.
  361.         *
  362.         * @return object 
  363.         */
  364.         function readChart(return $this->_chart}
  365.  
  366.         /**
  367.         * The type of chart to show
  368.         * @return enum (ctHorizontalChart, ctLineChart, ctPieChart, ctVerticalChart)
  369.         */
  370.         function getChartType(return $this->_charttype}
  371.         /**
  372.         * The type of chart to show.
  373.         * Note: Each time the chart type is changed the chart object has to be recreated.
  374.         * @param enum (ctHorizontalChart, ctLineChart, ctPieChart, ctVerticalChart)
  375.         */
  376.         function setChartType($value)
  377.         {
  378.                 if ($value != $this->_charttype)
  379.                 {
  380.                         $this->_charttype $value;
  381.                         // since the chart type changed the chart has to be recreated
  382.                         $this->createChart();
  383.                 }
  384.         }
  385.         function defaultChartType(return ctVerticalChart}
  386.  
  387.         function getHint(return $this->_hint}
  388.         function setHint($value$this->_hint=$value}
  389.  
  390.         function getParentShowHint(return $this->readParentShowHint()}
  391.         function setParentShowHint($value$this->writeParentShowHint($value)}
  392.  
  393.         function getShowHint(return $this->readShowHint()}
  394.         function setShowHint($value$this->writeShowHint($value)}
  395.  
  396.         function getStyle()             return $this->readstyle()}
  397.         function setStyle($value)       $this->writestyle($value)}
  398.  
  399.         function getVisible(return $this->readVisible()}
  400.         function setVisible($value$this->writeVisible($value)}
  401.  
  402. }
  403.  
  404. ?>

Documentation generated on Tue, 27 Mar 2007 13:33:22 +0200 by phpDocumentor 1.3.1