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