From 2a67480b6cc7f3b14cfe0d38582d342cdcf6219d 2012-10-09 13:23:28 From: Michal Klocek Date: 2012-10-09 13:23:28 Subject: [PATCH] Reimplement zoom/scroll hanling for barcategoryaxis Y --- diff --git a/demos/chartviewer/charts/charts.pri b/demos/chartviewer/charts/charts.pri index 226dbad..bf03b62 100644 --- a/demos/chartviewer/charts/charts.pri +++ b/demos/chartviewer/charts/charts.pri @@ -20,6 +20,12 @@ SOURCES += \ axis/barcategoryaxisy.cpp \ axis/datetimeaxisx.cpp \ axis/datetimeaxisy.cpp \ + axis/valueaxis2.cpp \ + axis/categoryaxis2.cpp \ + axis/barcategoryaxisx2.cpp \ + axis/barcategoryaxisy2.cpp \ + axis/datetimeaxisx2.cpp \ + axis/datetimeaxisy2.cpp \ multiaxis/multivalueaxis.cpp \ multiaxis/multivalueaxis2.cpp \ multiaxis/multivalueaxis3.cpp \ diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp index dd098cb..3498230 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp @@ -41,27 +41,22 @@ ChartBarCategoryAxisX::~ChartBarCategoryAxisX() QVector ChartBarCategoryAxisX::calculateLayout() const { QVector points; - const QRectF& gridRect = gridGeometry(); qreal range = max() - min(); - const qreal delta = gridRect.width()/range; if(delta<2) return points; qreal offset =-min()-0.5; - offset = int(offset * delta)%int(delta); int count = qFloor(range); - if(count < 1 ) return points; points.resize(count+2); for (int i = 0; i < count+2; ++i) { - qreal x = offset + i * delta + gridRect.left(); - points[i] = x; + points[i] = offset + i * delta + gridRect.left(); } return points; @@ -71,8 +66,8 @@ QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector& la { QStringList result ; const QRectF &gridRect = gridGeometry(); - qreal d = (max() - min()) / gridRect.width(); + for (int i = 0; i < layout.count() - 1; ++i) { qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5)); if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp index ee0a4f8..e611fe3 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp @@ -39,30 +39,25 @@ ChartBarCategoryAxisY::~ChartBarCategoryAxisY() QVector ChartBarCategoryAxisY::calculateLayout() const { - int count = m_categoriesAxis->d_ptr->count(); - - Q_ASSERT(count >= 1); - QVector points; - points.resize(count + 2); + const QRectF& gridRect = gridGeometry(); + qreal range = max() - min(); + const qreal delta = gridRect.height()/range; - const QRectF &gridRect = gridGeometry(); + if(delta<2) return points; - const qreal delta = gridRect.height() / (count); qreal offset = - min() - 0.5; + offset = int(offset * delta)%int(delta); - if (delta < 1) - return points; + int count = qFloor(range); + if(count < 1 ) return points; - if (offset < 0) - offset = int(offset * gridRect.height() / (max() - min())) % int(delta) + delta; - else - offset = int(offset * gridRect.height() / (max() - min())) % int(delta); + points.resize(count+2); - for (int i = -1; i < count + 1; ++i) { - int y = gridRect.bottom() - i * delta - offset; - points[i + 1] = y; + for (int i = 0; i < count+2; ++i) { + points[i] = gridRect.bottom() - i * delta -offset; } + return points; } @@ -71,6 +66,7 @@ QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector& la QStringList result; const QRectF &gridRect = gridGeometry(); qreal d = (max() - min()) / gridRect.height(); + for (int i = 0; i < layout.count() - 1; ++i) { qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5)); if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { diff --git a/src/axis/horizontalaxis.cpp b/src/axis/horizontalaxis.cpp index 66bfa21..2be7569 100644 --- a/src/axis/horizontalaxis.cpp +++ b/src/axis/horizontalaxis.cpp @@ -82,10 +82,10 @@ void HorizontalAxis::updateGeometry() //wrapping in case of interval axis const qreal delta = layout[i+1] - layout[i]; QString text = labelList.at(i); - if (fn.boundingRect(text).width() > delta ) + if (fn.boundingRect(text).width() + 1 > delta ) { QString label = text + "..."; - while (fn.boundingRect(label).width() > delta && label.length() > 3) + while (fn.boundingRect(label).width() >= delta && label.length() > 3) label.remove(label.length() - 4, 1); labelItem->setText(label); } @@ -117,12 +117,11 @@ void HorizontalAxis::updateGeometry() } //label overlap detection - if(labelItem->pos().x() <= width || + if(labelItem->pos().x() < width || labelItem->pos().x() < axisRect.left() || labelItem->pos().x() + rect.width() > axisRect.right()) { labelItem->setVisible(false); - } - else { + } else { labelItem->setVisible(true); width=rect.width()+labelItem->pos().x(); }