chartaxis.cpp
508 lines
| 15.6 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r1006 | #include "chartaxis_p.h" | ||
Michal Klocek
|
r1541 | #include "qabstractaxis.h" | ||
#include "qabstractaxis_p.h" | ||||
Michal Klocek
|
r262 | #include "chartpresenter_p.h" | ||
Michal Klocek
|
r2105 | #include "chartlayout_p.h" | ||
Marek Rosa
|
r2275 | #include "abstractdomain_p.h" | ||
Michal Klocek
|
r1577 | #include <qmath.h> | ||
Marek Rosa
|
r1717 | #include <QDateTime> | ||
Marek Rosa
|
r1854 | #include <QValueAxis> | ||
Marek Rosa
|
r2274 | #include <QLogValueAxis> | ||
Michal Klocek
|
r1890 | #include <QGraphicsLayout> | ||
Michal Klocek
|
r1965 | #include <QFontMetrics> | ||
Michal Klocek
|
r67 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r2273 | ChartAxis::ChartAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis) | ||
: ChartElement(item), | ||||
m_axis(axis), | ||||
Jani Honkonen
|
r2131 | m_labelsAngle(0), | ||
Michal Klocek
|
r2273 | m_grid(new QGraphicsItemGroup(item)), | ||
m_arrow(new QGraphicsItemGroup(item)), | ||||
m_shades(new QGraphicsItemGroup(item)), | ||||
m_labels(new QGraphicsItemGroup(item)), | ||||
m_title(new QGraphicsSimpleTextItem(item)), | ||||
Jani Honkonen
|
r2131 | m_animation(0), | ||
m_labelPadding(5), | ||||
m_intervalAxis(intervalAxis) | ||||
Michal Klocek
|
r67 | { | ||
Michal Klocek
|
r2273 | Q_ASSERT(item); | ||
Michal Klocek
|
r145 | //initial initialization | ||
Michal Klocek
|
r1729 | m_arrow->setHandlesChildEvents(false); | ||
Michal Klocek
|
r2273 | m_arrow->setZValue(ChartPresenter::AxisZValue); | ||
m_arrow->setVisible(m_axis->isLineVisible()); | ||||
Michal Klocek
|
r1745 | m_labels->setZValue(ChartPresenter::AxisZValue); | ||
Michal Klocek
|
r2273 | m_labels->setVisible(m_axis->labelsVisible()); | ||
Jani Honkonen
|
r784 | m_shades->setZValue(ChartPresenter::ShadesZValue); | ||
Michal Klocek
|
r2273 | m_shades->setVisible(m_axis->shadesVisible()); | ||
Jani Honkonen
|
r784 | m_grid->setZValue(ChartPresenter::GridZValue); | ||
Michal Klocek
|
r2273 | m_grid->setVisible(m_axis->isGridLineVisible()); | ||
Michal Klocek
|
r2142 | m_title->setZValue(ChartPresenter::GridZValue); | ||
Michal Klocek
|
r2273 | connectSlots(); | ||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r2273 | setFlag(QGraphicsItem::ItemHasNoContents,true); | ||
} | ||||
Michal Klocek
|
r1837 | |||
Michal Klocek
|
r2273 | void ChartAxis::connectSlots() | ||
{ | ||||
QObject::connect(m_axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleVisibleChanged(bool))); | ||||
QObject::connect(m_axis,SIGNAL(lineVisibleChanged(bool)),this,SLOT(handleArrowVisibleChanged(bool))); | ||||
Jani Honkonen
|
r2282 | QObject::connect(m_axis,SIGNAL(gridVisibleChanged(bool)),this,SLOT(handleGridVisibleChanged(bool))); | ||
Michal Klocek
|
r2273 | QObject::connect(m_axis,SIGNAL(labelsVisibleChanged(bool)),this,SLOT(handleLabelsVisibleChanged(bool))); | ||
QObject::connect(m_axis,SIGNAL(shadesVisibleChanged(bool)),this,SLOT(handleShadesVisibleChanged(bool))); | ||||
QObject::connect(m_axis,SIGNAL(labelsAngleChanged(int)),this,SLOT(handleLabelsAngleChanged(int))); | ||||
QObject::connect(m_axis,SIGNAL(linePenChanged(const QPen&)),this,SLOT(handleArrowPenChanged(const QPen&))); | ||||
QObject::connect(m_axis,SIGNAL(labelsPenChanged(const QPen&)),this,SLOT(handleLabelsPenChanged(const QPen&))); | ||||
QObject::connect(m_axis,SIGNAL(labelsBrushChanged(const QBrush&)),this,SLOT(handleLabelsBrushChanged(const QBrush&))); | ||||
QObject::connect(m_axis,SIGNAL(labelsFontChanged(const QFont&)),this,SLOT(handleLabelsFontChanged(const QFont&))); | ||||
QObject::connect(m_axis,SIGNAL(gridLinePenChanged(const QPen&)),this,SLOT(handleGridPenChanged(const QPen&))); | ||||
QObject::connect(m_axis,SIGNAL(shadesPenChanged(const QPen&)),this,SLOT(handleShadesPenChanged(const QPen&))); | ||||
QObject::connect(m_axis,SIGNAL(shadesBrushChanged(const QBrush&)),this,SLOT(handleShadesBrushChanged(const QBrush&))); | ||||
QObject::connect(m_axis,SIGNAL(titleTextChanged(const QString&)),this,SLOT(handleTitleTextChanged(const QString&))); | ||||
QObject::connect(m_axis,SIGNAL(titleFontChanged(const QFont&)),this,SLOT(handleTitleFontChanged(const QFont&))); | ||||
QObject::connect(m_axis,SIGNAL(titlePenChanged(const QPen&)),this,SLOT(handleTitlePenChanged(const QPen&))); | ||||
QObject::connect(m_axis,SIGNAL(titleBrushChanged(const QBrush&)),this,SLOT(handleTitleBrushChanged(const QBrush&))); | ||||
QObject::connect(m_axis->d_ptr.data(),SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(handleRangeChanged(qreal,qreal))); | ||||
Michal Klocek
|
r67 | } | ||
Michal Klocek
|
r1006 | ChartAxis::~ChartAxis() | ||
Michal Klocek
|
r67 | { | ||
} | ||||
Jani Honkonen
|
r2131 | void ChartAxis::setAnimation(AxisAnimation *animation) | ||
Michal Klocek
|
r176 | { | ||
Jani Honkonen
|
r2131 | m_animation = animation; | ||
Michal Klocek
|
r1241 | } | ||
Michal Klocek
|
r530 | |||
Michal Klocek
|
r1241 | void ChartAxis::setLayout(QVector<qreal> &layout) | ||
{ | ||||
Jani Honkonen
|
r2131 | m_layoutVector = layout; | ||
Michal Klocek
|
r1241 | } | ||
void ChartAxis::createItems(int count) | ||||
{ | ||||
Jani Honkonen
|
r2277 | if (m_arrow->childItems().size() == 0){ | ||
Michal Klocek
|
r2273 | QGraphicsLineItem* arrow = new ArrowItem(this, this); | ||
arrow->setPen(m_axis->linePen()); | ||||
m_arrow->addToGroup(arrow); | ||||
} | ||||
Jani Honkonen
|
r2131 | |||
Jani Honkonen
|
r2277 | if (m_intervalAxis && m_grid->childItems().size() == 0) { | ||
Michal Klocek
|
r2273 | for (int i = 0 ; i < 2 ; i ++){ | ||
Jani Honkonen
|
r2277 | QGraphicsLineItem* item = new QGraphicsLineItem(this); | ||
item->setPen(m_axis->gridLinePen()); | ||||
Michal Klocek
|
r2273 | m_grid->addToGroup(item); | ||
} | ||||
Michal Klocek
|
r2111 | } | ||
Jani Honkonen
|
r2131 | |||
Michal Klocek
|
r223 | for (int i = 0; i < count; ++i) { | ||
Michal Klocek
|
r2273 | QGraphicsLineItem* arrow = new QGraphicsLineItem(this); | ||
arrow->setPen(m_axis->linePen()); | ||||
Jani Honkonen
|
r2277 | QGraphicsLineItem* grid = new QGraphicsLineItem(this); | ||
grid->setPen(m_axis->gridLinePen()); | ||||
QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(this); | ||||
label->setFont(m_axis->labelsFont()); | ||||
label->setPen(m_axis->labelsPen()); | ||||
label->setBrush(m_axis->labelsBrush()); | ||||
m_arrow->addToGroup(arrow); | ||||
Michal Klocek
|
r2273 | m_grid->addToGroup(grid); | ||
Marek Rosa
|
r2221 | m_labels->addToGroup(label); | ||
Michal Klocek
|
r2273 | |||
if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2){ | ||||
Jani Honkonen
|
r2277 | QGraphicsRectItem* shades = new QGraphicsRectItem(this); | ||
shades->setPen(m_axis->shadesPen()); | ||||
shades->setBrush(m_axis->shadesBrush()); | ||||
Michal Klocek
|
r2273 | m_shades->addToGroup(shades); | ||
} | ||||
Michal Klocek
|
r1241 | } | ||
Michal Klocek
|
r2273 | |||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r1006 | void ChartAxis::deleteItems(int count) | ||
Michal Klocek
|
r176 | { | ||
Jani Honkonen
|
r784 | QList<QGraphicsItem *> lines = m_grid->childItems(); | ||
QList<QGraphicsItem *> labels = m_labels->childItems(); | ||||
QList<QGraphicsItem *> shades = m_shades->childItems(); | ||||
Michal Klocek
|
r1729 | QList<QGraphicsItem *> axis = m_arrow->childItems(); | ||
Michal Klocek
|
r176 | |||
Michal Klocek
|
r291 | for (int i = 0; i < count; ++i) { | ||
Jani Honkonen
|
r2131 | if (lines.size() % 2 && lines.size() > 1) | ||
delete(shades.takeLast()); | ||||
Michal Klocek
|
r291 | delete(lines.takeLast()); | ||
delete(labels.takeLast()); | ||||
delete(axis.takeLast()); | ||||
Michal Klocek
|
r272 | } | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r1006 | void ChartAxis::updateLayout(QVector<qreal> &layout) | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r1241 | int diff = m_layoutVector.size() - layout.size(); | ||
Jani Honkonen
|
r2131 | if (diff > 0) | ||
Michal Klocek
|
r1241 | deleteItems(diff); | ||
Jani Honkonen
|
r2131 | else if (diff < 0) | ||
Michal Klocek
|
r1241 | createItems(-diff); | ||
if (m_animation) { | ||||
Jani Honkonen
|
r2131 | switch (presenter()->state()) { | ||
case ChartPresenter::ZoomInState: | ||||
m_animation->setAnimationType(AxisAnimation::ZoomInAnimation); | ||||
m_animation->setAnimationPoint(presenter()->statePoint()); | ||||
break; | ||||
case ChartPresenter::ZoomOutState: | ||||
m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation); | ||||
m_animation->setAnimationPoint(presenter()->statePoint()); | ||||
break; | ||||
case ChartPresenter::ScrollUpState: | ||||
case ChartPresenter::ScrollLeftState: | ||||
m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation); | ||||
break; | ||||
case ChartPresenter::ScrollDownState: | ||||
case ChartPresenter::ScrollRightState: | ||||
m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation); | ||||
break; | ||||
case ChartPresenter::ShowState: | ||||
m_animation->setAnimationType(AxisAnimation::DefaultAnimation); | ||||
break; | ||||
} | ||||
m_animation->setValues(m_layoutVector, layout); | ||||
presenter()->startAnimation(m_animation); | ||||
} else { | ||||
sauimone
|
r745 | setLayout(layout); | ||
Michal Klocek
|
r1241 | updateGeometry(); | ||
Michal Klocek
|
r530 | } | ||
Michal Klocek
|
r85 | } | ||
Michal Klocek
|
r176 | |||
Michal Klocek
|
r2273 | void ChartAxis::setLabelPadding(int padding) | ||
{ | ||||
m_labelPadding = padding; | ||||
} | ||||
bool ChartAxis::isEmpty() | ||||
{ | ||||
return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max()); | ||||
} | ||||
void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid) | ||||
{ | ||||
m_gridRect = grid; | ||||
m_axisRect = axis; | ||||
if (isEmpty()) | ||||
return; | ||||
QVector<qreal> layout = calculateLayout(); | ||||
updateLayout(layout); | ||||
} | ||||
qreal ChartAxis::min() const | ||||
Michal Klocek
|
r184 | { | ||
Jani Honkonen
|
r2277 | return m_axis->d_ptr->min(); | ||
Michal Klocek
|
r184 | } | ||
Michal Klocek
|
r2273 | qreal ChartAxis::max() const | ||
Michal Klocek
|
r184 | { | ||
Jani Honkonen
|
r2277 | return m_axis->d_ptr->max(); | ||
Michal Klocek
|
r1729 | } | ||
Michal Klocek
|
r2273 | QFont ChartAxis::font() const | ||
Michal Klocek
|
r1729 | { | ||
Jani Honkonen
|
r2277 | return m_axis->labelsFont(); | ||
Michal Klocek
|
r184 | } | ||
Michal Klocek
|
r2273 | QFont ChartAxis::titleFont() const | ||
Michal Klocek
|
r176 | { | ||
Jani Honkonen
|
r2277 | return m_axis->titleFont(); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r2273 | QString ChartAxis::titleText() const | ||
Michal Klocek
|
r176 | { | ||
Jani Honkonen
|
r2277 | return m_axis->titleText(); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r2273 | void ChartAxis::axisSelected() | ||
Michal Klocek
|
r1729 | { | ||
Jani Honkonen
|
r2277 | emit clicked(); | ||
Michal Klocek
|
r1729 | } | ||
Michal Klocek
|
r2273 | Qt::Orientation ChartAxis::orientation() const | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r2273 | return m_axis->orientation(); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r2273 | Qt::Alignment ChartAxis::alignment() const | ||
{ | ||||
return m_axis->alignment(); | ||||
} | ||||
void ChartAxis::setLabels(const QStringList &labels) | ||||
{ | ||||
m_labelsList = labels; | ||||
} | ||||
QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r2273 | Q_UNUSED(which); | ||
Q_UNUSED(constraint); | ||||
return QSizeF(); | ||||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r2273 | //handlers | ||
void ChartAxis::handleArrowVisibleChanged(bool visible) | ||||
Michal Klocek
|
r1729 | { | ||
Michal Klocek
|
r2273 | m_arrow->setVisible(visible); | ||
Michal Klocek
|
r1729 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleGridVisibleChanged(bool visible) | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r2273 | m_grid->setVisible(visible); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r85 | |||
Michal Klocek
|
r2273 | void ChartAxis::handleLabelsVisibleChanged(bool visible) | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r2273 | m_labels->setVisible(visible); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r2273 | void ChartAxis::handleShadesVisibleChanged(bool visible) | ||
Michal Klocek
|
r1729 | { | ||
m_shades->setVisible(visible); | ||||
} | ||||
Michal Klocek
|
r2273 | void ChartAxis::handleLabelsAngleChanged(int angle) | ||
Michal Klocek
|
r176 | { | ||
Jani Honkonen
|
r2131 | foreach (QGraphicsItem *item, m_labels->childItems()) | ||
item->setRotation(angle); | ||||
Michal Klocek
|
r176 | |||
Jani Honkonen
|
r2131 | m_labelsAngle = angle; | ||
Michal Klocek
|
r67 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleLabelsPenChanged(const QPen &pen) | ||
Michal Klocek
|
r140 | { | ||
Jani Honkonen
|
r2131 | foreach (QGraphicsItem *item , m_labels->childItems()) | ||
static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen); | ||||
Michal Klocek
|
r140 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleLabelsBrushChanged(const QBrush &brush) | ||
Michal Klocek
|
r140 | { | ||
Jani Honkonen
|
r2131 | foreach (QGraphicsItem *item , m_labels->childItems()) | ||
static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush); | ||||
Michal Klocek
|
r140 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleLabelsFontChanged(const QFont &font) | ||
Michal Klocek
|
r140 | { | ||
Jani Honkonen
|
r2277 | foreach (QGraphicsItem *item , m_labels->childItems()) | ||
static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font); | ||||
QGraphicsLayoutItem::updateGeometry(); | ||||
presenter()->layout()->invalidate(); | ||||
Michal Klocek
|
r140 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleShadesBrushChanged(const QBrush &brush) | ||
Michal Klocek
|
r140 | { | ||
Jani Honkonen
|
r2131 | foreach (QGraphicsItem *item , m_shades->childItems()) | ||
static_cast<QGraphicsRectItem *>(item)->setBrush(brush); | ||||
Michal Klocek
|
r140 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleShadesPenChanged(const QPen &pen) | ||
Michal Klocek
|
r140 | { | ||
Jani Honkonen
|
r2131 | foreach (QGraphicsItem *item , m_shades->childItems()) | ||
static_cast<QGraphicsRectItem *>(item)->setPen(pen); | ||||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleArrowPenChanged(const QPen &pen) | ||
Michal Klocek
|
r184 | { | ||
Jani Honkonen
|
r2131 | foreach (QGraphicsItem *item , m_arrow->childItems()) | ||
static_cast<QGraphicsLineItem *>(item)->setPen(pen); | ||||
Michal Klocek
|
r184 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleGridPenChanged(const QPen &pen) | ||
Michal Klocek
|
r176 | { | ||
Jani Honkonen
|
r2131 | foreach (QGraphicsItem *item , m_grid->childItems()) | ||
static_cast<QGraphicsLineItem *>(item)->setPen(pen); | ||||
Michal Klocek
|
r2105 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleTitleTextChanged(const QString &title) | ||
Michal Klocek
|
r497 | { | ||
Jani Honkonen
|
r2277 | Q_UNUSED(title) | ||
QGraphicsLayoutItem::updateGeometry(); | ||||
presenter()->layout()->invalidate(); | ||||
Michal Klocek
|
r497 | } | ||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r2273 | void ChartAxis::handleTitlePenChanged(const QPen &pen) | ||
Michal Klocek
|
r2153 | { | ||
m_title->setPen(pen); | ||||
} | ||||
Michal Klocek
|
r2273 | void ChartAxis::handleTitleBrushChanged(const QBrush &brush) | ||
Michal Klocek
|
r2153 | { | ||
m_title->setBrush(brush); | ||||
} | ||||
Michal Klocek
|
r2273 | void ChartAxis::handleTitleFontChanged(const QFont &font) | ||
Michal Klocek
|
r2153 | { | ||
if(m_title->font() != font){ | ||||
m_title->setFont(font); | ||||
QGraphicsLayoutItem::updateGeometry(); | ||||
presenter()->layout()->invalidate(); | ||||
} | ||||
} | ||||
Michal Klocek
|
r2273 | void ChartAxis::handleVisibleChanged(bool visible) | ||
Michal Klocek
|
r1729 | { | ||
Jani Honkonen
|
r2277 | setVisible(visible); | ||
Michal Klocek
|
r1729 | } | ||
Michal Klocek
|
r2273 | void ChartAxis::handleRangeChanged(qreal min, qreal max) | ||
Michal Klocek
|
r439 | { | ||
Jani Honkonen
|
r2277 | Q_UNUSED(min); | ||
Q_UNUSED(max); | ||||
Michal Klocek
|
r1534 | |||
Jani Honkonen
|
r2277 | if (!isEmpty()) { | ||
Marek Rosa
|
r2093 | |||
Jani Honkonen
|
r2277 | QVector<qreal> layout = calculateLayout(); | ||
updateLayout(layout); | ||||
QSizeF before = effectiveSizeHint(Qt::PreferredSize); | ||||
QSizeF after = sizeHint(Qt::PreferredSize); | ||||
Michal Klocek
|
r439 | |||
Jani Honkonen
|
r2277 | if (before != after) { | ||
QGraphicsLayoutItem::updateGeometry(); | ||||
//we don't want to call invalidate on layout, since it will change minimum size of component, | ||||
//which we would like to avoid since it causes nasty flips when scrolling or zooming, | ||||
//instead recalculate layout and use plotArea for extra space. | ||||
presenter()->layout()->setGeometry(presenter()->layout()->geometry()); | ||||
} | ||||
} | ||||
Michal Klocek
|
r1890 | |||
Michal Klocek
|
r1965 | } | ||
Michal Klocek
|
r2273 | //helpers | ||
Michal Klocek
|
r2111 | |||
Michal Klocek
|
r2273 | QStringList ChartAxis::createValueLabels(qreal min, qreal max, int ticks,const QString& format) | ||
Michal Klocek
|
r2111 | { | ||
Michal Klocek
|
r2273 | //TODO: Q_ASSERT(m_max > m_min); | ||
//TODO: Q_ASSERT(ticks > 1); | ||||
Michal Klocek
|
r2111 | |||
QStringList labels; | ||||
Michal Klocek
|
r2273 | if(max <= min || ticks < 1){ | ||
return labels; | ||||
} | ||||
Michal Klocek
|
r2111 | |||
Michal Klocek
|
r2273 | int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0); | ||
n++; | ||||
Michal Klocek
|
r2111 | |||
Jani Honkonen
|
r2131 | if (format.isNull()) { | ||
for (int i = 0; i < ticks; i++) { | ||||
Michal Klocek
|
r2273 | qreal value = min + (i * (max - min) / (ticks - 1)); | ||
Jani Honkonen
|
r2131 | labels << QString::number(value, 'f', n); | ||
Michal Klocek
|
r2111 | } | ||
Jani Honkonen
|
r2131 | } else { | ||
Jani Honkonen
|
r2241 | QByteArray array = format.toLatin1(); | ||
Jani Honkonen
|
r2131 | for (int i = 0; i < ticks; i++) { | ||
Michal Klocek
|
r2273 | qreal value = min + (i * (max - min) / (ticks - 1)); | ||
Marek Rosa
|
r2268 | if (format.contains("d") | ||
|| format.contains("i") | ||||
|| format.contains("c")) | ||||
labels << QString().sprintf(array, (qint64)value); | ||||
else if (format.contains("u") | ||||
|| format.contains("o") | ||||
|| format.contains("x", Qt::CaseInsensitive)) | ||||
labels << QString().sprintf(array, (quint64)value); | ||||
Marek Rosa
|
r2269 | else if (format.contains("f", Qt::CaseInsensitive) | ||
|| format.contains("e", Qt::CaseInsensitive) | ||||
|| format.contains("g", Qt::CaseInsensitive)) | ||||
Marek Rosa
|
r2268 | labels << QString().sprintf(array, value); | ||
Michal Klocek
|
r2111 | } | ||
} | ||||
return labels; | ||||
} | ||||
Marek Rosa
|
r2274 | QStringList ChartAxis::createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format) | ||
{ | ||||
// Q_ASSERT(m_max > m_min); | ||||
// Q_ASSERT(ticks > 1); | ||||
QStringList labels; | ||||
int n = 0; | ||||
if (ticks > 1) | ||||
n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0); | ||||
n++; | ||||
int firstTick; | ||||
if (base > 1) | ||||
Marek Rosa
|
r2275 | firstTick = ceil(log10(min) / log10(base)); | ||
Marek Rosa
|
r2274 | else | ||
Marek Rosa
|
r2275 | firstTick = ceil(log10(max) / log10(base)); | ||
Marek Rosa
|
r2274 | |||
if (format.isNull()) { | ||||
for (int i = firstTick; i < ticks + firstTick; i++) { | ||||
qreal value = qPow(base, i); | ||||
labels << QString::number(value, 'f', n); | ||||
} | ||||
} else { | ||||
Jani Honkonen
|
r2277 | QByteArray array = format.toLatin1(); | ||
Marek Rosa
|
r2274 | for (int i = firstTick; i < ticks + firstTick; i++) { | ||
qreal value = qPow(base, i); | ||||
Marek Rosa
|
r2293 | if (format.contains("d") | ||
|| format.contains("i") | ||||
|| format.contains("c")) | ||||
labels << QString().sprintf(array, (qint64)value); | ||||
else if (format.contains("u") | ||||
|| format.contains("o") | ||||
|| format.contains("x", Qt::CaseInsensitive)) | ||||
labels << QString().sprintf(array, (quint64)value); | ||||
else if (format.contains("f", Qt::CaseInsensitive) | ||||
|| format.contains("e", Qt::CaseInsensitive) | ||||
|| format.contains("g", Qt::CaseInsensitive)) | ||||
labels << QString().sprintf(array, value); | ||||
Marek Rosa
|
r2274 | } | ||
} | ||||
return labels; | ||||
} | ||||
Michal Klocek
|
r2273 | QStringList ChartAxis::createDateTimeLabels(qreal min, qreal max,int ticks,const QString& format) | ||
Michal Klocek
|
r2111 | { | ||
Michal Klocek
|
r2273 | //TODO: Q_ASSERT(m_max > m_min); | ||
//TODO: Q_ASSERT(ticks > 1); | ||||
Michal Klocek
|
r2111 | QStringList labels; | ||
Michal Klocek
|
r2273 | |||
if(max <= min || ticks < 1) { | ||||
return labels; | ||||
} | ||||
int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0); | ||||
Michal Klocek
|
r2111 | n++; | ||
Jani Honkonen
|
r2131 | for (int i = 0; i < ticks; i++) { | ||
Michal Klocek
|
r2273 | qreal value = min + (i * (max - min) / (ticks - 1)); | ||
Michal Klocek
|
r2111 | labels << QDateTime::fromMSecsSinceEpoch(value).toString(format); | ||
} | ||||
return labels; | ||||
} | ||||
Michal Klocek
|
r1006 | #include "moc_chartaxis_p.cpp" | ||
Michal Klocek
|
r67 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||