##// END OF EJS Templates
Fixed layout for barcharts
sauimone -
r473:8959911f7807
parent child
Show More
@@ -10,7 +10,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) :
11 BarPresenterBase(series, parent)
11 BarPresenterBase(series, parent)
12 {
12 {
13 mBarDefaultWidth = 5;
13 mBarWidth = 5;
14 }
14 }
15
15
16 void BarPresenter::layoutChanged()
16 void BarPresenter::layoutChanged()
@@ -27,8 +27,7 void BarPresenter::layoutChanged()
27 return;
27 return;
28 }
28 }
29
29
30 // TODO: better way to auto-layout?
30 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
31 // Use reals for accurancy (we might get some compiler warnings... :)
32 int categoryCount = mSeries->categoryCount();
31 int categoryCount = mSeries->categoryCount();
33 int setCount = mSeries->barsetCount();
32 int setCount = mSeries->barsetCount();
34
33
@@ -37,38 +36,38 void BarPresenter::layoutChanged()
37 qreal tM = mSeries->max();
36 qreal tM = mSeries->max();
38 qreal scale = (tH/tM);
37 qreal scale = (tH/tM);
39 qreal tC = categoryCount+1;
38 qreal tC = categoryCount + 1;
40 qreal xStepPerSet = (tW/tC);
39 mBarWidth = tW / ((categoryCount * setCount) + tC);
40 qreal xStepPerCategory = (tW/tC) + mBarWidth;
41
41
42 // Scaling.
43 int itemIndex(0);
42 int itemIndex(0);
44 int labelIndex(0);
43 int labelIndex(0);
45
44
46 for (int category=0; category < categoryCount; category++) {
45 for (int category=0; category < categoryCount; category++) {
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
46 qreal xPos = xStepPerCategory * category;
48 qreal yPos = mHeight;
47 qreal yPos = mHeight;
49 for (int set = 0; set < setCount; set++) {
48 for (int set = 0; set < setCount; set++) {
50 qreal barHeight = mSeries->valueAt(set,category) * scale;
49 qreal barHeight = mSeries->valueAt(set,category) * scale;
51 Bar* bar = mBars.at(itemIndex);
50 Bar* bar = mBars.at(itemIndex);
52
51
53 // TODO: width settable per bar?
52 // TODO: width settable per bar?
54 bar->resize(mBarDefaultWidth, barHeight);
53 bar->resize(mBarWidth, barHeight);
55 bar->setBrush(mSeries->barsetAt(set)->brush());
54 bar->setBrush(mSeries->barsetAt(set)->brush());
56 bar->setPos(xPos, yPos-barHeight);
55 bar->setPos(xPos, yPos-barHeight);
57 itemIndex++;
56 itemIndex++;
58 xPos += mBarDefaultWidth;
57 xPos += mBarWidth;
59 }
58 }
60
59
61 // TODO: Layout for labels, remove magic number
60 // TODO: Layout for labels, remove magic number
62 xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
61 xPos = xStepPerCategory * category + mBarWidth/2;
63 BarLabel* label = mLabels.at(labelIndex);
62 BarLabel* label = mLabels.at(labelIndex);
64 label->setPos(xPos, mHeight + 20);
63 label->setPos(xPos, mHeight - 20);
65 labelIndex++;
64 labelIndex++;
66 }
65 }
67
66
68 // Position floating values
67 // Position floating values
69 itemIndex = 0;
68 itemIndex = 0;
70 for (int category=0; category < mSeries->categoryCount(); category++) {
69 for (int category=0; category < mSeries->categoryCount(); category++) {
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
70 qreal xPos = xStepPerCategory * category + mBarWidth/2;
72 qreal yPos = mHeight;
71 qreal yPos = mHeight;
73 for (int set=0; set < mSeries->barsetCount(); set++) {
72 for (int set=0; set < mSeries->barsetCount(); set++) {
74 qreal barHeight = mSeries->valueAt(set,category) * scale;
73 qreal barHeight = mSeries->valueAt(set,category) * scale;
@@ -86,10 +85,9 void BarPresenter::layoutChanged()
86 }
85 }
87
86
88 itemIndex++;
87 itemIndex++;
89 xPos += mBarDefaultWidth;
88 xPos += mBarWidth;
90 }
89 }
91 }
90 }
92 mLayoutDirty = true;
93 }
91 }
94
92
95 #include "moc_barpresenter_p.cpp"
93 #include "moc_barpresenter_p.cpp"
@@ -12,14 +12,12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
14 : ChartItem(parent)
14 : ChartItem(parent)
15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
15 ,mBarWidth(20) // TODO: remove hard coding, when we have layout code ready
16 ,mLayoutSet(false)
16 ,mLayoutSet(false)
17 ,mLayoutDirty(true)
18 ,mSeparatorsEnabled(false)
17 ,mSeparatorsEnabled(false)
19 ,mSeries(series)
18 ,mSeries(series)
20 {
19 {
21 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
20 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
22 // connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
23 dataChanged();
21 dataChanged();
24 }
22 }
25
23
@@ -34,12 +32,9 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *
34 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
32 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
35 return;
33 return;
36 }
34 }
37 // if (mLayoutDirty) {
38 // Layout or data has changed. Need to redraw.
39 foreach(QGraphicsItem* i, childItems()) {
35 foreach(QGraphicsItem* i, childItems()) {
40 i->paint(painter,option,widget);
36 i->paint(painter,option,widget);
41 }
37 }
42 // }
43 }
38 }
44
39
45 QRectF BarPresenterBase::boundingRect() const
40 QRectF BarPresenterBase::boundingRect() const
@@ -49,13 +44,12 QRectF BarPresenterBase::boundingRect() const
49
44
50 void BarPresenterBase::setBarWidth( int w )
45 void BarPresenterBase::setBarWidth( int w )
51 {
46 {
52 mBarDefaultWidth = w;
47 mBarWidth = w;
53 }
48 }
54
49
55 void BarPresenterBase::dataChanged()
50 void BarPresenterBase::dataChanged()
56 {
51 {
57 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
52 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
58 // qDebug() << "datachanged";
59 // Delete old bars
53 // Delete old bars
60 foreach (QGraphicsItem* item, childItems()) {
54 foreach (QGraphicsItem* item, childItems()) {
61 delete item;
55 delete item;
@@ -110,9 +104,6 void BarPresenterBase::dataChanged()
110 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
104 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
111 }
105 }
112 }
106 }
113
114 // TODO: if (autolayout) { layoutChanged() } or something
115 mLayoutDirty = true;
116 }
107 }
117
108
118 //handlers
109 //handlers
@@ -49,17 +49,13 protected:
49 // TODO: consider these.
49 // TODO: consider these.
50 int mHeight; // Layout spesific
50 int mHeight; // Layout spesific
51 int mWidth;
51 int mWidth;
52 int mBarDefaultWidth;
52 qreal mBarWidth;
53
53
54 bool mLayoutSet; // True, if component has been laid out.
54 bool mLayoutSet; // True, if component has been laid out.
55 bool mLayoutDirty;
56
57 bool mSeparatorsEnabled;
55 bool mSeparatorsEnabled;
58
56
59 // Owned
60 QBarSeries* mSeries;
61
62 // Not owned.
57 // Not owned.
58 QBarSeries* mSeries;
63 QList<Bar*> mBars;
59 QList<Bar*> mBars;
64 QList<BarLabel*> mLabels;
60 QList<BarLabel*> mLabels;
65 QList<Separator*> mSeparators;
61 QList<Separator*> mSeparators;
@@ -21,12 +21,12 QString BarValue::valueString()
21 return mValueString;
21 return mValueString;
22 }
22 }
23
23
24 void BarValue::setPen(const QPen& pen)
24 void BarValue::setPen(const QPen pen)
25 {
25 {
26 mPen = pen;
26 mPen = pen;
27 }
27 }
28
28
29 const QPen& BarValue::pen()
29 QPen BarValue::pen() const
30 {
30 {
31 return mPen;
31 return mPen;
32 }
32 }
@@ -20,8 +20,8 public:
20 void setValueString(QString str);
20 void setValueString(QString str);
21 QString valueString();
21 QString valueString();
22
22
23 void setPen(const QPen& pen);
23 void setPen(const QPen pen);
24 const QPen& pen();
24 QPen pen() const;
25
25
26 void resize(qreal w, qreal h);
26 void resize(qreal w, qreal h);
27 void setPos(qreal x, qreal y);
27 void setPos(qreal x, qreal y);
@@ -29,17 +29,17 void PercentBarPresenter::layoutChanged()
29 return;
29 return;
30 }
30 }
31
31
32 // TODO: better way to auto-layout
32 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
33 // Use reals for accurancy (we might get some compiler warnings... :)
34 int count = mSeries->categoryCount();
35 int itemIndex(0);
36 int labelIndex(0);
37 qreal tW = mWidth;
33 qreal tW = mWidth;
38 qreal tC = count+1;
34 qreal tC = mSeries->categoryCount() + 1;
35 qreal cC = mSeries->categoryCount() * 2 + 1;
36 mBarWidth = tW / cC;
39 qreal xStep = (tW/tC);
37 qreal xStep = (tW/tC);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
38 qreal xPos = ((tW/tC) - mBarWidth / 2);
41 qreal h = mHeight;
39 qreal h = mHeight;
42
40
41 int itemIndex(0);
42 int labelIndex(0);
43 for (int category = 0; category < mSeries->categoryCount(); category++) {
43 for (int category = 0; category < mSeries->categoryCount(); category++) {
44 qreal colSum = mSeries->categorySum(category);
44 qreal colSum = mSeries->categorySum(category);
45 qreal scale = (h / colSum);
45 qreal scale = (h / colSum);
@@ -49,7 +49,7 void PercentBarPresenter::layoutChanged()
49 Bar* bar = mBars.at(itemIndex);
49 Bar* bar = mBars.at(itemIndex);
50
50
51 // TODO: width settable per bar?
51 // TODO: width settable per bar?
52 bar->resize(mBarDefaultWidth, barHeight);
52 bar->resize(mBarWidth, barHeight);
53 bar->setBrush(mSeries->barsetAt(set)->brush());
53 bar->setBrush(mSeries->barsetAt(set)->brush());
54 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
55 itemIndex++;
55 itemIndex++;
@@ -74,7 +74,7 void PercentBarPresenter::layoutChanged()
74
74
75 // Position floating values
75 // Position floating values
76 itemIndex = 0;
76 itemIndex = 0;
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
77 xPos = ((tW/tC) - mBarWidth / 2);
78 for (int category=0; category < mSeries->categoryCount(); category++) {
78 for (int category=0; category < mSeries->categoryCount(); category++) {
79 qreal yPos = h;
79 qreal yPos = h;
80 qreal colSum = mSeries->categorySum(category);
80 qreal colSum = mSeries->categorySum(category);
@@ -103,8 +103,6 void PercentBarPresenter::layoutChanged()
103 }
103 }
104 xPos += xStep;
104 xPos += xStep;
105 }
105 }
106
107 mLayoutDirty = true;
108 }
106 }
109
107
110 #include "moc_percentbarpresenter_p.cpp"
108 #include "moc_percentbarpresenter_p.cpp"
@@ -122,7 +122,7 qreal QBarSet::total()
122 /*!
122 /*!
123 Sets pen for set. Bars of this set are drawn using \a pen
123 Sets pen for set. Bars of this set are drawn using \a pen
124 */
124 */
125 void QBarSet::setPen(QPen pen)
125 void QBarSet::setPen(const QPen pen)
126 {
126 {
127 mPen = pen;
127 mPen = pen;
128 }
128 }
@@ -130,7 +130,7 void QBarSet::setPen(QPen pen)
130 /*!
130 /*!
131 Returns pen of the set.
131 Returns pen of the set.
132 */
132 */
133 QPen QBarSet::pen()
133 QPen QBarSet::pen() const
134 {
134 {
135 return mPen;
135 return mPen;
136 }
136 }
@@ -138,7 +138,7 QPen QBarSet::pen()
138 /*!
138 /*!
139 Sets brush for the set. Bars of this set are drawn using \a brush
139 Sets brush for the set. Bars of this set are drawn using \a brush
140 */
140 */
141 void QBarSet::setBrush(QBrush brush)
141 void QBarSet::setBrush(const QBrush brush)
142 {
142 {
143 mBrush = brush;
143 mBrush = brush;
144 }
144 }
@@ -146,7 +146,7 void QBarSet::setBrush(QBrush brush)
146 /*!
146 /*!
147 Returns brush of the set.
147 Returns brush of the set.
148 */
148 */
149 QBrush QBarSet::brush()
149 QBrush QBarSet::brush() const
150 {
150 {
151 return mBrush;
151 return mBrush;
152 }
152 }
@@ -27,11 +27,11 public:
27 //qreal value(QString category);
27 //qreal value(QString category);
28 //void setValue(QString category, qreal value);
28 //void setValue(QString category, qreal value);
29
29
30 void setPen(QPen pen);
30 void setPen(const QPen pen);
31 QPen pen();
31 QPen pen() const;
32
32
33 void setBrush(QBrush brush);
33 void setBrush(const QBrush brush);
34 QBrush brush();
34 QBrush brush() const;
35
35
36 Q_SIGNALS:
36 Q_SIGNALS:
37 void clicked(QString category); // Clicked and hover signals exposed to user
37 void clicked(QString category); // Clicked and hover signals exposed to user
@@ -39,27 +39,26 void StackedBarPresenter::layoutChanged()
39 return;
39 return;
40 }
40 }
41
41
42 // TODO: better way to auto-layout
42 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
43 // Use reals for accurancy (we might get some compiler warnings... :)
44 // TODO: use temp variable for category count...
45 qreal maxSum = mSeries->maxCategorySum();
43 qreal maxSum = mSeries->maxCategorySum();
46 qreal h = mHeight;
44 qreal h = mHeight;
47 qreal scale = (h / maxSum);
45 qreal scale = (h / maxSum);
48
49 int itemIndex(0);
50 int labelIndex(0);
51 qreal tW = mWidth;
46 qreal tW = mWidth;
52 qreal tC = mSeries->categoryCount() + 1;
47 qreal tC = mSeries->categoryCount() + 1;
48 qreal cC = mSeries->categoryCount() * 2 + 1;
49 mBarWidth = tW / cC;
53 qreal xStep = (tW/tC);
50 qreal xStep = (tW/tC);
54 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
51 qreal xPos = ((tW/tC) - mBarWidth / 2);
55
52
53 int itemIndex(0);
54 int labelIndex(0);
56 for (int category = 0; category < mSeries->categoryCount(); category++) {
55 for (int category = 0; category < mSeries->categoryCount(); category++) {
57 qreal yPos = h;
56 qreal yPos = h;
58 for (int set=0; set < mSeries->barsetCount(); set++) {
57 for (int set=0; set < mSeries->barsetCount(); set++) {
59 qreal barHeight = mSeries->valueAt(set, category) * scale;
58 qreal barHeight = mSeries->valueAt(set, category) * scale;
60 Bar* bar = mBars.at(itemIndex);
59 Bar* bar = mBars.at(itemIndex);
61
60
62 bar->resize(mBarDefaultWidth, barHeight);
61 bar->resize(mBarWidth, barHeight);
63 bar->setBrush(mSeries->barsetAt(set)->brush());
62 bar->setBrush(mSeries->barsetAt(set)->brush());
64 bar->setPos(xPos, yPos-barHeight);
63 bar->setPos(xPos, yPos-barHeight);
65 itemIndex++;
64 itemIndex++;
@@ -84,7 +83,7 void StackedBarPresenter::layoutChanged()
84
83
85 // Position floating values
84 // Position floating values
86 itemIndex = 0;
85 itemIndex = 0;
87 xPos = ((tW/tC) - mBarDefaultWidth / 2);
86 xPos = ((tW/tC) - mBarWidth / 2);
88 for (int category=0; category < mSeries->categoryCount(); category++) {
87 for (int category=0; category < mSeries->categoryCount(); category++) {
89 qreal yPos = h;
88 qreal yPos = h;
90 for (int set=0; set < mSeries->barsetCount(); set++) {
89 for (int set=0; set < mSeries->barsetCount(); set++) {
@@ -107,8 +106,6 void StackedBarPresenter::layoutChanged()
107 }
106 }
108 xPos += xStep;
107 xPos += xStep;
109 }
108 }
110
111 mLayoutDirty = true;
112 }
109 }
113
110
114 #include "moc_stackedbarpresenter_p.cpp"
111 #include "moc_stackedbarpresenter_p.cpp"
General Comments 0
You need to be logged in to leave comments. Login now