@@ -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,4 +1,4 | |||||
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 | CHART_BUILD_HEADER_DIR = $$PWD/include |
|
3 | CHART_BUILD_HEADER_DIR = $$PWD/include | |
4 | CHART_BUILD_LIB_DIR = $$PWD/lib |
|
4 | CHART_BUILD_LIB_DIR = $$PWD/lib |
@@ -2,6 +2,7 | |||||
2 | #include "qchartseries.h" |
|
2 | #include "qchartseries.h" | |
3 | #include "qscatterseries.h" |
|
3 | #include "qscatterseries.h" | |
4 | #include "qscatterseries_p.h" |
|
4 | #include "qscatterseries_p.h" | |
|
5 | #include "qpieseries.h" | |||
5 | #include "qxychartseries.h" |
|
6 | #include "qxychartseries.h" | |
6 | #include "xylinechartitem_p.h" |
|
7 | #include "xylinechartitem_p.h" | |
7 | #include "xyplotdomain_p.h" |
|
8 | #include "xyplotdomain_p.h" | |
@@ -86,34 +87,45 void QChart::addSeries(QChartSeries* series) | |||||
86 | d->m_xyLineChartItems<<item; |
|
87 | d->m_xyLineChartItems<<item; | |
87 | break; |
|
88 | break; | |
88 | } |
|
89 | } | |
89 | case QChartSeries::SeriesTypeScatter: { |
|
90 | // TODO: Not tested: | |
90 | QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series); |
|
91 | // case QChartSeries::SeriesTypeScatter: { | |
91 | if (scatter) { |
|
92 | // QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series); | |
92 | scatter->d->setParentItem(this); |
|
93 | // if (scatter) { | |
93 | scene()->addItem(scatter->d); |
|
94 | // scatter->d->setParentItem(this); | |
94 | } |
|
95 | // scene()->addItem(scatter->d); | |
95 |
|
|
96 | // } | |
96 | } |
|
97 | // break; | |
|
98 | // } | |||
97 | } |
|
99 | } | |
98 | } |
|
100 | } | |
99 |
|
101 | |||
100 | QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type) |
|
102 | QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type) | |
101 | { |
|
103 | { | |
102 |
// TODO: support also other types |
|
104 | // TODO: support also other types; not only scatter and pie | |
103 |
Q_ASSERT(type == QChartSeries::SeriesTypeScatter |
|
105 | Q_ASSERT(type == QChartSeries::SeriesTypeScatter | |
104 | QChartSeries *series = 0; |
|
106 | || type == QChartSeries::SeriesTypePie); | |
105 |
|
107 | |||
106 | switch (type) { |
|
108 | switch (type) { | |
107 | case QChartSeries::SeriesTypeScatter: { |
|
109 | case QChartSeries::SeriesTypeScatter: { | |
108 | QScatterSeries *scatterSeries = new QScatterSeries(x, y, this); |
|
110 | QScatterSeries *scatterSeries = new QScatterSeries(x, y, this); | |
|
111 | connect(this, SIGNAL(sizeChanged(QRectF, qreal, qreal)), | |||
|
112 | scatterSeries, SLOT(chartSizeChanged(QRectF, qreal, qreal))); | |||
109 | scatterSeries->d->setParentItem(this); |
|
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 | default: |
|
124 | default: | |
113 | break; |
|
125 | break; | |
114 | } |
|
126 | } | |
115 |
|
127 | |||
116 |
return |
|
128 | return 0; | |
117 | } |
|
129 | } | |
118 |
|
130 | |||
119 | void QChart::setSize(const QSizeF& size) |
|
131 | void QChart::setSize(const QSizeF& size) |
@@ -14,7 +14,7 public: | |||||
14 | SeriesTypeLine = 0, |
|
14 | SeriesTypeLine = 0, | |
15 | // SeriesTypeArea, |
|
15 | // SeriesTypeArea, | |
16 | // SeriesTypeBar, |
|
16 | // SeriesTypeBar, | |
17 |
|
|
17 | SeriesTypePie, | |
18 | SeriesTypeScatter |
|
18 | SeriesTypeScatter | |
19 | // SeriesTypeSpline |
|
19 | // SeriesTypeSpline | |
20 | }; |
|
20 | }; |
@@ -56,7 +56,6 QScatterSeries::QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent) | |||||
56 | QChartSeries(parent), |
|
56 | QChartSeries(parent), | |
57 | d(new QScatterSeriesPrivate(x, y, qobject_cast<QGraphicsItem *> (parent))) |
|
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 | void QScatterSeries::chartSizeChanged(QRectF rect, qreal xscale, qreal yscale) |
|
61 | void QScatterSeries::chartSizeChanged(QRectF rect, qreal xscale, qreal yscale) |
@@ -12,11 +12,11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries | |||||
12 | Q_OBJECT |
|
12 | Q_OBJECT | |
13 | public: |
|
13 | public: | |
14 | //QScatterSeries(QSeriesData *data, QObject *chart); |
|
14 | //QScatterSeries(QSeriesData *data, QObject *chart); | |
15 |
QScatterSeries(QList<qreal> x, QList<qreal> y, QObject * |
|
15 | QScatterSeries(QList<qreal> x, QList<qreal> y, QObject *parent = 0); | |
16 | ~QScatterSeries(); |
|
16 | ~QScatterSeries(); | |
17 |
|
17 | |||
18 | public: // from QChartSeries |
|
18 | public: // from QChartSeries | |
19 |
|
|
19 | QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; } | |
20 |
|
20 | |||
21 | public Q_SLOTS: |
|
21 | public Q_SLOTS: | |
22 | void chartSizeChanged(QRectF rect, qreal xscale, qreal yscale); |
|
22 | void chartSizeChanged(QRectF rect, qreal xscale, qreal yscale); |
@@ -17,20 +17,24 SOURCES += \ | |||||
17 | xylinechart/xygrid.cpp \ |
|
17 | xylinechart/xygrid.cpp \ | |
18 | xylinechart/xyplotdomain.cpp \ |
|
18 | xylinechart/xyplotdomain.cpp \ | |
19 | qscatterseries.cpp \ |
|
19 | qscatterseries.cpp \ | |
|
20 | qpieseries.cpp \ | |||
20 | qchart.cpp \ |
|
21 | qchart.cpp \ | |
21 | axis.cpp \ |
|
22 | axis.cpp \ | |
22 | qchartwidget.cpp |
|
23 | qchartwidget.cpp \ | |
|
24 | pieslice.cpp | |||
23 |
|
25 | |||
24 | PRIVATE_HEADERS += \ |
|
26 | PRIVATE_HEADERS += \ | |
25 | xylinechart/xylinechartitem_p.h \ |
|
27 | xylinechart/xylinechartitem_p.h \ | |
26 | xylinechart/xyplotdomain_p.h \ |
|
28 | xylinechart/xyplotdomain_p.h \ | |
27 | xylinechart/xygrid_p.h \ |
|
29 | xylinechart/xygrid_p.h \ | |
28 | qscatterseries_p.h \ |
|
30 | qscatterseries_p.h \ | |
|
31 | pieslice.h \ | |||
29 | axis_p.h |
|
32 | axis_p.h | |
30 |
|
33 | |||
31 | PUBLIC_HEADERS += \ |
|
34 | PUBLIC_HEADERS += \ | |
32 | qchartseries.h \ |
|
35 | qchartseries.h \ | |
33 | qscatterseries.h \ |
|
36 | qscatterseries.h \ | |
|
37 | qpieseries.h \ | |||
34 | qchart.h \ |
|
38 | qchart.h \ | |
35 | qchartwidget.h \ |
|
39 | qchartwidget.h \ | |
36 | qchartglobal.h \ |
|
40 | qchartglobal.h \ | |
@@ -73,3 +77,5 QMAKE_EXTRA_TARGETS += chartversion | |||||
73 |
|
77 | |||
74 | unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
78 | unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR | |
75 | win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
79 | win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR | |
|
80 | ||||
|
81 |
@@ -142,6 +142,8 void MainWidget::addSeries(QString series, QString data) | |||||
142 | if (series == "Scatter") { |
|
142 | if (series == "Scatter") { | |
143 | /*QChartSeries* scatterSeries = */ |
|
143 | /*QChartSeries* scatterSeries = */ | |
144 | m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter); |
|
144 | m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter); | |
|
145 | } else if (series == "Pie") { | |||
|
146 | m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypePie); | |||
145 | } else if (series == "Line") { |
|
147 | } else if (series == "Line") { | |
146 | m_chartWidget->addSeries(series0); |
|
148 | m_chartWidget->addSeries(series0); | |
147 | } else { |
|
149 | } else { |
General Comments 0
You need to be logged in to leave comments.
Login now