@@ -1,66 +1,66 | |||
|
1 | #include "bar.h" | |
|
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 | void Bar::setPlotDomain(const PlotDomain& data) |
|
19 | 19 | { |
|
20 | 20 | mPlotDomain = data; |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | void Bar::resize( qreal w, qreal h ) |
|
24 | 24 | { |
|
25 | 25 | // qDebug() << "bar::resize" << w << h; |
|
26 | 26 | mWidth = w; |
|
27 | 27 | mHeight = h; |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | void Bar::setColor( QColor col ) |
|
31 | 31 | { |
|
32 | 32 | mColor = col; |
|
33 | 33 | } |
|
34 | 34 | void Bar::setPos(qreal x, qreal y) |
|
35 | 35 | { |
|
36 | 36 | // qDebug() << "Bar::setpos" << x << y; |
|
37 | 37 | mXpos = x; |
|
38 | 38 | mYpos = y; |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
42 | 42 | { |
|
43 | 43 | if (0 == mHeight) { |
|
44 | 44 | return; |
|
45 | 45 | } |
|
46 | 46 | // TODO: accept brush instead of color |
|
47 | 47 | QBrush brush(mColor); |
|
48 | 48 | painter->setBrush(brush); |
|
49 | 49 | |
|
50 | 50 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. |
|
51 | 51 | int x0 = mXpos; |
|
52 | 52 | int x1 = (mXpos + mWidth); |
|
53 | 53 | int w = x1-x0; |
|
54 | 54 | int y0 = mYpos; |
|
55 | 55 | int y1 = (mYpos + mHeight); |
|
56 | 56 | int h = y1-y0; |
|
57 | 57 | painter->drawRect(x0, y0 ,w ,h); |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | QRectF Bar::boundingRect() const |
|
61 | 61 | { |
|
62 | 62 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); |
|
63 | 63 | return r; |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | QTCOMMERCIALCHART_END_NAMESPACE |
|
1 | NO CONTENT: file renamed from src/barchart/bar.h to src/barchart/bar_p.h |
@@ -1,157 +1,157 | |||
|
1 | 1 | #include "bargroup.h" |
|
2 | #include "bar.h" | |
|
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(BarChartSeries& series, QGraphicsItem *parent) : |
|
9 | 9 | ChartItem(parent) |
|
10 | 10 | ,mSeries(series) |
|
11 | 11 | ,mLayoutSet(false) |
|
12 | 12 | ,mLayoutDirty(true) |
|
13 | 13 | ,mBarDefaultWidth(10) |
|
14 | 14 | { |
|
15 | 15 | dataChanged(); |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | void BarGroup::setSize(const QSizeF& size) |
|
19 | 19 | { |
|
20 | 20 | qDebug() << "BarGroup::setSize"; |
|
21 | 21 | mWidth = size.width(); |
|
22 | 22 | mHeight = size.height(); |
|
23 | 23 | layoutChanged(); |
|
24 | 24 | mLayoutSet = true; |
|
25 | 25 | } |
|
26 | 26 | |
|
27 | 27 | void BarGroup::setPlotDomain(const PlotDomain& data) |
|
28 | 28 | { |
|
29 | 29 | qDebug() << "BarGroup::setPlotDomain"; |
|
30 | 30 | // TODO: |
|
31 | 31 | mPlotDomain = data; |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | void BarGroup::setBarWidth( int w ) |
|
35 | 35 | { |
|
36 | 36 | mBarDefaultWidth = w; |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | int BarGroup::addColor( QColor color ) |
|
40 | 40 | { |
|
41 | 41 | int colorIndex = mColors.count(); |
|
42 | 42 | mColors.append(color); |
|
43 | 43 | return colorIndex; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void BarGroup::resetColors() |
|
47 | 47 | { |
|
48 | 48 | mColors.clear(); |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
52 | 52 | { |
|
53 | 53 | if (!mLayoutSet) { |
|
54 | 54 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
55 | 55 | return; |
|
56 | 56 | } |
|
57 | 57 | if (mLayoutDirty) { |
|
58 | 58 | // Layout or data has changed. Need to redraw. |
|
59 | 59 | foreach(QGraphicsItem* i, childItems()) { |
|
60 | 60 | i->paint(painter,option,widget); |
|
61 | 61 | mLayoutDirty = false; |
|
62 | 62 | } |
|
63 | 63 | } |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | QRectF BarGroup::boundingRect() const |
|
67 | 67 | { |
|
68 | 68 | return QRectF(0,0,mWidth,mHeight); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | void BarGroup::dataChanged() |
|
73 | 73 | { |
|
74 | 74 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
75 | 75 | |
|
76 | 76 | // Find out maximum and minimum of all series |
|
77 | 77 | mMax = mSeries.max(); |
|
78 | 78 | mMin = mSeries.min(); |
|
79 | 79 | |
|
80 | 80 | // Delete old bars |
|
81 | 81 | // Is this correct way to delete childItems? |
|
82 | 82 | foreach (QGraphicsItem* item, childItems()) { |
|
83 | 83 | delete item; |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | // Create new graphic items for bars |
|
87 | 87 | int totalItems = mSeries.countTotalItems(); |
|
88 | 88 | for (int i=0; i<totalItems; i++) { |
|
89 | 89 | Bar *bar = new Bar(this); |
|
90 | 90 | childItems().append(bar); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | // TODO: labels from series. This creates just some example labels |
|
94 | 94 | int count = mSeries.countColumns(); |
|
95 | 95 | for (int i=0; i<count; i++) { |
|
96 | 96 | BarLabel* label = new BarLabel(this); |
|
97 | 97 | QString text("Label " + QString::number(i)); |
|
98 | 98 | label->set(text); |
|
99 | 99 | childItems().append(label); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | // TODO: if (autolayout) { layoutChanged() } or something |
|
103 | 103 | mLayoutDirty = true; |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | void BarGroup::layoutChanged() |
|
107 | 107 | { |
|
108 | 108 | // Scale bars to new layout |
|
109 | 109 | // Layout for bars: |
|
110 | 110 | if (mSeries.countRows() <= 0) { |
|
111 | 111 | // Nothing to do. |
|
112 | 112 | return; |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | // TODO: better way to auto-layout? |
|
116 | 116 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
117 | 117 | int columnCount = mSeries.countColumns(); |
|
118 | 118 | int rowCount = mSeries.countRows(); |
|
119 | 119 | |
|
120 | 120 | qreal tW = mWidth; |
|
121 | 121 | qreal tH = mHeight; |
|
122 | 122 | qreal tM = mMax; |
|
123 | 123 | qreal scale = (tH/tM); |
|
124 | 124 | |
|
125 | 125 | qreal tC = columnCount+1; |
|
126 | 126 | qreal xStepPerSeries = (tW/tC); |
|
127 | 127 | |
|
128 | 128 | // Scaling. |
|
129 | 129 | int itemIndex(0); |
|
130 | 130 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
131 | 131 | |
|
132 | 132 | for (int column=0; column < columnCount; column++) { |
|
133 | 133 | qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); |
|
134 | 134 | qreal yPos = mHeight; |
|
135 | 135 | for (int row = 0; row < rowCount; row++) { |
|
136 | 136 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
137 | 137 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
138 | 138 | |
|
139 | 139 | // TODO: width settable per bar? |
|
140 | 140 | bar->resize(mBarDefaultWidth, barHeight); |
|
141 | 141 | bar->setColor(mColors.at(row)); |
|
142 | 142 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); |
|
143 | 143 | itemIndex++; |
|
144 | 144 | xPos += mBarDefaultWidth; |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | 147 | // TODO: Layout for labels, remove magic number |
|
148 | 148 | xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); |
|
149 | 149 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
150 | 150 | label->setPos(xPos, mHeight + 20); |
|
151 | 151 | labelIndex++; |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | mLayoutDirty = true; |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,57 +1,57 | |||
|
1 | 1 | #ifndef QBARGROUP_H |
|
2 | 2 | #define QBARGROUP_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | #include "bar.h" | |
|
5 | #include "bar_p.h" | |
|
6 | 6 | #include "barchartseries.h" |
|
7 | 7 | #include <QGraphicsItem> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class BarGroup : public ChartItem |
|
12 | 12 | { |
|
13 | 13 | public: |
|
14 | 14 | explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); |
|
15 | 15 | |
|
16 | 16 | public: // from ChartItem |
|
17 | 17 | void setSize(const QSizeF &size); |
|
18 | 18 | void setPlotDomain(const PlotDomain& data); |
|
19 | 19 | |
|
20 | 20 | // Layout "api" |
|
21 | 21 | void setPos(qreal x, qreal y); |
|
22 | 22 | void setBarWidth( int w ); // Default width for each bar |
|
23 | 23 | |
|
24 | 24 | int addColor( QColor color ); |
|
25 | 25 | void resetColors(); |
|
26 | 26 | |
|
27 | 27 | // From QGraphicsItem |
|
28 | 28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
29 | 29 | QRectF boundingRect() const; |
|
30 | 30 | |
|
31 | 31 | private: |
|
32 | 32 | |
|
33 | 33 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
34 | 34 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
35 | 35 | |
|
36 | 36 | private: |
|
37 | 37 | |
|
38 | 38 | // Data |
|
39 | 39 | BarChartSeries& mSeries; |
|
40 | 40 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
41 | 41 | int mMax; |
|
42 | 42 | |
|
43 | 43 | int mHeight; // Layout spesific |
|
44 | 44 | int mWidth; |
|
45 | 45 | int mBarDefaultWidth; |
|
46 | 46 | |
|
47 | 47 | bool mLayoutSet; // True, if component has been laid out. |
|
48 | 48 | bool mLayoutDirty; |
|
49 | 49 | |
|
50 | 50 | QList<QColor> mColors; // List of colors for series for now |
|
51 | 51 | |
|
52 | 52 | PlotDomain mPlotDomain; |
|
53 | 53 | }; |
|
54 | 54 | |
|
55 | 55 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | 56 | |
|
57 | 57 | #endif // QBARGROUP_H |
@@ -1,152 +1,152 | |||
|
1 | 1 | #include "percentbargroup.h" |
|
2 | #include "bar.h" | |
|
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 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : |
|
9 | 9 | ChartItem(parent) |
|
10 | 10 | ,mSeries(series) |
|
11 | 11 | ,mLayoutSet(false) |
|
12 | 12 | ,mLayoutDirty(true) |
|
13 | 13 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
14 | 14 | { |
|
15 | 15 | dataChanged(); |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | void PercentBarGroup::setSize(const QSizeF& size) |
|
20 | 20 | { |
|
21 | 21 | // qDebug() << "PercentBarGroup::setSize"; |
|
22 | 22 | mWidth = size.width(); |
|
23 | 23 | mHeight = size.height(); |
|
24 | 24 | layoutChanged(); |
|
25 | 25 | mLayoutSet = true; |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | void PercentBarGroup::setPlotDomain(const PlotDomain& data) |
|
29 | 29 | { |
|
30 | 30 | qDebug() << "PercentBarGroup::setPlotDomain"; |
|
31 | 31 | // TODO: |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | void PercentBarGroup::setBarWidth( int w ) |
|
35 | 35 | { |
|
36 | 36 | mBarDefaultWidth = w; |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | int PercentBarGroup::addColor( QColor color ) |
|
40 | 40 | { |
|
41 | 41 | int colorIndex = mColors.count(); |
|
42 | 42 | mColors.append(color); |
|
43 | 43 | return colorIndex; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void PercentBarGroup::resetColors() |
|
47 | 47 | { |
|
48 | 48 | mColors.clear(); |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
52 | 52 | { |
|
53 | 53 | if (!mLayoutSet) { |
|
54 | 54 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
55 | 55 | return; |
|
56 | 56 | } |
|
57 | 57 | if (mLayoutDirty) { |
|
58 | 58 | // Layout or data has changed. Need to redraw. |
|
59 | 59 | foreach(QGraphicsItem* i, childItems()) { |
|
60 | 60 | i->paint(painter,option,widget); |
|
61 | 61 | } |
|
62 | 62 | mLayoutDirty = false; |
|
63 | 63 | } |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | QRectF PercentBarGroup::boundingRect() const |
|
67 | 67 | { |
|
68 | 68 | return QRectF(0,0,mWidth,mHeight); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | void PercentBarGroup::dataChanged() |
|
73 | 73 | { |
|
74 | 74 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
75 | 75 | |
|
76 | 76 | // Find out maximum and minimum of all series |
|
77 | 77 | mMax = mSeries.max(); |
|
78 | 78 | mMin = mSeries.min(); |
|
79 | 79 | |
|
80 | 80 | // Delete old bars |
|
81 | 81 | // Is this correct way to delete childItems? |
|
82 | 82 | foreach (QGraphicsItem* item, childItems()) { |
|
83 | 83 | delete item; |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | // Create new graphic items for bars |
|
87 | 87 | int totalItems = mSeries.countTotalItems(); |
|
88 | 88 | for (int i=0; i<totalItems; i++) { |
|
89 | 89 | Bar *bar = new Bar(this); |
|
90 | 90 | childItems().append(bar); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | // TODO: labels from series. This creates just some example labels |
|
94 | 94 | int count = mSeries.countColumns(); |
|
95 | 95 | for (int i=0; i<count; i++) { |
|
96 | 96 | BarLabel* label = new BarLabel(this); |
|
97 | 97 | QString text("Label " + QString::number(i)); |
|
98 | 98 | label->set(text); |
|
99 | 99 | childItems().append(label); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | // TODO: if (autolayout) { layoutChanged() } or something |
|
103 | 103 | mLayoutDirty = true; |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | void PercentBarGroup::layoutChanged() |
|
107 | 107 | { |
|
108 | 108 | // Scale bars to new layout |
|
109 | 109 | // Layout for bars: |
|
110 | 110 | if (mSeries.countRows() <= 0) { |
|
111 | 111 | // Nothing to do. |
|
112 | 112 | return; |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | // TODO: better way to auto-layout |
|
116 | 116 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
117 | 117 | int count = mSeries.countColumns(); |
|
118 | 118 | int itemIndex(0); |
|
119 | 119 | qreal tW = mWidth; |
|
120 | 120 | qreal tC = count+1; |
|
121 | 121 | qreal xStep = (tW/tC); |
|
122 | 122 | // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); |
|
123 | 123 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
124 | 124 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
125 | 125 | |
|
126 | 126 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
127 | 127 | qreal colSum = mSeries.columnSum(column); |
|
128 | 128 | qreal h = mHeight; |
|
129 | 129 | qreal scale = (h / colSum); |
|
130 | 130 | qreal yPos = h; |
|
131 | 131 | for (int row=0; row < mSeries.countRows(); row++) { |
|
132 | 132 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
133 | 133 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
134 | 134 | |
|
135 | 135 | // TODO: width settable per bar? |
|
136 | 136 | bar->resize(mBarDefaultWidth, barHeight); |
|
137 | 137 | bar->setColor(mColors.at(row)); |
|
138 | 138 | bar->setPos(xPos, yPos-barHeight); |
|
139 | 139 | itemIndex++; |
|
140 | 140 | yPos -= barHeight; |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | // TODO: Layout for labels, remove magic number |
|
144 | 144 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
145 | 145 | label->setPos(xPos, mHeight + 20); |
|
146 | 146 | labelIndex++; |
|
147 | 147 | xPos += xStep; |
|
148 | 148 | } |
|
149 | 149 | mLayoutDirty = true; |
|
150 | 150 | } |
|
151 | 151 | |
|
152 | 152 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,57 +1,57 | |||
|
1 | 1 | #ifndef PERCENTBARGROUP_H |
|
2 | 2 | #define PERCENTBARGROUP_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | #include "bar.h" | |
|
5 | #include "bar_p.h" | |
|
6 | 6 | #include "percentbarchartseries.h" |
|
7 | 7 | #include <QGraphicsItem> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class PercentBarGroup : public ChartItem |
|
12 | 12 | { |
|
13 | 13 | public: |
|
14 | 14 | PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0); |
|
15 | 15 | |
|
16 | 16 | public: // From ChartItem |
|
17 | 17 | void setSize(const QSizeF &size); |
|
18 | 18 | void setPlotDomain(const PlotDomain& data); |
|
19 | 19 | |
|
20 | 20 | public: |
|
21 | 21 | // Layout "api" |
|
22 | 22 | void setPos(qreal x, qreal y); |
|
23 | 23 | void setBarWidth( int w ); // Default width for each bar |
|
24 | 24 | |
|
25 | 25 | int addColor( QColor color ); |
|
26 | 26 | void resetColors(); |
|
27 | 27 | |
|
28 | 28 | // From QGraphicsItem |
|
29 | 29 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
30 | 30 | QRectF boundingRect() const; |
|
31 | 31 | |
|
32 | 32 | private: |
|
33 | 33 | |
|
34 | 34 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
35 | 35 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
36 | 36 | |
|
37 | 37 | private: |
|
38 | 38 | |
|
39 | 39 | // Data |
|
40 | 40 | PercentBarChartSeries& mSeries; |
|
41 | 41 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
42 | 42 | int mMax; |
|
43 | 43 | |
|
44 | 44 | int mHeight; // Layout spesific |
|
45 | 45 | int mWidth; |
|
46 | 46 | int mBarDefaultWidth; |
|
47 | 47 | |
|
48 | 48 | bool mLayoutSet; // True, if component has been laid out. |
|
49 | 49 | bool mLayoutDirty; |
|
50 | 50 | |
|
51 | 51 | QList<QColor> mColors; // List of colors for series for now |
|
52 | 52 | |
|
53 | 53 | }; |
|
54 | 54 | |
|
55 | 55 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | 56 | |
|
57 | 57 | #endif // PERCENTBARGROUP_H |
@@ -1,163 +1,163 | |||
|
1 | 1 | #include "stackedbargroup.h" |
|
2 | #include "bar.h" | |
|
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 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
9 | 9 | ChartItem(parent) |
|
10 | 10 | ,mSeries(series) |
|
11 | 11 | ,mLayoutSet(false) |
|
12 | 12 | ,mLayoutDirty(true) |
|
13 | 13 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
14 | 14 | ,mTheme(0) |
|
15 | 15 | { |
|
16 | 16 | dataChanged(); |
|
17 | 17 | } |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | void StackedBarGroup::setSize(const QSizeF& size) |
|
21 | 21 | { |
|
22 | 22 | // qDebug() << "StackedBarGroup::setSize"; |
|
23 | 23 | mWidth = size.width(); |
|
24 | 24 | mHeight = size.height(); |
|
25 | 25 | layoutChanged(); |
|
26 | 26 | mLayoutSet = true; |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | void StackedBarGroup::setPlotDomain(const PlotDomain& data) |
|
30 | 30 | { |
|
31 | 31 | qDebug() << "StackedBarGroup::setPlotDomain"; |
|
32 | 32 | // TODO: |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | void StackedBarGroup::themeChanged(ChartTheme *theme) |
|
36 | 36 | { |
|
37 | 37 | mTheme = theme; |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | void StackedBarGroup::setBarWidth( int w ) |
|
41 | 41 | { |
|
42 | 42 | mBarDefaultWidth = w; |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | int StackedBarGroup::addColor( QColor color ) |
|
46 | 46 | { |
|
47 | 47 | int colorIndex = mColors.count(); |
|
48 | 48 | mColors.append(color); |
|
49 | 49 | return colorIndex; |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | void StackedBarGroup::resetColors() |
|
53 | 53 | { |
|
54 | 54 | mColors.clear(); |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
58 | 58 | { |
|
59 | 59 | if (!mLayoutSet) { |
|
60 | 60 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
61 | 61 | return; |
|
62 | 62 | } |
|
63 | 63 | if (mLayoutDirty) { |
|
64 | 64 | // Layout or data has changed. Need to redraw. |
|
65 | 65 | foreach(QGraphicsItem* i, childItems()) { |
|
66 | 66 | i->paint(painter,option,widget); |
|
67 | 67 | } |
|
68 | 68 | mLayoutDirty = false; |
|
69 | 69 | //TODO: draw labels. |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | QRectF StackedBarGroup::boundingRect() const |
|
75 | 75 | { |
|
76 | 76 | return QRectF(0,0,mWidth,mHeight); |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | void StackedBarGroup::dataChanged() |
|
81 | 81 | { |
|
82 | 82 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
83 | 83 | |
|
84 | 84 | // Find out maximum and minimum of all series |
|
85 | 85 | mMax = mSeries.max(); |
|
86 | 86 | mMin = mSeries.min(); |
|
87 | 87 | |
|
88 | 88 | // Delete old bars |
|
89 | 89 | // Is this correct way to delete childItems? |
|
90 | 90 | foreach (QGraphicsItem* item, childItems()) { |
|
91 | 91 | delete item; |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | // Create new graphic items for bars |
|
95 | 95 | int totalItems = mSeries.countTotalItems(); |
|
96 | 96 | for (int i=0; i<totalItems; i++) { |
|
97 | 97 | Bar *bar = new Bar(this); |
|
98 | 98 | childItems().append(bar); |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | // TODO: labels from series. This creates just some example labels |
|
102 | 102 | int count = mSeries.countColumns(); |
|
103 | 103 | for (int i=0; i<count; i++) { |
|
104 | 104 | BarLabel* label = new BarLabel(this); |
|
105 | 105 | QString text("Label " + QString::number(i)); |
|
106 | 106 | label->set(text); |
|
107 | 107 | childItems().append(label); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | // TODO: if (autolayout) { layoutChanged() } or something |
|
111 | 111 | mLayoutDirty = true; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | void StackedBarGroup::layoutChanged() |
|
115 | 115 | { |
|
116 | 116 | // Scale bars to new layout |
|
117 | 117 | // Layout for bars: |
|
118 | 118 | if (mSeries.countRows() <= 0) { |
|
119 | 119 | // Nothing to do. |
|
120 | 120 | return; |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | // TODO: better way to auto-layout |
|
124 | 124 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
125 | 125 | qreal maxSum = mSeries.maxColumnSum(); |
|
126 | 126 | qreal h = mHeight; |
|
127 | 127 | qreal scale = (h / maxSum); |
|
128 | 128 | |
|
129 | 129 | int count = mSeries.countColumns(); |
|
130 | 130 | int itemIndex(0); |
|
131 | 131 | qreal tW = mWidth; |
|
132 | 132 | qreal tC = count+1; |
|
133 | 133 | qreal xStep = (tW/tC); |
|
134 | 134 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
135 | 135 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
136 | 136 | |
|
137 | 137 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
138 | 138 | qreal yPos = h; |
|
139 | 139 | for (int row=0; row < mSeries.countRows(); row++) { |
|
140 | 140 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
141 | 141 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
142 | 142 | |
|
143 | 143 | // TODO: width settable per bar? |
|
144 | 144 | // TODO: how to get color for series(x) from theme? |
|
145 | 145 | // mTheme->themeForSeries(); |
|
146 | 146 | bar->resize(mBarDefaultWidth, barHeight); |
|
147 | 147 | bar->setColor(mColors.at(row)); |
|
148 | 148 | bar->setPos(xPos, yPos-barHeight); |
|
149 | 149 | itemIndex++; |
|
150 | 150 | yPos -= barHeight; |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | // TODO: Layout for labels, remove magic number |
|
154 | 154 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
155 | 155 | label->setPos(xPos, mHeight + 20); |
|
156 | 156 | labelIndex++; |
|
157 | 157 | xPos += xStep; |
|
158 | 158 | } |
|
159 | 159 | |
|
160 | 160 | mLayoutDirty = true; |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,65 +1,65 | |||
|
1 | 1 | #ifndef STACKEDBARGROUP_H |
|
2 | 2 | #define STACKEDBARGROUP_H |
|
3 | 3 | |
|
4 | 4 | #include "charttheme_p.h" |
|
5 | 5 | #include "chartitem_p.h" |
|
6 | 6 | #include "barlabel_p.h" |
|
7 | #include "bar.h" | |
|
7 | #include "bar_p.h" | |
|
8 | 8 | #include "stackedbarchartseries.h" |
|
9 | 9 | #include <QGraphicsItem> |
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 12 | |
|
13 | 13 | // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem |
|
14 | 14 | class StackedBarGroup : public ChartItem, public ChartThemeObserver |
|
15 | 15 | { |
|
16 | 16 | public: |
|
17 | 17 | StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); |
|
18 | 18 | |
|
19 | 19 | public: // From ChartItem |
|
20 | 20 | void setSize(const QSizeF &size); |
|
21 | 21 | void setPlotDomain(const PlotDomain& data); |
|
22 | 22 | |
|
23 | 23 | // From ChartThemeObserver |
|
24 | 24 | void themeChanged(ChartTheme *theme); |
|
25 | 25 | |
|
26 | 26 | public: // Layout "api" |
|
27 | 27 | void setPos(qreal x, qreal y); |
|
28 | 28 | void setBarWidth( int w ); // Default width for each bar |
|
29 | 29 | |
|
30 | 30 | int addColor( QColor color ); |
|
31 | 31 | void resetColors(); |
|
32 | 32 | |
|
33 | 33 | // From QGraphicsItem |
|
34 | 34 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
35 | 35 | QRectF boundingRect() const; |
|
36 | 36 | |
|
37 | 37 | private: |
|
38 | 38 | |
|
39 | 39 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
40 | 40 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
41 | 41 | |
|
42 | 42 | private: |
|
43 | 43 | |
|
44 | 44 | // Data |
|
45 | 45 | StackedBarChartSeries& mSeries; |
|
46 | 46 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
47 | 47 | int mMax; |
|
48 | 48 | |
|
49 | 49 | int mHeight; // Layout spesific |
|
50 | 50 | int mWidth; |
|
51 | 51 | int mBarDefaultWidth; |
|
52 | 52 | |
|
53 | 53 | bool mLayoutSet; // True, if component has been laid out. |
|
54 | 54 | bool mLayoutDirty; |
|
55 | 55 | |
|
56 | 56 | QList<QColor> mColors; // List of colors for series for now |
|
57 | 57 | |
|
58 | 58 | ChartTheme* mTheme; |
|
59 | 59 | // QList<BarLabel*> mLabels; |
|
60 | 60 | |
|
61 | 61 | }; |
|
62 | 62 | |
|
63 | 63 | QTCOMMERCIALCHART_END_NAMESPACE |
|
64 | 64 | |
|
65 | 65 | #endif // STACKEDBARGROUP_H |
@@ -1,115 +1,116 | |||
|
1 | 1 | !include( ../common.pri ) { |
|
2 | 2 | error( Couldn't find the common.pri file! ) |
|
3 | 3 | } |
|
4 | 4 | |
|
5 | 5 | TARGET = QtCommercialChart |
|
6 | 6 | DESTDIR = $$CHART_BUILD_LIB_DIR |
|
7 | 7 | TEMPLATE = lib |
|
8 | 8 | QT += core \ |
|
9 | 9 | gui |
|
10 | 10 | |
|
11 | 11 | CONFIG += debug_and_release |
|
12 | 12 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd |
|
13 | 13 | |
|
14 | 14 | SOURCES += \ |
|
15 | 15 | barchart/barchartseries.cpp \ |
|
16 | 16 | barchart/bargroup.cpp \ |
|
17 | 17 | barchart/bar.cpp \ |
|
18 | 18 | barchart/stackedbarchartseries.cpp \ |
|
19 | 19 | barchart/stackedbargroup.cpp \ |
|
20 | 20 | barchart/percentbarchartseries.cpp \ |
|
21 | 21 | barchart/percentbargroup.cpp \ |
|
22 | 22 | barchart/barlabel.cpp \ |
|
23 | 23 | xylinechart/qxychartseries.cpp \ |
|
24 | 24 | xylinechart/xylinechartitem.cpp \ |
|
25 | 25 | plotdomain.cpp \ |
|
26 | 26 | qscatterseries.cpp \ |
|
27 | 27 | qpieseries.cpp \ |
|
28 | 28 | qchart.cpp \ |
|
29 | 29 | axisitem.cpp \ |
|
30 | 30 | qchartwidget.cpp \ |
|
31 | 31 | pieslice.cpp \ |
|
32 | 32 | qchartview.cpp \ |
|
33 | 33 | qchartseries.cpp \ |
|
34 | 34 | qchartaxis.cpp \ |
|
35 | 35 | charttheme.cpp |
|
36 | 36 | |
|
37 | 37 | PRIVATE_HEADERS += \ |
|
38 | 38 | xylinechart/xylinechartitem_p.h \ |
|
39 | 39 | barchart/barlabel_p.h \ |
|
40 | barchart/bar_p.h \ | |
|
40 | 41 | plotdomain_p.h \ |
|
41 | 42 | qscatterseries_p.h \ |
|
42 | 43 | qpieseries_p.h \ |
|
43 | 44 | pieslice.h \ |
|
44 | 45 | axisitem_p.h \ |
|
45 | 46 | chartitem_p.h \ |
|
46 | 47 | charttheme_p.h |
|
47 | 48 | |
|
48 | 49 | PUBLIC_HEADERS += \ |
|
49 | 50 | qchartseries.h \ |
|
50 | 51 | qscatterseries.h \ |
|
51 | 52 | qpieseries.h \ |
|
52 | 53 | qchart.h \ |
|
53 | 54 | qchartwidget.h \ |
|
54 | 55 | qchartglobal.h \ |
|
55 | 56 | xylinechart/qxychartseries.h \ |
|
56 | 57 | barchart/barchartseries.h \ |
|
57 | 58 | barchart/bargroup.h \ |
|
58 | 59 | barchart/stackedbarchartseries.h \ |
|
59 | 60 | barchart/stackedbargroup.h \ |
|
60 | 61 | barchart/percentbarchartseries.h \ |
|
61 | 62 | barchart/percentbargroup.h \ |
|
62 | 63 | qchartview.h \ |
|
63 | 64 | qchartaxis.h |
|
64 | 65 | |
|
65 | 66 | HEADERS += $$PUBLIC_HEADERS |
|
66 | 67 | HEADERS += $$PRIVATE_HEADERS |
|
67 | 68 | |
|
68 | 69 | INCLUDEPATH += xylinechart \ |
|
69 | 70 | barchart \ |
|
70 | 71 | . |
|
71 | 72 | |
|
72 | 73 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib |
|
73 | 74 | MOC_DIR = $$CHART_BUILD_DIR/lib |
|
74 | 75 | UI_DIR = $$CHART_BUILD_DIR/lib |
|
75 | 76 | RCC_DIR = $$CHART_BUILD_DIR/lib |
|
76 | 77 | |
|
77 | 78 | |
|
78 | 79 | DEFINES += QTCOMMERCIALCHART_LIBRARY |
|
79 | 80 | |
|
80 | 81 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart |
|
81 | 82 | public_headers.files = $$PUBLIC_HEADERS |
|
82 | 83 | target.path = $$[QT_INSTALL_LIBS] |
|
83 | 84 | INSTALLS += target \ |
|
84 | 85 | public_headers |
|
85 | 86 | |
|
86 | 87 | |
|
87 | 88 | install_build_headers.name = bild_headers |
|
88 | 89 | install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
89 | 90 | install_build_headers.input = PUBLIC_HEADERS |
|
90 | 91 | install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR |
|
91 | 92 | install_build_headers.CONFIG += target_predeps no_link |
|
92 | 93 | QMAKE_EXTRA_COMPILERS += install_build_headers |
|
93 | 94 | |
|
94 | 95 | chartversion.target = qchartversion_p.h |
|
95 | 96 | chartversion.commands = @echo "build_time" > $$chartversion.target; |
|
96 | 97 | chartversion.depends = $$HEADERS $$SOURCES |
|
97 | 98 | PRE_TARGETDEPS += qchartversion_p.h |
|
98 | 99 | QMAKE_CLEAN+= qchartversion_p.h |
|
99 | 100 | QMAKE_EXTRA_TARGETS += chartversion |
|
100 | 101 | |
|
101 | 102 | unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
102 | 103 | win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
103 | 104 | |
|
104 | 105 | |
|
105 | 106 | |
|
106 | 107 | |
|
107 | 108 | |
|
108 | 109 | |
|
109 | 110 | |
|
110 | 111 | |
|
111 | 112 | |
|
112 | 113 | |
|
113 | 114 | |
|
114 | 115 | |
|
115 | 116 |
General Comments 0
You need to be logged in to leave comments.
Login now