##// END OF EJS Templates
Bugfix precision error when calculating layout in valueaxis
Bugfix precision error when calculating layout in valueaxis

File last commit:

r2138:44f1f46281dd
r2144:c7788799f12f
Show More
chartbarcategoryaxisy.cpp
146 lines | 4.2 KiB | text/x-c | CppLexer
/ src / axis / barcategoryaxis / chartbarcategoryaxisy.cpp
Marek Rosa
Added Chart classes for value and categories axis
r1555 /****************************************************************************
Michal Klocek
Adds scroll support for categories axis
r1716 **
** Copyright (C) 2012 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Commercial Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/
Marek Rosa
Added Chart classes for value and categories axis
r1555
Marek Rosa
renamed QBarCategoryAxis related files
r1810 #include "chartbarcategoryaxisy_p.h"
Marek Rosa
Added Chart classes for value and categories axis
r1555 #include "chartpresenter_p.h"
Marek Rosa
renamed QBarCategoryAxis related files
r1810 #include "qbarcategoryaxis_p.h"
Michal Klocek
Fixes rounding issue with labels numbering
r1720 #include <qmath.h>
Michal Klocek
Refactors layout...
r1965 #include <QFontMetrics>
Michal Klocek
Updates axis drawing code...
r2133 #include <QDebug>
Marek Rosa
Added Chart classes for value and categories axis
r1555
QTCOMMERCIALCHART_BEGIN_NAMESPACE
Jani Honkonen
src folder: another massive victory for coding style police
r2131 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter)
: VerticalAxis(axis, presenter, true),
m_categoriesAxis(axis)
Marek Rosa
Added Chart classes for value and categories axis
r1555 {
}
Marek Rosa
Renamed barcategory grephics item class which has been previously ommited
r1918 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
Marek Rosa
Added Chart classes for value and categories axis
r1555 {
}
Marek Rosa
Renamed barcategory grephics item class which has been previously ommited
r1918 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
Marek Rosa
Added Chart classes for value and categories axis
r1555 {
Michal Klocek
Implements qbarcategoriesaxis logic...
r1725 int count = m_categoriesAxis->d_ptr->count();
Jani Honkonen
src folder: another massive victory for coding style police
r2131 Q_ASSERT(count >= 1);
Marek Rosa
Added Chart classes for value and categories axis
r1555
QVector<qreal> points;
Jani Honkonen
src folder: another massive victory for coding style police
r2131 points.resize(count + 2);
Marek Rosa
Added Chart classes for value and categories axis
r1555
Jani Honkonen
src folder: another massive victory for coding style police
r2131 const QRectF &gridRect = gridGeometry();
Michal Klocek
Refactors axis updateGeometry handling...
r2111
Jani Honkonen
src folder: another massive victory for coding style police
r2131 const qreal delta = gridRect.height() / (count);
Michal Klocek
Refactors axis updateGeometry handling...
r2111 qreal offset = - min() - 0.5;
Michal Klocek
Adds scroll support for categories axis
r1716
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (delta < 1)
return points;
Michal Klocek
Bugfixes for layout...
r1837
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (offset < 0)
offset = int(offset * gridRect.height() / (max() - min())) % int(delta) + delta;
else
offset = int(offset * gridRect.height() / (max() - min())) % int(delta);
Michal Klocek
Adds scroll support for categories axis
r1716
Jani Honkonen
src folder: another massive victory for coding style police
r2131 for (int i = -1; i < count + 1; ++i) {
Michal Klocek
Refactors axis updateGeometry handling...
r2111 int y = gridRect.bottom() - i * delta - offset;
Jani Honkonen
src folder: another massive victory for coding style police
r2131 points[i + 1] = y;
Marek Rosa
Added Chart classes for value and categories axis
r1555 }
return points;
}
Marek Rosa
Renamed barcategory grephics item class which has been previously ommited
r1918 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
Michal Klocek
Fixes rounding issue with labels numbering
r1720 {
QStringList result;
Jani Honkonen
src folder: another massive victory for coding style police
r2131 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));
Michal Klocek
Refactors layout:...
r2105 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
Michal Klocek
Fixes rounding issue with labels numbering
r1720 result << m_categoriesAxis->categories().at(x);
Jani Honkonen
src folder: another massive victory for coding style police
r2131 } else {
Michal Klocek
Refactors layout:...
r2105 // No label for x coordinate
result << "";
}
Michal Klocek
Fixes rounding issue with labels numbering
r1720 }
result << "";
return result;
}
Marek Rosa
Renamed barcategory grephics item class which has been previously ommited
r1918 void ChartBarCategoryAxisY::updateGeometry()
Marek Rosa
Added Chart classes for value and categories axis
r1555 {
const QVector<qreal>& layout = ChartAxis::layout();
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (layout.isEmpty())
return;
Michal Klocek
Refactors axis updateGeometry handling...
r2111 setLabels(createCategoryLabels(layout));
VerticalAxis::updateGeometry();
Marek Rosa
Added Chart classes for value and categories axis
r1555 }
Marek Rosa
Renamed barcategory grephics item class which has been previously ommited
r1918 void ChartBarCategoryAxisY::handleAxisUpdated()
Michal Klocek
Fixes missing update barcategory...
r1643 {
Jani Honkonen
src folder: another massive victory for coding style police
r2131 if (m_categoriesAxis->categories() != m_categories) {
m_categories = m_categoriesAxis->categories();
if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2)
Michal Klocek
Fixes rounding issue with labels numbering
r1720 updateGeometry();
Michal Klocek
Fixes missing update barcategory...
r1643 }
ChartAxis::handleAxisUpdated();
}
Jani Honkonen
src folder: another massive victory for coding style police
r2131 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
Michal Klocek
Refactors layout...
r1965 {
Q_UNUSED(constraint)
Michal Klocek
Refactors axis updateGeometry handling...
r2111 QFontMetrics fn(font());
Michal Klocek
Refactors layout...
r1965 QSizeF sh;
Michal Klocek
Refactors ChartAxis...
r2138 QSizeF base = VerticalAxis::sizeHint(which, constraint);
Michal Klocek
Refactors layout...
r1965 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
Michal Klocek
Updates axis drawing code...
r2133 qreal width=0;
qreal height=0;
switch (which) {
case Qt::MinimumSize:
width = fn.boundingRect("...").width() + labelPadding();
height = fn.height();
width+=base.width();
height=qMax(height,base.height());
sh = QSizeF(width,height);
break;
case Qt::PreferredSize:{
for (int i = 0; i < ticksList.size(); ++i)
{
QRectF rect = fn.boundingRect(ticksList.at(i));
height+=rect.height();
Michal Klocek
Fixes categoryaxis sizeHints
r2136 width=qMax(rect.width()+labelPadding() + 1 ,width); //one pixel torelance
Michal Klocek
Updates axis drawing code...
r2133 }
height=qMax(height,base.height());
width+=base.width();
sh = QSizeF(width,height);
break;
}
default:
break;
}
return sh;
Michal Klocek
Refactors layout...
r1965 }
Marek Rosa
Added Chart classes for value and categories axis
r1555 QTCOMMERCIALCHART_END_NAMESPACE