@@ -1,62 +1,74 | |||
|
1 | 1 | #include "bar_p.h" |
|
2 | 2 | #include <QDebug> |
|
3 | 3 | #include <QPainter> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | Bar::Bar(QGraphicsItem *parent) |
|
8 | 8 | : ChartItem(parent) |
|
9 | 9 | { |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | void Bar::setSize(const QSizeF& size) |
|
13 | 13 | { |
|
14 | 14 | mWidth = size.width(); |
|
15 | 15 | mHeight = size.height(); |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | void Bar::resize( qreal w, qreal h ) |
|
20 | 20 | { |
|
21 | 21 | // qDebug() << "bar::resize" << w << h; |
|
22 | 22 | mWidth = w; |
|
23 | 23 | mHeight = h; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | void Bar::setColor( QColor col ) |
|
27 | 27 | { |
|
28 | 28 | mColor = col; |
|
29 | 29 | } |
|
30 | ||
|
30 | 31 | void Bar::setPos(qreal x, qreal y) |
|
31 | 32 | { |
|
32 | 33 | // qDebug() << "Bar::setpos" << x << y; |
|
33 | 34 | mXpos = x; |
|
34 | 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 | 48 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
38 | 49 | { |
|
39 | 50 | if (0 == mHeight) { |
|
40 | 51 | return; |
|
41 | 52 | } |
|
42 | 53 | // TODO: accept brush instead of color |
|
43 | QBrush brush(mColor); | |
|
44 | painter->setBrush(brush); | |
|
54 | painter->setBrush(mBrush); | |
|
55 | // QBrush brush(mColor); | |
|
56 | // painter->setBrush(brush); | |
|
45 | 57 | |
|
46 | 58 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. |
|
47 | 59 | int x0 = mXpos; |
|
48 | 60 | int x1 = (mXpos + mWidth); |
|
49 | 61 | int w = x1-x0; |
|
50 | 62 | int y0 = mYpos; |
|
51 | 63 | int y1 = (mYpos + mHeight); |
|
52 | 64 | int h = y1-y0; |
|
53 | 65 | painter->drawRect(x0, y0 ,w ,h); |
|
54 | 66 | } |
|
55 | 67 | |
|
56 | 68 | QRectF Bar::boundingRect() const |
|
57 | 69 | { |
|
58 | 70 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); |
|
59 | 71 | return r; |
|
60 | 72 | } |
|
61 | 73 | |
|
62 | 74 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,42 +1,49 | |||
|
1 | 1 | #ifndef BAR_H |
|
2 | 2 | #define BAR_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | 5 | #include "qchartglobal.h" |
|
6 | 6 | #include <QGraphicsItem> |
|
7 | #include <QPen> | |
|
8 | #include <QBrush> | |
|
7 | 9 | |
|
8 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 11 | |
|
10 | 12 | // Single bar item of chart |
|
11 | 13 | class Bar : public ChartItem |
|
12 | 14 | { |
|
13 | 15 | public: |
|
14 | 16 | Bar(QGraphicsItem *parent=0); |
|
15 | 17 | |
|
16 | 18 | public: // from ChartItem |
|
17 | 19 | void setSize(const QSizeF &size); |
|
18 | 20 | |
|
19 | 21 | // Layout Stuff |
|
20 |
void resize( |
|
|
21 | void setColor( QColor col ); // Color of bar | |
|
22 | void resize(qreal w, qreal h); // Size of bar. | |
|
22 | 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 | 28 | public: |
|
25 | 29 | // From QGraphicsItem |
|
26 | 30 | |
|
27 | 31 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
28 | 32 | QRectF boundingRect() const; |
|
29 | 33 | |
|
30 | 34 | private: |
|
31 | 35 | |
|
32 | 36 | qreal mHeight; |
|
33 | 37 | qreal mWidth; |
|
34 | 38 | qreal mXpos; |
|
35 | 39 | qreal mYpos; |
|
36 | 40 | QColor mColor; |
|
37 | 41 | |
|
42 | QBrush mBrush; | |
|
43 | QPen mPen; | |
|
44 | ||
|
38 | 45 | }; |
|
39 | 46 | |
|
40 | 47 | QTCOMMERCIALCHART_END_NAMESPACE |
|
41 | 48 | |
|
42 | 49 | #endif // BAR_H |
@@ -1,70 +1,73 | |||
|
1 | 1 | #include "bargroup.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include <QDebug> |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | BarGroup::BarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
9 | 9 | BarGroupBase(model,parent) |
|
10 | 10 | { |
|
11 | 11 | mBarDefaultWidth = 5; |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | void BarGroup::layoutChanged() |
|
15 | 15 | { |
|
16 | 16 | // qDebug() << "BarGroup::layoutChanged"; |
|
17 | 17 | // Scale bars to new layout |
|
18 | 18 | // Layout for bars: |
|
19 | 19 | if (mModel.countSets() <= 0) { |
|
20 | 20 | qDebug() << "No sets in model!"; |
|
21 | 21 | return; |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | if (childItems().count() == 0) { |
|
25 | 25 | qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; |
|
26 | 26 | return; |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | // TODO: better way to auto-layout? |
|
30 | 30 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
31 | 31 | int itemCount = mModel.countCategories(); |
|
32 |
int se |
|
|
32 | int setCount = mModel.countSets(); | |
|
33 | 33 | |
|
34 | 34 | qreal tW = mWidth; |
|
35 | 35 | qreal tH = mHeight; |
|
36 | 36 | qreal tM = mModel.max(); |
|
37 | 37 | qreal scale = (tH/tM); |
|
38 | 38 | qreal tC = itemCount+1; |
|
39 |
qreal xStepPerSe |
|
|
39 | qreal xStepPerSet = (tW/tC); | |
|
40 | 40 | |
|
41 | 41 | // Scaling. |
|
42 | 42 | int itemIndex(0); |
|
43 |
int labelIndex = itemCount * se |
|
|
43 | int labelIndex = itemCount * setCount; | |
|
44 | 44 | |
|
45 | 45 | for (int item=0; item < itemCount; item++) { |
|
46 |
qreal xPos = xStepPerSe |
|
|
46 | qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | |
|
47 | 47 | qreal yPos = mHeight; |
|
48 |
for (int se |
|
|
49 |
qreal barHeight = mModel.valueAt(se |
|
|
48 | for (int set = 0; set < setCount; set++) { | |
|
49 | qreal barHeight = mModel.valueAt(set, item) * scale; | |
|
50 | 50 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
51 | 51 | |
|
52 | 52 | // TODO: width settable per bar? |
|
53 | 53 | bar->resize(mBarDefaultWidth, barHeight); |
|
54 |
bar->set |
|
|
55 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | |
|
54 | bar->setBrush(mBrushes.at(set)); | |
|
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 | 59 | itemIndex++; |
|
57 | 60 | xPos += mBarDefaultWidth; |
|
58 | 61 | } |
|
59 | 62 | |
|
60 | 63 | // TODO: Layout for labels, remove magic number |
|
61 |
xPos = xStepPerSe |
|
|
64 | xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | |
|
62 | 65 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
63 | 66 | label->setPos(xPos, mHeight + 20); |
|
64 | 67 | labelIndex++; |
|
65 | 68 | } |
|
66 | 69 | |
|
67 | 70 | mLayoutDirty = true; |
|
68 | 71 | } |
|
69 | 72 | |
|
70 | 73 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,125 +1,137 | |||
|
1 | 1 | #include "bargroupbase.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "separator_p.h" |
|
5 | 5 | #include <QDebug> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | BarGroupBase::BarGroupBase(BarChartModel& model, QGraphicsItem *parent) |
|
10 | 10 | : ChartItem(parent) |
|
11 | 11 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
12 | 12 | ,mLayoutSet(false) |
|
13 | 13 | ,mLayoutDirty(true) |
|
14 | 14 | ,mSeparatorsVisible(true) |
|
15 | 15 | ,mModel(model) |
|
16 | 16 | { |
|
17 | 17 | dataChanged(); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | void BarGroupBase::setSeparatorsVisible(bool visible) |
|
21 | 21 | { |
|
22 | 22 | mSeparatorsVisible = visible; |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
26 | 26 | { |
|
27 | 27 | // qDebug() << "BarGroupBase::paint" << childItems().count(); |
|
28 | 28 | if (!mLayoutSet) { |
|
29 | 29 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; |
|
30 | 30 | return; |
|
31 | 31 | } |
|
32 | 32 | // if (mLayoutDirty) { |
|
33 | 33 | // Layout or data has changed. Need to redraw. |
|
34 | 34 | foreach(QGraphicsItem* i, childItems()) { |
|
35 | 35 | i->paint(painter,option,widget); |
|
36 | 36 | } |
|
37 | 37 | // } |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | QRectF BarGroupBase::boundingRect() const |
|
41 | 41 | { |
|
42 | 42 | return QRectF(0,0,mWidth,mHeight); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | void BarGroupBase::setBarWidth( int w ) |
|
46 | 46 | { |
|
47 | 47 | mBarDefaultWidth = w; |
|
48 | 48 | } |
|
49 | ||
|
49 | /* | |
|
50 | 50 | int BarGroupBase::addColor( QColor color ) |
|
51 | 51 |
|
|
52 | 52 |
|
|
53 | 53 | int colorIndex = mColors.count(); |
|
54 | 54 | mColors.append(color); |
|
55 | 55 | return colorIndex; |
|
56 | 56 |
|
|
57 | ||
|
57 | */ | |
|
58 | /* | |
|
58 | 59 | void BarGroupBase::resetColors() |
|
59 | 60 |
|
|
60 | 61 |
|
|
61 | 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 | 76 | void BarGroupBase::dataChanged() |
|
65 | 77 | { |
|
66 | 78 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
67 | 79 | |
|
68 | 80 | // Delete old bars |
|
69 | 81 | foreach (QGraphicsItem* item, childItems()) { |
|
70 | 82 | delete item; |
|
71 | 83 | } |
|
72 | 84 | |
|
73 | 85 | // Create new graphic items for bars |
|
74 | 86 | int totalItems = mModel.countTotalItems(); |
|
75 | 87 | for (int i=0; i<totalItems; i++) { |
|
76 | 88 | Bar *bar = new Bar(this); |
|
77 | 89 | childItems().append(bar); |
|
78 | 90 | } |
|
79 | 91 | |
|
80 | 92 | int count = mModel.countCategories(); |
|
81 | 93 | for (int i=0; i<count; i++) { |
|
82 | 94 | BarLabel* label = new BarLabel(this); |
|
83 | 95 | label->set(mModel.label(i)); |
|
84 | 96 | childItems().append(label); |
|
85 | 97 | } |
|
86 | 98 | |
|
87 | 99 | count = mModel.countCategories() - 1; // There is one less separator than columns |
|
88 | 100 | for (int i=0; i<count; i++) { |
|
89 | 101 | Separator* sep = new Separator(this); |
|
90 | 102 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
91 | 103 | childItems().append(sep); |
|
92 | 104 | } |
|
93 | 105 | |
|
94 | 106 | // TODO: if (autolayout) { layoutChanged() } or something |
|
95 | 107 | mLayoutDirty = true; |
|
96 | 108 | } |
|
97 | 109 | |
|
98 | 110 | //handlers |
|
99 | 111 | |
|
100 | 112 | void BarGroupBase::handleModelChanged(int index) |
|
101 | 113 | { |
|
102 | 114 | // qDebug() << "BarGroupBase::handleModelChanged" << index; |
|
103 | 115 | dataChanged(); |
|
104 | 116 | } |
|
105 | 117 | |
|
106 | 118 | void BarGroupBase::handleDomainChanged(const Domain& domain) |
|
107 | 119 | { |
|
108 | 120 | // qDebug() << "BarGroupBase::handleDomainChanged"; |
|
109 | 121 | // TODO: Figure out the use case for this. |
|
110 | 122 | // Affects the size of visible item, so layout is changed. |
|
111 | 123 | // layoutChanged(); |
|
112 | 124 | } |
|
113 | 125 | |
|
114 | 126 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) |
|
115 | 127 | { |
|
116 | 128 | mWidth = rect.width(); |
|
117 | 129 | mHeight = rect.height(); |
|
118 | 130 | layoutChanged(); |
|
119 | 131 | mLayoutSet = true; |
|
120 | 132 | setPos(rect.topLeft()); |
|
121 | 133 | } |
|
122 | 134 | |
|
123 | 135 | #include "moc_bargroupbase.cpp" |
|
124 | 136 | |
|
125 | 137 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,56 +1,70 | |||
|
1 | 1 | #ifndef BARGROUPBASE_H |
|
2 | 2 | #define BARGROUPBASE_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | 5 | #include "barchartmodel_p.h" |
|
6 | #include <QPen> | |
|
7 | #include <QBrush> | |
|
6 | 8 | #include <QGraphicsItem> |
|
7 | 9 | |
|
8 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 11 | |
|
10 | 12 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. |
|
11 | 13 | class BarGroupBase : public QObject, public ChartItem |
|
12 | 14 | { |
|
13 | 15 | Q_OBJECT |
|
14 | 16 | public: |
|
15 | 17 | BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); |
|
16 | 18 | void setSeparatorsVisible(bool visible = true); |
|
17 | 19 | |
|
18 | 20 | public: |
|
19 | 21 | |
|
20 | 22 | // From QGraphicsItem |
|
21 | 23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
22 | 24 | QRectF boundingRect() const; |
|
23 | 25 | |
|
24 | 26 | // TODO: these may change with layout awarness. |
|
25 | 27 | void setBarWidth( int w ); |
|
26 | int addColor( QColor color ); | |
|
27 | void resetColors(); | |
|
28 | // int addColor( QColor color ); | |
|
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 | 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 | 42 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
31 | 43 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes |
|
32 | 44 | |
|
33 | 45 | protected slots: |
|
34 | 46 | void handleModelChanged(int index); |
|
35 | 47 | void handleDomainChanged(const Domain& domain); |
|
36 | 48 | void handleGeometryChanged(const QRectF& size); |
|
37 | 49 | |
|
38 | 50 | protected: |
|
39 | 51 | |
|
40 | 52 | // TODO: consider these. |
|
41 | 53 | int mHeight; // Layout spesific |
|
42 | 54 | int mWidth; |
|
43 | 55 | int mBarDefaultWidth; |
|
44 | 56 | |
|
45 | 57 | bool mLayoutSet; // True, if component has been laid out. |
|
46 | 58 | bool mLayoutDirty; |
|
47 | 59 | |
|
48 | 60 | QList<QColor> mColors; // List of colors for series for now |
|
49 | 61 | bool mSeparatorsVisible; |
|
50 | 62 | BarChartModel& mModel; |
|
51 | 63 | |
|
64 | QPen mPen; | |
|
65 | QList<QBrush> mBrushes; | |
|
52 | 66 | }; |
|
53 | 67 | |
|
54 | 68 | QTCOMMERCIALCHART_END_NAMESPACE |
|
55 | 69 | |
|
56 | 70 | #endif // BARGROUPBASE_H |
@@ -1,79 +1,81 | |||
|
1 | 1 | #include "percentbargroup.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "separator_p.h" |
|
5 | 5 | #include <QDebug> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | PercentBarGroup::PercentBarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
11 | 11 | BarGroupBase(model, parent) |
|
12 | 12 | { |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | void PercentBarGroup::layoutChanged() |
|
16 | 16 | { |
|
17 | 17 | // qDebug() << "PercentBarGroup::layoutChanged"; |
|
18 | 18 | // Scale bars to new layout |
|
19 | 19 | // Layout for bars: |
|
20 | 20 | if (mModel.countSets() <= 0) { |
|
21 | 21 | qDebug() << "No sets in model!"; |
|
22 | 22 | // Nothing to do. |
|
23 | 23 | return; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | if (childItems().count() == 0) { |
|
27 | 27 | qDebug() << "WARNING: PercentBarGroup::layoutChanged called before graphics items are created!"; |
|
28 | 28 | return; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | // TODO: better way to auto-layout |
|
32 | 32 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
33 | 33 | int count = mModel.countCategories(); |
|
34 | 34 | int itemIndex(0); |
|
35 | 35 | qreal tW = mWidth; |
|
36 | 36 | qreal tC = count+1; |
|
37 | 37 | qreal xStep = (tW/tC); |
|
38 | 38 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
39 | 39 | int labelIndex = mModel.countCategories() * mModel.countSets(); |
|
40 | 40 | |
|
41 |
for (int c |
|
|
42 |
qreal colSum = mModel.categorySum(c |
|
|
41 | for (int category = 0; category < mModel.countCategories(); category++) { | |
|
42 | qreal colSum = mModel.categorySum(category); | |
|
43 | 43 | qreal h = mHeight; |
|
44 | 44 | qreal scale = (h / colSum); |
|
45 | 45 | qreal yPos = h; |
|
46 |
for (int |
|
|
47 |
qreal barHeight = mModel.valueAt( |
|
|
46 | for (int set=0; set < mModel.countSets(); set++) { | |
|
47 | qreal barHeight = mModel.valueAt(set, category) * scale; | |
|
48 | 48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
49 | 49 | |
|
50 | 50 | // TODO: width settable per bar? |
|
51 | 51 | bar->resize(mBarDefaultWidth, barHeight); |
|
52 |
bar->set |
|
|
52 | bar->setBrush(mBrushes.at(set)); | |
|
53 | // bar->setBrush(mBrush); | |
|
54 | // bar->setColor(mColors.at(set)); | |
|
53 | 55 | bar->setPos(xPos, yPos-barHeight); |
|
54 | 56 | itemIndex++; |
|
55 | 57 | yPos -= barHeight; |
|
56 | 58 | } |
|
57 | 59 | |
|
58 | 60 | // TODO: Layout for labels, remove magic number |
|
59 | 61 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
60 | 62 | label->setPos(xPos, mHeight + 20); |
|
61 | 63 | labelIndex++; |
|
62 | 64 | xPos += xStep; |
|
63 | 65 | } |
|
64 | 66 | |
|
65 | 67 | // Position separators |
|
66 | 68 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
67 | 69 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
68 | 70 | for (int s=0; s < mModel.countCategories() - 1; s++) { |
|
69 | 71 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
70 | 72 | sep->setPos(xPos,0); |
|
71 | 73 | sep->setSize(QSizeF(1,mHeight)); |
|
72 | 74 | xPos += xStep; |
|
73 | 75 | separatorIndex++; |
|
74 | 76 | } |
|
75 | 77 | |
|
76 | 78 | mLayoutDirty = true; |
|
77 | 79 | } |
|
78 | 80 | |
|
79 | 81 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,36 +1,41 | |||
|
1 | 1 | #include "qbarset.h" |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | QBarSet::QBarSet() |
|
6 | 6 | { |
|
7 | 7 | } |
|
8 | 8 | |
|
9 | 9 | void QBarSet::setName(QString name) |
|
10 | 10 | { |
|
11 | 11 | mName = name; |
|
12 | 12 | } |
|
13 | 13 | QString QBarSet::name() |
|
14 | 14 | { |
|
15 | 15 | return mName; |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | QBarSet& QBarSet::operator << (const qreal &value) |
|
19 | 19 | { |
|
20 | 20 | mValues.append(value); |
|
21 | 21 | return *this; |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | int QBarSet::count() |
|
25 | 25 | { |
|
26 | 26 | return mValues.count(); |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | qreal QBarSet::valueAt(int index) |
|
30 | 30 | { |
|
31 | 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 | 39 | //TODO?: |
|
35 | 40 | //#include "moc_qbarset.cpp" |
|
36 | 41 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,32 +1,32 | |||
|
1 | 1 | #ifndef QBARSET_H |
|
2 | 2 | #define QBARSET_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | #include <QPen> | |
|
6 | #include <QBrush> | |
|
5 | 7 | |
|
6 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 9 | |
|
8 | 10 | class QTCOMMERCIALCHART_EXPORT QBarSet // TODO? : public QObject |
|
9 | 11 | { |
|
10 | 12 | //Q_OBJECT; |
|
11 | 13 | public: |
|
12 | 14 | QBarSet(); |
|
13 | 15 | |
|
14 | 16 | void setName(QString name); |
|
15 | 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? | |
|
19 | ||
|
20 | int count(); | |
|
21 | qreal valueAt(int index); | |
|
20 | int count(); // count of values in set | |
|
21 | qreal valueAt(int index); // for modifying individual values | |
|
22 | void setValue(int index, qreal value); // | |
|
22 | 23 | |
|
23 | 24 | private: |
|
24 | 25 | |
|
25 | 26 | QString mName; |
|
26 | 27 | QList<qreal> mValues; |
|
27 | ||
|
28 | 28 | }; |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_END_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | #endif // QBARSET_H |
@@ -1,84 +1,86 | |||
|
1 | 1 | #include "stackedbargroup.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "separator_p.h" |
|
5 | 5 | #include <QDebug> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | StackedBarGroup::StackedBarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
10 | 10 | BarGroupBase(model,parent) |
|
11 | 11 | { |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | void StackedBarGroup::layoutChanged() |
|
15 | 15 | { |
|
16 | 16 | // qDebug() << "StackedBarGroup::layoutChanged"; |
|
17 | 17 | // Scale bars to new layout |
|
18 | 18 | // Layout for bars: |
|
19 | 19 | if (mModel.countSets() <= 0) { |
|
20 | 20 | qDebug() << "No sets in model!"; |
|
21 | 21 | // Nothing to do. |
|
22 | 22 | return; |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | if (mModel.countCategories() == 0) { |
|
26 | 26 | qDebug() << "No categories in model!"; |
|
27 | 27 | // Nothing to do |
|
28 | 28 | return; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | if (childItems().count() == 0) { |
|
32 | 32 | qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!"; |
|
33 | 33 | return; |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | // TODO: better way to auto-layout |
|
37 | 37 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
38 |
// TODO: use temp variable for c |
|
|
38 | // TODO: use temp variable for category count... | |
|
39 | 39 | qreal maxSum = mModel.maxCategorySum(); |
|
40 | 40 | qreal h = mHeight; |
|
41 | 41 | qreal scale = (h / maxSum); |
|
42 | 42 | |
|
43 | 43 | int itemIndex(0); |
|
44 | 44 | qreal tW = mWidth; |
|
45 | 45 | qreal tC = mModel.countCategories() + 1; |
|
46 | 46 | qreal xStep = (tW/tC); |
|
47 | 47 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
48 | 48 | int labelIndex = mModel.countSets() * mModel.countCategories(); |
|
49 | 49 | |
|
50 |
for (int c |
|
|
50 | for (int category = 0; category < mModel.countCategories(); category++) { | |
|
51 | 51 | qreal yPos = h; |
|
52 |
for (int |
|
|
53 |
qreal barHeight = mModel.valueAt( |
|
|
52 | for (int set=0; set < mModel.countSets(); set++) { | |
|
53 | qreal barHeight = mModel.valueAt(set, category) * scale; | |
|
54 | 54 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
55 | 55 | |
|
56 | 56 | bar->resize(mBarDefaultWidth, barHeight); |
|
57 |
bar->set |
|
|
57 | bar->setBrush(mBrushes.at(set)); | |
|
58 | // bar->setBrush(mBrush); | |
|
59 | // bar->setColor(mColors.at(set)); | |
|
58 | 60 | bar->setPos(xPos, yPos-barHeight); |
|
59 | 61 | itemIndex++; |
|
60 | 62 | yPos -= barHeight; |
|
61 | 63 | } |
|
62 | 64 | |
|
63 | 65 | // TODO: Layout for labels, remove magic number |
|
64 | 66 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
65 | 67 | label->setPos(xPos, mHeight + 20); |
|
66 | 68 | labelIndex++; |
|
67 | 69 | xPos += xStep; |
|
68 | 70 | } |
|
69 | 71 | |
|
70 | 72 | // Position separators |
|
71 | 73 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
72 | 74 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
73 | 75 | for (int s=0; s < mModel.countCategories() - 1; s++) { |
|
74 | 76 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
75 | 77 | sep->setPos(xPos,0); |
|
76 | 78 | sep->setSize(QSizeF(1,mHeight)); |
|
77 | 79 | xPos += xStep; |
|
78 | 80 | separatorIndex++; |
|
79 | 81 | } |
|
80 | 82 | |
|
81 | 83 | mLayoutDirty = true; |
|
82 | 84 | } |
|
83 | 85 | |
|
84 | 86 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,239 +1,245 | |||
|
1 | 1 | #include "charttheme_p.h" |
|
2 | 2 | #include "qchart.h" |
|
3 | 3 | #include "qchartaxis.h" |
|
4 | 4 | |
|
5 | 5 | |
|
6 | 6 | //series |
|
7 | 7 | #include "barchartseries.h" |
|
8 | 8 | #include "stackedbarchartseries.h" |
|
9 | 9 | #include "percentbarchartseries.h" |
|
10 | 10 | #include "qlinechartseries.h" |
|
11 | 11 | #include "qscatterseries.h" |
|
12 | 12 | #include "qpieseries.h" |
|
13 | 13 | |
|
14 | 14 | //items |
|
15 | 15 | #include "axisitem_p.h" |
|
16 | 16 | #include "bargroup.h" |
|
17 | 17 | #include "stackedbargroup.h" |
|
18 | 18 | #include "linechartitem_p.h" |
|
19 | 19 | #include "percentbargroup.h" |
|
20 | 20 | #include "scatterpresenter.h" |
|
21 | 21 | #include "piepresenter.h" |
|
22 | 22 | |
|
23 | 23 | //themes |
|
24 | 24 | #include "chartthemevanilla_p.h" |
|
25 | 25 | #include "chartthemeicy_p.h" |
|
26 | 26 | #include "chartthemegrayscale_p.h" |
|
27 | 27 | #include "chartthemescientific_p.h" |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | /* TODO |
|
33 | 33 | case QChart::ChartThemeUnnamed1: |
|
34 | 34 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); |
|
35 | 35 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); |
|
36 | 36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); |
|
37 | 37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); |
|
38 | 38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); |
|
39 | 39 | |
|
40 | 40 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); |
|
41 | 41 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
42 | 42 | */ |
|
43 | 43 | |
|
44 | 44 | ChartTheme::ChartTheme(QChart::ChartTheme id) |
|
45 | 45 | { |
|
46 | 46 | m_id = id; |
|
47 | 47 | m_seriesColor.append(QRgb(0xff000000)); |
|
48 | 48 | m_seriesColor.append(QRgb(0xff707070)); |
|
49 | 49 | m_gradientStartColor = QColor(QRgb(0xffffffff)); |
|
50 | 50 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
55 | 55 | { |
|
56 | 56 | switch(theme) { |
|
57 | 57 | case QChart::ChartThemeDefault: |
|
58 | 58 | return new ChartTheme(); |
|
59 | 59 | case QChart::ChartThemeVanilla: |
|
60 | 60 | return new ChartThemeVanilla(); |
|
61 | 61 | case QChart::ChartThemeIcy: |
|
62 | 62 | return new ChartThemeIcy(); |
|
63 | 63 | case QChart::ChartThemeGrayscale: |
|
64 | 64 | return new ChartThemeGrayscale(); |
|
65 | 65 | case QChart::ChartThemeScientific: |
|
66 | 66 | return new ChartThemeScientific(); |
|
67 | 67 | } |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | void ChartTheme::decorate(QChart* chart) |
|
71 | 71 | { |
|
72 | 72 | QLinearGradient backgroundGradient; |
|
73 | 73 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); |
|
74 | 74 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); |
|
75 | 75 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
76 | 76 | chart->setChartBackgroundBrush(backgroundGradient); |
|
77 | 77 | } |
|
78 | 78 | //TODO helper to by removed later |
|
79 | 79 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) |
|
80 | 80 | { |
|
81 | 81 | switch(series->type()) |
|
82 | 82 | { |
|
83 | 83 | case QChartSeries::SeriesTypeLine: { |
|
84 | 84 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); |
|
85 | 85 | LineChartItem* i = static_cast<LineChartItem*>(item); |
|
86 | 86 | decorate(i,s,count); |
|
87 | 87 | break; |
|
88 | 88 | } |
|
89 | 89 | case QChartSeries::SeriesTypeBar: { |
|
90 | 90 | BarChartSeries* b = static_cast<BarChartSeries*>(series); |
|
91 | 91 | BarGroup* i = static_cast<BarGroup*>(item); |
|
92 | 92 | decorate(i,b,count); |
|
93 | 93 | break; |
|
94 | 94 | } |
|
95 | 95 | case QChartSeries::SeriesTypeStackedBar: { |
|
96 | 96 | StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series); |
|
97 | 97 | StackedBarGroup* i = static_cast<StackedBarGroup*>(item); |
|
98 | 98 | decorate(i,s,count); |
|
99 | 99 | break; |
|
100 | 100 | } |
|
101 | 101 | case QChartSeries::SeriesTypePercentBar: { |
|
102 | 102 | PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series); |
|
103 | 103 | PercentBarGroup* i = static_cast<PercentBarGroup*>(item); |
|
104 | 104 | decorate(i,s,count); |
|
105 | 105 | break; |
|
106 | 106 | } |
|
107 | 107 | case QChartSeries::SeriesTypePie: { |
|
108 | 108 | QPieSeries* s = static_cast<QPieSeries*>(series); |
|
109 | 109 | PiePresenter* i = static_cast<PiePresenter*>(item); |
|
110 | 110 | decorate(i,s,count); |
|
111 | 111 | break; |
|
112 | 112 | } |
|
113 | 113 | default: |
|
114 | 114 | qDebug()<<"Wrong item to be decorated by theme"; |
|
115 | 115 | break; |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) |
|
121 | 121 | { |
|
122 | 122 | QPen pen; |
|
123 | 123 | if(pen != series->pen()){ |
|
124 | 124 | item->setPen(series->pen()); |
|
125 | 125 | return; |
|
126 | 126 | } |
|
127 | 127 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
128 | 128 | pen.setWidthF(2); |
|
129 | 129 | item->setPen(pen); |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 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 | 136 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
136 |
|
|
|
137 | QBrush brush(m_seriesColor.at(i)); | |
|
138 | item->addBrush(brush); | |
|
137 | 139 | } |
|
138 |
item->add |
|
|
139 |
item->add |
|
|
140 |
item->add |
|
|
141 |
item->add |
|
|
142 |
item->add |
|
|
140 | item->addBrush(QBrush(QColor(255,0,0,128))); | |
|
141 | item->addBrush(QBrush(QColor(255,255,0,128))); | |
|
142 | item->addBrush(QBrush(QColor(0,255,0,128))); | |
|
143 | item->addBrush(QBrush(QColor(0,0,255,128))); | |
|
144 | item->addBrush(QBrush(QColor(255,128,0,128))); | |
|
143 | 145 | } |
|
144 | 146 | |
|
145 | 147 | void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) |
|
146 | 148 | { |
|
147 | item->resetColors(); | |
|
148 | for (int i=0; i< m_seriesColor.count(); i++) { | |
|
149 | item->addColor(m_seriesColor.at(i)); | |
|
149 | // TODO: better way to descide series color and remove hard coded colors. | |
|
150 | item->resetBrushes(); | |
|
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->add |
|
|
152 |
item->add |
|
|
153 |
item->add |
|
|
154 |
item->add |
|
|
155 |
item->add |
|
|
155 | item->addBrush(QBrush(QColor(255,0,0,128))); | |
|
156 | item->addBrush(QBrush(QColor(255,255,0,128))); | |
|
157 | item->addBrush(QBrush(QColor(0,255,0,128))); | |
|
158 | item->addBrush(QBrush(QColor(0,0,255,128))); | |
|
159 | item->addBrush(QBrush(QColor(255,128,0,128))); | |
|
156 | 160 | } |
|
157 | 161 | |
|
158 | 162 | void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) |
|
159 | 163 | { |
|
160 | item->resetColors(); | |
|
161 | for (int i=0; i< m_seriesColor.count(); i++) { | |
|
162 | item->addColor(m_seriesColor.at(i)); | |
|
164 | // TODO: better way to descide series color and remove hard coded colors. | |
|
165 | item->resetBrushes(); | |
|
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->add |
|
|
165 |
item->add |
|
|
166 |
item->add |
|
|
167 |
item->add |
|
|
168 |
item->add |
|
|
170 | item->addBrush(QBrush(QColor(255,0,0,128))); | |
|
171 | item->addBrush(QBrush(QColor(255,255,0,128))); | |
|
172 | item->addBrush(QBrush(QColor(0,255,0,128))); | |
|
173 | item->addBrush(QBrush(QColor(0,0,255,128))); | |
|
174 | item->addBrush(QBrush(QColor(255,128,0,128))); | |
|
169 | 175 | } |
|
170 | 176 | |
|
171 | 177 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) |
|
172 | 178 | { |
|
173 | 179 | Q_ASSERT(presenter); |
|
174 | 180 | Q_ASSERT(series); |
|
175 | 181 | |
|
176 | 182 | presenter->m_markerPen.setColor(m_seriesColor.at(count % m_seriesColor.size())); |
|
177 | 183 | |
|
178 | 184 | // QPen pen; |
|
179 | 185 | // if(pen != series->pen()){ |
|
180 | 186 | // item->setPen(series->pen()); |
|
181 | 187 | // return; |
|
182 | 188 | // } |
|
183 | 189 | // pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
184 | 190 | // pen.setWidthF(2); |
|
185 | 191 | // item->setPen(pen); |
|
186 | 192 | } |
|
187 | 193 | |
|
188 | 194 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
|
189 | 195 | { |
|
190 | 196 | // create a list of slice colors based on current theme |
|
191 | 197 | int i = 0; |
|
192 | 198 | QList<QColor> colors; |
|
193 | 199 | while (colors.count() < series->count()) { |
|
194 | 200 | |
|
195 | 201 | // get base color |
|
196 | 202 | QColor c = m_seriesColor[i++]; |
|
197 | 203 | i = i % m_seriesColor.count(); |
|
198 | 204 | |
|
199 | 205 | // -1 means achromatic color -> cannot manipulate lightness |
|
200 | 206 | // TODO: find a better way to randomize lightness |
|
201 | 207 | if (c.toHsv().hue() == -1) |
|
202 | 208 | qWarning() << "ChartTheme::decorate() warning: achromatic theme color"; |
|
203 | 209 | |
|
204 | 210 | // randomize lightness |
|
205 | 211 | qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter |
|
206 | 212 | c = c.lighter(f); |
|
207 | 213 | |
|
208 | 214 | // find duplicates |
|
209 | 215 | bool isUnique = true; |
|
210 | 216 | foreach (QColor color, colors) { |
|
211 | 217 | if (c == color) |
|
212 | 218 | isUnique = false; |
|
213 | 219 | } |
|
214 | 220 | |
|
215 | 221 | // add to array if unique |
|
216 | 222 | //if (isUnique) |
|
217 | 223 | colors << c; |
|
218 | 224 | } |
|
219 | 225 | |
|
220 | 226 | // finally update colors |
|
221 | 227 | foreach (QPieSliceId id, series->ids()) { |
|
222 | 228 | QPieSlice s = series->slice(id); |
|
223 | 229 | s.setPen(QPen(Qt::black)); // TODO: get from theme |
|
224 | 230 | s.setBrush(colors.takeFirst()); |
|
225 | 231 | series->update(s); |
|
226 | 232 | } |
|
227 | 233 | } |
|
228 | 234 | |
|
229 | 235 | |
|
230 | 236 | void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) |
|
231 | 237 | { |
|
232 | 238 | //TODO: dummy defults for now |
|
233 | 239 | |
|
234 | 240 | axis.setLabelsBrush(Qt::black); |
|
235 | 241 | axis.setLabelsPen(Qt::NoPen); |
|
236 | 242 | item->handleAxisChanged(axis); |
|
237 | 243 | } |
|
238 | 244 | |
|
239 | 245 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now