From 9455cddd70de114487f352f012f6cf49cf850184 2012-03-07 15:06:17 From: Michal Klocek Date: 2012-03-07 15:06:17 Subject: [PATCH] Adds draft of axis bar label support --- diff --git a/src/axis/axisitem.cpp b/src/axis/axisitem.cpp index 8743fb2..4022dbf 100644 --- a/src/axis/axisitem.cpp +++ b/src/axis/axisitem.cpp @@ -25,6 +25,7 @@ m_axis(parent) setFlags(QGraphicsItem::ItemHasNoContents); QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated())); + QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated())); } AxisItem::~AxisItem() @@ -95,21 +96,19 @@ void AxisItem::updateItem() QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) { - - Q_ASSERT(max>=min); QStringList labels; - //int ticks = axis->ticksCount()-1; + QChartAxisCategories* categories = m_chartAxis->categories(); for(int i=0; i<= ticks; i++) { qreal value = min + (i * (max - min)/ ticks); - QString label ;//= axis->axisTickLabel(value); - if(label.isEmpty()) { + if(categories->count()==0) { labels << QString::number(value); } else { + QString label = categories->label(value); labels << label; } } @@ -270,7 +269,7 @@ void AxisItem::setLayout(const QVector& layout) QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); - if(i%2){ + if(i%2 && i+1 < layout.size()){ QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height()); } @@ -293,7 +292,7 @@ void AxisItem::setLayout(const QVector& layout) QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); - if(i%2){ + if(i%2 && i+1 < layout.size()){ QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]); } @@ -312,8 +311,21 @@ void AxisItem::setLayout(const QVector& layout) //handlers +void AxisItem::handleAxisCategoriesUpdated() +{ + updateItem(); +} + void AxisItem::handleAxisUpdated() { + + int count = m_chartAxis->ticksCount(); + + if(m_ticks!=count){ + m_ticks=count; + updateItem(); + } + if(isEmpty()) return; if(m_chartAxis->isAxisVisible()) { @@ -367,14 +379,6 @@ void AxisItem::handleRangeChanged(qreal min, qreal max) } -void AxisItem::handleTicksCountChanged(int ticks) -{ - m_ticks=ticks; - - if(isEmpty()) return; - updateItem(); -} - void AxisItem::handleGeometryChanged(const QRectF& rect) { m_rect = rect; diff --git a/src/axis/axisitem_p.h b/src/axis/axisitem_p.h index 37348ff..d19d7ff 100644 --- a/src/axis/axisitem_p.h +++ b/src/axis/axisitem_p.h @@ -51,8 +51,8 @@ public: public slots: void handleAxisUpdated();//qchartaxis update calls + void handleAxisCategoriesUpdated();//qchartaxis update calls void handleRangeChanged(qreal min , qreal max); //domain update calls - void handleTicksCountChanged(int ticks); //ticks changed void handleGeometryChanged(const QRectF& size); //geometry update calls public: @@ -66,6 +66,7 @@ private: void clear(int count); void createItems(int count); QStringList createLabels(int ticks, qreal min, qreal max); + private: QChartAxis* m_chartAxis; AxisType m_type; diff --git a/src/axis/qchartaxis.h b/src/axis/qchartaxis.h index 1e564da..2977187 100644 --- a/src/axis/qchartaxis.h +++ b/src/axis/qchartaxis.h @@ -60,7 +60,7 @@ public: void setTicksCount(int count); int ticksCount() const { return m_ticksCount;} - QChartAxisCategories& categories() { return m_category; } + QChartAxisCategories* categories() { return &m_category; } signals: void minChanged(qreal min); diff --git a/src/axis/qchartaxiscategories.cpp b/src/axis/qchartaxiscategories.cpp index 9a93190..55cfec0 100644 --- a/src/axis/qchartaxiscategories.cpp +++ b/src/axis/qchartaxiscategories.cpp @@ -34,6 +34,11 @@ int QChartAxisCategories::count() emit updated(); } +QString QChartAxisCategories::label(qreal value) const +{ + return m_map.value(value); +} + #include "moc_qchartaxiscategories.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/qchartaxiscategories.h b/src/axis/qchartaxiscategories.h index 16261c6..e77316f 100644 --- a/src/axis/qchartaxiscategories.h +++ b/src/axis/qchartaxiscategories.h @@ -14,6 +14,7 @@ public: void insert(qreal value,QString label); void remove(qreal value); + QString label(qreal value) const; void clear(); int count(); diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 5623fde..4be1618 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -100,21 +100,21 @@ void BarPresenterBase::initAxisLabels() if (0 == count) { return; } + count++; mChart->axisX()->setTicksCount(count); qreal min = 0; - qreal max = mSeries->categoryCount(); + qreal max = count; mChart->axisX()->setMin(min); mChart->axisX()->setMax(max); - qreal step = (max-min)/count; - QChartAxisCategories& categories = mChart->axisX()->categories(); - categories.clear(); - for (int i=0; icategoryName(i); - categories.insert(min,mSeries->categoryName(i)); - min += step; + min++; + QChartAxisCategories* categories = mChart->axisX()->categories(); + categories->clear(); + for (int i=0; iinsert(min,mSeries->categoryName(i)); + min++; } mChart->axisX()->setLabelsVisible(true); } diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index e8a08ee..1052294 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -109,13 +109,12 @@ void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); //initialize item->handleRangeChanged(domain->minX(),domain->maxX()); - item->handleTicksCountChanged(4); } else{ QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); //initialize item->handleRangeChanged(domain->minY(),domain->maxY()); - item->handleTicksCountChanged(4); + } QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));