OSCAR-code/Graphs/gYAxis.cpp

199 lines
4.7 KiB
C++
Raw Normal View History

/*
2011-06-26 08:30:44 +00:00
gYAxis Implementation
Copyright (c)2011 Mark Watkins <jedimark@users.sourceforge.net>
License: GPL
*/
2011-06-26 08:30:44 +00:00
#include <math.h>
#include <QDebug>
2011-06-26 08:30:44 +00:00
#include "gYAxis.h"
gYSpacer::gYSpacer(int spacer) :Layer(EmptyChannel)
{
};
2011-08-25 10:38:42 +00:00
gXGrid::gXGrid(QColor col)
:Layer(EmptyChannel)
{
m_major_color=QColor(190,190,190,64);
m_minor_color=QColor(220,220,220,64);
m_show_major_lines=true;
m_show_minor_lines=true;
}
gXGrid::~gXGrid()
{
}
void gXGrid::paint(gGraph & w,int left,int top, int width, int height)
{
2011-08-31 05:24:48 +00:00
int x,y;
2011-08-25 10:38:42 +00:00
2011-09-03 02:51:55 +00:00
EventDataType miny=w.min_y;
EventDataType maxy=w.max_y;
2011-08-25 10:38:42 +00:00
if (miny<0) { // even it up if it's starts negative
2011-08-25 10:38:42 +00:00
miny=-MAX(fabs(miny),fabs(maxy));
}
2011-09-03 02:51:55 +00:00
w.roundY(miny,maxy);
EventDataType dy=maxy-miny;
2011-08-25 10:38:42 +00:00
if (height<0) return;
QString fd="0";
GetTextExtent(fd,x,y);
double max_yticks=round(height / (y+15.0)); // plus spacing between lines
double yt=1/max_yticks;
double mxy=MAX(fabs(maxy),fabs(miny));
double mny=miny;
if (miny<0) {
mny=-mxy;
}
double rxy=mxy-mny;
double ymult=height/rxy;
double min_ytick=rxy*yt;
float ty,h;
if (min_ytick<=0) {
qDebug() << "min_ytick error in gXGrid::paint()";
return;
}
if (min_ytick>=1000000) {
min_ytick=100;
}
2011-09-01 07:12:25 +00:00
lines=w.backlines();
2011-08-25 10:38:42 +00:00
for (double i=miny; i<=maxy+min_ytick-0.00001; i+=min_ytick) {
ty=(i - miny) * ymult;
h=top+height-ty;
if (m_show_major_lines && (i > miny)) {
2011-09-01 07:12:25 +00:00
lines->add(left,h,left+width,h,m_major_color);
2011-08-25 10:38:42 +00:00
}
double z=(min_ytick/4)*ymult;
double g=h;
for (int i=0;i<3;i++) {
g+=z;
if (g>top+height) break;
//if (vertcnt>=maxverts) {
// qWarning() << "vertarray bounds exceeded in gYAxis for " << w.title() << "graph" << "MinY =" <<miny << "MaxY =" << maxy << "min_ytick=" <<min_ytick;
// break;
// }
2011-08-25 10:38:42 +00:00
if (m_show_minor_lines) {// && (i > miny)) {
2011-09-01 07:12:25 +00:00
lines->add(left,g,left+width,g,m_minor_color);
2011-08-25 10:38:42 +00:00
}
2011-09-01 07:12:25 +00:00
if (lines->full()) {
2011-08-29 07:13:58 +00:00
break;
}
2011-08-25 10:38:42 +00:00
}
2011-09-01 07:12:25 +00:00
if (lines->full()) {
2011-08-25 10:38:42 +00:00
qWarning() << "vertarray bounds exceeded in gYAxis for " << w.title() << "graph" << "MinY =" <<miny << "MaxY =" << maxy << "min_ytick=" <<min_ytick;
break;
}
}
}
2011-09-05 13:26:10 +00:00
gYAxis::gYAxis(ChannelID code,QColor col)
:Layer(code)
2011-06-26 08:30:44 +00:00
{
m_line_color=col;
m_text_color=col;
2011-06-26 08:30:44 +00:00
m_yaxis_scale=1;
2011-06-26 08:30:44 +00:00
}
gYAxis::~gYAxis()
{
}
void gYAxis::paint(gGraph & w,int left,int top, int width, int height)
2011-06-26 08:30:44 +00:00
{
2011-08-31 05:24:48 +00:00
int x,y;
2011-06-26 08:30:44 +00:00
int labelW=0;
EventDataType miny=w.min_y;
EventDataType maxy=w.max_y;
2011-07-27 09:21:53 +00:00
if (miny<0) { // even it up if it's starts negative
2011-08-07 11:37:56 +00:00
miny=-MAX(fabs(miny),fabs(maxy));
}
2011-07-27 09:21:53 +00:00
w.roundY(miny,maxy);
EventDataType dy=maxy-miny;
2011-07-27 09:21:53 +00:00
2011-07-18 02:33:25 +00:00
//if ((w.max_x-w.min_x)==0)
// return;
2011-06-26 08:30:44 +00:00
if (height<0) return;
QString fd="0";
GetTextExtent(fd,x,y);
double max_yticks=round(height / (y+15.0)); // plus spacing between lines
double yt=1/max_yticks;
double mxy=MAX(fabs(maxy),fabs(miny));
2011-08-07 11:37:56 +00:00
double mny=miny;
if (miny<0) {
mny=-mxy;
}
2011-06-26 08:30:44 +00:00
double rxy=mxy-mny;
double ymult=height/rxy;
double min_ytick=rxy*yt;
float ty,h;
if (min_ytick<=0) {
qDebug() << "min_ytick error in gYAxis::Plot()";
return;
}
2011-07-02 16:07:38 +00:00
if (min_ytick>=1000000) {
min_ytick=100;
}
2011-09-01 07:12:25 +00:00
lines=w.backlines();
2011-06-26 08:30:44 +00:00
2011-06-30 09:37:24 +00:00
for (double i=miny; i<=maxy+min_ytick-0.00001; i+=min_ytick) {
2011-06-26 08:30:44 +00:00
ty=(i - miny) * ymult;
if (dy<5) {
fd=QString().sprintf("%.2f",i*m_yaxis_scale);
} else {
fd=QString().sprintf("%.1f",i*m_yaxis_scale);
}
2011-06-26 08:30:44 +00:00
GetTextExtent(fd,x,y);
if (x>labelW) labelW=x;
h=top+height-ty;
w.renderText(fd,left+width-8-x,(h+(y/2.0)),0,m_text_color);
2011-06-26 08:30:44 +00:00
2011-09-01 07:12:25 +00:00
lines->add(left+width-4,h,left+width,h,m_line_color);
2011-06-26 08:30:44 +00:00
double z=(min_ytick/4)*ymult;
double g=h;
for (int i=0;i<3;i++) {
g+=z;
if (g>top+height) break;
2011-09-01 07:12:25 +00:00
lines->add(left+width-3,g,left+width,g,m_line_color);
if (lines->full()) {
qWarning() << "vertarray bounds exceeded in gYAxis for " << w.title() << "graph" << "MinY =" <<miny << "MaxY =" << maxy << "min_ytick=" <<min_ytick;
break;
}
}
2011-09-01 07:12:25 +00:00
if (lines->full()) {
qWarning() << "vertarray bounds exceeded in gYAxis for " << w.title() << "graph" << "MinY =" <<miny << "MaxY =" << maxy << "min_ytick=" <<min_ytick;
break;
}
2011-06-26 08:30:44 +00:00
}
}
2011-09-05 13:26:10 +00:00
bool gYAxis::mouseMoveEvent(QMouseEvent * event)
{
int x=event->x();
int y=event->y();
//qDebug() << "Hover at " << x << y;
}