##// END OF EJS Templates
drawing fix for bar chart
sauimone -
r83:1b872afb5c9f
parent child
Show More
@@ -1,66 +1,65
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(ChartItem *parent)
8 8 : ChartItem(parent)
9 9 {
10 10 }
11 11
12 12 void Bar::setSize(const QSize& size)
13 13 {
14 14 //mSize = size;
15 15 mWidth = size.width();
16 16 mHeight = size.height();
17 17 }
18 18
19 19 void Bar::setPlotDomain(const PlotDomain& data)
20 20 {
21 21 mPlotDomain = data;
22 22 }
23 23
24 24 void Bar::resize( int w, int h )
25 25 {
26 26 qDebug() << "bar::resize" << w << h;
27 27 mWidth = w;
28 28 mHeight = h;
29 29 }
30 30
31 31 void Bar::setColor( QColor col )
32 32 {
33 33 mColor = col;
34 34 }
35 35 void Bar::setPos(qreal x, qreal y)
36 36 {
37 37 qDebug() << "Bar::setpos" << x << y;
38 38 mXpos = x;
39 39 mYpos = y;
40 40 }
41 41
42 42 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
43 43 {
44 44 if (0 == mHeight) {
45 // Nothing to draw
45 46 return;
46 47 }
47 48 // Set color for bar. TODO: gradients, textures etc
48 49 QPen pen = painter->pen();
49 50 pen.setColor( mColor );
50 51 pen.setWidth( mWidth );
51 52 painter->setPen(pen);
52 53
53 54 // Draw bar
54 // TODO: Pen width affects bar height for now. This should be rect
55 // painter->drawRect(mXpos,mYpos,mWidth,mHeight);
56 painter->drawLine(mXpos, mYpos + mHeight - mWidth,
57 mXpos, mYpos + mWidth);
55 painter->drawLine(mXpos, mYpos ,
56 mXpos, mYpos - mHeight);
58 57 }
59 58
60 59 QRectF Bar::boundingRect() const
61 60 {
62 61 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
63 62 return r;
64 63 }
65 64
66 65 QTCOMMERCIALCHART_END_NAMESPACE
@@ -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 // TODO: singleton?
8 8 //BarGroup* BarGroup::mBarGroupInstance = NULL;
9 9
10 10 //BarGroup::BarGroup(QGraphicsItem *parent) :
11 11 // QGraphicsItem(parent)
12 12 // ,mSeries(series)
13 13 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
14 14 ChartItem(parent)
15 15 ,mSeries(series)
16 16 ,mLayoutSet(false)
17 17 ,mLayoutDirty(true)
18 18 ,mBarDefaultWidth(10)
19 19 {
20 20 dataChanged();
21 21 }
22 22
23 23
24 24 void BarGroup::setSize(const QSize& size)
25 25 {
26 26 // TODO: refactor this
27 27 qDebug() << "BarGroup::setSize";
28 28 resize(size.width(),size.height());
29 29 }
30 30
31 31 void BarGroup::setPlotDomain(const PlotDomain& data)
32 32 {
33 33 qDebug() << "BarGroup::setPlotDomain";
34 34 // TODO:
35 35 }
36 36
37 37
38 38 void BarGroup::resize( int w, int h )
39 39 {
40 40 qDebug() << "QBarChart::resize";
41 41 mWidth = w;
42 42 mHeight = h;
43 43 layoutChanged();
44 44 mLayoutSet = true;
45 45 }
46 46
47 47 void BarGroup::setBarWidth( int w )
48 48 {
49 49 mBarDefaultWidth = w;
50 50 }
51 51
52 52 int BarGroup::addColor( QColor color )
53 53 {
54 54 int colorIndex = mColors.count();
55 55 mColors.append(color);
56 56 return colorIndex;
57 57 }
58 58
59 59 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
60 60 {
61 61 if (!mLayoutSet) {
62 62 qDebug() << "QBarChart::paint called without layout set. Aborting.";
63 63 return;
64 64 }
65 65 if (mLayoutDirty) {
66 66 // Layout or data has changed. Need to redraw.
67 67 foreach(QGraphicsItem* i, childItems()) {
68 68 i->paint(painter,option,widget);
69 69 }
70 70 }
71 71 }
72 72
73 73 QRectF BarGroup::boundingRect() const
74 74 {
75 75 return QRectF(0,0,mWidth,mHeight);
76 76 }
77 77
78 78
79 79 void BarGroup::dataChanged()
80 80 {
81 81 qDebug() << "QBarChart::dataChanged mSeries";
82 82
83 83 // Find out maximum and minimum of all series
84 84 mMax = mSeries.max();
85 85 mMin = mSeries.min();
86 86
87 87 // Delete old bars
88 88 // Is this correct way to delete childItems?
89 89 foreach (QGraphicsItem* item, childItems()) {
90 90 delete item;
91 91 }
92 92
93 93 // Create new graphic items for bars
94 94 int totalItems = mSeries.countTotalItems();
95 95 for (int i=0; i<totalItems; i++) {
96 96 Bar *bar = new Bar(this);
97 97 childItems().append(bar);
98 98 }
99 99
100 100 mLayoutDirty = true;
101 101 }
102 102
103 103 void BarGroup::layoutChanged()
104 104 {
105 105 // Scale bars to new layout
106 106 // Layout for bars:
107 107 if (mSeries.countSeries() <= 0) {
108 108 // Nothing to do.
109 109 return;
110 110 }
111 111
112 112 // TODO: better way to auto-layout
113 113 int count = mSeries.countItemsInSeries();
114 114 int posStep = (mWidth / (count+1));
115 115 int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2;
116 116 qDebug() << "startpos" << startPos;
117 117
118 118 // Scaling.
119 119 int itemIndex(0);
120 120 for (int series = 0; series < mSeries.countSeries(); series++) {
121 121 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
122 122 qDebug() << itemIndex;
123 123 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
124 124 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
125 125
126 126 // TODO: width settable per bar?
127 127 bar->resize(mBarDefaultWidth, barHeight);
128 128 bar->setColor(mColors.at(series));
129 129
130 130 // TODO: bar width shouldn't affect height. Currently it does because pen width also affects height. (QPainter thingy...)
131 bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight - barHeight + mBarDefaultWidth);
131 // bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight - barHeight);
132 bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight);
132 133 itemIndex++;
133 134 }
134 135 }
135 136 mLayoutDirty = true;
136 137 }
137 138
138 139 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now