@@ -0,0 +1,22 | |||||
|
1 | #ifndef CHARTITEM_H_ | |||
|
2 | #define CHARTITEM_H_ | |||
|
3 | ||||
|
4 | #include "plotdomain_p.h" | |||
|
5 | #include "chartobjectinterface_p.h" | |||
|
6 | #include <QGraphicsItem> | |||
|
7 | ||||
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
9 | ||||
|
10 | class ChartItem : public QGraphicsItem, public ChartObjectInterface | |||
|
11 | { | |||
|
12 | enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM}; | |||
|
13 | public: | |||
|
14 | ChartItem(QGraphicsItem* parent = 0):QGraphicsItem(parent){}; | |||
|
15 | virtual ~ChartItem(){}; | |||
|
16 | // TODO: this is a hack; integration ongoing: | |||
|
17 | QGraphicsItem *graphicsItem() { return this; } | |||
|
18 | }; | |||
|
19 | ||||
|
20 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
21 | ||||
|
22 | #endif /* CHARTITEM_H_ */ |
@@ -1,176 +1,171 | |||||
1 | #include "axisitem_p.h" |
|
1 | #include "axisitem_p.h" | |
2 | #include <QPainter> |
|
2 | #include <QPainter> | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | #define LABEL_PADDING 5 |
|
5 | #define LABEL_PADDING 5 | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : |
|
9 | AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : | |
10 |
|
|
10 | ChartItem(parent), | |
11 | m_ticks(4), |
|
11 | m_ticks(4), | |
12 | m_type(type) |
|
12 | m_type(type) | |
13 | { |
|
13 | { | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | AxisItem::~AxisItem() |
|
16 | AxisItem::~AxisItem() | |
17 | { |
|
17 | { | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | void AxisItem::setLength(int length) |
|
20 | void AxisItem::setLength(int length) | |
21 | { |
|
21 | { | |
22 | QPainterPath path; |
|
22 | QPainterPath path; | |
23 | path.moveTo(QPointF(0,0)); |
|
23 | path.moveTo(QPointF(0,0)); | |
24 | path.lineTo(length,0); |
|
24 | path.lineTo(length,0); | |
25 | // path.lineTo(length-4,0); |
|
25 | // path.lineTo(length-4,0); | |
26 | // path.lineTo(length,3); |
|
26 | // path.lineTo(length,3); | |
27 | // path.lineTo(length-4,6); |
|
27 | // path.lineTo(length-4,6); | |
28 | // path.lineTo(length-4,4); |
|
28 | // path.lineTo(length-4,4); | |
29 | // path.lineTo(0,4); |
|
29 | // path.lineTo(0,4); | |
30 | // path.lineTo(0,2); |
|
30 | // path.lineTo(0,2); | |
31 | m_path=path; |
|
31 | m_path=path; | |
32 | update(); |
|
32 | update(); | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | QRectF AxisItem::boundingRect() const |
|
35 | QRectF AxisItem::boundingRect() const | |
36 | { |
|
36 | { | |
37 | return m_rect; |
|
37 | return m_rect; | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | void AxisItem::setPlotDomain(const PlotDomain& plotDomain) |
|
40 | void AxisItem::setPlotDomain(const PlotDomain& plotDomain) | |
41 | { |
|
41 | { | |
42 | m_plotDomain = plotDomain; |
|
42 | m_plotDomain = plotDomain; | |
43 | createItems(); |
|
43 | createItems(); | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 |
void AxisItem::set |
|
46 | void AxisItem::setSize(const QSize &size) | |
47 | { |
|
|||
48 | QGraphicsItem::setPos(pos); |
|
|||
49 | } |
|
|||
50 |
|
||||
51 | void AxisItem::resize(const QSize &size) |
|
|||
52 | { |
|
47 | { | |
53 | m_rect = QRectF(QPoint(0,0),size); |
|
48 | m_rect = QRectF(QPoint(0,0),size); | |
54 | createItems(); |
|
49 | createItems(); | |
55 | } |
|
50 | } | |
56 |
|
51 | |||
57 | void AxisItem::setTheme(ChartTheme *theme) |
|
52 | void AxisItem::setTheme(ChartTheme *theme) | |
58 | { |
|
53 | { | |
59 | if (theme) { |
|
54 | if (theme) { | |
60 | // TODO: add axis related properties to the theme class and use them here |
|
55 | // TODO: add axis related properties to the theme class and use them here | |
61 | } |
|
56 | } | |
62 | } |
|
57 | } | |
63 |
|
58 | |||
64 | /* |
|
59 | /* | |
65 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) |
|
60 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) | |
66 | { |
|
61 | { | |
67 | if (!m_rect.isValid()) |
|
62 | if (!m_rect.isValid()) | |
68 | return; |
|
63 | return; | |
69 |
|
64 | |||
70 | if(m_type==X_AXIS) { |
|
65 | if(m_type==X_AXIS) { | |
71 |
|
66 | |||
72 | const qreal deltaX = m_rect.width() / m_ticks; |
|
67 | const qreal deltaX = m_rect.width() / m_ticks; | |
73 |
|
68 | |||
74 | for (int i = 0; i <= m_ticks; ++i) { |
|
69 | for (int i = 0; i <= m_ticks; ++i) { | |
75 |
|
70 | |||
76 | int x = i * deltaX + m_rect.left(); |
|
71 | int x = i * deltaX + m_rect.left(); | |
77 |
|
72 | |||
78 | if(i==0) x--; |
|
73 | if(i==0) x--; | |
79 | if(i==m_ticks) x++; |
|
74 | if(i==m_ticks) x++; | |
80 |
|
75 | |||
81 | qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX() |
|
76 | qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX() | |
82 | / m_ticks); |
|
77 | / m_ticks); | |
83 | painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1); |
|
78 | painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1); | |
84 | // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5); |
|
79 | // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5); | |
85 |
|
80 | |||
86 | painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label)); |
|
81 | painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label)); | |
87 | } |
|
82 | } | |
88 | } |
|
83 | } | |
89 |
|
84 | |||
90 | if(m_type==Y_AXIS) { |
|
85 | if(m_type==Y_AXIS) { | |
91 |
|
86 | |||
92 | const qreal deltaY = (m_rect.height()) / m_ticks; |
|
87 | const qreal deltaY = (m_rect.height()) / m_ticks; | |
93 |
|
88 | |||
94 | for (int j = 0; j <= m_ticks; ++j) { |
|
89 | for (int j = 0; j <= m_ticks; ++j) { | |
95 |
|
90 | |||
96 | int y = j * -deltaY + m_rect.bottom(); |
|
91 | int y = j * -deltaY + m_rect.bottom(); | |
97 |
|
92 | |||
98 | if(j==0) y++; |
|
93 | if(j==0) y++; | |
99 | if(j==m_ticks) y--; |
|
94 | if(j==m_ticks) y--; | |
100 |
|
95 | |||
101 | qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() |
|
96 | qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() | |
102 | / m_ticks); |
|
97 | / m_ticks); | |
103 |
|
98 | |||
104 | painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y); |
|
99 | painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y); | |
105 | //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y); |
|
100 | //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y); | |
106 | //TODO : margin = 50 ; |
|
101 | //TODO : margin = 50 ; | |
107 | painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20, |
|
102 | painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20, | |
108 | Qt::AlignRight | Qt::AlignVCenter, |
|
103 | Qt::AlignRight | Qt::AlignVCenter, | |
109 | QString::number(label)); |
|
104 | QString::number(label)); | |
110 | } |
|
105 | } | |
111 | } |
|
106 | } | |
112 |
|
107 | |||
113 | //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); |
|
108 | //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); | |
114 | } |
|
109 | } | |
115 | */ |
|
110 | */ | |
116 | void AxisItem::createItems() |
|
111 | void AxisItem::createItems() | |
117 | { |
|
112 | { | |
118 |
|
113 | |||
119 | //TODO: this is very inefficient handling |
|
114 | //TODO: this is very inefficient handling | |
120 |
|
115 | |||
121 | qDeleteAll(m_shades); |
|
116 | qDeleteAll(m_shades); | |
122 | m_shades.clear(); |
|
117 | m_shades.clear(); | |
123 | qDeleteAll(m_grid); |
|
118 | qDeleteAll(m_grid); | |
124 | m_grid.clear(); |
|
119 | m_grid.clear(); | |
125 | qDeleteAll(m_labels); |
|
120 | qDeleteAll(m_labels); | |
126 | m_labels.clear(); |
|
121 | m_labels.clear(); | |
127 |
|
122 | |||
128 |
|
123 | |||
129 | if(m_type==X_AXIS) { |
|
124 | if(m_type==X_AXIS) { | |
130 |
|
125 | |||
131 | const qreal deltaX = m_rect.width() / m_ticks; |
|
126 | const qreal deltaX = m_rect.width() / m_ticks; | |
132 |
|
127 | |||
133 | for (int i = 0; i <= m_ticks; ++i) { |
|
128 | for (int i = 0; i <= m_ticks; ++i) { | |
134 |
|
129 | |||
135 | int x = i * deltaX + m_rect.left(); |
|
130 | int x = i * deltaX + m_rect.left(); | |
136 |
|
131 | |||
137 | qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks); |
|
132 | qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks); | |
138 |
|
133 | |||
139 | m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this); |
|
134 | m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this); | |
140 |
|
135 | |||
141 | QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this); |
|
136 | QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this); | |
142 | QPointF center = text->boundingRect().center(); |
|
137 | QPointF center = text->boundingRect().center(); | |
143 | text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING); |
|
138 | text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING); | |
144 | //text->rotate(-45); |
|
139 | //text->rotate(-45); | |
145 | m_labels<<text; |
|
140 | m_labels<<text; | |
146 | } |
|
141 | } | |
147 | } |
|
142 | } | |
148 |
|
143 | |||
149 | if(m_type==Y_AXIS) { |
|
144 | if(m_type==Y_AXIS) { | |
150 |
|
145 | |||
151 | const qreal deltaY = m_rect.height()/ m_ticks; |
|
146 | const qreal deltaY = m_rect.height()/ m_ticks; | |
152 |
|
147 | |||
153 | for (int j = 0; j <= m_ticks; ++j) { |
|
148 | for (int j = 0; j <= m_ticks; ++j) { | |
154 |
|
149 | |||
155 | int y = j * -deltaY + m_rect.bottom(); |
|
150 | int y = j * -deltaY + m_rect.bottom(); | |
156 |
|
151 | |||
157 | qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() |
|
152 | qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() | |
158 | / m_ticks); |
|
153 | / m_ticks); | |
159 |
|
154 | |||
160 | m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this); |
|
155 | m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this); | |
161 | QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this); |
|
156 | QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this); | |
162 | QPointF center = text->boundingRect().center(); |
|
157 | QPointF center = text->boundingRect().center(); | |
163 |
|
158 | |||
164 | text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y()); |
|
159 | text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y()); | |
165 | //text->rotate(-45); |
|
160 | //text->rotate(-45); | |
166 | m_labels<<text; |
|
161 | m_labels<<text; | |
167 |
|
162 | |||
168 | } |
|
163 | } | |
169 | } |
|
164 | } | |
170 |
|
165 | |||
171 | //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); |
|
166 | //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); | |
172 | } |
|
167 | } | |
173 |
|
168 | |||
174 | //TODO "nice numbers algorithm" |
|
169 | //TODO "nice numbers algorithm" | |
175 |
|
170 | |||
176 | QTCOMMERCIALCHART_END_NAMESPACE |
|
171 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,49 +1,48 | |||||
1 | #ifndef AXISITEM_H_ |
|
1 | #ifndef AXISITEM_H_ | |
2 | #define AXISITEM_H_ |
|
2 | #define AXISITEM_H_ | |
3 |
|
3 | |||
4 | #include "plotdomain_p.h" |
|
4 | #include "plotdomain_p.h" | |
5 |
#include "chartitem |
|
5 | #include "chartitem_p.h" | |
6 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 |
class AxisItem: public |
|
10 | class AxisItem : public ChartItem | |
11 | { |
|
11 | { | |
12 | public: |
|
12 | public: | |
13 | enum AxisType{X_AXIS,Y_AXIS}; |
|
13 | enum AxisType{X_AXIS,Y_AXIS}; | |
14 |
|
14 | |||
15 | AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); |
|
15 | AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); | |
16 | ~AxisItem(); |
|
16 | ~AxisItem(); | |
17 |
|
17 | |||
18 | //from QGraphicsItem |
|
18 | //from QGraphicsItem | |
19 | QRectF boundingRect() const; |
|
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 |
public: // from Chart |
|
22 | public: // from ChartObjectInterface | |
23 |
void set |
|
23 | void setSize(const QSize &size); | |
24 | void resize(const QSize &size); |
|
|||
25 | void setTheme(ChartTheme *theme); |
|
24 | void setTheme(ChartTheme *theme); | |
26 | void setPlotDomain(const PlotDomain& data); |
|
25 | void setPlotDomain(const PlotDomain& data); | |
27 |
|
26 | |||
28 | public: |
|
27 | public: | |
29 | void setLength(int length); |
|
28 | void setLength(int length); | |
30 | void setWidth(int width); |
|
29 | void setWidth(int width); | |
31 | AxisType axisType() const {return m_type;}; |
|
30 | AxisType axisType() const {return m_type;}; | |
32 |
|
31 | |||
33 | private: |
|
32 | private: | |
34 | void createItems(); |
|
33 | void createItems(); | |
35 | private: |
|
34 | private: | |
36 | QRectF m_rect; |
|
35 | QRectF m_rect; | |
37 | int m_ticks; |
|
36 | int m_ticks; | |
38 | PlotDomain m_plotDomain; |
|
37 | PlotDomain m_plotDomain; | |
39 | QPainterPath m_path; |
|
38 | QPainterPath m_path; | |
40 |
|
39 | |||
41 | QList<QGraphicsLineItem*> m_grid; |
|
40 | QList<QGraphicsLineItem*> m_grid; | |
42 | QList<QGraphicsRectItem*> m_shades; |
|
41 | QList<QGraphicsRectItem*> m_shades; | |
43 | QList<QGraphicsSimpleTextItem*> m_labels; |
|
42 | QList<QGraphicsSimpleTextItem*> m_labels; | |
44 | AxisType m_type; |
|
43 | AxisType m_type; | |
45 | }; |
|
44 | }; | |
46 |
|
45 | |||
47 | QTCOMMERCIALCHART_END_NAMESPACE |
|
46 | QTCOMMERCIALCHART_END_NAMESPACE | |
48 |
|
47 | |||
49 | #endif /* AXISITEM_H_ */ |
|
48 | #endif /* AXISITEM_H_ */ |
@@ -1,68 +1,63 | |||||
1 | #include "bar.h" |
|
1 | #include "bar.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QPainter> |
|
3 | #include <QPainter> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | Bar::Bar(QGraphicsItem *parent) |
|
7 | Bar::Bar(QGraphicsItem *parent) | |
8 |
: |
|
8 | : ChartItem(parent) | |
9 | { |
|
9 | { | |
10 | } |
|
10 | } | |
11 |
|
11 | |||
12 |
void Bar::set |
|
12 | void Bar::setSize(const QSize& size) | |
13 | { |
|
|||
14 | QGraphicsItem::setPos(pos); |
|
|||
15 | } |
|
|||
16 |
|
||||
17 | void Bar::resize(const QSize& size) |
|
|||
18 | { |
|
13 | { | |
19 | mWidth = size.width(); |
|
14 | mWidth = size.width(); | |
20 | mHeight = size.height(); |
|
15 | mHeight = size.height(); | |
21 | } |
|
16 | } | |
22 |
|
17 | |||
23 | void Bar::setPlotDomain(const PlotDomain& data) |
|
18 | void Bar::setPlotDomain(const PlotDomain& data) | |
24 | { |
|
19 | { | |
25 | mPlotDomain = data; |
|
20 | mPlotDomain = data; | |
26 | } |
|
21 | } | |
27 |
|
22 | |||
28 | void Bar::setTheme(ChartTheme *theme) |
|
23 | void Bar::setTheme(ChartTheme *theme) | |
29 | { |
|
24 | { | |
30 | // TODO |
|
25 | // TODO | |
31 | } |
|
26 | } | |
32 |
|
27 | |||
33 | void Bar::resize( int w, int h ) |
|
28 | void Bar::resize( int w, int h ) | |
34 | { |
|
29 | { | |
35 | // qDebug() << "bar::resize" << w << h; |
|
30 | // qDebug() << "bar::resize" << w << h; | |
36 | mWidth = w; |
|
31 | mWidth = w; | |
37 | mHeight = h; |
|
32 | mHeight = h; | |
38 | } |
|
33 | } | |
39 |
|
34 | |||
40 | void Bar::setColor( QColor col ) |
|
35 | void Bar::setColor( QColor col ) | |
41 | { |
|
36 | { | |
42 | mColor = col; |
|
37 | mColor = col; | |
43 | } |
|
38 | } | |
44 | void Bar::setPos(qreal x, qreal y) |
|
39 | void Bar::setPos(qreal x, qreal y) | |
45 | { |
|
40 | { | |
46 | // qDebug() << "Bar::setpos" << x << y; |
|
41 | // qDebug() << "Bar::setpos" << x << y; | |
47 | mXpos = x; |
|
42 | mXpos = x; | |
48 | mYpos = y; |
|
43 | mYpos = y; | |
49 | } |
|
44 | } | |
50 |
|
45 | |||
51 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
46 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
52 | { |
|
47 | { | |
53 | if (0 == mHeight) { |
|
48 | if (0 == mHeight) { | |
54 | return; |
|
49 | return; | |
55 | } |
|
50 | } | |
56 | // TODO: accept brush instead of color |
|
51 | // TODO: accept brush instead of color | |
57 | QBrush brush(mColor); |
|
52 | QBrush brush(mColor); | |
58 | painter->setBrush(brush); |
|
53 | painter->setBrush(brush); | |
59 | painter->drawRect(mXpos-mWidth, mYpos-mHeight ,mWidth ,mHeight); // Evil inverse rect, because we want bars to grow from bottom to top :) |
|
54 | painter->drawRect(mXpos-mWidth, mYpos-mHeight ,mWidth ,mHeight); // Evil inverse rect, because we want bars to grow from bottom to top :) | |
60 | } |
|
55 | } | |
61 |
|
56 | |||
62 | QRectF Bar::boundingRect() const |
|
57 | QRectF Bar::boundingRect() const | |
63 | { |
|
58 | { | |
64 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); |
|
59 | QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); | |
65 | return r; |
|
60 | return r; | |
66 | } |
|
61 | } | |
67 |
|
62 | |||
68 | QTCOMMERCIALCHART_END_NAMESPACE |
|
63 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,46 +1,45 | |||||
1 | #ifndef BAR_H |
|
1 | #ifndef BAR_H | |
2 | #define BAR_H |
|
2 | #define BAR_H | |
3 |
|
3 | |||
4 |
#include "chartitem |
|
4 | #include "chartitem_p.h" | |
5 | #include "qchartglobal.h" |
|
5 | #include "qchartglobal.h" | |
6 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | // Single bar item of chart |
|
10 | // Single bar item of chart | |
11 |
class Bar : public |
|
11 | class Bar : public ChartItem | |
12 | { |
|
12 | { | |
13 | public: |
|
13 | public: | |
14 | Bar(QGraphicsItem *parent=0); |
|
14 | Bar(QGraphicsItem *parent=0); | |
15 |
|
15 | |||
16 | public: // from ChartItemControl |
|
16 | public: // from ChartItemControl | |
17 |
void set |
|
17 | void setSize(const QSize &size); | |
18 | void resize(const QSize &size); |
|
|||
19 | void setTheme(ChartTheme *theme); |
|
18 | void setTheme(ChartTheme *theme); | |
20 | void setPlotDomain(const PlotDomain& data); |
|
19 | void setPlotDomain(const PlotDomain& data); | |
21 |
|
20 | |||
22 | // Layout Stuff |
|
21 | // Layout Stuff | |
23 | void resize( int w, int h ); // Size of bar. in screen coordinates. |
|
22 | void resize( int w, int h ); // Size of bar. in screen coordinates. | |
24 | void setColor( QColor col ); // Color of bar |
|
23 | void setColor( QColor col ); // Color of bar | |
25 | void setPos(qreal x, qreal y); |
|
24 | void setPos(qreal x, qreal y); | |
26 |
|
25 | |||
27 | public: |
|
26 | public: | |
28 | // From QGraphicsItem |
|
27 | // From QGraphicsItem | |
29 |
|
28 | |||
30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
29 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |
31 | QRectF boundingRect() const; |
|
30 | QRectF boundingRect() const; | |
32 |
|
31 | |||
33 | private: |
|
32 | private: | |
34 |
|
33 | |||
35 | int mHeight; |
|
34 | int mHeight; | |
36 | int mWidth; |
|
35 | int mWidth; | |
37 | qreal mXpos; |
|
36 | qreal mXpos; | |
38 | qreal mYpos; |
|
37 | qreal mYpos; | |
39 | QColor mColor; |
|
38 | QColor mColor; | |
40 |
|
39 | |||
41 | PlotDomain mPlotDomain; |
|
40 | PlotDomain mPlotDomain; | |
42 | }; |
|
41 | }; | |
43 |
|
42 | |||
44 | QTCOMMERCIALCHART_END_NAMESPACE |
|
43 | QTCOMMERCIALCHART_END_NAMESPACE | |
45 |
|
44 | |||
46 | #endif // BAR_H |
|
45 | #endif // BAR_H |
@@ -1,148 +1,143 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar.h" |
|
2 | #include "bar.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : |
|
7 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | |
8 |
|
|
8 | ChartItem(parent) | |
9 | ,mSeries(series) |
|
9 | ,mSeries(series) | |
10 | ,mLayoutSet(false) |
|
10 | ,mLayoutSet(false) | |
11 | ,mLayoutDirty(true) |
|
11 | ,mLayoutDirty(true) | |
12 | ,mBarDefaultWidth(10) |
|
12 | ,mBarDefaultWidth(10) | |
13 | { |
|
13 | { | |
14 | dataChanged(); |
|
14 | dataChanged(); | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 |
void BarGroup::set |
|
17 | void BarGroup::setSize(const QSize& size) | |
18 | { |
|
|||
19 | QGraphicsItem::setPos(pos); |
|
|||
20 | } |
|
|||
21 |
|
||||
22 | void BarGroup::resize(const QSize& size) |
|
|||
23 | { |
|
18 | { | |
24 | qDebug() << "BarGroup::setSize"; |
|
19 | qDebug() << "BarGroup::setSize"; | |
25 | mWidth = size.width(); |
|
20 | mWidth = size.width(); | |
26 | mHeight = size.height(); |
|
21 | mHeight = size.height(); | |
27 | layoutChanged(); |
|
22 | layoutChanged(); | |
28 | mLayoutSet = true; |
|
23 | mLayoutSet = true; | |
29 | } |
|
24 | } | |
30 |
|
25 | |||
31 | void BarGroup::setPlotDomain(const PlotDomain& data) |
|
26 | void BarGroup::setPlotDomain(const PlotDomain& data) | |
32 | { |
|
27 | { | |
33 | qDebug() << "BarGroup::setPlotDomain"; |
|
28 | qDebug() << "BarGroup::setPlotDomain"; | |
34 | // TODO: |
|
29 | // TODO: | |
35 | mPlotDomain = data; |
|
30 | mPlotDomain = data; | |
36 | } |
|
31 | } | |
37 |
|
32 | |||
38 | void BarGroup::setTheme(ChartTheme *theme) |
|
33 | void BarGroup::setTheme(ChartTheme *theme) | |
39 | { |
|
34 | { | |
40 | // TODO |
|
35 | // TODO | |
41 | } |
|
36 | } | |
42 |
|
37 | |||
43 | void BarGroup::setBarWidth( int w ) |
|
38 | void BarGroup::setBarWidth( int w ) | |
44 | { |
|
39 | { | |
45 | mBarDefaultWidth = w; |
|
40 | mBarDefaultWidth = w; | |
46 | } |
|
41 | } | |
47 |
|
42 | |||
48 | int BarGroup::addColor( QColor color ) |
|
43 | int BarGroup::addColor( QColor color ) | |
49 | { |
|
44 | { | |
50 | int colorIndex = mColors.count(); |
|
45 | int colorIndex = mColors.count(); | |
51 | mColors.append(color); |
|
46 | mColors.append(color); | |
52 | return colorIndex; |
|
47 | return colorIndex; | |
53 | } |
|
48 | } | |
54 |
|
49 | |||
55 | void BarGroup::resetColors() |
|
50 | void BarGroup::resetColors() | |
56 | { |
|
51 | { | |
57 | mColors.clear(); |
|
52 | mColors.clear(); | |
58 | } |
|
53 | } | |
59 |
|
54 | |||
60 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
55 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
61 | { |
|
56 | { | |
62 | if (!mLayoutSet) { |
|
57 | if (!mLayoutSet) { | |
63 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
58 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
64 | return; |
|
59 | return; | |
65 | } |
|
60 | } | |
66 | if (mLayoutDirty) { |
|
61 | if (mLayoutDirty) { | |
67 | // Layout or data has changed. Need to redraw. |
|
62 | // Layout or data has changed. Need to redraw. | |
68 | foreach(QGraphicsItem* i, childItems()) { |
|
63 | foreach(QGraphicsItem* i, childItems()) { | |
69 | i->paint(painter,option,widget); |
|
64 | i->paint(painter,option,widget); | |
70 | } |
|
65 | } | |
71 | } |
|
66 | } | |
72 | } |
|
67 | } | |
73 |
|
68 | |||
74 | QRectF BarGroup::boundingRect() const |
|
69 | QRectF BarGroup::boundingRect() const | |
75 | { |
|
70 | { | |
76 | return QRectF(0,0,mWidth,mHeight); |
|
71 | return QRectF(0,0,mWidth,mHeight); | |
77 | } |
|
72 | } | |
78 |
|
73 | |||
79 |
|
74 | |||
80 | void BarGroup::dataChanged() |
|
75 | void BarGroup::dataChanged() | |
81 | { |
|
76 | { | |
82 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
77 | qDebug() << "QBarChart::dataChanged mSeries"; | |
83 |
|
78 | |||
84 | // Find out maximum and minimum of all series |
|
79 | // Find out maximum and minimum of all series | |
85 | mMax = mSeries.max(); |
|
80 | mMax = mSeries.max(); | |
86 | mMin = mSeries.min(); |
|
81 | mMin = mSeries.min(); | |
87 |
|
82 | |||
88 | // Delete old bars |
|
83 | // Delete old bars | |
89 | // Is this correct way to delete childItems? |
|
84 | // Is this correct way to delete childItems? | |
90 | foreach (QGraphicsItem* item, childItems()) { |
|
85 | foreach (QGraphicsItem* item, childItems()) { | |
91 | delete item; |
|
86 | delete item; | |
92 | } |
|
87 | } | |
93 |
|
88 | |||
94 | // Create new graphic items for bars |
|
89 | // Create new graphic items for bars | |
95 | int totalItems = mSeries.countTotalItems(); |
|
90 | int totalItems = mSeries.countTotalItems(); | |
96 | for (int i=0; i<totalItems; i++) { |
|
91 | for (int i=0; i<totalItems; i++) { | |
97 | Bar *bar = new Bar(this); |
|
92 | Bar *bar = new Bar(this); | |
98 | childItems().append(bar); |
|
93 | childItems().append(bar); | |
99 | } |
|
94 | } | |
100 |
|
95 | |||
101 | mLayoutDirty = true; |
|
96 | mLayoutDirty = true; | |
102 | } |
|
97 | } | |
103 |
|
98 | |||
104 | void BarGroup::layoutChanged() |
|
99 | void BarGroup::layoutChanged() | |
105 | { |
|
100 | { | |
106 | // Scale bars to new layout |
|
101 | // Scale bars to new layout | |
107 | // Layout for bars: |
|
102 | // Layout for bars: | |
108 | if (mSeries.countRows() <= 0) { |
|
103 | if (mSeries.countRows() <= 0) { | |
109 | // Nothing to do. |
|
104 | // Nothing to do. | |
110 | return; |
|
105 | return; | |
111 | } |
|
106 | } | |
112 |
|
107 | |||
113 | // TODO: better way to auto-layout? |
|
108 | // TODO: better way to auto-layout? | |
114 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
109 | // Use reals for accurancy (we might get some compiler warnings... :) | |
115 | int columnCount = mSeries.countColumns(); |
|
110 | int columnCount = mSeries.countColumns(); | |
116 | int rowCount = mSeries.countRows(); |
|
111 | int rowCount = mSeries.countRows(); | |
117 |
|
112 | |||
118 | qreal tW = mWidth; |
|
113 | qreal tW = mWidth; | |
119 | qreal tH = mHeight; |
|
114 | qreal tH = mHeight; | |
120 | qreal tM = mMax; |
|
115 | qreal tM = mMax; | |
121 | qreal scale = (tH/tM); |
|
116 | qreal scale = (tH/tM); | |
122 |
|
117 | |||
123 | qreal tC = columnCount+1; |
|
118 | qreal tC = columnCount+1; | |
124 | qreal xStepPerSeries = (tW/tC); |
|
119 | qreal xStepPerSeries = (tW/tC); | |
125 |
|
120 | |||
126 | qDebug() << "XSTEP:" << xStepPerSeries; |
|
121 | qDebug() << "XSTEP:" << xStepPerSeries; | |
127 |
|
122 | |||
128 | // Scaling. |
|
123 | // Scaling. | |
129 | int itemIndex(0); |
|
124 | int itemIndex(0); | |
130 | for (int column=0; column < columnCount; column++) { |
|
125 | for (int column=0; column < columnCount; column++) { | |
131 | qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); |
|
126 | qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | |
132 | for (int row = 0; row < rowCount; row++) { |
|
127 | for (int row = 0; row < rowCount; row++) { | |
133 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
128 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
134 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
129 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
135 |
|
130 | |||
136 | // TODO: width settable per bar? |
|
131 | // TODO: width settable per bar? | |
137 | bar->resize(mBarDefaultWidth, barHeight); |
|
132 | bar->resize(mBarDefaultWidth, barHeight); | |
138 | bar->setColor(mColors.at(row)); |
|
133 | bar->setColor(mColors.at(row)); | |
139 | bar->setPos(xPos, mHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); |
|
134 | bar->setPos(xPos, mHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | |
140 | itemIndex++; |
|
135 | itemIndex++; | |
141 | xPos += mBarDefaultWidth; |
|
136 | xPos += mBarDefaultWidth; | |
142 | } |
|
137 | } | |
143 | } |
|
138 | } | |
144 |
|
139 | |||
145 | mLayoutDirty = true; |
|
140 | mLayoutDirty = true; | |
146 | } |
|
141 | } | |
147 |
|
142 | |||
148 | QTCOMMERCIALCHART_END_NAMESPACE |
|
143 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,59 +1,58 | |||||
1 | #ifndef QBARGROUP_H |
|
1 | #ifndef QBARGROUP_H | |
2 | #define QBARGROUP_H |
|
2 | #define QBARGROUP_H | |
3 |
|
3 | |||
4 |
#include "chartitem |
|
4 | #include "chartitem_p.h" | |
5 | #include "bar.h" |
|
5 | #include "bar.h" | |
6 | #include "barchartseries.h" |
|
6 | #include "barchartseries.h" | |
7 | #include <QGraphicsItem> |
|
7 | #include <QGraphicsItem> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 |
class BarGroup : public |
|
11 | class BarGroup : public ChartItem | |
12 | { |
|
12 | { | |
13 | public: |
|
13 | public: | |
14 | explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); |
|
14 | explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); | |
15 |
|
15 | |||
16 |
public: // from Chart |
|
16 | public: // from ChartObjectInterface | |
17 |
void set |
|
17 | void setSize(const QSize &size); | |
18 | void resize(const QSize &size); |
|
|||
19 | void setTheme(ChartTheme *theme); |
|
18 | void setTheme(ChartTheme *theme); | |
20 | void setPlotDomain(const PlotDomain& data); |
|
19 | void setPlotDomain(const PlotDomain& data); | |
21 |
|
20 | |||
22 | // Layout "api" |
|
21 | // Layout "api" | |
23 | void setPos(qreal x, qreal y); |
|
22 | void setPos(qreal x, qreal y); | |
24 | void setBarWidth( int w ); // Default width for each bar |
|
23 | void setBarWidth( int w ); // Default width for each bar | |
25 |
|
24 | |||
26 | int addColor( QColor color ); |
|
25 | int addColor( QColor color ); | |
27 | void resetColors(); |
|
26 | void resetColors(); | |
28 |
|
27 | |||
29 | // From QGraphicsItem |
|
28 | // From QGraphicsItem | |
30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
29 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
31 | QRectF boundingRect() const; |
|
30 | QRectF boundingRect() const; | |
32 |
|
31 | |||
33 | private: |
|
32 | private: | |
34 |
|
33 | |||
35 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
34 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
36 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
35 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
37 |
|
36 | |||
38 | private: |
|
37 | private: | |
39 |
|
38 | |||
40 | // Data |
|
39 | // Data | |
41 | BarChartSeries& mSeries; |
|
40 | BarChartSeries& mSeries; | |
42 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
41 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |
43 | int mMax; |
|
42 | int mMax; | |
44 |
|
43 | |||
45 | int mHeight; // Layout spesific |
|
44 | int mHeight; // Layout spesific | |
46 | int mWidth; |
|
45 | int mWidth; | |
47 | int mBarDefaultWidth; |
|
46 | int mBarDefaultWidth; | |
48 |
|
47 | |||
49 | bool mLayoutSet; // True, if component has been laid out. |
|
48 | bool mLayoutSet; // True, if component has been laid out. | |
50 | bool mLayoutDirty; |
|
49 | bool mLayoutDirty; | |
51 |
|
50 | |||
52 | QList<QColor> mColors; // List of colors for series for now |
|
51 | QList<QColor> mColors; // List of colors for series for now | |
53 |
|
52 | |||
54 | PlotDomain mPlotDomain; |
|
53 | PlotDomain mPlotDomain; | |
55 | }; |
|
54 | }; | |
56 |
|
55 | |||
57 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | QTCOMMERCIALCHART_END_NAMESPACE | |
58 |
|
57 | |||
59 | #endif // QBARGROUP_H |
|
58 | #endif // QBARGROUP_H |
@@ -1,148 +1,141 | |||||
1 | #include "percentbargroup.h" |
|
1 | #include "percentbargroup.h" | |
2 |
|
2 | |||
3 | #include "stackedbargroup.h" |
|
3 | #include "stackedbargroup.h" | |
4 | #include "bar.h" |
|
4 | #include "bar.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
9 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : | |
10 |
|
|
10 | ChartItem(parent) | |
11 | ,mSeries(series) |
|
11 | ,mSeries(series) | |
12 | ,mLayoutSet(false) |
|
12 | ,mLayoutSet(false) | |
13 | ,mLayoutDirty(true) |
|
13 | ,mLayoutDirty(true) | |
14 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
14 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
15 | { |
|
15 | { | |
16 | dataChanged(); |
|
16 | dataChanged(); | |
17 | } |
|
17 | } | |
18 |
|
18 | |||
19 |
|
19 | void StackedBarGroup::setSize(const QSize& size) | ||
20 | void StackedBarGroup::setPos(const QPointF & pos) |
|
|||
21 | { |
|
|||
22 | qDebug() << "StackedBarGroup::setPos"; |
|
|||
23 | // TODO: |
|
|||
24 | } |
|
|||
25 |
|
||||
26 | void StackedBarGroup::resize(const QSize& size) |
|
|||
27 | { |
|
20 | { | |
28 |
qDebug() << "StackedBarGroup:: |
|
21 | qDebug() << "StackedBarGroup::setSize"; | |
29 | mWidth = size.width(); |
|
22 | mWidth = size.width(); | |
30 | mHeight = size.height(); |
|
23 | mHeight = size.height(); | |
31 | layoutChanged(); |
|
24 | layoutChanged(); | |
32 | mLayoutSet = true; |
|
25 | mLayoutSet = true; | |
33 | } |
|
26 | } | |
34 |
|
27 | |||
35 | void StackedBarGroup::setPlotDomain(const PlotDomain& data) |
|
28 | void StackedBarGroup::setPlotDomain(const PlotDomain& data) | |
36 | { |
|
29 | { | |
37 | qDebug() << "StackedBarGroup::setPlotDomain"; |
|
30 | qDebug() << "StackedBarGroup::setPlotDomain"; | |
38 | // TODO: |
|
31 | // TODO: | |
39 | } |
|
32 | } | |
40 |
|
33 | |||
41 | void StackedBarGroup::setTheme(ChartTheme *theme) |
|
34 | void StackedBarGroup::setTheme(ChartTheme *theme) | |
42 | { |
|
35 | { | |
43 | qDebug() << "StackedBarGroup::setTheme"; |
|
36 | qDebug() << "StackedBarGroup::setTheme"; | |
44 | // TODO: |
|
37 | // TODO: | |
45 | } |
|
38 | } | |
46 |
|
39 | |||
47 | void StackedBarGroup::setBarWidth( int w ) |
|
40 | void StackedBarGroup::setBarWidth( int w ) | |
48 | { |
|
41 | { | |
49 | mBarDefaultWidth = w; |
|
42 | mBarDefaultWidth = w; | |
50 | } |
|
43 | } | |
51 |
|
44 | |||
52 | int StackedBarGroup::addColor( QColor color ) |
|
45 | int StackedBarGroup::addColor( QColor color ) | |
53 | { |
|
46 | { | |
54 | int colorIndex = mColors.count(); |
|
47 | int colorIndex = mColors.count(); | |
55 | mColors.append(color); |
|
48 | mColors.append(color); | |
56 | return colorIndex; |
|
49 | return colorIndex; | |
57 | } |
|
50 | } | |
58 |
|
51 | |||
59 | void StackedBarGroup::resetColors() |
|
52 | void StackedBarGroup::resetColors() | |
60 | { |
|
53 | { | |
61 | mColors.clear(); |
|
54 | mColors.clear(); | |
62 | } |
|
55 | } | |
63 |
|
56 | |||
64 | void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
57 | void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
65 | { |
|
58 | { | |
66 | if (!mLayoutSet) { |
|
59 | if (!mLayoutSet) { | |
67 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
60 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
68 | return; |
|
61 | return; | |
69 | } |
|
62 | } | |
70 | if (mLayoutDirty) { |
|
63 | if (mLayoutDirty) { | |
71 | // Layout or data has changed. Need to redraw. |
|
64 | // Layout or data has changed. Need to redraw. | |
72 | foreach(QGraphicsItem* i, childItems()) { |
|
65 | foreach(QGraphicsItem* i, childItems()) { | |
73 | i->paint(painter,option,widget); |
|
66 | i->paint(painter,option,widget); | |
74 | } |
|
67 | } | |
75 | } |
|
68 | } | |
76 | } |
|
69 | } | |
77 |
|
70 | |||
78 | QRectF StackedBarGroup::boundingRect() const |
|
71 | QRectF StackedBarGroup::boundingRect() const | |
79 | { |
|
72 | { | |
80 | return QRectF(0,0,mWidth,mHeight); |
|
73 | return QRectF(0,0,mWidth,mHeight); | |
81 | } |
|
74 | } | |
82 |
|
75 | |||
83 |
|
76 | |||
84 | void StackedBarGroup::dataChanged() |
|
77 | void StackedBarGroup::dataChanged() | |
85 | { |
|
78 | { | |
86 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
79 | qDebug() << "QBarChart::dataChanged mSeries"; | |
87 |
|
80 | |||
88 | // Find out maximum and minimum of all series |
|
81 | // Find out maximum and minimum of all series | |
89 | mMax = mSeries.max(); |
|
82 | mMax = mSeries.max(); | |
90 | mMin = mSeries.min(); |
|
83 | mMin = mSeries.min(); | |
91 |
|
84 | |||
92 | // Delete old bars |
|
85 | // Delete old bars | |
93 | // Is this correct way to delete childItems? |
|
86 | // Is this correct way to delete childItems? | |
94 | foreach (QGraphicsItem* item, childItems()) { |
|
87 | foreach (QGraphicsItem* item, childItems()) { | |
95 | delete item; |
|
88 | delete item; | |
96 | } |
|
89 | } | |
97 |
|
90 | |||
98 | // Create new graphic items for bars |
|
91 | // Create new graphic items for bars | |
99 | int totalItems = mSeries.countTotalItems(); |
|
92 | int totalItems = mSeries.countTotalItems(); | |
100 | for (int i=0; i<totalItems; i++) { |
|
93 | for (int i=0; i<totalItems; i++) { | |
101 | Bar *bar = new Bar(this); |
|
94 | Bar *bar = new Bar(this); | |
102 | childItems().append(bar); |
|
95 | childItems().append(bar); | |
103 | } |
|
96 | } | |
104 |
|
97 | |||
105 | mLayoutDirty = true; |
|
98 | mLayoutDirty = true; | |
106 | } |
|
99 | } | |
107 |
|
100 | |||
108 | void StackedBarGroup::layoutChanged() |
|
101 | void StackedBarGroup::layoutChanged() | |
109 | { |
|
102 | { | |
110 | // Scale bars to new layout |
|
103 | // Scale bars to new layout | |
111 | // Layout for bars: |
|
104 | // Layout for bars: | |
112 | if (mSeries.countRows() <= 0) { |
|
105 | if (mSeries.countRows() <= 0) { | |
113 | // Nothing to do. |
|
106 | // Nothing to do. | |
114 | return; |
|
107 | return; | |
115 | } |
|
108 | } | |
116 |
|
109 | |||
117 | // TODO: better way to auto-layout |
|
110 | // TODO: better way to auto-layout | |
118 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
111 | // Use reals for accurancy (we might get some compiler warnings... :) | |
119 | qreal maxSum = mSeries.maxColumnSum(); |
|
112 | qreal maxSum = mSeries.maxColumnSum(); | |
120 | qreal h = mHeight; |
|
113 | qreal h = mHeight; | |
121 | qreal scale = (h / maxSum); |
|
114 | qreal scale = (h / maxSum); | |
122 |
|
115 | |||
123 | int count = mSeries.countColumns(); |
|
116 | int count = mSeries.countColumns(); | |
124 | int itemIndex(0); |
|
117 | int itemIndex(0); | |
125 | qreal tW = mWidth; |
|
118 | qreal tW = mWidth; | |
126 | qreal tC = count+1; |
|
119 | qreal tC = count+1; | |
127 | qreal xStep = (tW/tC); |
|
120 | qreal xStep = (tW/tC); | |
128 | qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); |
|
121 | qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); | |
129 |
|
122 | |||
130 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
123 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
131 | qreal yPos = h; |
|
124 | qreal yPos = h; | |
132 | for (int row=0; row < mSeries.countRows(); row++) { |
|
125 | for (int row=0; row < mSeries.countRows(); row++) { | |
133 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
126 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
134 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
127 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
135 |
|
128 | |||
136 | // TODO: width settable per bar? |
|
129 | // TODO: width settable per bar? | |
137 | bar->resize(mBarDefaultWidth, barHeight); |
|
130 | bar->resize(mBarDefaultWidth, barHeight); | |
138 | bar->setColor(mColors.at(row)); |
|
131 | bar->setColor(mColors.at(row)); | |
139 | bar->setPos(xPos, yPos); |
|
132 | bar->setPos(xPos, yPos); | |
140 | itemIndex++; |
|
133 | itemIndex++; | |
141 | yPos -= barHeight; |
|
134 | yPos -= barHeight; | |
142 | } |
|
135 | } | |
143 | xPos += xStep; |
|
136 | xPos += xStep; | |
144 | } |
|
137 | } | |
145 | mLayoutDirty = true; |
|
138 | mLayoutDirty = true; | |
146 | } |
|
139 | } | |
147 |
|
140 | |||
148 | QTCOMMERCIALCHART_END_NAMESPACE |
|
141 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,59 +1,58 | |||||
1 | #ifndef PERCENTBARGROUP_H |
|
1 | #ifndef PERCENTBARGROUP_H | |
2 | #define PERCENTBARGROUP_H |
|
2 | #define PERCENTBARGROUP_H | |
3 |
|
3 | |||
4 |
#include "chartitem |
|
4 | #include "chartitem_p.h" | |
5 | #include "bar.h" |
|
5 | #include "bar.h" | |
6 | #include "percentbarchartseries.h" |
|
6 | #include "percentbarchartseries.h" | |
7 | #include <QGraphicsItem> |
|
7 | #include <QGraphicsItem> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 |
class PercentBarGroup : public |
|
11 | class PercentBarGroup : public ChartItem | |
12 | { |
|
12 | { | |
13 | public: |
|
13 | public: | |
14 | PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0); |
|
14 | PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0); | |
15 |
|
15 | |||
16 |
public: // From Chart |
|
16 | public: // From ChartObjectInterface | |
17 |
void set |
|
17 | void setSize(const QSize &size); | |
18 | void resize(const QSize &size); |
|
|||
19 | void setTheme(ChartTheme *theme); |
|
18 | void setTheme(ChartTheme *theme); | |
20 | void setPlotDomain(const PlotDomain& data); |
|
19 | void setPlotDomain(const PlotDomain& data); | |
21 |
|
20 | |||
22 | public: |
|
21 | public: | |
23 | // Layout "api" |
|
22 | // Layout "api" | |
24 | void setPos(qreal x, qreal y); |
|
23 | void setPos(qreal x, qreal y); | |
25 | void setBarWidth( int w ); // Default width for each bar |
|
24 | void setBarWidth( int w ); // Default width for each bar | |
26 |
|
25 | |||
27 | int addColor( QColor color ); |
|
26 | int addColor( QColor color ); | |
28 | void resetColors(); |
|
27 | void resetColors(); | |
29 |
|
28 | |||
30 | // From QGraphicsItem |
|
29 | // From QGraphicsItem | |
31 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
32 | QRectF boundingRect() const; |
|
31 | QRectF boundingRect() const; | |
33 |
|
32 | |||
34 | private: |
|
33 | private: | |
35 |
|
34 | |||
36 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
35 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
37 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
36 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
38 |
|
37 | |||
39 | private: |
|
38 | private: | |
40 |
|
39 | |||
41 | // Data |
|
40 | // Data | |
42 | PercentBarChartSeries& mSeries; |
|
41 | PercentBarChartSeries& mSeries; | |
43 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
42 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |
44 | int mMax; |
|
43 | int mMax; | |
45 |
|
44 | |||
46 | int mHeight; // Layout spesific |
|
45 | int mHeight; // Layout spesific | |
47 | int mWidth; |
|
46 | int mWidth; | |
48 | int mBarDefaultWidth; |
|
47 | int mBarDefaultWidth; | |
49 |
|
48 | |||
50 | bool mLayoutSet; // True, if component has been laid out. |
|
49 | bool mLayoutSet; // True, if component has been laid out. | |
51 | bool mLayoutDirty; |
|
50 | bool mLayoutDirty; | |
52 |
|
51 | |||
53 | QList<QColor> mColors; // List of colors for series for now |
|
52 | QList<QColor> mColors; // List of colors for series for now | |
54 |
|
53 | |||
55 | }; |
|
54 | }; | |
56 |
|
55 | |||
57 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | QTCOMMERCIALCHART_END_NAMESPACE | |
58 |
|
57 | |||
59 | #endif // PERCENTBARGROUP_H |
|
58 | #endif // PERCENTBARGROUP_H |
@@ -1,144 +1,138 | |||||
1 | #include "percentbargroup.h" |
|
1 | #include "percentbargroup.h" | |
2 | #include "bar.h" |
|
2 | #include "bar.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : |
|
7 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : | |
8 |
|
|
8 | ChartItem(parent) | |
9 | ,mSeries(series) |
|
9 | ,mSeries(series) | |
10 | ,mLayoutSet(false) |
|
10 | ,mLayoutSet(false) | |
11 | ,mLayoutDirty(true) |
|
11 | ,mLayoutDirty(true) | |
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
13 | { |
|
13 | { | |
14 | dataChanged(); |
|
14 | dataChanged(); | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 |
void PercentBarGroup::set |
|
17 | void PercentBarGroup::setSize(const QSize& size) | |
18 | { |
|
18 | { | |
19 |
qDebug() << "PercentBarGroup::set |
|
19 | qDebug() << "PercentBarGroup::setSize"; | |
20 | // TODO: |
|
|||
21 | } |
|
|||
22 |
|
||||
23 | void PercentBarGroup::resize(const QSize& size) |
|
|||
24 | { |
|
|||
25 | qDebug() << "PercentBarGroup::resize"; |
|
|||
26 | mWidth = size.width(); |
|
20 | mWidth = size.width(); | |
27 | mHeight = size.height(); |
|
21 | mHeight = size.height(); | |
28 | layoutChanged(); |
|
22 | layoutChanged(); | |
29 | mLayoutSet = true; |
|
23 | mLayoutSet = true; | |
30 | } |
|
24 | } | |
31 |
|
25 | |||
32 | void PercentBarGroup::setPlotDomain(const PlotDomain& data) |
|
26 | void PercentBarGroup::setPlotDomain(const PlotDomain& data) | |
33 | { |
|
27 | { | |
34 | qDebug() << "PercentBarGroup::setPlotDomain"; |
|
28 | qDebug() << "PercentBarGroup::setPlotDomain"; | |
35 | // TODO: |
|
29 | // TODO: | |
36 | } |
|
30 | } | |
37 |
|
31 | |||
38 | void PercentBarGroup::setTheme(ChartTheme *theme) |
|
32 | void PercentBarGroup::setTheme(ChartTheme *theme) | |
39 | { |
|
33 | { | |
40 | qDebug() << "PercentBarGroup::setTheme"; |
|
34 | qDebug() << "PercentBarGroup::setTheme"; | |
41 | // TODO: |
|
35 | // TODO: | |
42 | } |
|
36 | } | |
43 |
|
37 | |||
44 | void PercentBarGroup::setBarWidth( int w ) |
|
38 | void PercentBarGroup::setBarWidth( int w ) | |
45 | { |
|
39 | { | |
46 | mBarDefaultWidth = w; |
|
40 | mBarDefaultWidth = w; | |
47 | } |
|
41 | } | |
48 |
|
42 | |||
49 | int PercentBarGroup::addColor( QColor color ) |
|
43 | int PercentBarGroup::addColor( QColor color ) | |
50 | { |
|
44 | { | |
51 | int colorIndex = mColors.count(); |
|
45 | int colorIndex = mColors.count(); | |
52 | mColors.append(color); |
|
46 | mColors.append(color); | |
53 | return colorIndex; |
|
47 | return colorIndex; | |
54 | } |
|
48 | } | |
55 |
|
49 | |||
56 | void PercentBarGroup::resetColors() |
|
50 | void PercentBarGroup::resetColors() | |
57 | { |
|
51 | { | |
58 | mColors.clear(); |
|
52 | mColors.clear(); | |
59 | } |
|
53 | } | |
60 |
|
54 | |||
61 | void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
55 | void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
62 | { |
|
56 | { | |
63 | if (!mLayoutSet) { |
|
57 | if (!mLayoutSet) { | |
64 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
58 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
65 | return; |
|
59 | return; | |
66 | } |
|
60 | } | |
67 | if (mLayoutDirty) { |
|
61 | if (mLayoutDirty) { | |
68 | // Layout or data has changed. Need to redraw. |
|
62 | // Layout or data has changed. Need to redraw. | |
69 | foreach(QGraphicsItem* i, childItems()) { |
|
63 | foreach(QGraphicsItem* i, childItems()) { | |
70 | i->paint(painter,option,widget); |
|
64 | i->paint(painter,option,widget); | |
71 | } |
|
65 | } | |
72 | } |
|
66 | } | |
73 | } |
|
67 | } | |
74 |
|
68 | |||
75 | QRectF PercentBarGroup::boundingRect() const |
|
69 | QRectF PercentBarGroup::boundingRect() const | |
76 | { |
|
70 | { | |
77 | return QRectF(0,0,mWidth,mHeight); |
|
71 | return QRectF(0,0,mWidth,mHeight); | |
78 | } |
|
72 | } | |
79 |
|
73 | |||
80 |
|
74 | |||
81 | void PercentBarGroup::dataChanged() |
|
75 | void PercentBarGroup::dataChanged() | |
82 | { |
|
76 | { | |
83 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
77 | qDebug() << "QBarChart::dataChanged mSeries"; | |
84 |
|
78 | |||
85 | // Find out maximum and minimum of all series |
|
79 | // Find out maximum and minimum of all series | |
86 | mMax = mSeries.max(); |
|
80 | mMax = mSeries.max(); | |
87 | mMin = mSeries.min(); |
|
81 | mMin = mSeries.min(); | |
88 |
|
82 | |||
89 | // Delete old bars |
|
83 | // Delete old bars | |
90 | // Is this correct way to delete childItems? |
|
84 | // Is this correct way to delete childItems? | |
91 | foreach (QGraphicsItem* item, childItems()) { |
|
85 | foreach (QGraphicsItem* item, childItems()) { | |
92 | delete item; |
|
86 | delete item; | |
93 | } |
|
87 | } | |
94 |
|
88 | |||
95 | // Create new graphic items for bars |
|
89 | // Create new graphic items for bars | |
96 | int totalItems = mSeries.countTotalItems(); |
|
90 | int totalItems = mSeries.countTotalItems(); | |
97 | for (int i=0; i<totalItems; i++) { |
|
91 | for (int i=0; i<totalItems; i++) { | |
98 | Bar *bar = new Bar(this); |
|
92 | Bar *bar = new Bar(this); | |
99 | childItems().append(bar); |
|
93 | childItems().append(bar); | |
100 | } |
|
94 | } | |
101 |
|
95 | |||
102 | mLayoutDirty = true; |
|
96 | mLayoutDirty = true; | |
103 | } |
|
97 | } | |
104 |
|
98 | |||
105 | void PercentBarGroup::layoutChanged() |
|
99 | void PercentBarGroup::layoutChanged() | |
106 | { |
|
100 | { | |
107 | // Scale bars to new layout |
|
101 | // Scale bars to new layout | |
108 | // Layout for bars: |
|
102 | // Layout for bars: | |
109 | if (mSeries.countRows() <= 0) { |
|
103 | if (mSeries.countRows() <= 0) { | |
110 | // Nothing to do. |
|
104 | // Nothing to do. | |
111 | return; |
|
105 | return; | |
112 | } |
|
106 | } | |
113 |
|
107 | |||
114 | // TODO: better way to auto-layout |
|
108 | // TODO: better way to auto-layout | |
115 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
109 | // Use reals for accurancy (we might get some compiler warnings... :) | |
116 | int count = mSeries.countColumns(); |
|
110 | int count = mSeries.countColumns(); | |
117 | int itemIndex(0); |
|
111 | int itemIndex(0); | |
118 | qreal tW = mWidth; |
|
112 | qreal tW = mWidth; | |
119 | qreal tC = count+1; |
|
113 | qreal tC = count+1; | |
120 | qreal xStep = (tW/tC); |
|
114 | qreal xStep = (tW/tC); | |
121 | qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); |
|
115 | qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); | |
122 |
|
116 | |||
123 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
117 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
124 | qreal colSum = mSeries.columnSum(column); |
|
118 | qreal colSum = mSeries.columnSum(column); | |
125 | qreal h = mHeight; |
|
119 | qreal h = mHeight; | |
126 | qreal scale = (h / colSum); |
|
120 | qreal scale = (h / colSum); | |
127 | qreal yPos = h; |
|
121 | qreal yPos = h; | |
128 | for (int row=0; row < mSeries.countRows(); row++) { |
|
122 | for (int row=0; row < mSeries.countRows(); row++) { | |
129 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
123 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
130 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
124 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
131 |
|
125 | |||
132 | // TODO: width settable per bar? |
|
126 | // TODO: width settable per bar? | |
133 | bar->resize(mBarDefaultWidth, barHeight); |
|
127 | bar->resize(mBarDefaultWidth, barHeight); | |
134 | bar->setColor(mColors.at(row)); |
|
128 | bar->setColor(mColors.at(row)); | |
135 | bar->setPos(xPos, yPos); |
|
129 | bar->setPos(xPos, yPos); | |
136 | itemIndex++; |
|
130 | itemIndex++; | |
137 | yPos -= barHeight; |
|
131 | yPos -= barHeight; | |
138 | } |
|
132 | } | |
139 | xPos += xStep; |
|
133 | xPos += xStep; | |
140 | } |
|
134 | } | |
141 | mLayoutDirty = true; |
|
135 | mLayoutDirty = true; | |
142 | } |
|
136 | } | |
143 |
|
137 | |||
144 | QTCOMMERCIALCHART_END_NAMESPACE |
|
138 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,58 +1,57 | |||||
1 | #ifndef STACKEDBARGROUP_H |
|
1 | #ifndef STACKEDBARGROUP_H | |
2 | #define STACKEDBARGROUP_H |
|
2 | #define STACKEDBARGROUP_H | |
3 |
|
3 | |||
4 |
#include "chartitem |
|
4 | #include "chartitem_p.h" | |
5 | #include "bar.h" |
|
5 | #include "bar.h" | |
6 | #include "stackedbarchartseries.h" |
|
6 | #include "stackedbarchartseries.h" | |
7 | #include <QGraphicsItem> |
|
7 | #include <QGraphicsItem> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 |
class StackedBarGroup : public |
|
11 | class StackedBarGroup : public ChartItem | |
12 | { |
|
12 | { | |
13 | public: |
|
13 | public: | |
14 | StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); |
|
14 | StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); | |
15 |
|
15 | |||
16 |
public: // From Chart |
|
16 | public: // From ChartObjectInterface | |
17 |
void set |
|
17 | void setSize(const QSize &size); | |
18 | void resize(const QSize &size); |
|
|||
19 | void setTheme(ChartTheme *theme); |
|
18 | void setTheme(ChartTheme *theme); | |
20 | void setPlotDomain(const PlotDomain& data); |
|
19 | void setPlotDomain(const PlotDomain& data); | |
21 |
|
20 | |||
22 | public: // Layout "api" |
|
21 | public: // Layout "api" | |
23 | void setPos(qreal x, qreal y); |
|
22 | void setPos(qreal x, qreal y); | |
24 | void setBarWidth( int w ); // Default width for each bar |
|
23 | void setBarWidth( int w ); // Default width for each bar | |
25 |
|
24 | |||
26 | int addColor( QColor color ); |
|
25 | int addColor( QColor color ); | |
27 | void resetColors(); |
|
26 | void resetColors(); | |
28 |
|
27 | |||
29 | // From QGraphicsItem |
|
28 | // From QGraphicsItem | |
30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
29 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
31 | QRectF boundingRect() const; |
|
30 | QRectF boundingRect() const; | |
32 |
|
31 | |||
33 | private: |
|
32 | private: | |
34 |
|
33 | |||
35 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
34 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
36 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
35 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
37 |
|
36 | |||
38 | private: |
|
37 | private: | |
39 |
|
38 | |||
40 | // Data |
|
39 | // Data | |
41 | StackedBarChartSeries& mSeries; |
|
40 | StackedBarChartSeries& mSeries; | |
42 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
41 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |
43 | int mMax; |
|
42 | int mMax; | |
44 |
|
43 | |||
45 | int mHeight; // Layout spesific |
|
44 | int mHeight; // Layout spesific | |
46 | int mWidth; |
|
45 | int mWidth; | |
47 | int mBarDefaultWidth; |
|
46 | int mBarDefaultWidth; | |
48 |
|
47 | |||
49 | bool mLayoutSet; // True, if component has been laid out. |
|
48 | bool mLayoutSet; // True, if component has been laid out. | |
50 | bool mLayoutDirty; |
|
49 | bool mLayoutDirty; | |
51 |
|
50 | |||
52 | QList<QColor> mColors; // List of colors for series for now |
|
51 | QList<QColor> mColors; // List of colors for series for now | |
53 |
|
52 | |||
54 | }; |
|
53 | }; | |
55 |
|
54 | |||
56 | QTCOMMERCIALCHART_END_NAMESPACE |
|
55 | QTCOMMERCIALCHART_END_NAMESPACE | |
57 |
|
56 | |||
58 | #endif // STACKEDBARGROUP_H |
|
57 | #endif // STACKEDBARGROUP_H |
@@ -1,24 +1,30 | |||||
1 |
#ifndef CHART |
|
1 | #ifndef CHART_OBJECT_INTERFACE_H | |
2 |
#define CHART |
|
2 | #define CHART_OBJECT_INTERFACE_H | |
3 |
|
3 | |||
4 | #include "plotdomain_p.h" |
|
4 | #include "plotdomain_p.h" | |
5 | #include <qchartglobal.h> |
|
5 | #include <qchartglobal.h> | |
6 | #include <QSize> |
|
6 | #include <QSize> | |
7 |
|
7 | |||
|
8 | class QGraphicsItem; | |||
|
9 | ||||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
11 | |||
10 | class ChartTheme; |
|
12 | class ChartTheme; | |
11 | class PlotDomain; |
|
13 | class PlotDomain; | |
12 |
|
14 | |||
13 | class ChartItemControl |
|
15 | /*! | |
|
16 | * Internal abstract interface for passing updates on chart related properties. | |||
|
17 | */ | |||
|
18 | class ChartObjectInterface | |||
14 | { |
|
19 | { | |
15 | public: |
|
20 | public: | |
16 |
virtual void set |
|
21 | virtual void setSize(const QSize &size) = 0; | |
17 | virtual void resize(const QSize &size) = 0; |
|
|||
18 | virtual void setTheme(ChartTheme *theme) = 0; |
|
22 | virtual void setTheme(ChartTheme *theme) = 0; | |
19 | virtual void setPlotDomain(const PlotDomain& data) = 0; |
|
23 | virtual void setPlotDomain(const PlotDomain& data) = 0; | |
|
24 | // TODO: this is a hack; integration ongoing: | |||
|
25 | virtual QGraphicsItem *graphicsItem() { return 0; } | |||
20 | }; |
|
26 | }; | |
21 |
|
27 | |||
22 | QTCOMMERCIALCHART_END_NAMESPACE |
|
28 | QTCOMMERCIALCHART_END_NAMESPACE | |
23 |
|
29 | |||
24 |
#endif // CHART |
|
30 | #endif // CHART_OBJECT_INTERFACE_H |
@@ -1,368 +1,370 | |||||
1 | #include "qchart.h" |
|
1 | #include "qchart.h" | |
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 "qpieseries.h" | |
6 | #include "qpieseries_p.h" |
|
6 | #include "qpieseries_p.h" | |
7 | #include "qxychartseries.h" |
|
7 | #include "qxychartseries.h" | |
8 | #include "qchartaxis.h" |
|
8 | #include "qchartaxis.h" | |
9 | #include "barchartseries.h" |
|
9 | #include "barchartseries.h" | |
10 | #include "bargroup.h" |
|
10 | #include "bargroup.h" | |
11 | #include "stackedbarchartseries.h" |
|
11 | #include "stackedbarchartseries.h" | |
12 | #include "stackedbargroup.h" |
|
12 | #include "stackedbargroup.h" | |
13 | #include "percentbarchartseries.h" |
|
13 | #include "percentbarchartseries.h" | |
14 | #include "percentbargroup.h" |
|
14 | #include "percentbargroup.h" | |
15 | #include "charttheme_p.h" |
|
15 | #include "charttheme_p.h" | |
16 |
#include "chart |
|
16 | #include "chartobjectinterface_p.h" | |
17 |
|
17 | |||
18 | #include "xylinechartitem_p.h" |
|
18 | #include "xylinechartitem_p.h" | |
19 | #include "plotdomain_p.h" |
|
19 | #include "plotdomain_p.h" | |
20 | #include "axisitem_p.h" |
|
20 | #include "axisitem_p.h" | |
21 | #include <QGraphicsScene> |
|
21 | #include <QGraphicsScene> | |
22 | #include <QDebug> |
|
22 | #include <QDebug> | |
23 |
|
23 | |||
24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
25 |
|
25 | |||
26 | QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), |
|
26 | QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), | |
27 | m_backgroundItem(0), |
|
27 | m_backgroundItem(0), | |
28 | m_titleItem(0), |
|
28 | m_titleItem(0), | |
29 | m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)), |
|
29 | m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)), | |
30 | m_plotDataIndex(0), |
|
30 | m_plotDataIndex(0), | |
31 | m_marginSize(0), |
|
31 | m_marginSize(0), | |
32 | m_chartTheme(new ChartTheme()) |
|
32 | m_chartTheme(new ChartTheme()) | |
33 | { |
|
33 | { | |
34 | // TODO: the default theme? |
|
34 | // TODO: the default theme? | |
35 | setTheme(QChart::ChartThemeDefault); |
|
35 | setTheme(QChart::ChartThemeDefault); | |
36 |
|
36 | |||
37 | PlotDomain domain; |
|
37 | PlotDomain domain; | |
38 | m_plotDomainList << domain; |
|
38 | m_plotDomainList << domain; | |
39 | m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); |
|
39 | m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); | |
40 |
m_chart |
|
40 | m_chartObjectInterfaces << m_axisXItem; | |
41 |
m_chart |
|
41 | m_chartObjectInterfaces << m_axisYItem.at(0); | |
42 | } |
|
42 | } | |
43 |
|
43 | |||
44 | QChart::~QChart(){} |
|
44 | QChart::~QChart(){} | |
45 |
|
45 | |||
46 | QRectF QChart::boundingRect() const |
|
46 | QRectF QChart::boundingRect() const | |
47 | { |
|
47 | { | |
48 | return m_rect; |
|
48 | return m_rect; | |
49 | } |
|
49 | } | |
50 |
|
50 | |||
51 | void QChart::addSeries(QChartSeries* series) |
|
51 | void QChart::addSeries(QChartSeries* series) | |
52 | { |
|
52 | { | |
53 | // TODO: we should check the series not already added |
|
53 | // TODO: we should check the series not already added | |
54 |
|
54 | |||
55 | m_chartSeries << series; |
|
55 | m_chartSeries << series; | |
56 |
|
56 | |||
57 | switch(series->type()) |
|
57 | switch(series->type()) | |
58 | { |
|
58 | { | |
59 | case QChartSeries::SeriesTypeLine: { |
|
59 | case QChartSeries::SeriesTypeLine: { | |
60 |
|
60 | |||
61 | QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); |
|
61 | QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); | |
62 | m_plotDataIndex = 0 ; |
|
62 | m_plotDataIndex = 0 ; | |
63 | m_plotDomainList.resize(1); |
|
63 | m_plotDomainList.resize(1); | |
64 |
|
64 | |||
65 | PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; |
|
65 | PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; | |
66 |
|
66 | |||
67 | for (int i = 0 ; i < xyseries->count() ; i++) |
|
67 | for (int i = 0 ; i < xyseries->count() ; i++) | |
68 | { |
|
68 | { | |
69 | qreal x = xyseries->x(i); |
|
69 | qreal x = xyseries->x(i); | |
70 | qreal y = xyseries->y(i); |
|
70 | qreal y = xyseries->y(i); | |
71 | domain.m_minX = qMin(domain.m_minX,x); |
|
71 | domain.m_minX = qMin(domain.m_minX,x); | |
72 | domain.m_minY = qMin(domain.m_minY,y); |
|
72 | domain.m_minY = qMin(domain.m_minY,y); | |
73 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
73 | domain.m_maxX = qMax(domain.m_maxX,x); | |
74 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
74 | domain.m_maxY = qMax(domain.m_maxY,y); | |
75 | } |
|
75 | } | |
76 |
|
76 | |||
77 | XYLineChartItem* item = new XYLineChartItem(xyseries,this); |
|
77 | XYLineChartItem* item = new XYLineChartItem(xyseries,this); | |
78 |
|
78 | |||
79 |
// TODO: combine Chart |
|
79 | // TODO: combine ChartObjectInterface and ChartItem apis | |
80 |
m_chart |
|
80 | m_chartObjectInterfaces << item; | |
81 | item->setTheme(m_chartTheme); |
|
81 | item->setTheme(m_chartTheme); | |
82 |
|
82 | |||
83 |
foreach(Chart |
|
83 | foreach(ChartObjectInterface* i, m_chartObjectInterfaces) | |
84 | i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); |
|
84 | i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); | |
85 |
|
85 | |||
86 | break; |
|
86 | break; | |
87 | } |
|
87 | } | |
88 | case QChartSeries::SeriesTypeBar: { |
|
88 | case QChartSeries::SeriesTypeBar: { | |
89 |
|
89 | |||
90 | qDebug() << "barSeries added"; |
|
90 | qDebug() << "barSeries added"; | |
91 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); |
|
91 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); | |
92 | BarGroup* barGroup = new BarGroup(*barSeries,this); |
|
92 | BarGroup* barGroup = new BarGroup(*barSeries,this); | |
93 |
|
93 | |||
94 | // Add some fugly colors for 5 fist series... |
|
94 | // Add some fugly colors for 5 fist series... | |
95 | barGroup->addColor(QColor(255,0,0,128)); |
|
95 | barGroup->addColor(QColor(255,0,0,128)); | |
96 | barGroup->addColor(QColor(255,255,0,128)); |
|
96 | barGroup->addColor(QColor(255,255,0,128)); | |
97 | barGroup->addColor(QColor(0,255,0,128)); |
|
97 | barGroup->addColor(QColor(0,255,0,128)); | |
98 | barGroup->addColor(QColor(0,0,255,128)); |
|
98 | barGroup->addColor(QColor(0,0,255,128)); | |
99 | barGroup->addColor(QColor(255,128,0,128)); |
|
99 | barGroup->addColor(QColor(255,128,0,128)); | |
100 |
|
100 | |||
101 |
m_chart |
|
101 | m_chartObjectInterfaces << barGroup; | |
102 | childItems().append(barGroup); |
|
102 | childItems().append(barGroup); | |
103 | break; |
|
103 | break; | |
104 | } |
|
104 | } | |
105 | case QChartSeries::SeriesTypeStackedBar: { |
|
105 | case QChartSeries::SeriesTypeStackedBar: { | |
106 |
|
106 | |||
107 | qDebug() << "barSeries added"; |
|
107 | qDebug() << "barSeries added"; | |
108 | StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); |
|
108 | StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); | |
109 | StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this); |
|
109 | StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this); | |
110 |
|
110 | |||
111 | // Add some fugly colors for 5 fist series... |
|
111 | // Add some fugly colors for 5 fist series... | |
112 | stackedBarGroup->addColor(QColor(255,0,0,128)); |
|
112 | stackedBarGroup->addColor(QColor(255,0,0,128)); | |
113 | stackedBarGroup->addColor(QColor(255,255,0,128)); |
|
113 | stackedBarGroup->addColor(QColor(255,255,0,128)); | |
114 | stackedBarGroup->addColor(QColor(0,255,0,128)); |
|
114 | stackedBarGroup->addColor(QColor(0,255,0,128)); | |
115 | stackedBarGroup->addColor(QColor(0,0,255,128)); |
|
115 | stackedBarGroup->addColor(QColor(0,0,255,128)); | |
116 | stackedBarGroup->addColor(QColor(255,128,0,128)); |
|
116 | stackedBarGroup->addColor(QColor(255,128,0,128)); | |
117 |
|
117 | |||
118 |
m_chart |
|
118 | m_chartObjectInterfaces << stackedBarGroup; | |
119 | childItems().append(stackedBarGroup); |
|
119 | childItems().append(stackedBarGroup); | |
120 | break; |
|
120 | break; | |
121 | } |
|
121 | } | |
122 | case QChartSeries::SeriesTypePercentBar: { |
|
122 | case QChartSeries::SeriesTypePercentBar: { | |
123 |
|
123 | |||
124 | qDebug() << "barSeries added"; |
|
124 | qDebug() << "barSeries added"; | |
125 | PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); |
|
125 | PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); | |
126 | PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this); |
|
126 | PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this); | |
127 |
|
127 | |||
128 | // Add some fugly colors for 5 fist series... |
|
128 | // Add some fugly colors for 5 fist series... | |
129 | percentBarGroup->addColor(QColor(255,0,0,128)); |
|
129 | percentBarGroup->addColor(QColor(255,0,0,128)); | |
130 | percentBarGroup->addColor(QColor(255,255,0,128)); |
|
130 | percentBarGroup->addColor(QColor(255,255,0,128)); | |
131 | percentBarGroup->addColor(QColor(0,255,0,128)); |
|
131 | percentBarGroup->addColor(QColor(0,255,0,128)); | |
132 | percentBarGroup->addColor(QColor(0,0,255,128)); |
|
132 | percentBarGroup->addColor(QColor(0,0,255,128)); | |
133 | percentBarGroup->addColor(QColor(255,128,0,128)); |
|
133 | percentBarGroup->addColor(QColor(255,128,0,128)); | |
134 |
|
134 | |||
135 |
m_chart |
|
135 | m_chartObjectInterfaces << percentBarGroup; | |
136 | childItems().append(percentBarGroup); |
|
136 | childItems().append(percentBarGroup); | |
137 | break; |
|
137 | break; | |
138 | } |
|
138 | } | |
139 | case QChartSeries::SeriesTypeScatter: { |
|
139 | case QChartSeries::SeriesTypeScatter: { | |
140 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
140 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | |
141 | scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); |
|
141 | scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); | |
142 | scatterSeries->d->setParentItem(this); |
|
142 | scatterSeries->d->setParentItem(this); | |
143 |
m_chart |
|
143 | m_chartObjectInterfaces << scatterSeries->d; | |
144 | //TODO:? scatterSeries->d->m_themeIndex = m_chartSeries.count() - 1; |
|
144 | //TODO:? scatterSeries->d->m_themeIndex = m_chartSeries.count() - 1; | |
145 | break; |
|
145 | break; | |
146 | } |
|
146 | } | |
147 | case QChartSeries::SeriesTypePie: { |
|
147 | case QChartSeries::SeriesTypePie: { | |
148 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
148 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); | |
149 | // for (int i(0); i < pieSeries->sliceCount(); i++) { |
|
149 | // for (int i(0); i < pieSeries->sliceCount(); i++) { | |
150 | // if (!pieSeries->sliceColor(i).isValid()) |
|
150 | // if (!pieSeries->sliceColor(i).isValid()) | |
151 | // pieSeries->setSliceColor(i, nextColor()); |
|
151 | // pieSeries->setSliceColor(i, nextColor()); | |
152 | // } |
|
152 | // } | |
153 | pieSeries->d->setTheme(m_chartTheme); |
|
153 | pieSeries->d->setTheme(m_chartTheme); | |
154 |
m_chart |
|
154 | m_chartObjectInterfaces << pieSeries->d; | |
155 |
|
155 | |||
156 | // Set pre-defined colors in case the series has no colors defined |
|
156 | // Set pre-defined colors in case the series has no colors defined | |
157 | // TODO: how to define the color for all the slices of a pie? |
|
157 | // TODO: how to define the color for all the slices of a pie? | |
158 | // for (int (i); i < pieSeries.sliceCount(); i++) |
|
158 | // for (int (i); i < pieSeries.sliceCount(); i++) | |
159 | break; |
|
159 | break; | |
160 | } |
|
160 | } | |
161 | } |
|
161 | } | |
162 | } |
|
162 | } | |
163 |
|
163 | |||
164 | QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) |
|
164 | QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) | |
165 | { |
|
165 | { | |
166 | // TODO: support also other types; not only scatter and pie |
|
166 | // TODO: support also other types; not only scatter and pie | |
167 |
|
167 | |||
168 | QChartSeries *series(0); |
|
168 | QChartSeries *series(0); | |
169 |
|
169 | |||
170 | switch (type) { |
|
170 | switch (type) { | |
171 | case QChartSeries::SeriesTypeLine: { |
|
171 | case QChartSeries::SeriesTypeLine: { | |
172 | series = QXYChartSeries::create(); |
|
172 | series = QXYChartSeries::create(); | |
173 | break; |
|
173 | break; | |
174 | } |
|
174 | } | |
175 | case QChartSeries::SeriesTypeBar: { |
|
175 | case QChartSeries::SeriesTypeBar: { | |
176 | series = new BarChartSeries(this); |
|
176 | series = new BarChartSeries(this); | |
177 | break; |
|
177 | break; | |
178 | } |
|
178 | } | |
179 | case QChartSeries::SeriesTypeStackedBar: { |
|
179 | case QChartSeries::SeriesTypeStackedBar: { | |
180 | series = new StackedBarChartSeries(this); |
|
180 | series = new StackedBarChartSeries(this); | |
181 | break; |
|
181 | break; | |
182 | } |
|
182 | } | |
183 | case QChartSeries::SeriesTypePercentBar: { |
|
183 | case QChartSeries::SeriesTypePercentBar: { | |
184 | series = new PercentBarChartSeries(this); |
|
184 | series = new PercentBarChartSeries(this); | |
185 | break; |
|
185 | break; | |
186 | } |
|
186 | } | |
187 | case QChartSeries::SeriesTypeScatter: { |
|
187 | case QChartSeries::SeriesTypeScatter: { | |
188 | series = new QScatterSeries(this); |
|
188 | series = new QScatterSeries(this); | |
189 | break; |
|
189 | break; | |
190 | } |
|
190 | } | |
191 | case QChartSeries::SeriesTypePie: { |
|
191 | case QChartSeries::SeriesTypePie: { | |
192 | series = new QPieSeries(this); |
|
192 | series = new QPieSeries(this); | |
193 | break; |
|
193 | break; | |
194 | } |
|
194 | } | |
195 | default: |
|
195 | default: | |
196 | Q_ASSERT(false); |
|
196 | Q_ASSERT(false); | |
197 | break; |
|
197 | break; | |
198 | } |
|
198 | } | |
199 |
|
199 | |||
200 | addSeries(series); |
|
200 | addSeries(series); | |
201 | return series; |
|
201 | return series; | |
202 | } |
|
202 | } | |
203 |
|
203 | |||
204 | void QChart::setSize(const QSize& size) |
|
204 | void QChart::setSize(const QSize& size) | |
205 | { |
|
205 | { | |
206 | m_rect = QRect(QPoint(0,0),size); |
|
206 | m_rect = QRect(QPoint(0,0),size); | |
207 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
207 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | |
208 |
|
208 | |||
209 | //recaculate title |
|
209 | // recalculate title position | |
210 | if(m_titleItem){ |
|
210 | if (m_titleItem) { | |
211 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
211 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | |
212 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
212 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); | |
213 |
|
||||
214 | } |
|
213 | } | |
215 |
|
214 | |||
216 | //recalculate background gradient |
|
215 | //recalculate background gradient | |
217 | if (m_backgroundItem) { |
|
216 | if (m_backgroundItem) { | |
218 | m_backgroundItem->setRect(rect); |
|
217 | m_backgroundItem->setRect(rect); | |
219 | if (m_bacgroundOrinetation == HorizonatlGradientOrientation) |
|
218 | if (m_bacgroundOrinetation == HorizonatlGradientOrientation) | |
220 | m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0); |
|
219 | m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0); | |
221 | else |
|
220 | else | |
222 | m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height()); |
|
221 | m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height()); | |
223 | m_backgroundItem->setBrush(m_backgroundGradient); |
|
222 | m_backgroundItem->setBrush(m_backgroundGradient); | |
224 | } |
|
223 | } | |
225 |
|
224 | |||
226 | foreach (ChartItemControl *ctrl, m_chartItemControls) { |
|
225 | // resize and reposition childs | |
227 | ctrl->setPos(rect.topLeft()); |
|
226 | foreach (ChartObjectInterface *ctrl, m_chartObjectInterfaces) { | |
228 | ctrl->resize(rect.size()); |
|
227 | QGraphicsItem *item = ctrl->graphicsItem(); | |
|
228 | if (item) | |||
|
229 | item->setPos(rect.topLeft()); | |||
|
230 | ctrl->setSize(rect.size()); | |||
229 | } |
|
231 | } | |
230 |
|
232 | |||
231 | update(); |
|
233 | update(); | |
232 | } |
|
234 | } | |
233 |
|
235 | |||
234 | void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation) |
|
236 | void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation) | |
235 | { |
|
237 | { | |
236 |
|
238 | |||
237 | if(!m_backgroundItem){ |
|
239 | if(!m_backgroundItem){ | |
238 | m_backgroundItem = new QGraphicsRectItem(this); |
|
240 | m_backgroundItem = new QGraphicsRectItem(this); | |
239 | m_backgroundItem->setZValue(-1); |
|
241 | m_backgroundItem->setZValue(-1); | |
240 | } |
|
242 | } | |
241 |
|
243 | |||
242 | m_bacgroundOrinetation = orientation; |
|
244 | m_bacgroundOrinetation = orientation; | |
243 | m_backgroundGradient.setColorAt(0.0, startColor); |
|
245 | m_backgroundGradient.setColorAt(0.0, startColor); | |
244 | m_backgroundGradient.setColorAt(1.0, endColor); |
|
246 | m_backgroundGradient.setColorAt(1.0, endColor); | |
245 | m_backgroundGradient.setStart(0,0); |
|
247 | m_backgroundGradient.setStart(0,0); | |
246 |
|
248 | |||
247 | if(orientation == VerticalGradientOrientation){ |
|
249 | if(orientation == VerticalGradientOrientation){ | |
248 | m_backgroundGradient.setFinalStop(0,m_rect.height()); |
|
250 | m_backgroundGradient.setFinalStop(0,m_rect.height()); | |
249 | }else{ |
|
251 | }else{ | |
250 | m_backgroundGradient.setFinalStop(m_rect.width(),0); |
|
252 | m_backgroundGradient.setFinalStop(m_rect.width(),0); | |
251 | } |
|
253 | } | |
252 |
|
254 | |||
253 | m_backgroundItem->setBrush(m_backgroundGradient); |
|
255 | m_backgroundItem->setBrush(m_backgroundGradient); | |
254 | m_backgroundItem->setPen(Qt::NoPen); |
|
256 | m_backgroundItem->setPen(Qt::NoPen); | |
255 | m_backgroundItem->update(); |
|
257 | m_backgroundItem->update(); | |
256 | } |
|
258 | } | |
257 |
|
259 | |||
258 | void QChart::setTitle(const QString& title,const QFont& font) |
|
260 | void QChart::setTitle(const QString& title,const QFont& font) | |
259 | { |
|
261 | { | |
260 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); |
|
262 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); | |
261 | m_titleItem->setPlainText(title); |
|
263 | m_titleItem->setPlainText(title); | |
262 | m_titleItem->setFont(font); |
|
264 | m_titleItem->setFont(font); | |
263 | } |
|
265 | } | |
264 |
|
266 | |||
265 | int QChart::margin() const |
|
267 | int QChart::margin() const | |
266 | { |
|
268 | { | |
267 | return m_marginSize; |
|
269 | return m_marginSize; | |
268 | } |
|
270 | } | |
269 |
|
271 | |||
270 | void QChart::setMargin(int margin) |
|
272 | void QChart::setMargin(int margin) | |
271 | { |
|
273 | { | |
272 | m_marginSize = margin; |
|
274 | m_marginSize = margin; | |
273 | } |
|
275 | } | |
274 |
|
276 | |||
275 | void QChart::setTheme(QChart::ChartThemeId theme) |
|
277 | void QChart::setTheme(QChart::ChartThemeId theme) | |
276 | { |
|
278 | { | |
277 | if (theme != m_chartTheme->d->m_currentTheme) { |
|
279 | if (theme != m_chartTheme->d->m_currentTheme) { | |
278 | m_chartTheme->d->setTheme(theme); |
|
280 | m_chartTheme->d->setTheme(theme); | |
279 | setBackground(m_chartTheme->d->m_gradientStartColor, |
|
281 | setBackground(m_chartTheme->d->m_gradientStartColor, | |
280 | m_chartTheme->d->m_gradientEndColor, |
|
282 | m_chartTheme->d->m_gradientEndColor, | |
281 | m_bacgroundOrinetation); |
|
283 | m_bacgroundOrinetation); | |
282 |
foreach (Chart |
|
284 | foreach (ChartObjectInterface *ctrl, m_chartObjectInterfaces) | |
283 | ctrl->setTheme(m_chartTheme); |
|
285 | ctrl->setTheme(m_chartTheme); | |
284 | update(); |
|
286 | update(); | |
285 | } |
|
287 | } | |
286 | } |
|
288 | } | |
287 |
|
289 | |||
288 | void QChart::zoomInToRect(const QRect& rectangle) |
|
290 | void QChart::zoomInToRect(const QRect& rectangle) | |
289 | { |
|
291 | { | |
290 |
|
292 | |||
291 | if(!rectangle.isValid()) return; |
|
293 | if(!rectangle.isValid()) return; | |
292 |
|
294 | |||
293 | qreal margin = this->margin(); |
|
295 | qreal margin = this->margin(); | |
294 |
|
296 | |||
295 | QRect rect = rectangle.normalized(); |
|
297 | QRect rect = rectangle.normalized(); | |
296 | rect.translate(-margin, -margin); |
|
298 | rect.translate(-margin, -margin); | |
297 |
|
299 | |||
298 | PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; |
|
300 | PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; | |
299 |
|
301 | |||
300 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); |
|
302 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); | |
301 |
|
303 | |||
302 | m_plotDomainList.resize(m_plotDataIndex + 1); |
|
304 | m_plotDomainList.resize(m_plotDataIndex + 1); | |
303 | m_plotDomainList<<domain; |
|
305 | m_plotDomainList<<domain; | |
304 | m_plotDataIndex++; |
|
306 | m_plotDataIndex++; | |
305 |
|
307 | |||
306 |
foreach (Chart |
|
308 | foreach (ChartObjectInterface* ctrl, m_chartObjectInterfaces) | |
307 | ctrl->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
309 | ctrl->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | |
308 | update(); |
|
310 | update(); | |
309 | } |
|
311 | } | |
310 |
|
312 | |||
311 | void QChart::zoomIn() |
|
313 | void QChart::zoomIn() | |
312 | { |
|
314 | { | |
313 | if (m_plotDataIndex < m_plotDomainList.count() - 1) { |
|
315 | if (m_plotDataIndex < m_plotDomainList.count() - 1) { | |
314 | m_plotDataIndex++; |
|
316 | m_plotDataIndex++; | |
315 |
foreach (Chart |
|
317 | foreach (ChartObjectInterface* item, m_chartObjectInterfaces) | |
316 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
318 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | |
317 | update(); |
|
319 | update(); | |
318 | } else { |
|
320 | } else { | |
319 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
321 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | |
320 | rect.setWidth(rect.width()/2); |
|
322 | rect.setWidth(rect.width()/2); | |
321 | rect.setHeight(rect.height()/2); |
|
323 | rect.setHeight(rect.height()/2); | |
322 | rect.moveCenter(m_rect.center()); |
|
324 | rect.moveCenter(m_rect.center()); | |
323 | zoomInToRect(rect); |
|
325 | zoomInToRect(rect); | |
324 | } |
|
326 | } | |
325 | } |
|
327 | } | |
326 |
|
328 | |||
327 | void QChart::zoomOut() |
|
329 | void QChart::zoomOut() | |
328 | { |
|
330 | { | |
329 | if (m_plotDataIndex > 0) { |
|
331 | if (m_plotDataIndex > 0) { | |
330 | m_plotDataIndex--; |
|
332 | m_plotDataIndex--; | |
331 |
foreach (Chart |
|
333 | foreach (ChartObjectInterface* item, m_chartObjectInterfaces) | |
332 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
334 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | |
333 | update(); |
|
335 | update(); | |
334 | } |
|
336 | } | |
335 | } |
|
337 | } | |
336 |
|
338 | |||
337 | void QChart::zoomReset() |
|
339 | void QChart::zoomReset() | |
338 | { |
|
340 | { | |
339 | if (m_plotDataIndex > 0) { |
|
341 | if (m_plotDataIndex > 0) { | |
340 | m_plotDataIndex = 0; |
|
342 | m_plotDataIndex = 0; | |
341 |
foreach (Chart |
|
343 | foreach (ChartObjectInterface* item, m_chartObjectInterfaces) | |
342 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
344 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); | |
343 | update(); |
|
345 | update(); | |
344 | } |
|
346 | } | |
345 | } |
|
347 | } | |
346 |
|
348 | |||
347 | void QChart::setAxisX(const QChartAxis& axis) |
|
349 | void QChart::setAxisX(const QChartAxis& axis) | |
348 | { |
|
350 | { | |
349 | setAxis(m_axisXItem,axis); |
|
351 | setAxis(m_axisXItem,axis); | |
350 | } |
|
352 | } | |
351 | void QChart::setAxisY(const QChartAxis& axis) |
|
353 | void QChart::setAxisY(const QChartAxis& axis) | |
352 | { |
|
354 | { | |
353 | setAxis(m_axisYItem.at(0),axis); |
|
355 | setAxis(m_axisYItem.at(0),axis); | |
354 | } |
|
356 | } | |
355 |
|
357 | |||
356 | void QChart::setAxisY(const QList<QChartAxis>& axis) |
|
358 | void QChart::setAxisY(const QList<QChartAxis>& axis) | |
357 | { |
|
359 | { | |
358 | //TODO not implemented |
|
360 | //TODO not implemented | |
359 | } |
|
361 | } | |
360 |
|
362 | |||
361 | void QChart::setAxis(AxisItem *item, const QChartAxis& axis) |
|
363 | void QChart::setAxis(AxisItem *item, const QChartAxis& axis) | |
362 | { |
|
364 | { | |
363 | item->setVisible(axis.isAxisVisible()); |
|
365 | item->setVisible(axis.isAxisVisible()); | |
364 | } |
|
366 | } | |
365 |
|
367 | |||
366 | #include "moc_qchart.cpp" |
|
368 | #include "moc_qchart.cpp" | |
367 |
|
369 | |||
368 | QTCOMMERCIALCHART_END_NAMESPACE |
|
370 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,99 +1,99 | |||||
1 | #ifndef CHART_H |
|
1 | #ifndef CHART_H | |
2 | #define CHART_H |
|
2 | #define CHART_H | |
3 |
|
3 | |||
4 | #include <qchartglobal.h> |
|
4 | #include <qchartglobal.h> | |
5 | #include <qchartseries.h> |
|
5 | #include <qchartseries.h> | |
6 | #include <QGraphicsObject> |
|
6 | #include <QGraphicsObject> | |
7 | #include <QLinearGradient> |
|
7 | #include <QLinearGradient> | |
8 | #include <QFont> |
|
8 | #include <QFont> | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | class AxisItem; |
|
12 | class AxisItem; | |
13 | class QChartSeries; |
|
13 | class QChartSeries; | |
14 | class PlotDomain; |
|
14 | class PlotDomain; | |
15 | class BarGroup; |
|
15 | class BarGroup; | |
16 | class QChartAxis; |
|
16 | class QChartAxis; | |
17 | class ChartTheme; |
|
17 | class ChartTheme; | |
18 |
class Chart |
|
18 | class ChartObjectInterface; | |
19 |
|
19 | |||
20 | // TODO: We don't need to have QChart tied to QGraphicsItem: |
|
20 | // TODO: We don't need to have QChart tied to QGraphicsItem: | |
21 | //class QTCOMMERCIALCHART_EXPORT QChart |
|
21 | //class QTCOMMERCIALCHART_EXPORT QChart | |
22 | //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem { |
|
22 | //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem { | |
23 | // public: QChartGraphicsItem(QChart &chart); |
|
23 | // public: QChartGraphicsItem(QChart &chart); | |
24 |
|
24 | |||
25 | /*! |
|
25 | /*! | |
26 | * TODO: define the responsibilities |
|
26 | * TODO: define the responsibilities | |
27 | */ |
|
27 | */ | |
28 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject |
|
28 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject | |
29 | { |
|
29 | { | |
30 | Q_OBJECT |
|
30 | Q_OBJECT | |
31 | public: |
|
31 | public: | |
32 | enum GradientOrientation { |
|
32 | enum GradientOrientation { | |
33 | HorizonatlGradientOrientation, |
|
33 | HorizonatlGradientOrientation, | |
34 | VerticalGradientOrientation |
|
34 | VerticalGradientOrientation | |
35 | }; |
|
35 | }; | |
36 | enum ChartThemeId { |
|
36 | enum ChartThemeId { | |
37 | ChartThemeInvalid = -1, |
|
37 | ChartThemeInvalid = -1, | |
38 | /*! The default theme follows the GUI style of the Operating System */ |
|
38 | /*! The default theme follows the GUI style of the Operating System */ | |
39 | ChartThemeDefault, |
|
39 | ChartThemeDefault, | |
40 | ChartThemeVanilla, |
|
40 | ChartThemeVanilla, | |
41 | ChartThemeIcy, |
|
41 | ChartThemeIcy, | |
42 | ChartThemeGrayscale, |
|
42 | ChartThemeGrayscale, | |
43 | //ChartThemeScientific, |
|
43 | //ChartThemeScientific, | |
44 | ChartThemeUnnamed1 |
|
44 | ChartThemeUnnamed1 | |
45 | }; |
|
45 | }; | |
46 |
|
46 | |||
47 | public: |
|
47 | public: | |
48 | QChart(QGraphicsObject* parent = 0); |
|
48 | QChart(QGraphicsObject* parent = 0); | |
49 | ~QChart(); |
|
49 | ~QChart(); | |
50 |
|
50 | |||
51 | //from QGraphicsItem |
|
51 | //from QGraphicsItem | |
52 | QRectF boundingRect() const; |
|
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 | void addSeries(QChartSeries* series); |
|
55 | void addSeries(QChartSeries* series); | |
56 | //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type); |
|
56 | //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type); | |
57 | // TODO: who owns the series now? maybe owned by chart and returned a reference instead... |
|
57 | // TODO: who owns the series now? maybe owned by chart and returned a reference instead... | |
58 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
58 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); | |
59 |
|
59 | |||
60 | void setSize(const QSize& size); |
|
60 | void setSize(const QSize& size); | |
61 | void setMargin(int margin); |
|
61 | void setMargin(int margin); | |
62 | int margin() const; |
|
62 | int margin() const; | |
63 | void setTheme(QChart::ChartThemeId theme); |
|
63 | void setTheme(QChart::ChartThemeId theme); | |
64 |
|
64 | |||
65 | void setTitle(const QString& title,const QFont& font = QFont()); |
|
65 | void setTitle(const QString& title,const QFont& font = QFont()); | |
66 | void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation); |
|
66 | void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation); | |
67 |
|
67 | |||
68 | void zoomInToRect(const QRect& rectangle); |
|
68 | void zoomInToRect(const QRect& rectangle); | |
69 | void zoomIn(); |
|
69 | void zoomIn(); | |
70 | void zoomOut(); |
|
70 | void zoomOut(); | |
71 | void zoomReset(); |
|
71 | void zoomReset(); | |
72 |
|
72 | |||
73 | void setAxisX(const QChartAxis& axis); |
|
73 | void setAxisX(const QChartAxis& axis); | |
74 | void setAxisY(const QChartAxis& axis); |
|
74 | void setAxisY(const QChartAxis& axis); | |
75 | void setAxisY(const QList<QChartAxis>& axis); |
|
75 | void setAxisY(const QList<QChartAxis>& axis); | |
76 |
|
76 | |||
77 | private: |
|
77 | private: | |
78 | void setAxis(AxisItem *item, const QChartAxis& axis); |
|
78 | void setAxis(AxisItem *item, const QChartAxis& axis); | |
79 |
|
79 | |||
80 | private: |
|
80 | private: | |
81 | Q_DISABLE_COPY(QChart) |
|
81 | Q_DISABLE_COPY(QChart) | |
82 | QGraphicsRectItem* m_backgroundItem; |
|
82 | QGraphicsRectItem* m_backgroundItem; | |
83 | QLinearGradient m_backgroundGradient; |
|
83 | QLinearGradient m_backgroundGradient; | |
84 | GradientOrientation m_bacgroundOrinetation; |
|
84 | GradientOrientation m_bacgroundOrinetation; | |
85 | QGraphicsTextItem* m_titleItem; |
|
85 | QGraphicsTextItem* m_titleItem; | |
86 | AxisItem* m_axisXItem; |
|
86 | AxisItem* m_axisXItem; | |
87 | QList<AxisItem*> m_axisYItem; |
|
87 | QList<AxisItem*> m_axisYItem; | |
88 | QRect m_rect; |
|
88 | QRect m_rect; | |
89 | QList<QChartSeries *> m_chartSeries; |
|
89 | QList<QChartSeries *> m_chartSeries; | |
90 |
QList<Chart |
|
90 | QList<ChartObjectInterface *> m_chartObjectInterfaces; | |
91 | QVector<PlotDomain> m_plotDomainList; |
|
91 | QVector<PlotDomain> m_plotDomainList; | |
92 | int m_plotDataIndex; |
|
92 | int m_plotDataIndex; | |
93 | int m_marginSize; |
|
93 | int m_marginSize; | |
94 | ChartTheme *m_chartTheme; |
|
94 | ChartTheme *m_chartTheme; | |
95 | }; |
|
95 | }; | |
96 |
|
96 | |||
97 | QTCOMMERCIALCHART_END_NAMESPACE |
|
97 | QTCOMMERCIALCHART_END_NAMESPACE | |
98 |
|
98 | |||
99 | #endif |
|
99 | #endif |
@@ -1,192 +1,187 | |||||
1 | #include "qpieseries_p.h" |
|
1 | #include "qpieseries_p.h" | |
2 | #include "qpieseries.h" |
|
2 | #include "qpieseries.h" | |
3 | #include <QGraphicsObject> |
|
3 | #include <QGraphicsObject> | |
4 | #include "pieslice.h" |
|
4 | #include "pieslice.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | QPieSeriesPrivate::QPieSeriesPrivate() : |
|
9 | QPieSeriesPrivate::QPieSeriesPrivate() : | |
10 | m_sizeFactor(1.0), |
|
10 | m_sizeFactor(1.0), | |
11 | m_position(QPieSeries::PiePositionMaximized) |
|
11 | m_position(QPieSeries::PiePositionMaximized) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | QPieSeriesPrivate::~QPieSeriesPrivate() |
|
15 | QPieSeriesPrivate::~QPieSeriesPrivate() | |
16 | { |
|
16 | { | |
17 | while (m_slices.count()) |
|
17 | while (m_slices.count()) | |
18 | delete m_slices.takeLast(); |
|
18 | delete m_slices.takeLast(); | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | bool QPieSeriesPrivate::setData(QList<qreal> data) |
|
21 | bool QPieSeriesPrivate::setData(QList<qreal> data) | |
22 | { |
|
22 | { | |
23 | m_data = data; |
|
23 | m_data = data; | |
24 |
|
24 | |||
25 | if (m_parentItem) { |
|
25 | if (m_parentItem) { | |
26 | // Create slices |
|
26 | // Create slices | |
27 | qreal fullPie = 360; |
|
27 | qreal fullPie = 360; | |
28 | qreal total = 0; |
|
28 | qreal total = 0; | |
29 | foreach (qreal value, m_data) |
|
29 | foreach (qreal value, m_data) | |
30 | total += value; |
|
30 | total += value; | |
31 |
|
31 | |||
32 | m_chartSize = m_parentItem->boundingRect(); |
|
32 | m_chartSize = m_parentItem->boundingRect(); | |
33 | qreal angle = 0; |
|
33 | qreal angle = 0; | |
34 | // TODO: no need to create new slices in case size changed; we should re-use the existing ones |
|
34 | // TODO: no need to create new slices in case size changed; we should re-use the existing ones | |
35 | foreach (qreal value, m_data) { |
|
35 | foreach (qreal value, m_data) { | |
36 | qreal span = value / total * fullPie; |
|
36 | qreal span = value / total * fullPie; | |
37 | PieSlice *slice = new PieSlice(QColor(), angle, span, m_parentItem->boundingRect()); |
|
37 | PieSlice *slice = new PieSlice(QColor(), angle, span, m_parentItem->boundingRect()); | |
38 | slice->setParentItem(m_parentItem); |
|
38 | slice->setParentItem(m_parentItem); | |
39 | m_slices.append(slice); |
|
39 | m_slices.append(slice); | |
40 | angle += span; |
|
40 | angle += span; | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | setTheme(m_chartTheme); |
|
43 | setTheme(m_chartTheme); | |
44 | resizeSlices(m_chartSize); |
|
44 | resizeSlices(m_chartSize); | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | return true; |
|
47 | return true; | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 |
void QPieSeriesPrivate::set |
|
50 | void QPieSeriesPrivate::setSize(const QSize &size) | |
51 | { |
|
|||
52 | // TODO |
|
|||
53 | } |
|
|||
54 |
|
||||
55 | void QPieSeriesPrivate::resize(const QSize &size) |
|
|||
56 | { |
|
51 | { | |
57 | // TODO: allow user setting the size? |
|
52 | // TODO: allow user setting the size? | |
58 | // TODO: allow user defining the margins? |
|
53 | // TODO: allow user defining the margins? | |
59 | m_chartSize = QRect(0, 0, size.width(), size.height()); |
|
54 | m_chartSize = QRect(0, 0, size.width(), size.height()); | |
60 | resizeSlices(m_chartSize); |
|
55 | resizeSlices(m_chartSize); | |
61 | } |
|
56 | } | |
62 |
|
57 | |||
63 | void QPieSeriesPrivate::setTheme(ChartTheme *theme) |
|
58 | void QPieSeriesPrivate::setTheme(ChartTheme *theme) | |
64 | { |
|
59 | { | |
65 | if (theme) { |
|
60 | if (theme) { | |
66 | m_chartTheme = theme; |
|
61 | m_chartTheme = theme; | |
67 | for (int i(0); i < m_slices.count(); i++) |
|
62 | for (int i(0); i < m_slices.count(); i++) | |
68 | m_slices.at(i)->m_theme = theme->themeForSeries(); |
|
63 | m_slices.at(i)->m_theme = theme->themeForSeries(); | |
69 | } |
|
64 | } | |
70 | } |
|
65 | } | |
71 |
|
66 | |||
72 | void QPieSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain) |
|
67 | void QPieSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain) | |
73 | { |
|
68 | { | |
74 | // TODO |
|
69 | // TODO | |
75 | } |
|
70 | } | |
76 |
|
71 | |||
77 | void QPieSeriesPrivate::resizeSlices(QRectF rect) |
|
72 | void QPieSeriesPrivate::resizeSlices(QRectF rect) | |
78 | { |
|
73 | { | |
79 | QRectF tempRect = rect; |
|
74 | QRectF tempRect = rect; | |
80 | if (tempRect.width() < tempRect.height()) { |
|
75 | if (tempRect.width() < tempRect.height()) { | |
81 | tempRect.setWidth(tempRect.width() * m_sizeFactor); |
|
76 | tempRect.setWidth(tempRect.width() * m_sizeFactor); | |
82 | tempRect.setHeight(tempRect.width()); |
|
77 | tempRect.setHeight(tempRect.width()); | |
83 | tempRect.moveCenter(rect.center()); |
|
78 | tempRect.moveCenter(rect.center()); | |
84 | } else { |
|
79 | } else { | |
85 | tempRect.setHeight(tempRect.height() * m_sizeFactor); |
|
80 | tempRect.setHeight(tempRect.height() * m_sizeFactor); | |
86 | tempRect.setWidth(tempRect.height()); |
|
81 | tempRect.setWidth(tempRect.height()); | |
87 | tempRect.moveCenter(rect.center()); |
|
82 | tempRect.moveCenter(rect.center()); | |
88 | } |
|
83 | } | |
89 |
|
84 | |||
90 | switch (m_position) { |
|
85 | switch (m_position) { | |
91 | case QPieSeries::PiePositionTopLeft: { |
|
86 | case QPieSeries::PiePositionTopLeft: { | |
92 | tempRect.setHeight(tempRect.height() / 2); |
|
87 | tempRect.setHeight(tempRect.height() / 2); | |
93 | tempRect.setWidth(tempRect.height()); |
|
88 | tempRect.setWidth(tempRect.height()); | |
94 | tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2)); |
|
89 | tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2)); | |
95 | break; |
|
90 | break; | |
96 | } |
|
91 | } | |
97 | case QPieSeries::PiePositionTopRight: { |
|
92 | case QPieSeries::PiePositionTopRight: { | |
98 | tempRect.setHeight(tempRect.height() / 2); |
|
93 | tempRect.setHeight(tempRect.height() / 2); | |
99 | tempRect.setWidth(tempRect.height()); |
|
94 | tempRect.setWidth(tempRect.height()); | |
100 | tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2)); |
|
95 | tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2)); | |
101 | break; |
|
96 | break; | |
102 | } |
|
97 | } | |
103 | case QPieSeries::PiePositionBottomLeft: { |
|
98 | case QPieSeries::PiePositionBottomLeft: { | |
104 | tempRect.setHeight(tempRect.height() / 2); |
|
99 | tempRect.setHeight(tempRect.height() / 2); | |
105 | tempRect.setWidth(tempRect.height()); |
|
100 | tempRect.setWidth(tempRect.height()); | |
106 | tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3)); |
|
101 | tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3)); | |
107 | break; |
|
102 | break; | |
108 | } |
|
103 | } | |
109 | case QPieSeries::PiePositionBottomRight: { |
|
104 | case QPieSeries::PiePositionBottomRight: { | |
110 | tempRect.setHeight(tempRect.height() / 2); |
|
105 | tempRect.setHeight(tempRect.height() / 2); | |
111 | tempRect.setWidth(tempRect.height()); |
|
106 | tempRect.setWidth(tempRect.height()); | |
112 | tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3)); |
|
107 | tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3)); | |
113 | break; |
|
108 | break; | |
114 | } |
|
109 | } | |
115 | default: |
|
110 | default: | |
116 | break; |
|
111 | break; | |
117 | } |
|
112 | } | |
118 |
|
113 | |||
119 | foreach (PieSlice *slice, m_slices) |
|
114 | foreach (PieSlice *slice, m_slices) | |
120 | slice->m_rect = tempRect; |
|
115 | slice->m_rect = tempRect; | |
121 | } |
|
116 | } | |
122 |
|
117 | |||
123 | QPieSeries::QPieSeries(QGraphicsObject *parent) : |
|
118 | QPieSeries::QPieSeries(QGraphicsObject *parent) : | |
124 | QChartSeries(parent), |
|
119 | QChartSeries(parent), | |
125 | d(new QPieSeriesPrivate()) |
|
120 | d(new QPieSeriesPrivate()) | |
126 | { |
|
121 | { | |
127 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent); |
|
122 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent); | |
128 | if (parentItem) |
|
123 | if (parentItem) | |
129 | d->m_parentItem = parentItem; |
|
124 | d->m_parentItem = parentItem; | |
130 | } |
|
125 | } | |
131 |
|
126 | |||
132 | QPieSeries::~QPieSeries() |
|
127 | QPieSeries::~QPieSeries() | |
133 | { |
|
128 | { | |
134 | delete d; |
|
129 | delete d; | |
135 | } |
|
130 | } | |
136 |
|
131 | |||
137 | bool QPieSeries::setData(QList<qreal> data) |
|
132 | bool QPieSeries::setData(QList<qreal> data) | |
138 | { |
|
133 | { | |
139 | return d->setData(data); |
|
134 | return d->setData(data); | |
140 | } |
|
135 | } | |
141 |
|
136 | |||
142 | void QPieSeries::setSliceColor(int index, QColor color) |
|
137 | void QPieSeries::setSliceColor(int index, QColor color) | |
143 | { |
|
138 | { | |
144 | if (index >= 0 && index < d->m_slices.count()) |
|
139 | if (index >= 0 && index < d->m_slices.count()) | |
145 | d->m_slices.at(index)->m_color = color; |
|
140 | d->m_slices.at(index)->m_color = color; | |
146 | } |
|
141 | } | |
147 |
|
142 | |||
148 | QColor QPieSeries::sliceColor(int index) |
|
143 | QColor QPieSeries::sliceColor(int index) | |
149 | { |
|
144 | { | |
150 | if (index >= 0 && index < d->m_slices.count()) |
|
145 | if (index >= 0 && index < d->m_slices.count()) | |
151 | return d->m_slices.at(index)->m_color; |
|
146 | return d->m_slices.at(index)->m_color; | |
152 | else |
|
147 | else | |
153 | return QColor(); |
|
148 | return QColor(); | |
154 | } |
|
149 | } | |
155 |
|
150 | |||
156 | int QPieSeries::sliceCount() |
|
151 | int QPieSeries::sliceCount() | |
157 | { |
|
152 | { | |
158 | return d->m_slices.count(); |
|
153 | return d->m_slices.count(); | |
159 | } |
|
154 | } | |
160 |
|
155 | |||
161 | void QPieSeries::setSizeFactor(qreal factor) |
|
156 | void QPieSeries::setSizeFactor(qreal factor) | |
162 | { |
|
157 | { | |
163 | if (factor > 0.0) |
|
158 | if (factor > 0.0) | |
164 | d->m_sizeFactor = factor; |
|
159 | d->m_sizeFactor = factor; | |
165 | d->resizeSlices(d->m_chartSize); |
|
160 | d->resizeSlices(d->m_chartSize); | |
166 |
|
161 | |||
167 | // Initiate update via the parent graphics item |
|
162 | // Initiate update via the parent graphics item | |
168 | // TODO: potential issue: what if this function is called from the parent context? |
|
163 | // TODO: potential issue: what if this function is called from the parent context? | |
169 | if (d->m_parentItem) |
|
164 | if (d->m_parentItem) | |
170 | d->m_parentItem->update(); |
|
165 | d->m_parentItem->update(); | |
171 | } |
|
166 | } | |
172 |
|
167 | |||
173 | qreal QPieSeries::sizeFactor() |
|
168 | qreal QPieSeries::sizeFactor() | |
174 | { |
|
169 | { | |
175 | return d->m_sizeFactor; |
|
170 | return d->m_sizeFactor; | |
176 | } |
|
171 | } | |
177 |
|
172 | |||
178 | void QPieSeries::setPosition(PiePosition position) |
|
173 | void QPieSeries::setPosition(PiePosition position) | |
179 | { |
|
174 | { | |
180 | d->m_position = position; |
|
175 | d->m_position = position; | |
181 | d->resizeSlices(d->m_chartSize); |
|
176 | d->resizeSlices(d->m_chartSize); | |
182 |
|
177 | |||
183 | // Initiate update via the parent graphics item |
|
178 | // Initiate update via the parent graphics item | |
184 | // TODO: potential issue: what if this function is called from the parent context? |
|
179 | // TODO: potential issue: what if this function is called from the parent context? | |
185 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); |
|
180 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); | |
186 | Q_ASSERT(parentItem); |
|
181 | Q_ASSERT(parentItem); | |
187 | parentItem->update(); |
|
182 | parentItem->update(); | |
188 | } |
|
183 | } | |
189 |
|
184 | |||
190 | #include "moc_qpieseries.cpp" |
|
185 | #include "moc_qpieseries.cpp" | |
191 |
|
186 | |||
192 | QTCOMMERCIALCHART_END_NAMESPACE |
|
187 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,40 +1,39 | |||||
1 | #ifndef PIESERIESPRIVATE_H |
|
1 | #ifndef PIESERIESPRIVATE_H | |
2 | #define PIESERIESPRIVATE_H |
|
2 | #define PIESERIESPRIVATE_H | |
3 |
|
3 | |||
4 |
#include "chart |
|
4 | #include "chartobjectinterface_p.h" | |
5 | #include "qpieseries.h" |
|
5 | #include "qpieseries.h" | |
6 | #include <QRectF> |
|
6 | #include <QRectF> | |
7 | #include <QColor> |
|
7 | #include <QColor> | |
8 |
|
8 | |||
9 | class QGraphicsItem; |
|
9 | class QGraphicsItem; | |
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 | class PieSlice; |
|
11 | class PieSlice; | |
12 |
|
12 | |||
13 |
class QPieSeriesPrivate : public Chart |
|
13 | class QPieSeriesPrivate : public ChartObjectInterface | |
14 | { |
|
14 | { | |
15 | public: |
|
15 | public: | |
16 | // TODO: use a generic data class instead of x and y |
|
16 | // TODO: use a generic data class instead of x and y | |
17 | QPieSeriesPrivate(); |
|
17 | QPieSeriesPrivate(); | |
18 | ~QPieSeriesPrivate(); |
|
18 | ~QPieSeriesPrivate(); | |
19 |
|
19 | |||
20 |
public: // from Chart |
|
20 | public: // from ChartObjectInterface | |
21 |
void set |
|
21 | void setSize(const QSize &size); | |
22 | void resize(const QSize &size); |
|
|||
23 | void setTheme(ChartTheme *theme); |
|
22 | void setTheme(ChartTheme *theme); | |
24 | void setPlotDomain(const PlotDomain& data); |
|
23 | void setPlotDomain(const PlotDomain& data); | |
25 |
|
24 | |||
26 | public: |
|
25 | public: | |
27 | bool setData(QList<qreal> data); |
|
26 | bool setData(QList<qreal> data); | |
28 | void resizeSlices(QRectF rect); |
|
27 | void resizeSlices(QRectF rect); | |
29 | QGraphicsItem *m_parentItem; |
|
28 | QGraphicsItem *m_parentItem; | |
30 | QList<qreal> m_data; |
|
29 | QList<qreal> m_data; | |
31 | QList<PieSlice*> m_slices; |
|
30 | QList<PieSlice*> m_slices; | |
32 | QRectF m_chartSize; |
|
31 | QRectF m_chartSize; | |
33 | qreal m_sizeFactor; |
|
32 | qreal m_sizeFactor; | |
34 | QPieSeries::PiePosition m_position; |
|
33 | QPieSeries::PiePosition m_position; | |
35 | ChartTheme *m_chartTheme; |
|
34 | ChartTheme *m_chartTheme; | |
36 | }; |
|
35 | }; | |
37 |
|
36 | |||
38 | QTCOMMERCIALCHART_END_NAMESPACE |
|
37 | QTCOMMERCIALCHART_END_NAMESPACE | |
39 |
|
38 | |||
40 | #endif // PIESERIESPRIVATE_H |
|
39 | #endif // PIESERIESPRIVATE_H |
@@ -1,125 +1,121 | |||||
1 | #include "qscatterseries.h" |
|
1 | #include "qscatterseries.h" | |
2 | #include "qscatterseries_p.h" |
|
2 | #include "qscatterseries_p.h" | |
3 | #include "qchart.h" |
|
3 | #include "qchart.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 | #include <QGraphicsScene> |
|
5 | #include <QGraphicsScene> | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | //#define QSeriesData QList<qreal> |
|
10 | //#define QSeriesData QList<qreal> | |
11 |
|
11 | |||
12 | QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) : |
|
12 | QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) : | |
13 |
|
|
13 | ChartItem(parent), | |
14 | m_scalex(100), // TODO: let the use define the scale (or autoscaled) |
|
14 | m_scalex(100), // TODO: let the use define the scale (or autoscaled) | |
15 | m_scaley(100), |
|
15 | m_scaley(100), | |
16 | m_markerColor(QColor()) |
|
16 | m_markerColor(QColor()) | |
17 | { |
|
17 | { | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | void QScatterSeriesPrivate::resize(QRectF rect) |
|
20 | void QScatterSeriesPrivate::resize(QRectF rect) | |
21 | { |
|
21 | { | |
22 | m_scenex.clear(); |
|
22 | m_scenex.clear(); | |
23 | m_sceney.clear(); |
|
23 | m_sceney.clear(); | |
24 |
|
24 | |||
25 | foreach(qreal x, m_x) |
|
25 | foreach(qreal x, m_x) | |
26 | m_scenex.append(rect.left() + x * (rect.width() / m_scalex)); |
|
26 | m_scenex.append(rect.left() + x * (rect.width() / m_scalex)); | |
27 |
|
27 | |||
28 | foreach(qreal y, m_y) |
|
28 | foreach(qreal y, m_y) | |
29 | m_sceney.append(rect.bottom() - y * (rect.height() / m_scaley)); |
|
29 | m_sceney.append(rect.bottom() - y * (rect.height() / m_scaley)); | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 | // TODO: |
|
32 | void QScatterSeriesPrivate::setSize(const QSize &size) | |
33 | //void QScatterSeriesPrivate::setAxisScale(qreal xscale, qreal yscale) |
|
33 | { | |
|
34 | QGraphicsItem *parent = this->parentItem(); | |||
|
35 | if (parent) | |||
|
36 | resize(QRectF(parent->pos(), size)); | |||
|
37 | } | |||
|
38 | ||||
|
39 | void QScatterSeriesPrivate::setTheme(ChartTheme *theme) | |||
|
40 | { | |||
|
41 | m_theme = theme->themeForSeries(); | |||
|
42 | } | |||
|
43 | ||||
|
44 | void QScatterSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain) | |||
|
45 | { | |||
|
46 | // TODO | |||
|
47 | } | |||
34 |
|
48 | |||
35 | QRectF QScatterSeriesPrivate::boundingRect() const |
|
49 | QRectF QScatterSeriesPrivate::boundingRect() const | |
36 | { |
|
50 | { | |
37 | return QRectF(0, 0, 55, 100); |
|
51 | return QRectF(0, 0, 55, 100); | |
38 | } |
|
52 | } | |
39 |
|
53 | |||
40 | void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
54 | void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
41 | { |
|
55 | { | |
42 | // TODO: The opacity should be user definable? |
|
56 | // TODO: The opacity should be user definable? | |
43 | //brush.setColor(QColor(255, 82, 0, 100)); |
|
57 | //brush.setColor(QColor(255, 82, 0, 100)); | |
44 | if (m_markerColor.isValid()) { |
|
58 | if (m_markerColor.isValid()) { | |
45 | QPen pen = painter->pen(); |
|
59 | QPen pen = painter->pen(); | |
46 | QBrush brush = pen.brush(); |
|
60 | QBrush brush = pen.brush(); | |
47 | brush.setColor(m_markerColor); |
|
61 | brush.setColor(m_markerColor); | |
48 | pen.setBrush(brush); |
|
62 | pen.setBrush(brush); | |
49 | pen.setWidth(4); |
|
63 | pen.setWidth(4); | |
50 | painter->setPen(pen); |
|
64 | painter->setPen(pen); | |
51 | } |
|
65 | } | |
52 | else |
|
66 | else | |
53 | painter->setPen(m_theme.markerPen); |
|
67 | painter->setPen(m_theme.markerPen); | |
54 | // brush.setColor(m_theme..lineColor); |
|
68 | // brush.setColor(m_theme..lineColor); | |
55 |
|
69 | |||
56 | // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize |
|
70 | // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize | |
57 | // event right after construction or maybe given a size during initialization |
|
71 | // event right after construction or maybe given a size during initialization | |
58 | for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) { |
|
72 | for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) { | |
59 | if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i)) |
|
73 | if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i)) | |
60 | //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760); |
|
74 | //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760); | |
61 | painter->drawPoint(m_scenex.at(i), m_sceney.at(i)); |
|
75 | painter->drawPoint(m_scenex.at(i), m_sceney.at(i)); | |
62 | } |
|
76 | } | |
63 | } |
|
77 | } | |
64 |
|
78 | |||
65 | void QScatterSeriesPrivate::setPos(const QPointF & pos) |
|
|||
66 | { |
|
|||
67 | // TODO |
|
|||
68 | } |
|
|||
69 |
|
||||
70 | void QScatterSeriesPrivate::resize(const QSize &size) |
|
|||
71 | { |
|
|||
72 | resize(QRect(0, 0, size.width(), size.height())); |
|
|||
73 | } |
|
|||
74 |
|
||||
75 | void QScatterSeriesPrivate::setTheme(ChartTheme *theme) |
|
|||
76 | { |
|
|||
77 | m_theme = theme->themeForSeries(); |
|
|||
78 | } |
|
|||
79 |
|
||||
80 | void QScatterSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain) |
|
|||
81 | { |
|
|||
82 | // TODO |
|
|||
83 | } |
|
|||
84 |
|
||||
85 | QScatterSeries::QScatterSeries(QObject *parent) : |
|
79 | QScatterSeries::QScatterSeries(QObject *parent) : | |
86 | QChartSeries(parent), |
|
80 | QChartSeries(parent), | |
87 | d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent))) |
|
81 | d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent))) | |
88 | { |
|
82 | { | |
89 | } |
|
83 | } | |
90 |
|
84 | |||
91 | bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y) |
|
85 | bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y) | |
92 | { |
|
86 | { | |
93 | // TODO: validate data |
|
87 | // TODO: validate data | |
94 | d->m_x = x; |
|
88 | d->m_x = x; | |
95 | d->m_y = y; |
|
89 | d->m_y = y; | |
96 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); |
|
90 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); | |
97 | Q_ASSERT(parentItem); |
|
91 | Q_ASSERT(parentItem); | |
|
92 | // d->setPos(parentItem->pos()); | |||
|
93 | // d->setSize(parentItem->boundingRect().size().toSize()); | |||
98 | d->resize(parentItem->boundingRect()); |
|
94 | d->resize(parentItem->boundingRect()); | |
99 | return true; |
|
95 | return true; | |
100 | } |
|
96 | } | |
101 |
|
97 | |||
102 | void QScatterSeries::setMarkerColor(QColor color) |
|
98 | void QScatterSeries::setMarkerColor(QColor color) | |
103 | { |
|
99 | { | |
104 | d->m_markerColor = color; |
|
100 | d->m_markerColor = color; | |
105 | } |
|
101 | } | |
106 |
|
102 | |||
107 | QColor QScatterSeries::markerColor() |
|
103 | QColor QScatterSeries::markerColor() | |
108 | { |
|
104 | { | |
109 | return d->m_markerColor; |
|
105 | return d->m_markerColor; | |
110 | } |
|
106 | } | |
111 |
|
107 | |||
112 | // TODO: |
|
108 | // TODO: | |
113 | //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale) |
|
109 | //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale) | |
114 | //{ |
|
110 | //{ | |
115 | // d->rescale(xscale, yscale); |
|
111 | // d->rescale(xscale, yscale); | |
116 | //} |
|
112 | //} | |
117 |
|
113 | |||
118 | QScatterSeries::~QScatterSeries() |
|
114 | QScatterSeries::~QScatterSeries() | |
119 | { |
|
115 | { | |
120 | delete d; |
|
116 | delete d; | |
121 | } |
|
117 | } | |
122 |
|
118 | |||
123 | #include "moc_qscatterseries.cpp" |
|
119 | #include "moc_qscatterseries.cpp" | |
124 |
|
120 | |||
125 | QTCOMMERCIALCHART_END_NAMESPACE |
|
121 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,44 +1,44 | |||||
1 | #ifndef QSCATTERSERIESPRIVATE_H |
|
1 | #ifndef QSCATTERSERIESPRIVATE_H | |
2 | #define QSCATTERSERIESPRIVATE_H |
|
2 | #define QSCATTERSERIESPRIVATE_H | |
3 |
|
3 | |||
4 | #include "qchartseries.h" |
|
4 | #include "qchartseries.h" | |
5 | #include "charttheme_p.h" |
|
5 | #include "charttheme_p.h" | |
6 |
#include "chartitem |
|
6 | #include "chartitem_p.h" | |
7 | #include <QGraphicsItem> |
|
7 | #include <QGraphicsItem> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | /*! |
|
11 | /*! | |
12 | * The PIMPL class of QScatterSeries. |
|
12 | * The PIMPL class of QScatterSeries. | |
13 | */ |
|
13 | */ | |
14 |
class QScatterSeriesPrivate : public |
|
14 | class QScatterSeriesPrivate : public ChartItem | |
15 | { |
|
15 | { | |
16 | public: |
|
16 | public: | |
17 | QScatterSeriesPrivate(QGraphicsItem *parent); |
|
17 | QScatterSeriesPrivate(QGraphicsItem *parent); | |
18 |
|
18 | |||
|
19 | public: // from ChartObjectInterface | |||
|
20 | void setSize(const QSize &size); | |||
|
21 | void setTheme(ChartTheme *theme); | |||
|
22 | void setPlotDomain(const PlotDomain& data); | |||
|
23 | ||||
19 | public: // from QGraphicsItem |
|
24 | public: // from QGraphicsItem | |
20 | void setPos(const QPointF & pos); |
|
|||
21 | void resize(QRectF rect); |
|
|||
22 | QRectF boundingRect() const; |
|
25 | QRectF boundingRect() const; | |
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
26 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
24 |
|
27 | |||
25 | public: // from ChartItemControl |
|
|||
26 | void resize(const QSize &size); |
|
|||
27 | void setTheme(ChartTheme *theme); |
|
|||
28 | void setPlotDomain(const PlotDomain& data); |
|
|||
29 |
|
||||
30 | public: |
|
28 | public: | |
|
29 | void resize(QRectF rect); // TODO: replace with setSize | |||
|
30 | ||||
31 | // TODO: use the chart data class instead of list of x and y values? |
|
31 | // TODO: use the chart data class instead of list of x and y values? | |
32 | QList<qreal> m_x; |
|
32 | QList<qreal> m_x; | |
33 | QList<qreal> m_y; |
|
33 | QList<qreal> m_y; | |
34 | qreal m_scalex; |
|
34 | qreal m_scalex; | |
35 | qreal m_scaley; |
|
35 | qreal m_scaley; | |
36 | QList<qreal> m_scenex; |
|
36 | QList<qreal> m_scenex; | |
37 | QList<qreal> m_sceney; |
|
37 | QList<qreal> m_sceney; | |
38 | QColor m_markerColor; |
|
38 | QColor m_markerColor; | |
39 | SeriesTheme m_theme; |
|
39 | SeriesTheme m_theme; | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 | QTCOMMERCIALCHART_END_NAMESPACE |
|
42 | QTCOMMERCIALCHART_END_NAMESPACE | |
43 |
|
43 | |||
44 | #endif // QSCATTERSERIESPRIVATE_H |
|
44 | #endif // QSCATTERSERIESPRIVATE_H |
@@ -1,110 +1,113 | |||||
1 | !include( ../common.pri ) { |
|
1 | !include( ../common.pri ) { | |
2 | error( Couldn't find the common.pri file! ) |
|
2 | error( Couldn't find the common.pri file! ) | |
3 | } |
|
3 | } | |
4 |
|
4 | |||
5 | TARGET = QtCommercialChart |
|
5 | TARGET = QtCommercialChart | |
6 | DESTDIR = $$CHART_BUILD_LIB_DIR |
|
6 | DESTDIR = $$CHART_BUILD_LIB_DIR | |
7 | TEMPLATE = lib |
|
7 | TEMPLATE = lib | |
8 | QT += core \ |
|
8 | QT += core \ | |
9 | gui |
|
9 | gui | |
10 |
|
10 | |||
11 | CONFIG += debug_and_release |
|
11 | CONFIG += debug_and_release | |
12 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd |
|
12 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd | |
13 |
|
13 | |||
14 | SOURCES += \ |
|
14 | SOURCES += \ | |
15 | barchart/barchartseries.cpp \ |
|
15 | barchart/barchartseries.cpp \ | |
16 | barchart/bargroup.cpp \ |
|
16 | barchart/bargroup.cpp \ | |
17 | barchart/bar.cpp \ |
|
17 | barchart/bar.cpp \ | |
18 | barchart/stackedbarchartseries.cpp \ |
|
18 | barchart/stackedbarchartseries.cpp \ | |
19 | barchart/stackedbargroup.cpp \ |
|
19 | barchart/stackedbargroup.cpp \ | |
20 | barchart/percentbarchartseries.cpp \ |
|
20 | barchart/percentbarchartseries.cpp \ | |
21 | barchart/percentbargroup.cpp \ |
|
21 | barchart/percentbargroup.cpp \ | |
22 | xylinechart/qxychartseries.cpp \ |
|
22 | xylinechart/qxychartseries.cpp \ | |
23 | xylinechart/xylinechartitem.cpp \ |
|
23 | xylinechart/xylinechartitem.cpp \ | |
24 | plotdomain.cpp \ |
|
24 | plotdomain.cpp \ | |
25 | qscatterseries.cpp \ |
|
25 | qscatterseries.cpp \ | |
26 | qpieseries.cpp \ |
|
26 | qpieseries.cpp \ | |
27 | qchart.cpp \ |
|
27 | qchart.cpp \ | |
28 | axisitem.cpp \ |
|
28 | axisitem.cpp \ | |
29 | qchartwidget.cpp \ |
|
29 | qchartwidget.cpp \ | |
30 | pieslice.cpp \ |
|
30 | pieslice.cpp \ | |
31 | qchartview.cpp \ |
|
31 | qchartview.cpp \ | |
32 | qchartseries.cpp \ |
|
32 | qchartseries.cpp \ | |
33 | qchartaxis.cpp \ |
|
33 | qchartaxis.cpp \ | |
34 | charttheme.cpp |
|
34 | charttheme.cpp | |
35 |
|
35 | |||
36 | PRIVATE_HEADERS += \ |
|
36 | PRIVATE_HEADERS += \ | |
37 | xylinechart/xylinechartitem_p.h \ |
|
37 | xylinechart/xylinechartitem_p.h \ | |
38 | plotdomain_p.h \ |
|
38 | plotdomain_p.h \ | |
39 | qscatterseries_p.h \ |
|
39 | qscatterseries_p.h \ | |
40 | qpieseries_p.h \ |
|
40 | qpieseries_p.h \ | |
41 | pieslice.h \ |
|
41 | pieslice.h \ | |
42 | axisitem_p.h \ |
|
42 | axisitem_p.h \ | |
|
43 | chartitem_p.h \ | |||
43 | charttheme_p.h |
|
44 | charttheme_p.h | |
44 |
|
45 | |||
45 | PUBLIC_HEADERS += \ |
|
46 | PUBLIC_HEADERS += \ | |
46 | qchartseries.h \ |
|
47 | qchartseries.h \ | |
47 | qscatterseries.h \ |
|
48 | qscatterseries.h \ | |
48 | qpieseries.h \ |
|
49 | qpieseries.h \ | |
49 | qchart.h \ |
|
50 | qchart.h \ | |
50 | qchartwidget.h \ |
|
51 | qchartwidget.h \ | |
51 | qchartglobal.h \ |
|
52 | qchartglobal.h \ | |
52 | xylinechart/qxychartseries.h \ |
|
53 | xylinechart/qxychartseries.h \ | |
53 | barchart/barchartseries.h \ |
|
54 | barchart/barchartseries.h \ | |
54 | barchart/bargroup.h \ |
|
55 | barchart/bargroup.h \ | |
55 | barchart/stackedbarchartseries.h \ |
|
56 | barchart/stackedbarchartseries.h \ | |
56 | barchart/stackedbargroup.h \ |
|
57 | barchart/stackedbargroup.h \ | |
57 | barchart/percentbarchartseries.h \ |
|
58 | barchart/percentbarchartseries.h \ | |
58 | barchart/percentbargroup.h \ |
|
59 | barchart/percentbargroup.h \ | |
59 | qchartview.h \ |
|
60 | qchartview.h \ | |
60 | qchartaxis.h |
|
61 | qchartaxis.h | |
61 |
|
62 | |||
62 | HEADERS += $$PUBLIC_HEADERS \ |
|
63 | HEADERS += $$PUBLIC_HEADERS \ | |
63 |
chart |
|
64 | chartobjectinterface_p.h | |
64 | HEADERS += $$PRIVATE_HEADERS |
|
65 | HEADERS += $$PRIVATE_HEADERS | |
65 |
|
66 | |||
66 | INCLUDEPATH += xylinechart \ |
|
67 | INCLUDEPATH += xylinechart \ | |
67 | barchart \ |
|
68 | barchart \ | |
68 | . |
|
69 | . | |
69 |
|
70 | |||
70 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib |
|
71 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib | |
71 | MOC_DIR = $$CHART_BUILD_DIR/lib |
|
72 | MOC_DIR = $$CHART_BUILD_DIR/lib | |
72 | UI_DIR = $$CHART_BUILD_DIR/lib |
|
73 | UI_DIR = $$CHART_BUILD_DIR/lib | |
73 | RCC_DIR = $$CHART_BUILD_DIR/lib |
|
74 | RCC_DIR = $$CHART_BUILD_DIR/lib | |
74 |
|
75 | |||
75 |
|
76 | |||
76 | DEFINES += QTCOMMERCIALCHART_LIBRARY |
|
77 | DEFINES += QTCOMMERCIALCHART_LIBRARY | |
77 |
|
78 | |||
78 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart |
|
79 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart | |
79 | public_headers.files = $$PUBLIC_HEADERS |
|
80 | public_headers.files = $$PUBLIC_HEADERS | |
80 | target.path = $$[QT_INSTALL_LIBS] |
|
81 | target.path = $$[QT_INSTALL_LIBS] | |
81 | INSTALLS += target \ |
|
82 | INSTALLS += target \ | |
82 | public_headers |
|
83 | public_headers | |
83 |
|
84 | |||
84 |
|
85 | |||
85 | install_build_headers.name = bild_headers |
|
86 | install_build_headers.name = bild_headers | |
86 | install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
87 | install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h | |
87 | install_build_headers.input = PUBLIC_HEADERS |
|
88 | install_build_headers.input = PUBLIC_HEADERS | |
88 | install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR |
|
89 | install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR | |
89 | install_build_headers.CONFIG += target_predeps no_link |
|
90 | install_build_headers.CONFIG += target_predeps no_link | |
90 | QMAKE_EXTRA_COMPILERS += install_build_headers |
|
91 | QMAKE_EXTRA_COMPILERS += install_build_headers | |
91 |
|
92 | |||
92 | chartversion.target = qchartversion_p.h |
|
93 | chartversion.target = qchartversion_p.h | |
93 | chartversion.commands = @echo "build_time" > $$chartversion.target; |
|
94 | chartversion.commands = @echo "build_time" > $$chartversion.target; | |
94 | chartversion.depends = $$HEADERS $$SOURCES |
|
95 | chartversion.depends = $$HEADERS $$SOURCES | |
95 | PRE_TARGETDEPS += qchartversion_p.h |
|
96 | PRE_TARGETDEPS += qchartversion_p.h | |
96 | QMAKE_CLEAN+= qchartversion_p.h |
|
97 | QMAKE_CLEAN+= qchartversion_p.h | |
97 | QMAKE_EXTRA_TARGETS += chartversion |
|
98 | QMAKE_EXTRA_TARGETS += chartversion | |
98 |
|
99 | |||
99 | unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
100 | unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR | |
100 | win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR |
|
101 | win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR | |
101 |
|
102 | |||
102 |
|
103 | |||
103 |
|
104 | |||
104 |
|
105 | |||
105 |
|
106 | |||
106 |
|
107 | |||
107 |
|
108 | |||
108 |
|
109 | |||
109 |
|
110 | |||
110 |
|
111 | |||
|
112 | ||||
|
113 |
@@ -1,81 +1,76 | |||||
1 | #include "xylinechartitem_p.h" |
|
1 | #include "xylinechartitem_p.h" | |
2 | #include "axisitem_p.h" |
|
2 | #include "axisitem_p.h" | |
3 | #include "qxychartseries.h" |
|
3 | #include "qxychartseries.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 | #include <QStyleOptionGraphicsItem> |
|
5 | #include <QStyleOptionGraphicsItem> | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent) : |
|
10 | XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent) : | |
11 |
|
|
11 | ChartItem(parent), | |
12 | m_series(series), |
|
12 | m_series(series), | |
13 | m_pathItem(new QGraphicsPathItem(this)) |
|
13 | m_pathItem(new QGraphicsPathItem(this)) | |
14 | { |
|
14 | { | |
15 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
15 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 |
void XYLineChartItem::set |
|
18 | void XYLineChartItem::setSize(const QSize &size) | |
19 | { |
|
|||
20 | QGraphicsItem::setPos(pos); |
|
|||
21 | } |
|
|||
22 |
|
||||
23 | void XYLineChartItem::resize(const QSize &size) |
|
|||
24 | { |
|
19 | { | |
25 | m_rect = QRect(0, 0, size.width(), size.height()); |
|
20 | m_rect = QRect(0, 0, size.width(), size.height()); | |
26 | prepareGeometryChange(); |
|
21 | prepareGeometryChange(); | |
27 | updateGeometry(); |
|
22 | updateGeometry(); | |
28 | } |
|
23 | } | |
29 |
|
24 | |||
30 | void XYLineChartItem::setTheme(ChartTheme *theme) |
|
25 | void XYLineChartItem::setTheme(ChartTheme *theme) | |
31 | { |
|
26 | { | |
32 | if (theme) { |
|
27 | if (theme) { | |
33 | m_theme = theme->themeForSeries(); |
|
28 | m_theme = theme->themeForSeries(); | |
34 | prepareGeometryChange(); |
|
29 | prepareGeometryChange(); | |
35 | updateGeometry(); |
|
30 | updateGeometry(); | |
36 | } |
|
31 | } | |
37 | } |
|
32 | } | |
38 |
|
33 | |||
39 | void XYLineChartItem::setPlotDomain(const PlotDomain& data) |
|
34 | void XYLineChartItem::setPlotDomain(const PlotDomain& data) | |
40 | { |
|
35 | { | |
41 | m_plotDomain=data; |
|
36 | m_plotDomain=data; | |
42 | prepareGeometryChange(); |
|
37 | prepareGeometryChange(); | |
43 | updateGeometry(); |
|
38 | updateGeometry(); | |
44 |
|
39 | |||
45 | } |
|
40 | } | |
46 |
|
41 | |||
47 | QRectF XYLineChartItem::boundingRect() const |
|
42 | QRectF XYLineChartItem::boundingRect() const | |
48 | { |
|
43 | { | |
49 | return m_rect; |
|
44 | return m_rect; | |
50 | } |
|
45 | } | |
51 | /* |
|
46 | /* | |
52 | QPainterPath XYLineChartItem::shape() const |
|
47 | QPainterPath XYLineChartItem::shape() const | |
53 | { |
|
48 | { | |
54 | return m_pathItem->shape(); |
|
49 | return m_pathItem->shape(); | |
55 | } |
|
50 | } | |
56 | */ |
|
51 | */ | |
57 | void XYLineChartItem::updateGeometry() |
|
52 | void XYLineChartItem::updateGeometry() | |
58 | { |
|
53 | { | |
59 | if (!m_rect.isValid()) return; |
|
54 | if (!m_rect.isValid()) return; | |
60 |
|
55 | |||
61 | const qreal deltaX = m_rect.width()/m_plotDomain.spanX(); |
|
56 | const qreal deltaX = m_rect.width()/m_plotDomain.spanX(); | |
62 | const qreal deltaY = m_rect.height()/m_plotDomain.spanY(); |
|
57 | const qreal deltaY = m_rect.height()/m_plotDomain.spanY(); | |
63 |
|
58 | |||
64 | QPainterPath path; |
|
59 | QPainterPath path; | |
65 |
|
60 | |||
66 | for (int j = 0; j < m_series->count(); ++j) { |
|
61 | for (int j = 0; j < m_series->count(); ++j) { | |
67 | qreal dx = m_series->x(j) - m_plotDomain.m_minX; |
|
62 | qreal dx = m_series->x(j) - m_plotDomain.m_minX; | |
68 | qreal dy = m_series->y(j) - m_plotDomain.m_minY; |
|
63 | qreal dy = m_series->y(j) - m_plotDomain.m_minY; | |
69 | qreal x = (dx * deltaX) + m_rect.left(); |
|
64 | qreal x = (dx * deltaX) + m_rect.left(); | |
70 | qreal y = - (dy * deltaY) + m_rect.bottom(); |
|
65 | qreal y = - (dy * deltaY) + m_rect.bottom(); | |
71 | if(j==0) path.moveTo(x,y); |
|
66 | if(j==0) path.moveTo(x,y); | |
72 | else path.lineTo(x,y); |
|
67 | else path.lineTo(x,y); | |
73 | } |
|
68 | } | |
74 |
|
69 | |||
75 | m_pathItem->setPath(path); |
|
70 | m_pathItem->setPath(path); | |
76 | m_pathItem->setPen(m_theme.linePen); |
|
71 | m_pathItem->setPen(m_theme.linePen); | |
77 | m_pathItem->setBrush(Qt::NoBrush); |
|
72 | m_pathItem->setBrush(Qt::NoBrush); | |
78 | } |
|
73 | } | |
79 |
|
74 | |||
80 |
|
75 | |||
81 | QTCOMMERCIALCHART_END_NAMESPACE |
|
76 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,45 +1,44 | |||||
1 | #ifndef XYLINECHARTITEM_H |
|
1 | #ifndef XYLINECHARTITEM_H | |
2 | #define XYLINECHARTITEM_H |
|
2 | #define XYLINECHARTITEM_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 |
#include "chartitem |
|
5 | #include "chartitem_p.h" | |
6 | #include "charttheme_p.h" |
|
6 | #include "charttheme_p.h" | |
7 | #include <QGraphicsItem> |
|
7 | #include <QGraphicsItem> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class QXYChartSeries; |
|
11 | class QXYChartSeries; | |
12 |
|
12 | |||
13 |
class XYLineChartItem : public |
|
13 | class XYLineChartItem : public ChartItem | |
14 | { |
|
14 | { | |
15 |
|
15 | |||
16 | public: |
|
16 | public: | |
17 | XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0); |
|
17 | XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0); | |
18 | ~ XYLineChartItem(){}; |
|
18 | ~ XYLineChartItem(){}; | |
19 |
|
19 | |||
20 | //from QGraphicsItem |
|
20 | //from QGraphicsItem | |
21 | QRectF boundingRect() const; |
|
21 | QRectF boundingRect() const; | |
22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){}; |
|
22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){}; | |
23 | // virtual QPainterPath shape() const; |
|
23 | // virtual QPainterPath shape() const; | |
24 |
|
24 | |||
25 |
public: // from Chart |
|
25 | public: // from ChartObjectInterface | |
26 |
void set |
|
26 | void setSize(const QSize &size); | |
27 | void resize(const QSize &size); |
|
|||
28 | void setTheme(ChartTheme *theme); |
|
27 | void setTheme(ChartTheme *theme); | |
29 | void setPlotDomain(const PlotDomain& data); |
|
28 | void setPlotDomain(const PlotDomain& data); | |
30 |
|
29 | |||
31 | private: |
|
30 | private: | |
32 | void updateGeometry(); |
|
31 | void updateGeometry(); | |
33 |
|
32 | |||
34 | private: |
|
33 | private: | |
35 | QRect m_rect; |
|
34 | QRect m_rect; | |
36 | QPolygonF m_polyline; |
|
35 | QPolygonF m_polyline; | |
37 | QXYChartSeries* m_series; |
|
36 | QXYChartSeries* m_series; | |
38 | PlotDomain m_plotDomain; |
|
37 | PlotDomain m_plotDomain; | |
39 | QGraphicsPathItem *m_pathItem; |
|
38 | QGraphicsPathItem *m_pathItem; | |
40 | SeriesTheme m_theme; |
|
39 | SeriesTheme m_theme; | |
41 | }; |
|
40 | }; | |
42 |
|
41 | |||
43 | QTCOMMERCIALCHART_END_NAMESPACE |
|
42 | QTCOMMERCIALCHART_END_NAMESPACE | |
44 |
|
43 | |||
45 | #endif |
|
44 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now