##// END OF EJS Templates
Marek Rosa -
r426:95162da3dc01 merge
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -0,0 +1,30
1 /*!
2 \example example/areachart
3 \title AreaChart Example
4 \subtitle
5
6 The example shows how to create simple area chart.
7
8 \image areachart.png
9
10 To create area charts, we need two QLineSeries instances. They are going to define upper and lower boundary of the area.
11
12 \snippet ../example/areachart/main.cpp 1
13
14 We add data to both series, we use stream operator.
15
16 \snippet ../example/areachart/main.cpp 2
17
18 Now we create QAreaSeries instance using two line series objects. We set the custom gradient fill and width of the outline.
19
20 \snippet ../example/areachart/main.cpp 3
21
22 In the end we create QChartView instance, set title, set anti-aliasing and add area series. We also set the specific range for axes.
23
24 \snippet ../example/areachart/main.cpp 4
25
26 Chart is ready to be shown.
27
28 \snippet ../example/areachart/main.cpp 5
29
30 */ No newline at end of file
@@ -0,0 +1,8
1 !include( ../example.pri ) {
2 error( "Couldn't find the example.pri file!" )
3 }
4 TARGET = areachart
5 SOURCES += main.cpp
6
7
8
@@ -0,0 +1,59
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <qchartview.h>
4 #include <qlineseries.h>
5 #include <qareaseries.h>
6 #include <qchart.h>
7 #include <qchartaxis.h>
8 #include <cmath>
9
10 QTCOMMERCIALCHART_USE_NAMESPACE
11
12 int main(int argc, char *argv[])
13 {
14 QApplication a(argc, argv);
15
16 //![1]
17
18 QLineSeries* series0 = new QLineSeries();
19 QLineSeries* series1 = new QLineSeries();
20
21 //![1]
22
23 //![2]
24 *series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) << QPointF(12,6) << QPointF(16,7) << QPointF(18,5);
25 *series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) << QPointF(12,3) << QPointF(16,4) << QPointF(18,3);
26 //![2]
27 //![3]
28
29 QAreaSeries* series = new QAreaSeries(series0,series1);
30 QPen pen(0x059605);
31 pen.setWidth(3);
32 series->setPen(pen);
33
34 QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));
35 gradient.setColorAt(0.0,0x3cc63c);
36 gradient.setColorAt(1.0, 0x26f626);
37 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
38 series->setBrush(gradient);
39
40 //![3]
41 //![4]
42 QMainWindow window;
43 QChartView* chartView = new QChartView(&window);
44
45 chartView->setChartTitle("Basic area chart example");
46 chartView->setRenderHint(QPainter::Antialiasing);
47
48 chartView->addSeries(series);
49 chartView->axisX()->setRange(0,20);
50 chartView->axisY()->setRange(0,10);
51 //![4]
52 //![5]
53 window.setCentralWidget(chartView);
54 window.resize(400, 300);
55 window.show();
56 //![5]
57
58 return a.exec();
59 }
@@ -0,0 +1,14
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
3
4 SOURCES += \
5 #$$PWD/areachartanimationitem.cpp \
6 $$PWD/areachartitem.cpp \
7 $$PWD/qareaseries.cpp
8
9 PRIVATE_HEADERS += \
10 $$PWD/areachartitem_p.h \
11 # $$PWD/linechartanimationitem_p.h
12
13 PUBLIC_HEADERS += \
14 $$PWD/qareaseries.h No newline at end of file
@@ -0,0 +1,63
1 #include "linechartanimationitem_p.h"
2 #include "linechartitem_p.h"
3 #include <QPropertyAnimation>
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
7 const static int duration = 500;
8
9
10 LineChartAnimationItem::LineChartAnimationItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):
11 LineChartItem(presenter,series,parent)
12 {
13
14 }
15
16 LineChartAnimationItem::~LineChartAnimationItem()
17 {
18 }
19
20 void LineChartAnimationItem::addPoints(const QVector<QPointF>& points)
21 {
22 m_data=points;
23 clearView();
24 QPropertyAnimation *animation = new QPropertyAnimation(this, "a_addPoints", parent());
25 animation->setDuration(duration);
26 //animation->setEasingCurve(QEasingCurve::InOutBack);
27 animation->setKeyValueAt(0.0, 0);
28 animation->setKeyValueAt(1.0, m_data.size());
29 animation->start(QAbstractAnimation::DeleteWhenStopped);
30 }
31
32 void LineChartAnimationItem::setPoint(int index,const QPointF& point)
33 {
34 AnimationHelper* helper = new AnimationHelper(this,index);
35 QPropertyAnimation *animation = new QPropertyAnimation(helper, "point", parent());
36 animation->setDuration(duration);
37 //animation->setEasingCurve(QEasingCurve::InOutBack);
38 animation->setKeyValueAt(0.0, points().value(index));
39 animation->setKeyValueAt(1.0, point);
40 animation->start(QAbstractAnimation::DeleteWhenStopped);
41 }
42
43 void LineChartAnimationItem::aw_addPoints(int points)
44 {
45 int index = count();
46 for(int i = index;i< points ;i++){
47 LineChartItem::addPoint(m_data.at(i));
48 }
49 updateGeometry();
50 update();
51 }
52
53 void LineChartAnimationItem::aw_setPoint(int index,const QPointF& point)
54 {
55 LineChartItem::setPoint(index,point);
56 updateGeometry();
57 update();
58 }
59
60
61 #include "moc_linechartanimationitem_p.cpp"
62
63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,55
1 #ifndef LINECHARTANIMATIONITEM_P_H_
2 #define LINECHARTANIMATIONITEM_P_H_
3
4 #include "qchartglobal.h"
5 #include "linechartitem_p.h"
6 #include "domain_p.h"
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class LineChartItem;
11
12 class LineChartAnimationItem : public LineChartItem {
13 Q_OBJECT
14 Q_PROPERTY(int a_addPoints READ ar_addPoints WRITE aw_addPoints);
15 // Q_PROPERTY(QPointF a_setPoint READ ar_setPoint WRITE aw_setPoint);
16 public:
17 LineChartAnimationItem(ChartPresenter* presenter, QLineSeries *series, QGraphicsItem *parent = 0);
18 virtual ~LineChartAnimationItem();
19
20 void addPoints(const QVector<QPointF>& points);
21 void setPoint(int index,const QPointF& point);
22 //void removePoint(const QPointF& point){};
23 //void setPoint(const QPointF& oldPoint, const QPointF& newPoint){};
24
25 int ar_addPoints() const { return m_addPoints;}
26 void aw_addPoints(int points);
27 const QPointF& ar_setPoint() const { return m_setPoint;}
28 void aw_setPoint(int index,const QPointF& point);
29
30 private:
31 QVector<QPointF> m_data;
32 Domain m_domain;
33 int m_addPoints;
34 QPointF m_setPoint;
35 int m_setPoint_index;
36 };
37
38 class AnimationHelper: public QObject
39 {
40 Q_OBJECT
41 Q_PROPERTY(QPointF point READ point WRITE setPoint);
42 public:
43 AnimationHelper(LineChartAnimationItem* item,int index):m_item(item),m_index(index){};
44 void setPoint(const QPointF& point){
45 m_item->aw_setPoint(m_index,point);
46 }
47 QPointF point(){return m_point;}
48 QPointF m_point;
49 LineChartAnimationItem* m_item;
50 int m_index;
51 };
52
53 QTCOMMERCIALCHART_END_NAMESPACE
54
55 #endif
@@ -0,0 +1,116
1 #include "areachartitem_p.h"
2 #include "qareaseries.h"
3 #include "qlineseries.h"
4 #include "chartpresenter_p.h"
5 #include <QPainter>
6
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 //TODO: optimize : remove points which are not visible
11
12 AreaChartItem::AreaChartItem(ChartPresenter* presenter, QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent),
13 m_presenter(presenter),
14 m_series(areaSeries),
15 m_upper(0),
16 m_lower(0)
17 {
18 //m_items.setZValue(ChartPresenter::LineChartZValue);
19 m_upper = new AreaBoundItem(this,presenter,m_series->upperSeries());
20 if(m_series->lowerSeries()){
21 m_lower = new AreaBoundItem(this,presenter,m_series->lowerSeries());
22 }
23 setZValue(ChartPresenter::LineChartZValue);
24
25 QObject::connect(presenter,SIGNAL(geometryChanged(const QRectF&)),this,SLOT(handleGeometryChanged(const QRectF&)));
26 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
27
28 handleUpdated();
29 }
30
31 AreaChartItem::~AreaChartItem()
32 {
33 delete m_upper;
34 delete m_lower;
35 };
36
37 QRectF AreaChartItem::boundingRect() const
38 {
39 return m_rect;
40 }
41
42 QPainterPath AreaChartItem::shape() const
43 {
44 return m_path;
45 }
46
47 void AreaChartItem::setPen(const QPen& pen)
48 {
49 m_pen = pen;
50 }
51
52 void AreaChartItem::setBrush(const QBrush& brush)
53 {
54 m_brush = brush;
55 }
56
57 void AreaChartItem::updatePath()
58 {
59 QPainterPath path;
60
61 path.connectPath(m_upper->shape());
62 if(m_lower){
63 path.connectPath(m_lower->shape().toReversed());
64 }
65 else{
66 QPointF first = path.pointAtPercent(0);
67 QPointF last = path.pointAtPercent(1);
68 path.lineTo(last.x(),m_clipRect.bottom());
69 path.lineTo(first.x(),m_clipRect.bottom());
70 }
71 path.closeSubpath();
72 prepareGeometryChange();
73 m_path=path;
74 m_rect=path.boundingRect();
75 update();
76 }
77
78 void AreaChartItem::handleUpdated()
79 {
80 setPen(m_series->pen());
81 setBrush(m_series->brush());
82 update();
83 }
84
85 void AreaChartItem::handleDomainChanged(const Domain& domain)
86 {
87 m_upper->handleDomainChanged(domain);
88 if(m_lower)
89 m_lower->handleDomainChanged(domain);
90 }
91
92 void AreaChartItem::handleGeometryChanged(const QRectF& rect)
93 {
94 m_clipRect=rect.translated(-rect.topLeft());
95 setPos(rect.topLeft());
96 m_upper->handleGeometryChanged(rect);
97 if(m_lower)
98 m_lower->handleGeometryChanged(rect);
99 }
100 //painter
101
102 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 {
104 Q_UNUSED(widget);
105 Q_UNUSED(option);
106 painter->save();
107 painter->setPen(m_pen);
108 painter->setBrush(m_brush);
109 painter->setClipRect(m_clipRect);
110 painter->drawPath(m_path);
111 painter->restore();
112 }
113
114 #include "moc_areachartitem_p.cpp"
115
116 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,67
1 #ifndef AREACHARTITEM_H
2 #define AREACHARTITEM_H
3
4 #include "qchartglobal.h"
5 #include "linechartitem_p.h"
6 #include <QPen>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class ChartPresenter;
11 class QAreaSeries;
12 class AreaChartItem;
13
14 class AreaChartItem : public QObject ,public ChartItem
15 {
16 Q_OBJECT
17 public:
18 AreaChartItem(ChartPresenter* presenter, QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
19 ~ AreaChartItem();
20
21 //from QGraphicsItem
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 QPainterPath shape() const;
25
26 void setPen(const QPen& pen);
27 void setBrush(const QBrush& brush);
28 void setPointsVisible(bool visible);
29 void updatePath();
30 public slots:
31 void handleUpdated();
32 void handleDomainChanged(const Domain& domain);
33 void handleGeometryChanged(const QRectF& size);
34
35 private:
36 ChartPresenter* m_presenter;
37 QPainterPath m_path;
38 QAreaSeries* m_series;
39 LineChartItem* m_upper;
40 LineChartItem* m_lower;
41 QRectF m_rect;
42 QRectF m_clipRect;
43 QPen m_pen;
44 QBrush m_brush;
45 };
46
47 class AreaBoundItem : public LineChartItem
48 {
49 public:
50 AreaBoundItem(AreaChartItem* item,ChartPresenter* presenter, QLineSeries* lineSeries):LineChartItem(presenter,lineSeries),
51 m_item(item){};
52
53 ~AreaBoundItem(){};
54
55 void applyGeometry(QVector<QPointF>& points){
56 LineChartItem::applyGeometry(points);
57 m_item->updatePath();
58 }
59
60 private:
61 AreaChartItem* m_item;
62
63 };
64
65 QTCOMMERCIALCHART_END_NAMESPACE
66
67 #endif
@@ -0,0 +1,115
1 #include "qareaseries.h"
2 #include "qlineseries.h"
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6 /*!
7 \class QAreaSeries
8 \brief The QAreaSeries class is used for making area charts.
9
10 \mainclass
11
12 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
13 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
14 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
15 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
16 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
17
18 \image areachart.png
19
20 Creating basic area chart is simple:
21 \code
22 QLineSeries* lineSeries = new QLineSeries();
23 series->add(0, 6);
24 series->add(2, 4);
25 QAreaSeries* areaSeries = new QAreaSeries(lineSeries);
26 ...
27 chartView->addSeries(areaSeries);
28 \endcode
29 */
30
31 /*!
32 \fn virtual QSeriesType QAreaSeries::type() const
33 \brief Returns type of series.
34 \sa QSeries, QSeriesType
35 */
36
37 /*!
38 \fn QLineSeries* QAreaSeries::upperSeries() const
39 \brief Returns upperSeries used to define one of area boundaries.
40 */
41
42 /*!
43 \fn QLineSeries* QAreaSeries::lowerSeries() const
44 \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries
45 this function return Null pointer.
46 */
47
48 /*!
49 \fn QPen QAreaSeries::pen() const
50 \brief Returns the pen used to draw line for this series.
51 \sa setPen()
52 */
53
54 /*!
55 \fn QPen QAreaSeries::brush() const
56 \brief Returns the brush used to draw line for this series.
57 \sa setBrush()
58 */
59
60 /*!
61 \fn bool QAreaSeries::pointsVisible() const
62 \brief Returns if the points are drawn for this series.
63 \sa setPointsVisible()
64 */
65
66 /*!
67 \fn void QAreaSeries::updated()
68 \brief \internal
69 */
70
71 /*!
72 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
73 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
74 When series object is added to QChartView or QChart instance ownerships is transfered.
75 */
76 QAreaSeries::QAreaSeries(QLineSeries* upperSeries,QLineSeries* lowerSeries):QSeries(upperSeries),
77 m_upperSeries(upperSeries),
78 m_lowerSeries(lowerSeries),
79 m_pointsVisible(false)
80 {
81 }
82 /*!
83 Destroys the object. Series added to QChartView or QChart instances are owned by those,
84 and are deleted when mentioned object are destroyed.
85 */
86 QAreaSeries::~QAreaSeries()
87 {
88 }
89
90 /*!
91 Sets \a pen used for drawing area outline.
92 */
93 void QAreaSeries::setPen(const QPen& pen)
94 {
95 m_pen=pen;
96 }
97
98 /*!
99 Sets \a brush used for filling the area.
100 */
101 void QAreaSeries::setBrush(const QBrush& brush)
102 {
103 m_brush=brush;
104 }
105 /*!
106 Sets if data points are \a visible and should be drawn on line.
107 */
108 void QAreaSeries::setPointsVisible(bool visible)
109 {
110 m_pointsVisible=visible;
111 }
112
113 #include "moc_qareaseries.cpp"
114
115 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,48
1 #ifndef QAREASERIES_H_
2 #define QAREASERIES_H_
3
4 #include "qchartglobal.h"
5 #include "qseries.h"
6 #include <QDebug>
7 #include <QPen>
8 #include <QBrush>
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class QLineSeries;
12
13 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QSeries
14 {
15 Q_OBJECT
16 public:
17 QAreaSeries(QLineSeries* upperSeries,QLineSeries* lowerSeries=0);
18 virtual ~QAreaSeries();
19
20 public: // from QChartSeries
21 virtual QSeriesType type() const { return QSeries::SeriesTypeArea;}
22
23 QLineSeries* upperSeries() const { return m_upperSeries;}
24 QLineSeries* lowerSeries() const { return m_lowerSeries;}
25
26 void setPen(const QPen& pen);
27 QPen pen() const { return m_pen;}
28
29 void setBrush(const QBrush& brush);
30 QBrush brush() const { return m_brush;}
31
32 void setPointsVisible(bool visible);
33 bool pointsVisible() const {return m_pointsVisible;}
34
35 signals:
36 void updated();
37
38 private:
39 QBrush m_brush;
40 QPen m_pen;
41 QLineSeries* m_upperSeries;
42 QLineSeries* m_lowerSeries;
43 bool m_pointsVisible;
44 };
45
46 QTCOMMERCIALCHART_END_NAMESPACE
47
48 #endif
@@ -13,6 +13,7
13 13 <tr>
14 14 <td valign="top">
15 15 <ul>
16 <li><a href="qareaseries.html">QAreaSeries</a></li>
16 17 <li><a href="qbarseries.html">QBarSeries</a></li>
17 18 <li><a href="qbarset.html">QBarSet</a></li>
18 19 <li><a href="qchart.html">QChart</a></li>
@@ -13,6 +13,7
13 13 <tr>
14 14 <td valign="top">
15 15 <ul>
16 <li><a href="example-areachart.html">Area Chart example</a></li>
16 17 <li><a href="example-barchart.html">Bar Chart example</a></li>
17 18 <li><a href="example-stackedbarchart.html">Stacked Bar Chart example</a></li>
18 19 <li><a href="example-percentbarchart.html">Percent Bar Chart example</a></li>
@@ -16,21 +16,22
16 16
17 17 <table><tr>
18 18 <td><a href="example-linechart.html"><img src="images/linechart.png" alt="linechart" /></a></td>
19 <td><a href="example-barchart.html"><img src="images/barchart.png" alt="barchart" /></a></td>
19 <td><a href="example-areachart.html"><img src="images/areachart.png" alt="areachart" /></a></td>
20 20 </tr>
21 21 <tr>
22 <td><a href="example-barchart.html"><img src="images/barchart.png" alt="barchart" /></a></td>
22 23 <td><a href="example-percentbarchart.html"><img src="images/percentbarchart.png" alt="percentbarcchart" /></a></td>
23 <td><a href="example-stackedbarchart.html"><img src="images/stackedbarchart.png" alt="stackedbarchart" /></a></td>
24 24 </tr>
25 25 <tr>
26 <td><a href="example-stackedbarchart.html"><img src="images/stackedbarchart.png" alt="stackedbarchart" /></a></td>
26 27 <td><img src="images/chartview_example.jpg " alt="linechart" /></td>
27 <td><img src="images/chartview_example_pie.jpg " alt="piechart" /></td>
28 28 </tr><tr>
29 29 <td><img src="images/chartview_example_scatter.jpg " alt="scatterchart" /></td>
30 30 <td><img src="images/chartview_example_theme.jpg " alt="themechart" /></td>
31 31 </tr>
32 32 <tr>
33 <td><img src="images/chartview_example_bar.jpg " alt="barchart" /></td>
33 <td><img src="images/chartview_example_bar.jpg " alt="barchart" /></td>
34 <td><img src="images/chartview_example_pie.jpg " alt="piechart" /></td>
34 35 </tr>
35 36 </table>
36 37 </div>
@@ -15,4 +15,5 SUBDIRS += linechart \
15 15 presenterchart \
16 16 chartview \
17 17 scatterinteractions \
18 splinechart
18 splinechart \
19 areachart
@@ -2,6 +2,7
2 2 #include "qchartaxis.h"
3 3 //series
4 4 #include "qlineseries.h"
5 #include "qareaseries.h"
5 6 #include "qbarseries.h"
6 7 #include "qstackedbarseries.h"
7 8 #include "qpercentbarseries.h"
@@ -38,13 +39,13 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
38 39 {
39 40 // TODO: we should check the series not already added
40 41
41 series->setParent(this); // take ownership
42 series->setParent(this);// take ownership
42 43 clearDomains();
43 44
44 45 if(axisY==0) axisY = m_axisY;
45 axisY->setParent(this); // take ownership
46 axisY->setParent(this);// take ownership
46 47
47 QList<QSeries*> seriesList = m_seriesMap.values(axisY);
48 QList<QSeries*> seriesList = m_seriesMap.values(axisY);
48 49
49 50 QList<Domain> domainList = m_domainMap.values(axisY);
50 51
@@ -58,12 +59,12 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
58 59 {
59 60 case QSeries::SeriesTypeLine: {
60 61
61 QLineSeries* xyseries = static_cast<QLineSeries*>(series);
62 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
62 63
63 for (int i = 0; i < xyseries->count(); i++)
64 for (int i = 0; i < lineSeries->count(); i++)
64 65 {
65 qreal x = xyseries->x(i);
66 qreal y = xyseries->y(i);
66 qreal x = lineSeries->x(i);
67 qreal y = lineSeries->y(i);
67 68 domain.m_minX = qMin(domain.m_minX,x);
68 69 domain.m_minY = qMin(domain.m_minY,y);
69 70 domain.m_maxX = qMax(domain.m_maxX,x);
@@ -71,6 +72,45 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
71 72 }
72 73 break;
73 74 }
75 case QSeries::SeriesTypeArea: {
76
77 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
78
79 QLineSeries* upperSeries = areaSeries->upperSeries();
80 QLineSeries* lowerSeries = areaSeries->lowerSeries();
81
82 for (int i = 0; i < upperSeries->count(); i++)
83 {
84 qreal x = upperSeries->x(i);
85 qreal y = upperSeries->y(i);
86 domain.m_minX = qMin(domain.m_minX,x);
87 domain.m_minY = qMin(domain.m_minY,y);
88 domain.m_maxX = qMax(domain.m_maxX,x);
89 domain.m_maxY = qMax(domain.m_maxY,y);
90 }
91 if(lowerSeries){
92 for (int i = 0; i < lowerSeries->count(); i++)
93 {
94 qreal x = lowerSeries->x(i);
95 qreal y = lowerSeries->y(i);
96 domain.m_minX = qMin(domain.m_minX,x);
97 domain.m_minY = qMin(domain.m_minY,y);
98 domain.m_maxX = qMax(domain.m_maxX,x);
99 domain.m_maxY = qMax(domain.m_maxY,y);
100 }}
101 break;
102 }
103 case QSeries::SeriesTypeBar: {
104 qDebug() << "QChartSeries::SeriesTypeBar";
105 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
106 qreal x = barSeries->categoryCount();
107 qreal y = barSeries->max();
108 domain.m_minX = qMin(domain.m_minX,x);
109 domain.m_minY = qMin(domain.m_minY,y);
110 domain.m_maxX = qMax(domain.m_maxX,x);
111 domain.m_maxY = qMax(domain.m_maxY,y);
112 break;
113 }
74 114 case QSeries::SeriesTypeBar: {
75 115 qDebug() << "QChartSeries::SeriesTypeBar";
76 116 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
@@ -173,11 +213,11 void ChartDataSet::removeSeries(QSeries* series)
173 213 {
174 214 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
175 215 foreach(QChartAxis* axis , keys) {
176 if(m_seriesMap.contains(axis,series)){
216 if(m_seriesMap.contains(axis,series)) {
177 217 emit seriesRemoved(series);
178 218 m_seriesMap.remove(axis,series);
179 219 //remove axis if no longer there
180 if(!m_seriesMap.contains(axis)){
220 if(!m_seriesMap.contains(axis)) {
181 221 emit axisRemoved(axis);
182 222 m_domainMap.remove(axis);
183 223 if(axis != m_axisY)
@@ -194,7 +234,7 void ChartDataSet::removeAllSeries()
194 234 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
195 235 foreach(QChartAxis* axis , keys) {
196 236 QList<QSeries*> seriesList = m_seriesMap.values(axis);
197 for(int i =0 ; i < seriesList.size();i++ )
237 for(int i =0; i < seriesList.size();i++ )
198 238 {
199 239 emit seriesRemoved(seriesList.at(i));
200 240 delete(seriesList.at(i));
@@ -280,7 +320,6 void ChartDataSet::setDomain(int index,QChartAxis* axis)
280 320 emit axisRangeChanged(axisX(),labels);
281 321 }
282 322
283
284 323 void ChartDataSet::clearDomains(int toIndex)
285 324 {
286 325 Q_ASSERT(toIndex>=0);
@@ -294,10 +333,10 void ChartDataSet::clearDomains(int toIndex)
294 333 QList<Domain> domains = m_domainMap.values(key);
295 334 m_domainMap.remove(key);
296 335 int i = domains.size() - toIndex - 1;
297 while(i--){
336 while(i--) {
298 337 domains.removeFirst();
299 338 }
300 for(int j=domains.size()-1; j>=0 ;j--)
339 for(int j=domains.size()-1; j>=0;j--)
301 340 m_domainMap.insert(key,domains.at(j));
302 341 }
303 342 }
@@ -313,7 +352,7 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
313 352
314 353 Domain domain;
315 354
316 foreach (QChartAxis* axis , domainList){
355 foreach (QChartAxis* axis , domainList) {
317 356 domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height());
318 357 m_domainMap.insert(axis,domain);
319 358 }
@@ -328,7 +367,7 QChartAxis* ChartDataSet::axisY(QSeries* series) const
328 367 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
329 368
330 369 foreach(QChartAxis* axis , keys) {
331 if(m_seriesMap.contains(axis,series)){
370 if(m_seriesMap.contains(axis,series)) {
332 371 return axis;
333 372 }
334 373 }
@@ -343,19 +382,19 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
343 382
344 383 int ticks = axis->ticksCount()-1;
345 384
346 for(int i=0; i<= ticks; i++){
385 for(int i=0; i<= ticks; i++) {
347 386 qreal value = min + (i * (max - min)/ ticks);
348 387 QString label = axis->axisTickLabel(value);
349 if(label.isEmpty()){
388 if(label.isEmpty()) {
350 389 labels << QString::number(value);
351 }else{
390 }
391 else {
352 392 labels << label;
353 393 }
354 394 }
355 395 return labels;
356 396 }
357 397
358
359 398 void ChartDataSet::handleRangeChanged(QChartAxis* axis)
360 399 {
361 400 qreal min = axis->min();
@@ -399,16 +438,16 void ChartDataSet::handleRangeChanged(QChartAxis* axis)
399 438 setDomain(m_domainIndex,axis);
400 439 }
401 440
402
403 441 }
404 442
405 443 void ChartDataSet::handleTickChanged(QChartAxis* axis)
406 444 {
407 if(axis==axisX()){
445 if(axis==axisX()) {
408 446 Domain domain = m_domainMap.value(axisY());
409 447 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
410 448 emit axisRangeChanged(axis,labels);
411 }else{
449 }
450 else {
412 451 Domain domain = m_domainMap.value(axis);
413 452 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
414 453 emit axisRangeChanged(axis,labels);
@@ -8,12 +8,14
8 8 #include "qstackedbarseries.h"
9 9 #include "qpercentbarseries.h"
10 10 #include "qlineseries.h"
11 #include "qareaseries.h"
11 12 #include "qpieseries.h"
12 13 #include "qscatterseries.h"
13 14 #include "qsplineseries.h"
14 15 //items
15 16 #include "axisitem_p.h"
16 17 #include "axisanimationitem_p.h"
18 #include "areachartitem_p.h"
17 19 #include "barpresenter_p.h"
18 20 #include "stackedbarpresenter_p.h"
19 21 #include "percentbarpresenter_p.h"
@@ -111,6 +113,7 void ChartPresenter::handleSeriesAdded(QSeries* series)
111 113 switch(series->type())
112 114 {
113 115 case QSeries::SeriesTypeLine: {
116
114 117 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
115 118 LineChartItem* item;
116 119 if(m_options.testFlag(QChart::SeriesAnimations)){
@@ -119,14 +122,24 void ChartPresenter::handleSeriesAdded(QSeries* series)
119 122 item = new LineChartItem(this,lineSeries,m_chart);
120 123 }
121 124 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
122 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
123 QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int)));
124 QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int)));
125 QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int)));
126 QObject::connect(lineSeries,SIGNAL(updated()),item,SLOT(handleUpdated()));
127 125 m_chartItems.insert(series,item);
128 126 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
129 item->handleUpdated();
127 break;
128 }
129
130 case QSeries::SeriesTypeArea: {
131
132 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
133 AreaChartItem* item;
134 if(m_options.testFlag(QChart::SeriesAnimations)) {
135 item = new AreaChartItem(this,areaSeries,m_chart);
136 }
137 else {
138 item = new AreaChartItem(this,areaSeries,m_chart);
139 }
140 m_chartTheme->decorate(item,areaSeries,m_chartItems.count());
141 m_chartItems.insert(series,item);
142 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
130 143 break;
131 144 }
132 145
@@ -9,6 +9,7
9 9 #include "qstackedbarseries.h"
10 10 #include "qpercentbarseries.h"
11 11 #include "qlineseries.h"
12 #include "qareaseries.h"
12 13 #include "qscatterseries.h"
13 14 #include "qpieseries.h"
14 15 #include "qpieslice.h"
@@ -20,6 +21,7
20 21 #include "stackedbarpresenter_p.h"
21 22 #include "percentbarpresenter_p.h"
22 23 #include "linechartitem_p.h"
24 #include "areachartitem_p.h"
23 25 #include "scatterpresenter_p.h"
24 26 #include "piepresenter_p.h"
25 27 #include "splinepresenter_p.h"
@@ -61,7 +63,7 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
61 63 {
62 64 switch(theme) {
63 65 case QChart::ChartThemeDefault:
64 return new ChartThemeIcy();
66 return new ChartTheme();
65 67 case QChart::ChartThemeVanilla:
66 68 return new ChartThemeVanilla();
67 69 case QChart::ChartThemeIcy:
@@ -92,6 +94,12 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
92 94 decorate(i,s,count);
93 95 break;
94 96 }
97 case QSeries::SeriesTypeArea: {
98 QAreaSeries* s = static_cast<QAreaSeries*>(series);
99 AreaChartItem* i = static_cast<AreaChartItem*>(item);
100 decorate(i,s,count);
101 break;
102 }
95 103 case QSeries::SeriesTypeBar: {
96 104 QBarSeries* b = static_cast<QBarSeries*>(series);
97 105 BarPresenter* i = static_cast<BarPresenter*>(item);
@@ -131,6 +139,28 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
131 139
132 140 }
133 141
142 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count)
143 {
144 QPen pen;
145 QBrush brush;
146
147 if(pen != series->pen()){
148 item->setPen(series->pen());
149 }else{
150 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
151 pen.setWidthF(2);
152 item->setPen(pen);
153 }
154
155 if(brush != series->brush()){
156 item->setBrush(series->brush());
157 }else{
158 QBrush brush(m_seriesColor.at(count%m_seriesColor.size()));
159 item->setBrush(brush);
160 }
161 }
162
163
134 164 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
135 165 {
136 166 QPen pen;
@@ -23,6 +23,8 class PiePresenter;
23 23 class QPieSeries;
24 24 class SplinePresenter;
25 25 class QSplineSeries;
26 class AreaChartItem;
27 class QAreaSeries;
26 28
27 29 class ChartTheme
28 30 {
@@ -36,7 +38,8 public:
36 38 void decorate(BarPresenter* item, QBarSeries* series,int count);
37 39 void decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count);
38 40 void decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count);
39 void decorate(LineChartItem* item, QLineSeries*, int count);
41 void decorate(LineChartItem* item, QLineSeries* series,int count);
42 void decorate(AreaChartItem* item, QAreaSeries* series,int count);
40 43 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
41 44 void decorate(PiePresenter* item, QPieSeries* series, int count);
42 45 void decorate(QChartAxis* axis,AxisItem* item);
@@ -15,6 +15,12 m_items(this)
15 15 {
16 16 //m_items.setZValue(ChartPresenter::LineChartZValue);
17 17 setZValue(ChartPresenter::LineChartZValue);
18
19 QObject::connect(presenter,SIGNAL(geometryChanged(const QRectF&)),this,SLOT(handleGeometryChanged(const QRectF&)));
20 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
21 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
22 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
23 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18 24 }
19 25
20 26 QRectF LineChartItem::boundingRect() const
@@ -29,6 +29,7 PUBLIC_HEADERS += qchart.h \
29 29 qseries.h \
30 30 qchartview.h
31 31 include(linechart/linechart.pri)
32 include(areachart/areachart.pri)
32 33 include(barchart/barchart.pri)
33 34 include(piechart/piechart.pri)
34 35 include(scatterseries/scatter.pri)
General Comments 0
You need to be logged in to leave comments. Login now