##// END OF EJS Templates
Integrated draft version of pie series
Tero Ahola -
r51:73eb5fa755de
parent child
Show More
@@ -0,0 +1,75
1 #include "pieslice.h"
2 #include <QPainter>
3 #include <QDebug>
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span)
8 : m_color(color),
9 m_startAngle(startAngle),
10 m_span(span)
11 {
12 setAcceptHoverEvents(true);
13 }
14
15 PieSlice::~PieSlice()
16 {
17 }
18
19 QRectF PieSlice::boundingRect() const
20 {
21 return parentItem()->boundingRect();
22 }
23
24 QPainterPath PieSlice::shape() const
25 {
26 qreal angle = (-m_startAngle) + (90);
27 qreal span = -m_span;
28
29 QPainterPath path;
30 path.moveTo(boundingRect().center());
31 path.arcTo(boundingRect(), angle, span);
32
33 // TODO: draw the shape so that it might have a hole in the center
34 // - Sin & Cos will be needed to find inner/outer arc endpoints
35
36 // dx, dy are offsets from the center
37 //qreal l = boundingRect().height();
38 //qreal dx = qSin(angle*(M_PI/180)) * l;
39 //qreal dy = qCos(angle*(M_PI/180)) * l;
40
41 // TODO: exploded slice?
42
43 return path;
44 }
45
46 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 {
48 // Setup painter
49 painter->setBrush(m_color);
50 QPen pen;
51 //pen.setColor(m_color.darker());
52 pen.setColor(Qt::gray);
53 pen.setWidth(1);
54 painter->setPen(pen);
55
56 // From Qt docs:
57 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
58 // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction.
59 // Zero degrees is at the 3 o'clock position.
60 //
61 // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360.
62 //qreal angle = (-m_startAngle*16) + (90*16);
63 //qreal span = -m_span*16;
64 //painter->drawPie(boundingRect(), angle, span);
65
66 painter->drawPath(shape());
67 }
68
69 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
70 {
71 QGraphicsItem::hoverEnterEvent(event);
72 qDebug() << "hover" << m_color << m_startAngle << m_span;
73 }
74
75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,31
1 #ifndef PIESLICE_H
2 #define PIESLICE_H
3
4 #include "qchartglobal.h"
5 #include <QGraphicsItem>
6 #include <QRectF>
7 #include <QColor>
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11 class PieSlice : public QGraphicsItem
12 {
13 public:
14 PieSlice(const QColor& color, qreal startAngle, qreal span);
15 ~PieSlice();
16
17 public: // from QGraphicsItem
18 QRectF boundingRect() const;
19 QPainterPath shape() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
22
23 private:
24 QColor m_color;
25 qreal m_startAngle;
26 qreal m_span;
27 };
28
29 QTCOMMERCIALCHART_END_NAMESPACE
30
31 #endif // PIESLICE_H
@@ -0,0 +1,54
1 #include "qpieseries.h"
2 #include "pieslice.h"
3 #include <QGraphicsObject>
4 #include <QDebug>
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
9 QChartSeries(parent),
10 m_x(x)
11 {
12 }
13
14 QPieSeries::~QPieSeries()
15 {
16 }
17
18 void QPieSeries::chartSizeChanged(QRectF rect, qreal xscale, qreal yscale)
19 {
20 qreal fullPie = 360;
21 qreal total = 0;
22 foreach (qreal value, m_x)
23 total += value;
24
25 // We must have a parent for the graphics items we create
26 // TODO: maybe QChartSeries needs to be a QGraphicsObject to make this clear for the users?
27 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
28 Q_ASSERT(parentItem);
29 qreal angle = 0;
30 foreach (qreal value, m_x) {
31 qreal span = value / total * fullPie;
32 PieSlice *slice = new PieSlice(randomColor(), angle, span);
33 slice->setParentItem(parentItem);
34 m_slices.append(slice);
35 angle += span;
36 }
37 }
38
39 QColor QPieSeries::randomColor()
40 {
41 QColor c;
42 c.setRed(qrand() % 255);
43 c.setGreen(qrand() % 255);
44 c.setBlue(qrand() % 255);
45 return c;
46 }
47
48 void QPieSeries::setData(QList<int> data)
49 {
50 }
51
52 #include "moc_qpieseries.cpp"
53
54 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,40
1 #ifndef PIESERIES_H
2 #define PIESERIES_H
3
4 #include "qchartseries.h"
5 #include <QObject>
6 #include <QRectF>
7 #include <QColor>
8
9 class QGraphicsObject;
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class PieSlice;
12
13 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
14 {
15 Q_OBJECT
16 public:
17 // TODO: use a generic data class instead of x and y
18 QPieSeries(QList<qreal> x, QGraphicsObject *parent = 0);
19 ~QPieSeries();
20 QColor randomColor();
21 void setData(QList<int> data);
22
23 public: // from QChartSeries
24 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
25
26 public Q_SLOTS:
27 void chartSizeChanged(QRectF rect, qreal xscale, qreal yscale);
28
29 private:
30 //Q_DECLARE_PRIVATE(QPieSeries)
31 Q_DISABLE_COPY(QPieSeries)
32 // TODO: move the followin to internal impl
33 QList<qreal> m_x;
34 QList<PieSlice*> m_slices;
35 QSizeF m_size;
36 };
37
38 QTCOMMERCIALCHART_END_NAMESPACE
39
40 #endif // PIESERIES_H
@@ -1,16 +1,16
1 CONFIG+=integrated_build #remove if you want to build against installed libs
1 #CONFIG+=integrated_build #remove if you want to build against installed libs
2 2
3 3 CHART_BUILD_HEADER_DIR = $$PWD/include
4 4 CHART_BUILD_LIB_DIR = $$PWD/lib
5 5 CHART_BUILD_DIR = $$PWD/build
6 6 CHART_BUILD_BIN_DIR = $$PWD/bin
7 7
8 8 # hack to fix windows builds
9 9
10 10 win32:{
11 11 CHART_BUILD_HEADER_DIR = $$replace(CHART_BUILD_HEADER_DIR, "/","\\")
12 12 CHART_BUILD_LIB_DIR = $$replace(CHART_BUILD_LIB_DIR, "/","\\")
13 13 CHART_BUILD_BUILD_DIR = $$replace(CHART_BUILD_BUILD_DIR, "/","\\")
14 14 CHART_BUILD_BIN_DIR = $$replace(CHART_BUILD_BIN_DIR, "/","\\")
15 15 }
16 16
@@ -1,159 +1,171
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 #include "qpieseries.h"
5 6 #include "qxychartseries.h"
6 7 #include "xylinechartitem_p.h"
7 8 #include "xyplotdomain_p.h"
8 9 #include "axis_p.h"
9 10 #include "xygrid_p.h"
10 11 #include <QGraphicsScene>
11 12 #include <QDebug>
12 13
13 14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 15
15 16 class QChartPrivate
16 17 {
17 18 public:
18 19
19 20 QChartPrivate(QChart* parent):
20 21 m_axisX(new Axis(parent)),
21 22 m_axisY(new Axis(parent)),
22 23 m_grid(new XYGrid(parent)),
23 24 m_plotDataIndex(0),
24 25 m_marginSize(0){}
25 26
26 27 Axis* m_axisX;
27 28 Axis* m_axisY;
28 29 XYGrid* m_grid;
29 30 QRect m_rect;
30 31 QList<const QChartSeries*> m_series;
31 32 QList<XYPlotDomain> m_plotDomainList;
32 33 QList<XYLineChartItem*> m_xyLineChartItems;
33 34 QList<QGraphicsItem*> m_items;
34 35 int m_plotDataIndex;
35 36 int m_marginSize;
36 37
37 38 };
38 39
39 40 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40 41
41 42 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
42 43 d(new QChartPrivate(this))
43 44 {
44 45 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
45 46 // set axis
46 47 d->m_axisY->rotate(90);
47 48 }
48 49
49 50 QChart::~QChart(){}
50 51
51 52 QRectF QChart::boundingRect() const
52 53 {
53 54 return d->m_rect;
54 55 }
55 56
56 57 void QChart::addSeries(QChartSeries* series)
57 58 {
58 59 // TODO: we should check the series not already added
59 60
60 61 d->m_series<<series;
61 62
62 63 switch(series->type())
63 64 {
64 65 case QChartSeries::SeriesTypeLine: {
65 66
66 67 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
67 68
68 69 XYPlotDomain domain;
69 70 //TODO "nice numbers algorithm"
70 71 domain.m_ticksX=4;
71 72 domain.m_ticksY=4;
72 73
73 74 for (int i = 0 ; i < xyseries->count() ; i++)
74 75 {
75 76 qreal x = xyseries->x(i);
76 77 qreal y = xyseries->y(i);
77 78 domain.m_minX = qMin(domain.m_minX,x);
78 79 domain.m_minY = qMin(domain.m_minY,y);
79 80 domain.m_maxX = qMax(domain.m_maxX,x);
80 81 domain.m_maxY = qMax(domain.m_maxY,y);
81 82 }
82 83
83 84 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
84 85 item->updateXYPlotDomain(domain);
85 86 d->m_plotDomainList<<domain;
86 87 d->m_xyLineChartItems<<item;
87 88 break;
88 89 }
89 case QChartSeries::SeriesTypeScatter: {
90 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
91 if (scatter) {
92 scatter->d->setParentItem(this);
93 scene()->addItem(scatter->d);
94 }
95 break;
96 }
90 // TODO: Not tested:
91 // case QChartSeries::SeriesTypeScatter: {
92 // QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
93 // if (scatter) {
94 // scatter->d->setParentItem(this);
95 // scene()->addItem(scatter->d);
96 // }
97 // break;
98 // }
97 99 }
98 100 }
99 101
100 102 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
101 103 {
102 // TODO: support also other types in addition to scatter
103 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
104 QChartSeries *series = 0;
104 // TODO: support also other types; not only scatter and pie
105 Q_ASSERT(type == QChartSeries::SeriesTypeScatter
106 || type == QChartSeries::SeriesTypePie);
105 107
106 108 switch (type) {
107 109 case QChartSeries::SeriesTypeScatter: {
108 110 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
111 connect(this, SIGNAL(sizeChanged(QRectF, qreal, qreal)),
112 scatterSeries, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
109 113 scatterSeries->d->setParentItem(this);
110 break;
114 return scatterSeries;
115 }
116 case QChartSeries::SeriesTypePie: {
117 // TODO: we now have also a list of y values as a parameter, it is ignored
118 // we should use a generic data class instead of list of x and y values
119 QPieSeries *pieSeries = new QPieSeries(x, this);
120 connect(this, SIGNAL(sizeChanged(QRectF, qreal, qreal)),
121 pieSeries, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
122 return pieSeries;
111 123 }
112 124 default:
113 125 break;
114 126 }
115 127
116 return series;
128 return 0;
117 129 }
118 130
119 131 void QChart::setSize(const QSizeF& size)
120 132 {
121 133 d->m_rect = QRect(QPoint(0,0),size.toSize());
122 134 d->m_rect.adjust(margin(),margin(), -margin(), -margin());
123 135 d->m_grid->setPos(d->m_rect.topLeft());
124 136 d->m_grid->setSize(d->m_rect.size());
125 137
126 138 // TODO: calculate the scale
127 139 // TODO: calculate the origo
128 140 // TODO: not sure if emitting a signal here is the best from performance point of view
129 141 const qreal xscale = size.width() / 100;
130 142 const qreal yscale = size.height() / 100;
131 143 emit sizeChanged(QRectF(0, 0, size.width(), size.height()), xscale, yscale);
132 144
133 145 for (int i(0); i < d->m_plotDomainList.size(); i++)
134 146 d->m_plotDomainList[i].m_viewportRect = d->m_rect;
135 147
136 148 // TODO: line chart items are updated separately as they don't support update
137 149 // via sizeChanged signal
138 150 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
139 151 item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex));
140 152
141 153 if (d->m_plotDomainList.count())
142 154 d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex));
143 155 update();
144 156 }
145 157
146 158 int QChart::margin() const
147 159 {
148 160 return d->m_marginSize;
149 161 }
150 162
151 163 void QChart::setMargin(int margin)
152 164 {
153 165 d->m_marginSize = margin;
154 166 }
155 167
156 168 #include "moc_qchart.cpp"
157 169
158 170
159 171 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,38 +1,38
1 1 #ifndef QCHARTSERIES_H
2 2 #define QCHARTSERIES_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include <QObject>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 class QTCOMMERCIALCHART_EXPORT QChartSeries : public QObject
10 10 {
11 11
12 12 public:
13 13 enum QChartSeriesType {
14 14 SeriesTypeLine = 0,
15 15 // SeriesTypeArea,
16 16 // SeriesTypeBar,
17 // SeriesTypePie,
17 SeriesTypePie,
18 18 SeriesTypeScatter
19 19 // SeriesTypeSpline
20 20 };
21 21
22 22 protected:
23 23 QChartSeries(QObject *parent = 0):QObject(parent){};
24 24
25 25 public:
26 26 virtual ~QChartSeries(){};
27 27
28 28 //factory method
29 29 static QChartSeries* create(QObject* parent = 0 ){ return 0;}
30 30 //pure virtual
31 31 virtual QChartSeriesType type() const = 0;
32 32
33 33 };
34 34
35 35 QTCOMMERCIALCHART_END_NAMESPACE
36 36
37 37 #endif
38 38
@@ -1,77 +1,76
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(QList<qreal> x, QList<qreal> y, QGraphicsItem *parent) :
13 13 QGraphicsItem(parent),
14 14 m_x(x),
15 15 m_y(y)
16 16 {
17 17 }
18 18
19 19 void QScatterSeriesPrivate::resize(QRectF rect, qreal xscale, qreal yscale)
20 20 {
21 21 m_scenex.clear();
22 22 m_sceney.clear();
23 23
24 24 foreach(qreal x, m_x)
25 25 m_scenex.append(rect.left() + x * xscale);
26 26
27 27 foreach(qreal y, m_y)
28 28 m_sceney.append(rect.bottom() - y * yscale);
29 29 }
30 30
31 31 QRectF QScatterSeriesPrivate::boundingRect() const
32 32 {
33 33 return QRectF(0, 0, 55, 100);
34 34 }
35 35
36 36 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 37 {
38 38 QPen pen = painter->pen();
39 39 QBrush brush = pen.brush();
40 40 // TODO: The opacity should be user definable...
41 41 brush.setColor(QColor(255, 82, 0, 50));
42 42 pen.setBrush(brush);
43 43 pen.setWidth(4);
44 44 painter->setPen(pen);
45 45
46 46 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
47 47 // event right after construction or maybe given a size during initialization
48 48 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
49 49 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
50 50 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
51 51 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
52 52 }
53 53 }
54 54
55 55 QScatterSeries::QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent) :
56 56 QChartSeries(parent),
57 57 d(new QScatterSeriesPrivate(x, y, qobject_cast<QGraphicsItem *> (parent)))
58 58 {
59 connect(parent, SIGNAL(sizeChanged(QRectF, qreal, qreal)), this, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
60 59 }
61 60
62 61 void QScatterSeries::chartSizeChanged(QRectF rect, qreal xscale, qreal yscale)
63 62 {
64 63 // Recalculate scatter data point locations on the scene
65 64 // d->transform().reset();
66 65 // d->transform().translate();
67 66 d->resize(rect, xscale, yscale);
68 67 }
69 68
70 69 QScatterSeries::~QScatterSeries()
71 70 {
72 71 delete d;
73 72 }
74 73
75 74 #include "moc_qscatterseries.cpp"
76 75
77 76 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,33 +1,33
1 1 #ifndef QSCATTERSERIES_H
2 2 #define QSCATTERSERIES_H
3 3
4 4 #include "qchartseries.h"
5 5 #include <QRectF>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8 class QScatterSeriesPrivate;
9 9
10 10 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
11 11 {
12 12 Q_OBJECT
13 13 public:
14 14 //QScatterSeries(QSeriesData *data, QObject *chart);
15 QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *chart = 0);
15 QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent = 0);
16 16 ~QScatterSeries();
17 17
18 18 public: // from QChartSeries
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
19 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
20 20
21 21 public Q_SLOTS:
22 22 void chartSizeChanged(QRectF rect, qreal xscale, qreal yscale);
23 23
24 24 private:
25 25 Q_DECLARE_PRIVATE(QScatterSeries)
26 26 Q_DISABLE_COPY(QScatterSeries)
27 27 friend class QChart;
28 28 QScatterSeriesPrivate *const d;
29 29 };
30 30
31 31 QTCOMMERCIALCHART_END_NAMESPACE
32 32
33 33 #endif // QSCATTERSERIES_H
@@ -1,75 +1,81
1 1 !include( ../common.pri ) {
2 2 error( Couldn't find the common.pri file! )
3 3 }
4 4
5 5 TARGET = QtCommercialChart
6 6 DESTDIR = $$CHART_BUILD_LIB_DIR
7 7 TEMPLATE = lib
8 8 QT += core \
9 9 gui
10 10
11 11 CONFIG += debug_and_release
12 12 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
13 13
14 14 SOURCES += \
15 15 xylinechart/qxychartseries.cpp \
16 16 xylinechart/xylinechartitem.cpp \
17 17 xylinechart/xygrid.cpp \
18 18 xylinechart/xyplotdomain.cpp \
19 19 qscatterseries.cpp \
20 qpieseries.cpp \
20 21 qchart.cpp \
21 22 axis.cpp \
22 qchartwidget.cpp
23 qchartwidget.cpp \
24 pieslice.cpp
23 25
24 26 PRIVATE_HEADERS += \
25 27 xylinechart/xylinechartitem_p.h \
26 28 xylinechart/xyplotdomain_p.h \
27 29 xylinechart/xygrid_p.h \
28 30 qscatterseries_p.h \
31 pieslice.h \
29 32 axis_p.h
30 33
31 34 PUBLIC_HEADERS += \
32 35 qchartseries.h \
33 36 qscatterseries.h \
37 qpieseries.h \
34 38 qchart.h \
35 39 qchartwidget.h \
36 40 qchartglobal.h \
37 41 xylinechart/qxychartseries.h
38 42
39 43 HEADERS += $$PUBLIC_HEADERS
40 44 HEADERS += $$PRIVATE_HEADERS
41 45
42 46 INCLUDEPATH += xylinechart \
43 47 .
44 48
45 49 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
46 50 MOC_DIR = $$CHART_BUILD_DIR/lib
47 51 UI_DIR = $$CHART_BUILD_DIR/lib
48 52 RCC_DIR = $$CHART_BUILD_DIR/lib
49 53
50 54
51 55 DEFINES += QTCOMMERCIALCHART_LIBRARY
52 56
53 57 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
54 58 public_headers.files = $$PUBLIC_HEADERS
55 59 target.path = $$[QT_INSTALL_LIBS]
56 60 INSTALLS += target \
57 61 public_headers
58 62
59 63
60 64 install_build_headers.name = bild_headers
61 65 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
62 66 install_build_headers.input = PUBLIC_HEADERS
63 67 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
64 68 install_build_headers.CONFIG += target_predeps no_link
65 69 QMAKE_EXTRA_COMPILERS += install_build_headers
66 70
67 71 chartversion.target = qchartversion_p.h
68 72 chartversion.commands = @echo "build_time" > $$chartversion.target;
69 73 chartversion.depends = $$HEADERS $$SOURCES
70 74 PRE_TARGETDEPS += qchartversion_p.h
71 75 QMAKE_CLEAN+= qchartversion_p.h
72 76 QMAKE_EXTRA_TARGETS += chartversion
73 77
74 78 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
75 79 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
80
81
@@ -1,239 +1,241
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include <qxychartseries.h>
4 4 #include <QPushButton>
5 5 #include <QComboBox>
6 6 #include <QSpinBox>
7 7 #include <QCheckBox>
8 8 #include <QGridLayout>
9 9 #include <QHBoxLayout>
10 10 #include <QLabel>
11 11 #include <QSpacerItem>
12 12 #include <QMessageBox>
13 13 #include <cmath>
14 14 #include <QDebug>
15 15
16 16 QTCOMMERCIALCHART_USE_NAMESPACE
17 17
18 18 MainWidget::MainWidget(QWidget *parent) :
19 19 QWidget(parent)
20 20 {
21 21 QPushButton *addSeriesButton = new QPushButton("Add series");
22 22 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
23 23
24 24 // Chart background
25 25 QComboBox *backgroundCombo = new QComboBox(this);
26 26 backgroundCombo->addItem("None");
27 27 backgroundCombo->addItem("TODO Grid");
28 28 backgroundCombo->addItem("TODO Image");
29 29 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
30 30 this, SLOT(backgroundChanged(int)));
31 31
32 32 // Axis
33 33 // TODO: multiple axes?
34 34 m_autoScaleCheck = new QCheckBox("Automatic scaling");
35 35 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
36 36 // Allow setting also non-sense values (like -2147483648 and 2147483647)
37 37 m_xMinSpin = new QSpinBox();
38 38 m_xMinSpin->setMinimum(INT_MIN);
39 39 m_xMinSpin->setMaximum(INT_MAX);
40 40 m_xMinSpin->setValue(0);
41 41 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
42 42 m_xMaxSpin = new QSpinBox();
43 43 m_xMaxSpin->setMinimum(INT_MIN);
44 44 m_xMaxSpin->setMaximum(INT_MAX);
45 45 m_xMaxSpin->setValue(10);
46 46 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
47 47 m_yMinSpin = new QSpinBox();
48 48 m_yMinSpin->setMinimum(INT_MIN);
49 49 m_yMinSpin->setMaximum(INT_MAX);
50 50 m_yMinSpin->setValue(0);
51 51 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
52 52 m_yMaxSpin = new QSpinBox();
53 53 m_yMaxSpin->setMinimum(INT_MIN);
54 54 m_yMaxSpin->setMaximum(INT_MAX);
55 55 m_yMaxSpin->setValue(10);
56 56 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
57 57
58 58 QGridLayout *grid = new QGridLayout();
59 59 QHBoxLayout *hbox = new QHBoxLayout();
60 60 //grid->addWidget(new QLabel("Add series:"), 0, 0);
61 61 grid->addWidget(addSeriesButton, 0, 1);
62 62 grid->addWidget(new QLabel("Background:"), 2, 0);
63 63 grid->addWidget(backgroundCombo, 2, 1);
64 64 grid->addWidget(m_autoScaleCheck, 3, 0);
65 65 grid->addWidget(new QLabel("x min:"), 4, 0);
66 66 grid->addWidget(m_xMinSpin, 4, 1);
67 67 grid->addWidget(new QLabel("x max:"), 5, 0);
68 68 grid->addWidget(m_xMaxSpin, 5, 1);
69 69 grid->addWidget(new QLabel("y min:"), 6, 0);
70 70 grid->addWidget(m_yMinSpin, 6, 1);
71 71 grid->addWidget(new QLabel("y max:"), 7, 0);
72 72 grid->addWidget(m_yMaxSpin, 7, 1);
73 73 // add row with empty label to make all the other rows static
74 74 grid->addWidget(new QLabel(""), 8, 0);
75 75 grid->setRowStretch(8, 1);
76 76
77 77 hbox->addLayout(grid);
78 78
79 79 m_chartWidget = new QChartWidget(this);
80 80 //m_chartWidget->setColor(Qt::red);
81 81 hbox->addWidget(m_chartWidget);
82 82 // hbox->setStretch(1, 1);
83 83
84 84 setLayout(hbox);
85 85
86 86 m_autoScaleCheck->setChecked(true);
87 87 chartTypeChanged(4);
88 88 testDataChanged(0);
89 89 }
90 90
91 91 void MainWidget::addSeries()
92 92 {
93 93 DataSerieDialog dialog(m_defaultSeries, this);
94 94 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
95 95 dialog.exec();
96 96 }
97 97
98 98 void MainWidget::addSeries(QString series, QString data)
99 99 {
100 100 qDebug() << "addSeries: " << series << " data: " << data;
101 101 m_defaultSeries = series;
102 102
103 103 QXYChartSeries* series0 = QXYChartSeries::create();
104 104
105 105 // TODO: a dedicated data class for storing x and y values
106 106 QList<qreal> x;
107 107 QList<qreal> y;
108 108
109 109 if (data == "linear") {
110 110 for (int i = 0; i < 20; i++) {
111 111 x.append(i);
112 112 y.append(i);
113 113 }
114 114 for (int i = 0; i < 20; i++)
115 115 series0->add(i, i);
116 116 } else if (data == "linear, 1M") {
117 117 for (int i = 0; i < 10000; i++) {
118 118 x.append(i);
119 119 y.append(20);
120 120 }
121 121 for (int i = 0; i < 1000000; i++)
122 122 series0->add(i, 10);
123 123 } else if (data == "SIN") {
124 124 for (int i = 0; i < 100; i++) {
125 125 x.append(i);
126 126 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
127 127 }
128 128 for (int i = 0; i < 100; i++)
129 129 series0->add(i, abs(sin(3.14159265358979 / 50 * i) * 100));
130 130 } else if (data == "SIN + random") {
131 131 for (qreal i = 0; i < 100; i += 0.1) {
132 132 x.append(i + (rand() % 5));
133 133 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
134 134 }
135 135 for (qreal i = 0; i < 100; i += 0.1)
136 136 series0->add(i + (rand() % 5), abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
137 137 } else {
138 138 // TODO: check if data has a valid file name
139 139 }
140 140
141 141 // TODO: color of the series
142 142 if (series == "Scatter") {
143 143 /*QChartSeries* scatterSeries = */
144 144 m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
145 } else if (series == "Pie") {
146 m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypePie);
145 147 } else if (series == "Line") {
146 148 m_chartWidget->addSeries(series0);
147 149 } else {
148 150 // TODO
149 151 }
150 152 }
151 153
152 154 void MainWidget::chartTypeChanged(int itemIndex)
153 155 {
154 156 // TODO: change chart type
155 157 switch (itemIndex) {
156 158 case 4:
157 159 //m_chartWidget->setType(4);
158 160 break;
159 161 default: {
160 162 //m_chartWidget->setType(0);
161 163 break;
162 164 }
163 165 }
164 166 }
165 167
166 168 void MainWidget::testDataChanged(int itemIndex)
167 169 {
168 170 qDebug() << "testDataChanged: " << itemIndex;
169 171
170 172 // switch (itemIndex) {
171 173 // case 0: {
172 174 // QList<QChartDataPoint> data;
173 175 // for (int x = 0; x < 20; x++) {
174 176 // data.append(QChartDataPoint() << x << x / 2);
175 177 // }
176 178 // m_chartWidget->setData(data);
177 179 // break;
178 180 // }
179 181 // case 1: {
180 182 // QList<QChartDataPoint> data;
181 183 // for (int x = 0; x < 100; x++) {
182 184 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
183 185 // }
184 186 // m_chartWidget->setData(data);
185 187 // break;
186 188 // }
187 189 // case 2: {
188 190 // QList<QChartDataPoint> data;
189 191 // for (int x = 0; x < 1000; x++) {
190 192 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
191 193 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
192 194 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
193 195 // }
194 196 // m_chartWidget->setData(data);
195 197 // break;
196 198 // }
197 199 // default:
198 200 // break;
199 201 // }
200 202 }
201 203
202 204 void MainWidget::backgroundChanged(int itemIndex)
203 205 {
204 206 qDebug() << "backgroundChanged: " << itemIndex;
205 207 }
206 208
207 209 void MainWidget::autoScaleChanged(int value)
208 210 {
209 211 if (value) {
210 212 // TODO: enable auto scaling
211 213 } else {
212 214 // TODO: set scaling manually (and disable auto scaling)
213 215 }
214 216
215 217 m_xMinSpin->setEnabled(!value);
216 218 m_xMaxSpin->setEnabled(!value);
217 219 m_yMinSpin->setEnabled(!value);
218 220 m_yMaxSpin->setEnabled(!value);
219 221 }
220 222
221 223 void MainWidget::xMinChanged(int value)
222 224 {
223 225 qDebug() << "xMinChanged: " << value;
224 226 }
225 227
226 228 void MainWidget::xMaxChanged(int value)
227 229 {
228 230 qDebug() << "xMaxChanged: " << value;
229 231 }
230 232
231 233 void MainWidget::yMinChanged(int value)
232 234 {
233 235 qDebug() << "yMinChanged: " << value;
234 236 }
235 237
236 238 void MainWidget::yMaxChanged(int value)
237 239 {
238 240 qDebug() << "yMaxChanged: " << value;
239 241 }
General Comments 0
You need to be logged in to leave comments. Login now