"Fossies" - the Fresh Open Source Software Archive

Member "betterawstats/modules/render_htmlchart.inc.php" (17 Mar 2008, 9785 Bytes) of package /linux/www/old/betterawstats-1.0.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * betterawstats - an alternative display for awstats data
    4  *
    5  * @author      Oliver Spiesshofer, support at betterawstats dot com
    6  * @copyright   2008 Oliver Spiesshofer
    7  * @version     1.0
    8  * @link        http://betterawstats.com
    9 
   10  * Based on the GPL AWStats Totals script by:
   11  * Jeroen de Jong <jeroen@telartis.nl>
   12  * copyright   2004-2006 Telartis
   13  * version 1.13 (http://www.telartis.nl/xcms/awstats)
   14  *
   15  * This program is free software; you can redistribute it and/or
   16  * modify it under the terms of the GNU General Public License
   17  * as published by the Free Software Foundation; either version 2
   18  * of the License, or (at your option) any later version.
   19  *
   20  * This program is distributed in the hope that it will be useful,
   21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   23  * GNU General Public License for more details.
   24  *
   25  * You should have received a copy of the GNU General Public License
   26  * along with this program; if not, write to the Free Software
   27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   28  */
   29 
   30 // this file can't be used on its own
   31 if (strpos ($_SERVER['PHP_SELF'], 'render_htmlchart.inc.php') !== false) {
   32     die ('This file can not be used on its own!');
   33 }
   34 
   35 function baw_render_htmlchart($chart, $format, $get_avg=false, $top_x=false, $dataformat=false) {
   36     baw_debug("rendering chart");
   37     global $BAW_CONF, $BAW_LIB, $BAW_MES;
   38 
   39     // FORMATS -----------------------------------------------------------------
   40     $format_arr = array();
   41     $itemcount = count($chart);
   42 
   43     // we have time data, $top_x has to work from the back instead of the front
   44     $time_data = false;
   45     if ($top_x < 0) {
   46         $time_data = true;
   47         $top_x = abs($top_x);
   48     }
   49     if ($get_avg) {
   50         $top_x++;
   51     }
   52 
   53     $top_x = min($BAW_CONF['max_chart_items'], $top_x);
   54     $top_x_count = $top_x;
   55     // if its not set, use count of items (day, month etc)
   56     if (!$top_x_count) {
   57         $top_x_count = $itemcount;
   58     }
   59     // find the smaller one to know the resulting lines
   60     if (min($top_x_count, $itemcount) > 12) {
   61         $width = 4;
   62     } else {
   63         $width = 6;
   64     }
   65     // iterate formats for header
   66     $text_fields = 0;
   67     foreach ($format as $cell => $attr) {
   68         // iterate one formats attributes
   69         $format_arr[] = $attr['format'];
   70         // we need the number of textfields so we can prepend them to the averages array.
   71         // otherwise the fields for avg and others are not in the right columns
   72         if ($attr['format'] == 'layout_text') {
   73             $text_fields++;
   74         }
   75         // $title_arr[] = $attr['title'];
   76     }
   77     // Max & averages calculation ----------------------------------------------
   78     // the max has to be calculated before so the bar height can be calculated
   79     // the sum has to be known for averages
   80     // iterate all the table and count the sums of all numeric data
   81     $max_arr = array();
   82     $sum_arr = array();
   83     $avg_arr = array();
   84     $others = array();
   85 
   86     $filled_lines = $itemcount; // get the value for substraction
   87     $row_no = 0;
   88     $othercount = 0;
   89     $hasothers = false;
   90     foreach ($chart as $lineid => $row) {
   91         $cell_no = 0;
   92         $rowsum = 0;
   93         $hasothers = false;
   94         foreach ($row as $cell) {
   95             if ($format_arr[$cell_no] != 'layout_text') { // we dont include the text
   96                 // max calculation
   97                 @$max_arr[$cell_no] = max($max_arr[$cell_no], $cell);
   98                 // sum calculation
   99                 @$sum_arr[$cell_no] += $cell;
  100                 // average calculation
  101                 if (is_numeric($cell) && $get_avg) {
  102                     $rowsum += $cell;
  103                     if ($row_no == ($itemcount-1) && $filled_lines >= 1) { // last line, assume sums are done
  104                         if ($rowsum == 0 && $cell_no == 0) { // do once again for last line, dont substract further after first cell
  105                             $filled_lines--;
  106                         }
  107                         $avg_arr[$cell_no] = $sum_arr[$cell_no] / $filled_lines;
  108                     }
  109                 } else if ($get_avg) {
  110                     $avg_arr[$cell_no] = '';
  111                 }
  112                 // others calculation
  113                 if ((!$time_data && $top_x && ($row_no >= $top_x))) {
  114                     // max calculation
  115                     @$max_arr[$cell_no] = max($max_arr[$cell_no], $others[$cell_no]);
  116                     $hasothers = true;
  117                     @$others[$cell_no] += $cell;
  118                 } else if ($time_data && $top_x && (($itemcount - $row_no) >= $top_x)){
  119                     $hasothers = true;
  120                     // for now, we do not accumulate old data for months/days -- optional?
  121                     // risk is that 'others' is so big that it does not make sense
  122                     //@$others[$cell_no] += $cell;
  123                     //@$max_arr[$cell_no] = max($max_arr[$cell_no], $others[$cell_no]);
  124                 }
  125             }
  126             $cell_no ++;
  127         }
  128         // we processes the line, if we are already in the 'others' remove the line
  129         if ($hasothers) {
  130             $othercount ++;
  131             unset($chart[$lineid]);
  132         }
  133         // remove one line from avg-count if data empty
  134         if ($rowsum == 0) {
  135             $filled_lines--;
  136         }
  137         $row_no++; // count to find out if we are ready to do averages
  138     }
  139     // make the array longer to fit, first index is 1 since it comes after the array title
  140     if ($text_fields >= 1) {
  141         $empty_arr = array_pad(array(), $text_fields - 1, '');
  142     } else {
  143         $empty_arr = array();
  144     }
  145     // add others to the table
  146     if ($hasothers && !$time_data) { // we got others, add them to the end
  147         $row_no++;
  148         $others = $empty_arr + array($text_fields - 1 => $BAW_MES[2]) + $others;
  149         $chart += array('layout_others' => $others);
  150     } else if ($hasothers && $time_data) {
  151         $row_no++;
  152         $others = $empty_arr + array($text_fields - 1 => $BAW_MES[2]) +  $others;
  153         //$temp = array('layout_others' => $others);
  154         //$chart = $temp + $empty_arr + $chart;
  155     }
  156     // add averages to the table
  157     if ($get_avg) {
  158         $row_no++;
  159         $avg_arr = $empty_arr + array($text_fields - 1 => $BAW_MES[96]) + $avg_arr;
  160         $chart += array('layout_avg' => $avg_arr);
  161     }
  162 
  163     $out = "\n<table class=\"charttable\">\n";
  164     $out .= "    <tr>\n";
  165     // create the title
  166 
  167     if (isset($format[0]['title']) && $BAW_CONF['chart_titles']) {
  168         $count_str = sprintf($BAW_MES['records'], baw_num_format($itemcount));
  169         $out .="    <tr>\n        <th class=\"header_wrap\" colspan=\"$row_no\">{$format[0]['title']} ($count_str)</th>\n    </tr>\n";
  170     }
  171 
  172     $l = 0;
  173     $fieldcount = 0;
  174     $legend = array();
  175     foreach ($chart as $lineid => $row) {
  176         $class = '';
  177         if (isset($dataformat[$lineid])) {
  178             $class = " {$dataformat[$lineid]}";
  179         } else if ($lineid === 'layout_others' || $lineid === 'layout_avg') {
  180             $class .= " $lineid";
  181         }
  182         $out .= "        <td class=\"chartcell$class\">\n";
  183         $cell_no = 0;
  184         foreach ($row as $cell) {
  185             // only take the numeric values
  186             if ($format_arr[$cell_no] != 'layout_text') {
  187                 $tags = '';
  188                 $function = $BAW_LIB['formats'][$format_arr[$cell_no]]['frm'];
  189                 $txt =  $BAW_LIB['formats'][$format_arr[$cell_no]]['txt'];
  190                 $img =  $BAW_LIB['formats'][$format_arr[$cell_no]]['img'];
  191                 $alt = "{$BAW_MES[$txt]}: ". exec_function($function, $cell);
  192                 $max_string = str_replace('layout_', '', $format[$cell_no]['format']);
  193                 // scale after what?
  194                 $max_key = array_search($BAW_CONF["max_$max_string"], $format_arr);
  195                 if ((isset($max_arr[$max_key])) && ($max_arr[$max_key]>0)) {
  196                     $height = $cell / ($max_arr[$max_key] / 100);
  197                 } else {
  198                     $height = 1;
  199                 }
  200                 if ($height < 1) {
  201                     $height = 1;
  202                 }
  203                 $attr = array(
  204                     'height'=> $height,
  205                     'width' => $width,
  206                     'alt' => $alt,
  207                     'title' => $alt,
  208                     'class' => 'chartimg'
  209                 );
  210                 $out .= baw_create_image($BAW_CONF['icons_url'] . "/other/{$img}", $attr);
  211             } else {
  212                 // write legend
  213                 $fieldcount = max($cell_no, $fieldcount);
  214                 @$legend[$l][$cell_no] ="        <td$tags colspan=\"1\">$cell</td>\n";
  215                 if (isset($legend[$l-1][$cell_no]) && isset($cell)) {
  216                     if (preg_match('#colspan=\\"(\d+)\\">'. $cell.'</td>#', $legend[$l-1][$cell_no], $number)) {
  217                         $number[1]++;
  218                         @$legend[$l][$cell_no] ="        <td$tags colspan=\"{$number[1]}\">$cell</td>\n";
  219                         $legend[$l-1][$cell_no] ="";
  220                     }
  221                 }
  222             }
  223             $cell_no ++;
  224         }
  225         $out .= "\n        </td>\n";
  226         $f = 1;
  227         $l ++;
  228     }
  229 
  230     $out .= "    </tr>\n";
  231     // Display legend
  232     $linecount = $l;
  233     // we inverse the order so the ones standing closer to the data in the table
  234     // are on top here
  235     for ($f=$fieldcount; $f>=0; $f--) {
  236         $out .= "    <tr class=\"chartlegend\">\n";
  237         for ($l=0; $l<$linecount; $l++) {
  238             $out .= $legend[$l][$f];
  239         }
  240         $out .= "    </tr>\n";
  241     }
  242     $out .= "</table>\n";
  243     baw_debug("rendering chart finished");
  244     return $out;
  245 }
  246 
  247 ?>