@@ -0,0 +1,109 | |||||
|
1 | #include <limits.h> | |||
|
2 | #include <QDebug> | |||
|
3 | #include "barchartseriesbase.h" | |||
|
4 | #include "bargroup.h" | |||
|
5 | ||||
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
7 | ||||
|
8 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) | |||
|
9 | : QChartSeries(parent) | |||
|
10 | { | |||
|
11 | } | |||
|
12 | ||||
|
13 | bool BarChartSeriesBase::setData(QAbstractItemModel* model) | |||
|
14 | { | |||
|
15 | mModel = model; | |||
|
16 | return true; | |||
|
17 | } | |||
|
18 | ||||
|
19 | int BarChartSeriesBase::min() | |||
|
20 | { | |||
|
21 | Q_ASSERT(mModel->rowCount() > 0); | |||
|
22 | Q_ASSERT(mModel->columnCount() > 0); | |||
|
23 | ||||
|
24 | // TODO: make min and max members and update them when data changes. | |||
|
25 | // This is slower since they are checked every time, even if data is same since previous call. | |||
|
26 | int min = INT_MAX; | |||
|
27 | ||||
|
28 | for (int i=0; i <mModel->rowCount(); i++) { | |||
|
29 | for(int j=0; j<mModel->columnCount(); j++) { | |||
|
30 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |||
|
31 | if (temp < min) { | |||
|
32 | min = temp; | |||
|
33 | } | |||
|
34 | } | |||
|
35 | } | |||
|
36 | return min; | |||
|
37 | } | |||
|
38 | ||||
|
39 | int BarChartSeriesBase::max() | |||
|
40 | { | |||
|
41 | Q_ASSERT(mModel->rowCount() > 0); | |||
|
42 | Q_ASSERT(mModel->columnCount() > 0); | |||
|
43 | ||||
|
44 | // TODO: make min and max members and update them when data changes. | |||
|
45 | // This is slower since they are checked every time, even if data is same since previous call. | |||
|
46 | int max = INT_MIN; | |||
|
47 | ||||
|
48 | for (int i=0; i <mModel->rowCount(); i++) { | |||
|
49 | for(int j=0; j<mModel->columnCount(); j++) { | |||
|
50 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |||
|
51 | if (temp > max) { | |||
|
52 | max = temp; | |||
|
53 | } | |||
|
54 | } | |||
|
55 | } | |||
|
56 | return max; | |||
|
57 | } | |||
|
58 | ||||
|
59 | int BarChartSeriesBase::maxColumnSum() | |||
|
60 | { | |||
|
61 | Q_ASSERT(mModel->rowCount() > 0); | |||
|
62 | Q_ASSERT(mModel->columnCount() > 0); | |||
|
63 | ||||
|
64 | int max = INT_MIN; | |||
|
65 | ||||
|
66 | for (int col=0; col <mModel->columnCount(); col++) { | |||
|
67 | int sum = columnSum(col); | |||
|
68 | if (sum > max) { | |||
|
69 | max = sum; | |||
|
70 | } | |||
|
71 | } | |||
|
72 | return max; | |||
|
73 | } | |||
|
74 | ||||
|
75 | int BarChartSeriesBase::countRows() | |||
|
76 | { | |||
|
77 | return mModel->rowCount(); | |||
|
78 | } | |||
|
79 | ||||
|
80 | int BarChartSeriesBase::countColumns() | |||
|
81 | { | |||
|
82 | return mModel->columnCount(); | |||
|
83 | } | |||
|
84 | ||||
|
85 | int BarChartSeriesBase::countTotalItems() | |||
|
86 | { | |||
|
87 | return mModel->rowCount() * mModel->columnCount(); | |||
|
88 | } | |||
|
89 | ||||
|
90 | int BarChartSeriesBase::valueAt(int row, int column) | |||
|
91 | { | |||
|
92 | QModelIndex index = mModel->index(row,column); | |||
|
93 | return mModel->data(index).toInt(); | |||
|
94 | } | |||
|
95 | ||||
|
96 | int BarChartSeriesBase::columnSum(int column) | |||
|
97 | { | |||
|
98 | int sum(0); | |||
|
99 | int count = mModel->rowCount(); | |||
|
100 | ||||
|
101 | for (int row = 0; row < count; row++) { | |||
|
102 | sum += mModel->data(mModel->index(row,column)).toInt(); | |||
|
103 | } | |||
|
104 | return sum; | |||
|
105 | } | |||
|
106 | ||||
|
107 | #include "moc_barchartseriesbase.cpp" | |||
|
108 | ||||
|
109 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,49 | |||||
|
1 | #ifndef BARCHARTSERIESBASE_H | |||
|
2 | #define BARCHARTSERIESBASE_H | |||
|
3 | ||||
|
4 | #include <QList> | |||
|
5 | #include <QAbstractItemModel> | |||
|
6 | #include "qchartseries.h" | |||
|
7 | #include "qchartglobal.h" | |||
|
8 | ||||
|
9 | class BarGroupBase; | |||
|
10 | ||||
|
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
12 | ||||
|
13 | // Container for series | |||
|
14 | class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries | |||
|
15 | { | |||
|
16 | Q_OBJECT | |||
|
17 | protected: | |||
|
18 | BarChartSeriesBase(QObject* parent=0); | |||
|
19 | ||||
|
20 | public: | |||
|
21 | // from QChartSeries | |||
|
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; } | |||
|
23 | ||||
|
24 | // TODO: Better data model? | |||
|
25 | virtual bool setData(QAbstractItemModel* model); | |||
|
26 | ||||
|
27 | // Methods to find out minimum and maximum values of data | |||
|
28 | int min(); | |||
|
29 | int max(); | |||
|
30 | int maxColumnSum(); // returns maximum sum of items in all columns. | |||
|
31 | ||||
|
32 | int countRows(); | |||
|
33 | int countColumns(); // Count items in one series. | |||
|
34 | int countTotalItems(); | |||
|
35 | int valueAt(int row, int column); | |||
|
36 | ||||
|
37 | int columnSum(int column); | |||
|
38 | ||||
|
39 | public Q_SLOTS: | |||
|
40 | ||||
|
41 | private: | |||
|
42 | ||||
|
43 | QAbstractItemModel* mModel; | |||
|
44 | BarGroupBase* mBarGroup; | |||
|
45 | }; | |||
|
46 | ||||
|
47 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
48 | ||||
|
49 | #endif // BARCHARTSERIESBASE_H |
@@ -0,0 +1,122 | |||||
|
1 | #include "bargroupbase.h" | |||
|
2 | #include "bar_p.h" | |||
|
3 | #include "barlabel_p.h" | |||
|
4 | #include "separator_p.h" | |||
|
5 | #include "barchartseriesbase.h" | |||
|
6 | #include <QDebug> | |||
|
7 | ||||
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
9 | ||||
|
10 | BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) | |||
|
11 | : ChartItem(parent) | |||
|
12 | ,mSeries(series) | |||
|
13 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |||
|
14 | ,mLayoutSet(false) | |||
|
15 | ,mLayoutDirty(true) | |||
|
16 | ,mTheme(0) | |||
|
17 | ,mSeparatorsVisible(true) | |||
|
18 | { | |||
|
19 | dataChanged(); | |||
|
20 | } | |||
|
21 | ||||
|
22 | void BarGroupBase::setSeparatorsVisible(bool visible) | |||
|
23 | { | |||
|
24 | mSeparatorsVisible = visible; | |||
|
25 | } | |||
|
26 | ||||
|
27 | void BarGroupBase::setSize(const QSizeF& size) | |||
|
28 | { | |||
|
29 | mWidth = size.width(); | |||
|
30 | mHeight = size.height(); | |||
|
31 | layoutChanged(); | |||
|
32 | mLayoutSet = true; | |||
|
33 | } | |||
|
34 | ||||
|
35 | void BarGroupBase::setPlotDomain(const PlotDomain& data) | |||
|
36 | { | |||
|
37 | qDebug() << "BarGroupBase::setPlotDomain"; | |||
|
38 | // TODO: | |||
|
39 | } | |||
|
40 | ||||
|
41 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
|
42 | { | |||
|
43 | if (!mLayoutSet) { | |||
|
44 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; | |||
|
45 | return; | |||
|
46 | } | |||
|
47 | if (mLayoutDirty) { | |||
|
48 | // Layout or data has changed. Need to redraw. | |||
|
49 | foreach(QGraphicsItem* i, childItems()) { | |||
|
50 | i->paint(painter,option,widget); | |||
|
51 | } | |||
|
52 | } | |||
|
53 | } | |||
|
54 | ||||
|
55 | QRectF BarGroupBase::boundingRect() const | |||
|
56 | { | |||
|
57 | return QRectF(0,0,mWidth,mHeight); | |||
|
58 | } | |||
|
59 | ||||
|
60 | void BarGroupBase::themeChanged(ChartTheme *theme) | |||
|
61 | { | |||
|
62 | mTheme = theme; | |||
|
63 | } | |||
|
64 | ||||
|
65 | void BarGroupBase::setBarWidth( int w ) | |||
|
66 | { | |||
|
67 | mBarDefaultWidth = w; | |||
|
68 | } | |||
|
69 | ||||
|
70 | int BarGroupBase::addColor( QColor color ) | |||
|
71 | { | |||
|
72 | int colorIndex = mColors.count(); | |||
|
73 | mColors.append(color); | |||
|
74 | return colorIndex; | |||
|
75 | } | |||
|
76 | ||||
|
77 | void BarGroupBase::resetColors() | |||
|
78 | { | |||
|
79 | mColors.clear(); | |||
|
80 | } | |||
|
81 | ||||
|
82 | void BarGroupBase::dataChanged() | |||
|
83 | { | |||
|
84 | qDebug() << "BarGroupBase::dataChanged"; | |||
|
85 | ||||
|
86 | // Find out maximum and minimum of all series | |||
|
87 | mMax = mSeries.max(); | |||
|
88 | mMin = mSeries.min(); | |||
|
89 | ||||
|
90 | // Delete old bars | |||
|
91 | foreach (QGraphicsItem* item, childItems()) { | |||
|
92 | delete item; | |||
|
93 | } | |||
|
94 | ||||
|
95 | // Create new graphic items for bars | |||
|
96 | int totalItems = mSeries.countTotalItems(); | |||
|
97 | for (int i=0; i<totalItems; i++) { | |||
|
98 | Bar *bar = new Bar(this); | |||
|
99 | childItems().append(bar); | |||
|
100 | } | |||
|
101 | ||||
|
102 | // TODO: labels from series. This creates just some example labels | |||
|
103 | int count = mSeries.countColumns(); | |||
|
104 | for (int i=0; i<count; i++) { | |||
|
105 | BarLabel* label = new BarLabel(this); | |||
|
106 | QString text("Label " + QString::number(i)); | |||
|
107 | label->set(text); | |||
|
108 | childItems().append(label); | |||
|
109 | } | |||
|
110 | ||||
|
111 | count = mSeries.countColumns() - 1; // There is one less separator than columns | |||
|
112 | for (int i=0; i<count; i++) { | |||
|
113 | Separator* sep = new Separator(this); | |||
|
114 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme | |||
|
115 | childItems().append(sep); | |||
|
116 | } | |||
|
117 | ||||
|
118 | // TODO: if (autolayout) { layoutChanged() } or something | |||
|
119 | mLayoutDirty = true; | |||
|
120 | } | |||
|
121 | ||||
|
122 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,64 | |||||
|
1 | #ifndef BARGROUPBASE_H | |||
|
2 | #define BARGROUPBASE_H | |||
|
3 | ||||
|
4 | #include "charttheme_p.h" | |||
|
5 | #include "chartitem_p.h" | |||
|
6 | //#include "barlabel_p.h" | |||
|
7 | //#include "bar_p.h" | |||
|
8 | #include "barchartseriesbase.h" | |||
|
9 | #include <QGraphicsItem> | |||
|
10 | ||||
|
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
12 | ||||
|
13 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. | |||
|
14 | ||||
|
15 | class BarGroupBase : public ChartItem, public ChartThemeObserver | |||
|
16 | { | |||
|
17 | ||||
|
18 | public: | |||
|
19 | BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0); | |||
|
20 | void setSeparatorsVisible(bool visible = true); | |||
|
21 | ||||
|
22 | public: // From ChartItem | |||
|
23 | void setSize(const QSizeF &size); | |||
|
24 | void setPlotDomain(const PlotDomain& data); | |||
|
25 | ||||
|
26 | // From QGraphicsItem | |||
|
27 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |||
|
28 | QRectF boundingRect() const; | |||
|
29 | ||||
|
30 | // From ChartThemeObserver | |||
|
31 | void themeChanged(ChartTheme *theme); | |||
|
32 | ||||
|
33 | // TODO: these may change with layout awarness. | |||
|
34 | void setBarWidth( int w ); | |||
|
35 | int addColor( QColor color ); | |||
|
36 | void resetColors(); | |||
|
37 | ||||
|
38 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |||
|
39 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes | |||
|
40 | ||||
|
41 | protected: | |||
|
42 | ||||
|
43 | BarChartSeriesBase& mSeries; | |||
|
44 | ||||
|
45 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |||
|
46 | int mMax; | |||
|
47 | ||||
|
48 | int mHeight; // Layout spesific | |||
|
49 | int mWidth; | |||
|
50 | int mBarDefaultWidth; | |||
|
51 | ||||
|
52 | bool mLayoutSet; // True, if component has been laid out. | |||
|
53 | bool mLayoutDirty; | |||
|
54 | ||||
|
55 | QList<QColor> mColors; // List of colors for series for now | |||
|
56 | ||||
|
57 | ChartTheme* mTheme; | |||
|
58 | bool mSeparatorsVisible; | |||
|
59 | ||||
|
60 | }; | |||
|
61 | ||||
|
62 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
63 | ||||
|
64 | #endif // BARGROUPBASE_H |
@@ -0,0 +1,49 | |||||
|
1 | #include "separator_p.h" | |||
|
2 | #include <QDebug> | |||
|
3 | #include <QPainter> | |||
|
4 | ||||
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
6 | ||||
|
7 | Separator::Separator(QGraphicsItem *parent) | |||
|
8 | : ChartItem(parent) | |||
|
9 | { | |||
|
10 | } | |||
|
11 | ||||
|
12 | void Separator::setPos(qreal x, qreal y) | |||
|
13 | { | |||
|
14 | mXpos = x; | |||
|
15 | mYpos = y; | |||
|
16 | } | |||
|
17 | ||||
|
18 | void Separator::setColor(QColor color) | |||
|
19 | { | |||
|
20 | mColor = color; | |||
|
21 | } | |||
|
22 | ||||
|
23 | void Separator::setSize(const QSizeF &size) | |||
|
24 | { | |||
|
25 | mWidth = size.width(); | |||
|
26 | mHeight = size.height(); | |||
|
27 | } | |||
|
28 | ||||
|
29 | void Separator::setPlotDomain(const PlotDomain& data) | |||
|
30 | { | |||
|
31 | ||||
|
32 | } | |||
|
33 | ||||
|
34 | void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
|
35 | { | |||
|
36 | qDebug() << "separator::paint" << boundingRect(); | |||
|
37 | QPen pen(mColor); | |||
|
38 | painter->setPen(pen); | |||
|
39 | painter->drawLine(mXpos,mYpos,mXpos,mHeight); | |||
|
40 | } | |||
|
41 | ||||
|
42 | QRectF Separator::boundingRect() const | |||
|
43 | { | |||
|
44 | QRectF r(mXpos,mYpos,mWidth,mHeight); | |||
|
45 | return r; | |||
|
46 | } | |||
|
47 | ||||
|
48 | ||||
|
49 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,36 | |||||
|
1 | #ifndef SEPARATOR_H | |||
|
2 | #define SEPARATOR_H | |||
|
3 | ||||
|
4 | #include "chartitem_p.h" | |||
|
5 | ||||
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
7 | ||||
|
8 | class Separator : public ChartItem | |||
|
9 | { | |||
|
10 | public: | |||
|
11 | Separator(QGraphicsItem *parent = 0); | |||
|
12 | ||||
|
13 | void setPos(qreal x, qreal y); | |||
|
14 | void setColor(QColor color); | |||
|
15 | ||||
|
16 | // From ChartItem | |||
|
17 | void setSize(const QSizeF &size); | |||
|
18 | void setPlotDomain(const PlotDomain& data); | |||
|
19 | ||||
|
20 | // From QGraphicsItem | |||
|
21 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |||
|
22 | QRectF boundingRect() const; | |||
|
23 | ||||
|
24 | private: | |||
|
25 | ||||
|
26 | QColor mColor; | |||
|
27 | qreal mXpos; | |||
|
28 | qreal mYpos; | |||
|
29 | qreal mHeight; | |||
|
30 | qreal mWidth; | |||
|
31 | ||||
|
32 | }; | |||
|
33 | ||||
|
34 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
35 | ||||
|
36 | #endif // SEPARATOR_H |
@@ -1,80 +1,13 | |||||
1 | #include <QDebug> |
|
1 | #include <QDebug> | |
2 | #include "barchartseries.h" |
|
2 | #include "barchartseries.h" | |
3 | #include "bargroup.h" |
|
3 | ||
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
5 |
|
5 | |||
6 | BarChartSeries::BarChartSeries(QObject *parent) |
|
6 | BarChartSeries::BarChartSeries(QObject *parent) | |
7 |
: |
|
7 | : BarChartSeriesBase(parent) | |
8 | { |
|
|||
9 | } |
|
|||
10 |
|
||||
11 | bool BarChartSeries::setData(QAbstractItemModel* model) |
|
|||
12 | { |
|
|||
13 | mModel = model; |
|
|||
14 | return true; |
|
|||
15 | } |
|
|||
16 |
|
||||
17 | int BarChartSeries::min() |
|
|||
18 | { |
|
|||
19 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
20 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
21 |
|
||||
22 | // TODO: make min and max members and update them when data changes. |
|
|||
23 | // This is slower since they are checked every time, even if data is same since previous call. |
|
|||
24 | int min = INT_MAX; |
|
|||
25 |
|
||||
26 | for (int i=0; i <mModel->rowCount(); i++) { |
|
|||
27 | for(int j=0; j<mModel->columnCount(); j++) { |
|
|||
28 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
|||
29 | if (temp < min) { |
|
|||
30 | min = temp; |
|
|||
31 | } |
|
|||
32 | } |
|
|||
33 | } |
|
|||
34 | return min; |
|
|||
35 | } |
|
|||
36 |
|
||||
37 | int BarChartSeries::max() |
|
|||
38 | { |
|
|||
39 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
40 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
41 |
|
||||
42 | // TODO: make min and max members and update them when data changes. |
|
|||
43 | // This is slower since they are checked every time, even if data is same since previous call. |
|
|||
44 | int max = INT_MIN; |
|
|||
45 |
|
||||
46 | for (int i=0; i <mModel->rowCount(); i++) { |
|
|||
47 | for(int j=0; j<mModel->columnCount(); j++) { |
|
|||
48 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
|||
49 | if (temp > max) { |
|
|||
50 | max = temp; |
|
|||
51 | } |
|
|||
52 | } |
|
|||
53 | } |
|
|||
54 | return max; |
|
|||
55 | } |
|
|||
56 |
|
||||
57 |
|
||||
58 | int BarChartSeries::countRows() |
|
|||
59 | { |
|
|||
60 | return mModel->rowCount(); |
|
|||
61 | } |
|
|||
62 |
|
||||
63 | int BarChartSeries::countColumns() |
|
|||
64 | { |
|
|||
65 | return mModel->columnCount(); |
|
|||
66 | } |
|
|||
67 |
|
||||
68 | int BarChartSeries::countTotalItems() |
|
|||
69 | { |
|
|||
70 | return mModel->rowCount() * mModel->columnCount(); |
|
|||
71 | } |
|
|||
72 |
|
||||
73 | int BarChartSeries::valueAt(int row, int column) |
|
|||
74 | { |
|
8 | { | |
75 | return mModel->data(mModel->index(row,column)).toInt(); |
|
|||
76 | } |
|
9 | } | |
77 |
|
10 | |||
78 | #include "moc_barchartseries.cpp" |
|
11 | #include "moc_barchartseries.cpp" | |
79 |
|
12 | |||
80 | QTCOMMERCIALCHART_END_NAMESPACE |
|
13 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,45 +1,30 | |||||
1 | #ifndef BARCHARTSERIES_H |
|
1 | #ifndef BARCHARTSERIES_H | |
2 | #define BARCHARTSERIES_H |
|
2 | #define BARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 |
#include " |
|
6 | #include "barchartseriesbase.h" | |
7 | #include "qchartglobal.h" |
|
|||
8 |
|
7 | |||
9 | // TODO: Can this class be combined with series? |
|
|||
10 | class BarGroup; |
|
8 | class BarGroup; | |
11 |
|
9 | |||
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
13 |
|
11 | |||
14 | // Container for series |
|
12 | // Container for series | |
15 |
class QTCOMMERCIALCHART_EXPORT BarChartSeries : public |
|
13 | class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase | |
16 | { |
|
14 | { | |
17 | Q_OBJECT |
|
15 | Q_OBJECT | |
18 | public: |
|
16 | public: | |
19 | BarChartSeries(QObject* parent=0); |
|
17 | BarChartSeries(QObject* parent=0); | |
20 |
|
18 | |||
21 |
// from |
|
19 | // from BarChartSeriesBase | |
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } |
|
20 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } | |
23 |
|
21 | |||
24 | // TODO: Better data model? |
|
|||
25 | virtual bool setData(QAbstractItemModel* model); |
|
|||
26 |
|
||||
27 | // Methods to find out minimum and maximum values of data |
|
|||
28 | int min(); |
|
|||
29 | int max(); |
|
|||
30 | int countRows(); |
|
|||
31 | int countColumns(); // Count items in one series. |
|
|||
32 | int countTotalItems(); |
|
|||
33 | int valueAt(int row, int column); |
|
|||
34 |
|
||||
35 | public Q_SLOTS: |
|
|||
36 |
|
||||
37 | private: |
|
22 | private: | |
38 |
|
23 | |||
39 | QAbstractItemModel* mModel; |
|
24 | QAbstractItemModel* mModel; | |
40 | BarGroup* mBarGroup; |
|
25 | BarGroup* mBarGroup; | |
41 | }; |
|
26 | }; | |
42 |
|
27 | |||
43 | QTCOMMERCIALCHART_END_NAMESPACE |
|
28 | QTCOMMERCIALCHART_END_NAMESPACE | |
44 |
|
29 | |||
45 | #endif // BARCHARTSERIES_H |
|
30 | #endif // BARCHARTSERIES_H |
@@ -1,157 +1,65 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include <QDebug> |
|
4 | #include <QDebug> | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : |
|
8 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | |
9 | ChartItem(parent) |
|
9 | BarGroupBase(series,parent) | |
10 | ,mSeries(series) |
|
|||
11 | ,mBarDefaultWidth(10) |
|
|||
12 | ,mLayoutSet(false) |
|
|||
13 | ,mLayoutDirty(true) |
|
|||
14 | { |
|
10 | { | |
15 | dataChanged(); |
|
11 | mBarDefaultWidth = 10; | |
16 | } |
|
|||
17 |
|
||||
18 | void BarGroup::setSize(const QSizeF& size) |
|
|||
19 | { |
|
|||
20 | qDebug() << "BarGroup::setSize"; |
|
|||
21 | mWidth = size.width(); |
|
|||
22 | mHeight = size.height(); |
|
|||
23 | layoutChanged(); |
|
|||
24 | mLayoutSet = true; |
|
|||
25 | } |
|
|||
26 |
|
||||
27 | void BarGroup::setPlotDomain(const PlotDomain& data) |
|
|||
28 | { |
|
|||
29 | qDebug() << "BarGroup::setPlotDomain"; |
|
|||
30 | // TODO: |
|
|||
31 | mPlotDomain = data; |
|
|||
32 | } |
|
|||
33 |
|
||||
34 | void BarGroup::setBarWidth( int w ) |
|
|||
35 | { |
|
|||
36 | mBarDefaultWidth = w; |
|
|||
37 | } |
|
|||
38 |
|
||||
39 | int BarGroup::addColor( QColor color ) |
|
|||
40 | { |
|
|||
41 | int colorIndex = mColors.count(); |
|
|||
42 | mColors.append(color); |
|
|||
43 | return colorIndex; |
|
|||
44 | } |
|
|||
45 |
|
||||
46 | void BarGroup::resetColors() |
|
|||
47 | { |
|
|||
48 | mColors.clear(); |
|
|||
49 | } |
|
|||
50 |
|
||||
51 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
|||
52 | { |
|
|||
53 | if (!mLayoutSet) { |
|
|||
54 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
|||
55 | return; |
|
|||
56 | } |
|
|||
57 | if (mLayoutDirty) { |
|
|||
58 | // Layout or data has changed. Need to redraw. |
|
|||
59 | foreach(QGraphicsItem* i, childItems()) { |
|
|||
60 | i->paint(painter,option,widget); |
|
|||
61 | mLayoutDirty = false; |
|
|||
62 | } |
|
|||
63 | } |
|
|||
64 | } |
|
|||
65 |
|
||||
66 | QRectF BarGroup::boundingRect() const |
|
|||
67 | { |
|
|||
68 | return QRectF(0,0,mWidth,mHeight); |
|
|||
69 | } |
|
|||
70 |
|
||||
71 |
|
||||
72 | void BarGroup::dataChanged() |
|
|||
73 | { |
|
|||
74 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
|||
75 |
|
||||
76 | // Find out maximum and minimum of all series |
|
|||
77 | mMax = mSeries.max(); |
|
|||
78 | mMin = mSeries.min(); |
|
|||
79 |
|
||||
80 | // Delete old bars |
|
|||
81 | // Is this correct way to delete childItems? |
|
|||
82 | foreach (QGraphicsItem* item, childItems()) { |
|
|||
83 | delete item; |
|
|||
84 | } |
|
|||
85 |
|
||||
86 | // Create new graphic items for bars |
|
|||
87 | int totalItems = mSeries.countTotalItems(); |
|
|||
88 | for (int i=0; i<totalItems; i++) { |
|
|||
89 | Bar *bar = new Bar(this); |
|
|||
90 | childItems().append(bar); |
|
|||
91 | } |
|
|||
92 |
|
||||
93 | // TODO: labels from series. This creates just some example labels |
|
|||
94 | int count = mSeries.countColumns(); |
|
|||
95 | for (int i=0; i<count; i++) { |
|
|||
96 | BarLabel* label = new BarLabel(this); |
|
|||
97 | QString text("Label " + QString::number(i)); |
|
|||
98 | label->set(text); |
|
|||
99 | childItems().append(label); |
|
|||
100 | } |
|
|||
101 |
|
||||
102 | // TODO: if (autolayout) { layoutChanged() } or something |
|
|||
103 | mLayoutDirty = true; |
|
|||
104 | } |
|
12 | } | |
105 |
|
13 | |||
106 | void BarGroup::layoutChanged() |
|
14 | void BarGroup::layoutChanged() | |
107 | { |
|
15 | { | |
108 | // Scale bars to new layout |
|
16 | // Scale bars to new layout | |
109 | // Layout for bars: |
|
17 | // Layout for bars: | |
110 | if (mSeries.countRows() <= 0) { |
|
18 | if (mSeries.countRows() <= 0) { | |
111 | // Nothing to do. |
|
19 | // Nothing to do. | |
112 | return; |
|
20 | return; | |
113 | } |
|
21 | } | |
114 |
|
22 | |||
115 | // TODO: better way to auto-layout? |
|
23 | // TODO: better way to auto-layout? | |
116 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
24 | // Use reals for accurancy (we might get some compiler warnings... :) | |
117 | int columnCount = mSeries.countColumns(); |
|
25 | int columnCount = mSeries.countColumns(); | |
118 | int rowCount = mSeries.countRows(); |
|
26 | int rowCount = mSeries.countRows(); | |
119 |
|
27 | |||
120 | qreal tW = mWidth; |
|
28 | qreal tW = mWidth; | |
121 | qreal tH = mHeight; |
|
29 | qreal tH = mHeight; | |
122 | qreal tM = mMax; |
|
30 | qreal tM = mMax; | |
123 | qreal scale = (tH/tM); |
|
31 | qreal scale = (tH/tM); | |
124 |
|
32 | |||
125 | qreal tC = columnCount+1; |
|
33 | qreal tC = columnCount+1; | |
126 | qreal xStepPerSeries = (tW/tC); |
|
34 | qreal xStepPerSeries = (tW/tC); | |
127 |
|
35 | |||
128 | // Scaling. |
|
36 | // Scaling. | |
129 | int itemIndex(0); |
|
37 | int itemIndex(0); | |
130 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
38 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); | |
131 |
|
39 | |||
132 | for (int column=0; column < columnCount; column++) { |
|
40 | for (int column=0; column < columnCount; column++) { | |
133 | qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); |
|
41 | qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | |
134 | qreal yPos = mHeight; |
|
42 | qreal yPos = mHeight; | |
135 | for (int row = 0; row < rowCount; row++) { |
|
43 | for (int row = 0; row < rowCount; row++) { | |
136 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
44 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
137 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
45 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
138 |
|
46 | |||
139 | // TODO: width settable per bar? |
|
47 | // TODO: width settable per bar? | |
140 | bar->resize(mBarDefaultWidth, barHeight); |
|
48 | bar->resize(mBarDefaultWidth, barHeight); | |
141 | bar->setColor(mColors.at(row)); |
|
49 | bar->setColor(mColors.at(row)); | |
142 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); |
|
50 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | |
143 | itemIndex++; |
|
51 | itemIndex++; | |
144 | xPos += mBarDefaultWidth; |
|
52 | xPos += mBarDefaultWidth; | |
145 | } |
|
53 | } | |
146 |
|
54 | |||
147 | // TODO: Layout for labels, remove magic number |
|
55 | // TODO: Layout for labels, remove magic number | |
148 | xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); |
|
56 | xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | |
149 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
57 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
150 | label->setPos(xPos, mHeight + 20); |
|
58 | label->setPos(xPos, mHeight + 20); | |
151 | labelIndex++; |
|
59 | labelIndex++; | |
152 | } |
|
60 | } | |
153 |
|
61 | |||
154 | mLayoutDirty = true; |
|
62 | mLayoutDirty = true; | |
155 | } |
|
63 | } | |
156 |
|
64 | |||
157 | QTCOMMERCIALCHART_END_NAMESPACE |
|
65 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,57 +1,28 | |||||
1 | #ifndef QBARGROUP_H |
|
1 | #ifndef QBARGROUP_H | |
2 | #define QBARGROUP_H |
|
2 | #define QBARGROUP_H | |
3 |
|
3 | |||
4 |
#include " |
|
4 | #include "bargroupbase.h" | |
5 | #include "bar_p.h" |
|
|||
6 | #include "barchartseries.h" |
|
5 | #include "barchartseries.h" | |
7 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
8 |
|
7 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
9 | |||
11 | class BarGroup : public ChartItem |
|
10 | // Base class for bar groups | |
|
11 | ||||
|
12 | class BarGroup : public BarGroupBase | |||
12 | { |
|
13 | { | |
13 | public: |
|
14 | public: | |
14 | explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); |
|
15 | explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); | |
15 |
|
16 | |||
16 | public: // from ChartItem |
|
|||
17 | void setSize(const QSizeF &size); |
|
|||
18 | void setPlotDomain(const PlotDomain& data); |
|
|||
19 |
|
||||
20 | // Layout "api" |
|
|||
21 | void setPos(qreal x, qreal y); |
|
|||
22 | void setBarWidth( int w ); // Default width for each bar |
|
|||
23 |
|
||||
24 | int addColor( QColor color ); |
|
|||
25 | void resetColors(); |
|
|||
26 |
|
||||
27 | // From QGraphicsItem |
|
|||
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
|||
29 | QRectF boundingRect() const; |
|
|||
30 |
|
||||
31 | private: |
|
17 | private: | |
32 |
|
18 | |||
33 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
19 | // From BarGroupBase | |
34 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
20 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
35 |
|
21 | |||
36 | private: |
|
22 | private: | |
37 |
|
||||
38 | // Data |
|
23 | // Data | |
39 | BarChartSeries& mSeries; |
|
|||
40 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
|||
41 | int mMax; |
|
|||
42 |
|
||||
43 | int mHeight; // Layout spesific |
|
|||
44 | int mWidth; |
|
|||
45 | int mBarDefaultWidth; |
|
|||
46 |
|
||||
47 | bool mLayoutSet; // True, if component has been laid out. |
|
|||
48 | bool mLayoutDirty; |
|
|||
49 |
|
||||
50 | QList<QColor> mColors; // List of colors for series for now |
|
|||
51 |
|
||||
52 | PlotDomain mPlotDomain; |
|
|||
53 | }; |
|
24 | }; | |
54 |
|
25 | |||
55 | QTCOMMERCIALCHART_END_NAMESPACE |
|
26 | QTCOMMERCIALCHART_END_NAMESPACE | |
56 |
|
27 | |||
57 | #endif // QBARGROUP_H |
|
28 | #endif // QBARGROUP_H |
@@ -1,111 +1,17 | |||||
1 | #include "percentbarchartseries.h" |
|
1 | #include "percentbarchartseries.h" | |
2 |
|
2 | |||
3 | #include <limits.h> |
|
3 | #include <limits.h> | |
4 | #include <QDebug> |
|
4 | #include <QDebug> | |
5 | #include "percentbarchartseries.h" |
|
5 | #include "percentbarchartseries.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | PercentBarChartSeries::PercentBarChartSeries(QObject *parent) : |
|
9 | PercentBarChartSeries::PercentBarChartSeries(QObject *parent) : | |
10 |
|
|
10 | BarChartSeriesBase(parent) | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | bool PercentBarChartSeries::setData(QAbstractItemModel* model) |
|
|||
15 | { |
|
|||
16 | mModel = model; |
|
|||
17 | return true; |
|
|||
18 | } |
|
|||
19 |
|
||||
20 | int PercentBarChartSeries::min() |
|
|||
21 | { |
|
|||
22 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
23 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
24 |
|
||||
25 | // TODO: make min and max members and update them when data changes. |
|
|||
26 | // This is slower since they are checked every time, even if data is same since previous call. |
|
|||
27 | int min = INT_MAX; |
|
|||
28 |
|
||||
29 | for (int i=0; i <mModel->rowCount(); i++) { |
|
|||
30 | for(int j=0; j<mModel->columnCount(); j++) { |
|
|||
31 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
|||
32 | if (temp < min) { |
|
|||
33 | min = temp; |
|
|||
34 | } |
|
|||
35 | } |
|
|||
36 | } |
|
|||
37 | return min; |
|
|||
38 | } |
|
|||
39 |
|
||||
40 | int PercentBarChartSeries::max() |
|
|||
41 | { |
|
|||
42 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
43 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
44 |
|
||||
45 | // TODO: make min and max members and update them when data changes. |
|
|||
46 | // This is slower since they are checked every time, even if data is same since previous call. |
|
|||
47 | int max = INT_MIN; |
|
|||
48 |
|
||||
49 | for (int i=0; i <mModel->rowCount(); i++) { |
|
|||
50 | for(int j=0; j<mModel->columnCount(); j++) { |
|
|||
51 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
|||
52 | if (temp > max) { |
|
|||
53 | max = temp; |
|
|||
54 | } |
|
|||
55 | } |
|
|||
56 | } |
|
|||
57 | return max; |
|
|||
58 | } |
|
|||
59 |
|
||||
60 | int PercentBarChartSeries::maxColumnSum() |
|
|||
61 | { |
|
|||
62 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
63 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
64 |
|
||||
65 | int max = INT_MIN; |
|
|||
66 |
|
||||
67 | for (int col=0; col <mModel->columnCount(); col++) { |
|
|||
68 | int sum = columnSum(col); |
|
|||
69 | if (sum > max) { |
|
|||
70 | max = sum; |
|
|||
71 | } |
|
|||
72 | } |
|
|||
73 | return max; |
|
|||
74 | } |
|
|||
75 |
|
||||
76 | int PercentBarChartSeries::countRows() |
|
|||
77 | { |
|
|||
78 | return mModel->rowCount(); |
|
|||
79 | } |
|
|||
80 |
|
||||
81 | int PercentBarChartSeries::countColumns() |
|
|||
82 | { |
|
|||
83 | return mModel->columnCount(); |
|
|||
84 | } |
|
|||
85 |
|
||||
86 | int PercentBarChartSeries::countTotalItems() |
|
|||
87 | { |
|
|||
88 | return mModel->rowCount() * mModel->columnCount(); |
|
|||
89 | } |
|
|||
90 |
|
||||
91 | int PercentBarChartSeries::valueAt(int row, int column) |
|
|||
92 | { |
|
|||
93 | QModelIndex index = mModel->index(row,column); |
|
|||
94 | return mModel->data(index).toInt(); |
|
|||
95 | } |
|
|||
96 |
|
||||
97 | int PercentBarChartSeries::columnSum(int column) |
|
|||
98 | { |
|
|||
99 | int sum(0); |
|
|||
100 | int count = mModel->rowCount(); |
|
|||
101 |
|
||||
102 | for (int row = 0; row < count; row++) { |
|
|||
103 | sum += mModel->data(mModel->index(row,column)).toInt(); |
|
|||
104 | } |
|
|||
105 | return sum; |
|
|||
106 | } |
|
|||
107 |
|
||||
108 | #include "moc_percentbarchartseries.cpp" |
|
14 | #include "moc_percentbarchartseries.cpp" | |
109 |
|
15 | |||
110 | QTCOMMERCIALCHART_END_NAMESPACE |
|
16 | QTCOMMERCIALCHART_END_NAMESPACE | |
111 |
|
17 |
@@ -1,47 +1,32 | |||||
1 | #ifndef PERCENTBARCHARTSERIES_H |
|
1 | #ifndef PERCENTBARCHARTSERIES_H | |
2 | #define PERCENTBARCHARTSERIES_H |
|
2 | #define PERCENTBARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 |
#include " |
|
6 | #include "barchartseriesbase.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class PercentBarGroup; |
|
10 | class PercentBarGroup; | |
11 |
|
11 | |||
12 |
class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public |
|
12 | class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | PercentBarChartSeries(QObject* parent=0); |
|
16 | PercentBarChartSeries(QObject* parent=0); | |
17 |
|
17 | |||
18 |
// from |
|
18 | // from BarChartSeriesBase | |
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; } |
|
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; } | |
20 |
|
20 | |||
21 | // TODO: Better data model? |
|
|||
22 | virtual bool setData(QAbstractItemModel* model); |
|
|||
23 |
|
||||
24 | // Methods to find out minimum and maximum values of data |
|
|||
25 | int min(); |
|
|||
26 | int max(); |
|
|||
27 | int maxColumnSum(); // returns maximum sum of items in all columns. |
|
|||
28 |
|
||||
29 | int countRows(); |
|
|||
30 | int countColumns(); |
|
|||
31 | int countTotalItems(); |
|
|||
32 | int valueAt(int row, int column); |
|
|||
33 |
|
||||
34 | int columnSum(int column); |
|
|||
35 |
|
||||
36 | public Q_SLOTS: |
|
21 | public Q_SLOTS: | |
37 |
|
22 | |||
38 | private: |
|
23 | private: | |
39 |
|
24 | |||
40 | QAbstractItemModel* mModel; |
|
25 | QAbstractItemModel* mModel; | |
41 | PercentBarGroup* mPercentBarGroup; |
|
26 | PercentBarGroup* mPercentBarGroup; | |
42 | }; |
|
27 | }; | |
43 |
|
28 | |||
44 | QTCOMMERCIALCHART_END_NAMESPACE |
|
29 | QTCOMMERCIALCHART_END_NAMESPACE | |
45 |
|
30 | |||
46 |
|
31 | |||
47 | #endif // PERCENTBARCHARTSERIES_H |
|
32 | #endif // PERCENTBARCHARTSERIES_H |
@@ -1,152 +1,73 | |||||
1 | #include "percentbargroup.h" |
|
1 | #include "percentbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
|
4 | #include "separator_p.h" | |||
4 | #include <QDebug> |
|
5 | #include <QDebug> | |
5 |
|
6 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
8 | |||
8 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : |
|
|||
9 | ChartItem(parent) |
|
|||
10 | ,mSeries(series) |
|
|||
11 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
|||
12 | ,mLayoutSet(false) |
|
|||
13 | ,mLayoutDirty(true) |
|
|||
14 | { |
|
|||
15 | dataChanged(); |
|
|||
16 | } |
|
|||
17 |
|
||||
18 |
|
||||
19 | void PercentBarGroup::setSize(const QSizeF& size) |
|
|||
20 | { |
|
|||
21 | // qDebug() << "PercentBarGroup::setSize"; |
|
|||
22 | mWidth = size.width(); |
|
|||
23 | mHeight = size.height(); |
|
|||
24 | layoutChanged(); |
|
|||
25 | mLayoutSet = true; |
|
|||
26 | } |
|
|||
27 |
|
||||
28 | void PercentBarGroup::setPlotDomain(const PlotDomain& data) |
|
|||
29 | { |
|
|||
30 | qDebug() << "PercentBarGroup::setPlotDomain"; |
|
|||
31 | // TODO: |
|
|||
32 | } |
|
|||
33 |
|
9 | |||
34 | void PercentBarGroup::setBarWidth( int w ) |
|
10 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : | |
35 | { |
|
11 | BarGroupBase(series, parent) | |
36 | mBarDefaultWidth = w; |
|
|||
37 | } |
|
|||
38 |
|
||||
39 | int PercentBarGroup::addColor( QColor color ) |
|
|||
40 | { |
|
|||
41 | int colorIndex = mColors.count(); |
|
|||
42 | mColors.append(color); |
|
|||
43 | return colorIndex; |
|
|||
44 | } |
|
|||
45 |
|
||||
46 | void PercentBarGroup::resetColors() |
|
|||
47 | { |
|
|||
48 | mColors.clear(); |
|
|||
49 | } |
|
|||
50 |
|
||||
51 | void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
|||
52 | { |
|
|||
53 | if (!mLayoutSet) { |
|
|||
54 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
|||
55 | return; |
|
|||
56 | } |
|
|||
57 | if (mLayoutDirty) { |
|
|||
58 | // Layout or data has changed. Need to redraw. |
|
|||
59 | foreach(QGraphicsItem* i, childItems()) { |
|
|||
60 | i->paint(painter,option,widget); |
|
|||
61 | } |
|
|||
62 | mLayoutDirty = false; |
|
|||
63 | } |
|
|||
64 | } |
|
|||
65 |
|
||||
66 | QRectF PercentBarGroup::boundingRect() const |
|
|||
67 | { |
|
|||
68 | return QRectF(0,0,mWidth,mHeight); |
|
|||
69 | } |
|
|||
70 |
|
||||
71 |
|
||||
72 | void PercentBarGroup::dataChanged() |
|
|||
73 | { |
|
12 | { | |
74 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
|||
75 |
|
||||
76 | // Find out maximum and minimum of all series |
|
|||
77 | mMax = mSeries.max(); |
|
|||
78 | mMin = mSeries.min(); |
|
|||
79 |
|
||||
80 | // Delete old bars |
|
|||
81 | // Is this correct way to delete childItems? |
|
|||
82 | foreach (QGraphicsItem* item, childItems()) { |
|
|||
83 | delete item; |
|
|||
84 | } |
|
|||
85 |
|
||||
86 | // Create new graphic items for bars |
|
|||
87 | int totalItems = mSeries.countTotalItems(); |
|
|||
88 | for (int i=0; i<totalItems; i++) { |
|
|||
89 | Bar *bar = new Bar(this); |
|
|||
90 | childItems().append(bar); |
|
|||
91 | } |
|
|||
92 |
|
||||
93 | // TODO: labels from series. This creates just some example labels |
|
|||
94 | int count = mSeries.countColumns(); |
|
|||
95 | for (int i=0; i<count; i++) { |
|
|||
96 | BarLabel* label = new BarLabel(this); |
|
|||
97 | QString text("Label " + QString::number(i)); |
|
|||
98 | label->set(text); |
|
|||
99 | childItems().append(label); |
|
|||
100 | } |
|
|||
101 |
|
||||
102 | // TODO: if (autolayout) { layoutChanged() } or something |
|
|||
103 | mLayoutDirty = true; |
|
|||
104 | } |
|
13 | } | |
105 |
|
14 | |||
106 | void PercentBarGroup::layoutChanged() |
|
15 | void PercentBarGroup::layoutChanged() | |
107 | { |
|
16 | { | |
108 | // Scale bars to new layout |
|
17 | // Scale bars to new layout | |
109 | // Layout for bars: |
|
18 | // Layout for bars: | |
110 | if (mSeries.countRows() <= 0) { |
|
19 | if (mSeries.countRows() <= 0) { | |
111 | // Nothing to do. |
|
20 | // Nothing to do. | |
112 | return; |
|
21 | return; | |
113 | } |
|
22 | } | |
114 |
|
23 | |||
115 | // TODO: better way to auto-layout |
|
24 | // TODO: better way to auto-layout | |
116 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
25 | // Use reals for accurancy (we might get some compiler warnings... :) | |
117 | int count = mSeries.countColumns(); |
|
26 | int count = mSeries.countColumns(); | |
118 | int itemIndex(0); |
|
27 | int itemIndex(0); | |
119 | qreal tW = mWidth; |
|
28 | qreal tW = mWidth; | |
120 | qreal tC = count+1; |
|
29 | qreal tC = count+1; | |
121 | qreal xStep = (tW/tC); |
|
30 | qreal xStep = (tW/tC); | |
122 | // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); |
|
31 | // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); | |
123 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
32 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
124 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
33 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); | |
125 |
|
34 | |||
126 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
35 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
127 | qreal colSum = mSeries.columnSum(column); |
|
36 | qreal colSum = mSeries.columnSum(column); | |
128 | qreal h = mHeight; |
|
37 | qreal h = mHeight; | |
129 | qreal scale = (h / colSum); |
|
38 | qreal scale = (h / colSum); | |
130 | qreal yPos = h; |
|
39 | qreal yPos = h; | |
131 | for (int row=0; row < mSeries.countRows(); row++) { |
|
40 | for (int row=0; row < mSeries.countRows(); row++) { | |
132 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
41 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
133 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
42 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
134 |
|
43 | |||
135 | // TODO: width settable per bar? |
|
44 | // TODO: width settable per bar? | |
136 | bar->resize(mBarDefaultWidth, barHeight); |
|
45 | bar->resize(mBarDefaultWidth, barHeight); | |
137 | bar->setColor(mColors.at(row)); |
|
46 | bar->setColor(mColors.at(row)); | |
138 | bar->setPos(xPos, yPos-barHeight); |
|
47 | bar->setPos(xPos, yPos-barHeight); | |
139 | itemIndex++; |
|
48 | itemIndex++; | |
140 | yPos -= barHeight; |
|
49 | yPos -= barHeight; | |
141 | } |
|
50 | } | |
142 |
|
51 | |||
143 | // TODO: Layout for labels, remove magic number |
|
52 | // TODO: Layout for labels, remove magic number | |
144 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
53 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
145 | label->setPos(xPos, mHeight + 20); |
|
54 | label->setPos(xPos, mHeight + 20); | |
146 | labelIndex++; |
|
55 | labelIndex++; | |
147 | xPos += xStep; |
|
56 | xPos += xStep; | |
148 | } |
|
57 | } | |
|
58 | ||||
|
59 | // Position separators | |||
|
60 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |||
|
61 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |||
|
62 | for (int s=0; s < mSeries.countColumns() - 1; s++) { | |||
|
63 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |||
|
64 | sep->setPos(xPos,0); | |||
|
65 | sep->setSize(QSizeF(1,mHeight)); | |||
|
66 | xPos += xStep; | |||
|
67 | separatorIndex++; | |||
|
68 | } | |||
|
69 | ||||
149 | mLayoutDirty = true; |
|
70 | mLayoutDirty = true; | |
150 | } |
|
71 | } | |
151 |
|
72 | |||
152 | QTCOMMERCIALCHART_END_NAMESPACE |
|
73 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,57 +1,28 | |||||
1 | #ifndef PERCENTBARGROUP_H |
|
1 | #ifndef PERCENTBARGROUP_H | |
2 | #define PERCENTBARGROUP_H |
|
2 | #define PERCENTBARGROUP_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 | #include "bar_p.h" |
|
5 | #include "bar_p.h" | |
6 | #include "percentbarchartseries.h" |
|
6 | #include "percentbarchartseries.h" | |
|
7 | #include "bargroupbase.h" | |||
7 | #include <QGraphicsItem> |
|
8 | #include <QGraphicsItem> | |
8 |
|
9 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
11 | |||
11 |
class PercentBarGroup : public |
|
12 | class PercentBarGroup : public BarGroupBase | |
12 | { |
|
13 | { | |
13 | public: |
|
14 | public: | |
14 | PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0); |
|
15 | PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0); | |
15 |
|
16 | |||
16 | public: // From ChartItem |
|
|||
17 | void setSize(const QSizeF &size); |
|
|||
18 | void setPlotDomain(const PlotDomain& data); |
|
|||
19 |
|
||||
20 | public: |
|
|||
21 | // Layout "api" |
|
|||
22 | void setPos(qreal x, qreal y); |
|
|||
23 | void setBarWidth( int w ); // Default width for each bar |
|
|||
24 |
|
||||
25 | int addColor( QColor color ); |
|
|||
26 | void resetColors(); |
|
|||
27 |
|
||||
28 | // From QGraphicsItem |
|
|||
29 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
|||
30 | QRectF boundingRect() const; |
|
|||
31 |
|
||||
32 | private: |
|
17 | private: | |
33 |
|
18 | |||
34 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
|||
35 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
19 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
36 |
|
20 | |||
37 | private: |
|
21 | private: | |
38 |
|
22 | |||
39 | // Data |
|
23 | // Data | |
40 | PercentBarChartSeries& mSeries; |
|
|||
41 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
|||
42 | int mMax; |
|
|||
43 |
|
||||
44 | int mHeight; // Layout spesific |
|
|||
45 | int mWidth; |
|
|||
46 | int mBarDefaultWidth; |
|
|||
47 |
|
||||
48 | bool mLayoutSet; // True, if component has been laid out. |
|
|||
49 | bool mLayoutDirty; |
|
|||
50 |
|
||||
51 | QList<QColor> mColors; // List of colors for series for now |
|
|||
52 |
|
||||
53 | }; |
|
24 | }; | |
54 |
|
25 | |||
55 | QTCOMMERCIALCHART_END_NAMESPACE |
|
26 | QTCOMMERCIALCHART_END_NAMESPACE | |
56 |
|
27 | |||
57 | #endif // PERCENTBARGROUP_H |
|
28 | #endif // PERCENTBARGROUP_H |
@@ -1,109 +1,15 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include "stackedbarchartseries.h" |
|
3 | #include "stackedbarchartseries.h" | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | StackedBarChartSeries::StackedBarChartSeries(QObject *parent) : |
|
7 | StackedBarChartSeries::StackedBarChartSeries(QObject *parent) : | |
8 |
|
|
8 | BarChartSeriesBase(parent) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
12 | bool StackedBarChartSeries::setData(QAbstractItemModel* model) |
|
|||
13 | { |
|
|||
14 | mModel = model; |
|
|||
15 | return true; |
|
|||
16 | } |
|
|||
17 |
|
||||
18 | int StackedBarChartSeries::min() |
|
|||
19 | { |
|
|||
20 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
21 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
22 |
|
||||
23 | // TODO: make min and max members and update them when data changes. |
|
|||
24 | // This is slower since they are checked every time, even if data is same since previous call. |
|
|||
25 | int min = INT_MAX; |
|
|||
26 |
|
||||
27 | for (int i=0; i <mModel->rowCount(); i++) { |
|
|||
28 | for(int j=0; j<mModel->columnCount(); j++) { |
|
|||
29 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
|||
30 | if (temp < min) { |
|
|||
31 | min = temp; |
|
|||
32 | } |
|
|||
33 | } |
|
|||
34 | } |
|
|||
35 | return min; |
|
|||
36 | } |
|
|||
37 |
|
||||
38 | int StackedBarChartSeries::max() |
|
|||
39 | { |
|
|||
40 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
41 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
42 |
|
||||
43 | // TODO: make min and max members and update them when data changes. |
|
|||
44 | // This is slower since they are checked every time, even if data is same since previous call. |
|
|||
45 | int max = INT_MIN; |
|
|||
46 |
|
||||
47 | for (int i=0; i <mModel->rowCount(); i++) { |
|
|||
48 | for(int j=0; j<mModel->columnCount(); j++) { |
|
|||
49 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
|||
50 | if (temp > max) { |
|
|||
51 | max = temp; |
|
|||
52 | } |
|
|||
53 | } |
|
|||
54 | } |
|
|||
55 | return max; |
|
|||
56 | } |
|
|||
57 |
|
||||
58 | int StackedBarChartSeries::maxColumnSum() |
|
|||
59 | { |
|
|||
60 | Q_ASSERT(mModel->rowCount() > 0); |
|
|||
61 | Q_ASSERT(mModel->columnCount() > 0); |
|
|||
62 |
|
||||
63 | int max = INT_MIN; |
|
|||
64 |
|
||||
65 | for (int col=0; col <mModel->columnCount(); col++) { |
|
|||
66 | int sum = columnSum(col); |
|
|||
67 | if (sum > max) { |
|
|||
68 | max = sum; |
|
|||
69 | } |
|
|||
70 | } |
|
|||
71 | return max; |
|
|||
72 | } |
|
|||
73 |
|
||||
74 | int StackedBarChartSeries::countRows() |
|
|||
75 | { |
|
|||
76 | return mModel->rowCount(); |
|
|||
77 | } |
|
|||
78 |
|
||||
79 | int StackedBarChartSeries::countColumns() |
|
|||
80 | { |
|
|||
81 | return mModel->columnCount(); |
|
|||
82 | } |
|
|||
83 |
|
||||
84 | int StackedBarChartSeries::countTotalItems() |
|
|||
85 | { |
|
|||
86 | return mModel->rowCount() * mModel->columnCount(); |
|
|||
87 | } |
|
|||
88 |
|
||||
89 | int StackedBarChartSeries::valueAt(int row, int column) |
|
|||
90 | { |
|
|||
91 | QModelIndex index = mModel->index(row,column); |
|
|||
92 | return mModel->data(index).toInt(); |
|
|||
93 | } |
|
|||
94 |
|
||||
95 | int StackedBarChartSeries::columnSum(int column) |
|
|||
96 | { |
|
|||
97 | int sum(0); |
|
|||
98 | int count = mModel->rowCount(); |
|
|||
99 |
|
||||
100 | for (int row = 0; row < count; row++) { |
|
|||
101 | sum += mModel->data(mModel->index(row,column)).toInt(); |
|
|||
102 | } |
|
|||
103 | return sum; |
|
|||
104 | } |
|
|||
105 |
|
||||
106 | #include "moc_stackedbarchartseries.cpp" |
|
12 | #include "moc_stackedbarchartseries.cpp" | |
107 |
|
13 | |||
108 | QTCOMMERCIALCHART_END_NAMESPACE |
|
14 | QTCOMMERCIALCHART_END_NAMESPACE | |
109 |
|
15 |
@@ -1,47 +1,31 | |||||
1 | #ifndef STACKEDBARCHARTSERIES_H |
|
1 | #ifndef STACKEDBARCHARTSERIES_H | |
2 | #define STACKEDBARCHARTSERIES_H |
|
2 | #define STACKEDBARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 |
#include " |
|
6 | #include "barchartseriesbase.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class StackedBarGroup; |
|
10 | class StackedBarGroup; | |
11 |
|
11 | |||
12 |
class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public |
|
12 | class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | StackedBarChartSeries(QObject* parent=0); |
|
16 | StackedBarChartSeries(QObject* parent=0); | |
17 |
|
17 | |||
18 | // from QChartSeries |
|
18 | // from QChartSeries | |
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } |
|
19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } | |
20 |
|
20 | |||
21 | // TODO: Better data model? |
|
|||
22 | virtual bool setData(QAbstractItemModel* model); |
|
|||
23 |
|
||||
24 | // Methods to find out minimum and maximum values of data |
|
|||
25 | int min(); |
|
|||
26 | int max(); |
|
|||
27 | int maxColumnSum(); // returns maximum sum of items in all columns. |
|
|||
28 |
|
||||
29 | int countRows(); |
|
|||
30 | int countColumns(); |
|
|||
31 | int countTotalItems(); |
|
|||
32 | int valueAt(int row, int column); |
|
|||
33 |
|
||||
34 | private: |
|
|||
35 | int columnSum(int column); |
|
|||
36 |
|
||||
37 | public Q_SLOTS: |
|
21 | public Q_SLOTS: | |
38 |
|
22 | |||
39 | private: |
|
23 | private: | |
40 |
|
24 | |||
41 | QAbstractItemModel* mModel; |
|
25 | QAbstractItemModel* mModel; | |
42 | StackedBarGroup* mStackedBarGroup; |
|
26 | StackedBarGroup* mStackedBarGroup; | |
43 | }; |
|
27 | }; | |
44 |
|
28 | |||
45 | QTCOMMERCIALCHART_END_NAMESPACE |
|
29 | QTCOMMERCIALCHART_END_NAMESPACE | |
46 |
|
30 | |||
47 | #endif // STACKEDBARCHARTSERIES_H |
|
31 | #endif // STACKEDBARCHARTSERIES_H |
@@ -1,192 +1,81 | |||||
1 | #include "stackedbargroup.h" |
|
1 | #include "stackedbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
|
4 | #include "separator_p.h" | |||
4 | #include <QDebug> |
|
5 | #include <QDebug> | |
5 | #include <QPainter> |
|
6 | #include <QPainter> | |
6 |
|
7 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
9 | |||
9 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
10 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : | |
10 | ChartItem(parent) |
|
11 | BarGroupBase(series,parent) | |
11 | ,mSeries(series) |
|
|||
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
|||
13 | ,mLayoutSet(false) |
|
|||
14 | ,mLayoutDirty(true) |
|
|||
15 | ,mTheme(0) |
|
|||
16 | ,mSeparatorsVisible(true) |
|
|||
17 | { |
|
12 | { | |
18 | dataChanged(); |
|
|||
19 | } |
|
|||
20 |
|
||||
21 | void StackedBarGroup::setSeparatorsVisible(bool visible) |
|
|||
22 | { |
|
|||
23 | mSeparatorsVisible = visible; |
|
|||
24 | } |
|
|||
25 |
|
||||
26 | void StackedBarGroup::setSize(const QSizeF& size) |
|
|||
27 | { |
|
|||
28 | mWidth = size.width(); |
|
|||
29 | mHeight = size.height(); |
|
|||
30 | layoutChanged(); |
|
|||
31 | mLayoutSet = true; |
|
|||
32 | } |
|
|||
33 |
|
||||
34 | void StackedBarGroup::setPlotDomain(const PlotDomain& data) |
|
|||
35 | { |
|
|||
36 | qDebug() << "StackedBarGroup::setPlotDomain"; |
|
|||
37 | // TODO: |
|
|||
38 | } |
|
|||
39 |
|
||||
40 | void StackedBarGroup::themeChanged(ChartTheme *theme) |
|
|||
41 | { |
|
|||
42 | mTheme = theme; |
|
|||
43 | } |
|
|||
44 |
|
||||
45 | void StackedBarGroup::setBarWidth( int w ) |
|
|||
46 | { |
|
|||
47 | mBarDefaultWidth = w; |
|
|||
48 | } |
|
|||
49 |
|
||||
50 | int StackedBarGroup::addColor( QColor color ) |
|
|||
51 | { |
|
|||
52 | int colorIndex = mColors.count(); |
|
|||
53 | mColors.append(color); |
|
|||
54 | return colorIndex; |
|
|||
55 | } |
|
|||
56 |
|
||||
57 | void StackedBarGroup::resetColors() |
|
|||
58 | { |
|
|||
59 | mColors.clear(); |
|
|||
60 | } |
|
|||
61 |
|
||||
62 | void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
|||
63 | { |
|
|||
64 | if (!mLayoutSet) { |
|
|||
65 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
|||
66 | return; |
|
|||
67 | } |
|
|||
68 | if (mLayoutDirty) { |
|
|||
69 | // Layout or data has changed. Need to redraw. |
|
|||
70 | foreach(QGraphicsItem* i, childItems()) { |
|
|||
71 | i->paint(painter,option,widget); |
|
|||
72 | } |
|
|||
73 | if (mSeparatorsVisible) { |
|
|||
74 | //TODO: own class for separators (graphicsitem), because they may have style etc later. |
|
|||
75 | // this is just to see that the positions are calculated correctly. |
|
|||
76 | QPen pen(QColor(0,0,255,255)); |
|
|||
77 | painter->setPen(pen); |
|
|||
78 | for (int i=0; i<mSeparatorPositions.count(); i++ ) { |
|
|||
79 | qreal xp = mSeparatorPositions.at(i); |
|
|||
80 | painter->drawLine(xp,0,xp,mHeight); |
|
|||
81 | } |
|
|||
82 | } |
|
|||
83 | // mLayoutDirty = false; |
|
|||
84 | } |
|
|||
85 |
|
||||
86 | } |
|
|||
87 |
|
||||
88 | QRectF StackedBarGroup::boundingRect() const |
|
|||
89 | { |
|
|||
90 | return QRectF(0,0,mWidth,mHeight); |
|
|||
91 | } |
|
|||
92 |
|
||||
93 |
|
||||
94 | void StackedBarGroup::dataChanged() |
|
|||
95 | { |
|
|||
96 | qDebug() << "QBarChart::dataChanged"; |
|
|||
97 |
|
||||
98 | // Find out maximum and minimum of all series |
|
|||
99 | mMax = mSeries.max(); |
|
|||
100 | mMin = mSeries.min(); |
|
|||
101 |
|
||||
102 | // Delete old bars |
|
|||
103 | // Is this correct way to delete childItems? |
|
|||
104 | foreach (QGraphicsItem* item, childItems()) { |
|
|||
105 | delete item; |
|
|||
106 | } |
|
|||
107 |
|
||||
108 | // Create new graphic items for bars |
|
|||
109 | int totalItems = mSeries.countTotalItems(); |
|
|||
110 | for (int i=0; i<totalItems; i++) { |
|
|||
111 | Bar *bar = new Bar(this); |
|
|||
112 | childItems().append(bar); |
|
|||
113 | } |
|
|||
114 |
|
||||
115 | // TODO: labels from series. This creates just some example labels |
|
|||
116 | int count = mSeries.countColumns(); |
|
|||
117 | for (int i=0; i<count; i++) { |
|
|||
118 | BarLabel* label = new BarLabel(this); |
|
|||
119 | QString text("Label " + QString::number(i)); |
|
|||
120 | label->set(text); |
|
|||
121 | childItems().append(label); |
|
|||
122 | } |
|
|||
123 |
|
||||
124 | mSeparatorPositions.clear(); |
|
|||
125 |
|
||||
126 | // TODO: if (autolayout) { layoutChanged() } or something |
|
|||
127 | mLayoutDirty = true; |
|
|||
128 | } |
|
13 | } | |
129 |
|
14 | |||
130 | void StackedBarGroup::layoutChanged() |
|
15 | void StackedBarGroup::layoutChanged() | |
131 | { |
|
16 | { | |
132 | // Scale bars to new layout |
|
17 | // Scale bars to new layout | |
133 | // Layout for bars: |
|
18 | // Layout for bars: | |
134 | if (mSeries.countRows() <= 0) { |
|
19 | if (mSeries.countRows() <= 0) { | |
135 | // Nothing to do. |
|
20 | // Nothing to do. | |
136 | return; |
|
21 | return; | |
137 | } |
|
22 | } | |
138 |
|
23 | |||
139 | if (mSeries.countColumns() == 0) { |
|
24 | if (mSeries.countColumns() == 0) { | |
140 | // Nothing to do |
|
25 | // Nothing to do | |
141 | return; |
|
26 | return; | |
142 | } |
|
27 | } | |
143 |
|
28 | |||
144 | // TODO: better way to auto-layout |
|
29 | // TODO: better way to auto-layout | |
145 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
30 | // Use reals for accurancy (we might get some compiler warnings... :) | |
146 | // TODO: use temp variable for column count... |
|
31 | // TODO: use temp variable for column count... | |
147 | qreal maxSum = mSeries.maxColumnSum(); |
|
32 | qreal maxSum = mSeries.maxColumnSum(); | |
148 | qreal h = mHeight; |
|
33 | qreal h = mHeight; | |
149 | qreal scale = (h / maxSum); |
|
34 | qreal scale = (h / maxSum); | |
150 |
|
35 | |||
151 | int itemIndex(0); |
|
36 | int itemIndex(0); | |
152 | qreal tW = mWidth; |
|
37 | qreal tW = mWidth; | |
153 | qreal tC = mSeries.countColumns() + 1; |
|
38 | qreal tC = mSeries.countColumns() + 1; | |
154 | qreal xStep = (tW/tC); |
|
39 | qreal xStep = (tW/tC); | |
155 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
40 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
156 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
41 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); | |
157 |
|
42 | |||
158 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
43 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
159 | qreal yPos = h; |
|
44 | qreal yPos = h; | |
160 | for (int row=0; row < mSeries.countRows(); row++) { |
|
45 | for (int row=0; row < mSeries.countRows(); row++) { | |
161 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
46 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
162 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
47 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
163 |
|
48 | |||
164 | // TODO: width settable per bar? |
|
49 | // TODO: width settable per bar? | |
165 | // TODO: how to get color for series(x) from theme? |
|
50 | // TODO: how to get color for series(x) from theme? | |
166 | // mTheme->themeForSeries(); |
|
51 | // mTheme->themeForSeries(); | |
167 | bar->resize(mBarDefaultWidth, barHeight); |
|
52 | bar->resize(mBarDefaultWidth, barHeight); | |
168 | bar->setColor(mColors.at(row)); |
|
53 | bar->setColor(mColors.at(row)); | |
169 | bar->setPos(xPos, yPos-barHeight); |
|
54 | bar->setPos(xPos, yPos-barHeight); | |
170 | itemIndex++; |
|
55 | itemIndex++; | |
171 | yPos -= barHeight; |
|
56 | yPos -= barHeight; | |
172 | } |
|
57 | } | |
173 |
|
58 | |||
174 | // TODO: Layout for labels, remove magic number |
|
59 | // TODO: Layout for labels, remove magic number | |
175 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
60 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
176 | label->setPos(xPos, mHeight + 20); |
|
61 | label->setPos(xPos, mHeight + 20); | |
177 | labelIndex++; |
|
62 | labelIndex++; | |
178 | xPos += xStep; |
|
63 | xPos += xStep; | |
179 | } |
|
64 | } | |
180 |
|
65 | |||
|
66 | ||||
181 | // Position separators |
|
67 | // Position separators | |
182 | mSeparatorPositions.clear(); |
|
68 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
183 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
69 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
184 | for (int s=0; s < mSeries.countColumns() - 1; s++) { |
|
70 | for (int s=0; s < mSeries.countColumns() - 1; s++) { | |
185 | mSeparatorPositions.append(xPos); |
|
71 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
|
72 | sep->setPos(xPos,0); | |||
|
73 | sep->setSize(QSizeF(1,mHeight)); | |||
186 | xPos += xStep; |
|
74 | xPos += xStep; | |
|
75 | separatorIndex++; | |||
187 | } |
|
76 | } | |
188 |
|
77 | |||
189 | mLayoutDirty = true; |
|
78 | mLayoutDirty = true; | |
190 | } |
|
79 | } | |
191 |
|
80 | |||
192 | QTCOMMERCIALCHART_END_NAMESPACE |
|
81 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,69 +1,26 | |||||
1 | #ifndef STACKEDBARGROUP_H |
|
1 | #ifndef STACKEDBARGROUP_H | |
2 | #define STACKEDBARGROUP_H |
|
2 | #define STACKEDBARGROUP_H | |
3 |
|
3 | |||
4 |
#include " |
|
4 | #include "bargroupbase.h" | |
5 | #include "chartitem_p.h" |
|
|||
6 | #include "barlabel_p.h" |
|
|||
7 | #include "bar_p.h" |
|
|||
8 | #include "stackedbarchartseries.h" |
|
5 | #include "stackedbarchartseries.h" | |
9 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
10 |
|
7 | |||
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
12 |
|
9 | |||
13 | // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem |
|
10 | class StackedBarGroup : public BarGroupBase | |
14 | class StackedBarGroup : public ChartItem, public ChartThemeObserver |
|
|||
15 | { |
|
11 | { | |
16 | public: |
|
12 | public: | |
17 | StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); |
|
13 | StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); | |
18 |
|
14 | |||
19 | // Bar chart spesific |
|
|||
20 | void setSeparatorsVisible(bool visible = true); |
|
|||
21 |
|
||||
22 | public: // From ChartItem |
|
|||
23 | void setSize(const QSizeF &size); |
|
|||
24 | void setPlotDomain(const PlotDomain& data); |
|
|||
25 |
|
||||
26 | // From ChartThemeObserver |
|
|||
27 | void themeChanged(ChartTheme *theme); |
|
|||
28 |
|
||||
29 | public: // Layout "api" |
|
|||
30 | void setPos(qreal x, qreal y); |
|
|||
31 | void setBarWidth( int w ); // Default width for each bar |
|
|||
32 |
|
||||
33 | int addColor( QColor color ); |
|
|||
34 | void resetColors(); |
|
|||
35 |
|
||||
36 | // From QGraphicsItem |
|
|||
37 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
|||
38 | QRectF boundingRect() const; |
|
|||
39 |
|
||||
40 | private: |
|
15 | private: | |
41 |
|
16 | // From BarGroupBase | ||
42 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
|||
43 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
17 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
44 |
|
18 | |||
45 | private: |
|
19 | private: | |
46 |
|
20 | |||
47 | // Data |
|
21 | // Data | |
48 | StackedBarChartSeries& mSeries; |
|
|||
49 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
|||
50 | int mMax; |
|
|||
51 |
|
||||
52 | int mHeight; // Layout spesific |
|
|||
53 | int mWidth; |
|
|||
54 | int mBarDefaultWidth; |
|
|||
55 |
|
||||
56 | bool mLayoutSet; // True, if component has been laid out. |
|
|||
57 | bool mLayoutDirty; |
|
|||
58 |
|
||||
59 | QList<QColor> mColors; // List of colors for series for now |
|
|||
60 |
|
||||
61 | ChartTheme* mTheme; |
|
|||
62 | bool mSeparatorsVisible; |
|
|||
63 | QList<qreal> mSeparatorPositions; |
|
|||
64 |
|
||||
65 | }; |
|
22 | }; | |
66 |
|
23 | |||
67 | QTCOMMERCIALCHART_END_NAMESPACE |
|
24 | QTCOMMERCIALCHART_END_NAMESPACE | |
68 |
|
25 | |||
69 | #endif // STACKEDBARGROUP_H |
|
26 | #endif // STACKEDBARGROUP_H |
@@ -1,116 +1,122 | |||||
1 | !include( ../common.pri ) { |
|
1 | !include( ../common.pri ) { | |
2 | error( Couldn't find the common.pri file! ) |
|
2 | error( Couldn't find the common.pri file! ) | |
3 | } |
|
3 | } | |
4 |
|
4 | |||
5 | TARGET = QtCommercialChart |
|
5 | TARGET = QtCommercialChart | |
6 | DESTDIR = $$CHART_BUILD_LIB_DIR |
|
6 | DESTDIR = $$CHART_BUILD_LIB_DIR | |
7 | TEMPLATE = lib |
|
7 | TEMPLATE = lib | |
8 | QT += core \ |
|
8 | QT += core \ | |
9 | gui |
|
9 | gui | |
10 |
|
10 | |||
11 | CONFIG += debug_and_release |
|
11 | CONFIG += debug_and_release | |
12 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd |
|
12 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd | |
13 |
|
13 | |||
14 | SOURCES += \ |
|
14 | SOURCES += \ | |
15 | barchart/barchartseries.cpp \ |
|
15 | barchart/barchartseries.cpp \ | |
16 | barchart/bargroup.cpp \ |
|
16 | barchart/bargroup.cpp \ | |
17 | barchart/bar.cpp \ |
|
17 | barchart/bar.cpp \ | |
18 | barchart/stackedbarchartseries.cpp \ |
|
18 | barchart/stackedbarchartseries.cpp \ | |
19 | barchart/stackedbargroup.cpp \ |
|
19 | barchart/stackedbargroup.cpp \ | |
20 | barchart/percentbarchartseries.cpp \ |
|
20 | barchart/percentbarchartseries.cpp \ | |
21 | barchart/percentbargroup.cpp \ |
|
21 | barchart/percentbargroup.cpp \ | |
22 | barchart/barlabel.cpp \ |
|
22 | barchart/barlabel.cpp \ | |
23 | xylinechart/qxychartseries.cpp \ |
|
23 | xylinechart/qxychartseries.cpp \ | |
24 | xylinechart/xylinechartitem.cpp \ |
|
24 | xylinechart/xylinechartitem.cpp \ | |
25 | plotdomain.cpp \ |
|
25 | plotdomain.cpp \ | |
26 | qscatterseries.cpp \ |
|
26 | qscatterseries.cpp \ | |
27 | qpieseries.cpp \ |
|
27 | qpieseries.cpp \ | |
28 | qchart.cpp \ |
|
28 | qchart.cpp \ | |
29 | axisitem.cpp \ |
|
29 | axisitem.cpp \ | |
30 | qchartwidget.cpp \ |
|
30 | qchartwidget.cpp \ | |
31 | pieslice.cpp \ |
|
31 | pieslice.cpp \ | |
32 | qchartview.cpp \ |
|
32 | qchartview.cpp \ | |
33 | qchartseries.cpp \ |
|
33 | qchartseries.cpp \ | |
34 | qchartaxis.cpp \ |
|
34 | qchartaxis.cpp \ | |
35 | charttheme.cpp |
|
35 | charttheme.cpp \ | |
|
36 | barchart/separator.cpp \ | |||
|
37 | barchart/bargroupbase.cpp \ | |||
|
38 | barchart/barchartseriesbase.cpp | |||
36 |
|
39 | |||
37 | PRIVATE_HEADERS += \ |
|
40 | PRIVATE_HEADERS += \ | |
38 | xylinechart/xylinechartitem_p.h \ |
|
41 | xylinechart/xylinechartitem_p.h \ | |
39 | barchart/barlabel_p.h \ |
|
42 | barchart/barlabel_p.h \ | |
40 | barchart/bar_p.h \ |
|
43 | barchart/bar_p.h \ | |
|
44 | barchart/separator_p.h \ | |||
41 | plotdomain_p.h \ |
|
45 | plotdomain_p.h \ | |
42 | qscatterseries_p.h \ |
|
46 | qscatterseries_p.h \ | |
43 | qpieseries_p.h \ |
|
47 | qpieseries_p.h \ | |
44 | pieslice.h \ |
|
48 | pieslice.h \ | |
45 | axisitem_p.h \ |
|
49 | axisitem_p.h \ | |
46 | chartitem_p.h \ |
|
50 | chartitem_p.h \ | |
47 | charttheme_p.h |
|
51 | charttheme_p.h | |
48 |
|
52 | |||
49 | PUBLIC_HEADERS += \ |
|
53 | PUBLIC_HEADERS += \ | |
50 | qchartseries.h \ |
|
54 | qchartseries.h \ | |
51 | qscatterseries.h \ |
|
55 | qscatterseries.h \ | |
52 | qpieseries.h \ |
|
56 | qpieseries.h \ | |
53 | qchart.h \ |
|
57 | qchart.h \ | |
54 | qchartwidget.h \ |
|
58 | qchartwidget.h \ | |
55 | qchartglobal.h \ |
|
59 | qchartglobal.h \ | |
56 | xylinechart/qxychartseries.h \ |
|
60 | xylinechart/qxychartseries.h \ | |
57 | barchart/barchartseries.h \ |
|
61 | barchart/barchartseries.h \ | |
58 | barchart/bargroup.h \ |
|
62 | barchart/bargroup.h \ | |
59 | barchart/stackedbarchartseries.h \ |
|
63 | barchart/stackedbarchartseries.h \ | |
60 | barchart/stackedbargroup.h \ |
|
64 | barchart/stackedbargroup.h \ | |
61 | barchart/percentbarchartseries.h \ |
|
65 | barchart/percentbarchartseries.h \ | |
62 | barchart/percentbargroup.h \ |
|
66 | barchart/percentbargroup.h \ | |
|
67 | barchart/barchartseriesbase.h \ | |||
|
68 | barchart/bargroupbase.h \ | |||
63 | qchartview.h \ |
|
69 | qchartview.h \ | |
64 | qchartaxis.h |
|
70 | qchartaxis.h | |
65 |
|
71 | |||
66 | HEADERS += $$PUBLIC_HEADERS |
|
72 | HEADERS += $$PUBLIC_HEADERS | |
67 | HEADERS += $$PRIVATE_HEADERS |
|
73 | HEADERS += $$PRIVATE_HEADERS | |
68 |
|
74 | |||
69 | INCLUDEPATH += xylinechart \ |
|
75 | INCLUDEPATH += xylinechart \ | |
70 | barchart \ |
|
76 | barchart \ | |
71 | . |
|
77 | . | |
72 |
|
78 | |||
73 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib |
|
79 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib | |
74 | MOC_DIR = $$CHART_BUILD_DIR/lib |
|
80 | MOC_DIR = $$CHART_BUILD_DIR/lib | |
75 | UI_DIR = $$CHART_BUILD_DIR/lib |
|
81 | UI_DIR = $$CHART_BUILD_DIR/lib | |
76 | RCC_DIR = $$CHART_BUILD_DIR/lib |
|
82 | RCC_DIR = $$CHART_BUILD_DIR/lib | |
77 |
|
83 | |||
78 |
|
84 | |||
79 | DEFINES += QTCOMMERCIALCHART_LIBRARY |
|
85 | DEFINES += QTCOMMERCIALCHART_LIBRARY | |
80 |
|
86 | |||
81 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart |
|
87 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart | |
82 | public_headers.files = $$PUBLIC_HEADERS |
|
88 | public_headers.files = $$PUBLIC_HEADERS | |
83 | target.path = $$[QT_INSTALL_LIBS] |
|
89 | target.path = $$[QT_INSTALL_LIBS] | |
84 | INSTALLS += target \ |
|
90 | INSTALLS += target \ | |
85 | public_headers |
|
91 | public_headers | |
86 |
|
92 | |||
87 |
|
93 | |||
88 | install_build_headers.name = bild_headers |
|
94 | install_build_headers.name = bild_headers | |
89 | install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
95 | install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h | |
90 | install_build_headers.input = PUBLIC_HEADERS |
|
96 | install_build_headers.input = PUBLIC_HEADERS | |
91 | install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR |
|
97 | install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR | |
92 | install_build_headers.CONFIG += target_predeps no_link |
|
98 | install_build_headers.CONFIG += target_predeps no_link | |
93 | QMAKE_EXTRA_COMPILERS += install_build_headers |
|
99 | QMAKE_EXTRA_COMPILERS += install_build_headers | |
94 |
|
100 | |||
95 | chartversion.target = qchartversion_p.h |
|
101 | chartversion.target = qchartversion_p.h | |
96 | chartversion.commands = @echo "build_time" > $$chartversion.target; |
|
102 | chartversion.commands = @echo "build_time" > $$chartversion.target; | |
97 | chartversion.depends = $$HEADERS $$SOURCES |
|
103 | chartversion.depends = $$HEADERS $$SOURCES | |
98 | PRE_TARGETDEPS += qchartversion_p.h |
|
104 | PRE_TARGETDEPS += qchartversion_p.h | |
99 | QMAKE_CLEAN+= qchartversion_p.h |
|
105 | QMAKE_CLEAN+= qchartversion_p.h | |
100 | QMAKE_EXTRA_TARGETS += chartversion |
|
106 | QMAKE_EXTRA_TARGETS += chartversion | |
101 |
|
107 | |||
102 | unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
108 | unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR | |
103 | win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
109 | win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR | |
104 |
|
110 | |||
105 |
|
111 | |||
106 |
|
112 | |||
107 |
|
113 | |||
108 |
|
114 | |||
109 |
|
115 | |||
110 |
|
116 | |||
111 |
|
117 | |||
112 |
|
118 | |||
113 |
|
119 | |||
114 |
|
120 | |||
115 |
|
121 | |||
116 |
|
122 |
General Comments 0
You need to be logged in to leave comments.
Login now