git-svn-id: svn://db.shs.com.ru/libs@41 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2015-11-09 09:32:31 +00:00
parent 8da2529bda
commit c27e345c51
2 changed files with 80 additions and 29 deletions

View File

@@ -897,24 +897,21 @@ void Graphic::drawGrid() {
if (axis_type_x == Graphic::Numeric) {
if (grad_x == Graphic::Auto) step = splitRange(range, wid / gridx / font_sz.width() * 1.4);
else step = gridx;//range / wid * gridx;
} else
step = splitRangeDate(range, wid / gridx / font_sz.width() * 1.4, &df);
start = roundTo(canvas2realX(wid), step) + step;
px = start + step;
if (step > 0.) {
cnt = 1000;
while (cnt-- > 0) {
px -= step;
if (fabs(px) < step * .5) px = 0.;
cx = real2canvasX(px);
if (cx > cwid) continue;
if (cx < gbx) break;
painter->setPen(grid_pen);
painter->drawLine(cx, hei + 5, cx, 0);
painter->setPen(text_color);
int dx = -font_sz.height() / 4.;
painter->setFont(nf);
if (axis_type_x == Graphic::Numeric) {
start = roundTo(canvas2realX(wid), step) + step;
px = start + step;
if (step > 0.) {
cnt = 1000;
while (cnt-- > 0) {
px -= step;
if (fabs(px) < step * .5) px = 0.;
cx = real2canvasX(px);
if (cx > cwid) continue;
if (cx < gbx) break;
painter->setPen(grid_pen);
painter->drawLine(cx, hei + 5, cx, 0);
painter->setPen(text_color);
int dx = -font_sz.height() / 4.;
painter->setFont(nf);
str = gridMark(px * grid_numbers_x);
rect = fm.boundingRect(str.first);
painter->drawText(cx + dx, cy, str.first);
@@ -924,8 +921,35 @@ void Graphic::drawGrid() {
painter->setFont(sf);
painter->drawText(cx + dx, cy - font_sz.height() / 2.5, str.second);
}
} else {
str.first = QDateTime::fromMSecsSinceEpoch(px * grid_numbers_x).toString(df);
}
}
} else {
int cur_scl[7] = {0,0,0,0,0,0,0};
step = splitRangeDate(range, wid / gridx / font_sz.width() * 1.4, &df, cur_scl);
start = roundTo(canvas2realX(wid), step) + step;
px = start + step;
QDateTime cd = QDateTime::fromMSecsSinceEpoch(px * grid_numbers_x);
//qDebug() << "*** start" << cd << step;
roundDateTime(cd, cur_scl);
//qDebug() << "*** round" << cd;
addDateTime(cd, cur_scl);
//qDebug() << "*** add" << cd;
//qDebug() << "*** cur" << cur_scl[0] << cur_scl[1] << cur_scl[2] << cur_scl[3] << cur_scl[4] << cur_scl[5] << cur_scl[6];
if (step > 0.) {
cnt = 1000;
while (cnt-- > 0) {
addDateTime(cd, cur_scl, -1);
//roundDateTime(cd, cur_scl);
//qDebug() << "next" << cd;
cx = real2canvasX(cd.toMSecsSinceEpoch() / grid_numbers_x);
if (cx > cwid) continue;
if (cx < gbx) {/*qDebug() << cx << "<" << gbx;*/ break;}
painter->setPen(grid_pen);
painter->drawLine(cx, hei + 5, cx, 0);
painter->setPen(text_color);
int dx = -font_sz.height() / 4.;
painter->setFont(nf);
str.first = cd.toString(df);
painter->drawText(cx + dx, cy, str.first);
}
}
@@ -1068,15 +1092,16 @@ double Graphic::splitRange(double range, int count) {
}
double Graphic::splitRangeDate(double range, int count, QString * format) {
double Graphic::splitRangeDate(double range, int count, QString * format, int step[7]) {
double ret = splitRange(range, count);
if (ret < 1000. * 1) *format = "ss.zzz";
else if (ret < 1000. * 60) *format = "h:m:ss";
else if (ret < 1000. * 60 * 60) *format = "h:mm";
else if (ret < 1000. * 60 * 60 * 24) *format = "dd(ddd) hh";
else if (ret < 1000. * 60 * 60 * 24 * 30) *format = "MMM dd";
else if (ret < 1000. * 60 * 60 * 24 * 30 * 12) *format = "yyyy MMM";
else *format = "yyyy";
//qDebug() << "ret =" << ret << getScaleX();
if (ret < 1000. * 1) {*format = "ss.zzz"; step[0] = ret;}
else if (ret < 1000. * 60) {*format = "h:m:ss"; step[1] = qRound(ret / 1000);}
else if (ret < 1000. * 60 * 60) {*format = "h:mm"; step[2] = qRound(ret / 1000 / 60);}
else if (ret < 1000. * 60 * 60 * 24) {*format = "dd(ddd) hh"; step[3] = qRound(ret / 1000 / 60 / 60);}
else if (ret < 1000. * 60 * 60 * 24 * 30) {*format = "MMM dd"; step[4] = qRound(ret / 1000 / 60 / 60 / 24);}
else if (ret < 1000. * 60 * 60 * 24 * 30 * 12) {*format = "yyyy MMM"; step[5] = qRound(ret / 1000 / 60 / 60 / 24 / 30);}
else {*format = "yyyy"; step[6] = qRound(ret / 1000 / 60 / 60 / 24 / 30 / 12);}
return ret;
}
@@ -1087,6 +1112,30 @@ double Graphic::roundTo(double value, double round_to) {
}
void Graphic::roundDateTime(QDateTime & dt, int c[7]) {
QDate d(dt.date()); QTime t(dt.time());
//if (c[0] != 0) t.setHMS(t.hour(), t.minute(), t.second(), 0);
if (c[1] != 0) t.setHMS(t.hour(), t.minute(), t.second());
if (c[2] != 0) t.setHMS(t.hour(), t.minute(), 0);
if (c[3] != 0) t.setHMS(t.hour(), 0, 0);
if (c[4] != 0) {t.setHMS(0, 0, 0); d.setDate(d.year(), d.month(), d.day());}
if (c[5] != 0) {t.setHMS(0, 0, 0); d.setDate(d.year(), d.month(), 1);}
if (c[6] != 0) {t.setHMS(0, 0, 0); d.setDate(d.year(), 1, 1);}
dt = QDateTime(d, t);
}
void Graphic::addDateTime(QDateTime & dt, int c[7], int mul) {
if (c[0] != 0) dt = dt.addMSecs(mul * c[0]);
if (c[1] != 0) dt = dt.addSecs(mul * c[1]);
if (c[2] != 0) dt = dt.addSecs(mul * c[2] * 60);
if (c[3] != 0) dt = dt.addSecs(mul * c[3] * 60 * 60);
if (c[4] != 0) dt = dt.addDays(mul * c[4]);
if (c[5] != 0) dt = dt.addMonths(mul * c[5]);
if (c[6] != 0) dt = dt.addYears(mul * c[6]);
}
double Graphic::canvas2realX(double px) const {
int gbx = gridborder.x() + margins_.left(), cwid = lastw, wid = cwid - gbx - margins_.width();
double cx = px - gbx, sclx = selrect.width() / (double)wid;

View File

@@ -355,8 +355,10 @@ protected:
void setRectToLines();
void checkLines();
double splitRange(double range, int count = 1);
double splitRangeDate(double range, int count = 1, QString * format = 0);
double splitRangeDate(double range, int count = 1, QString * format = 0, int step[7] = 0);
double roundTo(double value, double round_to);
void roundDateTime(QDateTime & dt, int c[7]);
void addDateTime(QDateTime & dt, int c[7], int mul = 1);
QPointF absPoint(QPointF point) {return QPointF(qAbs(point.x()), qAbs(point.y()));}
QString pointCoords(QPointF point);
QPair<QString, QString> gridMark(double v) const;