yAxis wasn't accurate in AHI chart

This commit is contained in:
Mark Watkins 2011-09-03 12:11:10 +10:00
parent 8504db82e2
commit 0822f0cba4

View File

@ -89,6 +89,7 @@ void gBarChart::SetDay(Day * day)
}
}
m_maxy=ceil(m_maxy);
m_miny=floor(m_miny);
//m_minx-=86400000L;
// m_minx=qint64(QDateTime(m_profile->FirstDay(),QTime(0,0,0),Qt::UTC).toTime_t())*1000L;
@ -117,8 +118,39 @@ void gBarChart::paint(gGraph & w,int left, int top, int width, int height)
qint64 xx=maxx - minx;
float days=double(xx)/86400000.0;
EventDataType yy=m_maxy-m_miny;
EventDataType ymult=float(height)/yy;
EventDataType maxy=m_maxy;
EventDataType miny=m_miny;
int m;
if (maxy>500) {
m=ceil(maxy/100.0);
maxy=m*100;
m=floor(miny/100.0);
miny=m*100;
} else if (maxy>150) {
m=ceil(maxy/50.0);
maxy=m*50;
m=floor(miny/50.0);
miny=m*50;
} else if (maxy>80) {
m=ceil(maxy/20.0);
maxy=m*20;
m=floor(miny/20.0);
miny=m*20;
} else if (maxy>30) {
m=ceil(maxy/10.0);
maxy=m*10;
m=floor(miny/10.0);
miny=m*10;
} else if (maxy>5) {
m=ceil(maxy/5.0);
maxy=m*5;
m=floor(miny/5.0);
miny=m*5;
}
EventDataType yy=maxy-miny;
EventDataType ymult=float(height-2)/yy;
float barw=(float(width)/float(days));
@ -140,14 +172,27 @@ void gBarChart::paint(gGraph & w,int left, int top, int width, int height)
offset*=barw;
px=left-offset;
int total_days=0;
double total_val=0;
for (qint64 Q=minx;Q<=maxx+86400000L;Q+=86400000L) {
int zd=Q/86400000L;
QHash<int,QMap<ChannelID,EventDataType> >::iterator d=m_values.find(zd);
if (Q<minx) continue;
if (Q>maxx+86400000) continue; // break; // out of order if I end up using a hash instead.??
if (d!=m_values.end()) {
int x1=px,x2=px+barw;
if (x1<left) x1=left;
if (x2>left+width) x2=left+width;
if (x2<x1) continue;
ChannelID code;
total=d.value()[EmptyChannel];
if (total>0) {
total_val+=total;
total_days++;
}
py=top+height;
for (int j=0;j<m_codes.size();j++) {
code=m_codes[j];
@ -158,22 +203,16 @@ void gBarChart::paint(gGraph & w,int left, int top, int width, int height)
QColor col=m_colors[j];
QColor col2=Qt::white;
tmp=(g.value()/float(total));
h=tmp*(float(total)*ymult); // height of chunk
int x1=px,x2=px+barw;
if (x1<left) x1=left;
if (x2>left+width) x2=left+width;
if (x2>x1) {
quads->add(x1,py,col);
quads->add(x1,py-h,col);
quads->add(x2,py-h,col2);
quads->add(x2,py,col2);
lines->add(x1,py,x1,py-h,blk);
lines->add(x1,py-h,x2,py-h,blk);
lines->add(x1,py,x2,py,blk);
lines->add(x2,py,x2,py-h,blk);
}
tmp=g.value(); //(g.value()/float(total));
h=tmp*ymult; //(float(total)*ymult); // height of chunk
quads->add(x1,py,col);
quads->add(x1,py-h,col);
quads->add(x2,py-h,col2);
quads->add(x2,py,col2);
lines->add(x1,py,x1,py-h,blk);
lines->add(x1,py-h,x2,py-h,blk);
lines->add(x1,py,x2,py,blk);
lines->add(x2,py,x2,py-h,blk);
py-=h;
}
}
@ -181,149 +220,12 @@ void gBarChart::paint(gGraph & w,int left, int top, int width, int height)
px+=barw;
daynum++;
}
if (total_days>0) {
float val=total_val/float(total_days);
QString z="AHI="+QString::number(val,'f',2)+" days="+QString::number(total_days,'f',0);
w.renderText(z,left,top-1);
// if (!data) return;
//if (!data->IsReady()) return;
//int start_px=left;
//int start_py=top;
//days=data->np[0];
//days=0;
/* for (int i=0;i<data->np[0];i++) {
if ((data->point[0][i].x() >= w.min_x) && (data->point[0][i].x()<w.max_x)) days+=1;
// val = AHI for selected area.
}
if (days==0) return;
float barwidth,pxr;
float px,zpx;//,py;
if (m_orientation==Qt::Vertical) {
barwidth=(height-days)/float(days);
pxr=width/w.max_y;
px=start_py;
} else {
barwidth=(width-days)/float(days);
pxr=height/w.max_y;
px=start_px;
}
px+=1;
int t1,t2;
int u1,u2;
float textX, textY;
QString str;
bool draw_xticks_instead=false;
bool antialias=pref["UseAntiAliasing"].toBool();
if (antialias) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
}
zpx=px;
int i,idx=-1;
for (i=0;i<data->np[0];i++) {
if (data->point[0][i].x() < w.min_x) continue;
if (data->point[0][i].x() >= w.max_x) break;
if (idx<0) idx=i;
t1=px;
px+=barwidth+1;
t2=px-t1-1;
QRect rect;
//Qt:wxDirection dir;
u2=data->point[0][i].y()*pxr;
u1=start_py;
if (antialias) {
u1++;
u2++;
}
if (m_orientation==Qt::Vertical) {
rect=QRect(start_px,t1,u2,t2);
} else {
rect=QRect(t1,u1,t2,u2);
}
//dir=wxEAST;
//RoundedRectangle(rect.x,rect.y,rect.width,rect.height,1,color[0]); //,*wxLIGHT_GREY,dir);
// TODO: Put this in a function..
QColor & col1=color[0];
QColor col2("light grey");
glBegin(GL_QUADS);
//red color
glColor4ub(col1.red(),col1.green(),col1.blue(),col1.alpha());
glVertex2f(rect.x(), rect.y()+rect.height());
glVertex2f(rect.x(), rect.y());
//blue color
glColor4ub(col2.red(),col2.green(),col2.blue(),col2.alpha());
glVertex2f(rect.x()+rect.width(),rect.y());
glVertex2f(rect.x()+rect.width(), rect.y()+rect.height());
glEnd();
glColor4ub(0,0,0,255);
glLineWidth (1);
glBegin(GL_LINE_LOOP);
glVertex2f(rect.x(), rect.y()+rect.height()+.5);
glVertex2f(rect.x(), rect.y());
glVertex2f(rect.x()+rect.width(),rect.y());
glVertex2f(rect.x()+rect.width(), rect.y()+rect.height()+.5);
//glVertex2f(rect.x(), rect.y()+rect.height());
glEnd();
if (!draw_xticks_instead) {
str=FormatX(data->point[0][i].x());
GetTextExtent(str, textX, textY);
if (t2<textY+6)
draw_xticks_instead=true;
}
}
if (antialias) {
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
}
if (draw_xticks_instead) {
// turn off the minor ticks..
Xaxis->SetShowMinorTicks(false);
Xaxis->Plot(w,scrx,scry);
} else {
px=zpx;
for (i=idx;i<data->np[0];i++) {
if (data->point[0][i].x() < w.min_x) continue;
if (data->point[0][i].x() >= w.max_x) break;
t1=px;
px+=barwidth+1;
t2=px-t1-1;
str=FormatX(data->point[0][i].x());
GetTextExtent(str, textX, textY);
float j=t1+((t2/2.0)+(textY/2.0)+5);
if (m_orientation==Qt::Vertical) {
DrawText(str,start_px-textX-8,scry-j);
} else {
DrawText(str,j,scry-(start_py-3-(textX/2)),90);
}
}
}
glColor3f (0.1F, 0.1F, 0.1F);
glLineWidth(1);
glBegin (GL_LINES);
glVertex2f (start_px, start_py);
glVertex2f (start_px, start_py+height+1);
//glVertex2f (start_px,start_py);
//glVertex2f (start_px+width, start_py);
glEnd ();
*/
}