##// END OF EJS Templates
Added bar chart example
sauimone -
r78:681c12da395a
parent child
Show More
@@ -0,0 +1,17
1 !include( ../../common.pri ) {
2 error( "Couldn't find the common.pri file!" )
3 }
4
5 !include( ../../integrated.pri ) {
6 error( "Couldn't find the integrated.pri file !")
7 }
8
9 TARGET = barchart
10 TEMPLATE = app
11 QT += core gui
12 SOURCES += main.cpp \
13 chartwidget.cpp
14
15 HEADERS += \
16 chartwidget.h
17
@@ -0,0 +1,6
1 #include "chartwidget.h"
2
3 ChartWidget::ChartWidget(QWidget *parent) :
4 QChartView(parent)
5 {
6 }
@@ -0,0 +1,21
1 #ifndef CHARTWIDGET_H
2 #define CHARTWIDGET_H
3
4 #include <qchartview.h>
5
6 QTCOMMERCIALCHART_USE_NAMESPACE
7
8
9 class ChartWidget : public QChartView
10 {
11 Q_OBJECT
12 public:
13 explicit ChartWidget(QWidget *parent = 0);
14
15 signals:
16
17 public slots:
18
19 };
20
21 #endif // CHARTWIDGET_H
@@ -0,0 +1,47
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
4 #include <barchartseries.h>
5 #include "chartwidget.h"
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
9 int main(int argc, char *argv[])
10 {
11 QApplication a(argc, argv);
12 QMainWindow window;
13
14 BarChartSeries* series0 = new BarChartSeries();
15
16 // Create some test data to chart
17 QStandardItemModel dataModel(2,10);
18 QModelIndex index;
19 index = dataModel.index(0,0);
20 // Series 1, items 6 to 9 missing.
21 dataModel.setData(dataModel.index(0,0),1);
22 dataModel.setData(dataModel.index(0,1),12);
23 dataModel.setData(dataModel.index(0,2),5);
24 dataModel.setData(dataModel.index(0,3),8);
25 dataModel.setData(dataModel.index(0,4),17);
26 dataModel.setData(dataModel.index(0,5),9);
27
28 // Series 2, some other items missing
29 dataModel.setData(dataModel.index(1,0),5);
30 dataModel.setData(dataModel.index(1,3),4);
31 dataModel.setData(dataModel.index(1,5),7);
32 dataModel.setData(dataModel.index(1,6),8);
33 dataModel.setData(dataModel.index(1,8),9);
34 dataModel.setData(dataModel.index(1,9),9);
35
36 series0->setData(&dataModel);
37
38 ChartWidget* chartWidget = new ChartWidget(&window);
39 chartWidget->addSeries(series0);
40
41 window.setCentralWidget(chartWidget);
42 window.resize(400, 300);
43 window.show();
44
45 return a.exec();
46 }
47
@@ -1,4 +1,5
1 1 TEMPLATE = subdirs
2 2 SUBDIRS += linechart \
3 3 zoomlinechart \
4 colorlinechart
4 colorlinechart \
5 barchart
@@ -23,6 +23,7 void Bar::setPlotDomain(const PlotDomain& data)
23 23
24 24 void Bar::resize( int w, int h )
25 25 {
26 qDebug() << "bar::resize" << w << h;
26 27 mWidth = w;
27 28 mHeight = h;
28 29 }
@@ -33,6 +34,7 void Bar::setColor( QColor col )
33 34 }
34 35 void Bar::setPos(qreal x, qreal y)
35 36 {
37 qDebug() << "Bar::setpos" << x << y;
36 38 mXpos = x;
37 39 mYpos = y;
38 40 }
@@ -10,15 +10,30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10 //BarGroup::BarGroup(QGraphicsItem *parent) :
11 11 // QGraphicsItem(parent)
12 12 // ,mSeries(series)
13 BarGroup::BarGroup(BarChartSeries& series, ChartItem *parent) :
13 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
14 14 ChartItem(parent)
15 15 ,mSeries(series)
16 16 ,mLayoutSet(false)
17 17 ,mLayoutDirty(true)
18 ,mBarDefaultWidth(10)
18 19 {
19 20 dataChanged();
20 21 }
21 22
23
24 void BarGroup::setSize(const QSize& size)
25 {
26 // TODO: refactor this
27 qDebug() << "BarGroup::setSize";
28 resize(size.width(),size.height());
29 }
30
31 void BarGroup::setPlotDomain(const PlotDomain& data)
32 {
33 qDebug() << "BarGroup::setPlotDomain";
34 }
35
36
22 37 void BarGroup::resize( int w, int h )
23 38 {
24 39 qDebug() << "QBarChart::resize";
@@ -87,27 +102,33 void BarGroup::layoutChanged()
87 102 {
88 103 // Scale bars to new layout
89 104 // Layout for bars:
90 int count = mSeries.countSeries();
91 if (count <= 0) {
105 if (mSeries.countSeries() <= 0) {
92 106 // Nothing to do.
93 107 return;
94 108 }
95 109
96 110 // Align center
111 int count = mSeries.countItemsInSeries();
97 112 int posStep = (mWidth / (count));
98 113 int startPos = (mWidth / count+1);
99 114 qDebug() << "startpos" << startPos;
100 115
101 116 // Scaling. TODO: better one.
102 117 int itemIndex(0);
103 for (int series = 0; series < count; series++) {
104 for (int item=0; item < count; item++) {
118 for (int series = 0; series < mSeries.countSeries(); series++) {
119 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
120 qDebug() << itemIndex;
105 121 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
106 122 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
107 123
108 124 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
109 bar->setColor(mColor);
110 bar->setPos(itemIndex*posStep+startPos, 0);
125 //TODO: test hack
126 if (0 == series) {
127 bar->setColor(QColor(255,0,0,128));
128 } else {
129 bar->setColor(QColor(255,255,0,128));
130 }
131 bar->setPos(itemIndex*posStep+startPos + series * mBarDefaultWidth, 0);
111 132 itemIndex++;
112 133 }
113 134 }
@@ -27,7 +27,11 public:
27 27 private:
28 28 */
29 29 public:
30 explicit BarGroup(BarChartSeries& series, ChartItem *parent = 0);
30 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
31
32 // From ChartItem
33 virtual void setSize(const QSize& size);
34 virtual void setPlotDomain(const PlotDomain& data);
31 35
32 36 // Layout "api"
33 37 void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area
@@ -84,9 +84,9 void QChart::addSeries(QChartSeries* series)
84 84
85 85 qDebug() << "barSeries added";
86 86 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
87 connect(this, SIGNAL(sizeChanged(QRectF)),
88 barSeries, SLOT(chartSizeChanged(QRectF)));
89
87 BarGroup* barGroup = new BarGroup(*barSeries,this);
88 m_chartItems<<barGroup;
89 childItems().append(barGroup);
90 90 break;
91 91 }
92 92 case QChartSeries::SeriesTypeScatter: {
@@ -15,6 +15,8
15 15 #include <QMessageBox>
16 16 #include <cmath>
17 17 #include <QDebug>
18 #include <QStandardItemModel>
19
18 20
19 21 QTCOMMERCIALCHART_USE_NAMESPACE
20 22
@@ -192,14 +194,29 void MainWidget::addSeries(QString series, QString data)
192 194 // This is the another way of creating series. Should we create test cases for both ways, if we support them?
193 195 qDebug() << "Bar chart series";
194 196 newSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this);
195 QList<int> barData;
196 barData << 1;
197 barData << 12;
198 barData << 5;
199 barData << 8;
200 barData << 17;
201 barData << 9;
202 newSeries->setData(barData);
197
198 // Create some test data to chart
199 QStandardItemModel dataModel(2,10,this);
200 QModelIndex index;
201 index = dataModel.index(0,0);
202 // Series 1, items 6 to 9 missing.
203 dataModel.setData(dataModel.index(0,0),1);
204 dataModel.setData(dataModel.index(0,1),12);
205 dataModel.setData(dataModel.index(0,2),5);
206 dataModel.setData(dataModel.index(0,3),8);
207 dataModel.setData(dataModel.index(0,4),17);
208 dataModel.setData(dataModel.index(0,5),9);
209
210 // Series 2, some other items missing
211 dataModel.setData(dataModel.index(1,0),5);
212 dataModel.setData(dataModel.index(1,3),4);
213 dataModel.setData(dataModel.index(1,5),7);
214 dataModel.setData(dataModel.index(1,6),8);
215 dataModel.setData(dataModel.index(1,8),9);
216 dataModel.setData(dataModel.index(1,9),9);
217
218 newSeries->setData(&dataModel);
219
203 220 m_chartWidget->addSeries(newSeries);
204 221 }
205 222
General Comments 0
You need to be logged in to leave comments. Login now