##// END OF EJS Templates
Refactored the test app
Tero Ahola -
r110:d11ac5a26e2a
parent child
Show More
@@ -1,44 +1,42
1 1 integrated_build:{
2 2 message('Internal build within charts core source tree')
3 3 INCLUDEPATH += $$CHART_BUILD_HEADER_DIR
4 4 LIBS += -L $$CHART_BUILD_LIB_DIR -Wl,-rpath,$$CHART_BUILD_LIB_DIR
5 5 DESTDIR = $$CHART_BUILD_BIN_DIR
6 6 CONFIG(debug, debug|release) {
7 LIBS += -lQtCommercialChartd
8
9 #this is ugly hack to work around missing rpath, it simply copies lib
10 win32:{
11 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
12 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\\QtCommercialChartd.dll $$CHART_BUILD_BIN_DIR
13 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChartd.dll
14 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
15 QMAKE_EXTRA_TARGETS +=copylib
16 }
17
7 LIBS += -lQtCommercialChartd
8 #this is ugly hack to work around missing rpath, it simply copies lib
9 win32:{
10 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
11 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\\QtCommercialChartd.dll $$CHART_BUILD_BIN_DIR
12 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChartd.dll
13 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
14 QMAKE_EXTRA_TARGETS +=copylib
15 }
18 16 } else {
19 17 LIBS += -lQtCommercialChart
20 18
21 19 #this is ugly hack to work around missing rpath, it simply copies lib
22 20 win32: {
23 21 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChart
24 22 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\\QtCommercialChart.dll $$CHART_BUILD_BIN_DIR
25 23 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChart.dll
26 24 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChart
27 25 QMAKE_EXTRA_TARGETS +=copylib
28 26 }
29 27 }
30 28
31 29 mac: {
32 30 # This is a hack to make binaries to use the internal version of the QtCommercial Charts library on OSX
33 31 QMAKE_POST_LINK += install_name_tool -change "libQtCommercialChartd.1.dylib" "@rpath/libQtCommercialChartd.dylib" $$CHART_BUILD_BIN_DIR/chartwidgettest.app/Contents/MacOS/chartwidgettest
34 32 QMAKE_POST_LINK += && install_name_tool -change "libQtCommercialChartd.1.dylib" "@rpath/libQtCommercialChartd.dylib" $$CHART_BUILD_BIN_DIR/barchart.app/Contents/MacOS/barchart
35 33 QMAKE_POST_LINK += && install_name_tool -change "libQtCommercialChartd.1.dylib" "@rpath/libQtCommercialChartd.dylib" $$CHART_BUILD_BIN_DIR/colorlineChart.app/Contents/MacOS/colorlineChart
36 34 QMAKE_POST_LINK += && install_name_tool -change "libQtCommercialChartd.1.dylib" "@rpath/libQtCommercialChartd.dylib" $$CHART_BUILD_BIN_DIR/lineChart.app/Contents/MacOS/lineChart
37 35 QMAKE_POST_LINK += && install_name_tool -change "libQtCommercialChartd.1.dylib" "@rpath/libQtCommercialChartd.dylib" $$CHART_BUILD_BIN_DIR/percentbarchart.app/Contents/MacOS/percentbarchart
38 36 QMAKE_POST_LINK += && install_name_tool -change "libQtCommercialChartd.1.dylib" "@rpath/libQtCommercialChartd.dylib" $$CHART_BUILD_BIN_DIR/stackedbarchart.app/Contents/MacOS/stackedbarchart
39 37 QMAKE_POST_LINK += && install_name_tool -change "libQtCommercialChartd.1.dylib" "@rpath/libQtCommercialChartd.dylib" $$CHART_BUILD_BIN_DIR/zoomLineChart.app/Contents/MacOS/zoomLineChart
40 38 }
41 39
42 40 } else {
43 41 CONFIG+=qtcommercialchart
44 42 }
@@ -1,84 +1,84
1 1 #ifndef CHARTTHEME_H
2 2 #define CHARTTHEME_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include <QObject>
6 6 #include <QSharedData>
7 7 #include <QColor>
8 8 #include <QLinearGradient>
9 9 #include <QPen>
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 class ChartTheme;
14 14
15 15 class ChartThemeObserver
16 16 {
17 17 public:
18 18 virtual void themeChanged(ChartTheme *theme) = 0;
19 19 };
20 20
21 21 /*!
22 22 * The theme specific settings for the appearance of a series. TODO: These can be overridden by setting
23 23 * custom settings to a QChartSeries object.
24 24 */
25 25 struct SeriesTheme {
26 26 public:
27 27 SeriesTheme() :
28 28 linePen(QPen()),
29 29 markerPen(QPen()) {}
30 30 SeriesTheme(QColor lineColor, qreal lineWidth/*, QPen marker*/) :
31 31 linePen(QPen(QBrush(lineColor), lineWidth)),
32 32 markerPen(linePen) {}
33 33
34 34 //const QBrush & brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJo
35 35 // TODO:
36 36 //QColor lineColor;
37 37 QPen linePen;
38 38 //QBrush lineBrush;
39 39 QPen markerPen;
40 40 //QBrush markerBrush;
41 41 };
42 42
43 43 /*!
44 44 * Explicitly shared data class for the themes.
45 45 */
46 46 class ChartThemeData : public QSharedData
47 47 {
48 48 public:
49 49 ChartThemeData() : m_currentTheme(0) {}
50 50 ~ChartThemeData() {}
51 51
52 52 public:
53 53 void setTheme(int theme);
54 54
55 55 public:
56 56 int m_currentTheme;
57 57 QList<ChartThemeObserver *> m_observers;
58 58 QColor m_gradientStartColor;
59 59 QColor m_gradientEndColor;
60 60 QList<SeriesTheme> m_seriesThemes;
61 61 int m_seriesIndex;
62 62 };
63 63
64 64 class ChartTheme : public QObject
65 65 {
66 66 Q_OBJECT
67 67 public:
68 68 explicit ChartTheme(QObject *parent = 0);
69 explicit ChartTheme(const ChartTheme &other) : d(other.d) {}
69 explicit ChartTheme(const ChartTheme &other, QObject *parent = 0) : QObject(parent), d(other.d) {}
70 70 void operator =(const ChartTheme &other) { d = other.d; }
71 71
72 72 void setTheme(int theme);
73 73 SeriesTheme themeForSeries();
74 74 void addObserver(ChartThemeObserver *o) { d->m_observers << o; }
75 75
76 76 public:
77 77 // All the graphical elements of a QChart share the same theme settings
78 78 // so let's use explicitly shared data
79 79 QExplicitlySharedDataPointer<ChartThemeData> d;
80 80 };
81 81
82 82 QTCOMMERCIALCHART_END_NAMESPACE
83 83
84 84 #endif // CHARTTHEME_H
@@ -1,74 +1,74
1 1 #include "pieslice.h"
2 2 #include <QPainter>
3 3 #include <QDebug>
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect)
8 8 : m_color(color),
9 9 m_startAngle(startAngle),
10 10 m_span(span),
11 11 m_rect(rect)
12 12 {
13 13 setAcceptHoverEvents(true);
14 14 }
15 15
16 16 PieSlice::~PieSlice()
17 17 {
18 18 }
19 19
20 20 QRectF PieSlice::boundingRect() const
21 21 {
22 22 return m_rect;
23 23 }
24 24
25 25 QPainterPath PieSlice::shape() const
26 26 {
27 27 qreal angle = (-m_startAngle) + (90);
28 28 qreal span = -m_span;
29 29
30 30 QPainterPath path;
31 31 path.moveTo(boundingRect().center());
32 32 path.arcTo(boundingRect(), angle, span);
33 33
34 34 // TODO: draw the shape so that it might have a hole in the center
35 35 // - Sin & Cos will be needed to find inner/outer arc endpoints
36 36
37 37 // dx, dy are offsets from the center
38 38 //qreal l = boundingRect().height();
39 39 //qreal dx = qSin(angle*(M_PI/180)) * l;
40 40 //qreal dy = qCos(angle*(M_PI/180)) * l;
41 41
42 42 // TODO: exploded slice?
43 43
44 44 return path;
45 45 }
46 46
47 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
48 48 {
49 49 painter->setRenderHint(QPainter::Antialiasing);
50 50 // TODO: how to map theme settings to a pie slice? Now we
51 51 //painter->setPen(m_theme.linePen);
52 52 // TODO:
53 53 painter->setBrush(m_theme.linePen.color());
54 54
55 55 // From Qt docs:
56 56 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
57 57 // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction.
58 58 // Zero degrees is at the 3 o'clock position.
59 59 //
60 60 // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360.
61 61 //qreal angle = (-m_startAngle*16) + (90*16);
62 62 //qreal span = -m_span*16;
63 63 //painter->drawPie(boundingRect(), angle, span);
64 64
65 65 painter->drawPath(shape());
66 66 }
67 67
68 68 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
69 69 {
70 70 QGraphicsItem::hoverEnterEvent(event);
71 71 qDebug() << "hover" << m_color << m_startAngle << m_span;
72 72 }
73 73
74 74 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,412 +1,412
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 "qpieseries_p.h"
7 7 #include "qxychartseries.h"
8 8 #include "qchartaxis.h"
9 9 #include "barchartseries.h"
10 10 #include "bargroup.h"
11 11 #include "stackedbarchartseries.h"
12 12 #include "stackedbargroup.h"
13 13 #include "percentbarchartseries.h"
14 14 #include "percentbargroup.h"
15 15 #include "charttheme_p.h"
16 16 #include "chartitem_p.h"
17 17
18 18 #include "xylinechartitem_p.h"
19 19 #include "plotdomain_p.h"
20 20 #include "axisitem_p.h"
21 21 #include <QGraphicsScene>
22 22 #include <QDebug>
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
27 27 m_backgroundItem(0),
28 28 m_titleItem(0),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)),
30 30 m_plotDataIndex(0),
31 31 m_marginSize(0),
32 m_chartTheme(new ChartTheme())
32 m_chartTheme(new ChartTheme(this))
33 33 {
34 34 // TODO: the default theme?
35 35 setTheme(QChart::ChartThemeDefault);
36 36
37 37 PlotDomain domain;
38 38 m_plotDomainList << domain;
39 39 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
40 40 m_chartItems << m_axisXItem;
41 41 m_chartItems << m_axisYItem.at(0);
42 42 }
43 43
44 44 QChart::~QChart(){}
45 45
46 46 QRectF QChart::boundingRect() const
47 47 {
48 48 return m_rect;
49 49 }
50 50
51 51 void QChart::addSeries(QChartSeries* series)
52 52 {
53 53 // TODO: we should check the series not already added
54 54
55 55 m_chartSeries << series;
56 56
57 57 switch(series->type())
58 58 {
59 59 case QChartSeries::SeriesTypeLine: {
60 60
61 61 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
62 62 m_plotDataIndex = 0 ;
63 63 m_plotDomainList.resize(1);
64 64
65 65 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
66 66
67 67 for (int i = 0 ; i < xyseries->count() ; i++) {
68 68 qreal x = xyseries->x(i);
69 69 qreal y = xyseries->y(i);
70 70 domain.m_minX = qMin(domain.m_minX,x);
71 71 domain.m_minY = qMin(domain.m_minY,y);
72 72 domain.m_maxX = qMax(domain.m_maxX,x);
73 73 domain.m_maxY = qMax(domain.m_maxY,y);
74 74 }
75 75
76 76 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
77 77
78 78 m_chartItems << item;
79 79 // TODO:
80 80 //m_chartTheme->addObserver(xyseries);
81 81
82 82 break;
83 83 }
84 84 case QChartSeries::SeriesTypeBar: {
85 85
86 86 qDebug() << "barSeries added";
87 87 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
88 88 BarGroup* barGroup = new BarGroup(*barSeries,this);
89 89
90 90 // Add some fugly colors for 5 fist series...
91 91 barGroup->addColor(QColor(255,0,0,128));
92 92 barGroup->addColor(QColor(255,255,0,128));
93 93 barGroup->addColor(QColor(0,255,0,128));
94 94 barGroup->addColor(QColor(0,0,255,128));
95 95 barGroup->addColor(QColor(255,128,0,128));
96 96
97 97 m_chartItems << barGroup;
98 98 childItems().append(barGroup);
99 99
100 100 // TODO: setting of domain should this be somewhere else.
101 101 m_plotDataIndex = 0 ;
102 102 m_plotDomainList.resize(1);
103 103
104 104 qreal x = barSeries->countColumns();
105 105 qreal y = barSeries->max();
106 106
107 107 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
108 108
109 109 domain.m_minX = qMin(domain.m_minX,x);
110 110 domain.m_minY = qMin(domain.m_minY,y);
111 111 domain.m_maxX = qMax(domain.m_maxX,x);
112 112 domain.m_maxY = qMax(domain.m_maxY,y);
113 113 break;
114 114 }
115 115 case QChartSeries::SeriesTypeStackedBar: {
116 116
117 117 qDebug() << "barSeries added";
118 118 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
119 119 StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this);
120 120
121 121 // Add some fugly colors for 5 fist series...
122 122 stackedBarGroup->addColor(QColor(255,0,0,128));
123 123 stackedBarGroup->addColor(QColor(255,255,0,128));
124 124 stackedBarGroup->addColor(QColor(0,255,0,128));
125 125 stackedBarGroup->addColor(QColor(0,0,255,128));
126 126 stackedBarGroup->addColor(QColor(255,128,0,128));
127 127
128 128 m_chartItems << stackedBarGroup;
129 129 childItems().append(stackedBarGroup);
130 130
131 131 // TODO: setting of domain should this be somewhere else.
132 132 m_plotDataIndex = 0 ;
133 133 m_plotDomainList.resize(1);
134 134
135 135 qreal x = stackedBarSeries->countColumns();
136 136 qreal y = stackedBarSeries->maxColumnSum();
137 137
138 138 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
139 139
140 140 domain.m_minX = qMin(domain.m_minX,x);
141 141 domain.m_minY = qMin(domain.m_minY,y);
142 142 domain.m_maxX = qMax(domain.m_maxX,x);
143 143 domain.m_maxY = qMax(domain.m_maxY,y);
144 144 break;
145 145 }
146 146 case QChartSeries::SeriesTypePercentBar: {
147 147
148 148 qDebug() << "barSeries added";
149 149 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
150 150 PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this);
151 151
152 152 // Add some fugly colors for 5 fist series...
153 153 percentBarGroup->addColor(QColor(255,0,0,128));
154 154 percentBarGroup->addColor(QColor(255,255,0,128));
155 155 percentBarGroup->addColor(QColor(0,255,0,128));
156 156 percentBarGroup->addColor(QColor(0,0,255,128));
157 157 percentBarGroup->addColor(QColor(255,128,0,128));
158 158
159 159 m_chartItems << percentBarGroup;
160 160 childItems().append(percentBarGroup);
161 161
162 162 // TODO: setting of domain should this be somewhere else.
163 163 m_plotDataIndex = 0 ;
164 164 m_plotDomainList.resize(1);
165 165
166 166 qreal x = percentBarSeries->countColumns();
167 167
168 168 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
169 169
170 170 domain.m_minX = qMin(domain.m_minX,x);
171 171 domain.m_minY = 0;
172 172 domain.m_maxX = qMax(domain.m_maxX,x);
173 173 domain.m_maxY = 100;
174 174 break;
175 175 }
176 176 case QChartSeries::SeriesTypeScatter: {
177 177 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
178 178 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
179 179 scatterSeries->d->setParentItem(this);
180 180 m_chartItems << scatterSeries->d;
181 181 m_chartTheme->addObserver(scatterSeries->d);
182 182 break;
183 183 }
184 184 case QChartSeries::SeriesTypePie: {
185 185 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
186 186 pieSeries->d->setParentItem(this);
187 187 m_chartItems << pieSeries->d;
188 188 pieSeries->d->m_chartTheme = m_chartTheme;
189 189 m_chartTheme->addObserver(pieSeries->d);
190 190 break;
191 191 }
192 192 }
193 193
194 194 // Update all the items to match the new visible area of the chart
195 195 foreach(ChartItem* i, m_chartItems)
196 196 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
197 197 }
198 198
199 199 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
200 200 {
201 201 // TODO: support also other types; not only scatter and pie
202 202
203 203 QChartSeries *series(0);
204 204
205 205 switch (type) {
206 206 case QChartSeries::SeriesTypeLine: {
207 207 series = QXYChartSeries::create();
208 208 break;
209 209 }
210 210 case QChartSeries::SeriesTypeBar: {
211 211 series = new BarChartSeries(this);
212 212 break;
213 213 }
214 214 case QChartSeries::SeriesTypeStackedBar: {
215 215 series = new StackedBarChartSeries(this);
216 216 break;
217 217 }
218 218 case QChartSeries::SeriesTypePercentBar: {
219 219 series = new PercentBarChartSeries(this);
220 220 break;
221 221 }
222 222 case QChartSeries::SeriesTypeScatter: {
223 223 series = new QScatterSeries(this);
224 224 break;
225 225 }
226 226 case QChartSeries::SeriesTypePie: {
227 227 series = new QPieSeries(this);
228 228 break;
229 229 }
230 230 default:
231 231 Q_ASSERT(false);
232 232 break;
233 233 }
234 234
235 235 addSeries(series);
236 236 return series;
237 237 }
238 238
239 239 void QChart::setSize(const QSize& size)
240 240 {
241 241 m_rect = QRect(QPoint(0,0),size);
242 242 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
243 243
244 244 // recalculate title position
245 245 if (m_titleItem) {
246 246 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
247 247 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
248 248 }
249 249
250 250 //recalculate background gradient
251 251 if (m_backgroundItem) {
252 252 m_backgroundItem->setRect(rect);
253 253 if (m_bacgroundOrinetation == HorizonatlGradientOrientation)
254 254 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0);
255 255 else
256 256 m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height());
257 257 m_backgroundItem->setBrush(m_backgroundGradient);
258 258 }
259 259
260 260 // resize and reposition childs
261 261 foreach (ChartItem *item, m_chartItems) {
262 262 item->setPos(rect.topLeft());
263 263 item->setSize(rect.size());
264 264 }
265 265
266 266 update();
267 267 }
268 268
269 269 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
270 270 {
271 271
272 272 if(!m_backgroundItem){
273 273 m_backgroundItem = new QGraphicsRectItem(this);
274 274 m_backgroundItem->setZValue(-1);
275 275 }
276 276
277 277 m_bacgroundOrinetation = orientation;
278 278 m_backgroundGradient.setColorAt(0.0, startColor);
279 279 m_backgroundGradient.setColorAt(1.0, endColor);
280 280 m_backgroundGradient.setStart(0,0);
281 281
282 282 if(orientation == VerticalGradientOrientation){
283 283 m_backgroundGradient.setFinalStop(0,m_rect.height());
284 284 }else{
285 285 m_backgroundGradient.setFinalStop(m_rect.width(),0);
286 286 }
287 287
288 288 m_backgroundItem->setBrush(m_backgroundGradient);
289 289 m_backgroundItem->setPen(Qt::NoPen);
290 290 m_backgroundItem->update();
291 291 }
292 292
293 293 void QChart::setTitle(const QString& title,const QFont& font)
294 294 {
295 295 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
296 296 m_titleItem->setPlainText(title);
297 297 m_titleItem->setFont(font);
298 298 }
299 299
300 300 int QChart::margin() const
301 301 {
302 302 return m_marginSize;
303 303 }
304 304
305 305 void QChart::setMargin(int margin)
306 306 {
307 307 m_marginSize = margin;
308 308 }
309 309
310 310 void QChart::setTheme(QChart::ChartThemeId theme)
311 311 {
312 312 m_chartTheme->setTheme(theme);
313 313 setBackground(m_chartTheme->d->m_gradientStartColor,
314 314 m_chartTheme->d->m_gradientEndColor,
315 315 m_bacgroundOrinetation);
316 316
317 317 // TODO: Move the controlling of the series presentations into private implementation of the
318 318 // series instead of QChart controlling themes for each
319 319 // In other words, the following should be used when creating xy series:
320 320 // m_chartTheme->addObserver(xyseries)
321 321 foreach (QChartSeries *series, m_chartSeries) {
322 322 if (series->type() == QChartSeries::SeriesTypeLine) {
323 323 QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series);
324 324 SeriesTheme seriesTheme = m_chartTheme->themeForSeries();
325 325 xyseries->setPen(seriesTheme.linePen);
326 326 }
327 327 }
328 328
329 329 update();
330 330 }
331 331
332 332 void QChart::zoomInToRect(const QRect& rectangle)
333 333 {
334 334
335 335 if(!rectangle.isValid()) return;
336 336
337 337 qreal margin = this->margin();
338 338
339 339 QRect rect = rectangle.normalized();
340 340 rect.translate(-margin, -margin);
341 341
342 342 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
343 343
344 344 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
345 345
346 346 m_plotDomainList.resize(m_plotDataIndex + 1);
347 347 m_plotDomainList<<domain;
348 348 m_plotDataIndex++;
349 349
350 350 foreach (ChartItem* item, m_chartItems)
351 351 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
352 352 update();
353 353 }
354 354
355 355 void QChart::zoomIn()
356 356 {
357 357 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
358 358 m_plotDataIndex++;
359 359 foreach (ChartItem* item, m_chartItems)
360 360 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
361 361 update();
362 362 } else {
363 363 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
364 364 rect.setWidth(rect.width()/2);
365 365 rect.setHeight(rect.height()/2);
366 366 rect.moveCenter(m_rect.center());
367 367 zoomInToRect(rect);
368 368 }
369 369 }
370 370
371 371 void QChart::zoomOut()
372 372 {
373 373 if (m_plotDataIndex > 0) {
374 374 m_plotDataIndex--;
375 375 foreach (ChartItem* item, m_chartItems)
376 376 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
377 377 update();
378 378 }
379 379 }
380 380
381 381 void QChart::zoomReset()
382 382 {
383 383 if (m_plotDataIndex > 0) {
384 384 m_plotDataIndex = 0;
385 385 foreach (ChartItem* item, m_chartItems)
386 386 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
387 387 update();
388 388 }
389 389 }
390 390
391 391 void QChart::setAxisX(const QChartAxis& axis)
392 392 {
393 393 setAxis(m_axisXItem,axis);
394 394 }
395 395 void QChart::setAxisY(const QChartAxis& axis)
396 396 {
397 397 setAxis(m_axisYItem.at(0),axis);
398 398 }
399 399
400 400 void QChart::setAxisY(const QList<QChartAxis>& axis)
401 401 {
402 402 //TODO not implemented
403 403 }
404 404
405 405 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
406 406 {
407 407 item->setVisible(axis.isAxisVisible());
408 408 }
409 409
410 410 #include "moc_qchart.cpp"
411 411
412 412 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,99 +1,99
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsObject>
7 7 #include <QLinearGradient>
8 8 #include <QFont>
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 class AxisItem;
13 13 class QChartSeries;
14 14 class PlotDomain;
15 15 class BarGroup;
16 16 class QChartAxis;
17 17 class ChartTheme;
18 18 class ChartItem;
19 19
20 20 // TODO: We don't need to have QChart tied to QGraphicsItem:
21 21 //class QTCOMMERCIALCHART_EXPORT QChart
22 22 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
23 23 // public: QChartGraphicsItem(QChart &chart);
24 24
25 25 /*!
26 26 * TODO: define the responsibilities
27 27 */
28 28 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
29 29 {
30 30 Q_OBJECT
31 31 public:
32 32 enum GradientOrientation {
33 33 HorizonatlGradientOrientation,
34 34 VerticalGradientOrientation
35 35 };
36 36 enum ChartThemeId {
37 37 ChartThemeInvalid = -1,
38 38 /*! The default theme follows the GUI style of the Operating System */
39 39 ChartThemeDefault,
40 40 ChartThemeVanilla,
41 41 ChartThemeIcy,
42 42 ChartThemeGrayscale,
43 43 //ChartThemeScientific,
44 44 ChartThemeUnnamed1
45 45 };
46 46
47 47 public:
48 48 QChart(QGraphicsObject* parent = 0);
49 49 ~QChart();
50 50
51 51 //from QGraphicsItem
52 52 QRectF boundingRect() const;
53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
53 void paint(QPainter */*painter*/, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) {}
54 54
55 55 void addSeries(QChartSeries* series);
56 56 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
57 57 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
58 58 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
59 59
60 60 void setSize(const QSize& size);
61 61 void setMargin(int margin);
62 62 int margin() const;
63 63 void setTheme(QChart::ChartThemeId theme);
64 64
65 65 void setTitle(const QString& title,const QFont& font = QFont());
66 66 void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation);
67 67
68 68 void zoomInToRect(const QRect& rectangle);
69 69 void zoomIn();
70 70 void zoomOut();
71 71 void zoomReset();
72 72
73 73 void setAxisX(const QChartAxis& axis);
74 74 void setAxisY(const QChartAxis& axis);
75 75 void setAxisY(const QList<QChartAxis>& axis);
76 76
77 77 private:
78 78 void setAxis(AxisItem *item, const QChartAxis& axis);
79 79
80 80 private:
81 81 Q_DISABLE_COPY(QChart)
82 82 QGraphicsRectItem* m_backgroundItem;
83 83 QLinearGradient m_backgroundGradient;
84 84 GradientOrientation m_bacgroundOrinetation;
85 85 QGraphicsTextItem* m_titleItem;
86 86 AxisItem* m_axisXItem;
87 87 QList<AxisItem*> m_axisYItem;
88 88 QRect m_rect;
89 89 QList<QChartSeries *> m_chartSeries;
90 90 QList<ChartItem *> m_chartItems;
91 91 QVector<PlotDomain> m_plotDomainList;
92 92 int m_plotDataIndex;
93 93 int m_marginSize;
94 94 ChartTheme *m_chartTheme;
95 95 };
96 96
97 97 QTCOMMERCIALCHART_END_NAMESPACE
98 98
99 99 #endif
@@ -1,49 +1,49
1 1 #ifndef QCHARTSERIES_H
2 2 #define QCHARTSERIES_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include <QObject>
6 6 #include <QAbstractItemModel>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QTCOMMERCIALCHART_EXPORT QChartSeries : public QObject
11 11 {
12 12 Q_OBJECT
13 13 public:
14 14 enum QChartSeriesType {
15 15 SeriesTypeLine = 0,
16 16 // SeriesTypeArea,
17 17 SeriesTypeBar,
18 18 SeriesTypeStackedBar,
19 19 SeriesTypePercentBar,
20 20 SeriesTypePie,
21 21 SeriesTypeScatter
22 22 // SeriesTypeSpline
23 23 };
24 24
25 25 protected:
26 26 QChartSeries(QObject *parent = 0):QObject(parent){};
27 27
28 28 public:
29 29 virtual ~QChartSeries(){};
30 30
31 31 // Factory method
32 32 static QChartSeries* create(QChartSeriesType type, QObject* parent = 0 );
33 33
34 34 // Pure virtual
35 35 virtual QChartSeriesType type() const = 0;
36 36
37 virtual bool setData(QList<int> data) { return false; }
38 virtual bool setData(QList<qreal> data) { return false; }
39 virtual bool setData(QList<qreal> x, QList<qreal> y){ return false; }
37 virtual bool setData(QList<int> /*data*/) { return false; }
38 virtual bool setData(QList<qreal> /*data*/) { return false; }
39 virtual bool setData(QList<qreal> /*x*/, QList<qreal> /*y*/){ return false; }
40 40
41 41 // Prototype for data model. TODO: remove the other setData methods and use something like this for now?
42 virtual bool setData(QAbstractItemModel* model) { return false; }
42 virtual bool setData(QAbstractItemModel* /*model*/) { return false; }
43 43
44 44 };
45 45
46 46 QTCOMMERCIALCHART_END_NAMESPACE
47 47
48 48 #endif
49 49
@@ -1,125 +1,125
1 1 #include "qscatterseries.h"
2 2 #include "qscatterseries_p.h"
3 3 #include "qchart.h"
4 4 #include <QPainter>
5 5 #include <QGraphicsScene>
6 6 #include <QDebug>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 //#define QSeriesData QList<qreal>
11 11
12 12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
13 13 ChartItem(parent),
14 14 m_markerColor(QColor()),
15 15 m_visibleChartArea()
16 16 {
17 17 }
18 18
19 19 void QScatterSeriesPrivate::resize(QRectF rect)
20 20 {
21 21 qreal scalex = rect.width() / m_visibleChartArea.spanX();
22 22 qreal scaley = rect.height() / m_visibleChartArea.spanY();
23 23
24 24 m_scenex.clear();
25 25 m_sceney.clear();
26 26
27 27 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
28 28 foreach(qreal x, m_x)
29 29 m_scenex.append(rect.left() + x * scalex - m_visibleChartArea.m_minX * scalex);
30 30
31 31 foreach(qreal y, m_y)
32 32 m_sceney.append(rect.bottom() - y * scaley - m_visibleChartArea.m_minY * scaley);
33 33 }
34 34
35 35 void QScatterSeriesPrivate::setSize(const QSize &size)
36 36 {
37 37 QGraphicsItem *parent = this->parentItem();
38 38 if (parent)
39 39 resize(QRectF(parent->pos(), size));
40 40 }
41 41
42 42 void QScatterSeriesPrivate::themeChanged(ChartTheme *theme)
43 43 {
44 44 m_theme = theme->themeForSeries();
45 45 }
46 46
47 47 void QScatterSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain)
48 48 {
49 49 m_visibleChartArea = plotDomain;
50 50 resize(parentItem()->boundingRect());
51 51 }
52 52
53 53 QRectF QScatterSeriesPrivate::boundingRect() const
54 54 {
55 55 return QRectF(0, 0, 55, 100);
56 56 }
57 57
58 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
58 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
59 59 {
60 60 // TODO: The opacity should be user definable?
61 61 //brush.setColor(QColor(255, 82, 0, 100));
62 62 if (m_markerColor.isValid()) {
63 63 QPen pen = painter->pen();
64 64 QBrush brush = pen.brush();
65 65 brush.setColor(m_markerColor);
66 66 pen.setBrush(brush);
67 67 pen.setWidth(4);
68 68 painter->setPen(pen);
69 69 }
70 70 else
71 71 painter->setPen(m_theme.markerPen);
72 72 // brush.setColor(m_theme..lineColor);
73 73
74 74 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
75 75 // event right after construction or maybe given a size during initialization
76 76 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
77 77 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
78 78 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
79 79 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
80 80 }
81 81 }
82 82
83 83 QScatterSeries::QScatterSeries(QObject *parent) :
84 84 QChartSeries(parent),
85 85 d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent)))
86 86 {
87 87 }
88 88
89 89 bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y)
90 90 {
91 91 // TODO: validate data
92 92 d->m_x = x;
93 93 d->m_y = y;
94 94 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
95 95 Q_ASSERT(parentItem);
96 96 // d->setPos(parentItem->pos());
97 97 // d->setSize(parentItem->boundingRect().size().toSize());
98 98 d->resize(parentItem->boundingRect());
99 99 return true;
100 100 }
101 101
102 102 void QScatterSeries::setMarkerColor(QColor color)
103 103 {
104 104 d->m_markerColor = color;
105 105 }
106 106
107 107 QColor QScatterSeries::markerColor()
108 108 {
109 109 return d->m_markerColor;
110 110 }
111 111
112 112 // TODO:
113 113 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
114 114 //{
115 115 // d->rescale(xscale, yscale);
116 116 //}
117 117
118 118 QScatterSeries::~QScatterSeries()
119 119 {
120 120 delete d;
121 121 }
122 122
123 123 #include "moc_qscatterseries.cpp"
124 124
125 125 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,366 +1,377
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 18 #include <QStandardItemModel>
19 19
20 20
21 21 QTCOMMERCIALCHART_USE_NAMESPACE
22 22
23 23 MainWidget::MainWidget(QWidget *parent) :
24 24 QWidget(parent)
25 25 {
26 26 m_chartWidget = new QChartWidget(this);
27 27
28 // GridLayout for the controls for configuring the chart widget
29 QGridLayout *grid = new QGridLayout();
30 QGridLayout *mainLayout = new QGridLayout();
28 31 QPushButton *addSeriesButton = new QPushButton("Add series");
29 32 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
33 grid->addWidget(addSeriesButton, 0, 1);
34 initBackroundCombo(grid);
35 initScaleControls(grid);
36 initThemeCombo(grid);
37 QCheckBox *zoomCheckBox = new QCheckBox("Zoom enabled");
38 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
39 zoomCheckBox->setChecked(true);
40 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
41 // add row with empty label to make all the other rows static
42 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
43 grid->setRowStretch(grid->rowCount() - 1, 1);
44 mainLayout->addLayout(grid, 0, 0);
45
46 // Init series type specific controls
47 initPieControls();
48 mainLayout->addLayout(m_pieLayout, 2, 0);
49 // Scatter series specific settings
50 // m_scatterLayout = new QGridLayout();
51 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
52 // m_scatterLayout->setEnabled(false);
53 // mainLayout->addLayout(m_scatterLayout, 1, 0);
54
55 // Add layouts and the chart widget to the main layout
56 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
57 setLayout(mainLayout);
58
59 // force an update to test data
60 testDataChanged(0);
61 }
30 62
31 // Chart background
63 // Combo box for selecting the chart's background
64 void MainWidget::initBackroundCombo(QGridLayout *grid)
65 {
32 66 QComboBox *backgroundCombo = new QComboBox(this);
33 67 backgroundCombo->addItem("Color");
34 68 backgroundCombo->addItem("Gradient");
35 69 backgroundCombo->addItem("Image");
36 70 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
37 71 this, SLOT(backgroundChanged(int)));
38 72
39 // Axis
40 // TODO: multiple axes?
73 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
74 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
75 }
76
77 // Scale related controls (auto-scale vs. manual min-max values)
78 void MainWidget::initScaleControls(QGridLayout *grid)
79 {
41 80 m_autoScaleCheck = new QCheckBox("Automatic scaling");
42 81 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
43 82 // Allow setting also non-sense values (like -2147483648 and 2147483647)
44 83 m_xMinSpin = new QSpinBox();
45 84 m_xMinSpin->setMinimum(INT_MIN);
46 85 m_xMinSpin->setMaximum(INT_MAX);
47 86 m_xMinSpin->setValue(0);
48 87 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
49 88 m_xMaxSpin = new QSpinBox();
50 89 m_xMaxSpin->setMinimum(INT_MIN);
51 90 m_xMaxSpin->setMaximum(INT_MAX);
52 91 m_xMaxSpin->setValue(10);
53 92 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
54 93 m_yMinSpin = new QSpinBox();
55 94 m_yMinSpin->setMinimum(INT_MIN);
56 95 m_yMinSpin->setMaximum(INT_MAX);
57 96 m_yMinSpin->setValue(0);
58 97 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
59 98 m_yMaxSpin = new QSpinBox();
60 99 m_yMaxSpin->setMinimum(INT_MIN);
61 100 m_yMaxSpin->setMaximum(INT_MAX);
62 101 m_yMaxSpin->setValue(10);
63 102 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
64 103
104 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
105 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
106 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
107 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
108 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
109 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
110 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
111 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
112 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
113
114 m_autoScaleCheck->setChecked(true);
115 }
116
117 // Combo box for selecting theme
118 void MainWidget::initThemeCombo(QGridLayout *grid)
119 {
65 120 QComboBox *chartTheme = new QComboBox();
66 121 chartTheme->addItem("Default");
67 122 chartTheme->addItem("Vanilla");
68 123 chartTheme->addItem("Icy");
69 124 chartTheme->addItem("Grayscale");
70 125 chartTheme->addItem("Unnamed1");
71 126 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
72 127 this, SLOT(changeChartTheme(int)));
73 // connect(chartTheme, SIGNAL(currentIndexChanged(int)),
74 // m_signalMapper, SLOT(map()));
75 // m_signalMapper->setMapping(chartTheme, (QChart::ChartThemeId)chartTheme->currentIndex());
76 // connect(m_signalMapper, SIGNAL(mapped(QChart::ChartThemeId)),
77 // m_chartWidget, SLOT(setTheme(QChart::ChartThemeId)));
78
79 QCheckBox *zoomCheckBox = new QCheckBox("Zoom enabled");
80 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
81 zoomCheckBox->setChecked(true);
82
83 QGridLayout *grid = new QGridLayout();
84 QGridLayout *mainLayout = new QGridLayout();
85 grid->addWidget(addSeriesButton, 0, 1);
86 grid->addWidget(new QLabel("Background:"), 2, 0);
87 grid->addWidget(backgroundCombo, 2, 1);
88 grid->addWidget(m_autoScaleCheck, 3, 0);
89 grid->addWidget(new QLabel("x min:"), 4, 0);
90 grid->addWidget(m_xMinSpin, 4, 1);
91 grid->addWidget(new QLabel("x max:"), 5, 0);
92 grid->addWidget(m_xMaxSpin, 5, 1);
93 grid->addWidget(new QLabel("y min:"), 6, 0);
94 grid->addWidget(m_yMinSpin, 6, 1);
95 grid->addWidget(new QLabel("y max:"), 7, 0);
96 grid->addWidget(m_yMaxSpin, 7, 1);
97 128 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
98 129 grid->addWidget(chartTheme, 8, 1);
99 grid->addWidget(zoomCheckBox, 9, 0);
100 // add row with empty label to make all the other rows static
101 grid->addWidget(new QLabel(""), 10, 0);
102 grid->setRowStretch(10, 1);
103
104 mainLayout->addLayout(grid, 0, 0);
105
106 // Scatter specific settings
107 m_scatterLayout = new QGridLayout();
108 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
109 m_scatterLayout->setEnabled(false);
130 }
110 131
111 // Pie specific settings
132 void MainWidget::initPieControls()
133 {
134 // Pie series specific settings
112 135 // Pie size factory
113 136 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
114 137 pieSizeSpin->setMinimum(LONG_MIN);
115 138 pieSizeSpin->setMaximum(LONG_MAX);
116 139 pieSizeSpin->setValue(1.0);
117 140 pieSizeSpin->setSingleStep(0.1);
118 141 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
119 142 // Pie position
120 143 QComboBox *piePosCombo = new QComboBox(this);
121 144 piePosCombo->addItem("Maximized");
122 145 piePosCombo->addItem("Top left");
123 146 piePosCombo->addItem("Top right");
124 147 piePosCombo->addItem("Bottom left");
125 148 piePosCombo->addItem("Bottom right");
126 149 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
127 150 this, SLOT(setPiePosition(int)));
128 151 m_pieLayout = new QGridLayout();
129 152 m_pieLayout->setEnabled(false);
130 153 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
131 154 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
132 155 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
133 156 m_pieLayout->addWidget(piePosCombo, 1, 1);
134
135 mainLayout->addLayout(m_scatterLayout, 1, 0);
136 mainLayout->addLayout(m_pieLayout, 2, 0);
137
138 //m_chartWidget->setColor(Qt::red);
139 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
140 // hbox->setStretch(1, 1);
141
142 setLayout(mainLayout);
143
144 m_autoScaleCheck->setChecked(true);
145 testDataChanged(0);
146 157 }
147 158
148 159 void MainWidget::addSeries()
149 160 {
150 161 DataSerieDialog dialog(m_defaultSeriesName, this);
151 162 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
152 163 dialog.exec();
153 164 }
154 165
155 166 void MainWidget::addSeries(QString series, QString data)
156 167 {
157 168 qDebug() << "addSeries: " << series << " data: " << data;
158 169 m_defaultSeriesName = series;
159 170
160 171 // TODO: a dedicated data class for storing x and y values
161 172 QList<qreal> x;
162 173 QList<qreal> y;
163 174
164 175 if (data == "linear") {
165 176 for (int i = 0; i < 20; i++) {
166 177 x.append(i);
167 178 y.append(i);
168 179 }
169 180 } else if (data == "linear, 1M") {
170 181 // 1 million data points from 0.0001 to 100
171 182 // TODO: What is the requirement? Should we be able to show this kind of data with
172 183 // reasonable performance, or can we expect the application developer to do "data mining"
173 184 // for us, so that the count of data points given to QtCommercial Chart is always
174 185 // reasonable?
175 186 for (qreal i = 0; i < 100; i += 0.0001) {
176 187 x.append(i);
177 188 y.append(20);
178 189 }
179 190 } else if (data == "SIN") {
180 191 for (int i = 0; i < 100; i++) {
181 192 x.append(i);
182 193 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
183 194 }
184 195 } else if (data == "SIN + random") {
185 196 for (qreal i = 0; i < 100; i += 0.1) {
186 197 x.append(i + (rand() % 5));
187 198 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
188 199 }
189 200 } else {
190 201 // TODO: check if data has a valid file name
191 202 Q_ASSERT(false);
192 203 }
193 204
194 205 // TODO: color of the series
195 206 QChartSeries *newSeries = 0;
196 207 if (series == "Scatter") {
197 208 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
198 209 Q_ASSERT(newSeries->setData(x, y));
199 210 } else if (series == "Pie") {
200 211 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
201 212 Q_ASSERT(newSeries->setData(y));
202 213 } else if (series == "Line") {
203 214 // TODO: adding data to an existing line series does not give any visuals for some reason
204 215 // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine);
205 216 // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries);
206 217 // lineSeries->setColor(Qt::blue);
207 218 // for (int i(0); i < x.count() && i < y.count(); i++) {
208 219 // lineSeries->add(x.at(i), y.at(i));
209 220 // }
210 221 //Q_ASSERT(newSeries->setData(x, y));
211 222 QXYChartSeries* series0 = QXYChartSeries::create();
212 223 for (int i(0); i < x.count() && i < y.count(); i++)
213 224 series0->add(x.at(i), y.at(i));
214 225 m_chartWidget->addSeries(series0);
215 226 newSeries = series0;
216 227 } else {
217 228 // TODO
218 229 }
219 230
220 231 // BarChart
221 232 if (series == "Bar") {
222 233 // This is the another way of creating series. Should we create test cases for both ways, if we support them?
223 234 qDebug() << "Bar chart series";
224 235 newSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this);
225 236
226 237 // Create some test data to chart
227 238 QStandardItemModel dataModel(2,10,this);
228 239 QModelIndex index;
229 240 index = dataModel.index(0,0);
230 241 // Series 1, items 6 to 9 missing.
231 242 dataModel.setData(dataModel.index(0,0),1);
232 243 dataModel.setData(dataModel.index(0,1),12);
233 244 dataModel.setData(dataModel.index(0,2),5);
234 245 dataModel.setData(dataModel.index(0,3),8);
235 246 dataModel.setData(dataModel.index(0,4),17);
236 247 dataModel.setData(dataModel.index(0,5),9);
237 248
238 249 // Series 2, some other items missing
239 250 dataModel.setData(dataModel.index(1,0),5);
240 251 dataModel.setData(dataModel.index(1,3),4);
241 252 dataModel.setData(dataModel.index(1,5),7);
242 253 dataModel.setData(dataModel.index(1,6),8);
243 254 dataModel.setData(dataModel.index(1,8),9);
244 255 dataModel.setData(dataModel.index(1,9),9);
245 256
246 257 newSeries->setData(&dataModel);
247 258
248 259 m_chartWidget->addSeries(newSeries);
249 260 }
250 261
251 262 setCurrentSeries(newSeries);
252 263 }
253 264
254 265 void MainWidget::setCurrentSeries(QChartSeries *series)
255 266 {
256 267 m_currentSeries = series;
257 268 switch (m_currentSeries->type()) {
258 269 case QChartSeries::SeriesTypeLine:
259 270 break;
260 271 case QChartSeries::SeriesTypeScatter:
261 272 break;
262 273 case QChartSeries::SeriesTypePie:
263 274 break;
264 275 case QChartSeries::SeriesTypeBar:
265 276 qDebug() << "setCurrentSeries (bar)";
266 277 break;
267 278 default:
268 279 Q_ASSERT(false);
269 280 break;
270 281 }
271 282 }
272 283
273 284 void MainWidget::testDataChanged(int itemIndex)
274 285 {
275 286 qDebug() << "testDataChanged: " << itemIndex;
276 287
277 288 // switch (itemIndex) {
278 289 // case 0: {
279 290 // QList<QChartDataPoint> data;
280 291 // for (int x = 0; x < 20; x++) {
281 292 // data.append(QChartDataPoint() << x << x / 2);
282 293 // }
283 294 // m_chartWidget->setData(data);
284 295 // break;
285 296 // }
286 297 // case 1: {
287 298 // QList<QChartDataPoint> data;
288 299 // for (int x = 0; x < 100; x++) {
289 300 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
290 301 // }
291 302 // m_chartWidget->setData(data);
292 303 // break;
293 304 // }
294 305 // case 2: {
295 306 // QList<QChartDataPoint> data;
296 307 // for (int x = 0; x < 1000; x++) {
297 308 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
298 309 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
299 310 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
300 311 // }
301 312 // m_chartWidget->setData(data);
302 313 // break;
303 314 // }
304 315 // default:
305 316 // break;
306 317 // }
307 318 }
308 319
309 320 void MainWidget::backgroundChanged(int itemIndex)
310 321 {
311 322 qDebug() << "backgroundChanged: " << itemIndex;
312 323 }
313 324
314 325 void MainWidget::autoScaleChanged(int value)
315 326 {
316 327 if (value) {
317 328 // TODO: enable auto scaling
318 329 } else {
319 330 // TODO: set scaling manually (and disable auto scaling)
320 331 }
321 332
322 333 m_xMinSpin->setEnabled(!value);
323 334 m_xMaxSpin->setEnabled(!value);
324 335 m_yMinSpin->setEnabled(!value);
325 336 m_yMaxSpin->setEnabled(!value);
326 337 }
327 338
328 339 void MainWidget::xMinChanged(int value)
329 340 {
330 341 qDebug() << "xMinChanged: " << value;
331 342 }
332 343
333 344 void MainWidget::xMaxChanged(int value)
334 345 {
335 346 qDebug() << "xMaxChanged: " << value;
336 347 }
337 348
338 349 void MainWidget::yMinChanged(int value)
339 350 {
340 351 qDebug() << "yMinChanged: " << value;
341 352 }
342 353
343 354 void MainWidget::yMaxChanged(int value)
344 355 {
345 356 qDebug() << "yMaxChanged: " << value;
346 357 }
347 358
348 359 void MainWidget::changeChartTheme(int themeIndex)
349 360 {
350 361 qDebug() << "changeChartTheme: " << themeIndex;
351 362 m_chartWidget->setTheme((QChart::ChartThemeId) themeIndex);
352 363 }
353 364
354 365 void MainWidget::setPieSizeFactor(double size)
355 366 {
356 367 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
357 368 if (pie)
358 369 pie->setSizeFactor(qreal(size));
359 370 }
360 371
361 372 void MainWidget::setPiePosition(int position)
362 373 {
363 374 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
364 375 if (pie)
365 376 pie->setPosition((QPieSeries::PiePosition) position);
366 377 }
@@ -1,50 +1,56
1 1 #ifndef MAINWIDGET_H
2 2 #define MAINWIDGET_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartwidget.h>
6 6 #include <QWidget>
7 7
8 8 class QSpinBox;
9 9 class QCheckBox;
10 10 class QGridLayout;
11 11
12 12 QTCOMMERCIALCHART_USE_NAMESPACE
13 13
14 14 class MainWidget : public QWidget
15 15 {
16 16 Q_OBJECT
17 17 public:
18 18 explicit MainWidget(QWidget *parent = 0);
19 19
20 20 signals:
21 21
22 private:
23 void initBackroundCombo(QGridLayout *grid);
24 void initScaleControls(QGridLayout *grid);
25 void initThemeCombo(QGridLayout *grid);
26 void initPieControls();
27
22 28 private slots:
23 29 void addSeries();
24 30 void addSeries(QString series, QString data);
25 31 void testDataChanged(int itemIndex);
26 32 void backgroundChanged(int itemIndex);
27 33 void autoScaleChanged(int value);
28 34 void xMinChanged(int value);
29 35 void xMaxChanged(int value);
30 36 void yMinChanged(int value);
31 37 void yMaxChanged(int value);
32 38 void setCurrentSeries(QChartSeries *series);
33 39 void changeChartTheme(int themeIndex);
34 40 void setPieSizeFactor(double margin);
35 41 void setPiePosition(int position);
36 42
37 43 private:
38 44 QChartWidget *m_chartWidget;
39 45 QCheckBox *m_autoScaleCheck;
40 46 QSpinBox *m_xMinSpin;
41 47 QSpinBox *m_xMaxSpin;
42 48 QSpinBox *m_yMinSpin;
43 49 QSpinBox *m_yMaxSpin;
44 50 QString m_defaultSeriesName;
45 51 QChartSeries *m_currentSeries;
46 52 QGridLayout *m_scatterLayout;
47 53 QGridLayout *m_pieLayout;
48 54 };
49 55
50 56 #endif // MAINWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now