00001
00002
00003 #ifndef Impala_Application_TagsLife_BarPlot_h
00004 #define Impala_Application_TagsLife_BarPlot_h
00005
00006 #ifndef OglGui_WindowView2D_h
00007 #include "OglGui/WindowView2D.h"
00008 #endif
00009
00010 #include "Application/MediaTable/TableDataView.h"
00011 #include "Application/MediaTable/TableViewCache.h"
00012
00013 namespace Impala {
00014 namespace Application {
00015 namespace TagsLife {
00016
00017 typedef MediaTable::TableDataView TableDataView;
00018 typedef MediaTable::TableViewCache TableViewCache;
00019
00020
00021
00022
00023 class BarPlot : public OglGui::Window,
00024 public OglGui::WindowListener
00025 {
00026 public:
00027 typedef OglGui::Window Window;
00028 typedef OglGui::WindowView2D WindowView2D;
00029 typedef OglGui::StaticText StaticText;
00030 typedef MediaTable::TableDataView TableDataView;
00031 typedef MediaTable::TableViewCache TableViewCache;
00032
00033 BarPlot(Window* parent, int x, int y, int w, int h) :
00034 Window(parent,x,y,w,h)
00035 {
00036 Init(w,h);
00037 }
00038
00039 void ShowRange(int first, int last)
00040 {
00041 mFirstVisible = first;
00042 mLastVisible = last;
00043 }
00044
00045 void BarColors(ULONG borderCol, ULONG bgCol, ULONG fillCol)
00046 {
00047 mBarBorderColor = borderCol;
00048 mBarBgColor = bgCol;
00049 mBarFillColor = fillCol;
00050 }
00051
00052 int FirstVisible() { return mFirstVisible; }
00053 int LastVisible() { return mLastVisible; }
00054 void MinimumViewSize(int sz) { mMinViewSize = sz; }
00055 int MinimumViewSize() { return mMinViewSize; }
00056
00057
00058 void Clear()
00059 {
00060 mMax = 0;
00061 mSource = 0;
00062 mCache = 0;
00063 }
00064
00065 void AddColumnsFromDataSource(TableDataView* &source, String x, String y, String image = "", String fill="")
00066 {
00067 mSource = source;
00068 mSource->SetStartRow(0);
00069 mSource->SetNumberOfRows(mSource->GetTotalRows());
00070 mCache = new TableViewCache(this, mSource);
00071
00072 mColumnX = x;
00073 mColumnY = y;
00074 mColumnImage = image;
00075 mColumnFill = fill;
00076
00077 ILOG_DEBUG("GetTotalRows = " << mSource->GetTotalRows());
00078 for (int i=0; i<mSource->GetTotalRows(); i++)
00079 {
00080 int y = mSource->GetSortedIntData(mColumnY, i);
00081 if (y > mMax) mMax = y;
00082 }
00083 ILOG_DEBUG("Max for " << mColumnY << " = " << mMax);
00084 double newMin, newMax;
00085 double step = findStep(0.0, (double) mMax, newMin, newMax);
00086 mStep = ceil(step);
00087 mMax = ceil(newMax);
00088 }
00089
00090 int NrVisible()
00091 {
00092 return mLastVisible-mFirstVisible+1;
00093 }
00094
00095 virtual void DisplayFunc()
00096 {
00097 OGC oldOGC;
00098 OGCSave(&oldOGC);
00099 Window::DisplayFunc();
00100 if (NrVisible() != 0 && mSource != 0)
00101 {
00102 ShowHistogram();
00103 HandleViews();
00104 }
00105 OGCRestore(&oldOGC);
00106 }
00107
00108 protected:
00109 void HandleViews()
00110 {
00111 mCache->HideViews();
00112 SetSolidLineColor(mBarBorderColor);
00113
00114 mSlotSize = (W() - AXIS_X) / (float) NrVisible();
00115 if(mColumnImage == "") mSlotSize = 0;
00116 if(mSlotSize < mMinViewSize) {
00117 mSlotSize = 0;
00118 DrawRectangle(AXIS_X, AXIS_Y, W()-AXIS_X, 0);
00119 return;
00120 }
00121
00122 for(int i=mFirstVisible; i <= mLastVisible; i++) {
00123 int x = AXIS_X + (i-mFirstVisible) * mSlotSize;
00124 mCache->ShowView(mColumnImage, i, x, AXIS_Y, mSlotSize-1, mSlotSize-1);
00125 DrawRectangle(x, AXIS_Y, mSlotSize, mSlotSize);
00126 }
00127
00128 if (mCache && mCache->ProcessDelayLoad())
00129 UpdateScene();
00130 }
00131
00132 void ShowXLabel(int value, int x, int slotSz)
00133 {
00134 if (slotSz < 24)
00135 return;
00136
00137 int txtX, txtY;
00138 char buf[10];
00139 sprintf(buf,"%d", value);
00140 oglSys.AlignTextXY(mOglWnd,buf,oglCenterAlign,oglCenterAlign,
00141 slotSz, 20, &txtX, &txtY);
00142 oglSys.PosColPrintf(mOglWnd,x+txtX,txtY+2,mBarBorderColor,buf);
00143 }
00144
00145 void ShowYLabel(int value, int y)
00146 {
00147 int txtX, txtY;
00148 char buf[10];
00149 sprintf(buf,"%d", value);
00150 oglSys.AlignTextXY(mOglWnd,buf,oglRightAlign,oglBottomAlign,
00151 AXIS_X-3, 0, &txtX, &txtY);
00152 oglSys.PosColPrintf(mOglWnd,txtX,y-4,mBarBorderColor,buf);
00153 }
00154
00155 void ShowHistogram()
00156 {
00157 if(!NrVisible()) return;
00158 float slotSz = (W() - AXIS_X) / (float) NrVisible();
00159 float binSz = (slotSz >= 2) ? slotSz / 2 : 1;
00160 float viewH = (slotSz >= mMinViewSize) ? slotSz : 0;
00161 bool oneCol = mBarBgColor == mBarFillColor &&
00162 mBarBgColor == mBarBorderColor;
00163
00164 int yHeight = H()-AXIS_Y-viewH-7;
00165
00166 for (int i=mFirstVisible; i<=mLastVisible; i++)
00167 {
00168 int x = AXIS_X + (i-mFirstVisible) * slotSz;
00169 int y = mSource->GetSortedIntData(mColumnY, i);
00170 int barH = (y/(float) mMax) * yHeight;
00171
00172 if (barH >= 1)
00173 {
00174 SetSolidFillColor(mBarBgColor);
00175 FillRectangle(x+slotSz/4,AXIS_Y+viewH,binSz,barH);
00176 }
00177 if (!oneCol) {
00178 double fill = mSource->GetSortedNormalizedData(mColumnFill, i);
00179 if (fill * barH >= 1)
00180 {
00181 SetSolidFillColor(mBarFillColor);
00182 FillRectangle(x+slotSz/4,AXIS_Y+viewH,binSz,fill*barH);
00183 }
00184 if (!oneCol && barH >= 1)
00185 {
00186 SetSolidLineColor(mBarBorderColor);
00187 DrawRectangle(x+slotSz/4,AXIS_Y+viewH,binSz,barH);
00188 }
00189 }
00190 int xValue = mSource->GetSortedIntData(mColumnX, i);
00191 ShowXLabel(xValue, x, slotSz);
00192 }
00193
00194 SetSolidLineColor(mBarBorderColor);
00195
00196 DrawRectangle(AXIS_X, AXIS_Y+viewH, 0, yHeight);
00197 for(long i=0; i <= mMax; i+= mStep) {
00198
00199 int y = (i/(float) mMax) * yHeight+AXIS_Y+viewH;
00200 DrawRectangle(AXIS_X, y, -3, 0);
00201 ShowYLabel(i, y);
00202 }
00203 }
00204
00205 private:
00206 void Init(int w, int h)
00207 {
00208 mFirstVisible = 0;
00209 mLastVisible = 10;
00210 mMinViewSize = 8;
00211 mMax = 0;
00212
00213 mBarBorderColor = oglRED;
00214 mBarBgColor = oglRED;
00215 mBarFillColor = oglRED;
00216 }
00217
00218 double findStep(double min, double max, double& newMin, double& newMax) {
00219 double range = max - min;
00220 if(range < 0) range *= -1;
00221 double exponent = floor(log10(range));
00222 double coefficient = range/pow(10, exponent);
00223
00224 double step = 1;
00225 if(coefficient <= 5) {
00226 step = 0.5;
00227 if(coefficient <= 2.5) {
00228 step = 0.25;
00229 if(coefficient == 1) step = 0.1;
00230 }
00231 }
00232
00233 step *= pow(10, exponent);
00234 if(step == 2.5) step = 2;
00235
00236 newMin = floor(min/step)*step;
00237 newMax = ceil(max/step)*step;
00238
00239 if(min > max) return -step;
00240 return step;
00241 }
00242 double findStep(double& min, double& max) {return findStep(min, max, min, max);}
00243
00244 protected:
00245 TableDataView* mSource;
00246 TableViewCache* mCache;
00247 String mColumnX, mColumnY, mColumnFill, mColumnImage;
00248
00249 int mNrViews;
00250 int mFirstVisible;
00251 int mLastVisible;
00252 int mMinViewSize;
00253 long mMax;
00254 int mStep;
00255 float mSlotSize;
00256
00257 const static int AXIS_X = 65;
00258 const static int AXIS_Y = 20;
00259
00260 ULONG mBarBorderColor;
00261 ULONG mBarBgColor;
00262 ULONG mBarFillColor;
00263
00264 private:
00265 ILOG_VAR_DEC;
00266 };
00267
00268 ILOG_VAR_INIT(BarPlot, Application.TagsLife);
00269
00270 }
00271 }
00272 }
00273
00274 #endif