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

Source for file drivers2.php

Documentation is available at drivers2.php

  1. <?php
  2.     // include the libraries 
  3.     include 'Image/Graph.php';
  4.     include 'Image/Graph/Driver.php';
  5.  
  6.     // create a new PNG driver
  7.     $Driver =Image_Graph_Driver::factory('png',
  8.         array(
  9.             'width' => 600,
  10.             'height' => 400            
  11.         )
  12.     );     
  13.  
  14.     // create the graph
  15.     $Graph =Image_Graph::factory('graph'&$Driver);
  16.  
  17.     // add a new font
  18.     $Font =$Graph->addNew('ttf_font''Gothic');
  19.     $Font->setSize(7);
  20.     
  21.     // use this by default in the graph
  22.     $Graph->setFont($Font);
  23.  
  24.     // create the layout
  25.     $Graph->add(
  26.         Image_Graph::vertical(
  27.             Image_Graph::factory('title'array('Image_Graph Demonstration'12)),
  28.             Image_Graph::vertical(
  29.                 Image_Graph::vertical(
  30.                     $Plotarea_Weather Image_Graph::factory('plotarea'),
  31.                     $Legend_Weather Image_Graph::factory('legend'),
  32.                     85
  33.                 ),
  34.                 Image_Graph::horizontal(
  35.                     Image_Graph::vertical(
  36.                         Image_Graph::vertical(
  37.                             Image_Graph::factory(
  38.                                 'title'
  39.                                 array('Demonstration of Mathematical Functions'10)
  40.                             ),
  41.                             $Plotarea_SinCos Image_Graph::factory('plotarea''axis'),
  42.                             5
  43.                         ),
  44.                         $Legend_SinCos Image_Graph::factory('legend'),
  45.                         90
  46.                     ),
  47.                     $Plotarea_Car Image_Graph::factory('plotarea'),
  48.                     50
  49.                 ),
  50.                 60
  51.             ),
  52.         5)
  53.     );
  54.  
  55.     // setup the legends
  56.     $Legend_Weather->setPlotarea($Plotarea_Weather);
  57.     $Legend_Weather->setFontSize(7);
  58.     
  59.     $Legend_SinCos->setPlotarea($Plotarea_SinCos);
  60.     $Legend_SinCos->setFontSize(8);
  61.  
  62.     // create the grid
  63.     $GridY_Weather =$Plotarea_Weather->addNew(
  64.         'line_grid'
  65.         null
  66.         IMAGE_GRAPH_AXIS_Y
  67.     );
  68.     $GridY_Weather->setLineColor('gray@0.1');
  69.  
  70.     // create a axis marker marker to display min and max temperatures
  71.     $Marker_AverageSpan =
  72.         $Plotarea_Weather->addNew(
  73.             'Image_Graph_Axis_Marker_Area'
  74.             IMAGE_GRAPH_AXIS_Y
  75.         );
  76.     $Marker_AverageSpan->setFillColor('green@0.2');
  77.     $Marker_AverageSpan->setLowerBound(3.8);
  78.     $Marker_AverageSpan->setUpperBound(11.4);
  79.  
  80.     // create a axis marker marker to display average temperatures
  81.     $Marker_Average =
  82.         $Plotarea_Weather->addNew(
  83.             'Image_Graph_Axis_Marker_Line'
  84.             IMAGE_GRAPH_AXIS_Y
  85.         );
  86.     $Marker_Average->setLineColor('blue@0.4');
  87.     $Marker_Average->setValue(7.7);
  88.  
  89.     // create the datasets
  90.     $Dataset_Rainfall =Image_Graph::factory('dataset');
  91.     $Dataset_Rainfall->addPoint('Jan'60);
  92.     $Dataset_Rainfall->addPoint('Feb'41);
  93.     $Dataset_Rainfall->addPoint('Mar'48);
  94.     $Dataset_Rainfall->addPoint('Apr'42);
  95.     $Dataset_Rainfall->addPoint('May'50);
  96.     $Dataset_Rainfall->addPoint('Jun'55);
  97.     $Dataset_Rainfall->addPoint('Jul'67);
  98.     $Dataset_Rainfall->addPoint('Aug'65);
  99.     $Dataset_Rainfall->addPoint('Sep'72);
  100.     $Dataset_Rainfall->addPoint('Oct'77);
  101.     $Dataset_Rainfall->addPoint('Nov'80);
  102.     $Dataset_Rainfall->addPoint('Dec'68);
  103.     
  104.     // add a bar chart 
  105.     $Plot_Rainfall =
  106.         $Plotarea_Weather->addNew(
  107.             'bar'
  108.             &$Dataset_Rainfall
  109.             IMAGE_GRAPH_AXIS_Y_SECONDARY
  110.         );
  111.     $Plot_Rainfall->setLineColor('gray');
  112.     $Plot_Rainfall->setFillColor('yellow@0.1');
  113.     $Plot_Rainfall->setTitle('Average rainfall');
  114.     
  115.     // some more data
  116.     $Dataset_TempAvg =Image_Graph::factory('dataset');
  117.     $Dataset_TempAvg->addPoint('Jan'0.2);
  118.     $Dataset_TempAvg->addPoint('Feb'0.1);
  119.     $Dataset_TempAvg->addPoint('Mar'2.3);
  120.     $Dataset_TempAvg->addPoint('Apr'5.8);
  121.     $Dataset_TempAvg->addPoint('May'10.8);
  122.     $Dataset_TempAvg->addPoint('Jun'14.1);
  123.     $Dataset_TempAvg->addPoint('Jul'16.2);
  124.     $Dataset_TempAvg->addPoint('Aug'15.9);
  125.     $Dataset_TempAvg->addPoint('Sep'12.1);
  126.     $Dataset_TempAvg->addPoint('Oct'8.7);
  127.     $Dataset_TempAvg->addPoint('Nov'4.4);
  128.     $Dataset_TempAvg->addPoint('Dec'1.8);
  129.     
  130.     // create the plot
  131.     $Plot_TempAvg =
  132.         $Plotarea_Weather->addNew(
  133.             'Image_Graph_Plot_Smoothed_Line'
  134.             &$Dataset_TempAvg
  135.         );
  136.     $Plot_TempAvg->setLineColor('blue');
  137.     $Plot_TempAvg->setTitle('Average temperature');
  138.     
  139.     // some more data
  140.     $Dataset_TempMin =Image_Graph::factory('dataset');
  141.     $Dataset_TempMin->addPoint('Jan'-2.7);
  142.     $Dataset_TempMin->addPoint('Feb'-2.8);
  143.     $Dataset_TempMin->addPoint('Mar'-0.9);
  144.     $Dataset_TempMin->addPoint('Apr'1.2);
  145.     $Dataset_TempMin->addPoint('May'5.5);
  146.     $Dataset_TempMin->addPoint('Jun'9.2);
  147.     $Dataset_TempMin->addPoint('Jul'11.3);
  148.     $Dataset_TempMin->addPoint('Aug'11.1);
  149.     $Dataset_TempMin->addPoint('Sep'7.8);
  150.     $Dataset_TempMin->addPoint('Oct'5.0);
  151.     $Dataset_TempMin->addPoint('Nov'1.5);
  152.     $Dataset_TempMin->addPoint('Dec'-0.9);
  153.  
  154.     // create the plot
  155.     $Plot_TempMin =
  156.         $Plotarea_Weather->addNew(
  157.             'Image_Graph_Plot_Smoothed_Line'
  158.             &$Dataset_TempMin
  159.         );
  160.     $Plot_TempMin->setLineColor('teal');
  161.     $Plot_TempMin->setTitle('Minimum temperature');
  162.  
  163.     // some more data
  164.     $Dataset_TempMax =Image_Graph::factory('dataset');
  165.     $Dataset_TempMax->addPoint('Jan'2.4);
  166.     $Dataset_TempMax->addPoint('Feb'2.5);
  167.     $Dataset_TempMax->addPoint('Mar'5.4);
  168.     $Dataset_TempMax->addPoint('Apr'10.5);
  169.     $Dataset_TempMax->addPoint('May'15.8);
  170.     $Dataset_TempMax->addPoint('Jun'18.9);
  171.     $Dataset_TempMax->addPoint('Jul'21.2);
  172.     $Dataset_TempMax->addPoint('Aug'20.8);
  173.     $Dataset_TempMax->addPoint('Sep'16.3);
  174.     $Dataset_TempMax->addPoint('Oct'11.8);
  175.     $Dataset_TempMax->addPoint('Nov'6.9);
  176.     $Dataset_TempMax->addPoint('Dec'4.1);
  177.  
  178.     // create the plot
  179.     $Plot_TempMax =
  180.         $Plotarea_Weather->addNew(
  181.             'Image_Graph_Plot_Smoothed_Line'
  182.             &$Dataset_TempMax
  183.         );
  184.     $Plot_TempMax->setLineColor('red');
  185.     $Plot_TempMax->setTitle('Maximum temperature');   
  186.         
  187.     // create a datapreprocessor to display milimeter unit
  188.     $DataPreprocessor_MM =
  189.         Image_Graph::factory('Image_Graph_DataPreprocessor_Formatted''%d mm');
  190.     // create a datapreprocessor to display degrees centigrade unit
  191.     $DataPreprocessor_DegC =
  192.         Image_Graph::factory('Image_Graph_DataPreprocessor_Formatted''%d C');
  193.     
  194.     // create a value marker for the rainfall chart
  195.     $Marker_Rainfall =
  196.         $Plot_Rainfall->addNew('Image_Graph_Marker_Value'IMAGE_GRAPH_VALUE_Y);    
  197.     $Marker_Rainfall->setDataPreprocessor($DataPreprocessor_MM);
  198.     $Marker_Rainfall->setFontSize(7);
  199.     $PointingMarker_Rainfall =
  200.         $Plot_Rainfall->addNew(
  201.             'Image_Graph_Marker_Pointing_Angular'
  202.             array(20&$Marker_Rainfall)
  203.         );
  204.     $Plot_Rainfall->setMarker($PointingMarker_Rainfall);      
  205.     
  206.     // setup the primary y axis
  207.     $AxisY_Weather =$Plotarea_Weather->getAxis(IMAGE_GRAPH_AXIS_Y);
  208.     $AxisY_Weather->showLabel(IMAGE_GRAPH_LABEL_ZERO);
  209.     $AxisY_Weather->setDataPreprocessor($DataPreprocessor_DegC);
  210.     $AxisY_Weather->setTitle(
  211.         'Temperature'
  212.         array('vertical' => true'angle' => 90)
  213.     );
  214.  
  215.     // setup the secondary y axis
  216.     $AxisYsecondary_Weather =
  217.         $Plotarea_Weather->getAxis(IMAGE_GRAPH_AXIS_Y_SECONDARY);
  218.     $AxisYsecondary_Weather->setDataPreprocessor($DataPreprocessor_MM);
  219.     $AxisYsecondary_Weather->setTitle(
  220.         'Rainfall'
  221.         array('vertical' => true'angle' => 270)
  222.     );
  223.         
  224.     // setup grid for mathematical chart (sin, cos)
  225.     $GridX_SinCos =
  226.         $Plotarea_SinCos->addNew('line_grid'nullIMAGE_GRAPH_AXIS_X);
  227.     $GridY_SinCos =
  228.         $Plotarea_SinCos->addNew('line_grid'nullIMAGE_GRAPH_AXIS_Y);
  229.  
  230.     // create the datasets
  231.     $Dataset_Sin =
  232.         Image_Graph::factory(
  233.             'Image_Graph_Dataset_Function'
  234.             array(-2*pi()2*pi()'sin'50)
  235.         );
  236.     $Dataset_Cos =
  237.         Image_Graph::factory(
  238.             'Image_Graph_Dataset_Function'
  239.             array(-2*pi()2*pi()'cos'50)
  240.         );
  241.  
  242.     // create and setup plots
  243.     $Plot_Sin =$Plotarea_SinCos->addNew('line'$Dataset_Sin);
  244.     $Plot_Cos =$Plotarea_SinCos->addNew('line'$Dataset_Cos);
  245.     $Plot_Sin->setLineColor('red');
  246.     $Plot_Cos->setLineColor('blue');
  247.     $Plot_Sin->setTitle('sin(x)');
  248.     $Plot_Cos->setTitle('cos(x)');
  249.     
  250.     // configure axis
  251.     $AxisX_SinCos =$Plotarea_SinCos->getAxis(IMAGE_GRAPH_AXIS_X);
  252.     $AxisX_SinCos->setLabelInterval(array(-6-4-2246));
  253.     $AxisY_SinCos =$Plotarea_SinCos->getAxis(IMAGE_GRAPH_AXIS_Y);
  254.     $AxisY_SinCos->forceMinimum(-1.1);
  255.     $AxisY_SinCos->forceMaximum(1.1);
  256.     $AxisY_SinCos->setLabelInterval(array(-1-0.50.51));
  257.  
  258.     // a user defined callback function used in preprocessor
  259.     function carLabel($value{
  260.         return 2000+$value;
  261.     }
  262.  
  263.     // create x-axis label preprocessor for 'car' chart
  264.     $DataPreprocessor_Car =
  265.         Image_Graph::factory('Image_Graph_DataPreprocessor_Function''carLabel');
  266.  
  267.     // add grids
  268.     $GridX_Car =$Plotarea_Car->addNew('line_grid'nullIMAGE_GRAPH_AXIS_X);
  269.     $GridY_Car =$Plotarea_Car->addNew('line_grid'nullIMAGE_GRAPH_AXIS_Y);
  270.     $GridX_Car->setLineColor('gray@0.2');
  271.     $GridY_Car->setLineColor('gray@0.2');
  272.     
  273.     // setup dataset (random)
  274.     $Dataset_Car =Image_Graph::factory('random'array(1010100true));
  275.     
  276.     // create the car filling
  277.     $Fill_Car =
  278.         Image_Graph::factory('Image_Graph_Fill_Image''./images/audi-tt-coupe.png');                
  279.     $Plotarea_Car->setFillStyle($Fill_Car);
  280.  
  281.     // create the plot
  282.     $Plot_Car =
  283.         $Plotarea_Car->addNew('Image_Graph_Plot_Smoothed_Area'$Dataset_Car);
  284.     $Plot_Car->setLineColor('gray');
  285.     $Plot_Car->setFillColor('white@0.7');
  286.  
  287.     // setup axis
  288.     $AxisX_Car =$Plotarea_Car->getAxis(IMAGE_GRAPH_AXIS_X);
  289.     $AxisX_Car->setDataPreprocessor($DataPreprocessor_Car);
  290.     $AxisX_Car->setFontSize(6);
  291.     $AxisY_Car =$Plotarea_Car->getAxis(IMAGE_GRAPH_AXIS_Y);
  292.     $AxisY_Car->forceMaximum(100);
  293.         
  294.     // output the graph using the GD driver
  295.     $Graph->done(array('filename' => './frontpage_sample.png'));
  296.  
  297.     // create a new SVG driver
  298.     $Driver =Image_Graph_Driver::factory('svg',
  299.         array(
  300.             'width' => 600,
  301.             'height' => 400
  302.         )
  303.     )
  304.     // make the graph use this now instead
  305.     $Graph->setDriver($Driver);
  306.     
  307.     // 're'-output the graph, but not using the SVG driver
  308.     $Graph->done(array('filename' => './frontpage_sample.svg'));
  309.  
  310.     // create a new PDF driver
  311.     $Driver =Image_Graph_Driver::factory('pdflib',
  312.         array(
  313.             'page' => 'A4',
  314.             'align' => 'center',
  315.             'orientation' => 'landscape',           
  316.             'width' => 600,
  317.             'height' => 400
  318.         )
  319.     )
  320.     // make the graph use this now instead
  321.     $Graph->setDriver($Driver);
  322.     
  323.     // 're'-output the graph, but not using the PDF driver
  324.     $Graph->done(array('filename' => './frontpage_sample.pdf'));
  325.  
  326.     // create a new SWF driver
  327.     $Driver =Image_Graph_Driver::factory('swf',
  328.         array(
  329.             'width' => 600,
  330.             'height' => 400
  331.         )
  332.     )
  333.     // make the graph use this now instead
  334.     $Graph->setDriver($Driver);
  335.     
  336.     // 're'-output the graph, but not using the SWF driver
  337.     $Graph->done(array('filename' => './frontpage_sample.swf'));
  338.  
  339. ?>

Documentation generated on Mon, 11 Mar 2019 14:21:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.