Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
<?php
2
/*=======================================================================
3
// File: 	JPGRAPH_LOG.PHP
4
// Description:	Log scale plot extension for JpGraph
5
// Created: 	2001-01-08
6
// Ver:		$Id: jpgraph_log.php 781 2006-10-08 08:07:47Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
*/
11
 
12
 
13
DEFINE('LOGLABELS_PLAIN',0);
14
DEFINE('LOGLABELS_MAGNITUDE',1);
15
 
16
//===================================================
17
// CLASS LogScale
18
// Description: Logarithmic scale between world and screen
19
//===================================================
20
class LogScale extends LinearScale {
21
//---------------
22
// CONSTRUCTOR
23
 
24
    // Log scale is specified using the log of min and max
25
    function LogScale($min,$max,$type="y") {
26
	$this->LinearScale($min,$max,$type);
27
	$this->ticks = new LogTicks();
28
	$this->name = 'log';
29
    }
30
 
31
//----------------
32
// PUBLIC METHODS
33
 
34
    // Translate between world and screen
35
    function Translate($a) {
36
	if( !is_numeric($a) ) {
37
	    if( $a != '' && $a != '-' && $a != 'x' )
38
		JpGraphError::RaiseL(11001);
39
//('Your data contains non-numeric values.');
40
	    return 1;
41
	}
42
	if( $a < 0 ) {
43
	    JpGraphError::RaiseL(11002);
44
//("Negative data values can not be used in a log scale.");
45
	    exit(1);
46
	}
47
	if( $a==0 ) $a=1;
48
	$a=log10($a);
49
	return ceil($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor);
50
    }
51
 
52
    // Relative translate (don't include offset) usefull when we just want
53
    // to know the relative position (in pixels) on the axis
54
    function RelTranslate($a) {
55
	if( !is_numeric($a) ) {
56
	    if( $a != '' && $a != '-' && $a != 'x' )
57
		JpGraphError::RaiseL(11001);
58
//('Your data contains non-numeric values.');
59
	    return 1;
60
	}
61
	if( $a==0 ) $a=1;
62
	$a=log10($a);
63
	return round(($a*1.0 - $this->scale[0]) * $this->scale_factor);
64
    }
65
 
66
    // Use bcpow() for increased precision
67
    function GetMinVal() {
68
	if( function_exists("bcpow") )
69
	    return round(bcpow(10,$this->scale[0],15),14);
70
	else
71
	    return round(pow(10,$this->scale[0]),14);
72
    }
73
 
74
    function GetMaxVal() {
75
	if( function_exists("bcpow") )
76
	    return round(bcpow(10,$this->scale[1],15),14);
77
	else
78
	    return round(pow(10,$this->scale[1]),14);
79
    }
80
 
81
    // Logarithmic autoscaling is much simplier since we just
82
    // set the min and max to logs of the min and max values.
83
    // Note that for log autoscale the "maxstep" the fourth argument
84
    // isn't used. This is just included to give the method the same
85
    // signature as the linear counterpart.
86
    function AutoScale($img,$min,$max,$maxsteps,$majend=true) {
87
	if( $min==0 ) $min=1;
88
 
89
	if( $max <= 0 ) {
90
	    JpGraphError::RaiseL(11004);
91
//('Scale error for logarithmic scale. You have a problem with your data values. The max value must be greater than 0. It is mathematically impossible to have 0 in a logarithmic scale.');
92
	}
93
	$smin = floor(log10($min));
94
	$smax = ceil(log10($max));
95
	$this->Update($img,$smin,$smax);
96
    }
97
//---------------
98
// PRIVATE METHODS
99
} // Class
100
 
101
//===================================================
102
// CLASS LogTicks
103
// Description:
104
//===================================================
105
class LogTicks extends Ticks{
106
    private $label_logtype=LOGLABELS_MAGNITUDE;
107
//---------------
108
// CONSTRUCTOR
109
    function LogTicks() {
110
    }
111
//---------------
112
// PUBLIC METHODS
113
    function IsSpecified() {
114
	return true;
115
    }
116
 
117
    function SetLabelLogType($aType) {
118
	$this->label_logtype = $aType;
119
    }
120
 
121
    // For log scale it's meaningless to speak about a major step
122
    // We just return -1 to make the framework happy (specifically
123
    // StrokeLabels() )
124
    function GetMajor() {
125
	return -1;
126
    }
127
 
128
    function SetTextLabelStart($aStart) {
129
	JpGraphError::RaiseL(11005);
130
//('Specifying tick interval for a logarithmic scale is undefined. Remove any calls to SetTextLabelStart() or SetTextTickInterval() on the logarithmic scale.');
131
    }
132
 
133
    function SetXLabelOffset($dummy) {
134
	// For log scales we dont care about XLabel offset
135
    }
136
 
137
    // Draw ticks on image "img" using scale "scale". The axis absolute
138
    // position in the image is specified in pos, i.e. for an x-axis
139
    // it specifies the absolute y-coord and for Y-ticks it specified the
140
    // absolute x-position.
141
    function Stroke($img,$scale,$pos) {
142
	$start = $scale->GetMinVal();
143
	$limit = $scale->GetMaxVal();
144
	$nextMajor = 10*$start;
145
	$step = $nextMajor / 10.0;
146
 
147
 
148
	$img->SetLineWeight($this->weight);
149
 
150
	if( $scale->type == "y" ) {
151
	    // member direction specified if the ticks should be on
152
	    // left or right side.
153
	    $a=$pos + $this->direction*$this->GetMinTickAbsSize();
154
	    $a2=$pos + $this->direction*$this->GetMajTickAbsSize();
155
 
156
	    $count=1;
157
	    $this->maj_ticks_pos[0]=$scale->Translate($start);
158
	    $this->maj_ticklabels_pos[0]=$scale->Translate($start);
159
	    if( $this->supress_first )
160
		$this->maj_ticks_label[0]="";
161
	    else {
162
		if( $this->label_formfunc != '' ) {
163
		    $f = $this->label_formfunc;
164
		    $this->maj_ticks_label[0]=call_user_func($f,$start);
165
		}
166
		elseif( $this->label_logtype == LOGLABELS_PLAIN )
167
		    $this->maj_ticks_label[0]=$start;
168
		else
169
		    $this->maj_ticks_label[0]='10^'.round(log10($start));
170
	    }
171
	    $i=1;
172
	    for($y=$start; $y<=$limit; $y+=$step,++$count  ) {
173
		$ys=$scale->Translate($y);
174
		$this->ticks_pos[]=$ys;
175
		$this->ticklabels_pos[]=$ys;
176
		if( $count % 10 == 0 ) {
177
		    if( !$this->supress_tickmarks ) {
178
			if( $this->majcolor!="" ) {
179
			    $img->PushColor($this->majcolor);
180
			    $img->Line($pos,$ys,$a2,$ys);
181
			    $img->PopColor();
182
			}
183
			else
184
			    $img->Line($pos,$ys,$a2,$ys);
185
		    }
186
 
187
		    $this->maj_ticks_pos[$i]=$ys;
188
		    $this->maj_ticklabels_pos[$i]=$ys;
189
 
190
		    if( $this->label_formfunc != '' ) {
191
			$f = $this->label_formfunc;
192
			$this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
193
		    }
194
		    elseif( $this->label_logtype == 0 )
195
			$this->maj_ticks_label[$i]=$nextMajor;
196
		    else
197
			$this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
198
		    ++$i;
199
		    $nextMajor *= 10;
200
		    $step *= 10;
201
		    $count=1;
202
		}
203
		else {
204
		    if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
205
			if( $this->mincolor!="" ) $img->PushColor($this->mincolor);
206
			$img->Line($pos,$ys,$a,$ys);
207
			if( $this->mincolor!="" ) $img->PopColor();
208
		    }
209
		}
210
	    }
211
	}
212
	else {
213
	    $a=$pos - $this->direction*$this->GetMinTickAbsSize();
214
	    $a2=$pos - $this->direction*$this->GetMajTickAbsSize();
215
	    $count=1;
216
	    $this->maj_ticks_pos[0]=$scale->Translate($start);
217
	    $this->maj_ticklabels_pos[0]=$scale->Translate($start);
218
	    if( $this->supress_first )
219
		$this->maj_ticks_label[0]="";
220
	    else {
221
		if( $this->label_formfunc != '' ) {
222
		    $f = $this->label_formfunc;
223
		    $this->maj_ticks_label[0]=call_user_func($f,$start);
224
		}
225
		elseif( $this->label_logtype == 0 )
226
		    $this->maj_ticks_label[0]=$start;
227
		else
228
		    $this->maj_ticks_label[0]='10^'.round(log10($start));
229
	    }
230
	    $i=1;
231
	    for($x=$start; $x<=$limit; $x+=$step,++$count  ) {
232
		$xs=$scale->Translate($x);
233
		$this->ticks_pos[]=$xs;
234
		$this->ticklabels_pos[]=$xs;
235
		if( $count % 10 == 0 ) {
236
		    if( !$this->supress_tickmarks ) {
237
			$img->Line($xs,$pos,$xs,$a2);
238
		    }
239
		    $this->maj_ticks_pos[$i]=$xs;
240
		    $this->maj_ticklabels_pos[$i]=$xs;
241
 
242
		    if( $this->label_formfunc != '' ) {
243
			$f = $this->label_formfunc;
244
			$this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
245
		    }
246
		    elseif( $this->label_logtype == 0 )
247
			$this->maj_ticks_label[$i]=$nextMajor;
248
		    else
249
			$this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
250
		    ++$i;
251
		    $nextMajor *= 10;
252
		    $step *= 10;
253
		    $count=1;
254
		}
255
		else {
256
		    if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
257
			$img->Line($xs,$pos,$xs,$a);
258
		    }
259
		}
260
	    }
261
	}
262
	return true;
263
    }
264
} // Class
265
/* EOF */
266
?>