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