##// END OF EJS Templates
Refactora axis and line chart to use graphics items insted of painter.
Michal Klocek -
r85:f77f13ee265a
parent child
Show More
@@ -16,9 +16,13 int main(int argc, char *argv[])
16 16 QMainWindow window;
17 17
18 18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
19 QPen blue(Qt::blue);
20 blue.setWidth(3);
21 series0->setPen(blue);
20 22 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
23 QPen red(Qt::red);
24 red.setWidth(3);
25 series1->setPen(red);
22 26
23 27 int numPoints = 100;
24 28
@@ -30,7 +34,7 int main(int argc, char *argv[])
30 34 QChartView* chartView = new QChartView(&window);
31 35 chartView->addSeries(series0);
32 36 chartView->addSeries(series1);
33 chartView->setBackgroundColor(Qt::yellow);
37 //chartView->setBackgroundColor(Qt::yellow);
34 38
35 39 window.setCentralWidget(chartView);
36 40 window.resize(400, 300);
@@ -16,9 +16,13 int main(int argc, char *argv[])
16 16 QMainWindow window;
17 17
18 18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
19 QPen blue(Qt::blue);
20 blue.setWidth(3);
21 series0->setPen(blue);
20 22 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
23 QPen red(Qt::red);
24 red.setWidth(3);
25 series1->setPen(red);
22 26
23 27 int numPoints = 100;
24 28
@@ -28,6 +32,7 int main(int argc, char *argv[])
28 32 }
29 33
30 34 QChartView* chartView = new QChartView(&window);
35 chartView->setRenderHint(QPainter::Antialiasing);
31 36 chartView->addSeries(series0);
32 37 chartView->addSeries(series1);
33 38
@@ -15,9 +15,13 int main(int argc, char *argv[])
15 15 QMainWindow window;
16 16
17 17 QXYChartSeries* series0 = QXYChartSeries::create();
18 series0->setColor(Qt::blue);
18 QPen blue(Qt::blue);
19 blue.setWidth(3);
20 series0->setPen(blue);
19 21 QXYChartSeries* series1 = QXYChartSeries::create();
20 series1->setColor(Qt::red);
22 QPen red(Qt::red);
23 red.setWidth(3);
24 series1->setPen(red);
21 25
22 26 int numPoints = 100;
23 27
@@ -2,6 +2,8
2 2 #include <QPainter>
3 3 #include <QDebug>
4 4
5 #define LABEL_PADDING 5
6
5 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 8
7 9 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent): ChartItem(parent),
@@ -17,6 +19,7 AxisItem::~AxisItem()
17 19 void AxisItem::setSize(const QSize& size)
18 20 {
19 21 m_rect = QRectF(QPoint(0,0),size);
22 createItems();
20 23 }
21 24
22 25 void AxisItem::setLength(int length)
@@ -42,52 +45,124 QRectF AxisItem::boundingRect() const
42 45 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
43 46 {
44 47 m_plotDomain = plotDomain;
48 createItems();
45 49 }
46
50 /*
47 51 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
48 52 {
49 53 if (!m_rect.isValid())
50 return;
54 return;
51 55
52 56 if(m_type==X_AXIS) {
53 57
54 const qreal deltaX = (m_rect.width() -1) / m_ticks;
58 const qreal deltaX = m_rect.width() / m_ticks;
59
60 for (int i = 0; i <= m_ticks; ++i) {
61
62 int x = i * deltaX + m_rect.left();
55 63
56 for (int i = 0; i <= m_ticks; ++i) {
64 if(i==0) x--;
65 if(i==m_ticks) x++;
57 66
58 int x = i * deltaX + m_rect.left();
59 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
60 / m_ticks);
61 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
62 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
67 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
68 / m_ticks);
69 painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1);
70 // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5);
63 71
64 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
65 Qt::AlignHCenter | Qt::AlignTop,
66 QString::number(label));
67 }
72 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label));
73 }
68 74 }
69 75
70 76 if(m_type==Y_AXIS) {
71 77
72 const qreal deltaY = (m_rect.height() - 1) / m_ticks;
78 const qreal deltaY = (m_rect.height()) / m_ticks;
73 79
74 for (int j = 0; j <= m_ticks; ++j) {
80 for (int j = 0; j <= m_ticks; ++j) {
75 81
76 int y = j * -deltaY + m_rect.bottom();
77 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
78 / m_ticks);
82 int y = j * -deltaY + m_rect.bottom();
79 83
80 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
81 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
82 //TODO : margin = 50 ;
83 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
84 Qt::AlignRight | Qt::AlignVCenter,
85 QString::number(label));
86 }
84 if(j==0) y++;
85 if(j==m_ticks) y--;
86
87 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
88 / m_ticks);
89
90 painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y);
91 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
92 //TODO : margin = 50 ;
93 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
94 Qt::AlignRight | Qt::AlignVCenter,
95 QString::number(label));
96 }
87 97 }
88 98
89 99 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
100 }
101 */
102 void AxisItem::createItems()
103 {
104
105 //TODO: this is very inefficient handling
106
107 qDeleteAll(m_shades);
108 m_shades.clear();
109 qDeleteAll(m_grid);
110 m_grid.clear();
111 qDeleteAll(m_labels);
112 m_labels.clear();
113
114
115 if(m_type==X_AXIS) {
116
117 const qreal deltaX = m_rect.width() / m_ticks;
118
119 for (int i = 0; i <= m_ticks; ++i) {
120
121 int x = i * deltaX + m_rect.left();
122
123 //last grid outside chart rect
124 if(i==0) x--;
125 if(i==m_ticks) x++;
126
127 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
128
129 m_grid<<new QGraphicsLineItem(x, m_rect.top()-1, x, m_rect.bottom()+1+LABEL_PADDING,this);
130
131 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
132 QPointF center = text->boundingRect().center();
133 text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING);
134 //text->rotate(-45);
135 m_labels<<text;
136 }
137 }
138
139 if(m_type==Y_AXIS) {
140
141 const qreal deltaY = m_rect.height()/ m_ticks;
142
143 for (int j = 0; j <= m_ticks; ++j) {
144
145 int y = j * -deltaY + m_rect.bottom();
146
147 //last grid outside chart rect
148 if(j==0) y++;
149 if(j==m_ticks) y--;
150
151 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
152 / m_ticks);
153
154 m_grid<<new QGraphicsLineItem(m_rect.left()- 1 - LABEL_PADDING , y, m_rect.right()+1, y,this);
155 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
156 QPointF center = text->boundingRect().center();
157
158 text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y());
159 //text->rotate(-45);
160 m_labels<<text;
161
162 }
163 }
90 164
165 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
91 166 }
92 167
93 168 //TODO "nice numbers algorithm"
@@ -17,7 +17,7 public:
17 17
18 18 //from QGraphicsItem
19 19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21 21
22 22 //from ChartItem
23 23 void setSize(const QSize& size);
@@ -28,10 +28,16 public:
28 28 AxisType axisType() const {return m_type;};
29 29
30 30 private:
31 void createItems();
32 private:
31 33 QRectF m_rect;
32 34 int m_ticks;
33 35 PlotDomain m_plotDomain;
34 36 QPainterPath m_path;
37
38 QList<QGraphicsLineItem*> m_grid;
39 QList<QGraphicsRectItem*> m_shades;
40 QList<QGraphicsSimpleTextItem*> m_labels;
35 41 AxisType m_type;
36 42 };
37 43
@@ -4,7 +4,7
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qxychartseries.h"
7
7 #include "qchartaxis.h"
8 8 #include "barchartseries.h"
9 9 #include "bargroup.h"
10 10
@@ -17,21 +17,21
17 17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 18
19 19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 m_background(new QGraphicsRectItem(this)),
20 m_background(new QGraphicsRectItem()),
21 21 m_title(new QGraphicsTextItem(this)),
22 22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
24 23 m_plotDataIndex(0),
25 24 m_marginSize(0)
26 25 {
27 26 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
27 //setTheme(QChart::ChartThemeVanilla);
28 m_backgroundGradient.setColorAt(0.0, Qt::white);
29
30 30 PlotDomain domain;
31 31 m_plotDomainList<<domain;
32
32 m_axisY << new AxisItem(AxisItem::Y_AXIS,this);
33 33 m_chartItems<<m_axisX;
34 m_chartItems<<m_axisY;
34 m_chartItems<<m_axisY.at(0);
35 35 }
36 36
37 37 QChart::~QChart(){}
@@ -53,8 +53,9 void QChart::addSeries(QChartSeries* series)
53 53
54 54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
55 55 // Use color defined by theme in case the series does not define a custom color
56 if (!xyseries->color().isValid() && m_themeColors.count())
57 xyseries->setColor(nextColor());
56
57 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
58 xyseries->setPen(nextColor());
58 59
59 60 m_plotDataIndex = 0 ;
60 61 m_plotDomainList.resize(1);
@@ -168,6 +169,7 void QChart::setSize(const QSize& size)
168 169 m_background->setRect(rect);
169 170 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
170 171 m_background->setBrush(m_backgroundGradient);
172 m_background->setPen(Qt::NoPen);
171 173
172 174 //resize elements
173 175 foreach (ChartItem* item ,m_chartItems) {
@@ -271,7 +273,7 void QChart::setTheme(QChart::ChartThemeId theme)
271 273 // TODO: other series interested on themes?
272 274 if (series->type() == QChartSeries::SeriesTypeLine) {
273 275 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
274 lineseries->setColor(nextColor());
276 lineseries->setPen(nextColor());
275 277 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
276 278 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
277 279 scatter->setMarkerColor(nextColor());
@@ -340,6 +342,25 void QChart::zoomOut()
340 342 }
341 343 }
342 344
345 void QChart::setAxisX(const QChartAxis& axis)
346 {
347 setAxis(m_axisX,axis);
348 }
349 void QChart::setAxisY(const QChartAxis& axis)
350 {
351 setAxis(m_axisY.at(0),axis);
352 }
353
354 void QChart::setAxisY(const QList<QChartAxis>& axis)
355 {
356 //TODO not implemented
357 }
358
359 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
360 {
361 item->setVisible(axis.isAxisVisible());
362 }
363
343 364 #include "moc_qchart.cpp"
344 365
345 366 QTCOMMERCIALCHART_END_NAMESPACE
@@ -62,10 +62,12 public:
62 62 void zoomIn();
63 63 void zoomOut();
64 64
65 void setAxisX(QChartAxis* axis){};
66 void setAxisY(QChartAxis* axis){};
67 void setAxisY(QList<QChartAxis*> axis){};
65 void setAxisX(const QChartAxis& axis);
66 void setAxisY(const QChartAxis& axis);
67 void setAxisY(const QList<QChartAxis>& axis);
68 68
69 private:
70 void setAxis(AxisItem *item, const QChartAxis& axis);
69 71
70 72 signals:
71 73 //TODO chage to const QSize& size
@@ -80,7 +82,7 private:
80 82 QLinearGradient m_backgroundGradient;
81 83 QGraphicsTextItem* m_title;
82 84 AxisItem* m_axisX;
83 AxisItem* m_axisY;
85 QList<AxisItem*> m_axisY;
84 86 QRect m_rect;
85 87 QList<QChartSeries*> m_chartSeries;
86 88 QVector<PlotDomain> m_plotDomainList;
@@ -1,7 +1,13
1 1
2 2 #include "qchartaxis.h"
3 3
4 QChartAxis::QChartAxis()
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6 QChartAxis::QChartAxis():
7 m_axisVisible(true),
8 m_girdVisible(true),
9 m_labelsVisible(true),
10 m_rowShadesVisible(true)
5 11 {
6 12 // TODO Auto-generated constructor stub
7 13
@@ -12,3 +18,26 QChartAxis::~QChartAxis()
12 18 // TODO Auto-generated destructor stub
13 19 }
14 20
21 void QChartAxis::setAxisVisible(bool visible)
22 {
23 m_axisVisible=visible;
24 }
25
26 void QChartAxis::setGridVisible(bool visible)
27 {
28 m_girdVisible=visible;
29 }
30
31 void QChartAxis::setLabelsVisible(bool visible)
32 {
33 m_labelsVisible=visible;
34 }
35
36 void QChartAxis::setRowShadesVisible(bool visible)
37 {
38 m_rowShadesVisible=visible;
39 }
40
41
42
43 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,11 +1,31
1 1 #ifndef QCHARTAXIS_H_
2 2 #define QCHARTAXIS_H_
3 3
4 #include <qchartglobal.h>
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
4 8 class QChartAxis
5 9 {
6 10 public:
7 11 QChartAxis();
8 12 virtual ~QChartAxis();
13
14 bool isAxisVisible() const { return m_axisVisible;};
15 void setAxisVisible(bool visible);
16 bool isGridVisible() const { return m_girdVisible;};
17 void setGridVisible(bool visible);
18 bool isLabelsVisible() const { return m_labelsVisible;};
19 void setLabelsVisible(bool visible);
20 bool isRowShadesVisible() const { return m_rowShadesVisible;};
21 void setRowShadesVisible(bool visible);
22
23 private:
24 bool m_axisVisible;
25 bool m_girdVisible;
26 bool m_labelsVisible;
27 bool m_rowShadesVisible;
9 28 };
10 29
30 QTCOMMERCIALCHART_END_NAMESPACE
11 31 #endif /* QCHARTAXIS_H_ */
@@ -2,8 +2,7
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent),
6 m_color()
5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent)
7 6 {
8 7 }
9 8
@@ -18,11 +17,6 QXYChartSeries* QXYChartSeries::create(QObject* parent)
18 17 return new QXYChartSeries(parent);
19 18 }
20 19
21 void QXYChartSeries::setColor(const QColor& color)
22 {
23 m_color = color;
24 }
25
26 20 void QXYChartSeries::add(qreal x,qreal y)
27 21 {
28 22 m_x<<x;
@@ -53,6 +47,16 int QXYChartSeries::count() const
53 47
54 48 }
55 49
50 void QXYChartSeries::setPen(const QPen& pen)
51 {
52 m_pen=pen;
53 }
54
55 void QXYChartSeries::setBrush(const QBrush& brush)
56 {
57 m_brush=brush;
58 }
59
56 60 QDebug operator<< (QDebug debug, const QXYChartSeries series)
57 61 {
58 62 Q_ASSERT(series.m_x.size() == series.m_y.size());
@@ -4,7 +4,8
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include <QDebug>
7 #include <QColor>
7 #include <QPen>
8 #include <QBrush>
8 9
9 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 11
@@ -23,17 +24,22 public:
23 24
24 25 void add(qreal x, qreal y);
25 26 void clear();
26 void setColor(const QColor& color);
27 const QColor& color() const { return m_color;}
27
28 void setPen(const QPen& pen);
29 const QPen& pen() const { return m_pen;}
30 void setBrush(const QBrush& brush);
31 const QBrush& brush() const { return m_brush;}
32
28 33 int count() const;
29 34 qreal x(int pos) const;
30 35 qreal y(int pos) const;
31 36 friend QDebug operator<< (QDebug d, const QXYChartSeries series);
32 37
33 38 private:
34 QColor m_color;
35 39 QList<qreal> m_x;
36 40 QList<qreal> m_y;
41 QBrush m_brush;
42 QPen m_pen;
37 43
38 44 };
39 45
@@ -9,59 +9,59 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 11 m_series(series),
12 m_dirty(false)
12 m_pathItem(new QGraphicsPathItem(this))
13 13 {
14
14 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
15 15 }
16 16
17 17 void XYLineChartItem::setSize(const QSize& size)
18 18 {
19 19 m_rect=QRect(0,0,size.width(),size.height());
20 m_dirty=true;
20 prepareGeometryChange();
21 updateGeometry();
22
21 23 }
22 24
23 25 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
24 26 {
25 27 m_plotDomain=data;
26 m_dirty=true;
28 prepareGeometryChange();
29 updateGeometry();
30
27 31 }
28 32
29 33 QRectF XYLineChartItem::boundingRect() const
30 34 {
31 return m_polyline.boundingRect();
35 return m_rect;
32 36 }
33
37 /*
38 QPainterPath XYLineChartItem::shape() const
39 {
40 return m_pathItem->shape();
41 }
42 */
34 43 void XYLineChartItem::updateGeometry()
35 44 {
36 45 if (!m_rect.isValid()) return;
37 46
38 const qreal deltaX = (m_rect.width()-1)/m_plotDomain.spanX();
39 const qreal deltaY = (m_rect.height()-1)/m_plotDomain.spanY();
47 const qreal deltaX = m_rect.width()/m_plotDomain.spanX();
48 const qreal deltaY = m_rect.height()/m_plotDomain.spanY();
40 49
41 m_polyline.clear();
42 m_polyline.resize(m_series->count());
50 QPainterPath path;
43 51
44 52 for (int j = 0; j < m_series->count(); ++j) {
45 53 qreal dx = m_series->x(j) - m_plotDomain.m_minX;
46 54 qreal dy = m_series->y(j) - m_plotDomain.m_minY;
47 55 qreal x = (dx * deltaX) + m_rect.left();
48 56 qreal y = - (dy * deltaY) + m_rect.bottom();
49 m_polyline[j] = QPointF(x, y);
57 if(j==0) path.moveTo(x,y);
58 else path.lineTo(x,y);
50 59 }
51 60
61 m_pathItem->setPath(path);
62 m_pathItem->setPen(m_series->pen());
63 m_pathItem->setBrush(m_series->brush());
52 64 }
53 65
54 66
55 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
56 {
57 //TODO: remove it
58 if (m_dirty) {
59 updateGeometry();
60 m_dirty=false;
61 }
62 painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1));
63 painter->setPen(m_series->color());
64 painter->drawPolyline(m_polyline);
65 }
66
67 67 QTCOMMERCIALCHART_END_NAMESPACE
@@ -17,7 +17,8 public:
17 17
18 18 //from QGraphicsItem
19 19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21 // virtual QPainterPath shape() const;
21 22 //from ChartItem
22 23 void setSize(const QSize& size);
23 24 void setPlotDomain(const PlotDomain& data);
@@ -30,7 +31,7 private:
30 31 QPolygonF m_polyline;
31 32 QXYChartSeries* m_series;
32 33 PlotDomain m_plotDomain;
33 bool m_dirty;
34 QGraphicsPathItem *m_pathItem;
34 35 };
35 36
36 37 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now