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