2 /*=======================================================================
3 // File: JPGRAPH_LOG.PHP
4 // Description: Log scale plot extension for JpGraph
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_log.php,v 1.15.2.1 2003/08/15 15:27:40 aditus Exp $
9 // License: This code is released under QPL
10 // Copyright (C) 2001,2002 Johan Persson
11 //========================================================================
15 DEFINE('LOGLABELS_PLAIN',0);
16 DEFINE('LOGLABELS_MAGNITUDE',1);
18 //===================================================
20 // Description: Logarithmic scale between world and screen
21 //===================================================
22 class LogScale extends LinearScale {
26 // Log scale is specified using the log of min and max
27 function LogScale($min,$max,$type="y") {
28 $this->LinearScale($min,$max,$type);
29 $this->ticks = new LogTicks();
36 // Translate between world and screen
37 function Translate($a) {
39 JpGraphError::Raise("Negative data values can not be used in a log scale.");
44 return ceil($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor);
47 // Relative translate (don't include offset) usefull when we just want
48 // to know the relative position (in pixels) on the axis
49 function RelTranslate($a) {
52 return round(($a*1.0 - $this->scale[0]) * $this->scale_factor);
55 // Use bcpow() for increased precision
56 function GetMinVal() {
57 if( function_exists("bcpow") )
58 return round(bcpow(10,$this->scale[0],15),14);
60 return round(pow(10,$this->scale[0]));
63 function GetMaxVal() {
64 if( function_exists("bcpow") )
65 return round(bcpow(10,$this->scale[1],15),14);
67 return round(pow(10,$this->scale[1]));
70 // Logarithmic autoscaling is much simplier since we just
71 // set the min and max to logs of the min and max values.
72 // Note that for log autoscale the "maxstep" the fourth argument
73 // isn't used. This is just included to give the method the same
74 // signature as the linear counterpart.
75 function AutoScale(&$img,$min,$max,$dummy) {
78 $smin = floor(log10($min));
79 $smax = ceil(log10($max));
80 $this->Update($img,$smin,$smax);
86 //===================================================
89 //===================================================
90 class LogTicks extends Ticks{
91 var $label_logtype=LOGLABELS_MAGNITUDE;
98 function IsSpecified() {
102 function SetLabelLogType($aType) {
103 $this->label_logtype = $aType;
106 // For log scale it's meaningless to speak about a major step
107 // We just return -1 to make the framework happy (specifically
109 function GetMajor() {
113 function SetTextLabelStart($aStart) {
114 JpGraphError::Raise('Specifying tick interval for a logarithmic scale is undefined. Remove any calls to SetTextLabelStart() or SetTextTickInterval() on the logarithmic scale.');
117 function SetXLabelOffset($dummy) {
118 // For log scales we dont care about XLabel offset
121 // Draw ticks on image "img" using scale "scale". The axis absolute
122 // position in the image is specified in pos, i.e. for an x-axis
123 // it specifies the absolute y-coord and for Y-ticks it specified the
124 // absolute x-position.
125 function Stroke(&$img,&$scale,$pos) {
126 $start = $scale->GetMinVal();
127 $limit = $scale->GetMaxVal();
128 $nextMajor = 10*$start;
129 $step = $nextMajor / 10.0;
132 $img->SetLineWeight($this->weight);
134 if( $scale->type == "y" ) {
135 // member direction specified if the ticks should be on
136 // left or right side.
137 $a=$pos + $this->direction*$this->GetMinTickAbsSize();
138 $a2=$pos + $this->direction*$this->GetMajTickAbsSize();
141 $this->maj_ticks_pos[0]=$scale->Translate($start);
142 $this->maj_ticklabels_pos[0]=$scale->Translate($start);
143 if( $this->supress_first )
144 $this->maj_ticks_label[0]="";
146 if( $this->label_formfunc != '' ) {
147 $f = $this->label_formfunc;
148 $this->maj_ticks_label[0]=call_user_func($f,$start);
150 elseif( $this->label_logtype == LOGLABELS_PLAIN )
151 $this->maj_ticks_label[0]=$start;
153 $this->maj_ticks_label[0]='10^'.round(log10($start));
156 for($y=$start; $y<=$limit; $y+=$step,++$count ) {
157 $ys=$scale->Translate($y);
158 $this->ticks_pos[]=$ys;
159 $this->ticklabels_pos[]=$ys;
160 if( $count % 10 == 0 ) {
161 if( $this->majcolor!="" ) {
162 $img->PushColor($this->majcolor);
163 $img->Line($pos,$ys,$a2,$ys);
167 $img->Line($pos,$ys,$a2,$ys);
169 $this->maj_ticks_pos[$i]=$ys;
170 $this->maj_ticklabels_pos[$i]=$ys;
172 if( $this->label_formfunc != '' ) {
173 $f = $this->label_formfunc;
174 $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
176 elseif( $this->label_logtype == 0 )
177 $this->maj_ticks_label[$i]=$nextMajor;
179 $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
186 if( $this->mincolor!="" ) $img->PushColor($this->mincolor);
187 $img->Line($pos,$ys,$a,$ys);
188 if( $this->mincolor!="" ) $img->PopCOlor();
193 $a=$pos - $this->direction*$this->GetMinTickAbsSize();
194 $a2=$pos - $this->direction*$this->GetMajTickAbsSize();
196 $this->maj_ticks_pos[0]=$scale->Translate($start);
197 $this->maj_ticklabels_pos[0]=$scale->Translate($start);
198 if( $this->supress_first )
199 $this->maj_ticks_label[0]="";
201 if( $this->label_formfunc != '' ) {
202 $f = $this->label_formfunc;
203 $this->maj_ticks_label[0]=call_user_func($f,$start);
205 elseif( $this->label_logtype == 0 )
206 $this->maj_ticks_label[0]=$start;
208 $this->maj_ticks_label[0]='10^'.round(log10($start));
211 for($x=$start; $x<=$limit; $x+=$step,++$count ) {
212 $xs=$scale->Translate($x);
213 $this->ticks_pos[]=$xs;
214 $this->ticklabels_pos[]=$xs;
215 if( $count % 10 == 0 ) {
216 $img->Line($xs,$pos,$xs,$a2);
217 $this->maj_ticks_pos[$i]=$xs;
218 $this->maj_ticklabels_pos[$i]=$xs;
220 if( $this->label_formfunc != '' ) {
221 $f = $this->label_formfunc;
222 $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);
224 elseif( $this->label_logtype == 0 )
225 $this->maj_ticks_label[$i]=$nextMajor;
227 $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
234 $img->Line($xs,$pos,$xs,$a);