@@ -1,58 +1,66 | |||||
1 | #include "bar.h" |
|
1 | #include "bar.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QPainter> |
|
3 | #include <QPainter> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | Bar::Bar(QGraphicsItem *parent) |
|
7 | Bar::Bar(QGraphicsItem *parent) | |
8 | : ChartItem(parent) |
|
8 | : ChartItem(parent) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
12 | void Bar::setSize(const QSize& size) |
|
12 | void Bar::setSize(const QSize& size) | |
13 | { |
|
13 | { | |
14 | mWidth = size.width(); |
|
14 | mWidth = size.width(); | |
15 | mHeight = size.height(); |
|
15 | mHeight = size.height(); | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | void Bar::setPlotDomain(const PlotDomain& data) |
|
18 | void Bar::setPlotDomain(const PlotDomain& data) | |
19 | { |
|
19 | { | |
20 | mPlotDomain = data; |
|
20 | mPlotDomain = data; | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 |
void Bar::resize( |
|
23 | void Bar::resize( qreal w, qreal h ) | |
24 | { |
|
24 | { | |
25 | // qDebug() << "bar::resize" << w << h; |
|
25 | // qDebug() << "bar::resize" << w << h; | |
26 | mWidth = w; |
|
26 | mWidth = w; | |
27 | mHeight = h; |
|
27 | mHeight = h; | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | void Bar::setColor( QColor col ) |
|
30 | void Bar::setColor( QColor col ) | |
31 | { |
|
31 | { | |
32 | mColor = col; |
|
32 | mColor = col; | |
33 | } |
|
33 | } | |
34 | void Bar::setPos(qreal x, qreal y) |
|
34 | void Bar::setPos(qreal x, qreal y) | |
35 | { |
|
35 | { | |
36 | // qDebug() << "Bar::setpos" << x << y; |
|
36 | // qDebug() << "Bar::setpos" << x << y; | |
37 | mXpos = x; |
|
37 | mXpos = x; | |
38 | mYpos = y; |
|
38 | mYpos = y; | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
41 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
42 | { |
|
42 | { | |
43 | if (0 == mHeight) { |
|
43 | if (0 == mHeight) { | |
44 | return; |
|
44 | return; | |
45 | } |
|
45 | } | |
46 | // TODO: accept brush instead of color |
|
46 | // TODO: accept brush instead of color | |
47 | QBrush brush(mColor); |
|
47 | QBrush brush(mColor); | |
48 | painter->setBrush(brush); |
|
48 | painter->setBrush(brush); | |
49 | painter->drawRect(mXpos-mWidth, mYpos-mHeight ,mWidth ,mHeight); // Evil inverse rect, because we want bars to grow from bottom to top :) |
|
49 | ||
|
50 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. | |||
|
51 | int x0 = mXpos; | |||
|
52 | int x1 = (mXpos + mWidth); | |||
|
53 | int w = x1-x0; | |||
|
54 | int y0 = mYpos; | |||
|
55 | int y1 = (mYpos + mHeight); | |||
|
56 | int h = y1-y0; | |||
|
57 | painter->drawRect(x0, y0 ,w ,h); | |||
50 | } |
|
58 | } | |
51 |
|
59 | |||
52 | QRectF Bar::boundingRect() const |
|
60 | QRectF Bar::boundingRect() const | |
53 | { |
|
61 | { | |
54 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); |
|
62 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); | |
55 | return r; |
|
63 | return r; | |
56 | } |
|
64 | } | |
57 |
|
65 | |||
58 | QTCOMMERCIALCHART_END_NAMESPACE |
|
66 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,44 +1,44 | |||||
1 | #ifndef BAR_H |
|
1 | #ifndef BAR_H | |
2 | #define BAR_H |
|
2 | #define BAR_H | |
3 |
|
3 | |||
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 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | // Single bar item of chart |
|
10 | // Single bar item of chart | |
11 | class Bar : public ChartItem |
|
11 | class Bar : public ChartItem | |
12 | { |
|
12 | { | |
13 | public: |
|
13 | public: | |
14 | Bar(QGraphicsItem *parent=0); |
|
14 | Bar(QGraphicsItem *parent=0); | |
15 |
|
15 | |||
16 |
public: // from ChartItem |
|
16 | public: // from ChartItem | |
17 | void setSize(const QSize &size); |
|
17 | void setSize(const QSize &size); | |
18 | void setPlotDomain(const PlotDomain& data); |
|
18 | void setPlotDomain(const PlotDomain& data); | |
19 |
|
19 | |||
20 | // Layout Stuff |
|
20 | // Layout Stuff | |
21 |
void resize( |
|
21 | void resize( qreal w, qreal h ); // Size of bar. | |
22 | void setColor( QColor col ); // Color of bar |
|
22 | void setColor( QColor col ); // Color of bar | |
23 | void setPos(qreal x, qreal y); |
|
23 | void setPos(qreal x, qreal y); | |
24 |
|
24 | |||
25 | public: |
|
25 | public: | |
26 | // From QGraphicsItem |
|
26 | // From QGraphicsItem | |
27 |
|
27 | |||
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |
29 | QRectF boundingRect() const; |
|
29 | QRectF boundingRect() const; | |
30 |
|
30 | |||
31 | private: |
|
31 | private: | |
32 |
|
32 | |||
33 |
|
|
33 | qreal mHeight; | |
34 |
|
|
34 | qreal mWidth; | |
35 | qreal mXpos; |
|
35 | qreal mXpos; | |
36 | qreal mYpos; |
|
36 | qreal mYpos; | |
37 | QColor mColor; |
|
37 | QColor mColor; | |
38 |
|
38 | |||
39 | PlotDomain mPlotDomain; |
|
39 | PlotDomain mPlotDomain; | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 | QTCOMMERCIALCHART_END_NAMESPACE |
|
42 | QTCOMMERCIALCHART_END_NAMESPACE | |
43 |
|
43 | |||
44 | #endif // BAR_H |
|
44 | #endif // BAR_H |
@@ -1,138 +1,139 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar.h" |
|
2 | #include "bar.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : |
|
7 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | |
8 | ChartItem(parent) |
|
8 | ChartItem(parent) | |
9 | ,mSeries(series) |
|
9 | ,mSeries(series) | |
10 | ,mLayoutSet(false) |
|
10 | ,mLayoutSet(false) | |
11 | ,mLayoutDirty(true) |
|
11 | ,mLayoutDirty(true) | |
12 | ,mBarDefaultWidth(10) |
|
12 | ,mBarDefaultWidth(10) | |
13 | { |
|
13 | { | |
14 | dataChanged(); |
|
14 | dataChanged(); | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 | void BarGroup::setSize(const QSize& size) |
|
17 | void BarGroup::setSize(const QSize& size) | |
18 | { |
|
18 | { | |
19 | qDebug() << "BarGroup::setSize"; |
|
19 | qDebug() << "BarGroup::setSize"; | |
20 | mWidth = size.width(); |
|
20 | mWidth = size.width(); | |
21 | mHeight = size.height(); |
|
21 | mHeight = size.height(); | |
22 | layoutChanged(); |
|
22 | layoutChanged(); | |
23 | mLayoutSet = true; |
|
23 | mLayoutSet = true; | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | void BarGroup::setPlotDomain(const PlotDomain& data) |
|
26 | void BarGroup::setPlotDomain(const PlotDomain& data) | |
27 | { |
|
27 | { | |
28 | qDebug() << "BarGroup::setPlotDomain"; |
|
28 | qDebug() << "BarGroup::setPlotDomain"; | |
29 | // TODO: |
|
29 | // TODO: | |
30 | mPlotDomain = data; |
|
30 | mPlotDomain = data; | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | void BarGroup::setBarWidth( int w ) |
|
33 | void BarGroup::setBarWidth( int w ) | |
34 | { |
|
34 | { | |
35 | mBarDefaultWidth = w; |
|
35 | mBarDefaultWidth = w; | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | int BarGroup::addColor( QColor color ) |
|
38 | int BarGroup::addColor( QColor color ) | |
39 | { |
|
39 | { | |
40 | int colorIndex = mColors.count(); |
|
40 | int colorIndex = mColors.count(); | |
41 | mColors.append(color); |
|
41 | mColors.append(color); | |
42 | return colorIndex; |
|
42 | return colorIndex; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | void BarGroup::resetColors() |
|
45 | void BarGroup::resetColors() | |
46 | { |
|
46 | { | |
47 | mColors.clear(); |
|
47 | mColors.clear(); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
50 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
51 | { |
|
51 | { | |
52 | if (!mLayoutSet) { |
|
52 | if (!mLayoutSet) { | |
53 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
53 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
54 | return; |
|
54 | return; | |
55 | } |
|
55 | } | |
56 | if (mLayoutDirty) { |
|
56 | if (mLayoutDirty) { | |
57 | // Layout or data has changed. Need to redraw. |
|
57 | // Layout or data has changed. Need to redraw. | |
58 | foreach(QGraphicsItem* i, childItems()) { |
|
58 | foreach(QGraphicsItem* i, childItems()) { | |
59 | i->paint(painter,option,widget); |
|
59 | i->paint(painter,option,widget); | |
60 | } |
|
60 | } | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | QRectF BarGroup::boundingRect() const |
|
64 | QRectF BarGroup::boundingRect() const | |
65 | { |
|
65 | { | |
66 | return QRectF(0,0,mWidth,mHeight); |
|
66 | return QRectF(0,0,mWidth,mHeight); | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | void BarGroup::dataChanged() |
|
70 | void BarGroup::dataChanged() | |
71 | { |
|
71 | { | |
72 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
72 | qDebug() << "QBarChart::dataChanged mSeries"; | |
73 |
|
73 | |||
74 | // Find out maximum and minimum of all series |
|
74 | // Find out maximum and minimum of all series | |
75 | mMax = mSeries.max(); |
|
75 | mMax = mSeries.max(); | |
76 | mMin = mSeries.min(); |
|
76 | mMin = mSeries.min(); | |
77 |
|
77 | |||
78 | // Delete old bars |
|
78 | // Delete old bars | |
79 | // Is this correct way to delete childItems? |
|
79 | // Is this correct way to delete childItems? | |
80 | foreach (QGraphicsItem* item, childItems()) { |
|
80 | foreach (QGraphicsItem* item, childItems()) { | |
81 | delete item; |
|
81 | delete item; | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | // Create new graphic items for bars |
|
84 | // Create new graphic items for bars | |
85 | int totalItems = mSeries.countTotalItems(); |
|
85 | int totalItems = mSeries.countTotalItems(); | |
86 | for (int i=0; i<totalItems; i++) { |
|
86 | for (int i=0; i<totalItems; i++) { | |
87 | Bar *bar = new Bar(this); |
|
87 | Bar *bar = new Bar(this); | |
88 | childItems().append(bar); |
|
88 | childItems().append(bar); | |
89 | } |
|
89 | } | |
90 |
|
90 | |||
91 | mLayoutDirty = true; |
|
91 | mLayoutDirty = true; | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | void BarGroup::layoutChanged() |
|
94 | void BarGroup::layoutChanged() | |
95 | { |
|
95 | { | |
96 | // Scale bars to new layout |
|
96 | // Scale bars to new layout | |
97 | // Layout for bars: |
|
97 | // Layout for bars: | |
98 | if (mSeries.countRows() <= 0) { |
|
98 | if (mSeries.countRows() <= 0) { | |
99 | // Nothing to do. |
|
99 | // Nothing to do. | |
100 | return; |
|
100 | return; | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | // TODO: better way to auto-layout? |
|
103 | // TODO: better way to auto-layout? | |
104 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
104 | // Use reals for accurancy (we might get some compiler warnings... :) | |
105 | int columnCount = mSeries.countColumns(); |
|
105 | int columnCount = mSeries.countColumns(); | |
106 | int rowCount = mSeries.countRows(); |
|
106 | int rowCount = mSeries.countRows(); | |
107 |
|
107 | |||
108 | qreal tW = mWidth; |
|
108 | qreal tW = mWidth; | |
109 | qreal tH = mHeight; |
|
109 | qreal tH = mHeight; | |
110 | qreal tM = mMax; |
|
110 | qreal tM = mMax; | |
111 | qreal scale = (tH/tM); |
|
111 | qreal scale = (tH/tM); | |
112 |
|
112 | |||
113 | qreal tC = columnCount+1; |
|
113 | qreal tC = columnCount+1; | |
114 | qreal xStepPerSeries = (tW/tC); |
|
114 | qreal xStepPerSeries = (tW/tC); | |
115 |
|
115 | |||
116 | qDebug() << "XSTEP:" << xStepPerSeries; |
|
116 | qDebug() << "XSTEP:" << xStepPerSeries; | |
117 |
|
117 | |||
118 | // Scaling. |
|
118 | // Scaling. | |
119 | int itemIndex(0); |
|
119 | int itemIndex(0); | |
120 | for (int column=0; column < columnCount; column++) { |
|
120 | for (int column=0; column < columnCount; column++) { | |
121 | qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); |
|
121 | qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | |
|
122 | qreal yPos = mHeight; | |||
122 | for (int row = 0; row < rowCount; row++) { |
|
123 | for (int row = 0; row < rowCount; row++) { | |
123 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
124 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
124 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
125 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
125 |
|
126 | |||
126 | // TODO: width settable per bar? |
|
127 | // TODO: width settable per bar? | |
127 | bar->resize(mBarDefaultWidth, barHeight); |
|
128 | bar->resize(mBarDefaultWidth, barHeight); | |
128 | bar->setColor(mColors.at(row)); |
|
129 | bar->setColor(mColors.at(row)); | |
129 |
bar->setPos(xPos, |
|
130 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | |
130 | itemIndex++; |
|
131 | itemIndex++; | |
131 | xPos += mBarDefaultWidth; |
|
132 | xPos += mBarDefaultWidth; | |
132 | } |
|
133 | } | |
133 | } |
|
134 | } | |
134 |
|
135 | |||
135 | mLayoutDirty = true; |
|
136 | mLayoutDirty = true; | |
136 | } |
|
137 | } | |
137 |
|
138 | |||
138 | QTCOMMERCIALCHART_END_NAMESPACE |
|
139 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,133 +1,133 | |||||
1 | #include "percentbargroup.h" |
|
1 | #include "percentbargroup.h" | |
2 | #include "bar.h" |
|
2 | #include "bar.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : |
|
7 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : | |
8 | ChartItem(parent) |
|
8 | ChartItem(parent) | |
9 | ,mSeries(series) |
|
9 | ,mSeries(series) | |
10 | ,mLayoutSet(false) |
|
10 | ,mLayoutSet(false) | |
11 | ,mLayoutDirty(true) |
|
11 | ,mLayoutDirty(true) | |
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
13 | { |
|
13 | { | |
14 | dataChanged(); |
|
14 | dataChanged(); | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 |
|
17 | |||
18 | void PercentBarGroup::setSize(const QSize& size) |
|
18 | void PercentBarGroup::setSize(const QSize& size) | |
19 | { |
|
19 | { | |
20 | // qDebug() << "PercentBarGroup::setSize"; |
|
20 | // qDebug() << "PercentBarGroup::setSize"; | |
21 | mWidth = size.width(); |
|
21 | mWidth = size.width(); | |
22 | mHeight = size.height(); |
|
22 | mHeight = size.height(); | |
23 | layoutChanged(); |
|
23 | layoutChanged(); | |
24 | mLayoutSet = true; |
|
24 | mLayoutSet = true; | |
25 | } |
|
25 | } | |
26 |
|
26 | |||
27 | void PercentBarGroup::setPlotDomain(const PlotDomain& data) |
|
27 | void PercentBarGroup::setPlotDomain(const PlotDomain& data) | |
28 | { |
|
28 | { | |
29 | qDebug() << "PercentBarGroup::setPlotDomain"; |
|
29 | qDebug() << "PercentBarGroup::setPlotDomain"; | |
30 | // TODO: |
|
30 | // TODO: | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | void PercentBarGroup::setBarWidth( int w ) |
|
33 | void PercentBarGroup::setBarWidth( int w ) | |
34 | { |
|
34 | { | |
35 | mBarDefaultWidth = w; |
|
35 | mBarDefaultWidth = w; | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | int PercentBarGroup::addColor( QColor color ) |
|
38 | int PercentBarGroup::addColor( QColor color ) | |
39 | { |
|
39 | { | |
40 | int colorIndex = mColors.count(); |
|
40 | int colorIndex = mColors.count(); | |
41 | mColors.append(color); |
|
41 | mColors.append(color); | |
42 | return colorIndex; |
|
42 | return colorIndex; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | void PercentBarGroup::resetColors() |
|
45 | void PercentBarGroup::resetColors() | |
46 | { |
|
46 | { | |
47 | mColors.clear(); |
|
47 | mColors.clear(); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
50 | void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
51 | { |
|
51 | { | |
52 | if (!mLayoutSet) { |
|
52 | if (!mLayoutSet) { | |
53 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
53 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
54 | return; |
|
54 | return; | |
55 | } |
|
55 | } | |
56 | if (mLayoutDirty) { |
|
56 | if (mLayoutDirty) { | |
57 | // Layout or data has changed. Need to redraw. |
|
57 | // Layout or data has changed. Need to redraw. | |
58 | foreach(QGraphicsItem* i, childItems()) { |
|
58 | foreach(QGraphicsItem* i, childItems()) { | |
59 | i->paint(painter,option,widget); |
|
59 | i->paint(painter,option,widget); | |
60 | } |
|
60 | } | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | QRectF PercentBarGroup::boundingRect() const |
|
64 | QRectF PercentBarGroup::boundingRect() const | |
65 | { |
|
65 | { | |
66 | return QRectF(0,0,mWidth,mHeight); |
|
66 | return QRectF(0,0,mWidth,mHeight); | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | void PercentBarGroup::dataChanged() |
|
70 | void PercentBarGroup::dataChanged() | |
71 | { |
|
71 | { | |
72 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
72 | qDebug() << "QBarChart::dataChanged mSeries"; | |
73 |
|
73 | |||
74 | // Find out maximum and minimum of all series |
|
74 | // Find out maximum and minimum of all series | |
75 | mMax = mSeries.max(); |
|
75 | mMax = mSeries.max(); | |
76 | mMin = mSeries.min(); |
|
76 | mMin = mSeries.min(); | |
77 |
|
77 | |||
78 | // Delete old bars |
|
78 | // Delete old bars | |
79 | // Is this correct way to delete childItems? |
|
79 | // Is this correct way to delete childItems? | |
80 | foreach (QGraphicsItem* item, childItems()) { |
|
80 | foreach (QGraphicsItem* item, childItems()) { | |
81 | delete item; |
|
81 | delete item; | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | // Create new graphic items for bars |
|
84 | // Create new graphic items for bars | |
85 | int totalItems = mSeries.countTotalItems(); |
|
85 | int totalItems = mSeries.countTotalItems(); | |
86 | for (int i=0; i<totalItems; i++) { |
|
86 | for (int i=0; i<totalItems; i++) { | |
87 | Bar *bar = new Bar(this); |
|
87 | Bar *bar = new Bar(this); | |
88 | childItems().append(bar); |
|
88 | childItems().append(bar); | |
89 | } |
|
89 | } | |
90 |
|
90 | |||
91 | mLayoutDirty = true; |
|
91 | mLayoutDirty = true; | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | void PercentBarGroup::layoutChanged() |
|
94 | void PercentBarGroup::layoutChanged() | |
95 | { |
|
95 | { | |
96 | // Scale bars to new layout |
|
96 | // Scale bars to new layout | |
97 | // Layout for bars: |
|
97 | // Layout for bars: | |
98 | if (mSeries.countRows() <= 0) { |
|
98 | if (mSeries.countRows() <= 0) { | |
99 | // Nothing to do. |
|
99 | // Nothing to do. | |
100 | return; |
|
100 | return; | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | // TODO: better way to auto-layout |
|
103 | // TODO: better way to auto-layout | |
104 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
104 | // Use reals for accurancy (we might get some compiler warnings... :) | |
105 | int count = mSeries.countColumns(); |
|
105 | int count = mSeries.countColumns(); | |
106 | int itemIndex(0); |
|
106 | int itemIndex(0); | |
107 | qreal tW = mWidth; |
|
107 | qreal tW = mWidth; | |
108 | qreal tC = count+1; |
|
108 | qreal tC = count+1; | |
109 | qreal xStep = (tW/tC); |
|
109 | qreal xStep = (tW/tC); | |
110 | qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); |
|
110 | qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); | |
111 |
|
111 | |||
112 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
112 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
113 | qreal colSum = mSeries.columnSum(column); |
|
113 | qreal colSum = mSeries.columnSum(column); | |
114 | qreal h = mHeight; |
|
114 | qreal h = mHeight; | |
115 | qreal scale = (h / colSum); |
|
115 | qreal scale = (h / colSum); | |
116 | qreal yPos = h; |
|
116 | qreal yPos = h; | |
117 | for (int row=0; row < mSeries.countRows(); row++) { |
|
117 | for (int row=0; row < mSeries.countRows(); row++) { | |
118 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
118 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
119 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
119 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
120 |
|
120 | |||
121 | // TODO: width settable per bar? |
|
121 | // TODO: width settable per bar? | |
122 | bar->resize(mBarDefaultWidth, barHeight); |
|
122 | bar->resize(mBarDefaultWidth, barHeight); | |
123 | bar->setColor(mColors.at(row)); |
|
123 | bar->setColor(mColors.at(row)); | |
124 | bar->setPos(xPos, yPos); |
|
124 | bar->setPos(xPos, yPos-barHeight); | |
125 | itemIndex++; |
|
125 | itemIndex++; | |
126 | yPos -= barHeight; |
|
126 | yPos -= barHeight; | |
127 | } |
|
127 | } | |
128 | xPos += xStep; |
|
128 | xPos += xStep; | |
129 | } |
|
129 | } | |
130 | mLayoutDirty = true; |
|
130 | mLayoutDirty = true; | |
131 | } |
|
131 | } | |
132 |
|
132 | |||
133 | QTCOMMERCIALCHART_END_NAMESPACE |
|
133 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,134 +1,142 | |||||
1 | #include "stackedbargroup.h" |
|
1 | #include "stackedbargroup.h" | |
2 | #include "bar.h" |
|
2 | #include "bar.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
7 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : | |
8 | ChartItem(parent) |
|
8 | ChartItem(parent) | |
9 | ,mSeries(series) |
|
9 | ,mSeries(series) | |
10 | ,mLayoutSet(false) |
|
10 | ,mLayoutSet(false) | |
11 | ,mLayoutDirty(true) |
|
11 | ,mLayoutDirty(true) | |
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
|
13 | ,mTheme(0) | |||
13 | { |
|
14 | { | |
14 | dataChanged(); |
|
15 | dataChanged(); | |
15 | } |
|
16 | } | |
16 |
|
17 | |||
17 |
|
18 | |||
18 | void StackedBarGroup::setSize(const QSize& size) |
|
19 | void StackedBarGroup::setSize(const QSize& size) | |
19 | { |
|
20 | { | |
20 | // qDebug() << "StackedBarGroup::setSize"; |
|
21 | // qDebug() << "StackedBarGroup::setSize"; | |
21 | mWidth = size.width(); |
|
22 | mWidth = size.width(); | |
22 | mHeight = size.height(); |
|
23 | mHeight = size.height(); | |
23 | layoutChanged(); |
|
24 | layoutChanged(); | |
24 | mLayoutSet = true; |
|
25 | mLayoutSet = true; | |
25 | } |
|
26 | } | |
26 |
|
27 | |||
27 | void StackedBarGroup::setPlotDomain(const PlotDomain& data) |
|
28 | void StackedBarGroup::setPlotDomain(const PlotDomain& data) | |
28 | { |
|
29 | { | |
29 | qDebug() << "StackedBarGroup::setPlotDomain"; |
|
30 | qDebug() << "StackedBarGroup::setPlotDomain"; | |
30 | // TODO: |
|
31 | // TODO: | |
31 | } |
|
32 | } | |
32 |
|
33 | |||
|
34 | void StackedBarGroup::themeChanged(ChartTheme *theme) | |||
|
35 | { | |||
|
36 | mTheme = theme; | |||
|
37 | } | |||
|
38 | ||||
33 | void StackedBarGroup::setBarWidth( int w ) |
|
39 | void StackedBarGroup::setBarWidth( int w ) | |
34 | { |
|
40 | { | |
35 | mBarDefaultWidth = w; |
|
41 | mBarDefaultWidth = w; | |
36 | } |
|
42 | } | |
37 |
|
43 | |||
38 | int StackedBarGroup::addColor( QColor color ) |
|
44 | int StackedBarGroup::addColor( QColor color ) | |
39 | { |
|
45 | { | |
40 | int colorIndex = mColors.count(); |
|
46 | int colorIndex = mColors.count(); | |
41 | mColors.append(color); |
|
47 | mColors.append(color); | |
42 | return colorIndex; |
|
48 | return colorIndex; | |
43 | } |
|
49 | } | |
44 |
|
50 | |||
45 | void StackedBarGroup::resetColors() |
|
51 | void StackedBarGroup::resetColors() | |
46 | { |
|
52 | { | |
47 | mColors.clear(); |
|
53 | mColors.clear(); | |
48 | } |
|
54 | } | |
49 |
|
55 | |||
50 | void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
56 | void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
51 | { |
|
57 | { | |
52 | if (!mLayoutSet) { |
|
58 | if (!mLayoutSet) { | |
53 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
59 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
54 | return; |
|
60 | return; | |
55 | } |
|
61 | } | |
56 | if (mLayoutDirty) { |
|
62 | if (mLayoutDirty) { | |
57 | // Layout or data has changed. Need to redraw. |
|
63 | // Layout or data has changed. Need to redraw. | |
58 | foreach(QGraphicsItem* i, childItems()) { |
|
64 | foreach(QGraphicsItem* i, childItems()) { | |
59 | i->paint(painter,option,widget); |
|
65 | i->paint(painter,option,widget); | |
60 | } |
|
66 | } | |
61 | } |
|
67 | } | |
62 | } |
|
68 | } | |
63 |
|
69 | |||
64 | QRectF StackedBarGroup::boundingRect() const |
|
70 | QRectF StackedBarGroup::boundingRect() const | |
65 | { |
|
71 | { | |
66 | return QRectF(0,0,mWidth,mHeight); |
|
72 | return QRectF(0,0,mWidth,mHeight); | |
67 | } |
|
73 | } | |
68 |
|
74 | |||
69 |
|
75 | |||
70 | void StackedBarGroup::dataChanged() |
|
76 | void StackedBarGroup::dataChanged() | |
71 | { |
|
77 | { | |
72 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
78 | qDebug() << "QBarChart::dataChanged mSeries"; | |
73 |
|
79 | |||
74 | // Find out maximum and minimum of all series |
|
80 | // Find out maximum and minimum of all series | |
75 | mMax = mSeries.max(); |
|
81 | mMax = mSeries.max(); | |
76 | mMin = mSeries.min(); |
|
82 | mMin = mSeries.min(); | |
77 |
|
83 | |||
78 | // Delete old bars |
|
84 | // Delete old bars | |
79 | // Is this correct way to delete childItems? |
|
85 | // Is this correct way to delete childItems? | |
80 | foreach (QGraphicsItem* item, childItems()) { |
|
86 | foreach (QGraphicsItem* item, childItems()) { | |
81 | delete item; |
|
87 | delete item; | |
82 | } |
|
88 | } | |
83 |
|
89 | |||
84 | // Create new graphic items for bars |
|
90 | // Create new graphic items for bars | |
85 | int totalItems = mSeries.countTotalItems(); |
|
91 | int totalItems = mSeries.countTotalItems(); | |
86 | for (int i=0; i<totalItems; i++) { |
|
92 | for (int i=0; i<totalItems; i++) { | |
87 | Bar *bar = new Bar(this); |
|
93 | Bar *bar = new Bar(this); | |
88 | childItems().append(bar); |
|
94 | childItems().append(bar); | |
89 | } |
|
95 | } | |
90 |
|
96 | |||
91 | mLayoutDirty = true; |
|
97 | mLayoutDirty = true; | |
92 | } |
|
98 | } | |
93 |
|
99 | |||
94 | void StackedBarGroup::layoutChanged() |
|
100 | void StackedBarGroup::layoutChanged() | |
95 | { |
|
101 | { | |
96 | // Scale bars to new layout |
|
102 | // Scale bars to new layout | |
97 | // Layout for bars: |
|
103 | // Layout for bars: | |
98 | if (mSeries.countRows() <= 0) { |
|
104 | if (mSeries.countRows() <= 0) { | |
99 | // Nothing to do. |
|
105 | // Nothing to do. | |
100 | return; |
|
106 | return; | |
101 | } |
|
107 | } | |
102 |
|
108 | |||
103 | // TODO: better way to auto-layout |
|
109 | // TODO: better way to auto-layout | |
104 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
110 | // Use reals for accurancy (we might get some compiler warnings... :) | |
105 | qreal maxSum = mSeries.maxColumnSum(); |
|
111 | qreal maxSum = mSeries.maxColumnSum(); | |
106 | qreal h = mHeight; |
|
112 | qreal h = mHeight; | |
107 | qreal scale = (h / maxSum); |
|
113 | qreal scale = (h / maxSum); | |
108 |
|
114 | |||
109 | int count = mSeries.countColumns(); |
|
115 | int count = mSeries.countColumns(); | |
110 | int itemIndex(0); |
|
116 | int itemIndex(0); | |
111 | qreal tW = mWidth; |
|
117 | qreal tW = mWidth; | |
112 | qreal tC = count+1; |
|
118 | qreal tC = count+1; | |
113 | qreal xStep = (tW/tC); |
|
119 | qreal xStep = (tW/tC); | |
114 |
qreal xPos = ((tW/tC) |
|
120 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
115 |
|
121 | |||
116 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
122 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
117 | qreal yPos = h; |
|
123 | qreal yPos = h; | |
118 | for (int row=0; row < mSeries.countRows(); row++) { |
|
124 | for (int row=0; row < mSeries.countRows(); row++) { | |
119 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
125 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
120 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
126 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
121 |
|
127 | |||
122 | // TODO: width settable per bar? |
|
128 | // TODO: width settable per bar? | |
|
129 | // TODO: how to get color for series(x) from theme? | |||
|
130 | // mTheme->themeForSeries(); | |||
123 | bar->resize(mBarDefaultWidth, barHeight); |
|
131 | bar->resize(mBarDefaultWidth, barHeight); | |
124 | bar->setColor(mColors.at(row)); |
|
132 | bar->setColor(mColors.at(row)); | |
125 | bar->setPos(xPos, yPos); |
|
133 | bar->setPos(xPos, yPos-barHeight); | |
126 | itemIndex++; |
|
134 | itemIndex++; | |
127 | yPos -= barHeight; |
|
135 | yPos -= barHeight; | |
128 | } |
|
136 | } | |
129 | xPos += xStep; |
|
137 | xPos += xStep; | |
130 | } |
|
138 | } | |
131 | mLayoutDirty = true; |
|
139 | mLayoutDirty = true; | |
132 | } |
|
140 | } | |
133 |
|
141 | |||
134 | QTCOMMERCIALCHART_END_NAMESPACE |
|
142 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,57 +1,63 | |||||
1 | #ifndef STACKEDBARGROUP_H |
|
1 | #ifndef STACKEDBARGROUP_H | |
2 | #define STACKEDBARGROUP_H |
|
2 | #define STACKEDBARGROUP_H | |
3 |
|
3 | |||
|
4 | #include "charttheme_p.h" | |||
4 | #include "chartitem_p.h" |
|
5 | #include "chartitem_p.h" | |
5 | #include "bar.h" |
|
6 | #include "bar.h" | |
6 | #include "stackedbarchartseries.h" |
|
7 | #include "stackedbarchartseries.h" | |
7 | #include <QGraphicsItem> |
|
8 | #include <QGraphicsItem> | |
8 |
|
9 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
11 | |||
11 | // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem |
|
12 | // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem | |
12 | class StackedBarGroup : public ChartItem |
|
13 | class StackedBarGroup : public ChartItem, public ChartThemeObserver | |
13 | { |
|
14 | { | |
14 | public: |
|
15 | public: | |
15 | StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); |
|
16 | StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); | |
16 |
|
17 | |||
17 | public: // From ChartItem |
|
18 | public: // From ChartItem | |
18 | void setSize(const QSize &size); |
|
19 | void setSize(const QSize &size); | |
19 | void setPlotDomain(const PlotDomain& data); |
|
20 | void setPlotDomain(const PlotDomain& data); | |
20 |
|
21 | |||
|
22 | // From ChartThemeObserver | |||
|
23 | void themeChanged(ChartTheme *theme); | |||
|
24 | ||||
21 | public: // Layout "api" |
|
25 | public: // Layout "api" | |
22 | void setPos(qreal x, qreal y); |
|
26 | void setPos(qreal x, qreal y); | |
23 | void setBarWidth( int w ); // Default width for each bar |
|
27 | void setBarWidth( int w ); // Default width for each bar | |
24 |
|
28 | |||
25 | int addColor( QColor color ); |
|
29 | int addColor( QColor color ); | |
26 | void resetColors(); |
|
30 | void resetColors(); | |
27 |
|
31 | |||
28 | // From QGraphicsItem |
|
32 | // From QGraphicsItem | |
29 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
33 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
30 | QRectF boundingRect() const; |
|
34 | QRectF boundingRect() const; | |
31 |
|
35 | |||
32 | private: |
|
36 | private: | |
33 |
|
37 | |||
34 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
38 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
35 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
39 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
36 |
|
40 | |||
37 | private: |
|
41 | private: | |
38 |
|
42 | |||
39 | // Data |
|
43 | // Data | |
40 | StackedBarChartSeries& mSeries; |
|
44 | StackedBarChartSeries& mSeries; | |
41 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
45 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |
42 | int mMax; |
|
46 | int mMax; | |
43 |
|
47 | |||
44 | int mHeight; // Layout spesific |
|
48 | int mHeight; // Layout spesific | |
45 | int mWidth; |
|
49 | int mWidth; | |
46 | int mBarDefaultWidth; |
|
50 | int mBarDefaultWidth; | |
47 |
|
51 | |||
48 | bool mLayoutSet; // True, if component has been laid out. |
|
52 | bool mLayoutSet; // True, if component has been laid out. | |
49 | bool mLayoutDirty; |
|
53 | bool mLayoutDirty; | |
50 |
|
54 | |||
51 | QList<QColor> mColors; // List of colors for series for now |
|
55 | QList<QColor> mColors; // List of colors for series for now | |
52 |
|
56 | |||
|
57 | ChartTheme* mTheme; | |||
|
58 | ||||
53 | }; |
|
59 | }; | |
54 |
|
60 | |||
55 | QTCOMMERCIALCHART_END_NAMESPACE |
|
61 | QTCOMMERCIALCHART_END_NAMESPACE | |
56 |
|
62 | |||
57 | #endif // STACKEDBARGROUP_H |
|
63 | #endif // STACKEDBARGROUP_H |
General Comments 0
You need to be logged in to leave comments.
Login now