Charts Class that i wrote before 7 years – just found on phpclasses.org – PHP 4

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , , , . Bookmark the Permalink. Post a Comment. Leave a Trackback URL.

Hey….
Just found one of my first classes that i wrote, before 7 years and still popular on phpclasses.org

grchart.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
    GrCharts 0.1 pre-alpha.
    ---------------------------------------------------------------
    Under GNU/GPL.
    ---------------------------------------------------------------
    Change log:
    ---------------------------------------------------------------
    12/10/2004 - Added new functions (Style(); and chartsarray();)
    6/10/2004 - First Version: 0.1 pre-alpha
    ---------------------------------------------------------------
    Build by Garry Lachman.
*/

class grchart {
    var $last_id;
    var $total;
    var $precent_array;
    var $chart_array;
    var $style;
    var $log;
   
    function start($value) {
        $this->total = $value;
        $this->log .= "GrChart started with total: $value<br>";
        $this->last_id = "0";
        $this->log .= "Last ID set to \"0\"<br>";
    }
   
    function ch_style($value) {
        $this->style = $value;
        $this->log .="Style set to $value<br>";
    }
   
    function add_chart($value) {
        $current_id = $this->last_id + 1;
        $total = $this->total;
        $precent = ($value/$total) * 100;
        $this->precent_array[$current_id] = $precent;
        $this->last_id++;
       
        $this->log .= "---------------------------------------------<br>";
        $this->log .= "Added chart : <br>";
        $this->log .= "Value: $value <br>";
        $this->log .= "Precents: $precent%<br>";
        $this->log .= "Last ID set to \"$current_id\"<br>";
        $this->log .= "---------------------------------------------<br>";
    }
   
    function init() {
        $last_id = $this->last_id;
        $style = $this->style;
        $i = 1;
        $x = 0;
        while($i <= $last_id) {
            $work_id = $i;
            $work_precent = $this->precent_array[$work_id];
            while($x <= $work_precent) {
                $buffer = $buffer . "|";
                $x++;
            }
            $this->chart_array[$work_id] = $buffer;
            $buffer = "";
           
            $this->log .= "Created HTML code for chart no` $work_id<br>";
        $i++;
        $x=0;
        }
       
        $this->log .= "\n Init complite, all HTML code created <br>";
    }
   
    function style($value) {
        $style = $this->style;
        $chart = $this->chart_array[$value];
        $precent = $this->precent_array[$value];
        switch($style) {
            case red:
                $chart = str_replace("|", "<img src='images/red.gif'>", $chart);
                break;
               
            case green:
                $chart = str_replace("|", "<img src='images/green.gif'>", $chart);
                break;
               
            case pre:
                $chart = str_replace("|", " ", $chart);
                $chart .= "  (" . number_format($precent, 2, '.', '') . "%)";
                break;
        }
        return $chart;
       
    }
   
    function chart($value) {
        $chart = $this->style($value);
        return $chart;
    }
   
    function chartsarray() {
        $last_id = $this->last_id;
        $i = 0;
        $value = 0;
       
        while($i <= $last_id) {
            $style = $this->style;
            $chart = $this->chart_array[$value];
            $precent = $this->precent_array[$value];
            if ($chart <> "") {
                $chart = $this->style($value);
            $chartsarray[$value] = $chart;    
            }
            $value++;
            $i++;
        }    
    return $chartsarray;
    }
   
    function echo_log() {
        return $this->log;
    }

}
?>

Lets test it…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
include("grchart.class.php");
$chart = new grchart;

$chart->start(100);

$chart->add_chart("20");
$chart->add_chart("60");
$chart->add_chart("10");
$chart->add_chart("10");

$chart->init();

?>
<table width="100%" border="1">
<TR>
<TD>Red Style</TD>
<?
$chart->ch_style("red");
?>
</TR>
<TR>
<td><? print $chart->chart(1) ?></td>
</TR>
<TR>
<td><? print $chart->chart(2) ?></td>
</TR>
<TR>
<td><? print $chart->chart(3) ?></td>
</TR>
<TR>
<td><? print $chart->chart(4) ?></td>
</TR>
</table>

<br>

<table width="100%" border="1">
<TR>
<TD>Green Style</TD>
<?
$chart->ch_style("green");
?>
</TR>
<TR>
<td><? print $chart->chart(1) ?></td>
</TR>
<TR>
<td><? print $chart->chart(2) ?></td>
</TR>
<TR>
<td><? print $chart->chart(3) ?></td>
</TR>
<TR>
<td><? print $chart->chart(4) ?></td>
</TR>
</table>

<br>

<table width="100%" border="1">
<TR>
<TD>Pre Style</TD>
<?
$chart->ch_style("pre");
?>
</TR>
<TR>
<td><? print $chart->chart(1) ?></td>
</TR>
<TR>
<td><? print $chart->chart(2) ?></td>
</TR>
<TR>
<td><? print $chart->chart(3) ?></td>
</TR>
<TR>
<td><? print $chart->chart(4) ?></td>
</TR>
</table>
<br>
<?
$chart->ch_style("green");
$ca = $chart->chartsarray();
print $ca[1] . "<br>". $ca[2];
?>

Enjot it ;)
The project on phpclasses.php
http://www.phpclasses.org/package/1934-PHP-Generate-share-bar-charts-in-HTML.html

Have a nice day ;)
Garry Lachman

Share

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*