##// END OF EJS Templates
Refactored themes; now enabled for line, scatter and pies...
Tero Ahola -
r103:399cbfcd557c
parent child
Show More
@@ -0,0 +1,24
1 #ifndef CHARTITEMCONTROL_H
2 #define CHARTITEMCONTROL_H
3
4 #include "plotdomain_p.h"
5 #include <qchartglobal.h>
6 #include <QSize>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class ChartTheme;
11 class PlotDomain;
12
13 class ChartItemControl
14 {
15 public:
16 virtual void setPos (const QPointF & pos) = 0;
17 virtual void resize(const QSize &size) = 0;
18 virtual void setTheme(ChartTheme *theme) = 0;
19 virtual void setPlotDomain(const PlotDomain& data) = 0;
20 };
21
22 QTCOMMERCIALCHART_END_NAMESPACE
23
24 #endif // CHARTITEMCONTROL_H
@@ -0,0 +1,102
1 #include "charttheme_p.h"
2 #include "qchart.h"
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6 void ChartThemeData::setTheme(int theme)
7 {
8 if (theme == m_currentTheme) return;
9
10 m_seriesThemes.clear();
11 m_seriesIndex = 0;
12 m_currentTheme = theme;
13
14 switch (theme) {
15 case QChart::ChartThemeDefault:
16 // line: solid, dashed, dotted
17 // line width: 1
18 // line color (and opacity)
19 // line shadow (on/off)
20 // marker shape: "x", "o", "."
21 // TODO: define the default theme based on the OS
22 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QRgb(0xff000000), 2));
23 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QRgb(0xff707070), 2));
24 m_gradientStartColor = QColor(QRgb(0xffffffff));
25 m_gradientEndColor = QColor(QRgb(0xffafafaf));
26 break;
27 case QChart::ChartThemeVanilla:
28 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(217, 197, 116), 10));
29 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(214, 168, 150), 10));
30 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(160, 160, 113), 10));
31 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(210, 210, 52), 10));
32 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(136, 114, 58), 10));
33
34 m_gradientStartColor = QColor(QRgb(0xff9d844d));
35 m_gradientEndColor = QColor(QRgb(0xffafafaf));
36 break;
37 case QChart::ChartThemeIcy:
38 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(0, 3, 165), 2));
39 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(49, 52, 123), 2));
40 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(71, 114, 187), 2));
41 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(48, 97, 87), 2));
42 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(19, 71, 90), 2));
43 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(110, 70, 228), 2));
44
45 m_gradientStartColor = QColor(QRgb(0xffe4ffff));
46 m_gradientEndColor = QColor(QRgb(0xffe4ffff));
47 break;
48 case QChart::ChartThemeGrayscale:
49 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(0, 0, 0), 2));
50 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(50, 50, 50), 2));
51 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(100, 100, 100), 2));
52 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(140, 140, 140), 2));
53 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(180, 180, 180), 2));
54
55 m_gradientStartColor = QColor(QRgb(0xffffffff));
56 m_gradientEndColor = QColor(QRgb(0xffafafaf));
57 break;
58 case QChart::ChartThemeUnnamed1:
59 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xff3fa9f5)), 2));
60 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xff7AC943)), 2));
61 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xffFF931E)), 2));
62 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xffFF1D25)), 2));
63 m_seriesThemes.append(SeriesTheme(m_seriesThemes.count(), QColor(QRgb(0xffFF7BAC)), 2));
64
65 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
66 m_gradientEndColor = QColor(QRgb(0xffafafaf));
67 break;
68 default:
69 Q_ASSERT(false);
70 break;
71 }
72
73 //themeChanged();
74 // TODO: update coloring of different elements to match the selected theme
75 }
76
77 ChartTheme::ChartTheme(QObject *parent) :
78 QObject(parent),
79 d(new ChartThemeData())
80 {
81 d->m_currentTheme = QChart::ChartThemeInvalid;
82 d->m_seriesIndex = 0;
83 }
84
85 SeriesTheme ChartTheme::themeForSeries()
86 {
87 if (d->m_seriesThemes.count() == 0) {
88 return SeriesTheme();
89 } else {
90 // Get the next available theme for the series; if no more themes available start over
91 // beginning from the first theme in the list
92 SeriesTheme nextTheme =
93 d->m_seriesThemes[d->m_seriesIndex % d->m_seriesThemes.count()];
94 d->m_seriesIndex++;
95 return nextTheme;
96 }
97 }
98
99
100 #include "moc_charttheme_p.cpp"
101
102 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,80
1 #ifndef CHARTTHEME_H
2 #define CHARTTHEME_H
3
4 #include "qchartglobal.h"
5 #include <QObject>
6 #include <QSharedData>
7 #include <QColor>
8 #include <QLinearGradient>
9 #include <QPen>
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13 /*!
14 * The theme specific settings for the appearance of a series. TODO: These can be overridden by setting
15 * custom settings to a QChartSeries object.
16 */
17 struct SeriesTheme {
18 public:
19 // SeriesTheme() :
20 // themeIndex(-1), lineColor(QColor()) {}
21 // SeriesTheme(int index, QColor line) :
22 // themeIndex(index), lineColor(line) {}
23 SeriesTheme() :
24 themeIndex(-1), linePen(QPen()), markerPen(QPen()) {}
25 SeriesTheme(int index, QColor lineColor, qreal lineWidth/*, QPen marker*/) :
26 themeIndex(index),
27 linePen(QPen(QBrush(lineColor), lineWidth)),
28 markerPen(linePen) {}
29
30 //const QBrush & brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJo
31 int themeIndex;
32 // TODO:
33 //QColor lineColor;
34 QPen linePen;
35 //QBrush lineBrush;
36 QPen markerPen;
37 //QBrush markerBrush;
38 };
39
40 /*!
41 * Explicitly shared data class for the themes.
42 */
43 class ChartThemeData : public QSharedData
44 {
45 public:
46 ChartThemeData() : m_currentTheme(0) {}
47 ~ChartThemeData() {}
48
49 public:
50 void setTheme(int theme);
51
52 public:
53 int m_currentTheme;
54 QColor m_gradientStartColor;
55 QColor m_gradientEndColor;
56 QList<SeriesTheme> m_seriesThemes;
57 int m_seriesIndex;
58 };
59
60 class ChartTheme : public QObject
61 {
62 Q_OBJECT
63 public:
64 explicit ChartTheme(QObject *parent = 0);
65 explicit ChartTheme(const ChartTheme &other) : d(other.d) {}
66 void operator =(const ChartTheme &other) { d = other.d; }
67
68 //signals:
69 // void themeChanged(ChartTheme theme);
70 SeriesTheme themeForSeries();
71
72 public:
73 // All the graphical elements of a QChart share the same theme settings
74 // so let's use explicitly shared data
75 QExplicitlySharedDataPointer<ChartThemeData> d;
76 };
77
78 QTCOMMERCIALCHART_END_NAMESPACE
79
80 #endif // CHARTTHEME_H
@@ -0,0 +1,40
1 #ifndef PIESERIESPRIVATE_H
2 #define PIESERIESPRIVATE_H
3
4 #include "chartitemcontrol.h"
5 #include "qpieseries.h"
6 #include <QRectF>
7 #include <QColor>
8
9 class QGraphicsItem;
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class PieSlice;
12
13 class QPieSeriesPrivate : public ChartItemControl
14 {
15 public:
16 // TODO: use a generic data class instead of x and y
17 QPieSeriesPrivate();
18 ~QPieSeriesPrivate();
19
20 public: // from ChartItemControl
21 void setPos(const QPointF & pos);
22 void resize(const QSize &size);
23 void setTheme(ChartTheme *theme);
24 void setPlotDomain(const PlotDomain& data);
25
26 public:
27 bool setData(QList<qreal> data);
28 void resizeSlices(QRectF rect);
29 QGraphicsItem *m_parentItem;
30 QList<qreal> m_data;
31 QList<PieSlice*> m_slices;
32 QRectF m_chartSize;
33 qreal m_sizeFactor;
34 QPieSeries::PiePosition m_position;
35 ChartTheme *m_chartTheme;
36 };
37
38 QTCOMMERCIALCHART_END_NAMESPACE
39
40 #endif // PIESERIESPRIVATE_H
@@ -1,162 +1,176
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): ChartItem(parent),
9 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
10 m_ticks(4),
10 QGraphicsItem(parent),
11 m_type(type)
11 m_ticks(4),
12 m_type(type)
12 {
13 {
13 }
14 }
14
15
15 AxisItem::~AxisItem()
16 AxisItem::~AxisItem()
16 {
17 {
17 }
18 }
18
19
19 void AxisItem::setSize(const QSize& size)
20 {
21 m_rect = QRectF(QPoint(0,0),size);
22 createItems();
23 }
24
25 void AxisItem::setLength(int length)
20 void AxisItem::setLength(int length)
26 {
21 {
27 QPainterPath path;
22 QPainterPath path;
28 path.moveTo(QPointF(0,0));
23 path.moveTo(QPointF(0,0));
29 path.lineTo(length,0);
24 path.lineTo(length,0);
30 // path.lineTo(length-4,0);
25 // path.lineTo(length-4,0);
31 // path.lineTo(length,3);
26 // path.lineTo(length,3);
32 // path.lineTo(length-4,6);
27 // path.lineTo(length-4,6);
33 // path.lineTo(length-4,4);
28 // path.lineTo(length-4,4);
34 // path.lineTo(0,4);
29 // path.lineTo(0,4);
35 // path.lineTo(0,2);
30 // path.lineTo(0,2);
36 m_path=path;
31 m_path=path;
37 update();
32 update();
38 }
33 }
39
34
40 QRectF AxisItem::boundingRect() const
35 QRectF AxisItem::boundingRect() const
41 {
36 {
42 return m_rect;
37 return m_rect;
43 }
38 }
44
39
45 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
40 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
46 {
41 {
47 m_plotDomain = plotDomain;
42 m_plotDomain = plotDomain;
48 createItems();
43 createItems();
49 }
44 }
45
46 void AxisItem::setPos(const QPointF & pos)
47 {
48 QGraphicsItem::setPos(pos);
49 }
50
51 void AxisItem::resize(const QSize &size)
52 {
53 m_rect = QRectF(QPoint(0,0),size);
54 createItems();
55 }
56
57 void AxisItem::setTheme(ChartTheme *theme)
58 {
59 if (theme) {
60 // TODO: add axis related properties to the theme class and use them here
61 }
62 }
63
50 /*
64 /*
51 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
65 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
52 {
66 {
53 if (!m_rect.isValid())
67 if (!m_rect.isValid())
54 return;
68 return;
55
69
56 if(m_type==X_AXIS) {
70 if(m_type==X_AXIS) {
57
71
58 const qreal deltaX = m_rect.width() / m_ticks;
72 const qreal deltaX = m_rect.width() / m_ticks;
59
73
60 for (int i = 0; i <= m_ticks; ++i) {
74 for (int i = 0; i <= m_ticks; ++i) {
61
75
62 int x = i * deltaX + m_rect.left();
76 int x = i * deltaX + m_rect.left();
63
77
64 if(i==0) x--;
78 if(i==0) x--;
65 if(i==m_ticks) x++;
79 if(i==m_ticks) x++;
66
80
67 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
81 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
68 / m_ticks);
82 / m_ticks);
69 painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1);
83 painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1);
70 // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5);
84 // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5);
71
85
72 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label));
86 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label));
73 }
87 }
74 }
88 }
75
89
76 if(m_type==Y_AXIS) {
90 if(m_type==Y_AXIS) {
77
91
78 const qreal deltaY = (m_rect.height()) / m_ticks;
92 const qreal deltaY = (m_rect.height()) / m_ticks;
79
93
80 for (int j = 0; j <= m_ticks; ++j) {
94 for (int j = 0; j <= m_ticks; ++j) {
81
95
82 int y = j * -deltaY + m_rect.bottom();
96 int y = j * -deltaY + m_rect.bottom();
83
97
84 if(j==0) y++;
98 if(j==0) y++;
85 if(j==m_ticks) y--;
99 if(j==m_ticks) y--;
86
100
87 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
101 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
88 / m_ticks);
102 / m_ticks);
89
103
90 painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y);
104 painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y);
91 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
105 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
92 //TODO : margin = 50 ;
106 //TODO : margin = 50 ;
93 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
107 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
94 Qt::AlignRight | Qt::AlignVCenter,
108 Qt::AlignRight | Qt::AlignVCenter,
95 QString::number(label));
109 QString::number(label));
96 }
110 }
97 }
111 }
98
112
99 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
113 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
100 }
114 }
101 */
115 */
102 void AxisItem::createItems()
116 void AxisItem::createItems()
103 {
117 {
104
118
105 //TODO: this is very inefficient handling
119 //TODO: this is very inefficient handling
106
120
107 qDeleteAll(m_shades);
121 qDeleteAll(m_shades);
108 m_shades.clear();
122 m_shades.clear();
109 qDeleteAll(m_grid);
123 qDeleteAll(m_grid);
110 m_grid.clear();
124 m_grid.clear();
111 qDeleteAll(m_labels);
125 qDeleteAll(m_labels);
112 m_labels.clear();
126 m_labels.clear();
113
127
114
128
115 if(m_type==X_AXIS) {
129 if(m_type==X_AXIS) {
116
130
117 const qreal deltaX = m_rect.width() / m_ticks;
131 const qreal deltaX = m_rect.width() / m_ticks;
118
132
119 for (int i = 0; i <= m_ticks; ++i) {
133 for (int i = 0; i <= m_ticks; ++i) {
120
134
121 int x = i * deltaX + m_rect.left();
135 int x = i * deltaX + m_rect.left();
122
136
123 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
137 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
124
138
125 m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this);
139 m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this);
126
140
127 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
141 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
128 QPointF center = text->boundingRect().center();
142 QPointF center = text->boundingRect().center();
129 text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING);
143 text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING);
130 //text->rotate(-45);
144 //text->rotate(-45);
131 m_labels<<text;
145 m_labels<<text;
132 }
146 }
133 }
147 }
134
148
135 if(m_type==Y_AXIS) {
149 if(m_type==Y_AXIS) {
136
150
137 const qreal deltaY = m_rect.height()/ m_ticks;
151 const qreal deltaY = m_rect.height()/ m_ticks;
138
152
139 for (int j = 0; j <= m_ticks; ++j) {
153 for (int j = 0; j <= m_ticks; ++j) {
140
154
141 int y = j * -deltaY + m_rect.bottom();
155 int y = j * -deltaY + m_rect.bottom();
142
156
143 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
157 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
144 / m_ticks);
158 / m_ticks);
145
159
146 m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this);
160 m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this);
147 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
161 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
148 QPointF center = text->boundingRect().center();
162 QPointF center = text->boundingRect().center();
149
163
150 text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y());
164 text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y());
151 //text->rotate(-45);
165 //text->rotate(-45);
152 m_labels<<text;
166 m_labels<<text;
153
167
154 }
168 }
155 }
169 }
156
170
157 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
171 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
158 }
172 }
159
173
160 //TODO "nice numbers algorithm"
174 //TODO "nice numbers algorithm"
161
175
162 QTCOMMERCIALCHART_END_NAMESPACE
176 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,49
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "chartitem_p.h"
5 #include "plotdomain_p.h"
4 #include "plotdomain_p.h"
5 #include "chartitemcontrol.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class AxisItem: public ChartItem
10 class AxisItem: public QGraphicsItem, public ChartItemControl
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 //from ChartItem
22 public: // from ChartItemControl
23 void setSize(const QSize& size);
23 void setPos (const QPointF & pos);
24 void resize(const QSize &size);
25 void setTheme(ChartTheme *theme);
24 void setPlotDomain(const PlotDomain& data);
26 void setPlotDomain(const PlotDomain& data);
25
27
28 public:
26 void setLength(int length);
29 void setLength(int length);
27 void setWidth(int width);
30 void setWidth(int width);
28 AxisType axisType() const {return m_type;};
31 AxisType axisType() const {return m_type;};
29
32
30 private:
33 private:
31 void createItems();
34 void createItems();
32 private:
35 private:
33 QRectF m_rect;
36 QRectF m_rect;
34 int m_ticks;
37 int m_ticks;
35 PlotDomain m_plotDomain;
38 PlotDomain m_plotDomain;
36 QPainterPath m_path;
39 QPainterPath m_path;
37
40
38 QList<QGraphicsLineItem*> m_grid;
41 QList<QGraphicsLineItem*> m_grid;
39 QList<QGraphicsRectItem*> m_shades;
42 QList<QGraphicsRectItem*> m_shades;
40 QList<QGraphicsSimpleTextItem*> m_labels;
43 QList<QGraphicsSimpleTextItem*> m_labels;
41 AxisType m_type;
44 AxisType m_type;
42 };
45 };
43
46
44 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
45
48
46 #endif /* AXISITEM_H_ */
49 #endif /* AXISITEM_H_ */
@@ -1,58 +1,68
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(ChartItem *parent)
7 Bar::Bar(QGraphicsItem *parent)
8 : ChartItem(parent)
8 : QGraphicsItem(parent)
9 {
9 {
10 }
10 }
11
11
12 void Bar::setSize(const QSize& size)
12 void Bar::setPos(const QPointF & pos)
13 {
14 QGraphicsItem::setPos(pos);
15 }
16
17 void Bar::resize(const QSize& size)
13 {
18 {
14 mWidth = size.width();
19 mWidth = size.width();
15 mHeight = size.height();
20 mHeight = size.height();
16 }
21 }
17
22
18 void Bar::setPlotDomain(const PlotDomain& data)
23 void Bar::setPlotDomain(const PlotDomain& data)
19 {
24 {
20 mPlotDomain = data;
25 mPlotDomain = data;
21 }
26 }
22
27
28 void Bar::setTheme(ChartTheme *theme)
29 {
30 // TODO
31 }
32
23 void Bar::resize( int w, int h )
33 void Bar::resize( int w, int h )
24 {
34 {
25 // qDebug() << "bar::resize" << w << h;
35 // qDebug() << "bar::resize" << w << h;
26 mWidth = w;
36 mWidth = w;
27 mHeight = h;
37 mHeight = h;
28 }
38 }
29
39
30 void Bar::setColor( QColor col )
40 void Bar::setColor( QColor col )
31 {
41 {
32 mColor = col;
42 mColor = col;
33 }
43 }
34 void Bar::setPos(qreal x, qreal y)
44 void Bar::setPos(qreal x, qreal y)
35 {
45 {
36 // qDebug() << "Bar::setpos" << x << y;
46 // qDebug() << "Bar::setpos" << x << y;
37 mXpos = x;
47 mXpos = x;
38 mYpos = y;
48 mYpos = y;
39 }
49 }
40
50
41 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42 {
52 {
43 if (0 == mHeight) {
53 if (0 == mHeight) {
44 return;
54 return;
45 }
55 }
46 // TODO: accept brush instead of color
56 // TODO: accept brush instead of color
47 QBrush brush(mColor);
57 QBrush brush(mColor);
48 painter->setBrush(brush);
58 painter->setBrush(brush);
49 painter->drawRect(mXpos-mWidth, mYpos-mHeight ,mWidth ,mHeight); // Evil inverse rect, because we want bars to grow from bottom to top :)
59 painter->drawRect(mXpos-mWidth, mYpos-mHeight ,mWidth ,mHeight); // Evil inverse rect, because we want bars to grow from bottom to top :)
50 }
60 }
51
61
52 QRectF Bar::boundingRect() const
62 QRectF Bar::boundingRect() const
53 {
63 {
54 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
64 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
55 return r;
65 return r;
56 }
66 }
57
67
58 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,46
1 #ifndef BAR_H
1 #ifndef BAR_H
2 #define BAR_H
2 #define BAR_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "qchartglobal.h"
5 #include "qchartglobal.h"
6 #include <QGraphicsItem>
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9
9 // Single bar item of chart
10 // Single bar item of chart
10 class Bar : public ChartItem
11 class Bar : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 Bar(ChartItem *parent=0);
14 Bar(QGraphicsItem *parent=0);
14
15
15 // From ChartItem
16 public: // from ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos (const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
19 // Layout Stuff
22 // Layout Stuff
20 void resize( int w, int h ); // Size of bar. in screen coordinates.
23 void resize( int w, int h ); // Size of bar. in screen coordinates.
21 void setColor( QColor col ); // Color of bar
24 void setColor( QColor col ); // Color of bar
22 void setPos(qreal x, qreal y);
25 void setPos(qreal x, qreal y);
23
26
24 public:
27 public:
25 // From QGraphicsItem
28 // From QGraphicsItem
26
29
27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
28 QRectF boundingRect() const;
31 QRectF boundingRect() const;
29
32
30 private:
33 private:
31
34
32 int mHeight;
35 int mHeight;
33 int mWidth;
36 int mWidth;
34 qreal mXpos;
37 qreal mXpos;
35 qreal mYpos;
38 qreal mYpos;
36 QColor mColor;
39 QColor mColor;
37
40
38 PlotDomain mPlotDomain;
41 PlotDomain mPlotDomain;
39 };
42 };
40
43
41 QTCOMMERCIALCHART_END_NAMESPACE
44 QTCOMMERCIALCHART_END_NAMESPACE
42
45
43 #endif // BAR_H
46 #endif // BAR_H
@@ -1,139 +1,148
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 ChartItem(parent)
8 QGraphicsItem(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::setPos(const QPointF & pos)
18 {
19 QGraphicsItem::setPos(pos);
20 }
17
21
18 void BarGroup::setSize(const QSize& size)
22 void BarGroup::resize(const QSize& size)
19 {
23 {
20 qDebug() << "BarGroup::setSize";
24 qDebug() << "BarGroup::setSize";
21 mWidth = size.width();
25 mWidth = size.width();
22 mHeight = size.height();
26 mHeight = size.height();
23 layoutChanged();
27 layoutChanged();
24 mLayoutSet = true;
28 mLayoutSet = true;
25 }
29 }
26
30
27 void BarGroup::setPlotDomain(const PlotDomain& data)
31 void BarGroup::setPlotDomain(const PlotDomain& data)
28 {
32 {
29 qDebug() << "BarGroup::setPlotDomain";
33 qDebug() << "BarGroup::setPlotDomain";
30 // TODO:
34 // TODO:
31 mPlotDomain = data;
35 mPlotDomain = data;
32 }
36 }
33
37
38 void BarGroup::setTheme(ChartTheme *theme)
39 {
40 // TODO
41 }
42
34 void BarGroup::setBarWidth( int w )
43 void BarGroup::setBarWidth( int w )
35 {
44 {
36 mBarDefaultWidth = w;
45 mBarDefaultWidth = w;
37 }
46 }
38
47
39 int BarGroup::addColor( QColor color )
48 int BarGroup::addColor( QColor color )
40 {
49 {
41 int colorIndex = mColors.count();
50 int colorIndex = mColors.count();
42 mColors.append(color);
51 mColors.append(color);
43 return colorIndex;
52 return colorIndex;
44 }
53 }
45
54
46 void BarGroup::resetColors()
55 void BarGroup::resetColors()
47 {
56 {
48 mColors.clear();
57 mColors.clear();
49 }
58 }
50
59
51 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
60 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
61 {
53 if (!mLayoutSet) {
62 if (!mLayoutSet) {
54 qDebug() << "QBarChart::paint called without layout set. Aborting.";
63 qDebug() << "QBarChart::paint called without layout set. Aborting.";
55 return;
64 return;
56 }
65 }
57 if (mLayoutDirty) {
66 if (mLayoutDirty) {
58 // Layout or data has changed. Need to redraw.
67 // Layout or data has changed. Need to redraw.
59 foreach(QGraphicsItem* i, childItems()) {
68 foreach(QGraphicsItem* i, childItems()) {
60 i->paint(painter,option,widget);
69 i->paint(painter,option,widget);
61 }
70 }
62 }
71 }
63 }
72 }
64
73
65 QRectF BarGroup::boundingRect() const
74 QRectF BarGroup::boundingRect() const
66 {
75 {
67 return QRectF(0,0,mWidth,mHeight);
76 return QRectF(0,0,mWidth,mHeight);
68 }
77 }
69
78
70
79
71 void BarGroup::dataChanged()
80 void BarGroup::dataChanged()
72 {
81 {
73 qDebug() << "QBarChart::dataChanged mSeries";
82 qDebug() << "QBarChart::dataChanged mSeries";
74
83
75 // Find out maximum and minimum of all series
84 // Find out maximum and minimum of all series
76 mMax = mSeries.max();
85 mMax = mSeries.max();
77 mMin = mSeries.min();
86 mMin = mSeries.min();
78
87
79 // Delete old bars
88 // Delete old bars
80 // Is this correct way to delete childItems?
89 // Is this correct way to delete childItems?
81 foreach (QGraphicsItem* item, childItems()) {
90 foreach (QGraphicsItem* item, childItems()) {
82 delete item;
91 delete item;
83 }
92 }
84
93
85 // Create new graphic items for bars
94 // Create new graphic items for bars
86 int totalItems = mSeries.countTotalItems();
95 int totalItems = mSeries.countTotalItems();
87 for (int i=0; i<totalItems; i++) {
96 for (int i=0; i<totalItems; i++) {
88 Bar *bar = new Bar(this);
97 Bar *bar = new Bar(this);
89 childItems().append(bar);
98 childItems().append(bar);
90 }
99 }
91
100
92 mLayoutDirty = true;
101 mLayoutDirty = true;
93 }
102 }
94
103
95 void BarGroup::layoutChanged()
104 void BarGroup::layoutChanged()
96 {
105 {
97 // Scale bars to new layout
106 // Scale bars to new layout
98 // Layout for bars:
107 // Layout for bars:
99 if (mSeries.countRows() <= 0) {
108 if (mSeries.countRows() <= 0) {
100 // Nothing to do.
109 // Nothing to do.
101 return;
110 return;
102 }
111 }
103
112
104 // TODO: better way to auto-layout?
113 // TODO: better way to auto-layout?
105 // Use reals for accurancy (we might get some compiler warnings... :)
114 // Use reals for accurancy (we might get some compiler warnings... :)
106 int columnCount = mSeries.countColumns();
115 int columnCount = mSeries.countColumns();
107 int rowCount = mSeries.countRows();
116 int rowCount = mSeries.countRows();
108
117
109 qreal tW = mWidth;
118 qreal tW = mWidth;
110 qreal tH = mHeight;
119 qreal tH = mHeight;
111 qreal tM = mMax;
120 qreal tM = mMax;
112 qreal scale = (tH/tM);
121 qreal scale = (tH/tM);
113
122
114 qreal tC = columnCount+1;
123 qreal tC = columnCount+1;
115 qreal xStepPerSeries = (tW/tC);
124 qreal xStepPerSeries = (tW/tC);
116
125
117 qDebug() << "XSTEP:" << xStepPerSeries;
126 qDebug() << "XSTEP:" << xStepPerSeries;
118
127
119 // Scaling.
128 // Scaling.
120 int itemIndex(0);
129 int itemIndex(0);
121 for (int column=0; column < columnCount; column++) {
130 for (int column=0; column < columnCount; column++) {
122 qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
131 qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
123 for (int row = 0; row < rowCount; row++) {
132 for (int row = 0; row < rowCount; row++) {
124 qreal barHeight = mSeries.valueAt(row, column) * scale;
133 qreal barHeight = mSeries.valueAt(row, column) * scale;
125 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
134 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
126
135
127 // TODO: width settable per bar?
136 // TODO: width settable per bar?
128 bar->resize(mBarDefaultWidth, barHeight);
137 bar->resize(mBarDefaultWidth, barHeight);
129 bar->setColor(mColors.at(row));
138 bar->setColor(mColors.at(row));
130 bar->setPos(xPos, mHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
139 bar->setPos(xPos, mHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
131 itemIndex++;
140 itemIndex++;
132 xPos += mBarDefaultWidth;
141 xPos += mBarDefaultWidth;
133 }
142 }
134 }
143 }
135
144
136 mLayoutDirty = true;
145 mLayoutDirty = true;
137 }
146 }
138
147
139 QTCOMMERCIALCHART_END_NAMESPACE
148 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +1,59
1 #ifndef QBARGROUP_H
1 #ifndef QBARGROUP_H
2 #define QBARGROUP_H
2 #define QBARGROUP_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "bar.h"
5 #include "bar.h"
6 #include "barchartseries.h"
6 #include "barchartseries.h"
7 #include <QGraphicsItem>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class BarGroup : public ChartItem
11 class BarGroup : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
14 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
14
15
15 // From ChartItem
16 public: // from ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos (const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
19 // Layout "api"
22 // Layout "api"
20 void setPos(qreal x, qreal y);
23 void setPos(qreal x, qreal y);
21 void setBarWidth( int w ); // Default width for each bar
24 void setBarWidth( int w ); // Default width for each bar
22
25
23 int addColor( QColor color );
26 int addColor( QColor color );
24 void resetColors();
27 void resetColors();
25
28
26 // From QGraphicsItem
29 // From QGraphicsItem
27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 QRectF boundingRect() const;
31 QRectF boundingRect() const;
29
32
30 private:
33 private:
31
34
32 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
33 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
36 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
34
37
35 private:
38 private:
36
39
37 // Data
40 // Data
38 BarChartSeries& mSeries;
41 BarChartSeries& mSeries;
39 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)
40 int mMax;
43 int mMax;
41
44
42 int mHeight; // Layout spesific
45 int mHeight; // Layout spesific
43 int mWidth;
46 int mWidth;
44 int mBarDefaultWidth;
47 int mBarDefaultWidth;
45
48
46 bool mLayoutSet; // True, if component has been laid out.
49 bool mLayoutSet; // True, if component has been laid out.
47 bool mLayoutDirty;
50 bool mLayoutDirty;
48
51
49 QList<QColor> mColors; // List of colors for series for now
52 QList<QColor> mColors; // List of colors for series for now
50
53
51 PlotDomain mPlotDomain;
54 PlotDomain mPlotDomain;
52 };
55 };
53
56
54 QTCOMMERCIALCHART_END_NAMESPACE
57 QTCOMMERCIALCHART_END_NAMESPACE
55
58
56 #endif // QBARGROUP_H
59 #endif // QBARGROUP_H
@@ -1,136 +1,148
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 ChartItem(parent)
10 QGraphicsItem(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
20 void StackedBarGroup::setSize(const QSize& size)
20 void StackedBarGroup::setPos(const QPointF & pos)
21 {
21 {
22 qDebug() << "StackedBarGroup::setSize";
22 qDebug() << "StackedBarGroup::setPos";
23 // TODO:
24 }
25
26 void StackedBarGroup::resize(const QSize& size)
27 {
28 qDebug() << "StackedBarGroup::resize";
23 mWidth = size.width();
29 mWidth = size.width();
24 mHeight = size.height();
30 mHeight = size.height();
25 layoutChanged();
31 layoutChanged();
26 mLayoutSet = true;
32 mLayoutSet = true;
27 }
33 }
28
34
29 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
35 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
30 {
36 {
31 qDebug() << "StackedBarGroup::setPlotDomain";
37 qDebug() << "StackedBarGroup::setPlotDomain";
32 // TODO:
38 // TODO:
33 }
39 }
34
40
41 void StackedBarGroup::setTheme(ChartTheme *theme)
42 {
43 qDebug() << "StackedBarGroup::setTheme";
44 // TODO:
45 }
46
35 void StackedBarGroup::setBarWidth( int w )
47 void StackedBarGroup::setBarWidth( int w )
36 {
48 {
37 mBarDefaultWidth = w;
49 mBarDefaultWidth = w;
38 }
50 }
39
51
40 int StackedBarGroup::addColor( QColor color )
52 int StackedBarGroup::addColor( QColor color )
41 {
53 {
42 int colorIndex = mColors.count();
54 int colorIndex = mColors.count();
43 mColors.append(color);
55 mColors.append(color);
44 return colorIndex;
56 return colorIndex;
45 }
57 }
46
58
47 void StackedBarGroup::resetColors()
59 void StackedBarGroup::resetColors()
48 {
60 {
49 mColors.clear();
61 mColors.clear();
50 }
62 }
51
63
52 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
64 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
53 {
65 {
54 if (!mLayoutSet) {
66 if (!mLayoutSet) {
55 qDebug() << "QBarChart::paint called without layout set. Aborting.";
67 qDebug() << "QBarChart::paint called without layout set. Aborting.";
56 return;
68 return;
57 }
69 }
58 if (mLayoutDirty) {
70 if (mLayoutDirty) {
59 // Layout or data has changed. Need to redraw.
71 // Layout or data has changed. Need to redraw.
60 foreach(QGraphicsItem* i, childItems()) {
72 foreach(QGraphicsItem* i, childItems()) {
61 i->paint(painter,option,widget);
73 i->paint(painter,option,widget);
62 }
74 }
63 }
75 }
64 }
76 }
65
77
66 QRectF StackedBarGroup::boundingRect() const
78 QRectF StackedBarGroup::boundingRect() const
67 {
79 {
68 return QRectF(0,0,mWidth,mHeight);
80 return QRectF(0,0,mWidth,mHeight);
69 }
81 }
70
82
71
83
72 void StackedBarGroup::dataChanged()
84 void StackedBarGroup::dataChanged()
73 {
85 {
74 qDebug() << "QBarChart::dataChanged mSeries";
86 qDebug() << "QBarChart::dataChanged mSeries";
75
87
76 // Find out maximum and minimum of all series
88 // Find out maximum and minimum of all series
77 mMax = mSeries.max();
89 mMax = mSeries.max();
78 mMin = mSeries.min();
90 mMin = mSeries.min();
79
91
80 // Delete old bars
92 // Delete old bars
81 // Is this correct way to delete childItems?
93 // Is this correct way to delete childItems?
82 foreach (QGraphicsItem* item, childItems()) {
94 foreach (QGraphicsItem* item, childItems()) {
83 delete item;
95 delete item;
84 }
96 }
85
97
86 // Create new graphic items for bars
98 // Create new graphic items for bars
87 int totalItems = mSeries.countTotalItems();
99 int totalItems = mSeries.countTotalItems();
88 for (int i=0; i<totalItems; i++) {
100 for (int i=0; i<totalItems; i++) {
89 Bar *bar = new Bar(this);
101 Bar *bar = new Bar(this);
90 childItems().append(bar);
102 childItems().append(bar);
91 }
103 }
92
104
93 mLayoutDirty = true;
105 mLayoutDirty = true;
94 }
106 }
95
107
96 void StackedBarGroup::layoutChanged()
108 void StackedBarGroup::layoutChanged()
97 {
109 {
98 // Scale bars to new layout
110 // Scale bars to new layout
99 // Layout for bars:
111 // Layout for bars:
100 if (mSeries.countRows() <= 0) {
112 if (mSeries.countRows() <= 0) {
101 // Nothing to do.
113 // Nothing to do.
102 return;
114 return;
103 }
115 }
104
116
105 // TODO: better way to auto-layout
117 // TODO: better way to auto-layout
106 // Use reals for accurancy (we might get some compiler warnings... :)
118 // Use reals for accurancy (we might get some compiler warnings... :)
107 qreal maxSum = mSeries.maxColumnSum();
119 qreal maxSum = mSeries.maxColumnSum();
108 qreal h = mHeight;
120 qreal h = mHeight;
109 qreal scale = (h / maxSum);
121 qreal scale = (h / maxSum);
110
122
111 int count = mSeries.countColumns();
123 int count = mSeries.countColumns();
112 int itemIndex(0);
124 int itemIndex(0);
113 qreal tW = mWidth;
125 qreal tW = mWidth;
114 qreal tC = count+1;
126 qreal tC = count+1;
115 qreal xStep = (tW/tC);
127 qreal xStep = (tW/tC);
116 qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
128 qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
117
129
118 for (int column = 0; column < mSeries.countColumns(); column++) {
130 for (int column = 0; column < mSeries.countColumns(); column++) {
119 qreal yPos = h;
131 qreal yPos = h;
120 for (int row=0; row < mSeries.countRows(); row++) {
132 for (int row=0; row < mSeries.countRows(); row++) {
121 qreal barHeight = mSeries.valueAt(row, column) * scale;
133 qreal barHeight = mSeries.valueAt(row, column) * scale;
122 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
134 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
123
135
124 // TODO: width settable per bar?
136 // TODO: width settable per bar?
125 bar->resize(mBarDefaultWidth, barHeight);
137 bar->resize(mBarDefaultWidth, barHeight);
126 bar->setColor(mColors.at(row));
138 bar->setColor(mColors.at(row));
127 bar->setPos(xPos, yPos);
139 bar->setPos(xPos, yPos);
128 itemIndex++;
140 itemIndex++;
129 yPos -= barHeight;
141 yPos -= barHeight;
130 }
142 }
131 xPos += xStep;
143 xPos += xStep;
132 }
144 }
133 mLayoutDirty = true;
145 mLayoutDirty = true;
134 }
146 }
135
147
136 QTCOMMERCIALCHART_END_NAMESPACE
148 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,59
1 #ifndef PERCENTBARGROUP_H
1 #ifndef PERCENTBARGROUP_H
2 #define PERCENTBARGROUP_H
2 #define PERCENTBARGROUP_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "bar.h"
5 #include "bar.h"
6 #include "percentbarchartseries.h"
6 #include "percentbarchartseries.h"
7 #include <QGraphicsItem>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class PercentBarGroup : public ChartItem
11 class PercentBarGroup : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0);
14 PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0);
14
15
15 // From ChartItem
16 public: // From ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos(const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
22 public:
19 // Layout "api"
23 // Layout "api"
20 void setPos(qreal x, qreal y);
24 void setPos(qreal x, qreal y);
21 void setBarWidth( int w ); // Default width for each bar
25 void setBarWidth( int w ); // Default width for each bar
22
26
23 int addColor( QColor color );
27 int addColor( QColor color );
24 void resetColors();
28 void resetColors();
25
29
26 // From QGraphicsItem
30 // From QGraphicsItem
27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
31 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 QRectF boundingRect() const;
32 QRectF boundingRect() const;
29
33
30 private:
34 private:
31
35
32 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
33 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
37 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
34
38
35 private:
39 private:
36
40
37 // Data
41 // Data
38 PercentBarChartSeries& mSeries;
42 PercentBarChartSeries& mSeries;
39 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
43 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
40 int mMax;
44 int mMax;
41
45
42 int mHeight; // Layout spesific
46 int mHeight; // Layout spesific
43 int mWidth;
47 int mWidth;
44 int mBarDefaultWidth;
48 int mBarDefaultWidth;
45
49
46 bool mLayoutSet; // True, if component has been laid out.
50 bool mLayoutSet; // True, if component has been laid out.
47 bool mLayoutDirty;
51 bool mLayoutDirty;
48
52
49 QList<QColor> mColors; // List of colors for series for now
53 QList<QColor> mColors; // List of colors for series for now
50
54
51 };
55 };
52
56
53 QTCOMMERCIALCHART_END_NAMESPACE
57 QTCOMMERCIALCHART_END_NAMESPACE
54
58
55 #endif // PERCENTBARGROUP_H
59 #endif // PERCENTBARGROUP_H
@@ -1,133 +1,144
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 ChartItem(parent)
8 QGraphicsItem(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::setPos(const QPointF & pos)
18 {
19 qDebug() << "PercentBarGroup::setPos";
20 // TODO:
21 }
17
22
18 void PercentBarGroup::setSize(const QSize& size)
23 void PercentBarGroup::resize(const QSize& size)
19 {
24 {
20 qDebug() << "PercentBarGroup::setSize";
25 qDebug() << "PercentBarGroup::resize";
21 mWidth = size.width();
26 mWidth = size.width();
22 mHeight = size.height();
27 mHeight = size.height();
23 layoutChanged();
28 layoutChanged();
24 mLayoutSet = true;
29 mLayoutSet = true;
25 }
30 }
26
31
27 void PercentBarGroup::setPlotDomain(const PlotDomain& data)
32 void PercentBarGroup::setPlotDomain(const PlotDomain& data)
28 {
33 {
29 qDebug() << "PercentBarGroup::setPlotDomain";
34 qDebug() << "PercentBarGroup::setPlotDomain";
30 // TODO:
35 // TODO:
31 }
36 }
32
37
38 void PercentBarGroup::setTheme(ChartTheme *theme)
39 {
40 qDebug() << "PercentBarGroup::setTheme";
41 // TODO:
42 }
43
33 void PercentBarGroup::setBarWidth( int w )
44 void PercentBarGroup::setBarWidth( int w )
34 {
45 {
35 mBarDefaultWidth = w;
46 mBarDefaultWidth = w;
36 }
47 }
37
48
38 int PercentBarGroup::addColor( QColor color )
49 int PercentBarGroup::addColor( QColor color )
39 {
50 {
40 int colorIndex = mColors.count();
51 int colorIndex = mColors.count();
41 mColors.append(color);
52 mColors.append(color);
42 return colorIndex;
53 return colorIndex;
43 }
54 }
44
55
45 void PercentBarGroup::resetColors()
56 void PercentBarGroup::resetColors()
46 {
57 {
47 mColors.clear();
58 mColors.clear();
48 }
59 }
49
60
50 void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
61 void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 {
62 {
52 if (!mLayoutSet) {
63 if (!mLayoutSet) {
53 qDebug() << "QBarChart::paint called without layout set. Aborting.";
64 qDebug() << "QBarChart::paint called without layout set. Aborting.";
54 return;
65 return;
55 }
66 }
56 if (mLayoutDirty) {
67 if (mLayoutDirty) {
57 // Layout or data has changed. Need to redraw.
68 // Layout or data has changed. Need to redraw.
58 foreach(QGraphicsItem* i, childItems()) {
69 foreach(QGraphicsItem* i, childItems()) {
59 i->paint(painter,option,widget);
70 i->paint(painter,option,widget);
60 }
71 }
61 }
72 }
62 }
73 }
63
74
64 QRectF PercentBarGroup::boundingRect() const
75 QRectF PercentBarGroup::boundingRect() const
65 {
76 {
66 return QRectF(0,0,mWidth,mHeight);
77 return QRectF(0,0,mWidth,mHeight);
67 }
78 }
68
79
69
80
70 void PercentBarGroup::dataChanged()
81 void PercentBarGroup::dataChanged()
71 {
82 {
72 qDebug() << "QBarChart::dataChanged mSeries";
83 qDebug() << "QBarChart::dataChanged mSeries";
73
84
74 // Find out maximum and minimum of all series
85 // Find out maximum and minimum of all series
75 mMax = mSeries.max();
86 mMax = mSeries.max();
76 mMin = mSeries.min();
87 mMin = mSeries.min();
77
88
78 // Delete old bars
89 // Delete old bars
79 // Is this correct way to delete childItems?
90 // Is this correct way to delete childItems?
80 foreach (QGraphicsItem* item, childItems()) {
91 foreach (QGraphicsItem* item, childItems()) {
81 delete item;
92 delete item;
82 }
93 }
83
94
84 // Create new graphic items for bars
95 // Create new graphic items for bars
85 int totalItems = mSeries.countTotalItems();
96 int totalItems = mSeries.countTotalItems();
86 for (int i=0; i<totalItems; i++) {
97 for (int i=0; i<totalItems; i++) {
87 Bar *bar = new Bar(this);
98 Bar *bar = new Bar(this);
88 childItems().append(bar);
99 childItems().append(bar);
89 }
100 }
90
101
91 mLayoutDirty = true;
102 mLayoutDirty = true;
92 }
103 }
93
104
94 void PercentBarGroup::layoutChanged()
105 void PercentBarGroup::layoutChanged()
95 {
106 {
96 // Scale bars to new layout
107 // Scale bars to new layout
97 // Layout for bars:
108 // Layout for bars:
98 if (mSeries.countRows() <= 0) {
109 if (mSeries.countRows() <= 0) {
99 // Nothing to do.
110 // Nothing to do.
100 return;
111 return;
101 }
112 }
102
113
103 // TODO: better way to auto-layout
114 // TODO: better way to auto-layout
104 // Use reals for accurancy (we might get some compiler warnings... :)
115 // Use reals for accurancy (we might get some compiler warnings... :)
105 int count = mSeries.countColumns();
116 int count = mSeries.countColumns();
106 int itemIndex(0);
117 int itemIndex(0);
107 qreal tW = mWidth;
118 qreal tW = mWidth;
108 qreal tC = count+1;
119 qreal tC = count+1;
109 qreal xStep = (tW/tC);
120 qreal xStep = (tW/tC);
110 qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
121 qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
111
122
112 for (int column = 0; column < mSeries.countColumns(); column++) {
123 for (int column = 0; column < mSeries.countColumns(); column++) {
113 qreal colSum = mSeries.columnSum(column);
124 qreal colSum = mSeries.columnSum(column);
114 qreal h = mHeight;
125 qreal h = mHeight;
115 qreal scale = (h / colSum);
126 qreal scale = (h / colSum);
116 qreal yPos = h;
127 qreal yPos = h;
117 for (int row=0; row < mSeries.countRows(); row++) {
128 for (int row=0; row < mSeries.countRows(); row++) {
118 qreal barHeight = mSeries.valueAt(row, column) * scale;
129 qreal barHeight = mSeries.valueAt(row, column) * scale;
119 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
130 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
120
131
121 // TODO: width settable per bar?
132 // TODO: width settable per bar?
122 bar->resize(mBarDefaultWidth, barHeight);
133 bar->resize(mBarDefaultWidth, barHeight);
123 bar->setColor(mColors.at(row));
134 bar->setColor(mColors.at(row));
124 bar->setPos(xPos, yPos);
135 bar->setPos(xPos, yPos);
125 itemIndex++;
136 itemIndex++;
126 yPos -= barHeight;
137 yPos -= barHeight;
127 }
138 }
128 xPos += xStep;
139 xPos += xStep;
129 }
140 }
130 mLayoutDirty = true;
141 mLayoutDirty = true;
131 }
142 }
132
143
133 QTCOMMERCIALCHART_END_NAMESPACE
144 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,58
1 #ifndef STACKEDBARGROUP_H
1 #ifndef STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitemcontrol.h"
5 #include "bar.h"
5 #include "bar.h"
6 #include "stackedbarchartseries.h"
6 #include "stackedbarchartseries.h"
7 #include <QGraphicsItem>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class StackedBarGroup : public ChartItem
11 class StackedBarGroup : public QGraphicsItem, public ChartItemControl
11 {
12 {
12 public:
13 public:
13 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
14 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
14
15
15 // From ChartItem
16 public: // From ChartItemControl
16 virtual void setSize(const QSize& size);
17 void setPos(const QPointF & pos);
17 virtual void setPlotDomain(const PlotDomain& data);
18 void resize(const QSize &size);
19 void setTheme(ChartTheme *theme);
20 void setPlotDomain(const PlotDomain& data);
18
21
19 // Layout "api"
22 public: // Layout "api"
20 void setPos(qreal x, qreal y);
23 void setPos(qreal x, qreal y);
21 void setBarWidth( int w ); // Default width for each bar
24 void setBarWidth( int w ); // Default width for each bar
22
25
23 int addColor( QColor color );
26 int addColor( QColor color );
24 void resetColors();
27 void resetColors();
25
28
26 // From QGraphicsItem
29 // From QGraphicsItem
27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 QRectF boundingRect() const;
31 QRectF boundingRect() const;
29
32
30 private:
33 private:
31
34
32 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
33 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
36 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
34
37
35 private:
38 private:
36
39
37 // Data
40 // Data
38 StackedBarChartSeries& mSeries;
41 StackedBarChartSeries& mSeries;
39 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)
40 int mMax;
43 int mMax;
41
44
42 int mHeight; // Layout spesific
45 int mHeight; // Layout spesific
43 int mWidth;
46 int mWidth;
44 int mBarDefaultWidth;
47 int mBarDefaultWidth;
45
48
46 bool mLayoutSet; // True, if component has been laid out.
49 bool mLayoutSet; // True, if component has been laid out.
47 bool mLayoutDirty;
50 bool mLayoutDirty;
48
51
49 QList<QColor> mColors; // List of colors for series for now
52 QList<QColor> mColors; // List of colors for series for now
50
53
51 };
54 };
52
55
53 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
54
57
55 #endif // STACKEDBARGROUP_H
58 #endif // STACKEDBARGROUP_H
@@ -1,77 +1,73
1 #include "pieslice.h"
1 #include "pieslice.h"
2 #include <QPainter>
2 #include <QPainter>
3 #include <QDebug>
3 #include <QDebug>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect)
7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect)
8 : m_color(color),
8 : m_color(color),
9 m_startAngle(startAngle),
9 m_startAngle(startAngle),
10 m_span(span),
10 m_span(span),
11 m_rect(rect)
11 m_rect(rect)
12 {
12 {
13 setAcceptHoverEvents(true);
13 setAcceptHoverEvents(true);
14 }
14 }
15
15
16 PieSlice::~PieSlice()
16 PieSlice::~PieSlice()
17 {
17 {
18 }
18 }
19
19
20 QRectF PieSlice::boundingRect() const
20 QRectF PieSlice::boundingRect() const
21 {
21 {
22 return m_rect;
22 return m_rect;
23 }
23 }
24
24
25 QPainterPath PieSlice::shape() const
25 QPainterPath PieSlice::shape() const
26 {
26 {
27 qreal angle = (-m_startAngle) + (90);
27 qreal angle = (-m_startAngle) + (90);
28 qreal span = -m_span;
28 qreal span = -m_span;
29
29
30 QPainterPath path;
30 QPainterPath path;
31 path.moveTo(boundingRect().center());
31 path.moveTo(boundingRect().center());
32 path.arcTo(boundingRect(), angle, span);
32 path.arcTo(boundingRect(), angle, span);
33
33
34 // TODO: draw the shape so that it might have a hole in the center
34 // TODO: draw the shape so that it might have a hole in the center
35 // - Sin & Cos will be needed to find inner/outer arc endpoints
35 // - Sin & Cos will be needed to find inner/outer arc endpoints
36
36
37 // dx, dy are offsets from the center
37 // dx, dy are offsets from the center
38 //qreal l = boundingRect().height();
38 //qreal l = boundingRect().height();
39 //qreal dx = qSin(angle*(M_PI/180)) * l;
39 //qreal dx = qSin(angle*(M_PI/180)) * l;
40 //qreal dy = qCos(angle*(M_PI/180)) * l;
40 //qreal dy = qCos(angle*(M_PI/180)) * l;
41
41
42 // TODO: exploded slice?
42 // TODO: exploded slice?
43
43
44 return path;
44 return path;
45 }
45 }
46
46
47 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 void PieSlice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
48 {
48 {
49 // Setup painter
50 painter->setBrush(m_color);
51 painter->setRenderHint(QPainter::Antialiasing);
49 painter->setRenderHint(QPainter::Antialiasing);
52 QPen pen;
50 painter->setPen(m_theme.linePen);
53 //pen.setColor(m_color.darker());
51 // TODO:
54 pen.setColor(Qt::gray);
52 painter->setBrush(m_theme.linePen.color());
55 pen.setWidth(1);
56 painter->setPen(pen);
57
53
58 // From Qt docs:
54 // From Qt docs:
59 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
55 // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360).
60 // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction.
56 // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction.
61 // Zero degrees is at the 3 o'clock position.
57 // Zero degrees is at the 3 o'clock position.
62 //
58 //
63 // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360.
59 // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360.
64 //qreal angle = (-m_startAngle*16) + (90*16);
60 //qreal angle = (-m_startAngle*16) + (90*16);
65 //qreal span = -m_span*16;
61 //qreal span = -m_span*16;
66 //painter->drawPie(boundingRect(), angle, span);
62 //painter->drawPie(boundingRect(), angle, span);
67
63
68 painter->drawPath(shape());
64 painter->drawPath(shape());
69 }
65 }
70
66
71 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
67 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
72 {
68 {
73 QGraphicsItem::hoverEnterEvent(event);
69 QGraphicsItem::hoverEnterEvent(event);
74 qDebug() << "hover" << m_color << m_startAngle << m_span;
70 qDebug() << "hover" << m_color << m_startAngle << m_span;
75 }
71 }
76
72
77 QTCOMMERCIALCHART_END_NAMESPACE
73 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,32 +1,34
1 #ifndef PIESLICE_H
1 #ifndef PIESLICE_H
2 #define PIESLICE_H
2 #define PIESLICE_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "charttheme_p.h"
5 #include <QGraphicsItem>
6 #include <QGraphicsItem>
6 #include <QRectF>
7 #include <QRectF>
7 #include <QColor>
8 #include <QColor>
8
9
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11
11 class PieSlice : public QGraphicsItem
12 class PieSlice : public QGraphicsItem
12 {
13 {
13 public:
14 public:
14 PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect);
15 PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect);
15 ~PieSlice();
16 ~PieSlice();
16
17
17 public: // from QGraphicsItem
18 public: // from QGraphicsItem
18 QRectF boundingRect() const;
19 QRectF boundingRect() const;
19 QPainterPath shape() const;
20 QPainterPath shape() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
22 void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
22
23
23 public:
24 public:
24 QColor m_color;
25 QColor m_color;
25 qreal m_startAngle;
26 qreal m_startAngle;
26 qreal m_span;
27 qreal m_span;
27 QRectF m_rect;
28 QRectF m_rect;
29 SeriesTheme m_theme;
28 };
30 };
29
31
30 QTCOMMERCIALCHART_END_NAMESPACE
32 QTCOMMERCIALCHART_END_NAMESPACE
31
33
32 #endif // PIESLICE_H
34 #endif // PIESLICE_H
@@ -1,450 +1,368
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 "qxychartseries.h"
7 #include "qxychartseries.h"
7 #include "qchartaxis.h"
8 #include "qchartaxis.h"
8 #include "barchartseries.h"
9 #include "barchartseries.h"
9 #include "bargroup.h"
10 #include "bargroup.h"
10 #include "stackedbarchartseries.h"
11 #include "stackedbarchartseries.h"
11 #include "stackedbargroup.h"
12 #include "stackedbargroup.h"
12 #include "percentbarchartseries.h"
13 #include "percentbarchartseries.h"
13 #include "percentbargroup.h"
14 #include "percentbargroup.h"
15 #include "charttheme_p.h"
16 #include "chartitemcontrol.h"
14
17
15 #include "xylinechartitem_p.h"
18 #include "xylinechartitem_p.h"
16 #include "plotdomain_p.h"
19 #include "plotdomain_p.h"
17 #include "axisitem_p.h"
20 #include "axisitem_p.h"
18 #include <QGraphicsScene>
21 #include <QGraphicsScene>
19 #include <QDebug>
22 #include <QDebug>
20
23
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22
25
23 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
26 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
24 m_backgroundItem(0),
27 m_backgroundItem(0),
25 m_titleItem(0),
28 m_titleItem(0),
26 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
27 m_plotDataIndex(0),
30 m_plotDataIndex(0),
28 m_marginSize(0)
31 m_marginSize(0),
32 m_chartTheme(new ChartTheme())
29 {
33 {
30 // TODO: the default theme?
34 // TODO: the default theme?
31 setTheme(QChart::ChartThemeDefault);
35 setTheme(QChart::ChartThemeDefault);
32
36
33 PlotDomain domain;
37 PlotDomain domain;
34 m_plotDomainList<<domain;
38 m_plotDomainList << domain;
35 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
39 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
36 m_chartItems<<m_axisXItem;
40 m_chartItemControls << m_axisXItem;
37 m_chartItems<<m_axisYItem.at(0);
41 m_chartItemControls << m_axisYItem.at(0);
38 }
42 }
39
43
40 QChart::~QChart(){}
44 QChart::~QChart(){}
41
45
42 QRectF QChart::boundingRect() const
46 QRectF QChart::boundingRect() const
43 {
47 {
44 return m_rect;
48 return m_rect;
45 }
49 }
46
50
47 void QChart::addSeries(QChartSeries* series)
51 void QChart::addSeries(QChartSeries* series)
48 {
52 {
49 // TODO: we should check the series not already added
53 // TODO: we should check the series not already added
50
54
51 m_chartSeries << series;
55 m_chartSeries << series;
52
56
53 switch(series->type())
57 switch(series->type())
54 {
58 {
55 case QChartSeries::SeriesTypeLine: {
59 case QChartSeries::SeriesTypeLine: {
56
60
57 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
61 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
58 // Use color defined by theme in case the series does not define a custom color
59
60 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
61 xyseries->setPen(nextColor());
62
63 m_plotDataIndex = 0 ;
62 m_plotDataIndex = 0 ;
64 m_plotDomainList.resize(1);
63 m_plotDomainList.resize(1);
65
64
66 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
65 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
67
66
68 for (int i = 0 ; i < xyseries->count() ; i++)
67 for (int i = 0 ; i < xyseries->count() ; i++)
69 {
68 {
70 qreal x = xyseries->x(i);
69 qreal x = xyseries->x(i);
71 qreal y = xyseries->y(i);
70 qreal y = xyseries->y(i);
72 domain.m_minX = qMin(domain.m_minX,x);
71 domain.m_minX = qMin(domain.m_minX,x);
73 domain.m_minY = qMin(domain.m_minY,y);
72 domain.m_minY = qMin(domain.m_minY,y);
74 domain.m_maxX = qMax(domain.m_maxX,x);
73 domain.m_maxX = qMax(domain.m_maxX,x);
75 domain.m_maxY = qMax(domain.m_maxY,y);
74 domain.m_maxY = qMax(domain.m_maxY,y);
76 }
75 }
77
76
78 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
77 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
79 m_chartItems<<item;
80
78
81 foreach(ChartItem* i ,m_chartItems)
79 // TODO: combine ChartItemControl and ChartItem apis
80 m_chartItemControls << item;
81 item->setTheme(m_chartTheme);
82
83 foreach(ChartItemControl* i, m_chartItemControls)
82 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
84 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
83
85
84 break;
86 break;
85 }
87 }
86 case QChartSeries::SeriesTypeBar: {
88 case QChartSeries::SeriesTypeBar: {
87
89
88 qDebug() << "barSeries added";
90 qDebug() << "barSeries added";
89 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
91 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
90 BarGroup* barGroup = new BarGroup(*barSeries,this);
92 BarGroup* barGroup = new BarGroup(*barSeries,this);
91
93
92 // Add some fugly colors for 5 fist series...
94 // Add some fugly colors for 5 fist series...
93 barGroup->addColor(QColor(255,0,0,128));
95 barGroup->addColor(QColor(255,0,0,128));
94 barGroup->addColor(QColor(255,255,0,128));
96 barGroup->addColor(QColor(255,255,0,128));
95 barGroup->addColor(QColor(0,255,0,128));
97 barGroup->addColor(QColor(0,255,0,128));
96 barGroup->addColor(QColor(0,0,255,128));
98 barGroup->addColor(QColor(0,0,255,128));
97 barGroup->addColor(QColor(255,128,0,128));
99 barGroup->addColor(QColor(255,128,0,128));
98
100
99 m_chartItems<<barGroup;
101 m_chartItemControls << barGroup;
100 childItems().append(barGroup);
102 childItems().append(barGroup);
101 break;
103 break;
102 }
104 }
103 case QChartSeries::SeriesTypeStackedBar: {
105 case QChartSeries::SeriesTypeStackedBar: {
104
106
105 qDebug() << "barSeries added";
107 qDebug() << "barSeries added";
106 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
108 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
107 StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this);
109 StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this);
108
110
109 // Add some fugly colors for 5 fist series...
111 // Add some fugly colors for 5 fist series...
110 stackedBarGroup->addColor(QColor(255,0,0,128));
112 stackedBarGroup->addColor(QColor(255,0,0,128));
111 stackedBarGroup->addColor(QColor(255,255,0,128));
113 stackedBarGroup->addColor(QColor(255,255,0,128));
112 stackedBarGroup->addColor(QColor(0,255,0,128));
114 stackedBarGroup->addColor(QColor(0,255,0,128));
113 stackedBarGroup->addColor(QColor(0,0,255,128));
115 stackedBarGroup->addColor(QColor(0,0,255,128));
114 stackedBarGroup->addColor(QColor(255,128,0,128));
116 stackedBarGroup->addColor(QColor(255,128,0,128));
115
117
116 m_chartItems<<stackedBarGroup;
118 m_chartItemControls << stackedBarGroup;
117 childItems().append(stackedBarGroup);
119 childItems().append(stackedBarGroup);
118 break;
120 break;
119 }
121 }
120 case QChartSeries::SeriesTypePercentBar: {
122 case QChartSeries::SeriesTypePercentBar: {
121
123
122 qDebug() << "barSeries added";
124 qDebug() << "barSeries added";
123 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
125 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
124 PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this);
126 PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this);
125
127
126 // Add some fugly colors for 5 fist series...
128 // Add some fugly colors for 5 fist series...
127 percentBarGroup->addColor(QColor(255,0,0,128));
129 percentBarGroup->addColor(QColor(255,0,0,128));
128 percentBarGroup->addColor(QColor(255,255,0,128));
130 percentBarGroup->addColor(QColor(255,255,0,128));
129 percentBarGroup->addColor(QColor(0,255,0,128));
131 percentBarGroup->addColor(QColor(0,255,0,128));
130 percentBarGroup->addColor(QColor(0,0,255,128));
132 percentBarGroup->addColor(QColor(0,0,255,128));
131 percentBarGroup->addColor(QColor(255,128,0,128));
133 percentBarGroup->addColor(QColor(255,128,0,128));
132
134
133 m_chartItems<<percentBarGroup;
135 m_chartItemControls << percentBarGroup;
134 childItems().append(percentBarGroup);
136 childItems().append(percentBarGroup);
135 break;
137 break;
136 }
138 }
137 case QChartSeries::SeriesTypeScatter: {
139 case QChartSeries::SeriesTypeScatter: {
138 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
140 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
141 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
139 scatterSeries->d->setParentItem(this);
142 scatterSeries->d->setParentItem(this);
140 // Set pre-defined colors in case the series has no colors defined
143 m_chartItemControls << scatterSeries->d;
141 if (!scatterSeries->markerColor().isValid())
144 //TODO:? scatterSeries->d->m_themeIndex = m_chartSeries.count() - 1;
142 scatterSeries->setMarkerColor(nextColor());
143 connect(this, SIGNAL(sizeChanged(QRectF)),
144 scatterSeries, SLOT(chartSizeChanged(QRectF)));
145 // QColor nextColor = m_themeColors.takeFirst();
146 // nextColor.setAlpha(150); // TODO: default opacity?
147 // scatterSeries->setMarkerColor(nextColor);
148 break;
145 break;
149 }
146 }
150 case QChartSeries::SeriesTypePie: {
147 case QChartSeries::SeriesTypePie: {
151 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
148 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
152 for (int i(0); i < pieSeries->sliceCount(); i++) {
149 // for (int i(0); i < pieSeries->sliceCount(); i++) {
153 if (!pieSeries->sliceColor(i).isValid())
150 // if (!pieSeries->sliceColor(i).isValid())
154 pieSeries->setSliceColor(i, nextColor());
151 // pieSeries->setSliceColor(i, nextColor());
155 }
152 // }
156 connect(this, SIGNAL(sizeChanged(QRectF)),
153 pieSeries->d->setTheme(m_chartTheme);
157 pieSeries, SLOT(chartSizeChanged(QRectF)));
154 m_chartItemControls << pieSeries->d;
158
155
159 // 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
160 // 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?
161 // for (int (i); i < pieSeries.sliceCount(); i++)
158 // for (int (i); i < pieSeries.sliceCount(); i++)
162 break;
159 break;
163 }
160 }
164 }
161 }
165 }
162 }
166
163
167 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
164 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
168 {
165 {
169 // TODO: support also other types; not only scatter and pie
166 // TODO: support also other types; not only scatter and pie
170
167
171 QChartSeries *series(0);
168 QChartSeries *series(0);
172
169
173 switch (type) {
170 switch (type) {
174 case QChartSeries::SeriesTypeLine: {
171 case QChartSeries::SeriesTypeLine: {
175 series = QXYChartSeries::create();
172 series = QXYChartSeries::create();
176 break;
173 break;
177 }
174 }
178 case QChartSeries::SeriesTypeBar: {
175 case QChartSeries::SeriesTypeBar: {
179 series = new BarChartSeries(this);
176 series = new BarChartSeries(this);
180 break;
177 break;
181 }
178 }
182 case QChartSeries::SeriesTypeStackedBar: {
179 case QChartSeries::SeriesTypeStackedBar: {
183 series = new StackedBarChartSeries(this);
180 series = new StackedBarChartSeries(this);
184 break;
181 break;
185 }
182 }
186 case QChartSeries::SeriesTypePercentBar: {
183 case QChartSeries::SeriesTypePercentBar: {
187 series = new PercentBarChartSeries(this);
184 series = new PercentBarChartSeries(this);
188 break;
185 break;
189 }
186 }
190 case QChartSeries::SeriesTypeScatter: {
187 case QChartSeries::SeriesTypeScatter: {
191 series = new QScatterSeries(this);
188 series = new QScatterSeries(this);
192 break;
189 break;
193 }
190 }
194 case QChartSeries::SeriesTypePie: {
191 case QChartSeries::SeriesTypePie: {
195 series = new QPieSeries(this);
192 series = new QPieSeries(this);
196 break;
193 break;
197 }
194 }
198 default:
195 default:
199 Q_ASSERT(false);
196 Q_ASSERT(false);
200 break;
197 break;
201 }
198 }
202
199
203 addSeries(series);
200 addSeries(series);
204 return series;
201 return series;
205 }
202 }
206
203
207 void QChart::setSize(const QSize& size)
204 void QChart::setSize(const QSize& size)
208 {
205 {
209 m_rect = QRect(QPoint(0,0),size);
206 m_rect = QRect(QPoint(0,0),size);
210 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
207 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
211
208
212 //recaculate title
209 //recaculate title
213 if(m_titleItem){
210 if(m_titleItem){
214 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
211 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
215 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
212 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
216
213
217 }
214 }
218
215
219 //recalculate background gradient
216 //recalculate background gradient
220 if(m_backgroundItem){
217 if (m_backgroundItem) {
221 m_backgroundItem->setRect(rect);
218 m_backgroundItem->setRect(rect);
222 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
219 if (m_bacgroundOrinetation == HorizonatlGradientOrientation)
223 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
220 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0);
224 else
221 else
225 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
222 m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height());
226
227 m_backgroundItem->setBrush(m_backgroundGradient);
223 m_backgroundItem->setBrush(m_backgroundGradient);
228 }
224 }
229
225
230 //resize elements
226 foreach (ChartItemControl *ctrl, m_chartItemControls) {
231 foreach (ChartItem* item ,m_chartItems) {
227 ctrl->setPos(rect.topLeft());
232 item->setPos(rect.topLeft());
228 ctrl->resize(rect.size());
233 item->setSize(rect.size());
234
235 }
229 }
236 // TODO: TTD for setting scale
237 //emit scaleChanged(100, 100);
238 // TODO: calculate the origo
239 // TODO: not sure if emitting a signal here is the best from performance point of view
240 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
241
230
242 update();
231 update();
243 }
232 }
244
233
245 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
234 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
246 {
235 {
247
236
248 if(!m_backgroundItem){
237 if(!m_backgroundItem){
249 m_backgroundItem = new QGraphicsRectItem(this);
238 m_backgroundItem = new QGraphicsRectItem(this);
250 m_backgroundItem->setZValue(-1);
239 m_backgroundItem->setZValue(-1);
251 }
240 }
252
241
253 m_bacgroundOrinetation = orientation;
242 m_bacgroundOrinetation = orientation;
254 m_backgroundGradient.setColorAt( 0.0, startColor);
243 m_backgroundGradient.setColorAt(0.0, startColor);
255 m_backgroundGradient.setColorAt( 1.0, endColor);
244 m_backgroundGradient.setColorAt(1.0, endColor);
256 m_backgroundGradient.setStart(0,0);
245 m_backgroundGradient.setStart(0,0);
257
246
258 if(orientation == VerticalGradientOrientation){
247 if(orientation == VerticalGradientOrientation){
259 m_backgroundGradient.setFinalStop(0,m_rect.height());
248 m_backgroundGradient.setFinalStop(0,m_rect.height());
260 }else{
249 }else{
261 m_backgroundGradient.setFinalStop(m_rect.width(),0);
250 m_backgroundGradient.setFinalStop(m_rect.width(),0);
262 }
251 }
263
252
264 m_backgroundItem->setBrush(m_backgroundGradient);
253 m_backgroundItem->setBrush(m_backgroundGradient);
265 m_backgroundItem->setPen(Qt::NoPen);
254 m_backgroundItem->setPen(Qt::NoPen);
266 m_backgroundItem->update();
255 m_backgroundItem->update();
267 }
256 }
268
257
269 void QChart::setTitle(const QString& title,const QFont& font)
258 void QChart::setTitle(const QString& title,const QFont& font)
270 {
259 {
271 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
260 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
272 m_titleItem->setPlainText(title);
261 m_titleItem->setPlainText(title);
273 m_titleItem->setFont(font);
262 m_titleItem->setFont(font);
274 }
263 }
275
264
276 int QChart::margin() const
265 int QChart::margin() const
277 {
266 {
278 return m_marginSize;
267 return m_marginSize;
279 }
268 }
280
269
281 void QChart::setMargin(int margin)
270 void QChart::setMargin(int margin)
282 {
271 {
283 m_marginSize = margin;
272 m_marginSize = margin;
284 }
273 }
285
274
286 void QChart::setTheme(QChart::ChartThemeId theme)
275 void QChart::setTheme(QChart::ChartThemeId theme)
287 {
276 {
288 // if (theme != m_currentTheme) {
277 if (theme != m_chartTheme->d->m_currentTheme) {
289 m_themeColors.clear();
278 m_chartTheme->d->setTheme(theme);
290
279 setBackground(m_chartTheme->d->m_gradientStartColor,
291 // TODO: define color themes
280 m_chartTheme->d->m_gradientEndColor,
292 switch (theme) {
281 m_bacgroundOrinetation);
293 case QChart::ChartThemeDefault:
282 foreach (ChartItemControl *ctrl, m_chartItemControls)
294 // TODO: define the default theme based on the OS
283 ctrl->setTheme(m_chartTheme);
295 m_themeColors.append(QColor(QRgb(0xff000000)));
284 update();
296 m_themeColors.append(QColor(QRgb(0xff707070)));
297 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
298 break;
299 case QChart::ChartThemeVanilla:
300 m_themeColors.append(QColor(217, 197, 116));
301 m_themeColors.append(QColor(214, 168, 150));
302 m_themeColors.append(QColor(160, 160, 113));
303 m_themeColors.append(QColor(210, 210, 52));
304 m_themeColors.append(QColor(136, 114, 58));
305
306 setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
307 break;
308 case QChart::ChartThemeIcy:
309 m_themeColors.append(QColor(0, 3, 165));
310 m_themeColors.append(QColor(49, 52, 123));
311 m_themeColors.append(QColor(71, 114, 187));
312 m_themeColors.append(QColor(48, 97, 87));
313 m_themeColors.append(QColor(19, 71, 90));
314 m_themeColors.append(QColor(110, 70, 228));
315
316 setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation);
317 break;
318 case QChart::ChartThemeGrayscale:
319 m_themeColors.append(QColor(0, 0, 0));
320 m_themeColors.append(QColor(50, 50, 50));
321 m_themeColors.append(QColor(100, 100, 100));
322 m_themeColors.append(QColor(140, 140, 140));
323 m_themeColors.append(QColor(180, 180, 180));
324
325 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
326 break;
327 case QChart::ChartThemeUnnamed1:
328 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
329 m_themeColors.append(QColor(QRgb(0xff7AC943)));
330 m_themeColors.append(QColor(QRgb(0xffFF931E)));
331 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
332 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
333
334 setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
335 break;
336 default:
337 Q_ASSERT(false);
338 break;
339 }
340
341 if(m_backgroundItem){
342 m_backgroundItem->setBrush(m_backgroundGradient);
343 m_backgroundItem->setPen(Qt::NoPen);
344 }
345
346 foreach(QChartSeries* series, m_chartSeries) {
347 // TODO: other series interested on themes?
348 if (series->type() == QChartSeries::SeriesTypeLine) {
349 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
350 lineseries->setPen(nextColor());
351 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
352 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
353 scatter->setMarkerColor(nextColor());
354 } else if (series->type() == QChartSeries::SeriesTypePie) {
355 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
356 for (int i(0); i < pieSeries->sliceCount(); i++)
357 pieSeries->setSliceColor(i, nextColor());
358 }
359 }
285 }
360 update();
361 }
362
363 QColor QChart::nextColor()
364 {
365 QColor nextColor = m_themeColors.first();
366 m_themeColors.move(0, m_themeColors.size() - 1);
367 return nextColor;
368 }
286 }
369
287
370 void QChart::zoomInToRect(const QRect& rectangle)
288 void QChart::zoomInToRect(const QRect& rectangle)
371 {
289 {
372
290
373 if(!rectangle.isValid()) return;
291 if(!rectangle.isValid()) return;
374
292
375 qreal margin = this->margin();
293 qreal margin = this->margin();
376
294
377 QRect rect = rectangle.normalized();
295 QRect rect = rectangle.normalized();
378 rect.translate(-margin, -margin);
296 rect.translate(-margin, -margin);
379
297
380 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
298 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
381
299
382 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
300 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
383
301
384 m_plotDomainList.resize(m_plotDataIndex + 1);
302 m_plotDomainList.resize(m_plotDataIndex + 1);
385 m_plotDomainList<<domain;
303 m_plotDomainList<<domain;
386 m_plotDataIndex++;
304 m_plotDataIndex++;
387
305
388 foreach (ChartItem* item ,m_chartItems)
306 foreach (ChartItemControl* ctrl, m_chartItemControls)
389 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
307 ctrl->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
390 update();
308 update();
391 }
309 }
392
310
393 void QChart::zoomIn()
311 void QChart::zoomIn()
394 {
312 {
395 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
313 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
396 m_plotDataIndex++;
314 m_plotDataIndex++;
397 foreach (ChartItem* item ,m_chartItems)
315 foreach (ChartItemControl* item, m_chartItemControls)
398 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
316 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
399 update();
317 update();
400 } else {
318 } else {
401 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
319 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
402 rect.setWidth(rect.width()/2);
320 rect.setWidth(rect.width()/2);
403 rect.setHeight(rect.height()/2);
321 rect.setHeight(rect.height()/2);
404 rect.moveCenter(m_rect.center());
322 rect.moveCenter(m_rect.center());
405 zoomInToRect(rect);
323 zoomInToRect(rect);
406 }
324 }
407 }
325 }
408
326
409 void QChart::zoomOut()
327 void QChart::zoomOut()
410 {
328 {
411 if (m_plotDataIndex > 0) {
329 if (m_plotDataIndex > 0) {
412 m_plotDataIndex--;
330 m_plotDataIndex--;
413 foreach (ChartItem* item ,m_chartItems)
331 foreach (ChartItemControl* item, m_chartItemControls)
414 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
332 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
415 update();
333 update();
416 }
334 }
417 }
335 }
418
336
419 void QChart::zoomReset()
337 void QChart::zoomReset()
420 {
338 {
421 if (m_plotDataIndex > 0) {
339 if (m_plotDataIndex > 0) {
422 m_plotDataIndex = 0;
340 m_plotDataIndex = 0;
423 foreach (ChartItem* item ,m_chartItems)
341 foreach (ChartItemControl* item, m_chartItemControls)
424 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
342 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
425 update();
343 update();
426 }
344 }
427 }
345 }
428
346
429 void QChart::setAxisX(const QChartAxis& axis)
347 void QChart::setAxisX(const QChartAxis& axis)
430 {
348 {
431 setAxis(m_axisXItem,axis);
349 setAxis(m_axisXItem,axis);
432 }
350 }
433 void QChart::setAxisY(const QChartAxis& axis)
351 void QChart::setAxisY(const QChartAxis& axis)
434 {
352 {
435 setAxis(m_axisYItem.at(0),axis);
353 setAxis(m_axisYItem.at(0),axis);
436 }
354 }
437
355
438 void QChart::setAxisY(const QList<QChartAxis>& axis)
356 void QChart::setAxisY(const QList<QChartAxis>& axis)
439 {
357 {
440 //TODO not implemented
358 //TODO not implemented
441 }
359 }
442
360
443 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
361 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
444 {
362 {
445 item->setVisible(axis.isAxisVisible());
363 item->setVisible(axis.isAxisVisible());
446 }
364 }
447
365
448 #include "moc_qchart.cpp"
366 #include "moc_qchart.cpp"
449
367
450 QTCOMMERCIALCHART_END_NAMESPACE
368 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,104 +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 ChartItem;
16 class BarGroup;
15 class BarGroup;
17 class QChartAxis;
16 class QChartAxis;
17 class ChartTheme;
18 class ChartItemControl;
18
19
19 // TODO: We don't need to have QChart tied to QGraphicsItem:
20 // TODO: We don't need to have QChart tied to QGraphicsItem:
20 //class QTCOMMERCIALCHART_EXPORT QChart
21 //class QTCOMMERCIALCHART_EXPORT QChart
21 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
22 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
22 // public: QChartGraphicsItem(QChart &chart);
23 // public: QChartGraphicsItem(QChart &chart);
23
24
24 /*!
25 /*!
25 * TODO: define the responsibilities
26 * TODO: define the responsibilities
26 */
27 */
27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
28 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
28 {
29 {
29 Q_OBJECT
30 Q_OBJECT
30 public:
31 public:
31 enum GradientOrientation {
32 enum GradientOrientation {
32 HorizonatlGradientOrientation,
33 HorizonatlGradientOrientation,
33 VerticalGradientOrientation
34 VerticalGradientOrientation
34 };
35 };
35 enum ChartThemeId {
36 enum ChartThemeId {
37 ChartThemeInvalid = -1,
36 /*! The default theme follows the GUI style of the Operating System */
38 /*! The default theme follows the GUI style of the Operating System */
37 ChartThemeDefault = 0,
39 ChartThemeDefault,
38 ChartThemeVanilla,
40 ChartThemeVanilla,
39 ChartThemeIcy,
41 ChartThemeIcy,
40 ChartThemeGrayscale,
42 ChartThemeGrayscale,
41 //ChartThemeScientific,
43 //ChartThemeScientific,
42 ChartThemeUnnamed1
44 ChartThemeUnnamed1
43 };
45 };
44
46
45 public:
47 public:
46 QChart(QGraphicsObject* parent = 0);
48 QChart(QGraphicsObject* parent = 0);
47 ~QChart();
49 ~QChart();
48
50
49 //from QGraphicsItem
51 //from QGraphicsItem
50 QRectF boundingRect() const;
52 QRectF boundingRect() const;
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
52
54
53 void addSeries(QChartSeries* series);
55 void addSeries(QChartSeries* series);
54 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
56 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
55 // 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...
56 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
58 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
57
59
58 void setSize(const QSize& size);
60 void setSize(const QSize& size);
59 void setMargin(int margin);
61 void setMargin(int margin);
60 int margin() const;
62 int margin() const;
61 void setTheme(QChart::ChartThemeId theme);
63 void setTheme(QChart::ChartThemeId theme);
62
64
63 void setTitle(const QString& title,const QFont& font = QFont());
65 void setTitle(const QString& title,const QFont& font = QFont());
64 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);
65
67
66 void zoomInToRect(const QRect& rectangle);
68 void zoomInToRect(const QRect& rectangle);
67 void zoomIn();
69 void zoomIn();
68 void zoomOut();
70 void zoomOut();
69 void zoomReset();
71 void zoomReset();
70
72
71 void setAxisX(const QChartAxis& axis);
73 void setAxisX(const QChartAxis& axis);
72 void setAxisY(const QChartAxis& axis);
74 void setAxisY(const QChartAxis& axis);
73 void setAxisY(const QList<QChartAxis>& axis);
75 void setAxisY(const QList<QChartAxis>& axis);
74
76
75 private:
77 private:
76 void setAxis(AxisItem *item, const QChartAxis& axis);
78 void setAxis(AxisItem *item, const QChartAxis& axis);
77
79
78 signals:
79 //TODO chage to const QSize& size
80 void sizeChanged(QRectF rect);
81 void scaleChanged(qreal xscale, qreal yscale);
82
83 private:
80 private:
84 QColor nextColor();
85
86 Q_DISABLE_COPY(QChart)
81 Q_DISABLE_COPY(QChart)
87 QGraphicsRectItem* m_backgroundItem;
82 QGraphicsRectItem* m_backgroundItem;
88 QLinearGradient m_backgroundGradient;
83 QLinearGradient m_backgroundGradient;
89 GradientOrientation m_bacgroundOrinetation;
84 GradientOrientation m_bacgroundOrinetation;
90 QGraphicsTextItem* m_titleItem;
85 QGraphicsTextItem* m_titleItem;
91 AxisItem* m_axisXItem;
86 AxisItem* m_axisXItem;
92 QList<AxisItem*> m_axisYItem;
87 QList<AxisItem*> m_axisYItem;
93 QRect m_rect;
88 QRect m_rect;
94 QList<QChartSeries*> m_chartSeries;
89 QList<QChartSeries *> m_chartSeries;
90 QList<ChartItemControl *> m_chartItemControls;
95 QVector<PlotDomain> m_plotDomainList;
91 QVector<PlotDomain> m_plotDomainList;
96 QList<ChartItem*> m_chartItems;
97 int m_plotDataIndex;
92 int m_plotDataIndex;
98 int m_marginSize;
93 int m_marginSize;
99 QList<QColor> m_themeColors;
94 ChartTheme *m_chartTheme;
100 };
95 };
101
96
102 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
103
98
104 #endif
99 #endif
@@ -1,148 +1,192
1 #include "qpieseries_p.h"
1 #include "qpieseries.h"
2 #include "qpieseries.h"
2 #include "pieslice.h"
3 #include <QGraphicsObject>
3 #include <QGraphicsObject>
4 #include "pieslice.h"
4 #include <QDebug>
5 #include <QDebug>
5
6
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8
8 QPieSeries::QPieSeries(QGraphicsObject *parent) :
9 QPieSeriesPrivate::QPieSeriesPrivate() :
9 QChartSeries(parent),
10 m_sizeFactor(1.0),
10 m_sizeFactor(1.0),
11 m_position(PiePositionMaximized)
11 m_position(QPieSeries::PiePositionMaximized)
12 {
12 {
13 }
13 }
14
14
15 QPieSeries::~QPieSeries()
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 QPieSeries::setData(QList<qreal> data)
21 bool QPieSeriesPrivate::setData(QList<qreal> data)
22 {
22 {
23 m_data = data;
23 m_data = data;
24
24
25 // Create slices
25 if (m_parentItem) {
26 qreal fullPie = 360;
26 // Create slices
27 qreal total = 0;
27 qreal fullPie = 360;
28 foreach (qreal value, m_data)
28 qreal total = 0;
29 total += value;
29 foreach (qreal value, m_data)
30 total += value;
31
32 m_chartSize = m_parentItem->boundingRect();
33 qreal angle = 0;
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) {
36 qreal span = value / total * fullPie;
37 PieSlice *slice = new PieSlice(QColor(), angle, span, m_parentItem->boundingRect());
38 slice->setParentItem(m_parentItem);
39 m_slices.append(slice);
40 angle += span;
41 }
30
42
31 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
43 setTheme(m_chartTheme);
32 Q_ASSERT(parentItem);
44 resizeSlices(m_chartSize);
33 m_chartSize = parentItem->boundingRect();
34 qreal angle = 0;
35 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
36 foreach (qreal value, m_data) {
37 qreal span = value / total * fullPie;
38 PieSlice *slice = new PieSlice(QColor(), angle, span, parentItem->boundingRect());
39 slice->setParentItem(parentItem);
40 m_slices.append(slice);
41 angle += span;
42 }
45 }
43
46
44 resizeSlices(m_chartSize);
45 return true;
47 return true;
46 }
48 }
47
49
48 void QPieSeries::setSliceColor(int index, QColor color)
50 void QPieSeriesPrivate::setPos(const QPointF & pos)
49 {
51 {
50 if (index >= 0 && index < m_slices.count())
52 // TODO
51 m_slices.at(index)->m_color = color;
52 }
53 }
53
54
54 QColor QPieSeries::sliceColor(int index)
55 void QPieSeriesPrivate::resize(const QSize &size)
55 {
56 {
56 if (index >= 0 && index < m_slices.count())
57 // TODO: allow user setting the size?
57 return m_slices.at(index)->m_color;
58 // TODO: allow user defining the margins?
58 else
59 m_chartSize = QRect(0, 0, size.width(), size.height());
59 return QColor();
60 resizeSlices(m_chartSize);
60 }
61 }
61
62
62 int QPieSeries::sliceCount()
63 void QPieSeriesPrivate::setTheme(ChartTheme *theme)
63 {
64 {
64 return m_slices.count();
65 if (theme) {
66 m_chartTheme = theme;
67 for (int i(0); i < m_slices.count(); i++)
68 m_slices.at(i)->m_theme = theme->themeForSeries();
69 }
65 }
70 }
66
71
67 void QPieSeries::chartSizeChanged(QRectF chartRect)
72 void QPieSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain)
68 {
73 {
69 // TODO: allow user setting the size?
74 // TODO
70 // TODO: allow user defining the margins?
71 m_chartSize = chartRect;
72 resizeSlices(m_chartSize);
73 }
75 }
74
76
75 void QPieSeries::resizeSlices(QRectF rect)
77 void QPieSeriesPrivate::resizeSlices(QRectF rect)
76 {
78 {
77 QRectF tempRect = rect;
79 QRectF tempRect = rect;
78 if (tempRect.width() < tempRect.height()) {
80 if (tempRect.width() < tempRect.height()) {
79 tempRect.setWidth(tempRect.width() * m_sizeFactor);
81 tempRect.setWidth(tempRect.width() * m_sizeFactor);
80 tempRect.setHeight(tempRect.width());
82 tempRect.setHeight(tempRect.width());
81 tempRect.moveCenter(rect.center());
83 tempRect.moveCenter(rect.center());
82 } else {
84 } else {
83 tempRect.setHeight(tempRect.height() * m_sizeFactor);
85 tempRect.setHeight(tempRect.height() * m_sizeFactor);
84 tempRect.setWidth(tempRect.height());
86 tempRect.setWidth(tempRect.height());
85 tempRect.moveCenter(rect.center());
87 tempRect.moveCenter(rect.center());
86 }
88 }
87
89
88 switch (m_position) {
90 switch (m_position) {
89 case PiePositionTopLeft: {
91 case QPieSeries::PiePositionTopLeft: {
90 tempRect.setHeight(tempRect.height() / 2);
92 tempRect.setHeight(tempRect.height() / 2);
91 tempRect.setWidth(tempRect.height());
93 tempRect.setWidth(tempRect.height());
92 tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2));
94 tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2));
93 break;
95 break;
94 }
96 }
95 case PiePositionTopRight: {
97 case QPieSeries::PiePositionTopRight: {
96 tempRect.setHeight(tempRect.height() / 2);
98 tempRect.setHeight(tempRect.height() / 2);
97 tempRect.setWidth(tempRect.height());
99 tempRect.setWidth(tempRect.height());
98 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2));
100 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2));
99 break;
101 break;
100 }
102 }
101 case PiePositionBottomLeft: {
103 case QPieSeries::PiePositionBottomLeft: {
102 tempRect.setHeight(tempRect.height() / 2);
104 tempRect.setHeight(tempRect.height() / 2);
103 tempRect.setWidth(tempRect.height());
105 tempRect.setWidth(tempRect.height());
104 tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3));
106 tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3));
105 break;
107 break;
106 }
108 }
107 case PiePositionBottomRight: {
109 case QPieSeries::PiePositionBottomRight: {
108 tempRect.setHeight(tempRect.height() / 2);
110 tempRect.setHeight(tempRect.height() / 2);
109 tempRect.setWidth(tempRect.height());
111 tempRect.setWidth(tempRect.height());
110 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3));
112 tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3));
111 break;
113 break;
112 }
114 }
113 default:
115 default:
114 break;
116 break;
115 }
117 }
116
118
117 foreach (PieSlice *slice, m_slices)
119 foreach (PieSlice *slice, m_slices)
118 slice->m_rect = tempRect;
120 slice->m_rect = tempRect;
119 }
121 }
120
122
123 QPieSeries::QPieSeries(QGraphicsObject *parent) :
124 QChartSeries(parent),
125 d(new QPieSeriesPrivate())
126 {
127 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
128 if (parentItem)
129 d->m_parentItem = parentItem;
130 }
131
132 QPieSeries::~QPieSeries()
133 {
134 delete d;
135 }
136
137 bool QPieSeries::setData(QList<qreal> data)
138 {
139 return d->setData(data);
140 }
141
142 void QPieSeries::setSliceColor(int index, QColor color)
143 {
144 if (index >= 0 && index < d->m_slices.count())
145 d->m_slices.at(index)->m_color = color;
146 }
147
148 QColor QPieSeries::sliceColor(int index)
149 {
150 if (index >= 0 && index < d->m_slices.count())
151 return d->m_slices.at(index)->m_color;
152 else
153 return QColor();
154 }
155
156 int QPieSeries::sliceCount()
157 {
158 return d->m_slices.count();
159 }
160
121 void QPieSeries::setSizeFactor(qreal factor)
161 void QPieSeries::setSizeFactor(qreal factor)
122 {
162 {
123 if (factor > 0.0)
163 if (factor > 0.0)
124 m_sizeFactor = factor;
164 d->m_sizeFactor = factor;
125 resizeSlices(m_chartSize);
165 d->resizeSlices(d->m_chartSize);
126
166
127 // Initiate update via the parent graphics item
167 // Initiate update via the parent graphics item
128 // TODO: potential issue: what if this function is called from the parent context?
168 // TODO: potential issue: what if this function is called from the parent context?
129 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
169 if (d->m_parentItem)
130 Q_ASSERT(parentItem);
170 d->m_parentItem->update();
131 parentItem->update();
171 }
172
173 qreal QPieSeries::sizeFactor()
174 {
175 return d->m_sizeFactor;
132 }
176 }
133
177
134 void QPieSeries::setPosition(PiePosition position)
178 void QPieSeries::setPosition(PiePosition position)
135 {
179 {
136 m_position = position;
180 d->m_position = position;
137 resizeSlices(m_chartSize);
181 d->resizeSlices(d->m_chartSize);
138
182
139 // Initiate update via the parent graphics item
183 // Initiate update via the parent graphics item
140 // TODO: potential issue: what if this function is called from the parent context?
184 // TODO: potential issue: what if this function is called from the parent context?
141 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
185 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
142 Q_ASSERT(parentItem);
186 Q_ASSERT(parentItem);
143 parentItem->update();
187 parentItem->update();
144 }
188 }
145
189
146 #include "moc_qpieseries.cpp"
190 #include "moc_qpieseries.cpp"
147
191
148 QTCOMMERCIALCHART_END_NAMESPACE
192 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,61 +1,53
1 #ifndef PIESERIES_H
1 #ifndef PIESERIES_H
2 #define PIESERIES_H
2 #define PIESERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QObject>
5 #include <QObject>
6 #include <QRectF>
6 #include <QRectF>
7 #include <QColor>
7 #include <QColor>
8
8
9 class QGraphicsObject;
9 class QGraphicsObject;
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class QPieSeriesPrivate;
11 class PieSlice;
12 class PieSlice;
12
13
13 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
14 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
14 {
15 {
15 Q_OBJECT
16 Q_OBJECT
16
17
17 public:
18 public:
18 enum PiePosition {
19 enum PiePosition {
19 PiePositionMaximized = 0,
20 PiePositionMaximized = 0,
20 PiePositionTopLeft,
21 PiePositionTopLeft,
21 PiePositionTopRight,
22 PiePositionTopRight,
22 PiePositionBottomLeft,
23 PiePositionBottomLeft,
23 PiePositionBottomRight
24 PiePositionBottomRight
24 };
25 };
25
26
26 public:
27 public:
27 // TODO: use a generic data class instead of x and y
28 // TODO: use a generic data class instead of x and y
28 QPieSeries(QGraphicsObject *parent = 0);
29 QPieSeries(QGraphicsObject *parent = 0);
29 ~QPieSeries();
30 ~QPieSeries();
30
31
31 public: // from QChartSeries
32 public: // from QChartSeries
32 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
33 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
33 bool setData(QList<qreal> data);
34 bool setData(QList<qreal> data);
34
35
35 public:
36 public:
36 void setSliceColor(int index, QColor color);
37 void setSliceColor(int index, QColor color);
37 QColor sliceColor(int index);
38 QColor sliceColor(int index);
38 int sliceCount();
39 int sliceCount();
39 void setSizeFactor(qreal sizeFactor);
40 void setSizeFactor(qreal sizeFactor);
40 qreal sizeFactor() { return m_sizeFactor; }
41 qreal sizeFactor();
41 void setPosition(PiePosition position);
42 void setPosition(PiePosition position);
42
43
43 public Q_SLOTS:
44 void chartSizeChanged(QRectF rect);
45
46 private:
44 private:
47 void resizeSlices(QRectF rect);
45 Q_DECLARE_PRIVATE(QPieSeries)
48 //Q_DECLARE_PRIVATE(QPieSeries)
49 Q_DISABLE_COPY(QPieSeries)
46 Q_DISABLE_COPY(QPieSeries)
50 friend class QChart;
47 friend class QChart;
51 // TODO: move the followin to internal impl
48 QPieSeriesPrivate *const d;
52 QList<qreal> m_data;
53 QList<PieSlice*> m_slices;
54 QRectF m_chartSize;
55 qreal m_sizeFactor;
56 PiePosition m_position;
57 };
49 };
58
50
59 QTCOMMERCIALCHART_END_NAMESPACE
51 QTCOMMERCIALCHART_END_NAMESPACE
60
52
61 #endif // PIESERIES_H
53 #endif // PIESERIES_H
@@ -1,108 +1,125
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 QGraphicsItem(parent),
13 QGraphicsItem(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 // TODO:
33 //void QScatterSeriesPrivate::setAxisScale(qreal xscale, qreal yscale)
33 //void QScatterSeriesPrivate::setAxisScale(qreal xscale, qreal yscale)
34
34
35 QRectF QScatterSeriesPrivate::boundingRect() const
35 QRectF QScatterSeriesPrivate::boundingRect() const
36 {
36 {
37 return QRectF(0, 0, 55, 100);
37 return QRectF(0, 0, 55, 100);
38 }
38 }
39
39
40 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
40 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
41 {
41 {
42 QPen pen = painter->pen();
42 // TODO: The opacity should be user definable?
43 QBrush brush = pen.brush();
44 // TODO: The opacity should be user definable...
45 //brush.setColor(QColor(255, 82, 0, 100));
43 //brush.setColor(QColor(255, 82, 0, 100));
46 brush.setColor(m_markerColor);
44 if (m_markerColor.isValid()) {
47 pen.setBrush(brush);
45 QPen pen = painter->pen();
48 pen.setWidth(4);
46 QBrush brush = pen.brush();
49 painter->setPen(pen);
47 brush.setColor(m_markerColor);
48 pen.setBrush(brush);
49 pen.setWidth(4);
50 painter->setPen(pen);
51 }
52 else
53 painter->setPen(m_theme.markerPen);
54 // brush.setColor(m_theme..lineColor);
50
55
51 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
56 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
52 // event right after construction or maybe given a size during initialization
57 // event right after construction or maybe given a size during initialization
53 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
58 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
54 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
59 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
55 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
60 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
56 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
61 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
57 }
62 }
58 }
63 }
59
64
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
60 QScatterSeries::QScatterSeries(QObject *parent) :
85 QScatterSeries::QScatterSeries(QObject *parent) :
61 QChartSeries(parent),
86 QChartSeries(parent),
62 d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent)))
87 d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent)))
63 {
88 {
64 }
89 }
65
90
66 bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y)
91 bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y)
67 {
92 {
68 // TODO: validate data
93 // TODO: validate data
69 d->m_x = x;
94 d->m_x = x;
70 d->m_y = y;
95 d->m_y = y;
71 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
96 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
72 Q_ASSERT(parentItem);
97 Q_ASSERT(parentItem);
73 d->resize(parentItem->boundingRect());
98 d->resize(parentItem->boundingRect());
74 return true;
99 return true;
75 }
100 }
76
101
77 void QScatterSeries::chartSizeChanged(QRectF rect)
78 {
79 // Recalculate scatter data point locations on the scene
80 // d->transform().reset();
81 // d->transform().translate();
82 d->resize(rect);
83 }
84
85 void QScatterSeries::setMarkerColor(QColor color)
102 void QScatterSeries::setMarkerColor(QColor color)
86 {
103 {
87 d->m_markerColor = color;
104 d->m_markerColor = color;
88 }
105 }
89
106
90 QColor QScatterSeries::markerColor()
107 QColor QScatterSeries::markerColor()
91 {
108 {
92 return d->m_markerColor;
109 return d->m_markerColor;
93 }
110 }
94
111
95 // TODO:
112 // TODO:
96 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
113 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
97 //{
114 //{
98 // d->rescale(xscale, yscale);
115 // d->rescale(xscale, yscale);
99 //}
116 //}
100
117
101 QScatterSeries::~QScatterSeries()
118 QScatterSeries::~QScatterSeries()
102 {
119 {
103 delete d;
120 delete d;
104 }
121 }
105
122
106 #include "moc_qscatterseries.cpp"
123 #include "moc_qscatterseries.cpp"
107
124
108 QTCOMMERCIALCHART_END_NAMESPACE
125 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,40 +1,39
1 #ifndef QSCATTERSERIES_H
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QRectF>
5 #include <QRectF>
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 class QScatterSeriesPrivate;
9 class QScatterSeriesPrivate;
10
10
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14 public:
14 public:
15 //QScatterSeries(QSeriesData *data, QObject *chart);
15 //QScatterSeries(QSeriesData *data, QObject *chart);
16 QScatterSeries(QObject *parent = 0);
16 QScatterSeries(QObject *parent = 0);
17 ~QScatterSeries();
17 ~QScatterSeries();
18
18
19 public: // from QChartSeries
19 public: // from QChartSeries
20 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
20 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
21 bool setData(QList<qreal> x, QList<qreal> y);
21 bool setData(QList<qreal> x, QList<qreal> y);
22
22
23 public Q_SLOTS:
23 public Q_SLOTS:
24 void chartSizeChanged(QRectF rect);
25 // TODO: also affects opacity of the marker...? To be documented
24 // TODO: also affects opacity of the marker...? To be documented
26 void setMarkerColor(QColor color);
25 void setMarkerColor(QColor color);
27 QColor markerColor();
26 QColor markerColor();
28 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
27 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
29 //void setMarkerShape(QChartSeries::MarkerShape/QScatterSeries::MarkerShape shape);
28 //void setMarkerShape(QChartSeries::MarkerShape/QScatterSeries::MarkerShape shape);
30
29
31 private:
30 private:
32 Q_DECLARE_PRIVATE(QScatterSeries)
31 Q_DECLARE_PRIVATE(QScatterSeries)
33 Q_DISABLE_COPY(QScatterSeries)
32 Q_DISABLE_COPY(QScatterSeries)
34 friend class QChart;
33 friend class QChart;
35 QScatterSeriesPrivate *const d;
34 QScatterSeriesPrivate *const d;
36 };
35 };
37
36
38 QTCOMMERCIALCHART_END_NAMESPACE
37 QTCOMMERCIALCHART_END_NAMESPACE
39
38
40 #endif // QSCATTERSERIES_H
39 #endif // QSCATTERSERIES_H
@@ -1,35 +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"
6 #include "chartitemcontrol.h"
5 #include <QGraphicsItem>
7 #include <QGraphicsItem>
6
8
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
10
9 /*!
11 /*!
10 * The PIMPL class of QScatterSeries.
12 * The PIMPL class of QScatterSeries.
11 */
13 */
12 class QScatterSeriesPrivate : public QGraphicsItem
14 class QScatterSeriesPrivate : public QGraphicsItem, public ChartItemControl
13 {
15 {
14 public:
16 public:
15 QScatterSeriesPrivate(QGraphicsItem *parent);
17 QScatterSeriesPrivate(QGraphicsItem *parent);
16
18
17 public: // from QGraphicsItem
19 public: // from QGraphicsItem
20 void setPos(const QPointF & pos);
18 void resize(QRectF rect);
21 void resize(QRectF rect);
19 QRectF boundingRect() const;
22 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
24
25 public: // from ChartItemControl
26 void resize(const QSize &size);
27 void setTheme(ChartTheme *theme);
28 void setPlotDomain(const PlotDomain& data);
29
22 public:
30 public:
23 // 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?
24 QList<qreal> m_x;
32 QList<qreal> m_x;
25 QList<qreal> m_y;
33 QList<qreal> m_y;
26 qreal m_scalex;
34 qreal m_scalex;
27 qreal m_scaley;
35 qreal m_scaley;
28 QList<qreal> m_scenex;
36 QList<qreal> m_scenex;
29 QList<qreal> m_sceney;
37 QList<qreal> m_sceney;
30 QColor m_markerColor;
38 QColor m_markerColor;
39 SeriesTheme m_theme;
31 };
40 };
32
41
33 QTCOMMERCIALCHART_END_NAMESPACE
42 QTCOMMERCIALCHART_END_NAMESPACE
34
43
35 #endif // QSCATTERSERIES_H
44 #endif // QSCATTERSERIESPRIVATE_H
@@ -1,99 +1,110
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
35
35 PRIVATE_HEADERS += \
36 PRIVATE_HEADERS += \
36 xylinechart/xylinechartitem_p.h \
37 xylinechart/xylinechartitem_p.h \
37 plotdomain_p.h \
38 plotdomain_p.h \
38 qscatterseries_p.h \
39 qscatterseries_p.h \
40 qpieseries_p.h \
39 pieslice.h \
41 pieslice.h \
40 axisitem_p.h \
42 axisitem_p.h \
41 chartitem_p.h
43 charttheme_p.h
42
44
43 PUBLIC_HEADERS += \
45 PUBLIC_HEADERS += \
44 qchartseries.h \
46 qchartseries.h \
45 qscatterseries.h \
47 qscatterseries.h \
46 qpieseries.h \
48 qpieseries.h \
47 qchart.h \
49 qchart.h \
48 qchartwidget.h \
50 qchartwidget.h \
49 qchartglobal.h \
51 qchartglobal.h \
50 xylinechart/qxychartseries.h \
52 xylinechart/qxychartseries.h \
51 barchart/barchartseries.h \
53 barchart/barchartseries.h \
52 barchart/bargroup.h \
54 barchart/bargroup.h \
53 barchart/stackedbarchartseries.h \
55 barchart/stackedbarchartseries.h \
54 barchart/stackedbargroup.h \
56 barchart/stackedbargroup.h \
55 barchart/percentbarchartseries.h \
57 barchart/percentbarchartseries.h \
56 barchart/percentbargroup.h \
58 barchart/percentbargroup.h \
57 qchartview.h \
59 qchartview.h \
58 qchartaxis.h
60 qchartaxis.h
59
61
60 HEADERS += $$PUBLIC_HEADERS
62 HEADERS += $$PUBLIC_HEADERS \
63 chartitemcontrol.h
61 HEADERS += $$PRIVATE_HEADERS
64 HEADERS += $$PRIVATE_HEADERS
62
65
63 INCLUDEPATH += xylinechart \
66 INCLUDEPATH += xylinechart \
64 barchart \
67 barchart \
65 .
68 .
66
69
67 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
70 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
68 MOC_DIR = $$CHART_BUILD_DIR/lib
71 MOC_DIR = $$CHART_BUILD_DIR/lib
69 UI_DIR = $$CHART_BUILD_DIR/lib
72 UI_DIR = $$CHART_BUILD_DIR/lib
70 RCC_DIR = $$CHART_BUILD_DIR/lib
73 RCC_DIR = $$CHART_BUILD_DIR/lib
71
74
72
75
73 DEFINES += QTCOMMERCIALCHART_LIBRARY
76 DEFINES += QTCOMMERCIALCHART_LIBRARY
74
77
75 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
78 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
76 public_headers.files = $$PUBLIC_HEADERS
79 public_headers.files = $$PUBLIC_HEADERS
77 target.path = $$[QT_INSTALL_LIBS]
80 target.path = $$[QT_INSTALL_LIBS]
78 INSTALLS += target \
81 INSTALLS += target \
79 public_headers
82 public_headers
80
83
81
84
82 install_build_headers.name = bild_headers
85 install_build_headers.name = bild_headers
83 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
86 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
84 install_build_headers.input = PUBLIC_HEADERS
87 install_build_headers.input = PUBLIC_HEADERS
85 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
88 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
86 install_build_headers.CONFIG += target_predeps no_link
89 install_build_headers.CONFIG += target_predeps no_link
87 QMAKE_EXTRA_COMPILERS += install_build_headers
90 QMAKE_EXTRA_COMPILERS += install_build_headers
88
91
89 chartversion.target = qchartversion_p.h
92 chartversion.target = qchartversion_p.h
90 chartversion.commands = @echo "build_time" > $$chartversion.target;
93 chartversion.commands = @echo "build_time" > $$chartversion.target;
91 chartversion.depends = $$HEADERS $$SOURCES
94 chartversion.depends = $$HEADERS $$SOURCES
92 PRE_TARGETDEPS += qchartversion_p.h
95 PRE_TARGETDEPS += qchartversion_p.h
93 QMAKE_CLEAN+= qchartversion_p.h
96 QMAKE_CLEAN+= qchartversion_p.h
94 QMAKE_EXTRA_TARGETS += chartversion
97 QMAKE_EXTRA_TARGETS += chartversion
95
98
96 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
99 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
97 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
100 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
98
101
99
102
103
104
105
106
107
108
109
110
@@ -1,67 +1,81
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):ChartItem(parent),
10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent) :
11 m_series(series),
11 QGraphicsItem(parent),
12 m_pathItem(new QGraphicsPathItem(this))
12 m_series(series),
13 m_pathItem(new QGraphicsPathItem(this))
13 {
14 {
14 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
15 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
15 }
16 }
16
17
17 void XYLineChartItem::setSize(const QSize& size)
18 void XYLineChartItem::setPos(const QPointF & pos)
18 {
19 {
19 m_rect=QRect(0,0,size.width(),size.height());
20 QGraphicsItem::setPos(pos);
21 }
22
23 void XYLineChartItem::resize(const QSize &size)
24 {
25 m_rect = QRect(0, 0, size.width(), size.height());
20 prepareGeometryChange();
26 prepareGeometryChange();
21 updateGeometry();
27 updateGeometry();
28 }
22
29
30 void XYLineChartItem::setTheme(ChartTheme *theme)
31 {
32 if (theme) {
33 m_theme = theme->themeForSeries();
34 prepareGeometryChange();
35 updateGeometry();
36 }
23 }
37 }
24
38
25 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
39 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
26 {
40 {
27 m_plotDomain=data;
41 m_plotDomain=data;
28 prepareGeometryChange();
42 prepareGeometryChange();
29 updateGeometry();
43 updateGeometry();
30
44
31 }
45 }
32
46
33 QRectF XYLineChartItem::boundingRect() const
47 QRectF XYLineChartItem::boundingRect() const
34 {
48 {
35 return m_rect;
49 return m_rect;
36 }
50 }
37 /*
51 /*
38 QPainterPath XYLineChartItem::shape() const
52 QPainterPath XYLineChartItem::shape() const
39 {
53 {
40 return m_pathItem->shape();
54 return m_pathItem->shape();
41 }
55 }
42 */
56 */
43 void XYLineChartItem::updateGeometry()
57 void XYLineChartItem::updateGeometry()
44 {
58 {
45 if (!m_rect.isValid()) return;
59 if (!m_rect.isValid()) return;
46
60
47 const qreal deltaX = m_rect.width()/m_plotDomain.spanX();
61 const qreal deltaX = m_rect.width()/m_plotDomain.spanX();
48 const qreal deltaY = m_rect.height()/m_plotDomain.spanY();
62 const qreal deltaY = m_rect.height()/m_plotDomain.spanY();
49
63
50 QPainterPath path;
64 QPainterPath path;
51
65
52 for (int j = 0; j < m_series->count(); ++j) {
66 for (int j = 0; j < m_series->count(); ++j) {
53 qreal dx = m_series->x(j) - m_plotDomain.m_minX;
67 qreal dx = m_series->x(j) - m_plotDomain.m_minX;
54 qreal dy = m_series->y(j) - m_plotDomain.m_minY;
68 qreal dy = m_series->y(j) - m_plotDomain.m_minY;
55 qreal x = (dx * deltaX) + m_rect.left();
69 qreal x = (dx * deltaX) + m_rect.left();
56 qreal y = - (dy * deltaY) + m_rect.bottom();
70 qreal y = - (dy * deltaY) + m_rect.bottom();
57 if(j==0) path.moveTo(x,y);
71 if(j==0) path.moveTo(x,y);
58 else path.lineTo(x,y);
72 else path.lineTo(x,y);
59 }
73 }
60
74
61 m_pathItem->setPath(path);
75 m_pathItem->setPath(path);
62 m_pathItem->setPen(m_series->pen());
76 m_pathItem->setPen(m_theme.linePen);
63 m_pathItem->setBrush(Qt::NoBrush);
77 m_pathItem->setBrush(Qt::NoBrush);
64 }
78 }
65
79
66
80
67 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,39 +1,45
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_p.h"
5 #include "chartitemcontrol.h"
6 #include "charttheme_p.h"
7 #include <QGraphicsItem>
6
8
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
10
9 class QXYChartSeries;
11 class QXYChartSeries;
10
12
11 class XYLineChartItem : public ChartItem
13 class XYLineChartItem : public QGraphicsItem, public ChartItemControl
12 {
14 {
13
15
14 public:
16 public:
15 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
17 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
16 ~ XYLineChartItem(){};
18 ~ XYLineChartItem(){};
17
19
18 //from QGraphicsItem
20 //from QGraphicsItem
19 QRectF boundingRect() const;
21 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21 // virtual QPainterPath shape() const;
23 // virtual QPainterPath shape() const;
22 //from ChartItem
24
23 void setSize(const QSize& size);
25 public: // from ChartItemControl
26 void setPos(const QPointF & pos);
27 void resize(const QSize &size);
28 void setTheme(ChartTheme *theme);
24 void setPlotDomain(const PlotDomain& data);
29 void setPlotDomain(const PlotDomain& data);
25
30
26 private:
31 private:
27 void updateGeometry();
32 void updateGeometry();
28
33
29 private:
34 private:
30 QRect m_rect;
35 QRect m_rect;
31 QPolygonF m_polyline;
36 QPolygonF m_polyline;
32 QXYChartSeries* m_series;
37 QXYChartSeries* m_series;
33 PlotDomain m_plotDomain;
38 PlotDomain m_plotDomain;
34 QGraphicsPathItem *m_pathItem;
39 QGraphicsPathItem *m_pathItem;
40 SeriesTheme m_theme;
35 };
41 };
36
42
37 QTCOMMERCIALCHART_END_NAMESPACE
43 QTCOMMERCIALCHART_END_NAMESPACE
38
44
39 #endif
45 #endif
@@ -1,2 +1,5
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += chartwidgettest
2 SUBDIRS += chartwidgettest
3
4
5
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now