lib/Scaler.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  *
00004  *  Copyright (C) 2001-2007 Stefan S. Stratigakos
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019  *  USA.
00020  */
00021 
00022 #include "Scaler.h"
00023 #include <math.h>
00024 
00025 Scaler::Scaler ()
00026 {
00027   height = 0;
00028   logScale = 0;
00029   scaleHigh = 0;
00030   scaleLow = 0;
00031   logScaleHigh = 0;
00032   logRange = 0;
00033   range = 0;
00034   scaler = 0;
00035 
00036   scaleList.append(".00001");
00037   scaleList.append(".00002");
00038   scaleList.append(".00005");
00039   scaleList.append(".0001");
00040   scaleList.append(".0002");
00041   scaleList.append(".0005");
00042   scaleList.append(".001");
00043   scaleList.append(".002");
00044   scaleList.append(".005");
00045   scaleList.append(".01");
00046   scaleList.append(".02");
00047   scaleList.append(".05");
00048   scaleList.append(".1");
00049   scaleList.append(".2");
00050   scaleList.append(".5");
00051   scaleList.append("1");
00052   scaleList.append("2");
00053   scaleList.append("5");
00054   scaleList.append("10");
00055   scaleList.append("25");
00056   scaleList.append("50");
00057   scaleList.append("100");
00058   scaleList.append("250");
00059   scaleList.append("500");
00060   scaleList.append("1000");
00061   scaleList.append("2500");
00062   scaleList.append("5000");
00063   scaleList.append("10000");
00064   scaleList.append("25000");
00065   scaleList.append("50000");
00066   scaleList.append("100000");
00067   scaleList.append("250000");
00068   scaleList.append("500000");
00069   scaleList.append("1000000");
00070   scaleList.append("2500000");
00071   scaleList.append("5000000");
00072   scaleList.append("10000000");
00073   scaleList.append("25000000");
00074   scaleList.append("50000000");
00075   scaleList.append("100000000");
00076   scaleList.append("250000000");
00077   scaleList.append("500000000");
00078   scaleList.append("1000000000");
00079   scaleList.append("2500000000");
00080   scaleList.append("5000000000");
00081   scaleList.append("10000000000");
00082   scaleList.append("25000000000");
00083   scaleList.append("50000000000");
00084   scaleList.append("100000000000");
00085   scaleList.append("250000000000");
00086   scaleList.append("500000000000");
00087 }
00088 
00089 Scaler::~Scaler ()
00090 {
00091 }
00092 
00093 void Scaler::set (int ht, double h, double l, double lh, double lr, bool lf)
00094 {
00095   height = 0;
00096   logScale = 0;
00097   scaleHigh = 0;
00098   scaleLow = 0;
00099   logScaleHigh = 0;
00100   logRange = 0;
00101   range = 0;
00102   scaler = 0;
00103 
00104   if (h - l == 0)
00105     return;
00106     
00107   height = ht;
00108   logScale = lf;
00109   scaleHigh = h;
00110   scaleLow = l;
00111   logScaleHigh = lh;
00112   logRange = lr;
00113 
00114   range = scaleHigh - scaleLow;
00115   scaler = height / range;
00116 }
00117 
00118 int Scaler::convertToY (double val)
00119 {
00120   if (logScale)
00121   {
00122     if (val <= 0.0)
00123       return height;
00124     else
00125       return (int) (height * (logScaleHigh - log(val)) / logRange);
00126   }
00127 
00128   double t = val - scaleLow;
00129   int y = (int) (t * scaler);
00130   y = height - y;
00131   if (y > height)
00132     y = height;
00133   return y;
00134 }
00135 
00136 double Scaler::convertToVal (int y)
00137 {
00138   if (logScale)
00139   {
00140     if (y >= height)
00141       return scaleLow;
00142     else
00143       return exp(logScaleHigh - ((y * logRange) / height));
00144   }
00145 
00146   if (height == 0)
00147     return 0;
00148     
00149   int p = height - y;
00150   double val = scaleLow + (p / scaler) ;
00151   return val;
00152 }
00153 
00154 void Scaler::getScaleArray (QMemArray<double> &scaleArray)
00155 {
00156   int ticks;
00157   for (ticks = 2; (ticks * 15) < height; ticks++)
00158     ;
00159   ticks--;
00160   if (ticks > 10)
00161     ticks = 10;
00162     
00163   double interval = 0;
00164   int loop;
00165   for (loop = 0; loop < (int) scaleList.count(); loop++)
00166   {
00167     interval = scaleList[loop].toDouble();
00168     if ((range / interval) < ticks)
00169       break;
00170   }
00171 
00172   scaleArray.resize(20);
00173 
00174   loop = 0;
00175   double t = 0 - (ticks * interval);
00176   
00177 /*  
00178   double t = scaleLow;
00179   if (t < 0)
00180     t = 0 - (ticks * interval);
00181   else
00182     t = t - interval;
00183 */
00184 
00185   if (interval > 0)
00186   {
00187     while (t <= scaleHigh)
00188     {
00189       t = t + interval;
00190 
00191       if (t >= scaleLow)
00192       {
00193         scaleArray[loop] = t;
00194         loop++;
00195       }
00196     }
00197   }
00198 
00199   scaleArray.resize(loop);
00200 }
00201 
00202 double Scaler::getLogScaleHigh ()
00203 {
00204   return logScaleHigh;
00205 }
00206 
00207 double Scaler::getLogRange ()
00208 {
00209   return logRange;
00210 }
00211 
00212 int Scaler::getHeight ()
00213 {
00214   return height;
00215 }
00216 
00217 bool Scaler::getLogFlag ()
00218 {
00219   return logScale;
00220 }
00221 
00222 double Scaler::getLow ()
00223 {
00224   return scaleLow;
00225 }
00226 
00227 
00228