##// 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
@@ -1,62 +1,64
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 qDebug() << "bar::resize" << w << h;
26 27 mWidth = w;
27 28 mHeight = h;
28 29 }
29 30
30 31 void Bar::setColor( QColor col )
31 32 {
32 33 mColor = 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 }
39 41
40 42 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
41 43 {
42 44 // Set color for bar. TODO: gradients, textures etc
43 45 QPen pen = painter->pen();
44 46 pen.setColor( mColor );
45 47 pen.setWidth( mWidth );
46 48 painter->setPen(pen);
47 49
48 50 // Draw bar
49 51 // TODO: Pen width affects bar height for now. This should be rect
50 52 painter->drawLine(scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mHeight - mWidth,
51 53 scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mWidth);
52 54 }
53 55
54 56 QRectF Bar::boundingRect() const
55 57 {
56 58 // TODO: check validity of this (I suppose there is easier way, and currently this bit incorrect :)
57 59 // QRectF r(scenePos().x()+mXpos, scenePos().y()+mYpos, scenePos().x() + mWidth, scenePos().y() + mHeight );
58 60 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
59 61 return r;
60 62 }
61 63
62 64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,117 +1,138
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 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";
25 40 mWidth = w;
26 41 mHeight = h;
27 42 layoutChanged();
28 43 mLayoutSet = true;
29 44 }
30 45
31 46 void BarGroup::setBarWidth( int w )
32 47 {
33 48 mBarDefaultWidth = w;
34 49 }
35 50
36 51 void BarGroup::setColor( QColor color )
37 52 {
38 53 mColor = color;
39 54 }
40 55
41 56 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42 57 {
43 58 if (!mLayoutSet) {
44 59 qDebug() << "QBarChart::paint called without layout set. Aborting.";
45 60 return;
46 61 }
47 62 if (mLayoutDirty) {
48 63 // Layout or data has changed. Need to redraw.
49 64 foreach(QGraphicsItem* i, childItems()) {
50 65 i->paint(painter,option,widget);
51 66 }
52 67 }
53 68 }
54 69
55 70 QRectF BarGroup::boundingRect() const
56 71 {
57 72 // TODO: correct this (currently ignores position)
58 73 return QRectF(0,0,mWidth,mHeight);
59 74 }
60 75
61 76
62 77 void BarGroup::dataChanged()
63 78 {
64 79 qDebug() << "QBarChart::dataChanged mSeries";
65 80
66 81 // Find out maximum and minimum of all series
67 82 mMax = mSeries.max();
68 83 mMin = mSeries.min();
69 84
70 85 // Delete old bars
71 86 // Is this correct way to delete childItems?
72 87 foreach (QGraphicsItem* item, childItems()) {
73 88 delete item;
74 89 }
75 90
76 91 // Create new graphic items for bars
77 92 int totalItems = mSeries.countTotalItems();
78 93 for (int i=0; i<totalItems; i++) {
79 94 Bar *bar = new Bar(this);
80 95 childItems().append(bar);
81 96 }
82 97
83 98 mLayoutDirty = true;
84 99 }
85 100
86 101 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 }
114 135 mLayoutDirty = true;
115 136 }
116 137
117 138 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,67 +1,71
1 1 #ifndef QBARCHART_H
2 2 #define QBARCHART_H
3 3
4 4 #include "chartitem_p.h"
5 5 #include "bar.h"
6 6 #include "barchartseries.h"
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 // TODO: Better name for this? The function of this class is to know where each bar in series is laid out.
11 11 //class BarGroup : public QGraphicsItemGroup
12 12
13 13 class BarGroup : public ChartItem
14 14 {
15 15 /* // TODO: implement as singleton?
16 16 private:
17 17 static BarGroup* mBarGroupInstance;
18 18
19 19 public:
20 20 static BarGroup* instance()
21 21 {
22 22 if (mBarGroupInstance == NULL) {
23 23 mBarGroupInstance = new BarGroup();
24 24 }
25 25 return mBarGroupInstance;
26 26 }
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
34 38 void setPos(qreal x, qreal y);
35 39 void setBarWidth( int w ); // Default width for each bar
36 40 void setColor( QColor color ); // Default color for each bar
37 41
38 42 // From QGraphicsItem
39 43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
40 44 QRectF boundingRect() const;
41 45
42 46 private:
43 47
44 48 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
45 49 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
46 50
47 51 private:
48 52
49 53 // Data
50 54 BarChartSeries& mSeries;
51 55 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
52 56 int mMax;
53 57
54 58 int mHeight; // Layout spesific
55 59 int mWidth;
56 60 int mBarDefaultWidth;
57 61
58 62 QColor mColor;
59 63
60 64 bool mLayoutSet; // True, if component has been laid out.
61 65 bool mLayoutDirty;
62 66
63 67 };
64 68
65 69 QTCOMMERCIALCHART_END_NAMESPACE
66 70
67 71 #endif // QBARCHART_H
@@ -1,285 +1,285
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qxychartseries.h"
7 7
8 8 #include "barchartseries.h"
9 9 #include "bargroup.h"
10 10
11 11 #include "xylinechartitem_p.h"
12 12 #include "plotdomain_p.h"
13 13 #include "axisitem_p.h"
14 14 #include <QGraphicsScene>
15 15 #include <QDebug>
16 16
17 17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 18
19 19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 20 m_background(new QGraphicsRectItem(this)),
21 21 m_title(new QGraphicsTextItem(this)),
22 22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
23 23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
24 24 m_plotDataIndex(0),
25 25 m_marginSize(0)
26 26 {
27 27 // TODO: the default theme?
28 28 setTheme(QChart::ChartThemeVanilla);
29 29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
30 30 PlotDomain domain;
31 31 m_plotDomainList<<domain;
32 32
33 33 m_chartItems<<m_axisX;
34 34 m_chartItems<<m_axisY;
35 35 }
36 36
37 37 QChart::~QChart(){}
38 38
39 39 QRectF QChart::boundingRect() const
40 40 {
41 41 return m_rect;
42 42 }
43 43
44 44 void QChart::addSeries(QChartSeries* series)
45 45 {
46 46 // TODO: we should check the series not already added
47 47
48 48 m_series<<series;
49 49
50 50 switch(series->type())
51 51 {
52 52 case QChartSeries::SeriesTypeLine: {
53 53
54 54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
55 55 // Use color defined by theme in case the series does not define a custom color
56 56 if (!xyseries->color().isValid() && m_themeColors.count())
57 57 xyseries->setColor(m_themeColors.takeFirst());
58 58
59 59 m_plotDataIndex = 0 ;
60 60 m_plotDomainList.resize(1);
61 61
62 62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
63 63
64 64 for (int i = 0 ; i < xyseries->count() ; i++)
65 65 {
66 66 qreal x = xyseries->x(i);
67 67 qreal y = xyseries->y(i);
68 68 domain.m_minX = qMin(domain.m_minX,x);
69 69 domain.m_minY = qMin(domain.m_minY,y);
70 70 domain.m_maxX = qMax(domain.m_maxX,x);
71 71 domain.m_maxY = qMax(domain.m_maxY,y);
72 72 }
73 73
74 74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
75 75
76 76 m_chartItems<<item;
77 77
78 78 foreach(ChartItem* i ,m_chartItems)
79 79 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
80 80
81 81 break;
82 82 }
83 83 case QChartSeries::SeriesTypeBar: {
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: {
93 93 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
94 94 connect(this, SIGNAL(sizeChanged(QRectF)),
95 95 scatterSeries, SLOT(chartSizeChanged(QRectF)));
96 96 scatterSeries->d->setParentItem(this);
97 97 QColor nextColor = m_themeColors.takeFirst();
98 98 nextColor.setAlpha(150); // TODO: default opacity?
99 99 scatterSeries->setMarkerColor(nextColor);
100 100 }
101 101 case QChartSeries::SeriesTypePie: {
102 102 // TODO: we now have also a list of y values as a parameter, it is ignored
103 103 // we should use a generic data class instead of list of x and y values
104 104 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
105 105 connect(this, SIGNAL(sizeChanged(QRectF)),
106 106 pieSeries, SLOT(chartSizeChanged(QRectF)));
107 107 // TODO: how to define the color for all the slices of a pie?
108 108 }
109 109 }
110 110 }
111 111
112 112 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
113 113 {
114 114 // TODO: support also other types; not only scatter and pie
115 115
116 116 QChartSeries *series(0);
117 117
118 118 switch (type) {
119 119 case QChartSeries::SeriesTypeLine: {
120 120 series = QXYChartSeries::create();
121 121 break;
122 122 }
123 123 case QChartSeries::SeriesTypeBar: {
124 124 series = new BarChartSeries(this);
125 125 break;
126 126 }
127 127 case QChartSeries::SeriesTypeScatter: {
128 128 series = new QScatterSeries(this);
129 129 break;
130 130 }
131 131 case QChartSeries::SeriesTypePie: {
132 132 series = new QPieSeries(this);
133 133 break;
134 134 }
135 135 default:
136 136 Q_ASSERT(false);
137 137 break;
138 138 }
139 139
140 140 addSeries(series);
141 141 return series;
142 142 }
143 143
144 144 void QChart::setSize(const QSize& size)
145 145 {
146 146 m_rect = QRect(QPoint(0,0),size);
147 147 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
148 148
149 149
150 150 //recalculate background gradient
151 151 m_background->setRect(rect);
152 152 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
153 153 m_background->setBrush(m_backgroundGradient);
154 154
155 155 //resize elements
156 156 foreach (ChartItem* item ,m_chartItems) {
157 157 item->setPos(rect.topLeft());
158 158 item->setSize(rect.size());
159 159
160 160 }
161 161 // TODO: TTD for setting scale
162 162 //emit scaleChanged(100, 100);
163 163 // TODO: calculate the origo
164 164 // TODO: not sure if emitting a signal here is the best from performance point of view
165 165 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
166 166
167 167 update();
168 168 }
169 169
170 170 void QChart::setBackgroundColor(const QColor& color)
171 171 {
172 172 m_backgroundGradient.setColorAt( 0.0, Qt::white);
173 173 m_backgroundGradient.setColorAt( 1.0, color);
174 174 m_background->setBrush(m_backgroundGradient);
175 175 m_background->setPen(Qt::NoPen);
176 176 m_background->update();
177 177 }
178 178
179 179 void QChart::setTitle(const QString& title)
180 180 {
181 181 m_title->setPlainText(title);
182 182 }
183 183
184 184 int QChart::margin() const
185 185 {
186 186 return m_marginSize;
187 187 }
188 188
189 189 void QChart::setMargin(int margin)
190 190 {
191 191 m_marginSize = margin;
192 192 }
193 193
194 194 void QChart::setTheme(QChart::ChartTheme theme)
195 195 {
196 196 // TODO: define color themes
197 197 switch (theme) {
198 198 case ChartThemeVanilla:
199 199 m_themeColors.append(QColor(255, 238, 174));
200 200 m_themeColors.append(QColor(228, 228, 160));
201 201 m_themeColors.append(QColor(228, 179, 160));
202 202 m_themeColors.append(QColor(180, 151, 18));
203 203 m_themeColors.append(QColor(252, 252, 37));
204 204 break;
205 205 case ChartThemeIcy:
206 206 m_themeColors.append(QColor(255, 238, 174));
207 207 m_themeColors.append(QColor(228, 228, 160));
208 208 m_themeColors.append(QColor(228, 179, 160));
209 209 m_themeColors.append(QColor(180, 151, 18));
210 210 m_themeColors.append(QColor(252, 252, 37));
211 211 break;
212 212 case ChartThemeGrayscale:
213 213 m_themeColors.append(QColor(255, 238, 174));
214 214 m_themeColors.append(QColor(228, 228, 160));
215 215 m_themeColors.append(QColor(228, 179, 160));
216 216 m_themeColors.append(QColor(180, 151, 18));
217 217 m_themeColors.append(QColor(252, 252, 37));
218 218 break;
219 219 default:
220 220 Q_ASSERT(false);
221 221 break;
222 222 }
223 223
224 224 // TODO: update coloring of different elements to match the selected theme
225 225 }
226 226
227 227 void QChart::zoomInToRect(const QRect& rectangle)
228 228 {
229 229
230 230 if(!rectangle.isValid()) return;
231 231
232 232 qreal margin = this->margin();
233 233
234 234 QRect rect = rectangle.normalized();
235 235 rect.translate(-margin, -margin);
236 236
237 237 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
238 238 PlotDomain newDomain;
239 239
240 240 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
241 241 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
242 242
243 243 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
244 244 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
245 245 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
246 246 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
247 247
248 248 m_plotDomainList.resize(m_plotDataIndex + 1);
249 249 m_plotDomainList<<newDomain;
250 250 m_plotDataIndex++;
251 251
252 252 foreach (ChartItem* item ,m_chartItems)
253 253 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
254 254 update();
255 255 }
256 256
257 257 void QChart::zoomIn()
258 258 {
259 259 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
260 260 m_plotDataIndex++;
261 261 foreach (ChartItem* item ,m_chartItems)
262 262 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
263 263 update();
264 264 }else{
265 265 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
266 266 rect.setWidth(rect.width()/2);
267 267 rect.setHeight(rect.height()/2);
268 268 rect.moveCenter(m_rect.center());
269 269 zoomInToRect(rect);
270 270 }
271 271 }
272 272
273 273 void QChart::zoomOut()
274 274 {
275 275 if (m_plotDataIndex > 0) {
276 276 m_plotDataIndex--;
277 277 foreach (ChartItem* item ,m_chartItems)
278 278 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
279 279 update();
280 280 }
281 281 }
282 282
283 283 #include "moc_qchart.cpp"
284 284
285 285 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,314 +1,331
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include "qchartseries.h"
4 4 #include "qpieseries.h"
5 5 #include <qxychartseries.h>
6 6 #include <barchartseries.h>
7 7 #include <QPushButton>
8 8 #include <QComboBox>
9 9 #include <QSpinBox>
10 10 #include <QCheckBox>
11 11 #include <QGridLayout>
12 12 #include <QHBoxLayout>
13 13 #include <QLabel>
14 14 #include <QSpacerItem>
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
21 23 MainWidget::MainWidget(QWidget *parent) :
22 24 QWidget(parent)
23 25 {
24 26 QPushButton *addSeriesButton = new QPushButton("Add series");
25 27 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
26 28
27 29 // Chart background
28 30 QComboBox *backgroundCombo = new QComboBox(this);
29 31 backgroundCombo->addItem("None");
30 32 backgroundCombo->addItem("TODO Grid");
31 33 backgroundCombo->addItem("TODO Image");
32 34 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
33 35 this, SLOT(backgroundChanged(int)));
34 36
35 37 // Axis
36 38 // TODO: multiple axes?
37 39 m_autoScaleCheck = new QCheckBox("Automatic scaling");
38 40 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
39 41 // Allow setting also non-sense values (like -2147483648 and 2147483647)
40 42 m_xMinSpin = new QSpinBox();
41 43 m_xMinSpin->setMinimum(INT_MIN);
42 44 m_xMinSpin->setMaximum(INT_MAX);
43 45 m_xMinSpin->setValue(0);
44 46 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
45 47 m_xMaxSpin = new QSpinBox();
46 48 m_xMaxSpin->setMinimum(INT_MIN);
47 49 m_xMaxSpin->setMaximum(INT_MAX);
48 50 m_xMaxSpin->setValue(10);
49 51 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
50 52 m_yMinSpin = new QSpinBox();
51 53 m_yMinSpin->setMinimum(INT_MIN);
52 54 m_yMinSpin->setMaximum(INT_MAX);
53 55 m_yMinSpin->setValue(0);
54 56 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
55 57 m_yMaxSpin = new QSpinBox();
56 58 m_yMaxSpin->setMinimum(INT_MIN);
57 59 m_yMaxSpin->setMaximum(INT_MAX);
58 60 m_yMaxSpin->setValue(10);
59 61 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
60 62
61 63 QComboBox *chartTheme = new QComboBox();
62 64 chartTheme->addItem("Vanilla");
63 65 chartTheme->addItem("Icy");
64 66 chartTheme->addItem("Grayscale");
65 67 chartTheme->addItem("Tobedefined");
66 68 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
67 69 this, SLOT(changeChartTheme(int)));
68 70
69 71 QGridLayout *grid = new QGridLayout();
70 72 QGridLayout *mainLayout = new QGridLayout();
71 73 //grid->addWidget(new QLabel("Add series:"), 0, 0);
72 74 grid->addWidget(addSeriesButton, 0, 1);
73 75 grid->addWidget(new QLabel("Background:"), 2, 0);
74 76 grid->addWidget(backgroundCombo, 2, 1);
75 77 grid->addWidget(m_autoScaleCheck, 3, 0);
76 78 grid->addWidget(new QLabel("x min:"), 4, 0);
77 79 grid->addWidget(m_xMinSpin, 4, 1);
78 80 grid->addWidget(new QLabel("x max:"), 5, 0);
79 81 grid->addWidget(m_xMaxSpin, 5, 1);
80 82 grid->addWidget(new QLabel("y min:"), 6, 0);
81 83 grid->addWidget(m_yMinSpin, 6, 1);
82 84 grid->addWidget(new QLabel("y max:"), 7, 0);
83 85 grid->addWidget(m_yMaxSpin, 7, 1);
84 86 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
85 87 grid->addWidget(chartTheme, 8, 1);
86 88 // add row with empty label to make all the other rows static
87 89 grid->addWidget(new QLabel(""), 9, 0);
88 90 grid->setRowStretch(9, 1);
89 91
90 92 mainLayout->addLayout(grid, 0, 0);
91 93
92 94 // Scatter specific settings
93 95 m_scatterLayout = new QGridLayout();
94 96 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
95 97 m_scatterLayout->setEnabled(false);
96 98
97 99 // Pie specific settings
98 100 m_pieLayout = new QGridLayout();
99 101 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
100 102 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
101 103 pieSizeSpin->setMinimum(LONG_MIN);
102 104 pieSizeSpin->setMaximum(LONG_MAX);
103 105 pieSizeSpin->setValue(1.0);
104 106 pieSizeSpin->setSingleStep(0.1);
105 107 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
106 108 m_pieLayout->setEnabled(false);
107 109 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
108 110
109 111 mainLayout->addLayout(m_scatterLayout, 1, 0);
110 112 mainLayout->addLayout(m_pieLayout, 2, 0);
111 113
112 114 m_chartWidget = new QChartWidget(this);
113 115 //m_chartWidget->setColor(Qt::red);
114 116 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
115 117 // hbox->setStretch(1, 1);
116 118
117 119 setLayout(mainLayout);
118 120
119 121 m_autoScaleCheck->setChecked(true);
120 122 testDataChanged(0);
121 123 }
122 124
123 125 void MainWidget::addSeries()
124 126 {
125 127 DataSerieDialog dialog(m_defaultSeriesName, this);
126 128 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
127 129 dialog.exec();
128 130 }
129 131
130 132 void MainWidget::addSeries(QString series, QString data)
131 133 {
132 134 qDebug() << "addSeries: " << series << " data: " << data;
133 135 m_defaultSeriesName = series;
134 136
135 137 // TODO: a dedicated data class for storing x and y values
136 138 QList<qreal> x;
137 139 QList<qreal> y;
138 140
139 141 if (data == "linear") {
140 142 for (int i = 0; i < 20; i++) {
141 143 x.append(i);
142 144 y.append(i);
143 145 }
144 146 } else if (data == "linear, 1M") {
145 147 for (int i = 0; i < 10000; i++) {
146 148 x.append(i);
147 149 y.append(20);
148 150 }
149 151 } else if (data == "SIN") {
150 152 for (int i = 0; i < 100; i++) {
151 153 x.append(i);
152 154 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
153 155 }
154 156 } else if (data == "SIN + random") {
155 157 for (qreal i = 0; i < 100; i += 0.1) {
156 158 x.append(i + (rand() % 5));
157 159 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
158 160 }
159 161 } else {
160 162 // TODO: check if data has a valid file name
161 163 Q_ASSERT(false);
162 164 }
163 165
164 166 // TODO: color of the series
165 167 QChartSeries *newSeries = 0;
166 168 if (series == "Scatter") {
167 169 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
168 170 Q_ASSERT(newSeries->setData(x, y));
169 171 } else if (series == "Pie") {
170 172 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
171 173 Q_ASSERT(newSeries->setData(y));
172 174 } else if (series == "Line") {
173 175 // TODO: adding data to an existing line series does not give any visuals for some reason
174 176 // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine);
175 177 // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries);
176 178 // lineSeries->setColor(Qt::blue);
177 179 // for (int i(0); i < x.count() && i < y.count(); i++) {
178 180 // lineSeries->add(x.at(i), y.at(i));
179 181 // }
180 182 //Q_ASSERT(newSeries->setData(x, y));
181 183 QXYChartSeries* series0 = QXYChartSeries::create();
182 184 for (int i(0); i < x.count() && i < y.count(); i++)
183 185 series0->add(x.at(i), y.at(i));
184 186 m_chartWidget->addSeries(series0);
185 187 newSeries = series0;
186 188 } else {
187 189 // TODO
188 190 }
189 191
190 192 // BarChart
191 193 if (series == "Bar") {
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
206 223 setCurrentSeries(newSeries);
207 224 }
208 225
209 226 void MainWidget::setCurrentSeries(QChartSeries *series)
210 227 {
211 228 m_currentSeries = series;
212 229 switch (m_currentSeries->type()) {
213 230 case QChartSeries::SeriesTypeLine:
214 231 break;
215 232 case QChartSeries::SeriesTypeScatter:
216 233 break;
217 234 case QChartSeries::SeriesTypePie:
218 235 break;
219 236 case QChartSeries::SeriesTypeBar:
220 237 qDebug() << "setCurrentSeries (bar)";
221 238 break;
222 239 default:
223 240 Q_ASSERT(false);
224 241 break;
225 242 }
226 243 }
227 244
228 245 void MainWidget::testDataChanged(int itemIndex)
229 246 {
230 247 qDebug() << "testDataChanged: " << itemIndex;
231 248
232 249 // switch (itemIndex) {
233 250 // case 0: {
234 251 // QList<QChartDataPoint> data;
235 252 // for (int x = 0; x < 20; x++) {
236 253 // data.append(QChartDataPoint() << x << x / 2);
237 254 // }
238 255 // m_chartWidget->setData(data);
239 256 // break;
240 257 // }
241 258 // case 1: {
242 259 // QList<QChartDataPoint> data;
243 260 // for (int x = 0; x < 100; x++) {
244 261 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
245 262 // }
246 263 // m_chartWidget->setData(data);
247 264 // break;
248 265 // }
249 266 // case 2: {
250 267 // QList<QChartDataPoint> data;
251 268 // for (int x = 0; x < 1000; x++) {
252 269 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
253 270 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
254 271 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
255 272 // }
256 273 // m_chartWidget->setData(data);
257 274 // break;
258 275 // }
259 276 // default:
260 277 // break;
261 278 // }
262 279 }
263 280
264 281 void MainWidget::backgroundChanged(int itemIndex)
265 282 {
266 283 qDebug() << "backgroundChanged: " << itemIndex;
267 284 }
268 285
269 286 void MainWidget::autoScaleChanged(int value)
270 287 {
271 288 if (value) {
272 289 // TODO: enable auto scaling
273 290 } else {
274 291 // TODO: set scaling manually (and disable auto scaling)
275 292 }
276 293
277 294 m_xMinSpin->setEnabled(!value);
278 295 m_xMaxSpin->setEnabled(!value);
279 296 m_yMinSpin->setEnabled(!value);
280 297 m_yMaxSpin->setEnabled(!value);
281 298 }
282 299
283 300 void MainWidget::xMinChanged(int value)
284 301 {
285 302 qDebug() << "xMinChanged: " << value;
286 303 }
287 304
288 305 void MainWidget::xMaxChanged(int value)
289 306 {
290 307 qDebug() << "xMaxChanged: " << value;
291 308 }
292 309
293 310 void MainWidget::yMinChanged(int value)
294 311 {
295 312 qDebug() << "yMinChanged: " << value;
296 313 }
297 314
298 315 void MainWidget::yMaxChanged(int value)
299 316 {
300 317 qDebug() << "yMaxChanged: " << value;
301 318 }
302 319
303 320 void MainWidget::changeChartTheme(int themeIndex)
304 321 {
305 322 qDebug() << "changeChartTheme: " << themeIndex;
306 323 // m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
307 324 }
308 325
309 326 void MainWidget::setPieSizeFactor(double size)
310 327 {
311 328 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
312 329 Q_ASSERT(pie);
313 330 pie->setSizeFactor(qreal(size));
314 331 }
General Comments 0
You need to be logged in to leave comments. Login now