##// END OF EJS Templates
Some fixes to bar charts. Now bars draw almost correctly
sauimone -
r97:f3d03f051601
parent child
Show More
@@ -11,7 +11,6 Bar::Bar(ChartItem *parent)
11
11
12 void Bar::setSize(const QSize& size)
12 void Bar::setSize(const QSize& size)
13 {
13 {
14 //mSize = size;
15 mWidth = size.width();
14 mWidth = size.width();
16 mHeight = size.height();
15 mHeight = size.height();
17 }
16 }
@@ -23,7 +22,7 void Bar::setPlotDomain(const PlotDomain& data)
23
22
24 void Bar::resize( int w, int h )
23 void Bar::resize( int w, int h )
25 {
24 {
26 qDebug() << "bar::resize" << w << h;
25 // qDebug() << "bar::resize" << w << h;
27 mWidth = w;
26 mWidth = w;
28 mHeight = h;
27 mHeight = h;
29 }
28 }
@@ -34,7 +33,7 void Bar::setColor( QColor col )
34 }
33 }
35 void Bar::setPos(qreal x, qreal y)
34 void Bar::setPos(qreal x, qreal y)
36 {
35 {
37 qDebug() << "Bar::setpos" << x << y;
36 // qDebug() << "Bar::setpos" << x << y;
38 mXpos = x;
37 mXpos = x;
39 mYpos = y;
38 mYpos = y;
40 }
39 }
@@ -44,15 +43,10 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidg
44 if (0 == mHeight) {
43 if (0 == mHeight) {
45 return;
44 return;
46 }
45 }
47 // Set color for bar. TODO: gradients, textures etc
46 // TODO: accept brush instead of color
48 QPen pen = painter->pen();
47 QBrush brush(mColor);
49 pen.setColor( mColor );
48 painter->setBrush(brush);
50 pen.setWidth( mWidth );
49 painter->drawRect(mXpos-mWidth, mYpos-mHeight ,mWidth ,mHeight); // Evil inverse rect, because we want bars to grow from bottom to top :)
51 painter->setPen(pen);
52
53 // Draw bar
54 painter->drawLine(mXpos, mYpos,
55 mXpos, mYpos - mHeight);
56 }
50 }
57
51
58 QRectF Bar::boundingRect() const
52 QRectF Bar::boundingRect() const
@@ -1,4 +1,5
1 #include <limits.h>
1 #include <limits.h>
2 #include <QDebug>
2 #include "stackedbarchartseries.h"
3 #include "stackedbarchartseries.h"
3
4
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -60,12 +61,13 int StackedBarChartSeries::maxColumnSum()
60
61
61 int max = INT_MIN;
62 int max = INT_MIN;
62
63
63 for (int col=0; col <mModel->rowCount(); col++) {
64 for (int col=0; col <mModel->columnCount(); col++) {
64 int sum = columnSum(col);
65 int sum = columnSum(col);
65 if (sum > max) {
66 if (sum > max) {
66 max = sum;
67 max = sum;
67 }
68 }
68 }
69 }
70 qDebug() << "maxColumnSum" << max;
69 return max;
71 return max;
70 }
72 }
71
73
@@ -98,6 +100,7 int StackedBarChartSeries::columnSum(int column)
98 for (int row = 0; row < count; row++) {
100 for (int row = 0; row < count; row++) {
99 sum += mModel->data(mModel->index(row,column)).toInt();
101 sum += mModel->data(mModel->index(row,column)).toInt();
100 }
102 }
103 qDebug() << "sum for column" << column << "=" << sum;
101 return sum;
104 return sum;
102 }
105 }
103
106
@@ -9,7 +9,7 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p
9 ,mSeries(series)
9 ,mSeries(series)
10 ,mLayoutSet(false)
10 ,mLayoutSet(false)
11 ,mLayoutDirty(true)
11 ,mLayoutDirty(true)
12 ,mBarDefaultWidth(10) // TODO: remove hard coding, when we have layout code ready
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 {
13 {
14 dataChanged();
14 dataChanged();
15 }
15 }
@@ -110,11 +110,10 void StackedBarGroup::layoutChanged()
110 int posStepX = (mWidth / (count+1));
110 int posStepX = (mWidth / (count+1));
111
111
112 int itemIndex(0);
112 int itemIndex(0);
113 int xPos = (mWidth / (count+1)) - mSeries.countColumns() * mBarDefaultWidth /2;
113 int xPos = (mWidth / (count+1)) - mSeries.countRows() * mBarDefaultWidth /2;
114 for (int column = 0; column < mSeries.countColumns(); column++) {
114 for (int column = 0; column < mSeries.countColumns(); column++) {
115 int yPos = mHeight;
115 int yPos = mHeight;
116 for (int row=0; row < mSeries.countRows(); row++) {
116 for (int row=0; row < mSeries.countRows(); row++) {
117 qDebug() << itemIndex;
118 int barHeight = mSeries.valueAt(row, column) * scale;
117 int barHeight = mSeries.valueAt(row, column) * scale;
119 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
118 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
120
119
@@ -122,8 +121,9 void StackedBarGroup::layoutChanged()
122 bar->resize(mBarDefaultWidth, barHeight);
121 bar->resize(mBarDefaultWidth, barHeight);
123 bar->setColor(mColors.at(row));
122 bar->setColor(mColors.at(row));
124 bar->setPos(xPos, yPos);
123 bar->setPos(xPos, yPos);
124 qDebug() << itemIndex << "pos" << xPos << yPos;
125 itemIndex++;
125 itemIndex++;
126 yPos += barHeight;
126 yPos -= barHeight;
127 }
127 }
128 xPos += posStepX;
128 xPos += posStepX;
129 }
129 }
General Comments 0
You need to be logged in to leave comments. Login now