##// END OF EJS Templates
Changes QChartAxis -> QAxis
Michal Klocek -
r1006:a8ef6d6db8bf
parent child
Show More
@@ -1,65 +1,65
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chart.h"
22 #include <QChartAxis>
22 #include <QAxis>
23 23 #include <QSplineSeries>
24 24 #include <QTime>
25 25
26 26 Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 27 :QChart(parent, wFlags),
28 28 m_step(1),
29 29 m_x(0),
30 30 m_y(1)
31 31 {
32 32 qsrand((uint) QTime::currentTime().msec());
33 33
34 34 QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
35 35 m_timer.setInterval(1000);
36 36
37 37 m_series = new QSplineSeries(this);
38 38 QPen green(Qt::red);
39 39 green.setWidth(3);
40 40 m_series->setPen(green);
41 41 m_series->append(m_x, m_y);
42 42
43 43 addSeries(m_series);
44 44
45 45 axisY()->setRange(-5, 5);
46 46 axisX()->setRange(-9, 1);
47 47 axisX()->setTicksCount(11);
48 48
49 49 m_timer.start();
50 50 }
51 51
52 52 Chart::~Chart()
53 53 {
54 54
55 55 }
56 56
57 57 void Chart::handleTimeout()
58 58 {
59 59 m_x += m_step;
60 60 m_y = qrand() % 5 - 2.5;
61 61 m_series->append(m_x, m_y);
62 62 if (m_x >= 10)
63 63 m_series->remove(m_x - 10);
64 64 scrollRight();
65 65 }
@@ -1,121 +1,121
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QLineSeries>
25 25
26 26 QTCOMMERCIALCHART_USE_NAMESPACE
27 27
28 28 int main(int argc, char *argv[])
29 29 {
30 30 QApplication a(argc, argv);
31 31
32 32 //![1]
33 33 QLineSeries* series = new QLineSeries();
34 34 *series << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10,5);
35 35 QChart* chart = new QChart();
36 36 chart->addSeries(series);
37 37 //![1]
38 38
39 39 //![2]
40 40 // Customize series
41 41 QPen pen(QRgb(0xfdb157));
42 42 pen.setWidth(5);
43 43 series->setPen(pen);
44 44
45 45 // Customize chart title
46 46 QFont font;
47 47 font.setPixelSize(18);
48 48 chart->setTitleFont(font);
49 49 chart->setTitleBrush(QBrush(Qt::white));
50 50 chart->setTitle("Customchart example");
51 51
52 52 // Customize chart background
53 53 QLinearGradient backgroundGradient;
54 54 backgroundGradient.setStart(QPointF(0,0));
55 55 backgroundGradient.setFinalStop(QPointF(0,1));
56 56 backgroundGradient.setColorAt(0.0, QRgb(0xd2d0d1));
57 57 backgroundGradient.setColorAt(1.0, QRgb(0x4c4547));
58 58 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
59 59 chart->setBackgroundBrush(backgroundGradient);
60 60 //![2]
61 61
62 62 //![3]
63 QChartAxis* axisX = chart->axisX();
64 QChartAxis* axisY = chart->axisY();
63 QAxis* axisX = chart->axisX();
64 QAxis* axisY = chart->axisY();
65 65
66 66 // Customize axis label font
67 67 QFont labelsFont;
68 68 labelsFont.setPixelSize(12);
69 69 axisX->setLabelsFont(labelsFont);
70 70 axisY->setLabelsFont(labelsFont);
71 71
72 72 // Customize axis colors
73 73 QPen axisPen(QRgb(0xd18952));
74 74 axisPen.setWidth(2);
75 75 axisX->setAxisPen(axisPen);
76 76 axisY->setAxisPen(axisPen);
77 77
78 78 // Customize axis label colors
79 79 QBrush axisBrush(Qt::white);
80 80 axisX->setLabelsBrush(axisBrush);
81 81 axisY->setLabelsBrush(axisBrush);
82 82
83 83 // Customize grid lines and shades
84 84 axisX->setGridLineVisible(false);
85 85 axisY->setGridLineVisible(false);
86 86 axisY->setShadesPen(Qt::NoPen);
87 87 axisY->setShadesBrush(QBrush(QRgb(0xa5a2a3)));
88 88 axisY->setShadesVisible(true);
89 89 //![3]
90 90
91 91 //![4]
92 92 QChartAxisCategories* categoriesX = chart->axisX()->categories();
93 93 categoriesX->insert(1,"low");
94 94 categoriesX->insert(5,"optimal");
95 95 categoriesX->insert(10,"high");
96 96
97 97 QChartAxisCategories* categoriesY = chart->axisY()->categories();
98 98 categoriesY->insert(1,"slow");
99 99 categoriesY->insert(5,"med");
100 100 categoriesY->insert(10,"fast");
101 101
102 102 axisX->setRange(0,10);
103 103 axisX->setTicksCount(4);
104 104 axisY->setRange(0,10);
105 105 axisY->setTicksCount(4);
106 106 //![4]
107 107
108 108 //![5]
109 109 QChartView* chartView = new QChartView(chart);
110 110 chartView->setRenderHint(QPainter::Antialiasing);
111 111 //![5]
112 112
113 113 //![6]
114 114 QMainWindow window;
115 115 window.setCentralWidget(chartView);
116 116 window.resize(400, 300);
117 117 window.show();
118 118 //![6]
119 119
120 120 return a.exec();
121 121 }
@@ -1,51 +1,51
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "drilldownchart.h"
22 #include <QChartAxis>
22 #include <QAxis>
23 23
24 24 QTCOMMERCIALCHART_USE_NAMESPACE
25 25
26 26 DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 27 : QChart(parent, wFlags)
28 28 ,m_currentSeries(0)
29 29 {
30 30 }
31 31
32 32 void DrilldownChart::changeSeries(QAbstractSeries *series)
33 33 {
34 34 if (m_currentSeries) {
35 35 removeSeries(m_currentSeries);
36 36 }
37 37 m_currentSeries = series;
38 38 addSeries(series);
39 39 setTitle(series->name());
40 40 axisY()->setNiceNumbersEnabled(true);
41 41 }
42 42
43 43 void DrilldownChart::handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button)
44 44 {
45 45 Q_UNUSED(barset)
46 46 Q_UNUSED(button)
47 47 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
48 48 changeSeries(series->drilldownSeries(category));
49 49 }
50 50
51 51 #include "moc_drilldownchart.cpp"
@@ -1,65 +1,65
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "axisanimation_p.h"
22 22 #include <QTimer>
23 23
24 24 Q_DECLARE_METATYPE(QVector<qreal>)
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28
29 AxisAnimation::AxisAnimation(Axis *axis): ChartAnimation(axis),
29 AxisAnimation::AxisAnimation(ChartAxis *axis): ChartAnimation(axis),
30 30 m_axis(axis)
31 31 {
32 32 }
33 33
34 34 AxisAnimation::~AxisAnimation()
35 35 {
36 36 }
37 37
38 38 QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
39 39 {
40 40 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
41 41 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
42 42 QVector<qreal> result;
43 43
44 44 Q_ASSERT(startVector.count() == endVecotr.count()) ;
45 45
46 46 for(int i = 0; i < startVector.count(); i++){
47 47 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
48 48 result << value;
49 49 }
50 50 return qVariantFromValue(result);
51 51 }
52 52
53 53
54 54 void AxisAnimation::updateCurrentValue (const QVariant &value )
55 55 {
56 56 if (state() != QAbstractAnimation::Stopped)//workaround
57 57 {
58 58 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
59 59 Q_ASSERT(vector.count() != 0);
60 60 m_axis->setLayout(vector);
61 61 }
62 62
63 63 }
64 64
65 65 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,46
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef AXISANIMATION_H
22 22 #define AXISANIMATION_H
23 23
24 #include "axis_p.h"
24 #include "chartaxis_p.h"
25 25 #include "chartanimation_p.h"
26 26
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 class AxisAnimation: public ChartAnimation
31 31 {
32 32 public:
33 AxisAnimation(Axis *axis);
33 AxisAnimation(ChartAxis *axis);
34 34 ~AxisAnimation();
35 35 protected:
36 36 QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress ) const;
37 37 void updateCurrentValue(const QVariant &value );
38 38 private:
39 Axis *m_axis;
39 ChartAxis *m_axis;
40 40 };
41 41
42 42 QTCOMMERCIALCHART_END_NAMESPACE
43 43
44 44
45 45
46 46 #endif /* AXISITEM_H_ */
@@ -1,301 +1,301
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartanimator_p.h"
22 22 #include "axisanimation_p.h"
23 23 #include "xyanimation_p.h"
24 24 #include "splineanimation_p.h"
25 25 #include "xychartitem_p.h"
26 26 #include "pieanimation_p.h"
27 27 #include "baranimation_p.h"
28 28 #include "barchartitem_p.h"
29 29 #include "areachartitem_p.h"
30 30 #include "splinechartitem_p.h"
31 31 #include "scatterchartitem_p.h"
32 32 #include <QTimer>
33 33
34 34 Q_DECLARE_METATYPE(QVector<QPointF>)
35 35 Q_DECLARE_METATYPE(QVector<qreal>)
36 36 Q_DECLARE_METATYPE(QVector<QRectF>)
37 37
38 38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 39
40 40 const static int duration = 1000;
41 41
42 42 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent),
43 43 m_state(ShowState)
44 44 {
45 45 }
46 46
47 47 ChartAnimator::~ChartAnimator()
48 48 {
49 49 }
50 50
51 void ChartAnimator::addAnimation(Axis *item)
51 void ChartAnimator::addAnimation(ChartAxis *item)
52 52 {
53 53 ChartAnimation *animation = m_animations.value(item);
54 54
55 55 if (!animation) {
56 56 animation = new AxisAnimation(item);
57 57 m_animations.insert(item, animation);
58 58 }
59 59
60 60 item->setAnimator(this);
61 61 }
62 62
63 63 void ChartAnimator::addAnimation(SplineChartItem *item)
64 64 {
65 65 ChartAnimation *animation = m_animations.value(item);
66 66
67 67 if (!animation) {
68 68 animation = new SplineAnimation(item);
69 69 m_animations.insert(item, animation);
70 70 }
71 71
72 72 item->setAnimator(this);
73 73 }
74 74
75 75 void ChartAnimator::addAnimation(ScatterChartItem *item)
76 76 {
77 77 ChartAnimation *animation = m_animations.value(item);
78 78
79 79 if (!animation) {
80 80 animation = new XYAnimation(item);
81 81 m_animations.insert(item, animation);
82 82 }
83 83
84 84 item->setAnimator(this);
85 85 }
86 86
87 87 void ChartAnimator::addAnimation(LineChartItem *item)
88 88 {
89 89 ChartAnimation *animation = m_animations.value(item);
90 90
91 91 if (!animation) {
92 92 animation = new XYAnimation(item);
93 93 m_animations.insert(item, animation);
94 94 }
95 95
96 96 item->setAnimator(this);
97 97 }
98 98
99 99 void ChartAnimator::addAnimation(PieChartItem *item)
100 100 {
101 101 ChartAnimation *animation = m_animations.value(item);
102 102
103 103 if (!animation) {
104 104 animation = new PieAnimation(item);
105 105 m_animations.insert(item, animation);
106 106 }
107 107
108 108 item->setAnimator(this);
109 109 }
110 110
111 111 void ChartAnimator::addAnimation(BarChartItem *item)
112 112 {
113 113 ChartAnimation *animation = m_animations.value(item);
114 114
115 115 if (!animation) {
116 116 animation = new BarAnimation(item);
117 117 m_animations.insert(item, animation);
118 118 }
119 119
120 120 item->setAnimator(this);
121 121 }
122 122
123 123
124 124 void ChartAnimator::removeAnimation(Chart *item)
125 125 {
126 126 item->setAnimator(0);
127 127 m_animations.remove(item);
128 128 }
129 129
130 void ChartAnimator::updateLayout(Axis *item , QVector<qreal> &newLayout)
130 void ChartAnimator::updateLayout(ChartAxis *item , QVector<qreal> &newLayout)
131 131 {
132 132 AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item));
133 133
134 134 Q_ASSERT(animation);
135 135
136 136 QVector<qreal> oldLayout = item->layout();
137 137
138 138 if (newLayout.count() == 0)
139 139 return;
140 140
141 141 switch (m_state) {
142 142 case ZoomOutState: {
143 143 QRectF rect = item->geometry();
144 144 oldLayout.resize(newLayout.count());
145 145
146 146 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
147 oldLayout[i] = item->axisType() == Axis::X_AXIS ? rect.left() : rect.bottom();
148 oldLayout[j] = item->axisType() == Axis::X_AXIS ? rect.right() : rect.top();
147 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
148 oldLayout[j] = item->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
149 149 }
150 150 }
151 151 break;
152 152 case ZoomInState: {
153 int index = qMin(oldLayout.count() * (item->axisType() == Axis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0);
153 int index = qMin(oldLayout.count() * (item->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0);
154 154 oldLayout.resize(newLayout.count());
155 155
156 156 for(int i = 0; i < oldLayout.count(); i++)
157 157 oldLayout[i]= oldLayout[index];
158 158 }
159 159 break;
160 160 case ScrollDownState:
161 161 case ScrollRightState: {
162 162 oldLayout.resize(newLayout.count());
163 163
164 164 for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
165 165 oldLayout[i]= oldLayout[j];
166 166 }
167 167 break;
168 168 case ScrollUpState:
169 169 case ScrollLeftState: {
170 170 oldLayout.resize(newLayout.count());
171 171
172 172 for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
173 173 oldLayout[i]= oldLayout[j];
174 174 }
175 175 break;
176 176 default: {
177 177 oldLayout.resize(newLayout.count());
178 178 QRectF rect = item->geometry();
179 179 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
180 oldLayout[i] = item->axisType() == Axis::X_AXIS ? rect.left() : rect.top();
180 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
181 181 }
182 182 break;
183 183 }
184 184
185 185
186 186 if (animation->state() != QAbstractAnimation::Stopped)
187 187 animation->stop();
188 188
189 189 animation->setDuration(duration);
190 190 animation->setEasingCurve(QEasingCurve::OutQuart);
191 191 QVariantAnimation::KeyValues value;
192 192 animation->setKeyValues(value); //workaround for wrong interpolation call
193 193 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
194 194 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
195 195
196 196 QTimer::singleShot(0, animation, SLOT(start()));
197 197 }
198 198
199 199 void ChartAnimator::updateLayout(SplineChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index)
200 200 {
201 201 SplineAnimation *animation = static_cast<SplineAnimation *>(m_animations.value(item));
202 202
203 203 Q_ASSERT(animation);
204 204
205 205 if (newPoints.count() < 2 || newControlPoints.count() < 2)
206 206 return;
207 207
208 208 bool empty = oldPoints.count() == 0;
209 209
210 210
211 211 if (animation->state() != QAbstractAnimation::Stopped)
212 212 animation->stop();
213 213
214 214 animation->setDuration(duration);
215 215 if (!empty)
216 216 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
217 217 else
218 218 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
219 219
220 220 animation->setEasingCurve(QEasingCurve::OutQuart);
221 221 animation->setValues(oldPoints, newPoints, oldControlPoints, newControlPoints, index);
222 222
223 223 QTimer::singleShot(0, animation, SLOT(start()));
224 224 }
225 225
226 226
227 227 void ChartAnimator::updateLayout(XYChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
228 228 {
229 229 XYAnimation *animation = static_cast<XYAnimation *>(m_animations.value(item));
230 230
231 231 Q_ASSERT(animation);
232 232
233 233 if (newPoints.count() == 0)
234 234 return;
235 235
236 236 bool empty = oldPoints.count() == 0;
237 237
238 238
239 239 if (animation->state() != QAbstractAnimation::Stopped)
240 240 animation->stop();
241 241
242 242 animation->setDuration(duration);
243 243 if (!empty)
244 244 animation->setAnimationType(ChartAnimation::MoveDownAnimation);
245 245 else
246 246 animation->setAnimationType(ChartAnimation::LineDrawAnimation);
247 247
248 248 animation->setEasingCurve(QEasingCurve::OutQuart);
249 249 animation->setValues(oldPoints, newPoints, index);
250 250
251 251 QTimer::singleShot(0, animation, SLOT(start()));
252 252 }
253 253
254 254 void ChartAnimator::addAnimation(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty)
255 255 {
256 256 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
257 257 Q_ASSERT(animation);
258 258 animation->addSlice(slice, sliceData, isEmpty);
259 259 }
260 260
261 261 void ChartAnimator::removeAnimation(PieChartItem *item, QPieSlice *slice)
262 262 {
263 263 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
264 264 Q_ASSERT(animation);
265 265 animation->removeSlice(slice);
266 266 }
267 267
268 268 void ChartAnimator::updateLayout(PieChartItem *item, const PieLayout &layout)
269 269 {
270 270 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
271 271 Q_ASSERT(animation);
272 272 animation->updateValues(layout);
273 273 }
274 274
275 275 void ChartAnimator::updateLayout(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData)
276 276 {
277 277 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
278 278 Q_ASSERT(animation);
279 279 animation->updateValue(slice, sliceData);
280 280 }
281 281
282 282 void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
283 283 {
284 284 BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item));
285 285 Q_ASSERT(animation);
286 286 animation->setDuration(duration);
287 287 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
288 288 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
289 289 QTimer::singleShot(0, animation, SLOT(start()));
290 290 }
291 291
292 292
293 293 void ChartAnimator::setState(State state, const QPointF &point)
294 294 {
295 295 m_state = state;
296 296 m_point = point;
297 297 }
298 298
299 299 #include "moc_chartanimator_p.cpp"
300 300
301 301 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,79
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef CHARTANIMATOR_P_H
22 22 #define CHARTANIMATOR_P_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "chartanimation_p.h"
26 26 #include "piechartitem_p.h"
27 27 #include "barchartitem_p.h"
28 28 #include <QPointF>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 class ChartItem;
33 class Axis;
33 class ChartAxis;
34 34 class AreaChartItem;
35 35 class SplineChartItem;
36 36 class ScatterChartItem;
37 37 class LineChartItem;
38 38 class XYChartItem;
39 39
40 40 class ChartAnimator : public QObject
41 41 {
42 42 Q_OBJECT
43 43 public:
44 44 enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState, ScrollRightState, ZoomInState, ZoomOutState};
45 45
46 46 ChartAnimator(QObject *parent = 0);
47 47 virtual ~ChartAnimator();
48 48
49 void addAnimation(Axis *item);
49 void addAnimation(ChartAxis *item);
50 50 void addAnimation(PieChartItem *item);
51 51 void addAnimation(ScatterChartItem *item);
52 52 void addAnimation(LineChartItem *item);
53 53 void addAnimation(SplineChartItem *item);
54 54 void addAnimation(BarChartItem *item);
55 55 void removeAnimation(Chart *item);
56 56
57 57 void animationStarted();
58 58 void updateLayout(XYChartItem *item, QVector<QPointF> &oldLayout, QVector<QPointF> &newLayout, int index);
59 59 void updateLayout(SplineChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newContorlPoints, int index);
60 void updateLayout(Axis *item, QVector<qreal> &layout);
60 void updateLayout(ChartAxis *item, QVector<qreal> &layout);
61 61
62 62 void addAnimation(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty);
63 63 void removeAnimation(PieChartItem *item, QPieSlice *slice);
64 64 void updateLayout(PieChartItem *item, const PieLayout &layout);
65 65 void updateLayout(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData);
66 66
67 67 void updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
68 68
69 69 void setState(State state,const QPointF &point = QPointF());
70 70
71 71 private:
72 72 QMap<Chart *, ChartAnimation *> m_animations;
73 73 State m_state;
74 74 QPointF m_point;
75 75 };
76 76
77 77 QTCOMMERCIALCHART_END_NAMESPACE
78 78
79 79 #endif
@@ -1,16 +1,16
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 $$PWD/axis.cpp \
6 $$PWD/qchartaxis.cpp \
5 $$PWD/chartaxis.cpp \
6 $$PWD/qaxis.cpp \
7 7 $$PWD/qchartaxiscategories.cpp
8 8
9 9 PRIVATE_HEADERS += \
10 $$PWD/axis_p.h \
11 $$PWD/qchartaxis_p.h \
10 $$PWD/chartaxis_p.h \
11 $$PWD/qaxis_p.h \
12 12 $$PWD/qchartaxiscategories_p.h
13 13
14 14 PUBLIC_HEADERS += \
15 $$PWD/qchartaxis.h \
15 $$PWD/qaxis.h \
16 16 $$PWD/qchartaxiscategories.h No newline at end of file
@@ -1,466 +1,466
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "axis_p.h"
22 #include "qchartaxis.h"
23 #include "qchartaxis_p.h"
21 #include "chartaxis_p.h"
22 #include "qaxis.h"
23 #include "qaxis_p.h"
24 24 #include "qchartaxiscategories_p.h"
25 25 #include "chartpresenter_p.h"
26 26 #include "chartanimator_p.h"
27 27 #include <QPainter>
28 28 #include <QDebug>
29 29 #include <cmath>
30 30
31 31 static int label_padding = 5;
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 Axis::Axis(QChartAxis *axis,ChartPresenter *presenter,AxisType type) : Chart(presenter),
35 ChartAxis::ChartAxis(QAxis *axis,ChartPresenter *presenter,AxisType type) : Chart(presenter),
36 36 m_chartAxis(axis),
37 37 m_type(type),
38 38 m_labelsAngle(0),
39 39 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
40 40 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
41 41 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
42 42 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
43 43 m_min(0),
44 44 m_max(0),
45 45 m_ticksCount(0)
46 46 {
47 47 //initial initialization
48 48 m_axis->setZValue(ChartPresenter::AxisZValue);
49 49 m_axis->setHandlesChildEvents(false);
50 50
51 51 m_shades->setZValue(ChartPresenter::ShadesZValue);
52 52 m_grid->setZValue(ChartPresenter::GridZValue);
53 53
54 54 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
55 55 QObject::connect(m_chartAxis->categories()->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
56 56
57 57 handleAxisUpdated();
58 58 }
59 59
60 Axis::~Axis()
60 ChartAxis::~ChartAxis()
61 61 {
62 62 }
63 63
64 void Axis::createItems(int count)
64 void ChartAxis::createItems(int count)
65 65 {
66 66
67 67 if (m_axis->children().size() == 0)
68 68 m_axis->addToGroup(new AxisItem(this));
69 69 for (int i = 0; i < count; ++i) {
70 70 m_grid->addToGroup(new QGraphicsLineItem());
71 71 m_labels->addToGroup(new QGraphicsSimpleTextItem());
72 72 m_axis->addToGroup(new QGraphicsLineItem());
73 73 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
74 74 }
75 75 }
76 76
77 void Axis::deleteItems(int count)
77 void ChartAxis::deleteItems(int count)
78 78 {
79 79 QList<QGraphicsItem *> lines = m_grid->childItems();
80 80 QList<QGraphicsItem *> labels = m_labels->childItems();
81 81 QList<QGraphicsItem *> shades = m_shades->childItems();
82 82 QList<QGraphicsItem *> axis = m_axis->childItems();
83 83
84 84 for (int i = 0; i < count; ++i) {
85 85 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
86 86 delete(lines.takeLast());
87 87 delete(labels.takeLast());
88 88 delete(axis.takeLast());
89 89 }
90 90 }
91 91
92 void Axis::updateLayout(QVector<qreal> &layout)
92 void ChartAxis::updateLayout(QVector<qreal> &layout)
93 93 {
94 94 if (animator()) {
95 95 animator()->updateLayout(this,layout);
96 96 } else {
97 97 setLayout(layout);
98 98 }
99 99 }
100 100
101 bool Axis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
101 bool ChartAxis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
102 102 {
103 103 Q_ASSERT(max>=min);
104 104 Q_ASSERT(ticks>1);
105 105
106 106 QChartAxisCategories* categories = m_chartAxis->categories();
107 107
108 108 bool category = categories->count()>0;
109 109
110 110 if (!category) {
111 111 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
112 112 n++;
113 113 for (int i=0; i< ticks; i++) {
114 114 qreal value = min + (i * (max - min)/ (ticks-1));
115 115 labels << QString::number(value,'f',n);
116 116 }
117 117 } else {
118 118 QList<qreal> values = categories->values();
119 119 for (int i=0; i< ticks; i++) {
120 120 qreal value = (min + (i * (max - min)/ (ticks-1)));
121 121 int j=0;
122 122 for (; j<values.count(); j++) {
123 123 if (values.at(j) > value) break;
124 124 }
125 125 if (j!=0) value=values.at(j-1);
126 126
127 127 QString label = categories->label(value);
128 128 labels << label;
129 129 }
130 130 }
131 131
132 132 return category;
133 133 }
134 134
135 void Axis::setAxisOpacity(qreal opacity)
135 void ChartAxis::setAxisOpacity(qreal opacity)
136 136 {
137 137 m_axis->setOpacity(opacity);
138 138 }
139 139
140 qreal Axis::axisOpacity() const
140 qreal ChartAxis::axisOpacity() const
141 141 {
142 142 return m_axis->opacity();
143 143 }
144 144
145 void Axis::setGridOpacity(qreal opacity)
145 void ChartAxis::setGridOpacity(qreal opacity)
146 146 {
147 147 m_grid->setOpacity(opacity);
148 148 }
149 149
150 qreal Axis::gridOpacity() const
150 qreal ChartAxis::gridOpacity() const
151 151 {
152 152 return m_grid->opacity();
153 153 }
154 154
155 void Axis::setLabelsOpacity(qreal opacity)
155 void ChartAxis::setLabelsOpacity(qreal opacity)
156 156 {
157 157 m_labels->setOpacity(opacity);
158 158 }
159 159
160 qreal Axis::labelsOpacity() const
160 qreal ChartAxis::labelsOpacity() const
161 161 {
162 162 return m_labels->opacity();
163 163 }
164 164
165 void Axis::setShadesOpacity(qreal opacity)
165 void ChartAxis::setShadesOpacity(qreal opacity)
166 166 {
167 167 m_shades->setOpacity(opacity);
168 168 }
169 169
170 qreal Axis::shadesOpacity() const
170 qreal ChartAxis::shadesOpacity() const
171 171 {
172 172 return m_shades->opacity();
173 173 }
174 174
175 void Axis::setLabelsAngle(int angle)
175 void ChartAxis::setLabelsAngle(int angle)
176 176 {
177 177 foreach(QGraphicsItem* item , m_labels->childItems()) {
178 178 item->setRotation(angle);
179 179 }
180 180
181 181 m_labelsAngle=angle;
182 182 }
183 183
184 void Axis::setLabelsPen(const QPen &pen)
184 void ChartAxis::setLabelsPen(const QPen &pen)
185 185 {
186 186 foreach(QGraphicsItem* item , m_labels->childItems()) {
187 187 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
188 188 }
189 189 }
190 190
191 void Axis::setLabelsBrush(const QBrush &brush)
191 void ChartAxis::setLabelsBrush(const QBrush &brush)
192 192 {
193 193 foreach(QGraphicsItem* item , m_labels->childItems()) {
194 194 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
195 195 }
196 196 }
197 197
198 void Axis::setLabelsFont(const QFont &font)
198 void ChartAxis::setLabelsFont(const QFont &font)
199 199 {
200 200 foreach(QGraphicsItem* item , m_labels->childItems()) {
201 201 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
202 202 }
203 203 }
204 204
205 void Axis::setShadesBrush(const QBrush &brush)
205 void ChartAxis::setShadesBrush(const QBrush &brush)
206 206 {
207 207 foreach(QGraphicsItem* item , m_shades->childItems()) {
208 208 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
209 209 }
210 210 }
211 211
212 void Axis::setShadesPen(const QPen &pen)
212 void ChartAxis::setShadesPen(const QPen &pen)
213 213 {
214 214 foreach(QGraphicsItem* item , m_shades->childItems()) {
215 215 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
216 216 }
217 217 }
218 218
219 void Axis::setAxisPen(const QPen &pen)
219 void ChartAxis::setAxisPen(const QPen &pen)
220 220 {
221 221 foreach(QGraphicsItem* item , m_axis->childItems()) {
222 222 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
223 223 }
224 224 }
225 225
226 void Axis::setGridPen(const QPen &pen)
226 void ChartAxis::setGridPen(const QPen &pen)
227 227 {
228 228 foreach(QGraphicsItem* item , m_grid->childItems()) {
229 229 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
230 230 }
231 231 }
232 232
233 QVector<qreal> Axis::calculateLayout() const
233 QVector<qreal> ChartAxis::calculateLayout() const
234 234 {
235 235 Q_ASSERT(m_ticksCount>=2);
236 236
237 237 QVector<qreal> points;
238 238 points.resize(m_ticksCount);
239 239
240 240 switch (m_type)
241 241 {
242 242 case X_AXIS:
243 243 {
244 244 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
245 245 for (int i = 0; i < m_ticksCount; ++i) {
246 246 int x = i * deltaX + m_rect.left();
247 247 points[i] = x;
248 248 }
249 249 }
250 250 break;
251 251 case Y_AXIS:
252 252 {
253 253 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
254 254 for (int i = 0; i < m_ticksCount; ++i) {
255 255 int y = i * -deltaY + m_rect.bottom();
256 256 points[i] = y;
257 257 }
258 258 }
259 259 break;
260 260 }
261 261 return points;
262 262 }
263 263
264 void Axis::setLayout(QVector<qreal> &layout)
264 void ChartAxis::setLayout(QVector<qreal> &layout)
265 265 {
266 266 int diff = m_layoutVector.size() - layout.size();
267 267
268 268 if (diff>0) {
269 269 deleteItems(diff);
270 270 } else if (diff<0) {
271 271 createItems(-diff);
272 272 }
273 273
274 274 if( diff!=0) handleAxisUpdated();
275 275
276 276 QStringList ticksList;
277 277
278 278 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
279 279
280 280 QList<QGraphicsItem *> lines = m_grid->childItems();
281 281 QList<QGraphicsItem *> labels = m_labels->childItems();
282 282 QList<QGraphicsItem *> shades = m_shades->childItems();
283 283 QList<QGraphicsItem *> axis = m_axis->childItems();
284 284
285 285 Q_ASSERT(labels.size() == ticksList.size());
286 286 Q_ASSERT(layout.size() == ticksList.size());
287 287
288 288 qreal minWidth = 0;
289 289 qreal minHeight = 0;
290 290
291 291 switch (m_type)
292 292 {
293 293 case X_AXIS:
294 294 {
295 295 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
296 296 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
297 297
298 298 for (int i = 0; i < layout.size(); ++i) {
299 299 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
300 300 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
301 301 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
302 302 if (!categories || i<1) {
303 303 labelItem->setText(ticksList.at(i));
304 304 const QRectF& rect = labelItem->boundingRect();
305 305 minWidth+=rect.width();
306 306 minHeight=qMax(rect.height(),minHeight);
307 307 QPointF center = rect.center();
308 308 labelItem->setTransformOriginPoint(center.x(), center.y());
309 309 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
310 310 } else {
311 311 labelItem->setText(ticksList.at(i));
312 312 const QRectF& rect = labelItem->boundingRect();
313 313 minWidth+=rect.width();
314 314 minHeight=qMax(rect.height()+label_padding,minHeight);
315 315 QPointF center = rect.center();
316 316 labelItem->setTransformOriginPoint(center.x(), center.y());
317 317 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
318 318 }
319 319
320 320 if ((i+1)%2 && i>1) {
321 321 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
322 322 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
323 323 }
324 324 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
325 325 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
326 326 }
327 327
328 328 }
329 329 break;
330 330
331 331 case Y_AXIS:
332 332 {
333 333 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
334 334 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
335 335
336 336 for (int i = 0; i < layout.size(); ++i) {
337 337 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
338 338 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
339 339 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
340 340
341 341 if (!categories || i<1) {
342 342 labelItem->setText(ticksList.at(i));
343 343 const QRectF& rect = labelItem->boundingRect();
344 344 minWidth=qMax(rect.width()+label_padding,minWidth);
345 345 minHeight+=rect.height();
346 346 QPointF center = rect.center();
347 347 labelItem->setTransformOriginPoint(center.x(), center.y());
348 348 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
349 349 } else {
350 350 labelItem->setText(ticksList.at(i));
351 351 const QRectF& rect = labelItem->boundingRect();
352 352 minWidth=qMax(rect.width(),minWidth);
353 353 minHeight+=rect.height();
354 354 QPointF center = rect.center();
355 355 labelItem->setTransformOriginPoint(center.x(), center.y());
356 356 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
357 357 }
358 358
359 359 if ((i+1)%2 && i>1) {
360 360 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
361 361 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
362 362 }
363 363 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
364 364 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
365 365 }
366 366 }
367 367 break;
368 368 default:
369 369 qWarning()<<"Unknown axis type";
370 370 break;
371 371 }
372 372
373 373 m_layoutVector=layout;
374 374
375 375 presenter()->setMinimumMarginWidth(this,minWidth);
376 376 presenter()->setMinimumMarginHeight(this,minHeight);
377 377
378 378 }
379 379
380 bool Axis::isEmpty()
380 bool ChartAxis::isEmpty()
381 381 {
382 382 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0;
383 383 }
384 384
385 385 //handlers
386 386
387 void Axis::handleAxisCategoriesUpdated()
387 void ChartAxis::handleAxisCategoriesUpdated()
388 388 {
389 389 if (isEmpty()) return;
390 390 updateLayout(m_layoutVector);
391 391 }
392 392
393 void Axis::handleAxisUpdated()
393 void ChartAxis::handleAxisUpdated()
394 394 {
395 395
396 396 if (isEmpty()) return;
397 397
398 398 if (m_chartAxis->isAxisVisible()) {
399 399 setAxisOpacity(100);
400 400 } else {
401 401 setAxisOpacity(0);
402 402 }
403 403
404 404 if (m_chartAxis->isGridLineVisible()) {
405 405 setGridOpacity(100);
406 406 } else {
407 407 setGridOpacity(0);
408 408 }
409 409
410 410 if (m_chartAxis->labelsVisible()) {
411 411 setLabelsOpacity(100);
412 412 } else {
413 413 setLabelsOpacity(0);
414 414 }
415 415
416 416 if (m_chartAxis->shadesVisible()) {
417 417 setShadesOpacity(m_chartAxis->shadesOpacity());
418 418 } else {
419 419 setShadesOpacity(0);
420 420 }
421 421
422 422 setLabelsAngle(m_chartAxis->labelsAngle());
423 423 setAxisPen(m_chartAxis->axisPen());
424 424 setLabelsPen(m_chartAxis->labelsPen());
425 425 setLabelsBrush(m_chartAxis->labelsBrush());
426 426 setLabelsFont(m_chartAxis->labelsFont());
427 427 setGridPen(m_chartAxis->gridLinePen());
428 428 setShadesPen(m_chartAxis->shadesPen());
429 429 setShadesBrush(m_chartAxis->shadesBrush());
430 430
431 431 }
432 432
433 void Axis::handleRangeChanged(qreal min, qreal max,int tickCount)
433 void ChartAxis::handleRangeChanged(qreal min, qreal max,int tickCount)
434 434 {
435 435 if (qFuzzyIsNull(min - max) || tickCount < 2)
436 436 return;
437 437
438 438 m_min = min;
439 439 m_max = max;
440 440 m_ticksCount= tickCount;
441 441
442 442 if (isEmpty()) return;
443 443 QVector<qreal> layout = calculateLayout();
444 444 updateLayout(layout);
445 445
446 446 }
447 447
448 void Axis::handleGeometryChanged(const QRectF &rect)
448 void ChartAxis::handleGeometryChanged(const QRectF &rect)
449 449 {
450 450 if(m_rect != rect)
451 451 {
452 452 m_rect = rect;
453 453 if (isEmpty()) return;
454 454 QVector<qreal> layout = calculateLayout();
455 455 updateLayout(layout);
456 456 }
457 457 }
458 458
459 void Axis::axisSelected()
459 void ChartAxis::axisSelected()
460 460 {
461 461 qDebug()<<"TODO: axis clicked";
462 462 }
463 463
464 #include "moc_axis_p.cpp"
464 #include "moc_chartaxis_p.cpp"
465 465
466 466 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,143 +1,143
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef AXIS_H
22 22 #define AXIS_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "chart_p.h"
26 26 #include <QGraphicsItem>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 class QChartAxis;
30 class QAxis;
31 31 class ChartPresenter;
32 32
33 class Axis : public Chart
33 class ChartAxis : public Chart
34 34 {
35 35 Q_OBJECT
36 36 public:
37 37 enum AxisType{X_AXIS,Y_AXIS};
38 38
39 Axis(QChartAxis *axis, ChartPresenter *presenter, AxisType type = X_AXIS);
40 ~Axis();
39 ChartAxis(QAxis *axis, ChartPresenter *presenter, AxisType type = X_AXIS);
40 ~ChartAxis();
41 41
42 42 AxisType axisType() const { return m_type; }
43 43
44 44 void setAxisOpacity(qreal opacity);
45 45 qreal axisOpacity() const;
46 46
47 47 void setGridOpacity(qreal opacity);
48 48 qreal gridOpacity() const;
49 49
50 50 void setLabelsOpacity(qreal opacity);
51 51 qreal labelsOpacity() const;
52 52
53 53 void setShadesOpacity(qreal opacity);
54 54 qreal shadesOpacity() const;
55 55
56 56 void setLabelsAngle(int angle);
57 57 int labelsAngle()const { return m_labelsAngle; }
58 58
59 59 void setShadesBrush(const QBrush &brush);
60 60 void setShadesPen(const QPen &pen);
61 61
62 62 void setAxisPen(const QPen &pen);
63 63 void setGridPen(const QPen &pen);
64 64
65 65 void setLabelsPen(const QPen &pen);
66 66 void setLabelsBrush(const QBrush &brush);
67 67 void setLabelsFont(const QFont &font);
68 68
69 69 inline QRectF geometry() const { return m_rect; }
70 70 inline QVector<qreal> layout() { return m_layoutVector; }
71 71
72 72 public Q_SLOTS:
73 73 void handleAxisUpdated();
74 74 void handleAxisCategoriesUpdated();
75 75 void handleRangeChanged(qreal min , qreal max,int tickCount);
76 76 void handleGeometryChanged(const QRectF &size);
77 77
78 78
79 79 private:
80 80 inline bool isEmpty();
81 81 void createItems(int count);
82 82 void deleteItems(int count);
83 83
84 84 QVector<qreal> calculateLayout() const;
85 85 void updateLayout(QVector<qreal> &layout);
86 86 void setLayout(QVector<qreal> &layout);
87 87
88 88 bool createLabels(QStringList &labels,qreal min, qreal max,int ticks) const;
89 89 void axisSelected();
90 90
91 91 private:
92 QChartAxis* m_chartAxis;
92 QAxis* m_chartAxis;
93 93 AxisType m_type;
94 94 QRectF m_rect;
95 95 int m_labelsAngle;
96 96 QScopedPointer<QGraphicsItemGroup> m_grid;
97 97 QScopedPointer<QGraphicsItemGroup> m_shades;
98 98 QScopedPointer<QGraphicsItemGroup> m_labels;
99 99 QScopedPointer<QGraphicsItemGroup> m_axis;
100 100 QVector<qreal> m_layoutVector;
101 101 qreal m_min;
102 102 qreal m_max;
103 103 int m_ticksCount;
104 104
105 105 friend class AxisAnimation;
106 106 friend class AxisItem;
107 107
108 108 };
109 109
110 110 class AxisItem: public QGraphicsLineItem
111 111 {
112 112
113 113 public:
114 explicit AxisItem(Axis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
114 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
115 115
116 116 protected:
117 117 void mousePressEvent(QGraphicsSceneMouseEvent *event)
118 118 {
119 119 Q_UNUSED(event)
120 120 m_axis->axisSelected();
121 121 }
122 122
123 123 QRectF boundingRect() const
124 124 {
125 125 return shape().boundingRect();
126 126 }
127 127
128 128 QPainterPath shape() const
129 129 {
130 130 QPainterPath path = QGraphicsLineItem::shape();
131 131 QRectF rect = path.boundingRect();
132 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?8:0,m_axis->axisType()!=Axis::Y_AXIS?8:0));
132 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
133 133 return path;
134 134 }
135 135
136 136 private:
137 Axis* m_axis;
137 ChartAxis* m_axis;
138 138
139 139 };
140 140
141 141 QTCOMMERCIALCHART_END_NAMESPACE
142 142
143 143 #endif /* AXISITEM_H_ */
@@ -1,533 +1,533
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "qchartaxis.h"
22 #include "qchartaxis_p.h"
21 #include "qaxis.h"
22 #include "qaxis_p.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QChartAxis
28 28 \brief The QChartAxis class is used for manipulating chart's axis
29 29 and for adding optional axes to the chart.
30 30 \mainclass
31 31
32 32 There is only one x Axis, however there can be multiple y axes.
33 33 Each chart series can be bound to exactly one Y axis and the share common X axis.
34 34 Axis can be setup to show axis line with ticks, gird lines and shades.
35 35
36 36 */
37 37
38 38 /*!
39 \fn bool QChartAxis::isAxisVisible() const
39 \fn bool QAxis::isAxisVisible() const
40 40 \brief Returns if axis is visible
41 41 \sa setAxisVisible()
42 42 */
43 43
44 44 /*!
45 \fn QPen QChartAxis::axisPen() const
45 \fn QPen QAxis::axisPen() const
46 46 \brief Returns pen used to draw axis and ticks.
47 47 \sa setAxisPen()
48 48 */
49 49
50 50
51 51 /*!
52 \fn bool QChartAxis::isGridLineVisible() const
52 \fn bool QAxis::isGridLineVisible() const
53 53 \brief Returns if grid is visible
54 54 \sa setGridLineVisible()
55 55 */
56 56
57 57 /*!
58 \fn QPen QChartAxis::gridLinePen() const
58 \fn QPen QAxis::gridLinePen() const
59 59 \brief Returns pen used to draw grid.
60 60 \sa setGridLinePen()
61 61 */
62 62
63 63 /*!
64 \fn bool QChartAxis::labelsVisible() const
64 \fn bool QAxis::labelsVisible() const
65 65 \brief Returns if grid is visible
66 66 \sa setLabelsVisible()
67 67 */
68 68
69 69 /*!
70 \fn QPen QChartAxis::labelsPen() const
70 \fn QPen QAxis::labelsPen() const
71 71 \brief Returns the pen used to labels.
72 72 \sa setLabelsPen()
73 73 */
74 74
75 75 /*!
76 \fn QBrush QChartAxis::labelsBrush() const
76 \fn QBrush QAxis::labelsBrush() const
77 77 \brief Returns brush used to draw labels.
78 78 \sa setLabelsBrush()
79 79 */
80 80
81 81 /*!
82 \fn QFont QChartAxis::labelsFont() const
82 \fn QFont QAxis::labelsFont() const
83 83 \brief Returns font used to draw labels.
84 84 \sa setLabelsFont()
85 85 */
86 86
87 87 /*!
88 \fn QFont QChartAxis::labelsAngle() const
88 \fn QFont QAxis::labelsAngle() const
89 89 \brief Returns angle used to draw labels.
90 90 \sa setLabelsAngle()
91 91 */
92 92
93 93 /*!
94 \fn bool QChartAxis::shadesVisible() const
94 \fn bool QAxis::shadesVisible() const
95 95 \brief Returns if shades are visible.
96 96 \sa setShadesVisible()
97 97 */
98 98
99 99 /*!
100 \fn qreal QChartAxis::shadesOpacity() const
100 \fn qreal QAxis::shadesOpacity() const
101 101 \brief Returns opacity of shades.
102 102 */
103 103
104 104 /*!
105 \fn QPen QChartAxis::shadesPen() const
105 \fn QPen QAxis::shadesPen() const
106 106 \brief Returns pen used to draw shades.
107 107 \sa setShadesPen()
108 108 */
109 109
110 110 /*!
111 \fn QBrush QChartAxis::shadesBrush() const
111 \fn QBrush QAxis::shadesBrush() const
112 112 \brief Returns brush used to draw shades.
113 113 \sa setShadesBrush()
114 114 */
115 115
116 116 /*!
117 \fn qreal QChartAxis::min() const
117 \fn qreal QAxis::min() const
118 118 \brief Returns minimum value on the axis.
119 119 \sa setMin()
120 120 */
121 121
122 122 /*!
123 \fn qreal QChartAxis::max() const
123 \fn qreal QAxis::max() const
124 124 \brief Returns maximim value on the axis.
125 125 \sa setMax()
126 126 */
127 127
128 128 /*!
129 \fn void QChartAxis::minChanged(qreal min)
129 \fn void QAxis::minChanged(qreal min)
130 130 \brief Axis emits signal when \a min of axis has changed.
131 131 */
132 132
133 133 /*!
134 \fn void QChartAxis::maxChanged(qreal max)
134 \fn void QAxis::maxChanged(qreal max)
135 135 \brief Axis emits signal when \a max of axis has changed.
136 136 */
137 137
138 138 /*!
139 \fn void QChartAxis::rangeChanged(qreal min, qreal max)
139 \fn void QAxis::rangeChanged(qreal min, qreal max)
140 140 \brief Axis emits signal when \a min or \a max of axis has changed.
141 141 */
142 142
143 143 /*!
144 \fn int QChartAxis::ticksCount() const
144 \fn int QAxis::ticksCount() const
145 145 \brief Return number of ticks on the axis
146 146 \sa setTicksCount()
147 147 */
148 148
149 149 /*!
150 \fn QChartAxisCategories* QChartAxis::categories()
150 \fn QChartAxisCategories* QAxis::categories()
151 151 \brief Returns pointer to the list of categories which correspond to the values on the axis.
152 152 */
153 153
154 154 /*!
155 \fn void QChartAxis::ticksCountChanged(int count)
155 \fn void QAxis::ticksCountChanged(int count)
156 156 \brief Emits the new \a count of ticks on the axis
157 157 Signal is emitted when the number of the ticks on the axis has been changed to a different value.
158 158 Parementer count\a count is the new number of ticks on the axis.
159 159 */
160 160
161 161 /*!
162 162 Constructs new axis object which is a child of \a parent. Ownership is taken by
163 163 QChatView or QChart when axis added.
164 164 */
165 165
166 QChartAxis::QChartAxis(QObject *parent) : QObject(parent),
167 d_ptr(new QChartAxisPrivate(this))
166 QAxis::QAxis(QObject *parent) : QObject(parent),
167 d_ptr(new QAxisPrivate(this))
168 168 {
169 169
170 170 }
171 171
172 172 /*!
173 173 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
174 174 */
175 175
176 QChartAxis::~QChartAxis()
176 QAxis::~QAxis()
177 177 {
178 178 }
179 179
180 180 /*!
181 181 Sets \a pen used to draw axis line and ticks.
182 182 */
183 void QChartAxis::setAxisPen(const QPen &pen)
183 void QAxis::setAxisPen(const QPen &pen)
184 184 {
185 185 if (d_ptr->m_axisPen!=pen) {
186 186 d_ptr->m_axisPen = pen;
187 187 emit d_ptr->updated();
188 188 }
189 189 }
190 190
191 QPen QChartAxis::axisPen() const
191 QPen QAxis::axisPen() const
192 192 {
193 193 return d_ptr->m_axisPen;
194 194 }
195 195
196 196 /*!
197 197 Sets if axis and ticks are \a visible.
198 198 */
199 void QChartAxis::setAxisVisible(bool visible)
199 void QAxis::setAxisVisible(bool visible)
200 200 {
201 201 if (d_ptr->m_axisVisible != visible) {
202 202 d_ptr->m_axisVisible = visible;
203 203 emit d_ptr->updated();
204 204 }
205 205 }
206 206
207 bool QChartAxis::isAxisVisible() const
207 bool QAxis::isAxisVisible() const
208 208 {
209 209 return d_ptr->m_axisVisible;
210 210 }
211 211
212 212 /*!
213 213 Sets if grid line is \a visible.
214 214 */
215 void QChartAxis::setGridLineVisible(bool visible)
215 void QAxis::setGridLineVisible(bool visible)
216 216 {
217 217 if (d_ptr->m_gridLineVisible != visible) {
218 218 d_ptr->m_gridLineVisible = visible;
219 219 emit d_ptr->updated();
220 220 }
221 221 }
222 222
223 bool QChartAxis::isGridLineVisible() const
223 bool QAxis::isGridLineVisible() const
224 224 {
225 225 return d_ptr->m_gridLineVisible;
226 226 }
227 227 /*!
228 228 Sets \a pen used to draw grid line.
229 229 */
230 void QChartAxis::setGridLinePen(const QPen &pen)
230 void QAxis::setGridLinePen(const QPen &pen)
231 231 {
232 232 if (d_ptr->m_gridLinePen != pen) {
233 233 d_ptr->m_gridLinePen = pen;
234 234 emit d_ptr->updated();
235 235 }
236 236 }
237 237
238 QPen QChartAxis::gridLinePen() const
238 QPen QAxis::gridLinePen() const
239 239 {
240 240 return d_ptr->m_gridLinePen;
241 241 }
242 242
243 243 /*!
244 244 Sets if axis' labels are \a visible.
245 245 */
246 void QChartAxis::setLabelsVisible(bool visible)
246 void QAxis::setLabelsVisible(bool visible)
247 247 {
248 248 if (d_ptr->m_labelsVisible != visible) {
249 249 d_ptr->m_labelsVisible = visible;
250 250 emit d_ptr->updated();
251 251 }
252 252 }
253 253
254 bool QChartAxis::labelsVisible() const
254 bool QAxis::labelsVisible() const
255 255 {
256 256 return d_ptr->m_labelsVisible;
257 257 }
258 258 /*!
259 259 Sets \a pen used to draw labels.
260 260 */
261 void QChartAxis::setLabelsPen(const QPen &pen)
261 void QAxis::setLabelsPen(const QPen &pen)
262 262 {
263 263 if (d_ptr->m_labelsPen != pen) {
264 264 d_ptr->m_labelsPen = pen;
265 265 emit d_ptr->updated();
266 266 }
267 267 }
268 268
269 QPen QChartAxis::labelsPen() const
269 QPen QAxis::labelsPen() const
270 270 {
271 271 return d_ptr->m_labelsPen;
272 272 }
273 273
274 274 /*!
275 275 Sets \a brush used to draw labels.
276 276 */
277 void QChartAxis::setLabelsBrush(const QBrush &brush)
277 void QAxis::setLabelsBrush(const QBrush &brush)
278 278 {
279 279 if (d_ptr->m_labelsBrush != brush) {
280 280 d_ptr->m_labelsBrush = brush;
281 281 emit d_ptr->updated();
282 282 }
283 283 }
284 284
285 QBrush QChartAxis::labelsBrush() const
285 QBrush QAxis::labelsBrush() const
286 286 {
287 287 return d_ptr->m_labelsBrush;
288 288 }
289 289
290 290 /*!
291 291 Sets \a font used to draw labels.
292 292 */
293 void QChartAxis::setLabelsFont(const QFont &font)
293 void QAxis::setLabelsFont(const QFont &font)
294 294 {
295 295 if (d_ptr->m_labelsFont != font) {
296 296 d_ptr->m_labelsFont = font;
297 297 emit d_ptr->updated();
298 298 }
299 299 }
300 300
301 QFont QChartAxis::labelsFont() const
301 QFont QAxis::labelsFont() const
302 302 {
303 303 return d_ptr->m_labelsFont;
304 304 }
305 305
306 306 /*!
307 307 Sets \a angle for all the labels on given axis.
308 308 */
309 void QChartAxis::setLabelsAngle(int angle)
309 void QAxis::setLabelsAngle(int angle)
310 310 {
311 311 if (d_ptr->m_labelsAngle != angle) {
312 312 d_ptr->m_labelsAngle = angle;
313 313 emit d_ptr->updated();
314 314 }
315 315 }
316 316
317 int QChartAxis::labelsAngle() const
317 int QAxis::labelsAngle() const
318 318 {
319 319 return d_ptr->m_labelsAngle;
320 320 }
321 321
322 322 /*!
323 323 Sets if shades are \a visible.
324 324 */
325 void QChartAxis::setShadesVisible(bool visible)
325 void QAxis::setShadesVisible(bool visible)
326 326 {
327 327 if (d_ptr->m_shadesVisible != visible) {
328 328 d_ptr->m_shadesVisible = visible;
329 329 emit d_ptr->updated();
330 330 }
331 331 }
332 332
333 bool QChartAxis::shadesVisible() const
333 bool QAxis::shadesVisible() const
334 334 {
335 335 return d_ptr->m_shadesVisible;
336 336 }
337 337
338 338 /*!
339 339 Sets \a pen used to draw shades.
340 340 */
341 void QChartAxis::setShadesPen(const QPen &pen)
341 void QAxis::setShadesPen(const QPen &pen)
342 342 {
343 343 if (d_ptr->m_shadesPen != pen) {
344 344 d_ptr->m_shadesPen = pen;
345 345 emit d_ptr->updated();
346 346 }
347 347 }
348 348
349 QPen QChartAxis::shadesPen() const
349 QPen QAxis::shadesPen() const
350 350 {
351 351 return d_ptr->m_shadesPen;
352 352 }
353 353
354 354 /*!
355 355 Sets \a brush used to draw shades.
356 356 */
357 void QChartAxis::setShadesBrush(const QBrush &brush)
357 void QAxis::setShadesBrush(const QBrush &brush)
358 358 {
359 359 if (d_ptr->m_shadesBrush != brush) {
360 360 d_ptr->m_shadesBrush = brush;
361 361 emit d_ptr->updated();
362 362 }
363 363 }
364 364
365 QBrush QChartAxis::shadesBrush() const
365 QBrush QAxis::shadesBrush() const
366 366 {
367 367 return d_ptr->m_shadesBrush;
368 368 }
369 369
370 370 /*!
371 371 Sets \a opacity of the shades.
372 372 */
373 void QChartAxis::setShadesOpacity(qreal opacity)
373 void QAxis::setShadesOpacity(qreal opacity)
374 374 {
375 375 if (d_ptr->m_shadesOpacity != opacity) {
376 376 d_ptr->m_shadesOpacity=opacity;
377 377 emit d_ptr->updated();
378 378 }
379 379 }
380 380
381 qreal QChartAxis::shadesOpacity() const
381 qreal QAxis::shadesOpacity() const
382 382 {
383 383 return d_ptr->m_shadesOpacity;
384 384 }
385 385
386 386 /*!
387 387 Sets \a min value on the axis.
388 388 */
389 void QChartAxis::setMin(qreal min)
389 void QAxis::setMin(qreal min)
390 390 {
391 391 setRange(min,d_ptr->m_max);
392 392 }
393 393
394 qreal QChartAxis::min() const
394 qreal QAxis::min() const
395 395 {
396 396 return d_ptr->m_min;
397 397 }
398 398
399 399 /*!
400 400 Sets \a max value on the axis.
401 401 */
402 void QChartAxis::setMax(qreal max)
402 void QAxis::setMax(qreal max)
403 403 {
404 404 setRange(d_ptr->m_min,max);
405 405 }
406 406
407 qreal QChartAxis::max() const
407 qreal QAxis::max() const
408 408 {
409 409 return d_ptr->m_max;
410 410 }
411 411
412 412 /*!
413 413 Sets range from \a min to \a max on the axis.
414 414 */
415 void QChartAxis::setRange(qreal min, qreal max)
415 void QAxis::setRange(qreal min, qreal max)
416 416 {
417 417 bool changed = false;
418 418 if (!qFuzzyIsNull(d_ptr->m_min - min)) {
419 419 d_ptr->m_min = min;
420 420 changed = true;
421 421 emit minChanged(min);
422 422 }
423 423
424 424 if (!qFuzzyIsNull(d_ptr->m_max - max)) {
425 425 d_ptr->m_max = max;
426 426 changed = true;
427 427 emit maxChanged(max);
428 428 }
429 429
430 430 if (changed) {
431 431 emit rangeChanged(d_ptr->m_min,d_ptr->m_max);
432 432 emit d_ptr->changed(d_ptr->m_min, d_ptr->m_max, d_ptr->m_ticksCount, d_ptr->m_niceNumbers);
433 433 }
434 434 }
435 435
436 436 /*!
437 437 Sets \a count for ticks on the axis.
438 438 */
439 void QChartAxis::setTicksCount(int count)
439 void QAxis::setTicksCount(int count)
440 440 {
441 441 if (d_ptr->m_ticksCount != count) {
442 442 d_ptr->m_ticksCount = count;
443 443 emit ticksCountChanged(count);
444 444 emit d_ptr->changed(d_ptr->m_min, d_ptr->m_max, d_ptr->m_ticksCount, d_ptr->m_niceNumbers);
445 445 }
446 446 }
447 447
448 int QChartAxis::ticksCount() const
448 int QAxis::ticksCount() const
449 449 {
450 450 return d_ptr->m_ticksCount;
451 451 }
452 452
453 453 /*!
454 454 Sets axis, shades, labels and grid lines to be visible.
455 455 */
456 void QChartAxis::show()
456 void QAxis::show()
457 457 {
458 458 d_ptr->m_axisVisible=true;
459 459 d_ptr->m_gridLineVisible=true;
460 460 d_ptr->m_labelsVisible=true;
461 461 d_ptr->m_shadesVisible=true;
462 462 emit d_ptr->updated();
463 463 }
464 464
465 465 /*!
466 466 Sets axis, shades, labels and grid lines to not be visible.
467 467 */
468 void QChartAxis::hide()
468 void QAxis::hide()
469 469 {
470 470 d_ptr->m_axisVisible = false;
471 471 d_ptr->m_gridLineVisible = false;
472 472 d_ptr->m_labelsVisible = false;
473 473 d_ptr->m_shadesVisible = false;
474 474 emit d_ptr->updated();
475 475 }
476 476
477 477 /*!
478 478 Sets the nice numbers state to \a enable
479 479 */
480 void QChartAxis::setNiceNumbersEnabled(bool enable)
480 void QAxis::setNiceNumbersEnabled(bool enable)
481 481 {
482 482 if (d_ptr->m_niceNumbers != enable){
483 483 d_ptr->m_niceNumbers = enable;
484 484 emit d_ptr->changed(d_ptr->m_min, d_ptr->m_max, d_ptr->m_ticksCount, d_ptr->m_niceNumbers);
485 485 }
486 486 }
487 487
488 488 /*!
489 489 Returns whether nice numbers are enabled or not.
490 490 */
491 bool QChartAxis::niceNumbersEnabled() const
491 bool QAxis::niceNumbersEnabled() const
492 492 {
493 493 return d_ptr->m_niceNumbers;
494 494 }
495 495
496 QChartAxisCategories* QChartAxis::categories()
496 QChartAxisCategories* QAxis::categories()
497 497 {
498 498 return &d_ptr->m_category;
499 499 }
500 500
501 501 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
502 502
503 QChartAxisPrivate::QChartAxisPrivate(QChartAxis* q):
503 QAxisPrivate::QAxisPrivate(QAxis* q):
504 504 q_ptr(q),
505 505 m_axisVisible(true),
506 506 m_gridLineVisible(true),
507 507 m_labelsVisible(true),
508 508 m_labelsAngle(0),
509 509 m_shadesVisible(false),
510 510 m_shadesOpacity(1.0),
511 511 m_min(0),
512 512 m_max(0),
513 513 m_ticksCount(5),
514 514 m_niceNumbers(false)
515 515 {
516 516
517 517 }
518 518
519 QChartAxisPrivate::~QChartAxisPrivate()
519 QAxisPrivate::~QAxisPrivate()
520 520 {
521 521
522 522 }
523 523
524 void QChartAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
524 void QAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
525 525 {
526 526 q_ptr->setRange(min,max);
527 527 q_ptr->setTicksCount(count);
528 528 }
529 529
530 #include "moc_qchartaxis.cpp"
531 #include "moc_qchartaxis_p.cpp"
530 #include "moc_qaxis.cpp"
531 #include "moc_qaxis_p.cpp"
532 532
533 533 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,108 +1,108
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 #ifndef QCHARTAXIS_H
22 #define QCHARTAXIS_H
21 #ifndef QAXIS_H
22 #define QAXIS_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qchartaxiscategories.h>
26 26 #include <QPen>
27 27 #include <QFont>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 class QChartAxisPrivate;
31 class QAxisPrivate;
32 32
33 class QTCOMMERCIALCHART_EXPORT QChartAxis : public QObject
33 class QTCOMMERCIALCHART_EXPORT QAxis : public QObject
34 34 {
35 35 Q_OBJECT
36 36 public:
37 37
38 QChartAxis(QObject *parent =0);
39 ~QChartAxis();
38 QAxis(QObject *parent =0);
39 ~QAxis();
40 40
41 41 //axis handling
42 42 bool isAxisVisible() const;
43 43 void setAxisVisible(bool visible = true);
44 44 void setAxisPen(const QPen &pen);
45 45 QPen axisPen() const;
46 46
47 47 //grid handling
48 48 bool isGridLineVisible() const;
49 49 void setGridLineVisible(bool visible = true);
50 50 void setGridLinePen(const QPen &pen);
51 51 QPen gridLinePen() const;
52 52
53 53 //labels handling
54 54 bool labelsVisible() const;
55 55 void setLabelsVisible(bool visible = true);
56 56 void setLabelsPen(const QPen &pen);
57 57 QPen labelsPen() const;
58 58 void setLabelsBrush(const QBrush &brush);
59 59 QBrush labelsBrush() const;
60 60 void setLabelsFont(const QFont &font);
61 61 QFont labelsFont() const;
62 62 void setLabelsAngle(int angle);
63 63 int labelsAngle() const;
64 64
65 65 //shades handling
66 66 bool shadesVisible() const;
67 67 void setShadesVisible(bool visible = true);
68 68 void setShadesPen(const QPen &pen);
69 69 QPen shadesPen() const;
70 70 void setShadesBrush(const QBrush &brush);
71 71 QBrush shadesBrush() const;
72 72 void setShadesOpacity(qreal opacity);
73 73 qreal shadesOpacity() const;
74 74
75 75 //range handling
76 76 void setMin(qreal min);
77 77 qreal min() const;
78 78 void setMax(qreal max);
79 79 qreal max() const;
80 80 void setRange(qreal min, qreal max);
81 81
82 82 //ticks handling
83 83 void setTicksCount(int count);
84 84 int ticksCount() const;
85 85
86 86 void setNiceNumbersEnabled(bool enable = true);
87 87 bool niceNumbersEnabled() const;
88 88
89 89 QChartAxisCategories* categories();
90 90
91 91 void show();
92 92 void hide();
93 93
94 94 Q_SIGNALS:
95 95 void minChanged(qreal min);
96 96 void maxChanged(qreal max);
97 97 void rangeChanged(qreal min, qreal max);
98 98 void ticksCountChanged(int count);
99 99
100 100 private:
101 QScopedPointer<QChartAxisPrivate> d_ptr;
102 Q_DISABLE_COPY(QChartAxis);
101 QScopedPointer<QAxisPrivate> d_ptr;
102 Q_DISABLE_COPY(QAxis);
103 103 friend class ChartDataSet;
104 friend class Axis;
104 friend class ChartAxis;
105 105 };
106 106
107 107 QTCOMMERCIALCHART_END_NAMESPACE
108 108 #endif /* QCHARTAXIS_H_ */
@@ -1,85 +1,85
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 #ifndef QCHARTAXIS_P_H
31 #define QCHARTAXIS_P_H
30 #ifndef QAXIS_P_H
31 #define QAXIS_P_H
32 32
33 #include "qchartaxis.h"
33 #include "qaxis.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 class QChartAxisPrivate : public QObject
37 class QAxisPrivate : public QObject
38 38 {
39 39 Q_OBJECT
40 40 public:
41 QChartAxisPrivate(QChartAxis *q);
42 ~QChartAxisPrivate();
41 QAxisPrivate(QAxis *q);
42 ~QAxisPrivate();
43 43
44 44 Q_SIGNALS:
45 45 void updated();
46 46 void changed(qreal min, qreal max, int tickCount,bool niceNumbers);
47 47
48 48 public Q_SLOTS:
49 49 void handleAxisRangeChanged(qreal min, qreal max,int count);
50 50
51 51 private:
52 QChartAxis *q_ptr;
52 QAxis *q_ptr;
53 53
54 54 bool m_axisVisible;
55 55 QPen m_axisPen;
56 56 QBrush m_axisBrush;
57 57
58 58 bool m_gridLineVisible;
59 59 QPen m_gridLinePen;
60 60
61 61 bool m_labelsVisible;
62 62 QPen m_labelsPen;
63 63 QBrush m_labelsBrush;
64 64 QFont m_labelsFont;
65 65 int m_labelsAngle;
66 66
67 67 bool m_shadesVisible;
68 68 QPen m_shadesPen;
69 69 QBrush m_shadesBrush;
70 70 qreal m_shadesOpacity;
71 71
72 72 qreal m_min;
73 73 qreal m_max;
74 74
75 75 int m_ticksCount;
76 76 QChartAxisCategories m_category;
77 77
78 78 bool m_niceNumbers;
79 79
80 friend class QChartAxis;
80 friend class QAxis;
81 81 };
82 82
83 83 QTCOMMERCIALCHART_END_NAMESPACE
84 84
85 85 #endif
@@ -1,56 +1,56
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QCHARTAXISCATEGORIES_H
22 22 #define QCHARTAXISCATEGORIES_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qbarseries.h>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 class QChartAxisCategoriesPrivate;
30 30
31 31 class QTCOMMERCIALCHART_EXPORT QChartAxisCategories : public QObject
32 32 {
33 33 Q_OBJECT
34 34 private:
35 35 QChartAxisCategories();
36 36 public:
37 37 ~QChartAxisCategories();
38 38
39 39 void insert(const QBarCategories &category);
40 40 void insert(qreal value,QString label);
41 41 void remove(qreal value);
42 42 QList<qreal> values() const;
43 43 QString label(qreal value) const;
44 44 void clear();
45 45 int count();
46 46
47 47 private:
48 48 QScopedPointer<QChartAxisCategoriesPrivate> d_ptr;
49 friend class QChartAxisPrivate;
50 friend class Axis;
49 friend class QAxisPrivate;
50 friend class ChartAxis;
51 51 };
52 52
53 53
54 54 QTCOMMERCIALCHART_END_NAMESPACE
55 55
56 56 #endif /* QCHARTAXISCATEGORIES_H_ */
@@ -1,206 +1,206
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "barchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "barlabel_p.h"
24 24 #include "qbarset.h"
25 25 #include "qbarset_p.h"
26 26 #include "qbarseries.h"
27 27 #include "qbarseries_p.h"
28 28 #include "qchart.h"
29 #include "qchartaxis.h"
29 //#include "qchartaxis.h"
30 30 #include "qchartaxiscategories.h"
31 31 #include "chartpresenter_p.h"
32 32 #include "chartanimator_p.h"
33 33 #include "chartdataset_p.h"
34 34 #include <QPainter>
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37
38 38 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
39 39 ChartItem(presenter),
40 40 m_layoutSet(false),
41 41 m_series(series)
42 42 {
43 43 setFlag(ItemClipsChildrenToShape);
44 44 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
45 45 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
46 46 setZValue(ChartPresenter::BarSeriesZValue);
47 47 dataChanged();
48 48 }
49 49
50 50 BarChartItem::~BarChartItem()
51 51 {
52 52 }
53 53
54 54 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
55 55 {
56 56 Q_UNUSED(painter);
57 57 Q_UNUSED(option);
58 58 Q_UNUSED(widget);
59 59 }
60 60
61 61 QRectF BarChartItem::boundingRect() const
62 62 {
63 63 return m_rect;
64 64 }
65 65
66 66 void BarChartItem::dataChanged()
67 67 {
68 68 foreach(QGraphicsItem *item, childItems()) {
69 69 delete item;
70 70 }
71 71
72 72 m_bars.clear();
73 73 m_labels.clear();
74 74 m_layout.clear();
75 75
76 76 // Create new graphic items for bars
77 77 for (int c = 0; c < m_series->categoryCount(); c++) {
78 78 QString category = m_series->d_func()->categoryName(c);
79 79 for (int s = 0; s < m_series->barsetCount(); s++) {
80 80 QBarSet *set = m_series->d_func()->barsetAt(s);
81 81 Bar *bar = new Bar(set,category,this);
82 82 m_bars.append(bar);
83 83 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
84 84 connect(bar, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), m_series, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)));
85 85 connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool)));
86 86 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
87 87 m_layout.append(QRectF(0, 0, 0, 0));
88 88 }
89 89 }
90 90
91 91 // Create labels
92 92 for (int category = 0; category < m_series->categoryCount(); category++) {
93 93 for (int s = 0; s < m_series->barsetCount(); s++) {
94 94 QBarSet *set = m_series->d_func()->barsetAt(s);
95 95 BarLabel *value = new BarLabel(*set, this);
96 96 m_labels.append(value);
97 97 connect(set->d_ptr.data(),SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
98 98 }
99 99 }
100 100 }
101 101
102 102 QVector<QRectF> BarChartItem::calculateLayout()
103 103 {
104 104 QVector<QRectF> layout;
105 105
106 106 // Use temporary qreals for accurancy
107 107 qreal categoryCount = m_series->categoryCount();
108 108 qreal setCount = m_series->barsetCount();
109 109
110 110 // Domain:
111 111 qreal width = geometry().width();
112 112 qreal height = geometry().height();
113 113 qreal range = m_domainMaxY - m_domainMinY;
114 114 qreal scale = (height / range);
115 115 qreal categoryWidth = width / categoryCount;
116 116 qreal barWidth = categoryWidth / (setCount+1);
117 117
118 118 int itemIndex(0);
119 119 for (int category = 0; category < categoryCount; category++) {
120 120 qreal xPos = categoryWidth * category + barWidth / 2 + geometry().topLeft().x();
121 121 qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y();
122 122 for (int set = 0; set < setCount; set++) {
123 123 QBarSet* barSet = m_series->d_func()->barsetAt(set);
124 124
125 125 qreal barHeight = barSet->at(category) * scale;
126 126 Bar* bar = m_bars.at(itemIndex);
127 127
128 128 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
129 129 layout.append(rect);
130 130 bar->setPen(barSet->pen());
131 131 bar->setBrush(barSet->brush());
132 132
133 133 BarLabel* label = m_labels.at(itemIndex);
134 134
135 135 if (!qFuzzyIsNull(barSet->at(category))) {
136 136 label->setText(QString::number(barSet->at(category)));
137 137 } else {
138 138 label->setText(QString(""));
139 139 }
140 140
141 141 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 142 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 143 label->setFont(barSet->labelFont());
144 144
145 145 itemIndex++;
146 146 xPos += barWidth;
147 147 }
148 148 }
149 149 return layout;
150 150 }
151 151
152 152 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
153 153 {
154 154 if (animator())
155 155 animator()->updateLayout(this, m_layout, layout);
156 156 else
157 157 setLayout(layout);
158 158 }
159 159
160 160 void BarChartItem::setLayout(const QVector<QRectF> &layout)
161 161 {
162 162 m_layout = layout;
163 163
164 164 for (int i=0; i < m_bars.count(); i++)
165 165 m_bars.at(i)->setRect(layout.at(i));
166 166
167 167 update();
168 168 }
169 169 //handlers
170 170
171 171 void BarChartItem::handleModelChanged()
172 172 {
173 173 dataChanged();
174 174 }
175 175
176 176 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
177 177 {
178 178 m_domainMinX = minX;
179 179 m_domainMaxX = maxX;
180 180 m_domainMinY = minY;
181 181 m_domainMaxY = maxY;
182 182 handleLayoutChanged();
183 183 }
184 184
185 185 void BarChartItem::handleGeometryChanged(const QRectF &rect)
186 186 {
187 187 prepareGeometryChange();
188 188 m_rect = rect;
189 189 handleLayoutChanged();
190 190 m_layoutSet = true;
191 191 }
192 192
193 193 void BarChartItem::handleLayoutChanged()
194 194 {
195 195 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
196 196 // rect size zero.
197 197 return;
198 198 }
199 199 QVector<QRectF> layout = calculateLayout();
200 200 applyLayout(layout);
201 201 update();
202 202 }
203 203
204 204 #include "moc_barchartitem_p.cpp"
205 205
206 206 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,252 +1,252
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartdataset_p.h"
22 #include "qchartaxis.h"
23 #include "qchartaxis_p.h"
22 #include "qaxis.h"
23 #include "qaxis_p.h"
24 24 #include "qabstractseries_p.h"
25 25 #include "qbarseries.h"
26 26 #include "qstackedbarseries.h"
27 27 #include "qpercentbarseries.h"
28 28 #include "qpieseries.h"
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
33 m_axisX(new QChartAxis(this)),
34 m_axisY(new QChartAxis(this)),
33 m_axisX(new QAxis(this)),
34 m_axisY(new QAxis(this)),
35 35 m_domainIndex(0),
36 36 m_axisXInitialized(false)
37 37 {
38 38 }
39 39
40 40 ChartDataSet::~ChartDataSet()
41 41 {
42 42 }
43 43
44 void ChartDataSet::addSeries(QAbstractSeries* series, QChartAxis *axisY)
44 void ChartDataSet::addSeries(QAbstractSeries* series, QAxis *axisY)
45 45 {
46 46 if(axisY==0) axisY = m_axisY;
47 47
48 QChartAxis* axis = m_seriesAxisMap.value(series);
48 QAxis* axis = m_seriesAxisMap.value(series);
49 49
50 50 if(axis) {
51 51 qWarning() << "Can not add series. Series already on the chart";
52 52 return;
53 53 }
54 54
55 55 series->setParent(this); // take ownership
56 56 axisY->setParent(this); // take ownership
57 57
58 58 Domain* domain = m_axisDomainMap.value(axisY);
59 59
60 60 if(!domain) {
61 61 domain = new Domain(axisY);
62 62 QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
63 63 QObject::connect(axisX()->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
64 64 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
65 65 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX()->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
66 66 //initialize
67 67 m_axisDomainMap.insert(axisY,domain);
68 68 emit axisAdded(axisY,domain);
69 69 }
70 70
71 71 if(!m_axisXInitialized){
72 72 emit axisAdded(axisX(),domain);
73 73 m_axisXInitialized=true;
74 74 }
75 75
76 76 series->d_ptr->scaleDomain(*domain);
77 77
78 78 if(series->type() == QAbstractSeries::SeriesTypeBar
79 79 || series->type() == QAbstractSeries::SeriesTypeStackedBar
80 80 || series->type() == QAbstractSeries::SeriesTypePercentBar) {
81 81 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
82 82 setupCategories(barSeries);
83 83 }
84 84
85 85 if (series->type()== QAbstractSeries::SeriesTypePie && m_seriesAxisMap.count() == 0) {
86 86 axisX()->hide();
87 87 this->axisY()->hide();
88 88 }
89 89
90 90 m_seriesAxisMap.insert(series,axisY);
91 91
92 92 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
93 93
94 94 int key=0;
95 95 while (i.hasNext()) {
96 96 i.next();
97 97 if(i.key()!=key) {
98 98 break;
99 99 }
100 100 key++;
101 101 }
102 102
103 103 m_indexSeriesMap.insert(key,series);
104 104
105 105 emit seriesAdded(series,domain);
106 106
107 107 }
108 108
109 QChartAxis* ChartDataSet::removeSeries(QAbstractSeries* series)
109 QAxis* ChartDataSet::removeSeries(QAbstractSeries* series)
110 110 {
111 QChartAxis* axis = m_seriesAxisMap.value(series);
111 QAxis* axis = m_seriesAxisMap.value(series);
112 112
113 113 if(!axis){
114 114 qWarning()<<"Can not remove series. Series not found on the chart.";
115 115 return 0;
116 116 }
117 117
118 118 emit seriesRemoved(series);
119 119
120 120 m_seriesAxisMap.remove(series);
121 121 int key = seriesIndex(series);
122 122 Q_ASSERT(key!=-1);
123 123
124 124 m_indexSeriesMap.remove(key);
125 125 series->setParent(0);
126 126
127 QList<QChartAxis*> axes = m_seriesAxisMap.values();
127 QList<QAxis*> axes = m_seriesAxisMap.values();
128 128
129 129 int i = axes.indexOf(axis);
130 130
131 131 if(i==-1){
132 132 Domain* domain = m_axisDomainMap.take(axis);
133 133 emit axisRemoved(axis);
134 134 if(axis!=axisY()){
135 135 axis->setParent(0);
136 136 }
137 137 delete domain;
138 138 }
139 139
140 140 if(m_seriesAxisMap.values().size()==0)
141 141 {
142 142 m_axisXInitialized=false;
143 143 emit axisRemoved(axisX());
144 144 }
145 145
146 146 return axis;
147 147 }
148 148
149 149 void ChartDataSet::removeAllSeries()
150 150 {
151 151 QList<QAbstractSeries*> series = m_seriesAxisMap.keys();
152 QList<QChartAxis*> axes;
152 QList<QAxis*> axes;
153 153 foreach(QAbstractSeries *s , series) {
154 QChartAxis* axis = removeSeries(s);
154 QAxis* axis = removeSeries(s);
155 155 if(axis==axisY()) continue;
156 156 int i = axes.indexOf(axis);
157 157 if(i==-1){
158 158 axes<<axis;
159 159 }
160 160 }
161 161
162 162 Q_ASSERT(m_seriesAxisMap.count()==0);
163 163 Q_ASSERT(m_axisDomainMap.count()==0);
164 164
165 165 qDeleteAll(series);
166 166 qDeleteAll(axes);
167 167 }
168 168
169 169 void ChartDataSet::setupCategories(QBarSeries* series)
170 170 {
171 171 QChartAxisCategories* categories = axisX()->categories();
172 172 categories->clear();
173 173 categories->insert(series->categories());
174 174 }
175 175
176 176 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
177 177 {
178 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
178 QMapIterator<QAxis*, Domain*> i(m_axisDomainMap);
179 179 while (i.hasNext()) {
180 180 i.next();
181 181 i.value()->zoomIn(rect,size);
182 182 }
183 183 }
184 184
185 185 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
186 186 {
187 QMapIterator<QChartAxis*, Domain*> i(m_axisDomainMap);
187 QMapIterator<QAxis*, Domain*> i(m_axisDomainMap);
188 188 while (i.hasNext()) {
189 189 i.next();
190 190 i.value()->zoomOut(rect,size);
191 191 }
192 192 }
193 193
194 194 int ChartDataSet::seriesCount(QAbstractSeries::QSeriesType type)
195 195 {
196 196 int count=0;
197 QMapIterator<QAbstractSeries*, QChartAxis*> i(m_seriesAxisMap);
197 QMapIterator<QAbstractSeries*, QAxis*> i(m_seriesAxisMap);
198 198 while (i.hasNext()) {
199 199 i.next();
200 200 if(i.key()->type()==type) count++;
201 201 }
202 202 return count;
203 203 }
204 204
205 205 int ChartDataSet::seriesIndex(QAbstractSeries *series)
206 206 {
207 207 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
208 208 while (i.hasNext()) {
209 209 i.next();
210 210 if (i.value() == series)
211 211 return i.key();
212 212 }
213 213 return -1;
214 214 }
215 215
216 QChartAxis* ChartDataSet::axisY(QAbstractSeries *series) const
216 QAxis* ChartDataSet::axisY(QAbstractSeries *series) const
217 217 {
218 218 if(series == 0) return m_axisY;
219 219 return m_seriesAxisMap.value(series);
220 220 }
221 221
222 222 Domain* ChartDataSet::domain(QAbstractSeries *series) const
223 223 {
224 QChartAxis* axis = m_seriesAxisMap.value(series);
224 QAxis* axis = m_seriesAxisMap.value(series);
225 225 if(axis){
226 226 return m_axisDomainMap.value(axis);
227 227 }else
228 228 return 0;
229 229 }
230 230
231 Domain* ChartDataSet::domain(QChartAxis* axis) const
231 Domain* ChartDataSet::domain(QAxis* axis) const
232 232 {
233 233 if(!axis || axis==axisX()) {
234 234 return m_axisDomainMap.value(axisY());
235 235 }
236 236 else {
237 237 return m_axisDomainMap.value(axis);
238 238 }
239 239 }
240 240
241 241 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
242 242 {
243 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
243 QMapIterator<QAxis*, Domain*> i( m_axisDomainMap);
244 244 while (i.hasNext()) {
245 245 i.next();
246 246 i.value()->move(dx,dy,size);
247 247 }
248 248 }
249 249
250 250 #include "moc_chartdataset_p.cpp"
251 251
252 252 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,90 +1,90
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTDATASET_P_H
31 31 #define CHARTDATASET_P_H
32 32
33 33 #include "qabstractseries.h"
34 34 #include "domain_p.h"
35 35 #include <QVector>
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38
39 class QChartAxis;
39 class QAxis;
40 40 class QBarSeries;
41 41
42 42 class ChartDataSet : public QObject
43 43 {
44 44 Q_OBJECT
45 45 public:
46 46 ChartDataSet(QObject* parent=0);
47 47 virtual ~ChartDataSet();
48 48
49 void addSeries(QAbstractSeries* series,QChartAxis *axisY = 0);
50 QChartAxis* removeSeries(QAbstractSeries* series);
49 void addSeries(QAbstractSeries* series,QAxis *axisY = 0);
50 QAxis* removeSeries(QAbstractSeries* series);
51 51 void removeAllSeries();
52 52
53 53 void zoomInDomain(const QRectF& rect, const QSizeF& size);
54 54 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
55 55 void scrollDomain(int dx,int dy,const QSizeF& size);
56 56
57 57 int seriesCount(QAbstractSeries::QSeriesType type);
58 58 int seriesIndex(QAbstractSeries *series);
59 59
60 60 Domain* domain(QAbstractSeries* series) const;
61 Domain* domain(QChartAxis* axis) const;
61 Domain* domain(QAxis* axis) const;
62 62
63 QChartAxis* axisX() const { return m_axisX; }
64 QChartAxis* axisY(QAbstractSeries *series = 0) const;
63 QAxis* axisX() const { return m_axisX; }
64 QAxis* axisY(QAbstractSeries *series = 0) const;
65 65
66 66 Q_SIGNALS:
67 67 void seriesAdded(QAbstractSeries* series, Domain* domain);
68 68 void seriesRemoved(QAbstractSeries* series);
69 void axisAdded(QChartAxis* axis,Domain* domain);
70 void axisRemoved(QChartAxis* axis);
69 void axisAdded(QAxis* axis,Domain* domain);
70 void axisRemoved(QAxis* axis);
71 71
72 72 private:
73 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
73 QStringList createLabels(QAxis* axis,qreal min, qreal max);
74 74 void calculateDomain(QAbstractSeries* series,Domain* domain);
75 75 void setupCategories(QBarSeries* series);
76 76
77 77 private:
78 QMap<QAbstractSeries *, QChartAxis *> m_seriesAxisMap;
79 QMap<QChartAxis*, Domain *> m_axisDomainMap;
78 QMap<QAbstractSeries *, QAxis *> m_seriesAxisMap;
79 QMap<QAxis*, Domain *> m_axisDomainMap;
80 80 QMap<int, QAbstractSeries *> m_indexSeriesMap;
81 QChartAxis* m_axisX;
82 QChartAxis* m_axisY;
81 QAxis* m_axisX;
82 QAxis* m_axisY;
83 83
84 84 int m_domainIndex;
85 85 bool m_axisXInitialized;
86 86 };
87 87
88 88 QTCOMMERCIALCHART_END_NAMESPACE
89 89
90 90 #endif /* CHARTENGINE_P_H_ */
@@ -1,401 +1,402
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20 #include "chartpresenter_p.h"
21 21 #include "qchart.h"
22 22 #include "qchart_p.h"
23 #include "qchartaxis.h"
23 #include "qaxis.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "chartanimator_p.h"
27 27 #include "qabstractseries_p.h"
28 28 #include "qareaseries.h"
29 #include "axis_p.h"
29 #include "chartaxis_p.h"
30 30 #include "areachartitem_p.h"
31 #include "chartbackground_p.h"
31 32
32 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 34
34 35 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
35 36 m_chart(chart),
36 37 m_animator(0),
37 38 m_dataset(dataset),
38 39 m_chartTheme(0),
39 40 m_chartRect(QRectF(QPoint(0,0),m_chart->size())),
40 41 m_options(QChart::NoAnimation),
41 42 m_minLeftMargin(0),
42 43 m_minBottomMargin(0),
43 44 m_backgroundItem(0),
44 45 m_titleItem(0),
45 46 m_marginBig(60),
46 47 m_marginSmall(20),
47 48 m_marginTiny(10),
48 49 m_chartMargins(QRect(m_marginBig,m_marginBig,0,0))
49 50 {
50 51 }
51 52
52 53 ChartPresenter::~ChartPresenter()
53 54 {
54 55 delete m_chartTheme;
55 56 }
56 57
57 58 void ChartPresenter::setGeometry(const QRectF& rect)
58 59 {
59 60 m_rect = rect;
60 61 Q_ASSERT(m_rect.isValid());
61 62 updateLayout();
62 63 }
63 64
64 void ChartPresenter::setMinimumMarginWidth(Axis* axis, qreal width)
65 void ChartPresenter::setMinimumMarginWidth(ChartAxis* axis, qreal width)
65 66 {
66 67 switch(axis->axisType()){
67 case Axis::X_AXIS:
68 case ChartAxis::X_AXIS:
68 69 {
69 70 if(width>m_chartRect.width()+ m_chartMargins.left()) {
70 71 m_minLeftMargin= width - m_chartRect.width();
71 72 updateLayout();
72 73 }
73 74 break;
74 75 }
75 case Axis::Y_AXIS:
76 case ChartAxis::Y_AXIS:
76 77 {
77 78
78 79 if(m_minLeftMargin!=width){
79 80 m_minLeftMargin= width;
80 81 updateLayout();
81 82 }
82 83 break;
83 84 }
84 85
85 86 }
86 87 }
87 88
88 void ChartPresenter::setMinimumMarginHeight(Axis* axis, qreal height)
89 void ChartPresenter::setMinimumMarginHeight(ChartAxis* axis, qreal height)
89 90 {
90 91 switch(axis->axisType()){
91 case Axis::X_AXIS:
92 case ChartAxis::X_AXIS:
92 93 {
93 94 if(m_minBottomMargin!=height) {
94 95 m_minBottomMargin= height;
95 96 updateLayout();
96 97 }
97 98 break;
98 99 }
99 case Axis::Y_AXIS:
100 case ChartAxis::Y_AXIS:
100 101 {
101 102
102 103 if(height>m_chartMargins.bottom()+m_chartRect.height()){
103 104 m_minBottomMargin= height - m_chartRect.height();
104 105 updateLayout();
105 106 }
106 107 break;
107 108 }
108 109
109 110 }
110 111 }
111 112
112 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
113 void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain)
113 114 {
114 Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS);
115 ChartAxis* item = new ChartAxis(axis,this,axis==m_dataset->axisX()?ChartAxis::X_AXIS : ChartAxis::Y_AXIS);
115 116
116 117 if(m_options.testFlag(QChart::GridAxisAnimations)){
117 118 m_animator->addAnimation(item);
118 119 }
119 120
120 121 if(axis==m_dataset->axisX()){
121 122 m_chartTheme->decorate(axis,true);
122 123 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
123 124 //initialize
124 125 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
125 126
126 127 }
127 128 else{
128 129 m_chartTheme->decorate(axis,false);
129 130 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
130 131 //initialize
131 132 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
132 133 }
133 134
134 135 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
135 136 //initialize
136 137 item->handleGeometryChanged(m_chartRect);
137 138 m_axisItems.insert(axis, item);
138 139 }
139 140
140 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
141 void ChartPresenter::handleAxisRemoved(QAxis* axis)
141 142 {
142 Axis* item = m_axisItems.take(axis);
143 ChartAxis* item = m_axisItems.take(axis);
143 144 Q_ASSERT(item);
144 145 if(m_animator) m_animator->removeAnimation(item);
145 146 delete item;
146 147 }
147 148
148 149
149 150 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
150 151 {
151 152 Chart *item = series->d_ptr->createGraphics(this);
152 153 Q_ASSERT(item);
153 154 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
154 155 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
155 156 //initialize
156 157 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
157 158 if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect);
158 159 m_chartItems.insert(series,item);
159 160 }
160 161
161 162 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
162 163 {
163 164 Chart* item = m_chartItems.take(series);
164 165 Q_ASSERT(item);
165 166 if(m_animator) {
166 167 //small hack to handle area animations
167 168 if(series->type() == QAbstractSeries::SeriesTypeArea){
168 169 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
169 170 AreaChartItem* area = static_cast<AreaChartItem*>(item);
170 171 m_animator->removeAnimation(area->upperLineItem());
171 172 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
172 173 }else
173 174 m_animator->removeAnimation(item);
174 175 }
175 176 delete item;
176 177 }
177 178
178 179 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
179 180 {
180 181 if(m_chartTheme && m_chartTheme->id() == theme) return;
181 182 delete m_chartTheme;
182 183 m_chartTheme = ChartTheme::createTheme(theme);
183 184 m_chartTheme->setForced(force);
184 185 m_chartTheme->decorate(m_chart);
185 186 m_chartTheme->decorate(m_chart->legend());
186 187 resetAllElements();
187 188 }
188 189
189 190 QChart::ChartTheme ChartPresenter::theme()
190 191 {
191 192 return m_chartTheme->id();
192 193 }
193 194
194 195 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
195 196 {
196 197 if(m_options!=options) {
197 198
198 199 m_options=options;
199 200
200 201 if(m_options!=QChart::NoAnimation && !m_animator) {
201 202 m_animator= new ChartAnimator(this);
202 203 }
203 204 resetAllElements();
204 205 }
205 206
206 207 }
207 208
208 209 void ChartPresenter::resetAllElements()
209 210 {
210 QList<QChartAxis *> axisList = m_axisItems.uniqueKeys();
211 QList<QAxis *> axisList = m_axisItems.uniqueKeys();
211 212 QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys();
212 213
213 foreach(QChartAxis *axis, axisList) {
214 foreach(QAxis *axis, axisList) {
214 215 handleAxisRemoved(axis);
215 216 handleAxisAdded(axis,m_dataset->domain(axis));
216 217 }
217 218 foreach(QAbstractSeries *series, seriesList) {
218 219 handleSeriesRemoved(series);
219 220 handleSeriesAdded(series,m_dataset->domain(series));
220 221 }
221 222 }
222 223
223 224 void ChartPresenter::zoomIn()
224 225 {
225 226 QRectF rect = chartGeometry();
226 227 rect.setWidth(rect.width()/2);
227 228 rect.setHeight(rect.height()/2);
228 229 rect.moveCenter(chartGeometry().center());
229 230 zoomIn(rect);
230 231 }
231 232
232 233 void ChartPresenter::zoomIn(const QRectF& rect)
233 234 {
234 235 QRectF r = rect.normalized();
235 236 r.translate(-m_chartMargins.topLeft());
236 237 if(m_animator) {
237 238
238 239 QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
239 240 m_animator->setState(ChartAnimator::ZoomInState,point);
240 241 }
241 242 m_dataset->zoomInDomain(r,chartGeometry().size());
242 243 if(m_animator) {
243 244 m_animator->setState(ChartAnimator::ShowState);
244 245 }
245 246 }
246 247
247 248 void ChartPresenter::zoomOut()
248 249 {
249 250 if(m_animator)
250 251 {
251 252 m_animator->setState(ChartAnimator::ZoomOutState);
252 253 }
253 254
254 255 QSizeF size = chartGeometry().size();
255 256 QRectF rect = chartGeometry();
256 257 rect.translate(-m_chartMargins.topLeft());
257 258 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
258 259 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
259 260
260 261 if(m_animator){
261 262 m_animator->setState(ChartAnimator::ShowState);
262 263 }
263 264 }
264 265
265 266 void ChartPresenter::scroll(int dx,int dy)
266 267 {
267 268 if(m_animator){
268 269 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
269 270 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
270 271 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
271 272 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
272 273 }
273 274
274 275 m_dataset->scrollDomain(dx,dy,chartGeometry().size());
275 276
276 277 if(m_animator){
277 278 m_animator->setState(ChartAnimator::ShowState);
278 279 }
279 280 }
280 281
281 282 QChart::AnimationOptions ChartPresenter::animationOptions() const
282 283 {
283 284 return m_options;
284 285 }
285 286
286 287 void ChartPresenter::updateLayout()
287 288 {
288 289 if (!m_rect.isValid()) return;
289 290
290 291 // recalculate title size
291 292
292 293 QSize titleSize;
293 294 int titlePadding=0;
294 295
295 296 if (m_titleItem) {
296 297 titleSize= m_titleItem->boundingRect().size().toSize();
297 298 }
298 299
299 300 //defaults
300 301 m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig));
301 302 titlePadding = m_chartMargins.top()/2;
302 303
303 304 QLegend* legend = m_chart->d_ptr->m_legend;
304 305
305 306 // recalculate legend position
306 307 if (legend->isAttachedToChart() && legend->isEnabled()) {
307 308
308 309 QRect legendRect;
309 310
310 311 // Reserve some space for legend
311 312 switch (legend->alignment()) {
312 313
313 314 case QLegend::AlignmentTop: {
314 315 int ledgendSize = legend->minHeight();
315 316 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
316 317 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
317 318 m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny));
318 319 titlePadding = m_marginTiny + m_marginTiny;
319 320 break;
320 321 }
321 322 case QLegend::AlignmentBottom: {
322 323 int ledgendSize = legend->minHeight();
323 324 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
324 325 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
325 326 m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall));
326 327 titlePadding = m_chartMargins.top()/2;
327 328 break;
328 329 }
329 330 case QLegend::AlignmentLeft: {
330 331 int ledgendSize = legend->minWidth();
331 332 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
332 333 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
333 334 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom()));
334 335 titlePadding = m_chartMargins.top()/2;
335 336 break;
336 337 }
337 338 case QLegend::AlignmentRight: {
338 339 int ledgendSize = legend->minWidth();
339 340 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
340 341 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
341 342 m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom()));
342 343 titlePadding = m_chartMargins.top()/2;
343 344 break;
344 345 }
345 346 default: {
346 347 break;
347 348 }
348 349 }
349 350 }
350 351
351 352 if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom()))
352 353 {
353 354 m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom()));
354 355 return;
355 356 }
356 357
357 358
358 359 // recalculate title position
359 360 if (m_titleItem) {
360 361 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
361 362 m_titleItem->setPos(center.x(),titlePadding);
362 363 }
363 364
364 365 //recalculate background gradient
365 366 if (m_backgroundItem) {
366 367 m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
367 368 }
368 369
369 370
370 371 QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom());
371 372
372 373 legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
373 374
374 375 if(m_chartRect!=chartRect){
375 376 m_chartRect=chartRect;
376 377 emit geometryChanged(m_chartRect);
377 378 }
378 379
379 380
380 381 }
381 382
382 383 void ChartPresenter::createChartBackgroundItem()
383 384 {
384 385 if (!m_backgroundItem) {
385 386 m_backgroundItem = new ChartBackground(rootItem());
386 387 m_backgroundItem->setPen(Qt::NoPen);
387 388 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
388 389 }
389 390 }
390 391
391 392 void ChartPresenter::createChartTitleItem()
392 393 {
393 394 if (!m_titleItem) {
394 395 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
395 396 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
396 397 }
397 398 }
398 399
399 400 #include "moc_chartpresenter_p.cpp"
400 401
401 402 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,125 +1,124
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef CHARTPRESENTER_H
22 22 #define CHARTPRESENTER_H
23 23
24 24 #include "qchartglobal.h"
25 #include "chartbackground_p.h" //TODO fix me
26 25 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
27 #include "qchartaxis.h"
28 26 #include <QRectF>
29 27
30 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 29
32 30 class Chart;
33 31 class QAbstractSeries;
34 32 class ChartDataSet;
35 33 class Domain;
36 class Axis;
34 class ChartAxis;
37 35 class ChartTheme;
38 36 class ChartAnimator;
37 class ChartBackground;
39 38
40 39 class ChartPresenter: public QObject
41 40 {
42 41 Q_OBJECT
43 42 public:
44 43 enum ZValues {
45 44 BackgroundZValue = -1,
46 45 ShadesZValue,
47 46 GridZValue,
48 47 LineChartZValue,
49 48 BarSeriesZValue,
50 49 ScatterSeriesZValue,
51 50 PieSeriesZValue,
52 51 AxisZValue,
53 52 LegendZValue
54 53 };
55 54
56 55 ChartPresenter(QChart* chart,ChartDataSet *dataset);
57 56 virtual ~ChartPresenter();
58 57
59 58 ChartAnimator* animator() const { return m_animator; }
60 59 ChartTheme *chartTheme() const { return m_chartTheme; }
61 60 ChartDataSet *dataSet() const { return m_dataset; }
62 61 QGraphicsItem* rootItem() const { return m_chart; }
63 62
64 63 void setTheme(QChart::ChartTheme theme,bool force = true);
65 64 QChart::ChartTheme theme();
66 65
67 66 void setAnimationOptions(QChart::AnimationOptions options);
68 67 QChart::AnimationOptions animationOptions() const;
69 68
70 69 void zoomIn();
71 70 void zoomIn(const QRectF& rect);
72 71 void zoomOut();
73 72 void scroll(int dx,int dy);
74 73
75 74 void setGeometry(const QRectF& rect);
76 75 QRectF chartGeometry() const { return m_chartRect; }
77 76
78 void setMinimumMarginHeight(Axis* axis, qreal height);
79 void setMinimumMarginWidth(Axis* axis, qreal width);
77 void setMinimumMarginHeight(ChartAxis* axis, qreal height);
78 void setMinimumMarginWidth(ChartAxis* axis, qreal width);
80 79 qreal minimumLeftMargin() const { return m_minLeftMargin; }
81 80 qreal minimumBottomMargin() const { return m_minBottomMargin; }
82 81
83 82 public: //TODO: fix me
84 83 void resetAllElements();
85 84 void createChartBackgroundItem();
86 85 void createChartTitleItem();
87 86 QRectF margins() const { return m_chartMargins;}
88 87
89 88 public Q_SLOTS:
90 89 void handleSeriesAdded(QAbstractSeries* series,Domain* domain);
91 90 void handleSeriesRemoved(QAbstractSeries* series);
92 void handleAxisAdded(QChartAxis* axis,Domain* domain);
93 void handleAxisRemoved(QChartAxis* axis);
91 void handleAxisAdded(QAxis* axis,Domain* domain);
92 void handleAxisRemoved(QAxis* axis);
94 93 void updateLayout();
95 94
96 95 Q_SIGNALS:
97 96 void geometryChanged(const QRectF& rect);
98 97
99 98
100 99 private:
101 100 QChart* m_chart;
102 101 ChartAnimator* m_animator;
103 102 ChartDataSet* m_dataset;
104 103 ChartTheme *m_chartTheme;
105 104 QMap<QAbstractSeries *, Chart *> m_chartItems;
106 QMap<QChartAxis *, Axis *> m_axisItems;
105 QMap<QAxis *, ChartAxis *> m_axisItems;
107 106 QRectF m_rect;
108 107 QRectF m_chartRect;
109 108 QChart::AnimationOptions m_options;
110 109 qreal m_minLeftMargin;
111 110 qreal m_minBottomMargin;
112 111 public: //TODO: fixme
113 112 ChartBackground* m_backgroundItem;
114 113 QGraphicsSimpleTextItem* m_titleItem;
115 114 int m_marginBig;
116 115 int m_marginSmall;
117 116 int m_marginTiny;
118 117 QRectF m_chartMargins;
119 118 QRectF m_legendMargins;
120 119
121 120 };
122 121
123 122 QTCOMMERCIALCHART_END_NAMESPACE
124 123
125 124 #endif /* CHARTPRESENTER_H_ */
@@ -1,400 +1,400
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "charttheme_p.h"
22 22 #include "qchart.h"
23 23 #include "qchart_p.h"
24 24 #include "qchartview.h"
25 25 #include "qlegend.h"
26 #include "qchartaxis.h"
26 #include "qaxis.h"
27 27 #include <QTime>
28 28
29 29 //series
30 30 #include "qbarset.h"
31 31 #include "qbarseries.h"
32 32 #include "qstackedbarseries.h"
33 33 #include "qpercentbarseries.h"
34 34 #include "qlineseries.h"
35 35 #include "qareaseries.h"
36 36 #include "qscatterseries.h"
37 37 #include "qpieseries.h"
38 38 #include "qpieslice.h"
39 39 #include "qsplineseries.h"
40 40
41 41 //items
42 #include "axis_p.h"
42 #include "chartaxis_p.h"
43 43 #include "barchartitem_p.h"
44 44 #include "stackedbarchartitem_p.h"
45 45 #include "percentbarchartitem_p.h"
46 46 #include "linechartitem_p.h"
47 47 #include "areachartitem_p.h"
48 48 #include "scatterchartitem_p.h"
49 49 #include "piechartitem_p.h"
50 50 #include "splinechartitem_p.h"
51 51
52 52 //themes
53 53 #include "chartthemesystem_p.h"
54 54 #include "chartthemelight_p.h"
55 55 #include "chartthemebluecerulean_p.h"
56 56 #include "chartthemedark_p.h"
57 57 #include "chartthemebrownsand_p.h"
58 58 #include "chartthemebluencs_p.h"
59 59 #include "chartthemehighcontrast_p.h"
60 60 #include "chartthemeblueicy_p.h"
61 61
62 62 QTCOMMERCIALCHART_BEGIN_NAMESPACE
63 63
64 64 ChartTheme::ChartTheme(QChart::ChartTheme id) :
65 65 m_masterFont(QFont("arial", 14)),
66 66 m_labelFont(QFont("arial", 10)),
67 67 m_titleBrush(QColor(QRgb(0x000000))),
68 68 m_axisLinePen(QPen(QRgb(0x000000))),
69 69 m_axisLabelBrush(QColor(QRgb(0x000000))),
70 70 m_backgroundShadesPen(Qt::NoPen),
71 71 m_backgroundShadesBrush(Qt::NoBrush),
72 72 m_backgroundShades(BackgroundShadesNone),
73 73 m_backgroundDropShadowEnabled(false),
74 74 m_gridLinePen(QPen(QRgb(0x000000))),
75 75 m_force(false)
76 76 {
77 77 m_id = id;
78 78 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
79 79 }
80 80
81 81
82 82 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
83 83 {
84 84 switch(theme) {
85 85 case QChart::ChartThemeLight:
86 86 return new ChartThemeLight();
87 87 case QChart::ChartThemeBlueCerulean:
88 88 return new ChartThemeBlueCerulean();
89 89 case QChart::ChartThemeDark:
90 90 return new ChartThemeDark();
91 91 case QChart::ChartThemeBrownSand:
92 92 return new ChartThemeBrownSand();
93 93 case QChart::ChartThemeBlueNcs:
94 94 return new ChartThemeBlueNcs();
95 95 case QChart::ChartThemeHighContrast:
96 96 return new ChartThemeHighContrast();
97 97 case QChart::ChartThemeBlueIcy:
98 98 return new ChartThemeBlueIcy();
99 99 default:
100 100 return new ChartThemeSystem();
101 101 }
102 102 }
103 103
104 104 void ChartTheme::decorate(QChart *chart)
105 105 {
106 106 QBrush brush;
107 107
108 108 if(brush == chart->backgroundBrush() || m_force)
109 109 chart->setBackgroundBrush(m_chartBackgroundGradient);
110 110 chart->setTitleFont(m_masterFont);
111 111 chart->setTitleBrush(m_titleBrush);
112 112 chart->setBackgroundDropShadowEnabled(m_backgroundDropShadowEnabled);
113 113 }
114 114
115 115 void ChartTheme::decorate(QLegend *legend)
116 116 {
117 117 QPen pen;
118 118 QBrush brush;
119 119
120 120 if (pen == legend->pen() || m_force){
121 121 legend->setPen(Qt::NoPen);
122 122 }
123 123
124 124 if (brush == legend->brush() || m_force) {
125 125 legend->setBrush(m_chartBackgroundGradient);
126 126 }
127 127 }
128 128
129 129 void ChartTheme::decorate(QAreaSeries *series, int index)
130 130 {
131 131 QPen pen;
132 132 QBrush brush;
133 133
134 134 if (pen == series->pen() || m_force){
135 135 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
136 136 pen.setWidthF(2);
137 137 series->setPen(pen);
138 138 }
139 139
140 140 if (brush == series->brush() || m_force) {
141 141 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
142 142 series->setBrush(brush);
143 143 }
144 144 }
145 145
146 146
147 147 void ChartTheme::decorate(QLineSeries *series,int index)
148 148 {
149 149 QPen pen;
150 150 if(pen == series->pen() || m_force ){
151 151 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
152 152 pen.setWidthF(2);
153 153 series->setPen(pen);
154 154 }
155 155 }
156 156
157 157 void ChartTheme::decorate(QBarSeries *series, int index)
158 158 {
159 159 QBrush brush;
160 160 QPen pen;
161 161 QList<QBarSet *> sets = series->barSets();
162 162
163 163 qreal takeAtPos = 0.5;
164 164 qreal step = 0.2;
165 165 if (sets.count() > 1 ) {
166 166 step = 1.0 / (qreal) sets.count();
167 167 if (sets.count() % m_seriesGradients.count())
168 168 step *= m_seriesGradients.count();
169 169 else
170 170 step *= (m_seriesGradients.count() - 1);
171 171 }
172 172
173 173 for (int i(0); i < sets.count(); i++) {
174 174 int colorIndex = (index + i) % m_seriesGradients.count();
175 175 if (i > 0 && i % m_seriesGradients.count() == 0) {
176 176 // There is no dedicated base color for each sets, generate more colors
177 177 takeAtPos += step;
178 178 if (takeAtPos == 1.0)
179 179 takeAtPos += step;
180 180 takeAtPos -= (int) takeAtPos;
181 181 }
182 182 if (brush == sets.at(i)->brush() || m_force )
183 183 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
184 184
185 185 // Pick label color from the opposite end of the gradient.
186 186 // 0.3 as a boundary seems to work well.
187 187 if (takeAtPos < 0.3)
188 188 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
189 189 else
190 190 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
191 191
192 192 if (pen == sets.at(i)->pen() || m_force) {
193 193 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
194 194 sets.at(i)->setPen(c);
195 195 }
196 196 }
197 197 }
198 198
199 199 void ChartTheme::decorate(QScatterSeries *series, int index)
200 200 {
201 201 QPen pen;
202 202 QBrush brush;
203 203
204 204 if (pen == series->pen() || m_force) {
205 205 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
206 206 pen.setWidthF(2);
207 207 series->setPen(pen);
208 208 }
209 209
210 210 if (brush == series->brush() || m_force) {
211 211 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
212 212 series->setBrush(brush);
213 213 }
214 214 }
215 215
216 216 void ChartTheme::decorate(QPieSeries *series, int index)
217 217 {
218 218
219 219 for (int i(0); i < series->slices().count(); i++) {
220 220
221 221 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
222 222
223 223 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
224 224 qreal pos = (qreal) (i + 1) / (qreal) series->count();
225 225 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
226 226
227 227 QPieSlice *s = series->slices().at(i);
228 228 PieSliceData data = PieSliceData::data(s);
229 229
230 230 if (data.m_slicePen.isThemed() || m_force) {
231 231 data.m_slicePen = penColor;
232 232 data.m_slicePen.setThemed(true);
233 233 }
234 234
235 235 if (data.m_sliceBrush.isThemed() || m_force) {
236 236 data.m_sliceBrush = brushColor;
237 237 data.m_sliceBrush.setThemed(true);
238 238 }
239 239
240 240 if (data.m_labelPen.isThemed() || m_force) {
241 241 data.m_labelPen = QPen(m_titleBrush.color());
242 242 data.m_labelPen.setThemed(true);
243 243 }
244 244
245 245 if (data.m_labelFont.isThemed() || m_force) {
246 246 data.m_labelFont = m_labelFont;
247 247 data.m_labelFont.setThemed(true);
248 248 }
249 249
250 250 if (PieSliceData::data(s) != data) {
251 251 PieSliceData::data(s) = data;
252 252 emit PieSliceData::data(s).emitChangedSignal(s);
253 253 }
254 254 }
255 255 }
256 256
257 257 void ChartTheme::decorate(QSplineSeries *series, int index)
258 258 {
259 259 QPen pen;
260 260 if(pen == series->pen() || m_force){
261 261 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
262 262 pen.setWidthF(2);
263 263 series->setPen(pen);
264 264 }
265 265 }
266 266
267 void ChartTheme::decorate(QChartAxis *axis,bool axisX)
267 void ChartTheme::decorate(QAxis *axis,bool axisX)
268 268 {
269 269 QPen pen;
270 270 QBrush brush;
271 271 QFont font;
272 272
273 273 if (axis->isAxisVisible()) {
274 274
275 275 if(brush == axis->labelsBrush() || m_force){
276 276 axis->setLabelsBrush(m_axisLabelBrush);
277 277 }
278 278 if(pen == axis->labelsPen() || m_force){
279 279 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
280 280 }
281 281
282 282
283 283 if (axis->shadesVisible() || m_force) {
284 284
285 285 if(brush == axis->shadesBrush() || m_force){
286 286 axis->setShadesBrush(m_backgroundShadesBrush);
287 287 }
288 288
289 289 if(pen == axis->shadesPen() || m_force){
290 290 axis->setShadesPen(m_backgroundShadesPen);
291 291 }
292 292
293 293 if( m_force && (m_backgroundShades == BackgroundShadesBoth
294 294 || (m_backgroundShades == BackgroundShadesVertical && axisX)
295 295 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
296 296 axis->setShadesVisible(true);
297 297
298 298 }
299 299 }
300 300
301 301 if(pen == axis->axisPen() || m_force){
302 302 axis->setAxisPen(m_axisLinePen);
303 303 }
304 304
305 305 if(pen == axis->gridLinePen() || m_force){
306 306 axis->setGridLinePen(m_gridLinePen);
307 307 }
308 308
309 309 if(font == axis->labelsFont() || m_force){
310 310 axis->setLabelsFont(m_labelFont);
311 311 }
312 312 }
313 313 }
314 314
315 315 void ChartTheme::generateSeriesGradients()
316 316 {
317 317 // Generate gradients in HSV color space
318 318 foreach (const QColor& color, m_seriesColors) {
319 319 QLinearGradient g;
320 320 qreal h = color.hsvHueF();
321 321 qreal s = color.hsvSaturationF();
322 322
323 323 // TODO: tune the algorithm to give nice results with most base colors defined in
324 324 // most themes. The rest of the gradients we can define manually in theme specific
325 325 // implementation.
326 326 QColor start = color;
327 327 start.setHsvF(h, 0.0, 1.0);
328 328 g.setColorAt(0.0, start);
329 329
330 330 g.setColorAt(0.5, color);
331 331
332 332 QColor end = color;
333 333 end.setHsvF(h, s, 0.25);
334 334 g.setColorAt(1.0, end);
335 335
336 336 m_seriesGradients << g;
337 337 }
338 338 }
339 339
340 340
341 341 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
342 342 {
343 343 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
344 344 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
345 345 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
346 346 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
347 347 QColor c;
348 348 c.setRgbF(r, g, b);
349 349 return c;
350 350 }
351 351
352 352 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
353 353 {
354 354 Q_ASSERT(pos >= 0 && pos <= 1.0);
355 355
356 356 // another possibility:
357 357 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
358 358
359 359 QGradientStops stops = gradient.stops();
360 360 int count = stops.count();
361 361
362 362 // find previous stop relative to position
363 363 QGradientStop prev = stops.first();
364 364 for (int i = 0; i < count; i++) {
365 365 QGradientStop stop = stops.at(i);
366 366 if (pos > stop.first)
367 367 prev = stop;
368 368
369 369 // given position is actually a stop position?
370 370 if (pos == stop.first) {
371 371 //qDebug() << "stop color" << pos;
372 372 return stop.second;
373 373 }
374 374 }
375 375
376 376 // find next stop relative to position
377 377 QGradientStop next = stops.last();
378 378 for (int i = count - 1; i >= 0; i--) {
379 379 QGradientStop stop = stops.at(i);
380 380 if (pos < stop.first)
381 381 next = stop;
382 382 }
383 383
384 384 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
385 385
386 386 qreal range = next.first - prev.first;
387 387 qreal posDelta = pos - prev.first;
388 388 qreal relativePos = posDelta / range;
389 389
390 390 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
391 391
392 392 return colorAt(prev.second, next.second, relativePos);
393 393 }
394 394
395 395 void ChartTheme::setForced(bool enabled)
396 396 {
397 397 m_force=enabled;
398 398 }
399 399
400 400 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,102 +1,102
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef CHARTTHEME_H
22 22 #define CHARTTHEME_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qchart.h"
26 26 #include <QColor>
27 27 #include <QGradientStops>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class ChartItem;
32 32 class LineChartItem;
33 33 class QLineSeries;
34 34 class BarChartItem;
35 35 class QBarSeries;
36 36 class StackedBarChartItem;
37 37 class QStackedBarSeries;
38 38 class QPercentBarSeries;
39 39 class PercentBarChartItem;
40 40 class QScatterSeries;
41 41 class ScatterChartItem;
42 42 class PieChartItem;
43 43 class QPieSeries;
44 44 class SplineChartItem;
45 45 class QSplineSeries;
46 46 class AreaChartItem;
47 47 class QAreaSeries;
48 48
49 49 class ChartTheme
50 50 {
51 51 public:
52 52 enum BackgroundShadesMode {
53 53 BackgroundShadesNone = 0,
54 54 BackgroundShadesVertical,
55 55 BackgroundShadesHorizontal,
56 56 BackgroundShadesBoth
57 57 };
58 58
59 59 protected:
60 60 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeLight);
61 61 public:
62 62 static ChartTheme *createTheme(QChart::ChartTheme theme);
63 63 QChart::ChartTheme id() const {return m_id;}
64 64 void decorate(QChart *chart);
65 65 void decorate(QLegend *legend);
66 66 void decorate(QBarSeries *series, int index);
67 67 void decorate(QLineSeries *series, int index);
68 68 void decorate(QAreaSeries *series, int index);
69 69 void decorate(QScatterSeries *series, int index);
70 70 void decorate(QPieSeries *series, int index);
71 71 void decorate(QSplineSeries *series, int index);
72 void decorate(QChartAxis *axis, bool axisX);
72 void decorate(QAxis *axis, bool axisX);
73 73 void setForced(bool enabled);
74 74 bool isForced() { return m_force; }
75 75
76 76 public: // utils
77 77 void generateSeriesGradients();
78 78 static QColor colorAt(const QColor &start, const QColor &end, qreal pos);
79 79 static QColor colorAt(const QGradient &gradient, qreal pos);
80 80
81 81 protected:
82 82 QChart::ChartTheme m_id;
83 83 QList<QColor> m_seriesColors;
84 84 QList<QGradient> m_seriesGradients;
85 85 QLinearGradient m_chartBackgroundGradient;
86 86
87 87 QFont m_masterFont;
88 88 QFont m_labelFont;
89 89 QBrush m_titleBrush;
90 90 QPen m_axisLinePen;
91 91 QBrush m_axisLabelBrush;
92 92 QPen m_backgroundShadesPen;
93 93 QBrush m_backgroundShadesBrush;
94 94 BackgroundShadesMode m_backgroundShades;
95 95 bool m_backgroundDropShadowEnabled;
96 96 QPen m_gridLinePen;
97 97 bool m_force;
98 98 };
99 99
100 100 QTCOMMERCIALCHART_END_NAMESPACE
101 101
102 102 #endif // CHARTTHEME_H
@@ -1,434 +1,436
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qchart.h"
22 22 #include "qchart_p.h"
23 23 #include "legendscroller_p.h"
24 24 #include "qlegend_p.h"
25 #include "chartbackground_p.h"
26 #include "qaxis.h"
25 27 #include <QGraphicsScene>
26 28 #include <QGraphicsSceneResizeEvent>
27 29
28 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 31
30 32 /*!
31 33 \enum QChart::ChartTheme
32 34
33 35 This enum describes the theme used by the chart.
34 36
35 37 \value ChartThemeLight The default theme
36 38 \value ChartThemeBlueCerulean
37 39 \value ChartThemeDark
38 40 \value ChartThemeBrownSand
39 41 \value ChartThemeBlueNcs
40 42 \value ChartThemeHighContrast
41 43 \value ChartThemeBlueIcy
42 44 */
43 45
44 46 /*!
45 47 \enum QChart::AnimationOption
46 48
47 49 For enabling/disabling animations. Defaults to NoAnimation.
48 50
49 51 \value NoAnimation
50 52 \value GridAxisAnimations
51 53 \value SeriesAnimations
52 54 \value AllAnimations
53 55 */
54 56
55 57 /*!
56 58 \class QChart
57 59 \brief QtCommercial chart API.
58 60
59 61 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
60 62 representation of different types of QChartSeries and other chart related objects like
61 63 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
62 64 convenience class QChartView instead of QChart.
63 65 \sa QChartView
64 66 */
65 67
66 68 /*!
67 69 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
68 70 */
69 71 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
70 72 d_ptr(new QChartPrivate())
71 73 {
72 74 d_ptr->m_dataset = new ChartDataSet(this);
73 75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
74 76 d_ptr->createConnections();
75 77 d_ptr->m_legend = new LegendScroller(this);
76 78 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
77 79 }
78 80
79 81 /*!
80 82 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
81 83 */
82 84 QChart::~QChart()
83 85 {
84 86 //delete first presenter , since this is a root of all the graphical items
85 87 delete d_ptr->m_presenter;
86 88 d_ptr->m_presenter=0;
87 89 }
88 90
89 91 /*!
90 92 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
91 93 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
92 94 the y axis).
93 95 */
94 void QChart::addSeries(QAbstractSeries *series, QChartAxis *axisY)
96 void QChart::addSeries(QAbstractSeries *series, QAxis *axisY)
95 97 {
96 98 Q_ASSERT(series);
97 99 d_ptr->m_dataset->addSeries(series, axisY);
98 100 }
99 101
100 102 /*!
101 103 Removes the \a series specified in a perameter from the QChartView.
102 104 It releses its ownership of the specified QChartSeries object.
103 105 It does not delete the pointed QChartSeries data object
104 106 \sa addSeries(), removeAllSeries()
105 107 */
106 108 void QChart::removeSeries(QAbstractSeries *series)
107 109 {
108 110 Q_ASSERT(series);
109 111 d_ptr->m_dataset->removeSeries(series);
110 112 }
111 113
112 114 /*!
113 115 Removes all the QChartSeries that have been added to the QChartView
114 116 It also deletes the pointed QChartSeries data objects
115 117 \sa addSeries(), removeSeries()
116 118 */
117 119 void QChart::removeAllSeries()
118 120 {
119 121 d_ptr->m_dataset->removeAllSeries();
120 122 }
121 123
122 124 /*!
123 125 Sets the \a brush that is used for painting the background of the chart area.
124 126 */
125 127 void QChart::setBackgroundBrush(const QBrush& brush)
126 128 {
127 129 //TODO: refactor me
128 130 d_ptr->m_presenter->createChartBackgroundItem();
129 131 d_ptr->m_presenter->m_backgroundItem->setBrush(brush);
130 132 d_ptr->m_presenter->m_backgroundItem->update();
131 133 }
132 134
133 135 /*!
134 136 Gets the brush that is used for painting the background of the chart area.
135 137 */
136 138 QBrush QChart::backgroundBrush() const
137 139 {
138 140 //TODO: refactor me
139 141 if (!d_ptr->m_presenter->m_backgroundItem) return QBrush();
140 142 return (d_ptr->m_presenter->m_backgroundItem)->brush();
141 143 }
142 144
143 145 /*!
144 146 Sets the \a pen that is used for painting the background of the chart area.
145 147 */
146 148 void QChart::setBackgroundPen(const QPen& pen)
147 149 {
148 150 //TODO: refactor me
149 151 d_ptr->m_presenter->createChartBackgroundItem();
150 152 d_ptr->m_presenter->m_backgroundItem->setPen(pen);
151 153 d_ptr->m_presenter->m_backgroundItem->update();
152 154 }
153 155
154 156 /*!
155 157 Gets the pen that is used for painting the background of the chart area.
156 158 */
157 159 QPen QChart::backgroundPen() const
158 160 {
159 161 //TODO: refactor me
160 162 if (!d_ptr->m_presenter->m_backgroundItem) return QPen();
161 163 return d_ptr->m_presenter->m_backgroundItem->pen();
162 164 }
163 165
164 166 /*!
165 167 Sets the chart \a title. The description text that is drawn above the chart.
166 168 */
167 169 void QChart::setTitle(const QString& title)
168 170 {
169 171 //TODO: refactor me
170 172 d_ptr->m_presenter->createChartTitleItem();
171 173 d_ptr->m_presenter->m_titleItem->setText(title);
172 174 d_ptr->m_presenter->updateLayout();
173 175 }
174 176
175 177 /*!
176 178 Returns the chart title. The description text that is drawn above the chart.
177 179 */
178 180 QString QChart::title() const
179 181 {
180 182 //TODO: refactor me
181 183 if (d_ptr->m_presenter->m_titleItem)
182 184 return d_ptr->m_presenter->m_titleItem->text();
183 185 else
184 186 return QString();
185 187 }
186 188
187 189 /*!
188 190 Sets the \a font that is used for drawing the chart description text that is rendered above the chart.
189 191 */
190 192 void QChart::setTitleFont(const QFont& font)
191 193 {
192 194 //TODO: refactor me
193 195 d_ptr->m_presenter->createChartTitleItem();
194 196 d_ptr->m_presenter->m_titleItem->setFont(font);
195 197 d_ptr->m_presenter->updateLayout();
196 198 }
197 199
198 200 /*!
199 201 Gets the font that is used for drawing the chart description text that is rendered above the chart.
200 202 */
201 203 QFont QChart::titleFont() const
202 204 {
203 205 if (d_ptr->m_presenter->m_titleItem)
204 206 return d_ptr->m_presenter->m_titleItem->font();
205 207 else
206 208 return QFont();
207 209 }
208 210
209 211 /*!
210 212 Sets the \a brush used for rendering the title text.
211 213 */
212 214 void QChart::setTitleBrush(const QBrush &brush)
213 215 {
214 216 //TODO: refactor me
215 217 d_ptr->m_presenter->createChartTitleItem();
216 218 d_ptr->m_presenter->m_titleItem->setBrush(brush);
217 219 d_ptr->m_presenter->updateLayout();
218 220 }
219 221
220 222 /*!
221 223 Returns the brush used for rendering the title text.
222 224 */
223 225 QBrush QChart::titleBrush() const
224 226 {
225 227 //TODO: refactor me
226 228 if (!d_ptr->m_presenter->m_titleItem) return QBrush();
227 229 return d_ptr->m_presenter->m_titleItem->brush();
228 230 }
229 231
230 232 /*!
231 233 Sets the \a theme used by the chart for rendering the graphical representation of the data
232 234 \sa theme()
233 235 */
234 236 void QChart::setTheme(QChart::ChartTheme theme)
235 237 {
236 238 d_ptr->m_presenter->setTheme(theme);
237 239 }
238 240
239 241 /*!
240 242 Returns the theme enum used by the chart.
241 243 \sa ChartTheme, setTheme()
242 244 */
243 245 QChart::ChartTheme QChart::theme() const
244 246 {
245 247 return d_ptr->m_presenter->theme();
246 248 }
247 249
248 250 /*!
249 251 Zooms in the view by a factor of 2
250 252 */
251 253 void QChart::zoomIn()
252 254 {
253 255 d_ptr->m_presenter->zoomIn();
254 256 }
255 257
256 258 /*!
257 259 Zooms in the view to a maximum level at which \a rect is still fully visible.
258 260 */
259 261 void QChart::zoomIn(const QRectF& rect)
260 262 {
261 263 if (!rect.isValid()) return;
262 264 d_ptr->m_presenter->zoomIn(rect);
263 265 }
264 266
265 267 /*!
266 268 Restores the view zoom level to the previous one.
267 269 */
268 270 void QChart::zoomOut()
269 271 {
270 272 d_ptr->m_presenter->zoomOut();
271 273 }
272 274
273 275 /*!
274 276 Returns the pointer to the x axis object of the chart
275 277 */
276 QChartAxis* QChart::axisX() const
278 QAxis* QChart::axisX() const
277 279 {
278 280 return d_ptr->m_dataset->axisX();
279 281 }
280 282
281 283 /*!
282 284 Returns the pointer to the y axis object of the \a series
283 285 If no \a series is provided then default Y axis of the chart is returned.
284 286 */
285 QChartAxis* QChart::axisY(QAbstractSeries *series) const
287 QAxis* QChart::axisY(QAbstractSeries *series) const
286 288 {
287 289 return d_ptr->m_dataset->axisY(series);
288 290 }
289 291
290 292 /*!
291 293 Returns the legend object of the chart. Ownership stays in chart.
292 294 */
293 295 QLegend* QChart::legend() const
294 296 {
295 297 return d_ptr->m_legend;
296 298 }
297 299
298 300 /*!
299 301 Returns the rect that contains information about margins (distance between chart widget edge and axes).
300 302 Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
301 303 */
302 304 QRectF QChart::margins() const
303 305 {
304 306 return d_ptr->m_presenter->margins();
305 307 }
306 308
307 309
308 310 /*!
309 311 Resizes and updates the chart area using the \a event data
310 312 */
311 313 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
312 314 {
313 315 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
314 316 QGraphicsWidget::resizeEvent(event);
315 317 d_ptr->m_presenter->setGeometry(d_ptr->m_rect);
316 318 }
317 319
318 320 /*!
319 321 Sets animation \a options for the chart
320 322 */
321 323 void QChart::setAnimationOptions(AnimationOptions options)
322 324 {
323 325 d_ptr->m_presenter->setAnimationOptions(options);
324 326 }
325 327
326 328 /*!
327 329 Returns animation options for the chart
328 330 */
329 331 QChart::AnimationOptions QChart::animationOptions() const
330 332 {
331 333 return d_ptr->m_presenter->animationOptions();
332 334 }
333 335
334 336 /*!
335 337 Scrolls the visible area of the chart to the left by the distance between two x axis ticks
336 338 */
337 339 void QChart::scrollLeft()
338 340 {
339 341 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
340 342 }
341 343
342 344 /*!
343 345 Scrolls the visible area of the chart to the right by the distance between two x axis ticks
344 346 */
345 347 void QChart::scrollRight()
346 348 {
347 349 d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
348 350 }
349 351
350 352 /*!
351 353 Scrolls the visible area of the chart up by the distance between two y axis ticks
352 354 */
353 355 void QChart::scrollUp()
354 356 {
355 357 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
356 358 }
357 359
358 360 /*!
359 361 Scrolls the visible area of the chart down by the distance between two y axis ticks
360 362 */
361 363 void QChart::scrollDown()
362 364 {
363 365 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
364 366 }
365 367
366 368 /*!
367 369 Sets the chart background visibility state to \a visible
368 370 */
369 371 void QChart::setBackgroundVisible(bool visible)
370 372 {
371 373 //TODO: refactor me
372 374 d_ptr->m_presenter->createChartBackgroundItem();
373 375 d_ptr->m_presenter->m_backgroundItem->setVisible(visible);
374 376 }
375 377
376 378 /*!
377 379 Returns the chart's background visibility state
378 380 */
379 381 bool QChart::isBackgroundVisible() const
380 382 {
381 383 //TODO: refactor me
382 384 if (!d_ptr->m_presenter->m_backgroundItem)
383 385 return false;
384 386
385 387 return d_ptr->m_presenter->m_backgroundItem->isVisible();
386 388 }
387 389
388 390 /*!
389 391 Sets the background drop shadow effect state to \a enabled.
390 392 */
391 393 void QChart::setBackgroundDropShadowEnabled(bool enabled)
392 394 {
393 395 d_ptr->m_presenter->createChartBackgroundItem();
394 396 d_ptr->m_presenter->m_backgroundItem->setDropShadowEnabled(enabled);
395 397 }
396 398
397 399 /*!
398 400 Returns true if the drop shadow effect is enabled for the chart background.
399 401 */
400 402 bool QChart::isBackgroundDropShadowEnabled() const
401 403 {
402 404 if (!d_ptr->m_presenter->m_backgroundItem)
403 405 return false;
404 406
405 407 return d_ptr->m_presenter->m_backgroundItem->isDropShadowEnabled();
406 408 }
407 409
408 410 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
409 411
410 412 QChartPrivate::QChartPrivate():
411 413 m_legend(0),
412 414 m_dataset(0),
413 415 m_presenter(0)
414 416 {
415 417
416 418 }
417 419
418 420 QChartPrivate::~QChartPrivate()
419 421 {
420 422
421 423 }
422 424
423 425 void QChartPrivate::createConnections()
424 426 {
425 427
426 428 QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries *, Domain *)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries *, Domain *)));
427 429 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries *)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries *)));
428 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
429 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),m_presenter,SLOT(handleAxisRemoved(QChartAxis*)));
430 QObject::connect(m_dataset,SIGNAL(axisAdded(QAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAxis*,Domain*)));
431 QObject::connect(m_dataset,SIGNAL(axisRemoved(QAxis*)),m_presenter,SLOT(handleAxisRemoved(QAxis*)));
430 432 }
431 433
432 434 #include "moc_qchart.cpp"
433 435
434 436 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,120 +1,120
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QCHART_H
22 22 #define QCHART_H
23 23
24 24 #include <QAbstractSeries>
25 25 #include <QLegend>
26 26 #include <QGraphicsWidget>
27 27
28 28 class QGraphicsSceneResizeEvent;
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 class QAbstractSeries;
33 class QChartAxis;
33 class QAxis;
34 34 class QLegend;
35 35 struct QChartPrivate;
36 36
37 37 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
38 38 {
39 39 Q_OBJECT
40 40 Q_ENUMS(ChartTheme)
41 41
42 42 public:
43 43 enum ChartTheme {
44 44 ChartThemeLight = 0,
45 45 ChartThemeBlueCerulean,
46 46 ChartThemeDark,
47 47 ChartThemeBrownSand,
48 48 ChartThemeBlueNcs,
49 49 ChartThemeHighContrast,
50 50 ChartThemeBlueIcy
51 51 };
52 52
53 53 enum AnimationOption {
54 54 NoAnimation = 0x0,
55 55 GridAxisAnimations = 0x1,
56 56 SeriesAnimations =0x2,
57 57 AllAnimations = 0x3
58 58 };
59 59
60 60 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
61 61
62 62 public:
63 63 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
64 64 ~QChart();
65 65
66 void addSeries(QAbstractSeries *series, QChartAxis *axisY = 0);
66 void addSeries(QAbstractSeries *series, QAxis *axisY = 0);
67 67 void removeSeries(QAbstractSeries *series);
68 68 void removeAllSeries();
69 69
70 70 void setTheme(QChart::ChartTheme theme);
71 71 QChart::ChartTheme theme() const;
72 72
73 73 void setTitle(const QString& title);
74 74 QString title() const;
75 75 void setTitleFont(const QFont& font);
76 76 QFont titleFont() const;
77 77 void setTitleBrush(const QBrush &brush);
78 78 QBrush titleBrush() const;
79 79
80 80 void setBackgroundBrush(const QBrush &brush);
81 81 QBrush backgroundBrush() const;
82 82 void setBackgroundPen(const QPen &pen);
83 83 QPen backgroundPen() const;
84 84 void setBackgroundVisible(bool visible = true);
85 85 bool isBackgroundVisible() const;
86 86 void setBackgroundDropShadowEnabled(bool enabled = true);
87 87 bool isBackgroundDropShadowEnabled() const;
88 88
89 89 void setAnimationOptions(AnimationOptions options);
90 90 AnimationOptions animationOptions() const;
91 91
92 92 void zoomIn();
93 93 void zoomIn(const QRectF &rect);
94 94 void zoomOut();
95 95 void scrollLeft();
96 96 void scrollRight();
97 97 void scrollUp();
98 98 void scrollDown();
99 99
100 QChartAxis* axisX() const;
101 QChartAxis* axisY(QAbstractSeries* series = 0) const;
100 QAxis* axisX() const;
101 QAxis* axisY(QAbstractSeries* series = 0) const;
102 102
103 103 QLegend* legend() const;
104 104 QRectF margins() const;
105 105
106 106 protected:
107 107 void resizeEvent(QGraphicsSceneResizeEvent *event);
108 108
109 109 protected:
110 110 QScopedPointer<QChartPrivate> d_ptr;
111 111 friend class QLegend;
112 112 friend class ChartPresenter;
113 113 Q_DISABLE_COPY(QChart)
114 114 };
115 115
116 116 QTCOMMERCIALCHART_END_NAMESPACE
117 117
118 118 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
119 119
120 120 #endif
@@ -1,71 +1,71
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QCHARTVIEW_H
22 22 #define QCHARTVIEW_H
23 23
24 #include <QChartAxis>
24 #include <QAxis>
25 25 #include <QAbstractSeries>
26 26 #include <QChart>
27 27 #include <QGraphicsView>
28 28
29 29 class QGraphicsScene;
30 30 class QRubberBand;
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 struct QChartViewPrivate;
35 35
36 36 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
37 37 {
38 38 Q_OBJECT
39 39
40 40 public:
41 41
42 42 enum RubberBand{
43 43 NoRubberBand = 0x0,
44 44 VerticalRubberBand = 0x1,
45 45 HorizonalRubberBand = 0x2,
46 46 RectangleRubberBand = 0x3
47 47 };
48 48
49 49 Q_DECLARE_FLAGS(RubberBands, RubberBand)
50 50
51 51 explicit QChartView(QChart *chart,QWidget *parent = 0);
52 52 ~QChartView();
53 53
54 54 void setRubberBand(const RubberBands& rubberBands);
55 55 RubberBands rubberBand() const;
56 56 QChart* chart() const;
57 57
58 58 protected:
59 59 void resizeEvent(QResizeEvent *event);
60 60 void mousePressEvent(QMouseEvent *event);
61 61 void mouseMoveEvent(QMouseEvent *event);
62 62 void mouseReleaseEvent(QMouseEvent *event);
63 63
64 64 protected:
65 65 QScopedPointer<QChartViewPrivate> d_ptr;
66 66 Q_DISABLE_COPY(QChartView)
67 67 };
68 68
69 69 QTCOMMERCIALCHART_END_NAMESPACE
70 70
71 71 #endif // QCHARTWIDGET_H
@@ -1,570 +1,570
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtTest/QtTest>
22 #include <qchartaxis.h>
22 #include <qaxis.h>
23 23 #include <qlineseries.h>
24 24 #include <private/chartdataset_p.h>
25 25 #include <private/domain_p.h>
26 26
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29 Q_DECLARE_METATYPE(Domain *)
30 Q_DECLARE_METATYPE(QChartAxis *)
30 Q_DECLARE_METATYPE(QAxis *)
31 31 Q_DECLARE_METATYPE(QAbstractSeries *)
32 32 Q_DECLARE_METATYPE(QLineSeries *)
33 33
34 34 class tst_ChartDataSet: public QObject {
35 35
36 36 Q_OBJECT
37 37
38 38 public Q_SLOTS:
39 39 void initTestCase();
40 40 void cleanupTestCase();
41 41 void init();
42 42 void cleanup();
43 43
44 44 private Q_SLOTS:
45 45 void chartdataset_data();
46 46 void chartdataset();
47 47 void addSeries_data();
48 48 void addSeries();
49 49 void removeSeries_data();
50 50 void removeSeries();
51 51 void removeAllSeries_data();
52 52 void removeAllSeries();
53 53 void axisY_data();
54 54 void axisY();
55 55 void seriesCount_data();
56 56 void seriesCount();
57 57 void seriesIndex_data();
58 58 void seriesIndex();
59 59 void domain_data();
60 60 void domain();
61 61 void zoomInDomain_data();
62 62 void zoomInDomain();
63 63 void zoomOutDomain_data();
64 64 void zoomOutDomain();
65 65 void scrollDomain_data();
66 66 void scrollDomain();
67 67 };
68 68
69 69 void tst_ChartDataSet::initTestCase()
70 70 {
71 71 qRegisterMetaType<Domain*>();
72 qRegisterMetaType<QChartAxis*>();
72 qRegisterMetaType<QAxis*>();
73 73 qRegisterMetaType<QAbstractSeries*>();
74 74 }
75 75
76 76 void tst_ChartDataSet::cleanupTestCase()
77 77 {
78 78 }
79 79
80 80 void tst_ChartDataSet::init()
81 81 {
82 82 }
83 83
84 84 void tst_ChartDataSet::cleanup()
85 85 {
86 86 }
87 87
88 88 void tst_ChartDataSet::chartdataset_data()
89 89 {
90 90 }
91 91
92 92 void tst_ChartDataSet::chartdataset()
93 93 {
94 94 ChartDataSet dataSet;
95 95 QVERIFY2(dataSet.axisX(), "Missing axisX.");
96 96 QVERIFY2(dataSet.axisY(), "Missing axisY.");
97 97 //check if not dangling pointer
98 98 dataSet.axisX()->objectName();
99 99 dataSet.axisY()->objectName();
100 100 QLineSeries* series = new QLineSeries(this);
101 101 QCOMPARE(dataSet.seriesIndex(series),-1);
102 102 }
103 103
104 104 void tst_ChartDataSet::addSeries_data()
105 105 {
106 106 QTest::addColumn<QLineSeries*>("series0");
107 QTest::addColumn<QChartAxis*>("axis0");
107 QTest::addColumn<QAxis*>("axis0");
108 108 QTest::addColumn<QLineSeries*>("series1");
109 QTest::addColumn<QChartAxis*>("axis1");
109 QTest::addColumn<QAxis*>("axis1");
110 110 QTest::addColumn<QLineSeries*>("series2");
111 QTest::addColumn<QChartAxis*>("axis2");
111 QTest::addColumn<QAxis*>("axis2");
112 112 QTest::addColumn<int>("axisCount");
113 113
114 114 QLineSeries* series0 = new QLineSeries(this);
115 115 QLineSeries* series1 = new QLineSeries(this);
116 116 QLineSeries* series2 = new QLineSeries(this);
117 117
118 QChartAxis* axis0 = new QChartAxis(this);
119 QChartAxis* axis1 = new QChartAxis(this);
120 QChartAxis* axis2 = new QChartAxis(this);
118 QAxis* axis0 = new QAxis(this);
119 QAxis* axis1 = new QAxis(this);
120 QAxis* axis2 = new QAxis(this);
121 121
122 QTest::newRow("default axis Y: series0,series1,series2") << series0 << (QChartAxis*)0 << series1 << (QChartAxis*)0 << series2 << (QChartAxis*)0 << 2;
123 QTest::newRow("default axis Y: series0, axis 0: series1,series2") << series0 << (QChartAxis*)0 << series1 << axis0 << series2 << axis0 << 3;
122 QTest::newRow("default axis Y: series0,series1,series2") << series0 << (QAxis*)0 << series1 << (QAxis*)0 << series2 << (QAxis*)0 << 2;
123 QTest::newRow("default axis Y: series0, axis 0: series1,series2") << series0 << (QAxis*)0 << series1 << axis0 << series2 << axis0 << 3;
124 124 QTest::newRow("axis0: series0, axis1: series1, axis2: series2") << series0 << axis0 << series1 << axis1 << series2 << axis2 << 4;
125 125 }
126 126
127 127 void tst_ChartDataSet::addSeries()
128 128 {
129 129 QFETCH(QLineSeries*, series0);
130 QFETCH(QChartAxis*, axis0);
130 QFETCH(QAxis*, axis0);
131 131 QFETCH(QLineSeries*, series1);
132 QFETCH(QChartAxis*, axis1);
132 QFETCH(QAxis*, axis1);
133 133 QFETCH(QLineSeries*, series2);
134 QFETCH(QChartAxis*, axis2);
134 QFETCH(QAxis*, axis2);
135 135 QFETCH(int, axisCount);
136 136
137 137 ChartDataSet dataSet;
138 138
139 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
140 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
139 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QAxis*,Domain*)));
140 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QAxis*)));
141 141 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QAbstractSeries *,Domain*)));
142 142 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QAbstractSeries *)));
143 143
144 144 dataSet.addSeries(series0,axis0);
145 145 dataSet.addSeries(series1,axis1);
146 146 dataSet.addSeries(series2,axis2);
147 147
148 148 QCOMPARE(spy0.count(), axisCount);
149 149 QCOMPARE(spy1.count(), 0);
150 150 QCOMPARE(spy2.count(), 3);
151 151 QCOMPARE(spy3.count(), 0);
152 152
153 153 if(axis0==0) axis0 = dataSet.axisY();
154 154 if(axis1==0) axis1 = dataSet.axisY();
155 155 if(axis2==0) axis2 = dataSet.axisY();
156 156
157 157 QVERIFY(axis0 == dataSet.removeSeries(series0));
158 158 QVERIFY(axis1 == dataSet.removeSeries(series1));
159 159 QVERIFY(axis2 == dataSet.removeSeries(series2));
160 160 }
161 161
162 162 void tst_ChartDataSet::removeSeries_data()
163 163 {
164 164 addSeries_data();
165 165 }
166 166
167 167 void tst_ChartDataSet::removeSeries()
168 168 {
169 169 QFETCH(QLineSeries*, series0);
170 QFETCH(QChartAxis*, axis0);
170 QFETCH(QAxis*, axis0);
171 171 QFETCH(QLineSeries*, series1);
172 QFETCH(QChartAxis*, axis1);
172 QFETCH(QAxis*, axis1);
173 173 QFETCH(QLineSeries*, series2);
174 QFETCH(QChartAxis*, axis2);
174 QFETCH(QAxis*, axis2);
175 175 QFETCH(int, axisCount);
176 176
177 177 ChartDataSet dataSet;
178 178
179 179 dataSet.addSeries(series0,axis0);
180 180 dataSet.addSeries(series1,axis1);
181 181 dataSet.addSeries(series2,axis2);
182 182
183 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis *, Domain *)));
184 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis *)));
183 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QAxis *, Domain *)));
184 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QAxis *)));
185 185 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
186 186 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QAbstractSeries *)));
187 187
188 188 dataSet.removeSeries(series0);
189 189 dataSet.removeSeries(series1);
190 190 dataSet.removeSeries(series2);
191 191
192 192 QCOMPARE(spy0.count(), 0);
193 193 QCOMPARE(spy1.count(), axisCount);
194 194 QCOMPARE(spy2.count(), 0);
195 195 QCOMPARE(spy3.count(), 3);
196 196 }
197 197
198 198 void tst_ChartDataSet::removeAllSeries_data()
199 199 {
200 200
201 201 }
202 202
203 203 void tst_ChartDataSet::removeAllSeries()
204 204 {
205 205 QLineSeries* series0 = new QLineSeries(this);
206 206 QLineSeries* series1 = new QLineSeries(this);
207 207 QLineSeries* series2 = new QLineSeries(this);
208 208
209 QChartAxis* axis0 = new QChartAxis(this);
210 QChartAxis* axis1 = new QChartAxis(this);
211 QChartAxis* axis2 = new QChartAxis(this);
209 QAxis* axis0 = new QAxis(this);
210 QAxis* axis1 = new QAxis(this);
211 QAxis* axis2 = new QAxis(this);
212 212
213 213
214 214 ChartDataSet dataSet;
215 215
216 216 dataSet.addSeries(series0, axis0);
217 217 dataSet.addSeries(series1, axis1);
218 218 dataSet.addSeries(series2, axis2);
219 219
220 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis *, Domain *)));
221 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis *)));
220 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QAxis *, Domain *)));
221 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QAxis *)));
222 222 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
223 223 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QAbstractSeries *)));
224 224
225 225 dataSet.removeAllSeries();
226 226
227 227 QCOMPARE(spy0.count(), 0);
228 228 QCOMPARE(spy1.count(), 4);
229 229 QCOMPARE(spy2.count(), 0);
230 230 QCOMPARE(spy3.count(), 3);
231 231 }
232 232
233 233
234 234 void tst_ChartDataSet::axisY_data()
235 235 {
236 QTest::addColumn<QChartAxis*>("axis0");
237 QTest::addColumn<QChartAxis*>("axis1");
238 QTest::addColumn<QChartAxis*>("axis2");
239 QTest::newRow("1 defualt, 2 optional") << (QChartAxis*)0 << new QChartAxis() << new QChartAxis();
240 QTest::newRow("3 optional") << new QChartAxis() << new QChartAxis() << new QChartAxis();
236 QTest::addColumn<QAxis*>("axis0");
237 QTest::addColumn<QAxis*>("axis1");
238 QTest::addColumn<QAxis*>("axis2");
239 QTest::newRow("1 defualt, 2 optional") << (QAxis*)0 << new QAxis() << new QAxis();
240 QTest::newRow("3 optional") << new QAxis() << new QAxis() << new QAxis();
241 241 }
242 242
243 243 void tst_ChartDataSet::axisY()
244 244 {
245 QFETCH(QChartAxis*, axis0);
246 QFETCH(QChartAxis*, axis1);
247 QFETCH(QChartAxis*, axis2);
245 QFETCH(QAxis*, axis0);
246 QFETCH(QAxis*, axis1);
247 QFETCH(QAxis*, axis2);
248 248
249 249 ChartDataSet dataSet;
250 250
251 QChartAxis* defaultAxisY = dataSet.axisY();
251 QAxis* defaultAxisY = dataSet.axisY();
252 252
253 253 QVERIFY2(defaultAxisY, "Missing axisY.");
254 254
255 255 QLineSeries* series0 = new QLineSeries();
256 256 dataSet.addSeries(series0,axis0);
257 257
258 258 QLineSeries* series1 = new QLineSeries();
259 259 dataSet.addSeries(series1,axis1);
260 260
261 261 QLineSeries* series2 = new QLineSeries();
262 262 dataSet.addSeries(series2,axis2);
263 263
264 264 if(!axis0) axis0=defaultAxisY ;
265 265 if(!axis1) axis1=defaultAxisY ;
266 266 if(!axis2) axis2=defaultAxisY ;
267 267
268 268 QVERIFY(dataSet.axisY(series0) == axis0);
269 269 QVERIFY(dataSet.axisY(series1) == axis1);
270 270 QVERIFY(dataSet.axisY(series2) == axis2);
271 271
272 272 }
273 273
274 274 void tst_ChartDataSet::seriesCount_data()
275 275 {
276 276 addSeries_data();
277 277 }
278 278
279 279 void tst_ChartDataSet::seriesCount()
280 280 {
281 281 QFETCH(QLineSeries*, series0);
282 QFETCH(QChartAxis*, axis0);
282 QFETCH(QAxis*, axis0);
283 283 QFETCH(QLineSeries*, series1);
284 QFETCH(QChartAxis*, axis1);
284 QFETCH(QAxis*, axis1);
285 285 QFETCH(QLineSeries*, series2);
286 QFETCH(QChartAxis*, axis2);
286 QFETCH(QAxis*, axis2);
287 287 QFETCH(int, axisCount);
288 288 Q_UNUSED(axisCount);
289 289
290 290 ChartDataSet dataSet;
291 291
292 292 dataSet.addSeries(series0, axis0);
293 293 dataSet.addSeries(series1, axis1);
294 294 dataSet.addSeries(series2, axis2);
295 295
296 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis *, Domain *)));
297 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis *)));
296 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QAxis *, Domain *)));
297 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QAxis *)));
298 298 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
299 299 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QAbstractSeries *)));
300 300
301 301 QCOMPARE(dataSet.seriesCount(series0->type()),3);
302 302 QCOMPARE(spy0.count(), 0);
303 303 QCOMPARE(spy1.count(), 0);
304 304 QCOMPARE(spy2.count(), 0);
305 305 QCOMPARE(spy3.count(), 0);
306 306
307 307 dataSet.removeSeries(series0);
308 308 dataSet.removeSeries(series1);
309 309 dataSet.removeSeries(series2);
310 310 }
311 311
312 312 void tst_ChartDataSet::seriesIndex_data()
313 313 {
314 314 addSeries_data();
315 315 }
316 316
317 317 void tst_ChartDataSet::seriesIndex()
318 318 {
319 319 //TODO: rewrite this series_index_data to match better
320 320
321 321 QFETCH(QLineSeries*, series0);
322 QFETCH(QChartAxis*, axis0);
322 QFETCH(QAxis*, axis0);
323 323 QFETCH(QLineSeries*, series1);
324 QFETCH(QChartAxis*, axis1);
324 QFETCH(QAxis*, axis1);
325 325 QFETCH(QLineSeries*, series2);
326 QFETCH(QChartAxis*, axis2);
326 QFETCH(QAxis*, axis2);
327 327 QFETCH(int, axisCount);
328 328 Q_UNUSED(axisCount);
329 329
330 330 ChartDataSet dataSet;
331 331
332 332 dataSet.addSeries(series0, axis0);
333 333 dataSet.addSeries(series1, axis1);
334 334 dataSet.addSeries(series2, axis2);
335 335
336 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*,Domain*)));
337 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
336 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QAxis*,Domain*)));
337 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QAxis*)));
338 338 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QSeries*,Domain*)));
339 339 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
340 340
341 341 QCOMPARE(dataSet.seriesIndex(series0),0);
342 342 QCOMPARE(dataSet.seriesIndex(series1),1);
343 343 QCOMPARE(dataSet.seriesIndex(series2),2);
344 344
345 345 QCOMPARE(spy0.count(), 0);
346 346 QCOMPARE(spy1.count(), 0);
347 347 QCOMPARE(spy2.count(), 0);
348 348 QCOMPARE(spy3.count(), 0);
349 349
350 350 dataSet.removeSeries(series0);
351 351 dataSet.removeSeries(series1);
352 352 dataSet.removeSeries(series2);
353 353
354 354 QCOMPARE(dataSet.seriesIndex(series0),-1);
355 355 QCOMPARE(dataSet.seriesIndex(series1),-1);
356 356 QCOMPARE(dataSet.seriesIndex(series2),-1);
357 357
358 358 dataSet.addSeries(series0, axis0);
359 359 dataSet.addSeries(series1, axis1);
360 360 dataSet.addSeries(series2, axis2);
361 361
362 362 QCOMPARE(dataSet.seriesIndex(series0),0);
363 363 QCOMPARE(dataSet.seriesIndex(series1),1);
364 364 QCOMPARE(dataSet.seriesIndex(series2),2);
365 365
366 366 dataSet.removeSeries(series1);
367 367
368 368 QCOMPARE(dataSet.seriesIndex(series0),0);
369 369 QCOMPARE(dataSet.seriesIndex(series1),-1);
370 370 QCOMPARE(dataSet.seriesIndex(series2),2);
371 371
372 372 dataSet.addSeries(series1, axis1);
373 373 QCOMPARE(dataSet.seriesIndex(series0),0);
374 374 QCOMPARE(dataSet.seriesIndex(series1),1);
375 375 QCOMPARE(dataSet.seriesIndex(series2),2);
376 376
377 377 dataSet.removeSeries(series2);
378 378 QCOMPARE(dataSet.seriesIndex(series0),0);
379 379 QCOMPARE(dataSet.seriesIndex(series1),1);
380 380 QCOMPARE(dataSet.seriesIndex(series2),-1);
381 381
382 382 dataSet.removeSeries(series0);
383 383 QCOMPARE(dataSet.seriesIndex(series0),-1);
384 384 QCOMPARE(dataSet.seriesIndex(series1),1);
385 385 QCOMPARE(dataSet.seriesIndex(series2),-1);
386 386
387 387 dataSet.addSeries(series2);
388 388 QCOMPARE(dataSet.seriesIndex(series0),-1);
389 389 QCOMPARE(dataSet.seriesIndex(series1),1);
390 390 QCOMPARE(dataSet.seriesIndex(series2),0);
391 391
392 392 dataSet.addSeries(series0);
393 393 QCOMPARE(dataSet.seriesIndex(series0),2);
394 394 QCOMPARE(dataSet.seriesIndex(series1),1);
395 395 QCOMPARE(dataSet.seriesIndex(series2),0);
396 396
397 397 dataSet.removeSeries(series0);
398 398 dataSet.removeSeries(series1);
399 399 dataSet.removeSeries(series2);
400 400
401 401 }
402 402
403 403 void tst_ChartDataSet::domain_data()
404 404 {
405 405 addSeries_data();
406 406 }
407 407
408 408 void tst_ChartDataSet::domain()
409 409 {
410 410 QFETCH(QLineSeries*, series0);
411 QFETCH(QChartAxis*, axis0);
411 QFETCH(QAxis*, axis0);
412 412 QFETCH(QLineSeries*, series1);
413 QFETCH(QChartAxis*, axis1);
413 QFETCH(QAxis*, axis1);
414 414 QFETCH(QLineSeries*, series2);
415 QFETCH(QChartAxis*, axis2);
415 QFETCH(QAxis*, axis2);
416 416 QFETCH(int, axisCount);
417 417 Q_UNUSED(axisCount);
418 418
419 419 ChartDataSet dataSet;
420 420
421 421 dataSet.addSeries(series0, axis0);
422 422 dataSet.addSeries(series1, axis1);
423 423 dataSet.addSeries(series2, axis2);
424 424
425 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis *, Domain *)));
426 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QChartAxis *)));
425 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QAxis *, Domain *)));
426 QSignalSpy spy1(&dataSet, SIGNAL(axisRemoved(QAxis *)));
427 427 QSignalSpy spy2(&dataSet, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
428 428 QSignalSpy spy3(&dataSet, SIGNAL(seriesRemoved(QAbstractSeries *)));
429 429
430 430 QVERIFY(dataSet.domain(axis0)==dataSet.domain(series0));
431 431 QVERIFY(dataSet.domain(axis1)==dataSet.domain(series1));
432 432 QVERIFY(dataSet.domain(axis2)==dataSet.domain(series2));
433 433 QCOMPARE(spy0.count(), 0);
434 434 QCOMPARE(spy1.count(), 0);
435 435 QCOMPARE(spy2.count(), 0);
436 436 QCOMPARE(spy3.count(), 0);
437 437
438 438 dataSet.removeSeries(series0);
439 439 dataSet.removeSeries(series1);
440 440 dataSet.removeSeries(series2);
441 441 }
442 442
443 443 void tst_ChartDataSet::zoomInDomain_data()
444 444 {
445 445 addSeries_data();
446 446 }
447 447
448 448 void tst_ChartDataSet::zoomInDomain()
449 449 {
450 450 QFETCH(QLineSeries*, series0);
451 QFETCH(QChartAxis*, axis0);
451 QFETCH(QAxis*, axis0);
452 452 QFETCH(QLineSeries*, series1);
453 QFETCH(QChartAxis*, axis1);
453 QFETCH(QAxis*, axis1);
454 454 QFETCH(QLineSeries*, series2);
455 QFETCH(QChartAxis*, axis2);
455 QFETCH(QAxis*, axis2);
456 456 QFETCH(int, axisCount);
457 457
458 458 Q_UNUSED(axisCount);
459 459 ChartDataSet dataSet;
460 460
461 461 dataSet.addSeries(series0, axis0);
462 462 dataSet.addSeries(series1, axis1);
463 463 dataSet.addSeries(series2, axis2);
464 464
465 465 Domain* domain0 = dataSet.domain(series0);
466 466 Domain* domain1 = dataSet.domain(series1);
467 467 Domain* domain2 = dataSet.domain(series2);
468 468
469 469 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
470 470 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
471 471 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
472 472
473 473 dataSet.zoomInDomain(QRect(0,0,100,100),QSize(1000,1000));
474 474
475 475 QCOMPARE(spy0.count(), 1);
476 476 QCOMPARE(spy1.count(), 1);
477 477 QCOMPARE(spy2.count(), 1);
478 478
479 479 dataSet.removeSeries(series0);
480 480 dataSet.removeSeries(series1);
481 481 dataSet.removeSeries(series2);
482 482 }
483 483
484 484 void tst_ChartDataSet::zoomOutDomain_data()
485 485 {
486 486 addSeries_data();
487 487 }
488 488
489 489 void tst_ChartDataSet::zoomOutDomain()
490 490 {
491 491 QFETCH(QLineSeries*, series0);
492 QFETCH(QChartAxis*, axis0);
492 QFETCH(QAxis*, axis0);
493 493 QFETCH(QLineSeries*, series1);
494 QFETCH(QChartAxis*, axis1);
494 QFETCH(QAxis*, axis1);
495 495 QFETCH(QLineSeries*, series2);
496 QFETCH(QChartAxis*, axis2);
496 QFETCH(QAxis*, axis2);
497 497 QFETCH(int, axisCount);
498 498
499 499 Q_UNUSED(axisCount);
500 500
501 501 ChartDataSet dataSet;
502 502
503 503 dataSet.addSeries(series0, axis0);
504 504 dataSet.addSeries(series1, axis1);
505 505 dataSet.addSeries(series2, axis2);
506 506
507 507 Domain* domain0 = dataSet.domain(series0);
508 508 Domain* domain1 = dataSet.domain(series1);
509 509 Domain* domain2 = dataSet.domain(series2);
510 510
511 511 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
512 512 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
513 513 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
514 514
515 515 dataSet.zoomOutDomain(QRect(0,0,100,100),QSize(1000,1000));
516 516
517 517 QCOMPARE(spy0.count(), 1);
518 518 QCOMPARE(spy1.count(), 1);
519 519 QCOMPARE(spy2.count(), 1);
520 520
521 521 dataSet.removeSeries(series0);
522 522 dataSet.removeSeries(series1);
523 523 dataSet.removeSeries(series2);
524 524 }
525 525
526 526 void tst_ChartDataSet::scrollDomain_data()
527 527 {
528 528 addSeries_data();
529 529 }
530 530
531 531 void tst_ChartDataSet::scrollDomain()
532 532 {
533 533 QFETCH(QLineSeries*, series0);
534 QFETCH(QChartAxis*, axis0);
534 QFETCH(QAxis*, axis0);
535 535 QFETCH(QLineSeries*, series1);
536 QFETCH(QChartAxis*, axis1);
536 QFETCH(QAxis*, axis1);
537 537 QFETCH(QLineSeries*, series2);
538 QFETCH(QChartAxis*, axis2);
538 QFETCH(QAxis*, axis2);
539 539 QFETCH(int, axisCount);
540 540
541 541 Q_UNUSED(axisCount);
542 542
543 543 ChartDataSet dataSet;
544 544
545 545 dataSet.addSeries(series0, axis0);
546 546 dataSet.addSeries(series1, axis1);
547 547 dataSet.addSeries(series2, axis2);
548 548
549 549 Domain* domain0 = dataSet.domain(series0);
550 550 Domain* domain1 = dataSet.domain(series1);
551 551 Domain* domain2 = dataSet.domain(series2);
552 552
553 553 QSignalSpy spy0(domain0, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
554 554 QSignalSpy spy1(domain1, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
555 555 QSignalSpy spy2(domain2, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
556 556
557 557 dataSet.scrollDomain(10,10,QSize(1000,1000));
558 558
559 559 QCOMPARE(spy0.count(), 1);
560 560 QCOMPARE(spy1.count(), 1);
561 561 QCOMPARE(spy2.count(), 1);
562 562
563 563 dataSet.removeSeries(series0);
564 564 dataSet.removeSeries(series1);
565 565 dataSet.removeSeries(series2);
566 566 }
567 567
568 568 QTEST_MAIN(tst_ChartDataSet)
569 569 #include "tst_chartdataset.moc"
570 570
@@ -1,712 +1,712
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtTest/QtTest>
22 22 #include <private/domain_p.h>
23 #include <qchartaxis.h>
23 #include <qaxis.h>
24 24
25 25 QTCOMMERCIALCHART_USE_NAMESPACE
26 26
27 27 Q_DECLARE_METATYPE(Domain*)
28 28 Q_DECLARE_METATYPE(QSizeF)
29 29
30 30 class tst_Domain: public QObject
31 31 {
32 32 Q_OBJECT
33 33
34 34 public Q_SLOTS:
35 35 void initTestCase();
36 36 void cleanupTestCase();
37 37 void init();
38 38 void cleanup();
39 39
40 40 private Q_SLOTS:
41 41 void domain();
42 42 void handleAxisRangeXChanged_data();
43 43 void handleAxisRangeXChanged();
44 44 void handleAxisRangeYChanged_data();
45 45 void handleAxisRangeYChanged();
46 46 void isEmpty_data();
47 47 void isEmpty();
48 48 void maxX_data();
49 49 void maxX();
50 50 void maxY_data();
51 51 void maxY();
52 52 void minX_data();
53 53 void minX();
54 54 void minY_data();
55 55 void minY();
56 56 void operatorEquals_data();
57 57 void operatorEquals();
58 58 void setRange_data();
59 59 void setRange();
60 60 void setRangeX_data();
61 61 void setRangeX();
62 62 void setRangeY_data();
63 63 void setRangeY();
64 64 void spanX_data();
65 65 void spanX();
66 66 void spanY_data();
67 67 void spanY();
68 68 void zoom_data();
69 69 void zoom();
70 70 void move_data();
71 71 void move();
72 72 void handleAxisXChanged_data();
73 73 void handleAxisXChanged();
74 74 void handleAxisYChanged_data();
75 75 void handleAxisYChanged();
76 76 };
77 77
78 78 void tst_Domain::initTestCase()
79 79 {
80 80 }
81 81
82 82 void tst_Domain::cleanupTestCase()
83 83 {
84 84 }
85 85
86 86 void tst_Domain::init()
87 87 {
88 88 }
89 89
90 90 void tst_Domain::cleanup()
91 91 {
92 92 }
93 93
94 94 void tst_Domain::domain()
95 95 {
96 96 Domain domain;
97 97
98 98 QCOMPARE(domain.isEmpty(), true);
99 99 QCOMPARE(domain.maxX(), 0.0);
100 100 QCOMPARE(domain.maxY(), 0.0);
101 101 QCOMPARE(domain.minX(), 0.0);
102 102 QCOMPARE(domain.minY(), 0.0);
103 103 }
104 104
105 105 void tst_Domain::handleAxisRangeXChanged_data()
106 106 {
107 107 QTest::addColumn<qreal>("min");
108 108 QTest::addColumn<qreal>("max");
109 109 QTest::newRow("-1 1") << -1.0 << 1.0;
110 110 QTest::newRow("0 1") << 0.0 << 1.0;
111 111 QTest::newRow("-1 0") << -1.0 << 0.0;
112 112 }
113 113
114 114 void tst_Domain::handleAxisRangeXChanged()
115 115 {
116 116 QFETCH(qreal, min);
117 117 QFETCH(qreal, max);
118 118
119 119 Domain domain;
120 120
121 121 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
122 122 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
123 123 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
124 124
125 125 domain.handleAxisXChanged(min, max);
126 126
127 127 QList<QVariant> arg0 = spy0.first();
128 128 QVERIFY(qFuzzyIsNull(arg0.at(0).toReal() - min));
129 129 QVERIFY(qFuzzyIsNull(arg0.at(1).toReal() - max));
130 130
131 131 QList<QVariant> arg1 = spy1.first();
132 132 QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min));
133 133 QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max));
134 134
135 135 QCOMPARE(spy0.count(), 1);
136 136 QCOMPARE(spy1.count(), 1);
137 137 QCOMPARE(spy2.count(), 0);
138 138
139 139 }
140 140
141 141 void tst_Domain::handleAxisRangeYChanged_data()
142 142 {
143 143 QTest::addColumn<qreal>("min");
144 144 QTest::addColumn<qreal>("max");
145 145 QTest::newRow("-1 1") << -1.0 << 1.0;
146 146 QTest::newRow("0 1") << 0.0 << 1.0;
147 147 QTest::newRow("-1 0") << -1.0 << 0.0;
148 148 }
149 149
150 150 void tst_Domain::handleAxisRangeYChanged()
151 151 {
152 152 QFETCH(qreal, min);
153 153 QFETCH(qreal, max);
154 154
155 155 Domain domain;
156 156
157 157 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
158 158 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
159 159 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
160 160
161 161 domain.handleAxisYChanged(min, max, 5);
162 162
163 163 QList<QVariant> arg0 = spy0.first();
164 164 QVERIFY(qFuzzyIsNull(arg0.at(2).toReal() - min));
165 165 QVERIFY(qFuzzyIsNull(arg0.at(3).toReal() - max));
166 166
167 167 QList<QVariant> arg1 = spy2.first();
168 168 QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min));
169 169 QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max));
170 170
171 171 QCOMPARE(spy0.count(), 1);
172 172 QCOMPARE(spy1.count(), 0);
173 173 QCOMPARE(spy2.count(), 1);
174 174 }
175 175
176 176 void tst_Domain::isEmpty_data()
177 177 {
178 178 QTest::addColumn<qreal>("minX");
179 179 QTest::addColumn<qreal>("maxX");
180 180 QTest::addColumn<qreal>("minY");
181 181 QTest::addColumn<qreal>("maxY");
182 182 QTest::addColumn<bool>("isEmpty");
183 183 QTest::newRow("0 0 0 0") << 0.0 << 0.0 << 0.0 << 0.0 << true;
184 184 QTest::newRow("0 1 0 0") << 0.0 << 1.0 << 0.0 << 0.0 << true;
185 185 QTest::newRow("0 0 0 1") << 0.0 << 1.0 << 0.0 << 0.0 << true;
186 186 QTest::newRow("0 1 0 1") << 0.0 << 1.0 << 0.0 << 1.0 << false;
187 187 }
188 188
189 189 void tst_Domain::isEmpty()
190 190 {
191 191 QFETCH(qreal, minX);
192 192 QFETCH(qreal, maxX);
193 193 QFETCH(qreal, minY);
194 194 QFETCH(qreal, maxY);
195 195 QFETCH(bool, isEmpty);
196 196
197 197 Domain domain;
198 198 domain.setRange(minX, maxX, minY, maxY);
199 199 QCOMPARE(domain.isEmpty(), isEmpty);
200 200 }
201 201
202 202 void tst_Domain::maxX_data()
203 203 {
204 204 QTest::addColumn<qreal>("maxX1");
205 205 QTest::addColumn<qreal>("maxX2");
206 206 QTest::addColumn<int>("count");
207 207 QTest::newRow("1") << 0.0 << 1.0 << 1;
208 208 QTest::newRow("1.0") << 1.0 << 1.0 << 1;
209 209 QTest::newRow("2.0") << 1.0 << 0.0 << 2;
210 210 }
211 211
212 212 void tst_Domain::maxX()
213 213 {
214 214 QFETCH(qreal, maxX1);
215 215 QFETCH(qreal, maxX2);
216 216 QFETCH(int, count);
217 217
218 218 Domain domain;
219 219
220 220 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
221 221 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
222 222 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
223 223
224 224 domain.setMaxX(maxX1);
225 225 QCOMPARE(domain.maxX(), maxX1);
226 226 domain.setMaxX(maxX2);
227 227 QCOMPARE(domain.maxX(), maxX2);
228 228
229 229 QCOMPARE(spy0.count(), count);
230 230 QCOMPARE(spy1.count(), count);
231 231 QCOMPARE(spy2.count(), 0);
232 232
233 233 }
234 234
235 235 void tst_Domain::maxY_data()
236 236 {
237 237 QTest::addColumn<qreal>("maxY1");
238 238 QTest::addColumn<qreal>("maxY2");
239 239 QTest::addColumn<int>("count");
240 240 QTest::newRow("1") << 0.0 << 1.0 << 1;
241 241 QTest::newRow("1.0") << 1.0 << 1.0 << 1;
242 242 QTest::newRow("2.0") << 1.0 << 0.0 << 2;
243 243 }
244 244
245 245 void tst_Domain::maxY()
246 246 {
247 247 QFETCH(qreal, maxY1);
248 248 QFETCH(qreal, maxY2);
249 249 QFETCH(int, count);
250 250
251 251 Domain domain;
252 252
253 253 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
254 254 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
255 255 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
256 256
257 257 domain.setMaxY(maxY1);
258 258 QCOMPARE(domain.maxY(), maxY1);
259 259 domain.setMaxY(maxY2);
260 260 QCOMPARE(domain.maxY(), maxY2);
261 261
262 262 QCOMPARE(spy0.count(), count);
263 263 QCOMPARE(spy1.count(), 0);
264 264 QCOMPARE(spy2.count(), count);
265 265 }
266 266
267 267 void tst_Domain::minX_data()
268 268 {
269 269 QTest::addColumn<qreal>("minX1");
270 270 QTest::addColumn<qreal>("minX2");
271 271 QTest::addColumn<int>("count");
272 272 QTest::newRow("1") << 0.0 << 1.0 << 1;
273 273 QTest::newRow("1.0") << 1.0 << 1.0 << 1;
274 274 QTest::newRow("2.0") << 1.0 << 0.0 << 2;
275 275 }
276 276
277 277 void tst_Domain::minX()
278 278 {
279 279 QFETCH(qreal, minX1);
280 280 QFETCH(qreal, minX2);
281 281 QFETCH(int, count);
282 282
283 283 Domain domain;
284 284
285 285 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
286 286 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
287 287 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
288 288
289 289 domain.setMinX(minX1);
290 290 QCOMPARE(domain.minX(), minX1);
291 291 domain.setMinX(minX2);
292 292 QCOMPARE(domain.minX(), minX2);
293 293
294 294 QCOMPARE(spy0.count(), count);
295 295 QCOMPARE(spy1.count(), count);
296 296 QCOMPARE(spy2.count(), 0);
297 297 }
298 298
299 299 void tst_Domain::minY_data()
300 300 {
301 301 QTest::addColumn<qreal>("minY1");
302 302 QTest::addColumn<qreal>("minY2");
303 303 QTest::addColumn<int>("count");
304 304 QTest::newRow("1") << 0.0 << 1.0 << 1;
305 305 QTest::newRow("1.0") << 1.0 << 1.0 << 1;
306 306 QTest::newRow("2.0") << 1.0 << 0.0 << 2;
307 307 }
308 308
309 309 void tst_Domain::minY()
310 310 {
311 311 QFETCH(qreal, minY1);
312 312 QFETCH(qreal, minY2);
313 313 QFETCH(int, count);
314 314
315 315 Domain domain;
316 316
317 317 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
318 318 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
319 319 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
320 320
321 321 domain.setMinY(minY1);
322 322 QCOMPARE(domain.minY(), minY1);
323 323 domain.setMinY(minY2);
324 324 QCOMPARE(domain.minY(), minY2);
325 325
326 326 QCOMPARE(spy0.count(), count);
327 327 QCOMPARE(spy1.count(), 0);
328 328 QCOMPARE(spy2.count(), count);
329 329 }
330 330
331 331 void tst_Domain::operatorEquals_data()
332 332 {
333 333
334 334 QTest::addColumn<Domain*>("domain1");
335 335 QTest::addColumn<Domain*>("domain2");
336 336 QTest::addColumn<bool>("equals");
337 337 QTest::addColumn<bool>("notEquals");
338 338 Domain* a;
339 339 Domain* b;
340 340 a = new Domain();
341 341 a->setRange(0, 100, 0, 100);
342 342 b = new Domain();
343 343 b->setRange(0, 100, 0, 100);
344 344 QTest::newRow("equals") << a << b << true << false;
345 345 a = new Domain();
346 346 a->setRange(0, 100, 0, 100);
347 347 b = new Domain();
348 348 b->setRange(0, 100, 0, 1);
349 349 QTest::newRow("equals") << a << b << false << true;
350 350 a = new Domain();
351 351 a->setRange(0, 100, 0, 100);
352 352 b = new Domain();
353 353 b->setRange(0, 1, 0, 100);
354 354 QTest::newRow("equals") << a << b << false << true;
355 355
356 356 }
357 357
358 358 void tst_Domain::operatorEquals()
359 359 {
360 360 QFETCH(Domain*, domain1);
361 361 QFETCH(Domain*, domain2);
362 362 QFETCH(bool, equals);
363 363 QFETCH(bool, notEquals);
364 364
365 365 Domain domain;
366 366
367 367 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
368 368 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
369 369 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
370 370
371 371 QCOMPARE(*domain1==*domain2, equals);
372 372 QCOMPARE(*domain1!=*domain2, notEquals);
373 373
374 374 QCOMPARE(spy0.count(), 0);
375 375 QCOMPARE(spy1.count(), 0);
376 376 QCOMPARE(spy2.count(), 0);
377 377 }
378 378
379 379 void tst_Domain::setRange_data()
380 380 {
381 381 QTest::addColumn<qreal>("minX");
382 382 QTest::addColumn<qreal>("maxX");
383 383 QTest::addColumn<qreal>("minY");
384 384 QTest::addColumn<qreal>("maxY");
385 385 QTest::newRow("1,2,1,2") << 1.0 << 2.0 << 1.0 << 2.0;
386 386 QTest::newRow("1,3,1,3") << 1.0 << 3.0 << 1.0 << 3.0;
387 387 QTest::newRow("-1,5,-2,-1") << -1.0 << 5.0 << -2.0 << -1.0;
388 388 }
389 389
390 390 void tst_Domain::setRange()
391 391 {
392 392 QFETCH(qreal, minX);
393 393 QFETCH(qreal, maxX);
394 394 QFETCH(qreal, minY);
395 395 QFETCH(qreal, maxY);
396 396
397 397 Domain domain;
398 398
399 399 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
400 400 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
401 401 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
402 402
403 403 domain.setRange(minX, maxX, minY, maxY);
404 404
405 405 QCOMPARE(domain.minX(), minX);
406 406 QCOMPARE(domain.maxX(), maxX);
407 407 QCOMPARE(domain.minY(), minY);
408 408 QCOMPARE(domain.maxY(), maxY);
409 409
410 410 QCOMPARE(spy0.count(), 1);
411 411 QCOMPARE(spy1.count(), 1);
412 412 QCOMPARE(spy2.count(), 1);
413 413
414 414 }
415 415
416 416 void tst_Domain::setRangeX_data()
417 417 {
418 418 QTest::addColumn<qreal>("min");
419 419 QTest::addColumn<qreal>("max");
420 420 QTest::newRow("-1 1") << -1.0 << 1.0;
421 421 QTest::newRow("0 1") << 0.0 << 1.0;
422 422 QTest::newRow("-1 0") << -1.0 << 0.0;
423 423 }
424 424
425 425 void tst_Domain::setRangeX()
426 426 {
427 427 QFETCH(qreal, min);
428 428 QFETCH(qreal, max);
429 429
430 430 Domain domain;
431 431
432 432 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
433 433 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
434 434 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
435 435
436 436 domain.setRangeX(min, max);
437 437
438 438 QList<QVariant> arg0 = spy0.first();
439 439 QVERIFY(qFuzzyIsNull(arg0.at(0).toReal() - min));
440 440 QVERIFY(qFuzzyIsNull(arg0.at(1).toReal() - max));
441 441
442 442 QList<QVariant> arg1 = spy1.first();
443 443 QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min));
444 444 QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max));
445 445
446 446 QCOMPARE(spy0.count(), 1);
447 447 QCOMPARE(spy1.count(), 1);
448 448 QCOMPARE(spy2.count(), 0);
449 449 }
450 450
451 451 void tst_Domain::setRangeY_data()
452 452 {
453 453 QTest::addColumn<qreal>("min");
454 454 QTest::addColumn<qreal>("max");
455 455 QTest::newRow("-1 1") << -1.0 << 1.0;
456 456 QTest::newRow("0 1") << 0.0 << 1.0;
457 457 QTest::newRow("-1 0") << -1.0 << 0.0;
458 458 }
459 459
460 460 void tst_Domain::setRangeY()
461 461 {
462 462 QFETCH(qreal, min);
463 463 QFETCH(qreal, max);
464 464
465 465 Domain domain;
466 466
467 467 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
468 468 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
469 469 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
470 470
471 471 domain.setRangeY(min, max);
472 472
473 473 QList<QVariant> arg0 = spy0.first();
474 474 QVERIFY(qFuzzyIsNull(arg0.at(2).toReal() - min));
475 475 QVERIFY(qFuzzyIsNull(arg0.at(3).toReal() - max));
476 476
477 477 QList<QVariant> arg1 = spy2.first();
478 478 QVERIFY(qFuzzyIsNull(arg1.at(0).toReal() - min));
479 479 QVERIFY(qFuzzyIsNull(arg1.at(1).toReal() - max));
480 480
481 481 QCOMPARE(spy0.count(), 1);
482 482 QCOMPARE(spy1.count(), 0);
483 483 QCOMPARE(spy2.count(), 1);
484 484 }
485 485
486 486 void tst_Domain::spanX_data()
487 487 {
488 488 QTest::addColumn<qreal>("minX");
489 489 QTest::addColumn<qreal>("maxX");
490 490 QTest::addColumn<qreal>("spanX");
491 491 QTest::newRow("1 2 1") << 1.0 << 2.0 << 1.0;
492 492 QTest::newRow("0 2 2") << 1.0 << 2.0 << 1.0;
493 493 }
494 494
495 495 void tst_Domain::spanX()
496 496 {
497 497 QFETCH(qreal, minX);
498 498 QFETCH(qreal, maxX);
499 499 QFETCH(qreal, spanX);
500 500
501 501 Domain domain;
502 502
503 503 domain.setRangeX(minX, maxX);
504 504
505 505 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
506 506 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
507 507 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
508 508
509 509 QCOMPARE(domain.spanX(), spanX);
510 510
511 511 QCOMPARE(spy0.count(), 0);
512 512 QCOMPARE(spy1.count(), 0);
513 513 QCOMPARE(spy2.count(), 0);
514 514 }
515 515
516 516 void tst_Domain::spanY_data()
517 517 {
518 518 QTest::addColumn<qreal>("minY");
519 519 QTest::addColumn<qreal>("maxY");
520 520 QTest::addColumn<qreal>("spanY");
521 521 QTest::newRow("1 2 1") << 1.0 << 2.0 << 1.0;
522 522 QTest::newRow("0 2 2") << 1.0 << 2.0 << 1.0;
523 523 }
524 524
525 525 void tst_Domain::spanY()
526 526 {
527 527 QFETCH(qreal, minY);
528 528 QFETCH(qreal, maxY);
529 529 QFETCH(qreal, spanY);
530 530
531 531 Domain domain;
532 532
533 533 domain.setRangeY(minY, maxY);
534 534
535 535 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
536 536 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
537 537 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
538 538
539 539 QCOMPARE(domain.spanY(), spanY);
540 540
541 541 QCOMPARE(spy0.count(), 0);
542 542 QCOMPARE(spy1.count(), 0);
543 543 QCOMPARE(spy2.count(), 0);
544 544 }
545 545
546 546 void tst_Domain::zoom_data()
547 547 {
548 548 QTest::addColumn<QRectF>("rect0");
549 549 QTest::addColumn<QSizeF>("size0");
550 550 QTest::addColumn<QRectF>("rect1");
551 551 QTest::addColumn<QSizeF>("size1");
552 552 QTest::addColumn<QRectF>("rect2");
553 553 QTest::addColumn<QSizeF>("size2");
554 554 QTest::newRow("first") << QRectF(10, 10, 100, 100) << QSizeF(1000, 1000)
555 555 << QRectF(20, 20, 100, 100) << QSizeF(1000, 1000) << QRectF(50, 50, 100, 100)
556 556 << QSizeF(1000, 1000);
557 557 QTest::newRow("scound") << QRectF(10, 10, 50, 50) << QSizeF(1000, 1000)
558 558 << QRectF(20, 20, 100, 100) << QSizeF(1000, 1000) << QRectF(50, 50, 100, 100)
559 559 << QSizeF(1000, 1000);
560 560 QTest::newRow("third") << QRectF(10, 10, 10, 10) << QSizeF(100, 100) << QRectF(20, 20, 20, 20)
561 561 << QSizeF(100, 100) << QRectF(50, 50, 50, 50) << QSizeF(100, 100);
562 562 }
563 563
564 564 void tst_Domain::zoom()
565 565 {
566 566 QFETCH(QRectF, rect0);
567 567 QFETCH(QSizeF, size0);
568 568 QFETCH(QRectF, rect1);
569 569 QFETCH(QSizeF, size1);
570 570 QFETCH(QRectF, rect2);
571 571 QFETCH(QSizeF, size2);
572 572
573 573 Domain domain;
574 574
575 575 domain.setRange(0, 1000, 0, 1000);
576 576
577 577 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
578 578 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
579 579 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
580 580
581 581 Domain domain0;
582 582 domain0.setRange(domain.minX(), domain.maxX(), domain.minY(), domain.maxY());
583 583 domain.zoomIn(rect0, size0);
584 584 Domain domain1;
585 585 domain1.setRange(domain.minX(), domain.maxX(), domain.minY(), domain.maxY());
586 586 domain.zoomIn(rect1, size1);
587 587 Domain domain2;
588 588 domain2.setRange(domain.minX(), domain.maxX(), domain.minY(), domain.maxY());
589 589 domain.zoomIn(rect2, size2);
590 590 domain.zoomOut(rect2, size2);
591 591 QCOMPARE(domain == domain2, true);
592 592 domain.zoomOut(rect1, size1);
593 593 QCOMPARE(domain == domain1, true);
594 594 domain.zoomOut(rect0, size0);
595 595 QCOMPARE(domain == domain0, true);
596 596 QCOMPARE(spy0.count(), 6);
597 597 QCOMPARE(spy1.count(), 6);
598 598 QCOMPARE(spy2.count(), 6);
599 599
600 600 }
601 601
602 602 void tst_Domain::move_data()
603 603 {
604 604 QTest::addColumn<int>("dx");
605 605 QTest::addColumn<int>("dy");
606 606 QTest::addColumn<QSizeF>("size");
607 607 QTest::newRow("dx 100, dy 0, size 1000x1000") << 100 << 0 << QSizeF(1000, 1000);
608 608 QTest::newRow("dx 0, dy 100, size 1000x1000") << 0 << 100 << QSizeF(1000, 1000);
609 609 QTest::newRow("dx -100, dy 0, size 1000x1000") << -100 << 0 << QSizeF(1000, 1000);
610 610 QTest::newRow("dx 0, dy -100, size 1000x1000") << 0 << -100 << QSizeF(1000, 1000);
611 611 QTest::newRow("dx 100, dy 100, size 1000x1000") << 100 << 100 << QSizeF(1000, 1000);
612 612 QTest::newRow("dx 100, dy 50, size 1000x1000") << 100 << 50 << QSizeF(1000, 1000);
613 613 }
614 614
615 615 void tst_Domain::move()
616 616 {
617 617 QFETCH(int, dx);
618 618 QFETCH(int, dy);
619 619 QFETCH(QSizeF, size);
620 620 Domain domain;
621 621
622 622 domain.setRange(0, size.width(), 0, size.height());
623 623
624 624 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
625 625 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
626 626 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
627 627
628 628 domain.move(dx, dy, size);
629 629
630 630 Domain result;
631 631 result.setRange(dx, size.width() + dx, dy, size.height() + dy);
632 632
633 633 QCOMPARE(domain == result, true);
634 634 QCOMPARE(spy0.count(), 1);
635 635 QCOMPARE(spy1.count(), dx!=0?1:0);
636 636 QCOMPARE(spy2.count(), dy!=0?1:0);
637 637 }
638 638
639 639 void tst_Domain::handleAxisXChanged_data()
640 640 {
641 641 QTest::addColumn<qreal>("min");
642 642 QTest::addColumn<qreal>("max");
643 643 QTest::addColumn<int>("tickCount");
644 644 QTest::addColumn<bool>("niceNumbers");
645 645 QTest::addColumn<qreal>("resultMin");
646 646 QTest::addColumn<qreal>("resultMax");
647 647 QTest::addColumn<int>("resultTickCount");
648 648 QTest::newRow("0,100,5,false") << 0.0 << 100.0 << 5 << false << 0.0 << 100.0 << 5;
649 649 QTest::newRow("0,100,5,true") << 0.0 << 100.0 << 5 << true << 0.0 << 100.0 << 6;
650 650 QTest::newRow("0.1,99,5,true") << 0.1 << 99.0 << 5 << true << 0.0 << 100.0 << 6;
651 651 QTest::newRow("5,93.5,true") << 5.0 << 93.5 << 5 << true << 0.0 << 100.0 << 6;
652 652 }
653 653
654 654 void tst_Domain::handleAxisXChanged()
655 655 {
656 656 QFETCH(qreal, min);
657 657 QFETCH(qreal, max);
658 658 QFETCH(int, tickCount);
659 659 QFETCH(bool, niceNumbers);
660 660 QFETCH(qreal, resultMin);
661 661 QFETCH(qreal, resultMax);
662 662 QFETCH(int, resultTickCount);
663 663
664 664 Domain domain;
665 665 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
666 666 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
667 667 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
668 668
669 669 domain.handleAxisXChanged(min, max, tickCount, niceNumbers);
670 670
671 671 QCOMPARE(resultMin, domain.minX());
672 672 QCOMPARE(resultMax, domain.maxX());
673 673 QCOMPARE(resultTickCount, domain.tickXCount());
674 674 QCOMPARE(spy0.count(), 1);
675 675 QCOMPARE(spy1.count(), 1);
676 676 QCOMPARE(spy2.count(), 0);
677 677
678 678 }
679 679
680 680 void tst_Domain::handleAxisYChanged_data()
681 681 {
682 682 handleAxisXChanged_data();
683 683 }
684 684
685 685 void tst_Domain::handleAxisYChanged()
686 686 {
687 687 QFETCH(qreal, min);
688 688 QFETCH(qreal, max);
689 689 QFETCH(int, tickCount);
690 690 QFETCH(bool, niceNumbers);
691 691 QFETCH(qreal, resultMin);
692 692 QFETCH(qreal, resultMax);
693 693 QFETCH(int, resultTickCount);
694 694
695 695 Domain domain;
696 696 QSignalSpy spy0(&domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
697 697 QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal,int)));
698 698 QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal,int)));
699 699
700 700 domain.handleAxisYChanged(min, max, tickCount, niceNumbers);
701 701
702 702 QCOMPARE(resultMin, domain.minY());
703 703 QCOMPARE(resultMax, domain.maxY());
704 704 QCOMPARE(resultTickCount, domain.tickYCount());
705 705 QCOMPARE(spy0.count(), 1);
706 706 QCOMPARE(spy1.count(), 0);
707 707 QCOMPARE(spy2.count(), 1);
708 708 }
709 709
710 710 QTEST_MAIN(tst_Domain)
711 711 #include "tst_domain.moc"
712 712
@@ -1,572 +1,572
1 1 #include <QtTest/QtTest>
2 2 #include <qchartview.h>
3 3 #include <qlineseries.h>
4 4 #include <qareaseries.h>
5 5 #include <qscatterseries.h>
6 6 #include <qsplineseries.h>
7 7 #include <qpieseries.h>
8 8 #include <qbarseries.h>
9 9 #include <qpercentbarseries.h>
10 10 #include <qstackedbarseries.h>
11 11
12 12 QTCOMMERCIALCHART_USE_NAMESPACE
13 13
14 Q_DECLARE_METATYPE(QChartAxis *)
14 Q_DECLARE_METATYPE(QAxis *)
15 15 Q_DECLARE_METATYPE(QAbstractSeries *)
16 16 Q_DECLARE_METATYPE(QChart::AnimationOption)
17 17 Q_DECLARE_METATYPE(QBrush)
18 18 Q_DECLARE_METATYPE(QPen)
19 19 Q_DECLARE_METATYPE(QChart::ChartTheme)
20 20
21 21 class tst_QChart : public QObject
22 22 {
23 23 Q_OBJECT
24 24
25 25 public slots:
26 26 void initTestCase();
27 27 void cleanupTestCase();
28 28 void init();
29 29 void cleanup();
30 30
31 31 private slots:
32 32 void qchart_data();
33 33 void qchart();
34 34
35 35 void addSeries_data();
36 36 void addSeries();
37 37 void animationOptions_data();
38 38 void animationOptions();
39 39 void axisX_data();
40 40 void axisX();
41 41 void axisY_data();
42 42 void axisY();
43 43 void backgroundBrush_data();
44 44 void backgroundBrush();
45 45 void backgroundPen_data();
46 46 void backgroundPen();
47 47 void isBackgroundVisible_data();
48 48 void isBackgroundVisible();
49 49 void legend_data();
50 50 void legend();
51 51 void margins_data();
52 52 void margins();
53 53 void removeAllSeries_data();
54 54 void removeAllSeries();
55 55 void removeSeries_data();
56 56 void removeSeries();
57 57 void scrollDown_data();
58 58 void scrollDown();
59 59 void scrollLeft_data();
60 60 void scrollLeft();
61 61 void scrollRight_data();
62 62 void scrollRight();
63 63 void scrollUp_data();
64 64 void scrollUp();
65 65 void theme_data();
66 66 void theme();
67 67 void title_data();
68 68 void title();
69 69 void titleBrush_data();
70 70 void titleBrush();
71 71 void titleFont_data();
72 72 void titleFont();
73 73 void zoomIn_data();
74 74 void zoomIn();
75 75 void zoomOut_data();
76 76 void zoomOut();
77 77
78 78 private:
79 79 void createTestData();
80 80
81 81 private:
82 82 QChartView* m_view;
83 83 QChart* m_chart;
84 84 };
85 85
86 86 void tst_QChart::initTestCase()
87 87 {
88 88
89 89 }
90 90
91 91 void tst_QChart::cleanupTestCase()
92 92 {
93 93
94 94 }
95 95
96 96 void tst_QChart::init()
97 97 {
98 98 m_view = new QChartView(new QChart());
99 99 m_chart = m_view->chart();
100 100 }
101 101
102 102 void tst_QChart::createTestData()
103 103 {
104 104 QLineSeries* series0 = new QLineSeries(this);
105 105 *series0 << QPointF(0, 0) << QPointF(100, 100);
106 106 m_chart->addSeries(series0);
107 107 m_view->show();
108 108 QTest::qWaitForWindowShown(m_view);
109 109 }
110 110
111 111 void tst_QChart::cleanup()
112 112 {
113 113 delete m_view;
114 114 m_view = 0;
115 115 m_chart = 0;
116 116 }
117 117
118 118 void tst_QChart::qchart_data()
119 119 {
120 120 }
121 121
122 122 void tst_QChart::qchart()
123 123 {
124 124 QVERIFY(m_chart);
125 125 QVERIFY(m_chart->legend());
126 126 QVERIFY(!m_chart->legend()->isVisible());
127 127
128 128 QCOMPARE(m_chart->animationOptions(), QChart::NoAnimation);
129 129 QVERIFY(m_chart->axisX());
130 130 QVERIFY(m_chart->axisY());
131 131 QVERIFY(m_chart->backgroundBrush()!=QBrush());
132 132 QVERIFY(m_chart->backgroundPen()!=QPen());
133 133 QCOMPARE(m_chart->isBackgroundVisible(), true);
134 134
135 135 QVERIFY(m_chart->margins().top()>0);
136 136 QVERIFY(m_chart->margins().left()>0);
137 137 QVERIFY(m_chart->margins().right()>0);
138 138 QVERIFY(m_chart->margins().bottom()>0);
139 139
140 140 QCOMPARE(m_chart->theme(), QChart::ChartThemeLight);
141 141 QCOMPARE(m_chart->title(), QString());
142 142
143 143 //QCOMPARE(m_chart->titleBrush(),QBrush());
144 144 //QCOMPARE(m_chart->titleFont(),QFont());
145 145
146 146 m_chart->removeAllSeries();
147 147 m_chart->scrollDown();
148 148 m_chart->scrollLeft();
149 149 m_chart->scrollRight();
150 150 m_chart->scrollUp();
151 151
152 152 m_chart->zoomIn();
153 153 m_chart->zoomIn(QRectF());
154 154 m_chart->zoomOut();
155 155 }
156 156
157 157 void tst_QChart::addSeries_data()
158 158 {
159 159 QTest::addColumn<QAbstractSeries *>("series");
160 QTest::addColumn<QChartAxis *>("axis");
160 QTest::addColumn<QAxis *>("axis");
161 161
162 162 QAbstractSeries* series0 = new QLineSeries(this);
163 163 QAbstractSeries* series1 = new QAreaSeries(static_cast<QLineSeries*>(series0));
164 164 QAbstractSeries* series2 = new QScatterSeries(this);
165 165 QAbstractSeries* series3 = new QSplineSeries(this);
166 166 QAbstractSeries* series4 = new QPieSeries(this);
167 167 QAbstractSeries* series5 = new QBarSeries(QBarCategories(),this);
168 168 QAbstractSeries* series6 = new QPercentBarSeries(QBarCategories(),this);
169 169 QAbstractSeries* series7 = new QStackedBarSeries(QBarCategories(),this);
170 170
171 QChartAxis* axis = new QChartAxis(this);
171 QAxis* axis = new QAxis(this);
172 172
173 QTest::newRow("default axis: lineSeries") << series0 << (QChartAxis*) 0;
173 QTest::newRow("default axis: lineSeries") << series0 << (QAxis*) 0;
174 174 QTest::newRow("axis0: lineSeries") << series0 << axis;
175 QTest::newRow("default axis: areaSeries") << series1 << (QChartAxis*) 0;
175 QTest::newRow("default axis: areaSeries") << series1 << (QAxis*) 0;
176 176 QTest::newRow("axis: areaSeries") << series1 << axis;
177 QTest::newRow("default axis: scatterSeries") << series2 << (QChartAxis*) 0;
177 QTest::newRow("default axis: scatterSeries") << series2 << (QAxis*) 0;
178 178 QTest::newRow("axis1: scatterSeries") << series2 << axis;
179 QTest::newRow("default axis: splineSeries") << series3 << (QChartAxis*) 0;
179 QTest::newRow("default axis: splineSeries") << series3 << (QAxis*) 0;
180 180 QTest::newRow("axis: splineSeries") << series3 << axis;
181 QTest::newRow("default axis: pieSeries") << series4 << (QChartAxis*) 0;
181 QTest::newRow("default axis: pieSeries") << series4 << (QAxis*) 0;
182 182 QTest::newRow("axis: pieSeries") << series4 << axis;
183 QTest::newRow("default axis: barSeries") << series5 << (QChartAxis*) 0;
183 QTest::newRow("default axis: barSeries") << series5 << (QAxis*) 0;
184 184 QTest::newRow("axis: barSeries") << series5 << axis;
185 QTest::newRow("default axis: percentBarSeries") << series6 << (QChartAxis*) 0;
185 QTest::newRow("default axis: percentBarSeries") << series6 << (QAxis*) 0;
186 186 QTest::newRow("axis: barSeries") << series6 << axis;
187 QTest::newRow("default axis: stackedBarSeries") << series7 << (QChartAxis*) 0;
187 QTest::newRow("default axis: stackedBarSeries") << series7 << (QAxis*) 0;
188 188 QTest::newRow("axis: barSeries") << series7 << axis;
189 189
190 190 }
191 191
192 192 void tst_QChart::addSeries()
193 193 {
194 194 QFETCH(QAbstractSeries *, series);
195 QFETCH(QChartAxis *, axis);
195 QFETCH(QAxis *, axis);
196 196 m_view->show();
197 197 QTest::qWaitForWindowShown(m_view);
198 198 if(!axis) axis = m_chart->axisY();
199 199 m_chart->addSeries(series,axis);
200 200 QCOMPARE(m_chart->axisY(series),axis);
201 201 m_chart->removeSeries(series);
202 202
203 203 }
204 204
205 205 void tst_QChart::animationOptions_data()
206 206 {
207 207 QTest::addColumn<QChart::AnimationOption>("animationOptions");
208 208 QTest::newRow("AllAnimations") << QChart::AllAnimations;
209 209 QTest::newRow("NoAnimation") << QChart::NoAnimation;
210 210 QTest::newRow("GridAxisAnimations") << QChart::GridAxisAnimations;
211 211 QTest::newRow("SeriesAnimations") << QChart::SeriesAnimations;
212 212 }
213 213
214 214 void tst_QChart::animationOptions()
215 215 {
216 216 createTestData();
217 217 QFETCH(QChart::AnimationOption, animationOptions);
218 218 m_chart->setAnimationOptions(animationOptions);
219 219 QCOMPARE(m_chart->animationOptions(), animationOptions);
220 220 }
221 221
222 222 void tst_QChart::axisX_data()
223 223 {
224 224
225 225 }
226 226
227 227 void tst_QChart::axisX()
228 228 {
229 229 QVERIFY(m_chart->axisX());
230 QChartAxis* axis = m_chart->axisX();
230 QAxis* axis = m_chart->axisX();
231 231 createTestData();
232 232 //it should be the same axis
233 233 QCOMPARE(axis,m_chart->axisX());
234 234 }
235 235
236 236 void tst_QChart::axisY_data()
237 237 {
238 QTest::addColumn<QChartAxis*>("axis0");
239 QTest::addColumn<QChartAxis*>("axis1");
240 QTest::addColumn<QChartAxis*>("axis2");
241 QTest::newRow("1 defualt, 2 optional") << (QChartAxis*)0 << new QChartAxis() << new QChartAxis();
242 QTest::newRow("3 optional") << new QChartAxis() << new QChartAxis() << new QChartAxis();
238 QTest::addColumn<QAxis*>("axis0");
239 QTest::addColumn<QAxis*>("axis1");
240 QTest::addColumn<QAxis*>("axis2");
241 QTest::newRow("1 defualt, 2 optional") << (QAxis*)0 << new QAxis() << new QAxis();
242 QTest::newRow("3 optional") << new QAxis() << new QAxis() << new QAxis();
243 243 }
244 244
245 245
246 246 void tst_QChart::axisY()
247 247 {
248 QFETCH(QChartAxis*, axis0);
249 QFETCH(QChartAxis*, axis1);
250 QFETCH(QChartAxis*, axis2);
248 QFETCH(QAxis*, axis0);
249 QFETCH(QAxis*, axis1);
250 QFETCH(QAxis*, axis2);
251 251
252 QChartAxis* defaultAxisY = m_chart->axisY();
252 QAxis* defaultAxisY = m_chart->axisY();
253 253
254 254 QVERIFY2(defaultAxisY, "Missing axisY.");
255 255
256 256 QLineSeries* series0 = new QLineSeries();
257 257 m_chart->addSeries(series0, axis0);
258 258
259 259 QLineSeries* series1 = new QLineSeries();
260 260 m_chart->addSeries(series1, axis1);
261 261
262 262 QLineSeries* series2 = new QLineSeries();
263 263 m_chart->addSeries(series2, axis2);
264 264
265 265 if (!axis0)
266 266 axis0 = defaultAxisY;
267 267 if (!axis1)
268 268 axis1 = defaultAxisY;
269 269 if (!axis2)
270 270 axis2 = defaultAxisY;
271 271
272 272 QVERIFY(m_chart->axisY(series0) == axis0);
273 273 QVERIFY(m_chart->axisY(series1) == axis1);
274 274 QVERIFY(m_chart->axisY(series2) == axis2);
275 275 }
276 276
277 277 void tst_QChart::backgroundBrush_data()
278 278 {
279 279 QTest::addColumn<QBrush>("backgroundBrush");
280 280 QTest::newRow("null") << QBrush();
281 281 QTest::newRow("blue") << QBrush(Qt::blue);
282 282 QTest::newRow("white") << QBrush(Qt::white);
283 283 QTest::newRow("black") << QBrush(Qt::black);
284 284 }
285 285
286 286 void tst_QChart::backgroundBrush()
287 287 {
288 288 QFETCH(QBrush, backgroundBrush);
289 289 m_chart->setBackgroundBrush(backgroundBrush);
290 290 QCOMPARE(m_chart->backgroundBrush(), backgroundBrush);
291 291 }
292 292
293 293 void tst_QChart::backgroundPen_data()
294 294 {
295 295 QTest::addColumn<QPen>("backgroundPen");
296 296 QTest::newRow("null") << QPen();
297 297 QTest::newRow("blue") << QPen(Qt::blue);
298 298 QTest::newRow("white") << QPen(Qt::white);
299 299 QTest::newRow("black") << QPen(Qt::black);
300 300 }
301 301
302 302
303 303 void tst_QChart::backgroundPen()
304 304 {
305 305 QFETCH(QPen, backgroundPen);
306 306 m_chart->setBackgroundPen(backgroundPen);
307 307 QCOMPARE(m_chart->backgroundPen(), backgroundPen);
308 308 }
309 309
310 310 void tst_QChart::isBackgroundVisible_data()
311 311 {
312 312 QTest::addColumn<bool>("isBackgroundVisible");
313 313 QTest::newRow("true") << true;
314 314 QTest::newRow("false") << false;
315 315 }
316 316
317 317 void tst_QChart::isBackgroundVisible()
318 318 {
319 319 QFETCH(bool, isBackgroundVisible);
320 320 m_chart->setBackgroundVisible(isBackgroundVisible);
321 321 QCOMPARE(m_chart->isBackgroundVisible(), isBackgroundVisible);
322 322
323 323 }
324 324
325 325 void tst_QChart::legend_data()
326 326 {
327 327
328 328 }
329 329
330 330 void tst_QChart::legend()
331 331 {
332 332 QVERIFY(m_chart->legend());
333 333 }
334 334
335 335 void tst_QChart::margins_data()
336 336 {
337 337
338 338 }
339 339
340 340 void tst_QChart::margins()
341 341 {QTest::addColumn<int>("seriesCount");
342 342 QTest::newRow("0") << 0;
343 343 QTest::newRow("-1") << -1;
344 344 createTestData();
345 345 QRectF rect = m_chart->geometry();
346 346
347 347 QVERIFY(m_chart->margins().top()+m_chart->margins().bottom() < rect.height());
348 348 QVERIFY(m_chart->margins().left()+m_chart->margins().right() < rect.width());
349 349
350 350 }
351 351
352 352 void tst_QChart::removeAllSeries_data()
353 353 {
354 354
355 355 }
356 356
357 357 void tst_QChart::removeAllSeries()
358 358 {
359 359 QLineSeries* series0 = new QLineSeries(this);
360 360 QLineSeries* series1 = new QLineSeries(this);
361 361 QLineSeries* series2 = new QLineSeries(this);
362 362
363 363 m_chart->addSeries(series0);
364 364 m_chart->addSeries(series1);
365 365 m_chart->addSeries(series2);
366 366 m_view->show();
367 367 QTest::qWaitForWindowShown(m_view);
368 368
369 369 QVERIFY(m_chart->axisY(series0)!=0);
370 370 QVERIFY(m_chart->axisY(series1)!=0);
371 371 QVERIFY(m_chart->axisY(series2)!=0);
372 372
373 373 m_chart->removeAllSeries();
374 374
375 375 QVERIFY(m_chart->axisY(series0)==0);
376 376 QVERIFY(m_chart->axisY(series1)==0);
377 377 QVERIFY(m_chart->axisY(series2)==0);
378 378 }
379 379
380 380 void tst_QChart::removeSeries_data()
381 381 {
382 382 addSeries_data();
383 383 }
384 384
385 385 void tst_QChart::removeSeries()
386 386 {
387 387 QFETCH(QAbstractSeries *, series);
388 QFETCH(QChartAxis *, axis);
388 QFETCH(QAxis *, axis);
389 389 m_view->show();
390 390 QTest::qWaitForWindowShown(m_view);
391 391 if(!axis) axis = m_chart->axisY();
392 392 m_chart->addSeries(series,axis);
393 393 QCOMPARE(m_chart->axisY(series),axis);
394 394 m_chart->removeSeries(series);
395 395 QVERIFY(m_chart->axisY(series)==0);
396 396 }
397 397
398 398 void tst_QChart::scrollDown_data()
399 399 {
400 400
401 401 }
402 402
403 403 void tst_QChart::scrollDown()
404 404 {
405 405 createTestData();
406 406 qreal min = m_chart->axisY()->min();
407 407 m_chart->scrollDown();
408 408 QVERIFY(m_chart->axisY()->min()<min);
409 409 }
410 410
411 411 void tst_QChart::scrollLeft_data()
412 412 {
413 413
414 414 }
415 415
416 416 void tst_QChart::scrollLeft()
417 417 {
418 418 createTestData();
419 419 qreal min = m_chart->axisX()->min();
420 420 m_chart->scrollLeft();
421 421 QVERIFY(m_chart->axisX()->min()<min);
422 422 }
423 423
424 424 void tst_QChart::scrollRight_data()
425 425 {
426 426
427 427 }
428 428
429 429 void tst_QChart::scrollRight()
430 430 {
431 431 createTestData();
432 432 qreal min = m_chart->axisX()->min();
433 433 m_chart->scrollRight();
434 434 QVERIFY(m_chart->axisX()->min()>min);
435 435 }
436 436
437 437 void tst_QChart::scrollUp_data()
438 438 {
439 439
440 440 }
441 441
442 442 void tst_QChart::scrollUp()
443 443 {
444 444 createTestData();
445 445 qreal min = m_chart->axisY()->min();
446 446 m_chart->scrollUp();
447 447 QVERIFY(m_chart->axisY()->min()>min);
448 448 }
449 449
450 450 void tst_QChart::theme_data()
451 451 {
452 452 QTest::addColumn<QChart::ChartTheme>("theme");
453 453 QTest::newRow("ChartThemeBlueCerulean") << QChart::ChartThemeBlueCerulean;
454 454 QTest::newRow("ChartThemeBlueIcy") << QChart::ChartThemeBlueIcy;
455 455 QTest::newRow("ChartThemeBlueNcs") << QChart::ChartThemeBlueNcs;
456 456 QTest::newRow("ChartThemeBrownSand") << QChart::ChartThemeBrownSand;
457 457 QTest::newRow("ChartThemeDark") << QChart::ChartThemeDark;
458 458 QTest::newRow("hartThemeHighContrast") << QChart::ChartThemeHighContrast;
459 459 QTest::newRow("ChartThemeLight") << QChart::ChartThemeLight;
460 460 }
461 461
462 462 void tst_QChart::theme()
463 463 {
464 464 QFETCH(QChart::ChartTheme, theme);
465 465 createTestData();
466 466 m_chart->setTheme(theme);
467 467 QVERIFY(m_chart->theme()==theme);
468 468 }
469 469
470 470 void tst_QChart::title_data()
471 471 {
472 472 QTest::addColumn<QString>("title");
473 473 QTest::newRow("null") << QString();
474 474 QTest::newRow("foo") << QString("foo");
475 475 }
476 476
477 477 void tst_QChart::title()
478 478 {
479 479 QFETCH(QString, title);
480 480 m_chart->setTitle(title);
481 481 QCOMPARE(m_chart->title(), title);
482 482 }
483 483
484 484 void tst_QChart::titleBrush_data()
485 485 {
486 486 QTest::addColumn<QBrush>("titleBrush");
487 487 QTest::newRow("null") << QBrush();
488 488 QTest::newRow("blue") << QBrush(Qt::blue);
489 489 QTest::newRow("white") << QBrush(Qt::white);
490 490 QTest::newRow("black") << QBrush(Qt::black);
491 491 }
492 492
493 493 void tst_QChart::titleBrush()
494 494 {
495 495 QFETCH(QBrush, titleBrush);
496 496 m_chart->setTitleBrush(titleBrush);
497 497 QCOMPARE(m_chart->titleBrush(), titleBrush);
498 498 }
499 499
500 500 void tst_QChart::titleFont_data()
501 501 {
502 502 QTest::addColumn<QFont>("titleFont");
503 503 QTest::newRow("null") << QFont();
504 504 QTest::newRow("courier") << QFont("Courier", 8, QFont::Bold, true);
505 505 }
506 506
507 507 void tst_QChart::titleFont()
508 508 {
509 509 QFETCH(QFont, titleFont);
510 510 m_chart->setTitleFont(titleFont);
511 511 QCOMPARE(m_chart->titleFont(), titleFont);
512 512 }
513 513
514 514 void tst_QChart::zoomIn_data()
515 515 {
516 516 QTest::addColumn<QRectF>("rect");
517 517 QTest::newRow("null") << QRectF();
518 518 QTest::newRow("100x100") << QRectF(10,10,100,100);
519 519 QTest::newRow("200x200") << QRectF(10,10,200,200);
520 520 }
521 521
522 522
523 523 void tst_QChart::zoomIn()
524 524 {
525 525 QFETCH(QRectF, rect);
526 526 createTestData();
527 527 QRectF marigns = m_chart->margins();
528 528 rect.adjust(marigns.left(),marigns.top(),-marigns.right(),-marigns.bottom());
529 529 qreal minX = m_chart->axisX()->min();
530 530 qreal minY = m_chart->axisY()->min();
531 531 qreal maxX = m_chart->axisX()->max();
532 532 qreal maxY = m_chart->axisY()->max();
533 533 m_chart->zoomIn(rect);
534 534 if(rect.isValid()){
535 535 QVERIFY(minX<m_chart->axisX()->min());
536 536 QVERIFY(maxX>m_chart->axisX()->max());
537 537 QVERIFY(minY<m_chart->axisY()->min());
538 538 QVERIFY(maxY>m_chart->axisY()->max());
539 539 }
540 540 }
541 541
542 542 void tst_QChart::zoomOut_data()
543 543 {
544 544
545 545 }
546 546
547 547 void tst_QChart::zoomOut()
548 548 {
549 549 createTestData();
550 550 qreal minX = m_chart->axisX()->min();
551 551 qreal minY = m_chart->axisY()->min();
552 552 qreal maxX = m_chart->axisX()->max();
553 553 qreal maxY = m_chart->axisY()->max();
554 554
555 555 m_chart->zoomIn();
556 556
557 557 QVERIFY(minX<m_chart->axisX()->min());
558 558 QVERIFY(maxX>m_chart->axisX()->max());
559 559 QVERIFY(minY<m_chart->axisY()->min());
560 560 QVERIFY(maxY>m_chart->axisY()->max());
561 561
562 562 m_chart->zoomOut();
563 563
564 564 QVERIFY(minX==m_chart->axisX()->min());
565 565 QVERIFY(maxX==m_chart->axisX()->max());
566 566 QVERIFY(minY==m_chart->axisY()->min());
567 567 QVERIFY(maxY==m_chart->axisY()->max());
568 568 }
569 569
570 570 QTEST_MAIN(tst_QChart)
571 571 #include "tst_qchart.moc"
572 572
@@ -1,215 +1,215
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21
22 22 #include <QtTest/QtTest>
23 23 #include <qchartview.h>
24 24 #include <qlineseries.h>
25 25 #include <cmath>
26 26
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29
30 30 Q_DECLARE_METATYPE(QChart*)
31 31 Q_DECLARE_METATYPE(QChartView::RubberBands)
32 32 Q_DECLARE_METATYPE(Qt::Key)
33 33
34 34 class tst_QChartView : public QObject
35 35 {
36 36 Q_OBJECT
37 37
38 38 public Q_SLOTS:
39 39 void initTestCase();
40 40 void cleanupTestCase();
41 41 void init();
42 42 void cleanup();
43 43
44 44 private Q_SLOTS:
45 45 void qchartview_data();
46 46 void qchartview();
47 47 void chart_data();
48 48 void chart();
49 49 void rubberBand_data();
50 50 void rubberBand();
51 51 void keys_data();
52 52 void keys();
53 53
54 54 private:
55 55 QChartView* m_view;
56 56 };
57 57
58 58 void tst_QChartView::initTestCase()
59 59 {
60 60 //test tracks mouse, give a while to user to relese it
61 61 QTest::qWait(1000);
62 62 }
63 63
64 64 void tst_QChartView::cleanupTestCase()
65 65 {
66 66 }
67 67
68 68 void tst_QChartView::init()
69 69 {
70 70 m_view = new QChartView(new QChart());
71 71 m_view->chart()->legend()->setVisible(false);
72 72 }
73 73
74 74 void tst_QChartView::cleanup()
75 75 {
76 76 delete m_view;
77 77 m_view =0;
78 78 }
79 79
80 80 void tst_QChartView::qchartview_data()
81 81 {
82 82
83 83 }
84 84
85 85 void tst_QChartView::qchartview()
86 86 {
87 87 QVERIFY(m_view->chart());
88 88 QCOMPARE(m_view->rubberBand(), QChartView::NoRubberBand);
89 89 m_view->show();
90 90 QTest::qWaitForWindowShown(m_view);
91 91 }
92 92
93 93 void tst_QChartView::chart_data()
94 94 {
95 95
96 96 QTest::addColumn<QChart*>("chart");
97 97 QTest::newRow("qchart") << new QChart();
98 98 }
99 99
100 100 void tst_QChartView::chart()
101 101 {
102 102 QFETCH(QChart*, chart);
103 103 QChartView* view = new QChartView(chart);
104 104 QCOMPARE(view->chart(), chart);
105 105 delete view;
106 106 }
107 107
108 108 void tst_QChartView::rubberBand_data()
109 109 {
110 110 QTest::addColumn<QChartView::RubberBands>("rubberBand");
111 111 QTest::addColumn<int>("Xcount");
112 112 QTest::addColumn<int>("Ycount");
113 113
114 114 QTest::addColumn<int>("minX");
115 115 QTest::addColumn<int>("maxX");
116 116 QTest::addColumn<int>("minY");
117 117 QTest::addColumn<int>("maxY");
118 118
119 119 QTest::newRow("HorizonalRubberBand") << QChartView::RubberBands(QChartView::HorizonalRubberBand) << 0 << 1 << 20 << 180 << 0<< 200;
120 120 QTest::newRow("VerticalRubberBand") << QChartView::RubberBands(QChartView::VerticalRubberBand) << 1 << 0 << 0 << 200 << 20<< 180;
121 121 QTest::newRow("RectangleRubberBand") << QChartView::RubberBands(QChartView::RectangleRubberBand) << 1 << 1 <<20 << 180 << 20<< 180;
122 122 }
123 123
124 124 void tst_QChartView::rubberBand()
125 125 {
126 126 QFETCH(QChartView::RubberBands, rubberBand);
127 127 QFETCH(int, Xcount);
128 128 QFETCH(int, Ycount);
129 129 QFETCH(int, minX);
130 130 QFETCH(int, maxX);
131 131 QFETCH(int, minY);
132 132 QFETCH(int, maxY);
133 133
134 134 m_view->setRubberBand(rubberBand);
135 135 QRectF padding = m_view->chart()->margins();
136 136 QCOMPARE(m_view->rubberBand(), rubberBand);
137 137
138 138 QLineSeries* line = new QLineSeries();
139 139 *line << QPointF(0, 0) << QPointF(200, 200);
140 140
141 141 m_view->chart()->addSeries(line);
142 142 m_view->resize(200 + padding.left() + padding.right(), 200 + padding.top()+ padding.bottom());
143 143 m_view->show();
144 144
145 145 //this is hack since view does not get events otherwise
146 146 m_view->setMouseTracking(true);
147 147
148 QChartAxis* axisY = m_view->chart()->axisY();
148 QAxis* axisY = m_view->chart()->axisY();
149 149 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal)));
150 QChartAxis* axisX = m_view->chart()->axisX();
150 QAxis* axisX = m_view->chart()->axisX();
151 151 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal)));
152 152
153 153 QTest::qWaitForWindowShown(m_view);
154 154 QTest::qWait(500);
155 155 QTest::mouseMove(m_view->viewport(), QPoint(minX, minY) + padding.topLeft().toPoint());
156 156 QTest::mousePress(m_view->viewport(), Qt::LeftButton, 0, QPoint(minX, minY) + padding.topLeft().toPoint());
157 157 QTest::mouseMove(m_view->viewport(), QPoint(maxX, maxY) + padding.topLeft().toPoint());
158 158 QTest::mouseRelease(m_view->viewport(), Qt::LeftButton, 0, QPoint(maxX, maxY)+ padding.topLeft().toPoint());
159 159
160 160 QCOMPARE(spy0.count(), Xcount);
161 161 QCOMPARE(spy1.count(), Ycount);
162 162
163 163 //this is hack since view does not get events otherwise
164 164 m_view->setMouseTracking(false);
165 165
166 166 QVERIFY(axisX->min() - minX < 1);
167 167 QVERIFY(axisX->max() - maxX < 1);
168 168 QVERIFY(axisY->min() - minY < 1);
169 169 QVERIFY(axisY->max() - maxY < 1);
170 170 }
171 171
172 172 void tst_QChartView::keys_data()
173 173 {
174 174 QTest::addColumn<Qt::Key>("key");
175 175 QTest::addColumn<int>("Xcount");
176 176 QTest::addColumn<int>("Ycount");
177 177 QTest::newRow("Qt::Key_Plus") << Qt::Key_Plus << 1 << 1;
178 178 QTest::newRow("Qt::Key_Minus") << Qt::Key_Minus << 1 << 1;
179 179 QTest::newRow("Qt::Key_Up") << Qt::Key_Up << 0 << 1;
180 180 QTest::newRow("Qt::Key_Down") << Qt::Key_Down << 0 << 1;
181 181 QTest::newRow("Qt::Key_Left") << Qt::Key_Left << 1 << 0;
182 182 QTest::newRow("Qt::Key_Right") << Qt::Key_Right << 1 << 0;
183 183 }
184 184
185 185 void tst_QChartView::keys()
186 186 {
187 187 QFETCH(Qt::Key,key);
188 188 QFETCH(int, Xcount);
189 189 QFETCH(int, Ycount);
190 190
191 191 QRectF padding = m_view->chart()->margins();
192 192
193 193 QLineSeries* line = new QLineSeries();
194 194 *line << QPointF(0, 0) << QPointF(100, 100);
195 195
196 196 m_view->chart()->addSeries(line);
197 197 m_view->resize(100 + padding.left() + padding.right(), 100 + padding.top()+ padding.bottom());
198 198 m_view->show();
199 199
200 QChartAxis* axisY = m_view->chart()->axisY();
200 QAxis* axisY = m_view->chart()->axisY();
201 201 QSignalSpy spy0(axisY, SIGNAL(rangeChanged(qreal,qreal)));
202 QChartAxis* axisX = m_view->chart()->axisX();
202 QAxis* axisX = m_view->chart()->axisX();
203 203 QSignalSpy spy1(axisX, SIGNAL(rangeChanged(qreal,qreal)));
204 204
205 205 QTest::keyPress(m_view, key);
206 206 QTest::keyRelease(m_view, key);
207 207
208 208 QCOMPARE(spy0.count(), Ycount);
209 209 QCOMPARE(spy1.count(), Xcount);
210 210
211 211 }
212 212
213 213 QTEST_MAIN(tst_QChartView)
214 214 #include "tst_qchartview.moc"
215 215
@@ -1,244 +1,244
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "widget.h"
22 22
23 23 #include <QChart>
24 24 #include <QScatterSeries>
25 #include <QChartAxis>
25 #include <QAxis>
26 26 #include <QBarSet>
27 27 #include <QBarSeries>
28 28 #include <QLegend>
29 29
30 30 #include <QGridLayout>
31 31 #include <QPushButton>
32 32 #include <QLabel>
33 33 #include <QListWidget>
34 34 #include <QPrinter>
35 35 #include <QPrintDialog>
36 36 #include <QRadioButton>
37 37 #include <QStringList>
38 38 #include <QSqlQuery>
39 39 #include <QDebug>
40 40
41 41 QTCOMMERCIALCHART_USE_NAMESPACE
42 42
43 43 Widget::Widget(QWidget *parent)
44 44 : QWidget(parent)
45 45 {
46 46 setGeometry(100, 100, 1000, 600);
47 47
48 48 // right panel layout
49 49 barChartRadioButton = new QRadioButton(tr("Bar chart"));
50 50 barChartRadioButton->setChecked(true);
51 51 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
52 52 scatterChartRadioButton->setChecked(false);
53 53 countrieslist = new QListWidget;
54 54 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
55 55
56 56 //list of years widget
57 57 yearslist = new QListWidget;
58 58 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
59 59 for (int i = 1990; i < 2011; i++)
60 60 yearslist->addItem(QString("%1").arg(i));
61 61
62 62 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
63 63 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
64 64
65 65 QPushButton* printButton = new QPushButton(tr("Print to pdf"));
66 66 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
67 67
68 68 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
69 69 rightPanelLayout->addWidget(barChartRadioButton);
70 70 rightPanelLayout->addWidget(scatterChartRadioButton);
71 71 rightPanelLayout->addWidget(countrieslist);
72 72 rightPanelLayout->addWidget(yearslist);
73 73 rightPanelLayout->addWidget(refreshButton);
74 74 rightPanelLayout->addWidget(printButton);
75 75 rightPanelLayout->setStretch(0, 1);
76 76 rightPanelLayout->setStretch(1, 0);
77 77
78 78 QChart *chart = new QChart();
79 79 chart->setTitle("GDP by country");
80 80 chart->legend()->setVisible(true);
81 81
82 82 // main layout
83 83 chartView = new QChartView(chart);
84 84 QGridLayout* mainLayout = new QGridLayout;
85 85 mainLayout->addWidget(chartView, 0, 0);
86 86 mainLayout->addLayout(rightPanelLayout, 0, 1);
87 87 mainLayout->setColumnStretch(0,1);
88 88 setLayout(mainLayout);
89 89
90 90 // connect to the database
91 91 db = QSqlDatabase::addDatabase("QSQLITE");
92 92 db.setDatabaseName("gdpData");
93 93 if(!db.open())
94 94 {
95 95 qDebug() << "could not open database. SQLite db file missing (?)";
96 96 return;
97 97 }
98 98
99 99 // get the list of all countires and regions.
100 100 QSqlQuery query;
101 101 query.exec("SELECT DISTINCT country FROM gdp2");
102 102
103 103 // add the countries to the country filter
104 104 while (query.next()) {
105 105 countrieslist->addItem(query.value(0).toString());
106 106 }
107 107 }
108 108
109 109 Widget::~Widget()
110 110 {
111 111 //
112 112 db.close();
113 113 }
114 114
115 115 /*!
116 116 refreshes the chart
117 117 */
118 118 void Widget::refreshChart()
119 119 {
120 120 chartView->chart()->removeAllSeries();
121 121
122 122 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
123 123 QStringList selectedCountriesStrings;
124 124 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
125 125 for (int i = 0; i < selectedCountriesItems.size(); i++)
126 126 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
127 127 selectedCountriesStrings.sort();
128 128
129 129 QSqlQuery query;
130 130 // selected years items list is not sorted. copy the values to QList<int> and sort them.
131 131 QList<int> selectedYearsInts;
132 132 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
133 133 for (int i = 0; i < selectedYearsItems.size(); i++)
134 134 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
135 135 qSort(selectedYearsInts.begin(), selectedYearsInts.end());
136 136
137 137 if (barChartRadioButton->isChecked())
138 138 {
139 139 // use the sorted selected coutries list to initialize BarCategory
140 140 QStringList category;
141 141 for (int i = 0; i < selectedCountriesStrings.size(); i++)
142 142 category << selectedCountriesStrings[i];
143 143 QBarSeries* series0 = new QBarSeries(category);
144 144 series0 = new QBarSeries(category);
145 145
146 146 // prepare the selected counries SQL query
147 147 QString countriesQuery = "country IN (";
148 148 for (int i = 0; i < selectedCountriesStrings.size(); i++)
149 149 {
150 150 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
151 151 if ( i < selectedCountriesStrings.size() - 1)
152 152 countriesQuery.append(",");
153 153 else
154 154 countriesQuery.append(")");
155 155 }
156 156
157 157 // perform a query for each selected year
158 158 for (int i = 0; i < selectedYearsInts.size(); i++)
159 159 {
160 160 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
161 161 QBarSet* barSet = new QBarSet(QString::number(selectedYearsInts[i]));
162 162
163 163 // while (query.next()) {
164 164 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
165 165 // }
166 166 query.first();
167 167
168 168 // the data for some of the coutries for some years might be missing.
169 169 // QBarChart needs bars to have same size
170 170 for (int k = 0; k < selectedCountriesStrings.size(); k++)
171 171 {
172 172 if (selectedCountriesStrings[k] == query.value(0).toString())
173 173 {
174 174 *barSet << query.value(1).toReal();
175 175 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
176 176 query.next();
177 177 }
178 178 else
179 179 {
180 180 // data missing, put 0
181 181 *barSet << 0.0f;
182 182 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
183 183 }
184 184 }
185 185 series0->appendBarSet(barSet);
186 186 }
187 187 // add the serie to the chart
188 188 chartView->chart()->addSeries(series0);
189 189 }
190 190 else if (scatterChartRadioButton->isChecked())
191 191 {
192 192 QString yearsQuery = "year IN (";
193 193 for (int i = 0; i < selectedYearsInts.size(); i++)
194 194 {
195 195 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
196 196 if ( i < selectedYearsInts.size() - 1)
197 197 yearsQuery.append(",");
198 198 else
199 199 yearsQuery.append(")");
200 200 }
201 201
202 202 // perform a query for each selected country
203 203 for (int i = 0; i < selectedCountriesStrings.size(); i++)
204 204 {
205 205 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
206 206 query.first();
207 207
208 208 QScatterSeries* series = new QScatterSeries;
209 209 series->setName(selectedCountriesStrings[i]);
210 210 // the data for some of the coutries for some years might be missing.
211 211 for (int k = 0; k < selectedYearsInts.size(); k++)
212 212 {
213 213 if (selectedYearsInts[k] == query.value(0).toInt())
214 214 {
215 215 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
216 216 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
217 217 query.next();
218 218 }
219 219 else
220 220 {
221 221 // data missing, put 0
222 222 *series << QPointF(selectedYearsInts[k] , 0.0f);
223 223 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
224 224 }
225 225 }
226 226 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
227 227 chartView->chart()->addSeries(series);
228 228 }
229 229 chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
230 230 }
231 231 }
232 232
233 233 void Widget::printChart()
234 234 {
235 235 QPrinter printer;
236 236 // QPrinter printer(QPrinter::HighResolution);
237 237 printer.setOutputFormat(QPrinter::PdfFormat);
238 238 printer.setOrientation(QPrinter::Landscape);
239 239 printer.setOutputFileName("print.pdf");
240 240
241 241 QPainter painter;
242 242 painter.begin(&printer);
243 243 chartView->render(&painter);
244 244 }
@@ -1,336 +1,336
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "tablewidget.h"
22 22 #include <QGridLayout>
23 23 #include <QTableView>
24 24 #include <QStyledItemDelegate>
25 25 #include <QLineSeries>
26 26 #include <QSplineSeries>
27 27 #include <QScatterSeries>
28 28 #include "customtablemodel.h"
29 29 #include <QPieSeries>
30 30 #include <QPieSlice>
31 31 #include <QAreaSeries>
32 32 #include <QBarSeries>
33 33 #include <QBarSet>
34 34 #include <QPushButton>
35 35 #include <QRadioButton>
36 36 #include <QLabel>
37 37 #include <QSpinBox>
38 38 #include <QTime>
39 39
40 40 TableWidget::TableWidget(QWidget *parent)
41 41 : QWidget(parent)
42 42 {
43 43 setGeometry(100, 100, 1000, 600);
44 44 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
45 45 // create simple model for storing data
46 46 // user's table data model
47 47 m_model = new CustomTableModel;
48 48 m_tableView = new QTableView;
49 49 m_tableView->setModel(m_model);
50 50 m_tableView->setMinimumHeight(300);
51 51 // tableView->setMinimumSize(340, 480);
52 52 // tableView->setItemDelegate(new QStyledItemDelegate);
53 53 m_chart = new QChart;
54 54 m_chartView = new QChartView(m_chart);
55 55 m_chartView->setRenderHint(QPainter::Antialiasing);
56 56 m_chartView->setMinimumSize(640, 480);
57 57
58 58 // add, remove data buttons
59 59 QPushButton* addRowAboveButton = new QPushButton("Add row above");
60 60 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
61 61
62 62 QPushButton* addRowBelowButton = new QPushButton("Add row below");
63 63 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
64 64
65 65 QPushButton* removeRowButton = new QPushButton("Remove row");
66 66 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
67 67
68 68 QLabel *spinBoxLabel = new QLabel("Rows affected:");
69 69
70 70 // spin box for setting number of affected items (add, remove)
71 71 m_linesCountSpinBox = new QSpinBox;
72 72 m_linesCountSpinBox->setRange(1, 10);
73 73 m_linesCountSpinBox->setValue(1);
74 74
75 75 // buttons layout
76 76 QVBoxLayout* buttonsLayout = new QVBoxLayout;
77 77 buttonsLayout->addWidget(spinBoxLabel);
78 78 buttonsLayout->addWidget(m_linesCountSpinBox);
79 79 buttonsLayout->addWidget(addRowAboveButton);
80 80 buttonsLayout->addWidget(addRowBelowButton);
81 81 buttonsLayout->addWidget(removeRowButton);
82 82 buttonsLayout->addStretch();
83 83
84 84 // chart type radio buttons
85 85 m_lineRadioButton = new QRadioButton("Line");
86 86 m_splineRadioButton = new QRadioButton("Spline");
87 87 m_scatterRadioButton = new QRadioButton("Scatter");
88 88 m_pieRadioButton = new QRadioButton("Pie");
89 89 m_areaRadioButton = new QRadioButton("Area");
90 90 m_barRadioButton = new QRadioButton("Bar");
91 91
92 92 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
93 93 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 94 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
95 95 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
96 96 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
97 97 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
98 98 m_lineRadioButton->setChecked(true);
99 99
100 100 // radio buttons layout
101 101 QVBoxLayout* radioLayout = new QVBoxLayout;
102 102 radioLayout->addWidget(m_lineRadioButton);
103 103 radioLayout->addWidget(m_splineRadioButton);
104 104 radioLayout->addWidget(m_scatterRadioButton);
105 105 radioLayout->addWidget(m_pieRadioButton);
106 106 radioLayout->addWidget(m_areaRadioButton);
107 107 radioLayout->addWidget(m_barRadioButton);
108 108 radioLayout->addStretch();
109 109
110 110 // create main layout
111 111 QGridLayout* mainLayout = new QGridLayout;
112 112 mainLayout->addLayout(buttonsLayout, 1, 1);
113 113 mainLayout->addLayout(radioLayout, 2, 1);
114 114 mainLayout->addWidget(m_tableView, 1, 0);
115 115 mainLayout->addWidget(m_chartView, 2, 0);
116 116 setLayout(mainLayout);
117 117 m_lineRadioButton->setFocus();
118 118 }
119 119
120 120 void TableWidget::addRowAbove()
121 121 {
122 122 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
123 123
124 124 }
125 125
126 126 void TableWidget::addRowBelow()
127 127 {
128 128 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
129 129
130 130 }
131 131
132 132 void TableWidget::removeRow()
133 133 {
134 134 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
135 135 }
136 136
137 137 void TableWidget::updateChartType(bool toggle)
138 138 {
139 139 // this if is needed, so that the function is only called once.
140 140 // For the radioButton that was enabled.
141 141 if (toggle) {
142 142 m_chart->removeAllSeries();
143 143
144 144 // renable axes of the chart (pie hides them)
145 145 // x axis
146 QChartAxis *axis = m_chart->axisX();
146 QAxis *axis = m_chart->axisX();
147 147 axis->setAxisVisible(true);
148 148 axis->setGridLineVisible(true);
149 149 axis->setLabelsVisible(true);
150 150
151 151 // y axis
152 152 axis = m_chart->axisY();
153 153 axis->setAxisVisible(true);
154 154 axis->setGridLineVisible(true);
155 155 axis->setLabelsVisible(true);
156 156
157 157 m_model->clearMapping();
158 158
159 159 QString seriesColorHex = "#000000";
160 160 QPen pen;
161 161 pen.setWidth(2);
162 162
163 163 if (m_lineRadioButton->isChecked())
164 164 {
165 165 // series 1
166 166 m_series = new QLineSeries;
167 167 m_series->setModel(m_model);
168 168 m_series->setModelMapping(0,1, Qt::Vertical);
169 169 // m_series->setModelMappingRange(1, 4);
170 170 m_chart->addSeries(m_series);
171 171 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
172 172 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
173 173
174 174 // series 2
175 175 m_series = new QLineSeries;
176 176 m_series->setModel(m_model);
177 177 m_series->setModelMapping(2,3, Qt::Vertical);
178 178 m_chart->addSeries(m_series);
179 179 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
180 180 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
181 181
182 182 // series 3
183 183 m_series = new QLineSeries;
184 184 m_series->setModel(m_model);
185 185 m_series->setModelMapping(4,5, Qt::Vertical);
186 186 // m_series->setModelMappingRange(2, 0);
187 187 m_chart->addSeries(m_series);
188 188 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
189 189 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
190 190 }
191 191 else if (m_splineRadioButton->isChecked())
192 192 {
193 193 // series 1
194 194 m_series = new QSplineSeries;
195 195 m_series->setModel(m_model);
196 196 m_series->setModelMapping(0,1, Qt::Vertical);
197 197 // m_series->setModelMappingRange(1, 4);
198 198 // series->setModelMapping(0,1, Qt::Horizontal);
199 199 m_chart->addSeries(m_series);
200 200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
201 201 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
202 202
203 203 // series 2
204 204 m_series = new QSplineSeries;
205 205 m_series->setModel(m_model);
206 206 m_series->setModelMapping(2,3, Qt::Vertical);
207 207 // m_series->setModelMappingRange(0, 0);
208 208 // series->setModelMapping(2,3, Qt::Horizontal);
209 209 m_chart->addSeries(m_series);
210 210 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
211 211 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
212 212
213 213 // series 3
214 214 m_series = new QSplineSeries;
215 215 m_series->setModel(m_model);
216 216 m_series->setModelMapping(4,5, Qt::Vertical);
217 217 // m_series->setModelMappingRange(2, 0);
218 218 // series->setModelMapping(4,5, Qt::Horizontal);
219 219 m_chart->addSeries(m_series);
220 220 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
221 221 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
222 222 }
223 223 else if (m_scatterRadioButton->isChecked())
224 224 {
225 225 // series 1
226 226 m_series = new QScatterSeries;
227 227 m_series->setModel(m_model);
228 228 m_series->setModelMapping(0,1, Qt::Vertical);
229 229 // m_series->setModelMappingRange(2, 0);
230 230 // series->setModelMapping(0,1, Qt::Horizontal);
231 231 m_chart->addSeries(m_series);
232 232
233 233 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
234 234 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
235 235
236 236 // series 2
237 237 m_series = new QScatterSeries;
238 238 m_series->setModel(m_model);
239 239 m_series->setModelMapping(2,3, Qt::Vertical);
240 240 // m_series->setModelMappingRange(1, 6);
241 241 // series->setModelMapping(2,3, Qt::Horizontal);
242 242 m_chart->addSeries(m_series);
243 243
244 244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
245 245 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
246 246
247 247 // series 3
248 248 m_series = new QScatterSeries;
249 249 m_series->setModel(m_model);
250 250 m_series->setModelMapping(4,5, Qt::Vertical);
251 251 // series->setModelMapping(4,5, Qt::Horizontal);
252 252 m_chart->addSeries(m_series);
253 253 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
254 254 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
255 255 }
256 256 else if (m_pieRadioButton->isChecked())
257 257 {
258 258 // pie 1
259 259 QPieSeries* pieSeries = new QPieSeries;
260 260 pieSeries->setModel(m_model);
261 261 pieSeries->setModelMapping(0,0, Qt::Vertical);
262 262 pieSeries->setLabelsVisible(true);
263 263 pieSeries->setPieSize(0.4);
264 264 pieSeries->setHorizontalPosition(0.2);
265 265 pieSeries->setVerticalPosition(0.35);
266 266
267 267 m_chart->addSeries(pieSeries);
268 268 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
269 269 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
270 270
271 271 // pie 2
272 272 pieSeries = new QPieSeries;
273 273 pieSeries->setModel(m_model);
274 274 pieSeries->setModelMapping(1,1, Qt::Vertical);
275 275 pieSeries->setLabelsVisible(true);
276 276 pieSeries->setPieSize(0.4);
277 277 pieSeries->setHorizontalPosition(0.8);
278 278 pieSeries->setVerticalPosition(0.35);
279 279 m_chart->addSeries(pieSeries);
280 280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
281 281 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
282 282
283 283 // pie 3
284 284 pieSeries = new QPieSeries;
285 285 pieSeries->setModel(m_model);
286 286 pieSeries->setModelMapping(2,2, Qt::Vertical);
287 287 pieSeries->setLabelsVisible(true);
288 288 pieSeries->setPieSize(0.4);
289 289 pieSeries->setHorizontalPosition(0.5);
290 290 pieSeries->setVerticalPosition(0.65);
291 291 m_chart->addSeries(pieSeries);
292 292 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
293 293 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
294 294 }
295 295 else if (m_areaRadioButton->isChecked())
296 296 {
297 297 QLineSeries* upperLineSeries = new QLineSeries;
298 298 upperLineSeries->setModel(m_model);
299 299 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
300 300 // upperLineSeries->setModelMappingRange(1, 5);
301 301 QLineSeries* lowerLineSeries = new QLineSeries;
302 302 lowerLineSeries->setModel(m_model);
303 303 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
304 304 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
305 305 m_chart->addSeries(areaSeries);
306 306 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
307 307 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
308 308 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
309 309 }
310 310 else if (m_barRadioButton->isChecked())
311 311 {
312 312 QBarSeries* barSeries = new QBarSeries(QStringList());
313 313 barSeries->setModel(m_model);
314 314 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
315 315 m_chart->addSeries(barSeries);
316 316 QList<QBarSet*> barsets = barSeries->barSets();
317 317 for (int i = 0; i < barsets.count(); i++) {
318 318 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
319 319 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
320 320 }
321 321 }
322 322
323 323
324 324 m_chart->axisX()->setRange(0, 500);
325 325 m_chart->axisY()->setRange(0, 120);
326 326
327 327 // repaint table view colors
328 328 m_tableView->repaint();
329 329 m_tableView->setFocus();
330 330 }
331 331 }
332 332
333 333 TableWidget::~TableWidget()
334 334 {
335
335
336 336 }
General Comments 0
You need to be logged in to leave comments. Login now