##// END OF EJS Templates
correct drawing for barchart
sauimone -
r82:144fd3c1acc4
parent child
Show More
@@ -14,18 +14,23 int main(int argc, char *argv[])
14 BarChartSeries* series0 = new BarChartSeries();
14 BarChartSeries* series0 = new BarChartSeries();
15
15
16 // Create some test data to chart
16 // Create some test data to chart
17 QStandardItemModel dataModel(2,10);
17 QStandardItemModel dataModel(5,10);
18 QModelIndex index;
18 QModelIndex index;
19 index = dataModel.index(0,0);
19 index = dataModel.index(0,0);
20 // Series 1, items 6 to 9 missing.
20
21 // Series 0
21 dataModel.setData(dataModel.index(0,0),1);
22 dataModel.setData(dataModel.index(0,0),1);
22 dataModel.setData(dataModel.index(0,1),12);
23 dataModel.setData(dataModel.index(0,1),2);
23 dataModel.setData(dataModel.index(0,2),5);
24 dataModel.setData(dataModel.index(0,2),3);
24 dataModel.setData(dataModel.index(0,3),8);
25 dataModel.setData(dataModel.index(0,3),4);
25 dataModel.setData(dataModel.index(0,4),17);
26 dataModel.setData(dataModel.index(0,4),5);
26 dataModel.setData(dataModel.index(0,5),9);
27 dataModel.setData(dataModel.index(0,5),6);
28 dataModel.setData(dataModel.index(0,6),7);
29 dataModel.setData(dataModel.index(0,7),8);
30 dataModel.setData(dataModel.index(0,8),9);
31 dataModel.setData(dataModel.index(0,9),10);
27
32
28 // Series 2, some other items missing
33 // Series 1, some other items missing
29 dataModel.setData(dataModel.index(1,0),5);
34 dataModel.setData(dataModel.index(1,0),5);
30 dataModel.setData(dataModel.index(1,3),4);
35 dataModel.setData(dataModel.index(1,3),4);
31 dataModel.setData(dataModel.index(1,5),7);
36 dataModel.setData(dataModel.index(1,5),7);
@@ -33,6 +38,42 int main(int argc, char *argv[])
33 dataModel.setData(dataModel.index(1,8),9);
38 dataModel.setData(dataModel.index(1,8),9);
34 dataModel.setData(dataModel.index(1,9),9);
39 dataModel.setData(dataModel.index(1,9),9);
35
40
41 // Series 2
42 dataModel.setData(dataModel.index(2,0),3);
43 dataModel.setData(dataModel.index(2,1),5);
44 dataModel.setData(dataModel.index(2,2),8);
45 dataModel.setData(dataModel.index(2,3),13);
46 dataModel.setData(dataModel.index(2,4),8);
47 dataModel.setData(dataModel.index(2,5),5);
48 dataModel.setData(dataModel.index(2,6),3);
49 dataModel.setData(dataModel.index(2,7),2);
50 dataModel.setData(dataModel.index(2,8),1);
51 dataModel.setData(dataModel.index(2,9),1);
52
53 // Series 3
54 dataModel.setData(dataModel.index(3,0),5);
55 dataModel.setData(dataModel.index(3,1),6);
56 dataModel.setData(dataModel.index(3,2),7);
57 dataModel.setData(dataModel.index(3,3),3);
58 dataModel.setData(dataModel.index(3,4),4);
59 dataModel.setData(dataModel.index(3,5),5);
60 dataModel.setData(dataModel.index(3,6),8);
61 dataModel.setData(dataModel.index(3,7),9);
62 dataModel.setData(dataModel.index(3,8),10);
63 dataModel.setData(dataModel.index(3,9),5);
64
65 // Series 4
66 dataModel.setData(dataModel.index(4,0),9);
67 dataModel.setData(dataModel.index(4,1),7);
68 dataModel.setData(dataModel.index(4,2),5);
69 dataModel.setData(dataModel.index(4,3),3);
70 dataModel.setData(dataModel.index(4,4),1);
71 dataModel.setData(dataModel.index(4,5),2);
72 dataModel.setData(dataModel.index(4,6),4);
73 dataModel.setData(dataModel.index(4,7),6);
74 dataModel.setData(dataModel.index(4,8),8);
75 dataModel.setData(dataModel.index(4,9),10);
76
36 series0->setData(&dataModel);
77 series0->setData(&dataModel);
37
78
38 ChartWidget* chartWidget = new ChartWidget(&window);
79 ChartWidget* chartWidget = new ChartWidget(&window);
@@ -41,6 +41,9 void Bar::setPos(qreal x, qreal y)
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) {
45 return;
46 }
44 // Set color for bar. TODO: gradients, textures etc
47 // Set color for bar. TODO: gradients, textures etc
45 QPen pen = painter->pen();
48 QPen pen = painter->pen();
46 pen.setColor( mColor );
49 pen.setColor( mColor );
@@ -49,14 +52,13 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidg
49
52
50 // Draw bar
53 // Draw bar
51 // TODO: Pen width affects bar height for now. This should be rect
54 // TODO: Pen width affects bar height for now. This should be rect
52 painter->drawLine(scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mHeight - mWidth,
55 // painter->drawRect(mXpos,mYpos,mWidth,mHeight);
53 scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mWidth);
56 painter->drawLine(mXpos, mYpos + mHeight - mWidth,
57 mXpos, mYpos + mWidth);
54 }
58 }
55
59
56 QRectF Bar::boundingRect() const
60 QRectF Bar::boundingRect() const
57 {
61 {
58 // TODO: check validity of this (I suppose there is easier way, and currently this bit incorrect :)
59 // QRectF r(scenePos().x()+mXpos, scenePos().y()+mYpos, scenePos().x() + mWidth, scenePos().y() + mHeight );
60 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
62 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
61 return r;
63 return r;
62 }
64 }
@@ -79,12 +79,13 int BarChartSeries::valueAt(int series, int item)
79 return mModel->data(index).toInt();
79 return mModel->data(index).toInt();
80 }
80 }
81
81
82 /*
82 void BarChartSeries::chartSizeChanged(QRectF rect)
83 void BarChartSeries::chartSizeChanged(QRectF rect)
83 {
84 {
84 qDebug() << "barchart size changed:" << rect;
85 qDebug() << "barchart size changed:" << rect;
85 // mBarGroup->resize(rect.toRect().width(), rect.toRect().height());
86 // mBarGroup->resize(rect.toRect().width(), rect.toRect().height());
86 }
87 }
87
88 */
88 #include "moc_barchartseries.cpp"
89 #include "moc_barchartseries.cpp"
89
90
90 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
@@ -22,7 +22,7 public:
22 // from QChartSeries
22 // from QChartSeries
23 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
23 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
24
24
25 // TODO: This as dataModel instead of n different setters. (data model itself can accept lists and whatnot)
25 // TODO: Better data model?
26 virtual bool setData(QAbstractItemModel* model);
26 virtual bool setData(QAbstractItemModel* model);
27
27
28 // Methods to find out minimum and maximum values of data
28 // Methods to find out minimum and maximum values of data
@@ -35,7 +35,8 public:
35
35
36 public Q_SLOTS:
36 public Q_SLOTS:
37
37
38 void chartSizeChanged(QRectF rect);
38 // TODO: wrong place for this... series don't know anything about layout
39 // void chartSizeChanged(QRectF rect);
39
40
40 private:
41 private:
41
42
@@ -31,6 +31,7 void BarGroup::setSize(const QSize& size)
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 }
35 }
35
36
36
37
@@ -48,9 +49,11 void BarGroup::setBarWidth( int w )
48 mBarDefaultWidth = w;
49 mBarDefaultWidth = w;
49 }
50 }
50
51
51 void BarGroup::setColor( QColor color )
52 int BarGroup::addColor( QColor color )
52 {
53 {
53 mColor = color;
54 int colorIndex = mColors.count();
55 mColors.append(color);
56 return colorIndex;
54 }
57 }
55
58
56 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
59 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -69,7 +72,6 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
69
72
70 QRectF BarGroup::boundingRect() const
73 QRectF BarGroup::boundingRect() const
71 {
74 {
72 // TODO: correct this (currently ignores position)
73 return QRectF(0,0,mWidth,mHeight);
75 return QRectF(0,0,mWidth,mHeight);
74 }
76 }
75
77
@@ -107,13 +109,13 void BarGroup::layoutChanged()
107 return;
109 return;
108 }
110 }
109
111
110 // Align center
112 // TODO: better way to auto-layout
111 int count = mSeries.countItemsInSeries();
113 int count = mSeries.countItemsInSeries();
112 int posStep = (mWidth / (count));
114 int posStep = (mWidth / (count+1));
113 int startPos = (mWidth / count+1);
115 int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2;
114 qDebug() << "startpos" << startPos;
116 qDebug() << "startpos" << startPos;
115
117
116 // Scaling. TODO: better one.
118 // Scaling.
117 int itemIndex(0);
119 int itemIndex(0);
118 for (int series = 0; series < mSeries.countSeries(); series++) {
120 for (int series = 0; series < mSeries.countSeries(); series++) {
119 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
121 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
@@ -121,14 +123,12 void BarGroup::layoutChanged()
121 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
123 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
122 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
124 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
123
125
124 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
126 // TODO: width settable per bar?
125 //TODO: test hack
127 bar->resize(mBarDefaultWidth, barHeight);
126 if (0 == series) {
128 bar->setColor(mColors.at(series));
127 bar->setColor(QColor(255,0,0,128));
129
128 } else {
130 // TODO: bar width shouldn't affect height. Currently it does because pen width also affects height. (QPainter thingy...)
129 bar->setColor(QColor(255,255,0,128));
131 bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight - barHeight + mBarDefaultWidth);
130 }
131 bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, 0);
132 itemIndex++;
132 itemIndex++;
133 }
133 }
134 }
134 }
@@ -37,7 +37,9 public:
37 void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area
37 void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area
38 void setPos(qreal x, qreal y);
38 void setPos(qreal x, qreal y);
39 void setBarWidth( int w ); // Default width for each bar
39 void setBarWidth( int w ); // Default width for each bar
40 void setColor( QColor color ); // Default color for each bar
40
41 // TODO: set color theme instead? or use some external color theme call this
42 int addColor( QColor color );
41
43
42 // From QGraphicsItem
44 // From QGraphicsItem
43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
45 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
@@ -59,11 +61,10 private:
59 int mWidth;
61 int mWidth;
60 int mBarDefaultWidth;
62 int mBarDefaultWidth;
61
63
62 QColor mColor;
63
64 bool mLayoutSet; // True, if component has been laid out.
64 bool mLayoutSet; // True, if component has been laid out.
65 bool mLayoutDirty;
65 bool mLayoutDirty;
66
66
67 QList<QColor> mColors; // List of colors for series for now
67 };
68 };
68
69
69 QTCOMMERCIALCHART_END_NAMESPACE
70 QTCOMMERCIALCHART_END_NAMESPACE
@@ -84,6 +84,14 void QChart::addSeries(QChartSeries* series)
84 qDebug() << "barSeries added";
84 qDebug() << "barSeries added";
85 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
85 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
86 BarGroup* barGroup = new BarGroup(*barSeries,this);
86 BarGroup* barGroup = new BarGroup(*barSeries,this);
87
88 // Add some fugly colors for 5 fist series...
89 barGroup->addColor(QColor(255,0,0,128));
90 barGroup->addColor(QColor(255,255,0,128));
91 barGroup->addColor(QColor(0,255,0,128));
92 barGroup->addColor(QColor(0,0,255,128));
93 barGroup->addColor(QColor(255,128,0,128));
94
87 m_chartItems<<barGroup;
95 m_chartItems<<barGroup;
88 childItems().append(barGroup);
96 childItems().append(barGroup);
89 break;
97 break;
General Comments 0
You need to be logged in to leave comments. Login now