##// END OF EJS Templates
Labels for barchart to axis
sauimone -
r487:03622a1338c4
parent child
Show More
@@ -138,9 +138,9 int main(int argc, char *argv[])
138 //! [6]
138 //! [6]
139
139
140 // Disable axis, since they don't really apply to bar chart
140 // Disable axis, since they don't really apply to bar chart
141 drilldownChart->axisX()->setAxisVisible(false);
141 // drilldownChart->axisX()->setAxisVisible(false);
142 drilldownChart->axisX()->setGridVisible(false);
142 drilldownChart->axisX()->setGridVisible(false);
143 drilldownChart->axisX()->setLabelsVisible(false);
143 // drilldownChart->axisX()->setLabelsVisible(false);
144
144
145 window.setCentralWidget(drilldownChart);
145 window.setCentralWidget(drilldownChart);
146 window.resize(400, 300);
146 window.resize(400, 300);
@@ -7,10 +7,9
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) :
11 BarPresenterBase(series, parent)
11 BarPresenterBase(series, parent)
12 {
12 {
13 mBarWidth = 5;
14 }
13 }
15
14
16 void BarPresenter::layoutChanged()
15 void BarPresenter::layoutChanged()
@@ -14,7 +14,7 class BarPresenter : public BarPresenterBase
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 explicit BarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
17 explicit BarPresenter(QBarSeries *series, QChart *parent = 0);
18
18
19 private:
19 private:
20
20
@@ -5,19 +5,23
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include "qbarseries.h"
7 #include "qbarseries.h"
8 #include "qchart.h"
9 #include "qchartaxis.h"
10 #include "qchartaxiscategories.h"
8 #include <QDebug>
11 #include <QDebug>
9 #include <QToolTip>
12 #include <QToolTip>
10
13
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
15
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
16 BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent)
14 : ChartItem(parent)
17 : ChartItem(parent)
15 ,mBarWidth(20) // TODO: remove hard coding, when we have layout code ready
16 ,mLayoutSet(false)
18 ,mLayoutSet(false)
17 ,mSeparatorsEnabled(false)
19 ,mSeparatorsEnabled(false)
18 ,mSeries(series)
20 ,mSeries(series)
21 ,mChart(parent)
19 {
22 {
20 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
23 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
24 initAxisLabels();
21 dataChanged();
25 dataChanged();
22 }
26 }
23
27
@@ -42,11 +46,6 QRectF BarPresenterBase::boundingRect() const
42 return QRectF(0,0,mWidth,mHeight);
46 return QRectF(0,0,mWidth,mHeight);
43 }
47 }
44
48
45 void BarPresenterBase::setBarWidth( int w )
46 {
47 mBarWidth = w;
48 }
49
50 void BarPresenterBase::dataChanged()
49 void BarPresenterBase::dataChanged()
51 {
50 {
52 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
51 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
@@ -76,6 +75,7 void BarPresenterBase::dataChanged()
76 }
75 }
77
76
78 // Create labels
77 // Create labels
78 /*
79 int count = mSeries->categoryCount();
79 int count = mSeries->categoryCount();
80 for (int i=0; i<count; i++) {
80 for (int i=0; i<count; i++) {
81 BarLabel* label = new BarLabel(this);
81 BarLabel* label = new BarLabel(this);
@@ -83,9 +83,10 void BarPresenterBase::dataChanged()
83 childItems().append(label);
83 childItems().append(label);
84 mLabels.append(label);
84 mLabels.append(label);
85 }
85 }
86 */
86
87
87 // Create separators
88 // Create separators
88 count = mSeries->categoryCount() - 1; // There is one less separator than columns
89 int count = mSeries->categoryCount() - 1; // There is one less separator than columns
89 for (int i=0; i<count; i++) {
90 for (int i=0; i<count; i++) {
90 Separator* sep = new Separator(this);
91 Separator* sep = new Separator(this);
91 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
92 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
@@ -106,6 +107,31 void BarPresenterBase::dataChanged()
106 }
107 }
107 }
108 }
108
109
110 void BarPresenterBase::initAxisLabels()
111 {
112 int count = mSeries->categoryCount();
113 if (0 == count) {
114 return;
115 }
116
117 mChart->axisX()->setTicksCount(count);
118
119 qreal min = 0;
120 qreal max = mSeries->categoryCount();
121
122 mChart->axisX()->setMin(min);
123 mChart->axisX()->setMax(max);
124 qreal step = (max-min)/count;
125 QChartAxisCategories& categories = mChart->axisX()->categories();
126 categories.clear();
127 for (int i=0; i<count; i++) {
128 qDebug() << "initAxisLabels" << min << mSeries->categoryName(i);
129 categories.insert(min,mSeries->categoryName(i));
130 min += step;
131 }
132 mChart->axisX()->setLabelsVisible(true);
133 }
134
109 //handlers
135 //handlers
110
136
111 void BarPresenterBase::handleModelChanged(int index)
137 void BarPresenterBase::handleModelChanged(int index)
@@ -116,10 +142,24 void BarPresenterBase::handleModelChanged(int index)
116
142
117 void BarPresenterBase::handleDomainChanged(const Domain& domain)
143 void BarPresenterBase::handleDomainChanged(const Domain& domain)
118 {
144 {
119 // qDebug() << "BarPresenterBase::handleDomainChanged";
145 qDebug() << "BarPresenterBase::handleDomainChanged";
120 // TODO: Figure out the use case for this.
146 /*
121 // Affects the size of visible item, so layout is changed.
147 int count = mSeries->categoryCount();
122 // layoutChanged();
148 if (0 == count) {
149 return;
150 }
151
152 // Position labels to domain
153 qreal min = domain.minX();
154 qreal max = domain.maxX();
155 qreal step = (max-min)/count;
156 QChartAxisCategories& categories = mChart->axisX()->categories();
157 categories.clear();
158 for (int i=0; i<count; i++) {
159 categories.insert(min,mSeries->categoryName(i));
160 min += step;
161 }
162 */
123 }
163 }
124
164
125 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
165 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
@@ -13,6 +13,8 class Bar;
13 class BarLabel;
13 class BarLabel;
14 class Separator;
14 class Separator;
15 class BarValue;
15 class BarValue;
16 class QChartAxisCategories;
17 class QChart;
16
18
17 // Common implemantation of different presenters. Not to be instantiated.
19 // Common implemantation of different presenters. Not to be instantiated.
18 // TODO: combine this with BarPresenter and derive other presenters from it?
20 // TODO: combine this with BarPresenter and derive other presenters from it?
@@ -20,7 +22,7 class BarPresenterBase : public QObject, public ChartItem
20 {
22 {
21 Q_OBJECT
23 Q_OBJECT
22 public:
24 public:
23 BarPresenterBase(QBarSeries *series, QGraphicsItem *parent = 0);
25 BarPresenterBase(QBarSeries *series, QChart *parent = 0);
24 virtual ~BarPresenterBase();
26 virtual ~BarPresenterBase();
25
27
26 public:
28 public:
@@ -28,13 +30,13 public:
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
31 QRectF boundingRect() const;
30
32
31 // TODO: these may change with layout awarness.
32 void setBarWidth( int w );
33
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
33 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
34 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
35 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
37
36
37 protected:
38 void initAxisLabels();
39
38 public slots:
40 public slots:
39 void handleModelChanged(int index);
41 void handleModelChanged(int index);
40 void handleDomainChanged(const Domain& domain);
42 void handleDomainChanged(const Domain& domain);
@@ -60,7 +62,7 protected:
60 QList<BarLabel*> mLabels;
62 QList<BarLabel*> mLabels;
61 QList<Separator*> mSeparators;
63 QList<Separator*> mSeparators;
62 QList<BarValue*> mFloatingValues;
64 QList<BarValue*> mFloatingValues;
63
65 QChart* mChart;
64 };
66 };
65
67
66 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
@@ -9,7 +9,7
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11
11
12 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
12 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) :
13 BarPresenterBase(series, parent)
13 BarPresenterBase(series, parent)
14 {
14 {
15 }
15 }
@@ -13,7 +13,7 class PercentBarPresenter : public BarPresenterBase
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
16 PercentBarPresenter(QBarSeries *series, QChart *parent = 0);
17
17
18 private:
18 private:
19
19
@@ -118,6 +118,7 QString QBarSeries::categoryName(int category)
118 */
118 */
119 void QBarSeries::setToolTipEnabled(bool enabled)
119 void QBarSeries::setToolTipEnabled(bool enabled)
120 {
120 {
121 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
121 if (enabled) {
122 if (enabled) {
122 for (int i=0; i<mModel->barsetCount(); i++) {
123 for (int i=0; i<mModel->barsetCount(); i++) {
123 QBarSet *set = mModel->setAt(i);
124 QBarSet *set = mModel->setAt(i);
@@ -8,7 +8,7
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QChart *parent) :
12 BarPresenterBase(series,parent)
12 BarPresenterBase(series,parent)
13 {
13 {
14 }
14 }
@@ -66,8 +66,8 void StackedBarPresenter::layoutChanged()
66 }
66 }
67
67
68 // TODO: Layout for labels, remove magic number
68 // TODO: Layout for labels, remove magic number
69 BarLabel* label = mLabels.at(labelIndex);
69 // BarLabel* label = mLabels.at(labelIndex);
70 label->setPos(xPos, mHeight + 20);
70 // label->setPos(xPos, mHeight + 20);
71 labelIndex++;
71 labelIndex++;
72 xPos += xStep;
72 xPos += xStep;
73 }
73 }
@@ -11,7 +11,7 class StackedBarPresenter : public BarPresenterBase
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
14 StackedBarPresenter(QBarSeries *series, QChart *parent = 0);
15 ~StackedBarPresenter();
15 ~StackedBarPresenter();
16
16
17 private:
17 private:
General Comments 0
You need to be logged in to leave comments. Login now