##// END OF EJS Templates
brush support for bargroups
sauimone -
r183:45734e367adb
parent child
Show More
@@ -27,6 +27,7 void Bar::setColor( QColor col )
27 {
27 {
28 mColor = col;
28 mColor = col;
29 }
29 }
30
30 void Bar::setPos(qreal x, qreal y)
31 void Bar::setPos(qreal x, qreal y)
31 {
32 {
32 // qDebug() << "Bar::setpos" << x << y;
33 // qDebug() << "Bar::setpos" << x << y;
@@ -34,14 +35,25 void Bar::setPos(qreal x, qreal y)
34 mYpos = y;
35 mYpos = y;
35 }
36 }
36
37
38 void Bar::setPen(QPen pen)
39 {
40 mPen = pen;
41 }
42
43 void Bar::setBrush(QBrush brush)
44 {
45 mBrush = brush;
46 }
47
37 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
48 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
38 {
49 {
39 if (0 == mHeight) {
50 if (0 == mHeight) {
40 return;
51 return;
41 }
52 }
42 // TODO: accept brush instead of color
53 // TODO: accept brush instead of color
43 QBrush brush(mColor);
54 painter->setBrush(mBrush);
44 painter->setBrush(brush);
55 // QBrush brush(mColor);
56 // painter->setBrush(brush);
45
57
46 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
58 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
47 int x0 = mXpos;
59 int x0 = mXpos;
@@ -4,6 +4,8
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qchartglobal.h"
5 #include "qchartglobal.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7 #include <QPen>
8 #include <QBrush>
7
9
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
11
@@ -17,9 +19,11 public: // from ChartItem
17 void setSize(const QSizeF &size);
19 void setSize(const QSizeF &size);
18
20
19 // Layout Stuff
21 // Layout Stuff
20 void resize( qreal w, qreal h ); // Size of bar.
22 void resize(qreal w, qreal h); // Size of bar.
21 void setColor( QColor col ); // Color of bar
22 void setPos(qreal x, qreal y);
23 void setPos(qreal x, qreal y);
24 void setPen(QPen pen);
25 void setBrush(QBrush brush);
26 void setColor( QColor col); // deprecated
23
27
24 public:
28 public:
25 // From QGraphicsItem
29 // From QGraphicsItem
@@ -35,6 +39,9 private:
35 qreal mYpos;
39 qreal mYpos;
36 QColor mColor;
40 QColor mColor;
37
41
42 QBrush mBrush;
43 QPen mPen;
44
38 };
45 };
39
46
40 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
@@ -29,36 +29,39 void BarGroup::layoutChanged()
29 // TODO: better way to auto-layout?
29 // TODO: better way to auto-layout?
30 // Use reals for accurancy (we might get some compiler warnings... :)
30 // Use reals for accurancy (we might get some compiler warnings... :)
31 int itemCount = mModel.countCategories();
31 int itemCount = mModel.countCategories();
32 int seriesCount = mModel.countSets();
32 int setCount = mModel.countSets();
33
33
34 qreal tW = mWidth;
34 qreal tW = mWidth;
35 qreal tH = mHeight;
35 qreal tH = mHeight;
36 qreal tM = mModel.max();
36 qreal tM = mModel.max();
37 qreal scale = (tH/tM);
37 qreal scale = (tH/tM);
38 qreal tC = itemCount+1;
38 qreal tC = itemCount+1;
39 qreal xStepPerSeries = (tW/tC);
39 qreal xStepPerSet = (tW/tC);
40
40
41 // Scaling.
41 // Scaling.
42 int itemIndex(0);
42 int itemIndex(0);
43 int labelIndex = itemCount * seriesCount;
43 int labelIndex = itemCount * setCount;
44
44
45 for (int item=0; item < itemCount; item++) {
45 for (int item=0; item < itemCount; item++) {
46 qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
46 qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2));
47 qreal yPos = mHeight;
47 qreal yPos = mHeight;
48 for (int series = 0; series < seriesCount; series++) {
48 for (int set = 0; set < setCount; set++) {
49 qreal barHeight = mModel.valueAt(series, item) * scale;
49 qreal barHeight = mModel.valueAt(set, item) * scale;
50 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
50 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
51
51
52 // TODO: width settable per bar?
52 // TODO: width settable per bar?
53 bar->resize(mBarDefaultWidth, barHeight);
53 bar->resize(mBarDefaultWidth, barHeight);
54 bar->setColor(mColors.at(series));
54 bar->setBrush(mBrushes.at(set));
55 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
55 // bar->setPen(mModel.barSet(set).pen());
56 // bar->setColor(mColors.at(set));
57 // bar->setPen();
58 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
56 itemIndex++;
59 itemIndex++;
57 xPos += mBarDefaultWidth;
60 xPos += mBarDefaultWidth;
58 }
61 }
59
62
60 // TODO: Layout for labels, remove magic number
63 // TODO: Layout for labels, remove magic number
61 xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
64 xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2));
62 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
65 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
63 label->setPos(xPos, mHeight + 20);
66 label->setPos(xPos, mHeight + 20);
64 labelIndex++;
67 labelIndex++;
@@ -46,7 +46,7 void BarGroupBase::setBarWidth( int w )
46 {
46 {
47 mBarDefaultWidth = w;
47 mBarDefaultWidth = w;
48 }
48 }
49
49 /*
50 int BarGroupBase::addColor( QColor color )
50 int BarGroupBase::addColor( QColor color )
51 {
51 {
52 // qDebug() << "BarGroupBase::addColor";
52 // qDebug() << "BarGroupBase::addColor";
@@ -54,12 +54,24 int BarGroupBase::addColor( QColor color )
54 mColors.append(color);
54 mColors.append(color);
55 return colorIndex;
55 return colorIndex;
56 }
56 }
57
57 */
58 /*
58 void BarGroupBase::resetColors()
59 void BarGroupBase::resetColors()
59 {
60 {
60 // qDebug() << "BarGroupBase::resetColors";
61 // qDebug() << "BarGroupBase::resetColors";
61 mColors.clear();
62 mColors.clear();
62 }
63 }
64 */
65 void BarGroupBase::resetBrushes()
66 {
67 mBrushes.clear();
68 }
69
70 void BarGroupBase::addBrush(QBrush brush)
71 {
72 mBrushes.append(brush);
73 }
74
63
75
64 void BarGroupBase::dataChanged()
76 void BarGroupBase::dataChanged()
65 {
77 {
@@ -3,6 +3,8
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "barchartmodel_p.h"
5 #include "barchartmodel_p.h"
6 #include <QPen>
7 #include <QBrush>
6 #include <QGraphicsItem>
8 #include <QGraphicsItem>
7
9
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -23,8 +25,18 public:
23
25
24 // TODO: these may change with layout awarness.
26 // TODO: these may change with layout awarness.
25 void setBarWidth( int w );
27 void setBarWidth( int w );
26 int addColor( QColor color );
28 // int addColor( QColor color );
27 void resetColors();
29 // void resetColors();
30
31 void resetBrushes();
32 void addBrush(QBrush brush);
33
34 void setPen(QPen pen);
35 QPen pen();
36
37 void setBrush(QBrush brush);
38 QBrush brush();
39
28
40
29 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
41 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
30 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
42 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
@@ -49,6 +61,8 protected:
49 bool mSeparatorsVisible;
61 bool mSeparatorsVisible;
50 BarChartModel& mModel;
62 BarChartModel& mModel;
51
63
64 QPen mPen;
65 QList<QBrush> mBrushes;
52 };
66 };
53
67
54 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
@@ -38,18 +38,20 void PercentBarGroup::layoutChanged()
38 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
38 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
39 int labelIndex = mModel.countCategories() * mModel.countSets();
39 int labelIndex = mModel.countCategories() * mModel.countSets();
40
40
41 for (int column = 0; column < mModel.countCategories(); column++) {
41 for (int category = 0; category < mModel.countCategories(); category++) {
42 qreal colSum = mModel.categorySum(column);
42 qreal colSum = mModel.categorySum(category);
43 qreal h = mHeight;
43 qreal h = mHeight;
44 qreal scale = (h / colSum);
44 qreal scale = (h / colSum);
45 qreal yPos = h;
45 qreal yPos = h;
46 for (int row=0; row < mModel.countSets(); row++) {
46 for (int set=0; set < mModel.countSets(); set++) {
47 qreal barHeight = mModel.valueAt(row, column) * scale;
47 qreal barHeight = mModel.valueAt(set, category) * scale;
48 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
48 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
49
49
50 // TODO: width settable per bar?
50 // TODO: width settable per bar?
51 bar->resize(mBarDefaultWidth, barHeight);
51 bar->resize(mBarDefaultWidth, barHeight);
52 bar->setColor(mColors.at(row));
52 bar->setBrush(mBrushes.at(set));
53 // bar->setBrush(mBrush);
54 // bar->setColor(mColors.at(set));
53 bar->setPos(xPos, yPos-barHeight);
55 bar->setPos(xPos, yPos-barHeight);
54 itemIndex++;
56 itemIndex++;
55 yPos -= barHeight;
57 yPos -= barHeight;
@@ -31,6 +31,11 qreal QBarSet::valueAt(int index)
31 return mValues.at(index);
31 return mValues.at(index);
32 }
32 }
33
33
34 void QBarSet::setValue(int index, qreal value)
35 {
36 mValues.replace(index,value);
37 }
38
34 //TODO?:
39 //TODO?:
35 //#include "moc_qbarset.cpp"
40 //#include "moc_qbarset.cpp"
36 QTCOMMERCIALCHART_END_NAMESPACE
41 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,6 +2,8
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QPen>
6 #include <QBrush>
5
7
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
9
@@ -13,18 +15,16 public:
13
15
14 void setName(QString name);
16 void setName(QString name);
15 QString name();
17 QString name();
16 QBarSet& operator << (const qreal &value);
18 QBarSet& operator << (const qreal &value); // appends new value to set
17
19
18 //TODO: What is the way to set a single value to n:th item? Is there need for such functionality?
20 int count(); // count of values in set
19
21 qreal valueAt(int index); // for modifying individual values
20 int count();
22 void setValue(int index, qreal value); //
21 qreal valueAt(int index);
22
23
23 private:
24 private:
24
25
25 QString mName;
26 QString mName;
26 QList<qreal> mValues;
27 QList<qreal> mValues;
27
28 };
28 };
29
29
30 QTCOMMERCIALCHART_END_NAMESPACE
30 QTCOMMERCIALCHART_END_NAMESPACE
@@ -35,7 +35,7 void StackedBarGroup::layoutChanged()
35
35
36 // TODO: better way to auto-layout
36 // TODO: better way to auto-layout
37 // Use reals for accurancy (we might get some compiler warnings... :)
37 // Use reals for accurancy (we might get some compiler warnings... :)
38 // TODO: use temp variable for column count...
38 // TODO: use temp variable for category count...
39 qreal maxSum = mModel.maxCategorySum();
39 qreal maxSum = mModel.maxCategorySum();
40 qreal h = mHeight;
40 qreal h = mHeight;
41 qreal scale = (h / maxSum);
41 qreal scale = (h / maxSum);
@@ -47,14 +47,16 void StackedBarGroup::layoutChanged()
47 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
47 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
48 int labelIndex = mModel.countSets() * mModel.countCategories();
48 int labelIndex = mModel.countSets() * mModel.countCategories();
49
49
50 for (int column = 0; column < mModel.countCategories(); column++) {
50 for (int category = 0; category < mModel.countCategories(); category++) {
51 qreal yPos = h;
51 qreal yPos = h;
52 for (int row=0; row < mModel.countSets(); row++) {
52 for (int set=0; set < mModel.countSets(); set++) {
53 qreal barHeight = mModel.valueAt(row, column) * scale;
53 qreal barHeight = mModel.valueAt(set, category) * scale;
54 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
54 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
55
55
56 bar->resize(mBarDefaultWidth, barHeight);
56 bar->resize(mBarDefaultWidth, barHeight);
57 bar->setColor(mColors.at(row));
57 bar->setBrush(mBrushes.at(set));
58 // bar->setBrush(mBrush);
59 // bar->setColor(mColors.at(set));
58 bar->setPos(xPos, yPos-barHeight);
60 bar->setPos(xPos, yPos-barHeight);
59 itemIndex++;
61 itemIndex++;
60 yPos -= barHeight;
62 yPos -= barHeight;
@@ -131,41 +131,47 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int coun
131
131
132 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
132 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
133 {
133 {
134 item->resetColors();
134 // TODO: better way to descide series color and remove hard coded colors.
135 item->resetBrushes();
135 for (int i=0; i<m_seriesColor.count(); i++) {
136 for (int i=0; i<m_seriesColor.count(); i++) {
136 item->addColor(m_seriesColor.at(i));
137 QBrush brush(m_seriesColor.at(i));
138 item->addBrush(brush);
137 }
139 }
138 item->addColor(QColor(255,0,0,128));
140 item->addBrush(QBrush(QColor(255,0,0,128)));
139 item->addColor(QColor(255,255,0,128));
141 item->addBrush(QBrush(QColor(255,255,0,128)));
140 item->addColor(QColor(0,255,0,128));
142 item->addBrush(QBrush(QColor(0,255,0,128)));
141 item->addColor(QColor(0,0,255,128));
143 item->addBrush(QBrush(QColor(0,0,255,128)));
142 item->addColor(QColor(255,128,0,128));
144 item->addBrush(QBrush(QColor(255,128,0,128)));
143 }
145 }
144
146
145 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
147 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
146 {
148 {
147 item->resetColors();
149 // TODO: better way to descide series color and remove hard coded colors.
148 for (int i=0; i< m_seriesColor.count(); i++) {
150 item->resetBrushes();
149 item->addColor(m_seriesColor.at(i));
151 for (int i=0; i<m_seriesColor.count(); i++) {
152 QBrush brush(m_seriesColor.at(i));
153 item->addBrush(brush);
150 }
154 }
151 item->addColor(QColor(255,0,0,128));
155 item->addBrush(QBrush(QColor(255,0,0,128)));
152 item->addColor(QColor(255,255,0,128));
156 item->addBrush(QBrush(QColor(255,255,0,128)));
153 item->addColor(QColor(0,255,0,128));
157 item->addBrush(QBrush(QColor(0,255,0,128)));
154 item->addColor(QColor(0,0,255,128));
158 item->addBrush(QBrush(QColor(0,0,255,128)));
155 item->addColor(QColor(255,128,0,128));
159 item->addBrush(QBrush(QColor(255,128,0,128)));
156 }
160 }
157
161
158 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
162 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
159 {
163 {
160 item->resetColors();
164 // TODO: better way to descide series color and remove hard coded colors.
161 for (int i=0; i< m_seriesColor.count(); i++) {
165 item->resetBrushes();
162 item->addColor(m_seriesColor.at(i));
166 for (int i=0; i<m_seriesColor.count(); i++) {
167 QBrush brush(m_seriesColor.at(i));
168 item->addBrush(brush);
163 }
169 }
164 item->addColor(QColor(255,0,0,128));
170 item->addBrush(QBrush(QColor(255,0,0,128)));
165 item->addColor(QColor(255,255,0,128));
171 item->addBrush(QBrush(QColor(255,255,0,128)));
166 item->addColor(QColor(0,255,0,128));
172 item->addBrush(QBrush(QColor(0,255,0,128)));
167 item->addColor(QColor(0,0,255,128));
173 item->addBrush(QBrush(QColor(0,0,255,128)));
168 item->addColor(QColor(255,128,0,128));
174 item->addBrush(QBrush(QColor(255,128,0,128)));
169 }
175 }
170
176
171 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
177 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
General Comments 0
You need to be logged in to leave comments. Login now