##// END OF EJS Templates
Adds axis setRange implementation
Michal Klocek -
r400:911bb45e5e52
parent child
Show More
@@ -1,67 +1,71
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartview.h>
4 4 #include <qlineseries.h>
5 5 #include <qchart.h>
6 6 #include <qchartaxis.h>
7 7 #include <cmath>
8 8
9 9 QTCOMMERCIALCHART_USE_NAMESPACE
10 10
11 11 #define PI 3.14159265358979
12 12
13 13 int main(int argc, char *argv[])
14 14 {
15 15 QApplication a(argc, argv);
16 16
17 17 QMainWindow window;
18 18
19 19 QLineSeries* series0 = new QLineSeries();
20 20 QPen blue(Qt::blue);
21 21 blue.setWidth(3);
22 22 series0->setPen(blue);
23 23 QLineSeries* series1 = new QLineSeries();
24 24 QPen red(Qt::red);
25 25 red.setWidth(3);
26 26 series1->setPen(red);
27 27
28 28 int numPoints = 100;
29 29
30 30 for (int x = 0; x <= numPoints; ++x) {
31 31 series0->add(x, fabs(sin(PI/50*x)*100));
32 32 series1->add(x, fabs(cos(PI/50*x)*100));
33 33 }
34 34
35 35 QChartView* chartView = new QChartView(&window);
36 36
37 37 chartView->setRenderHint(QPainter::Antialiasing);
38 38 chartView->setChartTitle("This is custom axis chart example");
39 39 chartView->addSeries(series0);
40 40 chartView->addSeries(series1);
41 41
42 42 QLinearGradient backgroundGradient;
43 43 backgroundGradient.setColorAt(0.0, Qt::white);
44 44 backgroundGradient.setColorAt(1.0, QRgb(0xffff80));
45 45 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
46 46 chartView->setChartBackgroundBrush(backgroundGradient);
47 47
48 48 QChartAxis* axisX = chartView->axisX();
49 49 axisX->setLabelsAngle(45);
50 50 axisX->setGridPen(Qt::DashLine);
51 51 axisX->addAxisTickLabel(0,"low");
52 52 axisX->addAxisTickLabel(50,"medium");
53 53 axisX->addAxisTickLabel(100,"High");
54 axisX->setMin(-10);
55 axisX->setMax(200);
54 56
55 57 QChartAxis* axisY = chartView->axisY();
56 58 axisY->setLabelsAngle(45);
57 59 axisY->setShadesBrush(Qt::yellow);
58 60 axisY->addAxisTickLabel(0,"low");
59 61 axisY->addAxisTickLabel(50,"medium");
60 62 axisY->addAxisTickLabel(100,"High");
63 axisY->setMin(-10);
64 axisY->setMax(200);
61 65
62 66 window.setCentralWidget(chartView);
63 67 window.resize(400, 300);
64 68 window.show();
65 69
66 70 return a.exec();
67 71 }
@@ -1,358 +1,379
1 1 #include "chartdataset_p.h"
2 2 #include "qchartaxis.h"
3 3 //series
4 4 #include "qlineseries.h"
5 5 #include "qbarseries.h"
6 6 #include "qstackedbarseries.h"
7 7 #include "qpercentbarseries.h"
8 8 #include "qpieseries.h"
9 9 #include "qscatterseries.h"
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 14 m_axisX(new QChartAxis(this)),
15 15 m_axisY(new QChartAxis(this)),
16 16 m_domainIndex(0),
17 17 m_axisXInitialized(false)
18 18 {
19 19 }
20 20
21 21 ChartDataSet::~ChartDataSet()
22 22 {
23 23 // TODO Auto-generated destructor stub
24 24 }
25 25
26 26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 27 {
28 28 int i = m_domainMap.count(axisY);
29 29 if(i == 0){
30 30 return Domain();
31 31 }
32 32 i = i - m_domainIndex -1;
33 33 return m_domainMap.values(axisY).at(i);
34 34 }
35 35
36 36 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
37 37 {
38 38 // TODO: we should check the series not already added
39 39
40 40 series->setParent(this); // take ownership
41 41 clearDomains();
42 42
43 43 if(axisY==0) axisY = m_axisY;
44 44 axisY->setParent(this); // take ownership
45 45
46 46 QList<QSeries*> seriesList = m_seriesMap.values(axisY);
47 47
48 48 QList<Domain> domainList = m_domainMap.values(axisY);
49 49
50 50 Q_ASSERT(domainList.size()<=1);
51 51
52 52 Domain domain;
53 53
54 54 if(domainList.size()>0) domain = domainList.at(0);
55 55
56 56 switch(series->type())
57 57 {
58 58 case QSeries::SeriesTypeLine: {
59 59
60 60 QLineSeries* xyseries = static_cast<QLineSeries*>(series);
61 61
62 62 for (int i = 0; i < xyseries->count(); i++)
63 63 {
64 64 qreal x = xyseries->x(i);
65 65 qreal y = xyseries->y(i);
66 66 domain.m_minX = qMin(domain.m_minX,x);
67 67 domain.m_minY = qMin(domain.m_minY,y);
68 68 domain.m_maxX = qMax(domain.m_maxX,x);
69 69 domain.m_maxY = qMax(domain.m_maxY,y);
70 70 }
71 71 break;
72 72 }
73 73 case QSeries::SeriesTypeBar: {
74 74 qDebug() << "QChartSeries::SeriesTypeBar";
75 75 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
76 76 qreal x = barSeries->categoryCount();
77 77 qreal y = barSeries->max();
78 78 domain.m_minX = qMin(domain.m_minX,x);
79 79 domain.m_minY = qMin(domain.m_minY,y);
80 80 domain.m_maxX = qMax(domain.m_maxX,x);
81 81 domain.m_maxY = qMax(domain.m_maxY,y);
82 82 break;
83 83 }
84 84 case QSeries::SeriesTypeStackedBar: {
85 85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86 86
87 87 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
88 88 qreal x = stackedBarSeries->categoryCount();
89 89 qreal y = stackedBarSeries->maxCategorySum();
90 90 domain.m_minX = qMin(domain.m_minX,x);
91 91 domain.m_minY = qMin(domain.m_minY,y);
92 92 domain.m_maxX = qMax(domain.m_maxX,x);
93 93 domain.m_maxY = qMax(domain.m_maxY,y);
94 94 break;
95 95 }
96 96 case QSeries::SeriesTypePercentBar: {
97 97 qDebug() << "QChartSeries::SeriesTypePercentBar";
98 98
99 99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
100 100 qreal x = percentBarSeries->categoryCount();
101 101 domain.m_minX = qMin(domain.m_minX,x);
102 102 domain.m_minY = 0;
103 103 domain.m_maxX = qMax(domain.m_maxX,x);
104 104 domain.m_maxY = 100;
105 105 break;
106 106 }
107 107
108 108 case QSeries::SeriesTypePie: {
109 109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 110 // TODO: domain stuff
111 111 break;
112 112 }
113 113
114 114 case QSeries::SeriesTypeScatter: {
115 115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 116 Q_ASSERT(scatterSeries);
117 117 foreach (QPointF point, scatterSeries->data()) {
118 118 domain.m_minX = qMin(domain.m_minX, point.x());
119 119 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 120 domain.m_minY = qMin(domain.m_minY, point.y());
121 121 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 122 }
123 123 break;
124 124 }
125 125
126 126 default: {
127 127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
128 128 return;
129 129 break;
130 130 }
131 131
132 132 }
133 133
134 134 if(!m_domainMap.contains(axisY))
135 135 {
136 136 emit axisAdded(axisY);
137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
137 QObject::connect(axisY,SIGNAL(rangeChanged(QChartAxis*)),this,SLOT(handleRangeChanged(QChartAxis*)));
139 138 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
140 139 }
141 m_domainMap.replace(axisY,domain);
142 m_seriesMap.insert(axisY,series);
143 140
144 141 if(!m_axisXInitialized)
145 142 {
146 143 emit axisAdded(axisX());
147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
144 QObject::connect(axisX(),SIGNAL(rangeChanged(QChartAxis*)),this,SLOT(handleRangeChanged(QChartAxis*)));
149 145 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
150 146 m_axisXInitialized=true;
151 147 }
152 148
153 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
154 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
155 emit axisRangeChanged(axisY,ylabels);
156 emit axisRangeChanged(axisX(),xlabels);
149 m_domainMap.replace(axisY,domain);
150 m_seriesMap.insert(axisY,series);
157 151 emit seriesAdded(series);
158 emit seriesDomainChanged(series,domain);
152 setDomain(m_domainIndex);
159 153
160 154 }
161 155
162 156 void ChartDataSet::removeSeries(QSeries* series)
163 157 {
164 158 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
165 159 foreach(QChartAxis* axis , keys) {
166 160 if(m_seriesMap.contains(axis,series)){
167 161 emit seriesRemoved(series);
168 162 m_seriesMap.remove(axis,series);
169 163 //remove axis if no longer there
170 164 if(!m_seriesMap.contains(axis)){
171 165 emit axisRemoved(axis);
172 166 m_domainMap.remove(axis);
173 167 if(axis != m_axisY)
174 168 delete axis;
175 169 }
176 170 series->setParent(0);
177 171 break;
178 172 }
179 173 }
180 174 }
181 175
182 176 void ChartDataSet::removeAllSeries()
183 177 {
184 178 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
185 179 foreach(QChartAxis* axis , keys) {
186 180 QList<QSeries*> seriesList = m_seriesMap.values(axis);
187 181 for(int i =0 ; i < seriesList.size();i++ )
188 182 {
189 183 emit seriesRemoved(seriesList.at(i));
190 184 delete(seriesList.at(i));
191 185 }
192 186 m_seriesMap.remove(axis);
193 187 m_domainMap.remove(axis);
194 188 emit axisRemoved(axis);
195 189 if(axis != m_axisY) delete axis;
196 190 }
197 191 m_domainIndex=0;
198 192 }
199 193
200 194 bool ChartDataSet::nextDomain()
201 195 {
202 196 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
203 197
204 198 if (m_domainIndex < limit) {
205 199 m_domainIndex++;
206 200 setDomain(m_domainIndex);
207 201 return true;
208 202 }
209 203 else {
210 204 return false;
211 205 }
212 206 }
213 207
214 208 bool ChartDataSet::previousDomain()
215 209 {
216 210 if (m_domainIndex > 0) {
217 211 m_domainIndex--;
218 212 setDomain(m_domainIndex);
219 213 return true;
220 214 }
221 215 else {
222 216 return false;
223 217 }
224 218 }
225 219
226 220 void ChartDataSet::setDomain(int index)
227 221 {
228 222 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
229 223
230 224 if(domainList.count()==0) return;
231 225
232 226 Domain domain;
233 227
234 228 foreach (QChartAxis* axis , domainList) {
235 229 int i = m_domainMap.count(axis) - index -1;
236 230 Q_ASSERT(i>=0);
237 231 domain = m_domainMap.values(axis).at(i);
238 232 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
239 233 QList<QSeries*> seriesList = m_seriesMap.values(axis);
240 234 foreach(QSeries* series, seriesList) {
241 235 emit seriesDomainChanged(series,domain);
242 236 }
237 axis->setRange(domain.m_minY,domain.m_maxY);
243 238 emit axisRangeChanged(axis,labels);
239
244 240 }
245 241
246 242 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
243 axisX()->setRange(domain.m_minX,domain.m_maxY);
247 244 emit axisRangeChanged(axisX(),labels);
248 245 }
249 246
250 247 void ChartDataSet::clearDomains(int toIndex)
251 248 {
252 249 Q_ASSERT(toIndex>=0);
253 250
254 251 m_domainIndex = toIndex;
255 252
256 253 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
257 254
258 255 foreach (QChartAxis* key , keys)
259 256 {
260 257 QList<Domain> domains = m_domainMap.values(key);
261 258 m_domainMap.remove(key);
262 259 int i = domains.size() - toIndex - 1;
263 260 while(i--){
264 261 domains.removeFirst();
265 262 }
266 263 for(int j=domains.size()-1; j>=0 ;j--)
267 264 m_domainMap.insert(key,domains.at(j));
268 265 }
269 266 }
270 267
271 268 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
272 269 {
273 270 Q_ASSERT(rect.isValid());
274 271 Q_ASSERT(viewport.isValid());
275 272
276 273 clearDomains(m_domainIndex);
277 274
278 275 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
279 276
280 277 Domain domain;
281 278
282 279 foreach (QChartAxis* axis , domainList){
283 280 domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height());
284 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
285 QList<QSeries*> seriesList = m_seriesMap.values(axis);
286 foreach(QSeries* series, seriesList){
287 emit seriesDomainChanged(series,domain);
288 }
289 emit axisRangeChanged(axis,labels);
290 281 m_domainMap.insert(axis,domain);
291 282 }
292 283
293 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
294 emit axisRangeChanged(axisX(),labels);
295
296 m_domainIndex++;
284 setDomain(++m_domainIndex);
297 285 }
298 286
299 287 QChartAxis* ChartDataSet::axisY(QSeries* series) const
300 288 {
301 289 if(series == 0) return m_axisY;
302 290
303 291 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
304 292
305 293 foreach(QChartAxis* axis , keys) {
306 294 if(m_seriesMap.contains(axis,series)){
307 295 return axis;
308 296 }
309 297 }
310 298 return 0;
311 299 }
312 300
313 301 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
314 302 {
315 303 Q_ASSERT(max>=min);
316 304
317 305 QStringList labels;
318 306
319 307 int ticks = axis->ticksCount()-1;
320 308
321 309 for(int i=0; i<= ticks; i++){
322 310 qreal value = min + (i * (max - min)/ ticks);
323 311 QString label = axis->axisTickLabel(value);
324 312 if(label.isEmpty()){
325 313 labels << QString::number(value);
326 314 }else{
327 315 labels << label;
328 316 }
329 317 }
330 318 return labels;
331 319 }
332 320
333 321
334 void ChartDataSet::handleMinChanged(qreal min)
322 void ChartDataSet::handleRangeChanged(QChartAxis* axis)
335 323 {
324 qreal min = axis->min();
325 qreal max = axis->max();
336 326
337 }
327 if(axis==axisX()) {
338 328
339 void ChartDataSet::handleMaxChanged(qreal max)
340 {
329 m_domainIndex=0;
330
331 clearDomains(m_domainIndex);
332
333 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
334
335 foreach (QChartAxis* axis , domainList) {
336
337 Q_ASSERT(m_domainMap.values(axis).size()==1);
338
339 Domain domain = m_domainMap.value(axis);
340 domain.m_minX=min;
341 domain.m_maxX=max;
342 m_domainMap.replace(axis,domain);
343 }
344
345 }
346 else {
347
348 QList<Domain> domains = m_domainMap.values(axis);
349 m_domainMap.remove(axis);
350
351 for(int i=0;i<domains.size();i++)
352 {
353 domains[i].m_minY=min;
354 domains[i].m_maxY=max;
355 }
356
357 for(int j=domains.size()-1; j>=0;j--)
358 m_domainMap.insert(axis,domains.at(j));
359 }
341 360
361 setDomain(m_domainIndex);
342 362 }
343 363
344 364 void ChartDataSet::handleTickChanged(QChartAxis* axis)
345 365 {
346 Domain domain = m_domainMap.value(axisY());
347 366 if(axis==axisX()){
367 Domain domain = m_domainMap.value(axisY());
348 368 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
349 369 emit axisRangeChanged(axis,labels);
350 370 }else{
371 Domain domain = m_domainMap.value(axis);
351 372 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
352 373 emit axisRangeChanged(axis,labels);
353 374 }
354 375 }
355 376
356 377 #include "moc_chartdataset_p.cpp"
357 378
358 379 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,60 +1,59
1 1 #ifndef CHARTDATASET_P_H_
2 2 #define CHARTDATASET_P_H_
3 3
4 4 #include "qseries.h"
5 5 #include "domain_p.h"
6 6 #include <QVector>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QChartAxis;
11 11
12 12 class ChartDataSet : public QObject
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 ChartDataSet(QObject* parent=0);
17 17 virtual ~ChartDataSet();
18 18
19 19 void addSeries(QSeries* series,QChartAxis *axisY = 0);
20 20 void removeSeries(QSeries* series);
21 21 void removeAllSeries();
22 22 void addDomain(const QRectF& rect, const QRectF& viewport);
23 23 bool nextDomain();
24 24 bool previousDomain();
25 25 void clearDomains(int toIndex =0);
26 26 const Domain domain(QChartAxis *axisY) const;
27 27 int domainIndex() const {return m_domainIndex;}
28 28 void setDomain(int index);
29 29
30 30 QChartAxis* axisX() const { return m_axisX;};
31 31 QChartAxis* axisY(QSeries* series = 0) const;
32 32
33 33 signals:
34 34 void seriesAdded(QSeries* series);
35 35 void seriesRemoved(QSeries* series);
36 36 void axisAdded(QChartAxis* axis);
37 37 void axisRemoved(QChartAxis* axis);
38 38 void axisRangeChanged(QChartAxis* axis, const QStringList& labels);
39 39 void seriesDomainChanged(QSeries* series,const Domain& domain);
40 40
41 41 private slots:
42 void handleMinChanged(qreal min);
43 void handleMaxChanged(qreal max);
42 void handleRangeChanged(QChartAxis*);
44 43 void handleTickChanged(QChartAxis*);
45 44
46 45 private:
47 46 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
48 47
49 48 private:
50 49 QMultiMap<QChartAxis*, Domain> m_domainMap;
51 50 QMultiMap<QChartAxis*, QSeries*> m_seriesMap;
52 51 QChartAxis* m_axisX;
53 52 QChartAxis* m_axisY;
54 53 int m_domainIndex;
55 54 bool m_axisXInitialized;
56 55 };
57 56
58 57 QTCOMMERCIALCHART_END_NAMESPACE
59 58
60 59 #endif /* CHARTENGINE_P_H_ */
@@ -1,224 +1,224
1 1 #include "qlineseries.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 /*!
6 6 \class QLineSeries
7 7 \brief The QLineSeries class is used for making line charts.
8 8
9 9 \mainclass
10 10
11 11 A line chart is used to show information as a series of data points
12 12 connected by straight lines.
13 13
14 14 \image linechart.png
15 15
16 16 Creating basic line chart is simple:
17 17 \code
18 18 QLineSeries* series = new QLineSeries();
19 19 series->add(0, 6);
20 20 series->add(2, 4);
21 21 ...
22 22 chartView->addSeries(series);
23 23 \endcode
24 24 */
25 25
26 26 /*!
27 27 \fn virtual QSeriesType QLineSeries::type() const
28 28 \brief Returns type of series.
29 29 \sa QSeries, QSeriesType
30 30 */
31 31
32 32 /*!
33 33 \fn QPen QLineSeries::pen() const
34 34 \brief Returns the pen used to draw line for this series.
35 35 \sa setPen()
36 36 */
37 37
38 38 /*!
39 39 \fn bool QLineSeries::pointsVisible() const
40 40 \brief Returns if the points are drawn for this series.
41 41 \sa setPointsVisible()
42 42 */
43 43
44 44
45 45 /*!
46 46 \fn void QLineSeries::pointReplaced(int index)
47 47 \brief \internal \a index
48 48 */
49 49
50 50 /*!
51 51 \fn void QLineSeries::pointAdded(int index)
52 52 \brief \internal \a index
53 53 */
54 54
55 55 /*!
56 56 \fn void QLineSeries::pointRemoved(int index)
57 57 \brief \internal \a index
58 58 */
59 59
60 60 /*!
61 \fn void QLineSeries::updated(int index)
61 \fn void QLineSeries::updated()
62 62 \brief \internal
63 63 */
64 64
65 65 /*!
66 66 Constructs empty series object which is a child of \a parent.
67 67 When series object is added to QChartView or QChart instance ownerships is transfered.
68 68 */
69 69 QLineSeries::QLineSeries(QObject* parent):QSeries(parent),
70 70 m_pointsVisible(false)
71 71 {
72 72 }
73 73 /*!
74 74 Destroys the object. Series added to QChartView or QChart instances are owned by those,
75 75 and are deleted when mentioned object are destroyed.
76 76 */
77 77 QLineSeries::~QLineSeries()
78 78 {
79 79 }
80 80
81 81 /*!
82 82 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
83 83 */
84 84 void QLineSeries::add(qreal x,qreal y)
85 85 {
86 86 Q_ASSERT(m_x.size() == m_y.size());
87 87 m_x<<x;
88 88 m_y<<y;
89 89 emit pointAdded(m_x.size()-1);
90 90 }
91 91
92 92 /*!
93 93 This is an overloaded function.
94 94 Adds data \a point to the series. Points are connected with lines on the chart.
95 95 */
96 96 void QLineSeries::add(const QPointF& point)
97 97 {
98 98 add(point.x(),point.y());
99 99 }
100 100
101 101 /*!
102 102 Modifies \a y value for given \a x a value.
103 103 */
104 104 void QLineSeries::replace(qreal x,qreal y)
105 105 {
106 106 int index = m_x.indexOf(x);
107 107 m_x[index]=x;
108 108 m_y[index]=y;
109 109 emit pointReplaced(index);
110 110 }
111 111
112 112 /*!
113 113 This is an overloaded function.
114 114 Replaces current y value of for given \a point x value with \a point y value.
115 115 */
116 116 void QLineSeries::replace(const QPointF& point)
117 117 {
118 118 replace(point.x(),point.y());
119 119 }
120 120
121 121 /*!
122 122 Removes current \a x and y value.
123 123 */
124 124 void QLineSeries::remove(qreal x)
125 125 {
126 126 int index = m_x.indexOf(x);
127 127 m_x.remove(index);
128 128 m_y.remove(index);
129 129 emit pointRemoved(index);
130 130 }
131 131
132 132 /*!
133 133 Removes current \a point x value. Note \a point y value is ignored.
134 134 */
135 135 void QLineSeries::remove(const QPointF& point)
136 136 {
137 137 remove(point.x());
138 138 }
139 139
140 140 /*!
141 141 Clears all the data.
142 142 */
143 143 void QLineSeries::clear()
144 144 {
145 145 m_x.clear();
146 146 m_y.clear();
147 147 }
148 148
149 149 /*!
150 150 \internal \a pos
151 151 */
152 152 qreal QLineSeries::x(int pos) const
153 153 {
154 154 return m_x.at(pos);
155 155 }
156 156
157 157 /*!
158 158 \internal \a pos
159 159 */
160 160 qreal QLineSeries::y(int pos) const
161 161 {
162 162 return m_y.at(pos);
163 163 }
164 164
165 165 /*!
166 166 Returns number of data points within series.
167 167 */
168 168 int QLineSeries::count() const
169 169 {
170 170 Q_ASSERT(m_x.size() == m_y.size());
171 171
172 172 return m_x.size();
173 173
174 174 }
175 175
176 176 /*!
177 177 Sets \a pen used for drawing given series..
178 178 */
179 179 void QLineSeries::setPen(const QPen& pen)
180 180 {
181 181 if(pen!=m_pen){
182 182 m_pen=pen;
183 183 emit updated();
184 184 }
185 185 }
186 186
187 187 /*!
188 188 Sets if data points are \a visible and should be drawn on line.
189 189 */
190 190 void QLineSeries::setPointsVisible(bool visible)
191 191 {
192 192 if(m_pointsVisible!=visible){
193 193 m_pointsVisible=visible;
194 194 emit updated();
195 195 }
196 196 }
197 197
198 198 QDebug operator<< (QDebug debug, const QLineSeries series)
199 199 {
200 200 Q_ASSERT(series.m_x.size() == series.m_y.size());
201 201
202 202 int size = series.m_x.size();
203 203
204 204 for (int i=0;i<size;i++) {
205 205 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
206 206 }
207 207 return debug.space();
208 208 }
209 209
210 210 /*!
211 211 Stream operator for adding a data \a point to the series.
212 212 \sa add()
213 213 */
214 214
215 215 QLineSeries& QLineSeries::operator<< (const QPointF &point)
216 216 {
217 217 add(point);
218 218 return *this;
219 219 }
220 220
221 221
222 222 #include "moc_qlineseries.cpp"
223 223
224 224 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,353 +1,377
1 1 #include "qchartaxis.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 /*!
6 6 \class QChartAxis
7 7 \brief The QChartAxis class is used for manipulating chart's axis
8 8 and for adding optional axes to the chart.
9 9 \mainclass
10 10
11 11 There is only one x Axis, however there can be multiple y axes.
12 12 Each chart series can be bound to exactly one Y axis and the share common X axis.
13 13 Axis can be setup to show axis line with ticks, gird lines and shades.
14 14
15 15 */
16 16
17 17 /*!
18 18 \fn bool QChartAxis::isAxisVisible() const
19 19 \brief Returns if axis is visible
20 20 \sa setAxisVisible()
21 21 */
22 22
23 23 /*!
24 24 \fn QPen QChartAxis::axisPen() const
25 25 \brief Returns pen used to draw axis and ticks.
26 26 \sa setAxisPen()
27 27 */
28 28
29 29
30 30 /*!
31 31 \fn bool QChartAxis::isGridVisible() const
32 32 \brief Returns if grid is visible
33 33 \sa setGridVisible()
34 34 */
35 35
36 36 /*!
37 37 \fn QPen QChartAxis::gridPen() const
38 38 \brief Returns pen used to draw grid.
39 39 \sa setGridPen()
40 40 */
41 41
42 42 /*!
43 43 \fn bool QChartAxis::labelsVisible() const
44 44 \brief Returns if grid is visible
45 45 \sa setLabelsVisible()
46 46 */
47 47
48 48 /*!
49 49 \fn QPen QChartAxis::labelsPen() const
50 50 \brief Returns the pen used to labels.
51 51 \sa setLabelsPen()
52 52 */
53 53
54 54 /*!
55 55 \fn QBrush QChartAxis::labelsBrush() const
56 56 \brief Returns brush used to draw labels.
57 57 \sa setLabelsBrush()
58 58 */
59 59
60 60 /*!
61 61 \fn QFont QChartAxis::labelsFont() const
62 62 \brief Returns font used to draw labels.
63 63 \sa setLabelsFont()
64 64 */
65 65
66 66 /*!
67 67 \fn QFont QChartAxis::labelsAngle() const
68 68 \brief Returns angle used to draw labels.
69 69 \sa setLabelsAngle()
70 70 */
71 71
72 72 /*!
73 73 \fn bool QChartAxis::shadesVisible() const
74 74 \brief Returns if shades are visible.
75 75 \sa setShadesVisible()
76 76 */
77 77
78 78 /*!
79 79 \fn qreal QChartAxis::shadesOpacity() const
80 80 \brief Returns opacity of shades.
81 81 */
82 82
83 83 /*!
84 84 \fn QPen QChartAxis::shadesPen() const
85 85 \brief Returns pen used to draw shades.
86 86 \sa setShadesPen()
87 87 */
88 88
89 89 /*!
90 90 \fn QBrush QChartAxis::shadesBrush() const
91 91 \brief Returns brush used to draw shades.
92 92 \sa setShadesBrush()
93 93 */
94 94
95 95 /*!
96 96 \fn qreal QChartAxis::min() const
97 97 \brief Returns minimum value on the axis.
98 98 \sa setMin()
99 99 */
100 100
101 101 /*!
102 102 \fn qreal QChartAxis::max() const
103 103 \brief Returns maximim value on the axis.
104 104 \sa setMax()
105 105 */
106 106
107 107 /*!
108 108 \fn void QChartAxis::minChanged(qreal min)
109 109 \brief Axis emits signal when \a min of axis has changed.
110 110 */
111 111
112 112 /*!
113 113 \fn void QChartAxis::maxChanged(qreal max)
114 114 \brief Axis emits signal when \a max of axis has changed.
115 115 */
116 116 /*!
117 117 \fn int QChartAxis::ticksCount() const
118 118 \brief Return number of ticks on the axis
119 119 \sa setTicksCount()
120 120 */
121 121
122 122 /*!
123 123 \fn void QChartAxis::update(QChartAxis*)
124 124 \brief \internal
125 125 */
126 126
127 127 /*!
128 128 \fn void QChartAxis::ticksChanged(QChartAxis*)
129 129 \brief \internal
130 130 */
131 131
132 132 /*!
133 \fn void QChartAxis::rangeChanged(QChartAxis*)
134 \brief \internal
135 */
136
137 /*!
138 \fn void QChartAxis::updateRange(qreal min, qreal max)
139 \brief \internal \a min \a max
140 */
141
142 /*!
133 143 Constructs new axis object which is a child of \a parent. Ownership is taken by
134 144 QChatView or QChart when axis added.
135 145 */
136 146
137 147 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
138 148 m_axisVisible(true),
139 149 m_gridVisible(true),
140 150 m_labelsVisible(true),
141 151 m_labelsAngle(0),
142 152 m_shadesVisible(true),
143 153 m_shadesOpacity(1.0),
144 154 m_min(0),
145 155 m_max(0),
146 156 m_ticksCount(5)
147 157 {
148 158
149 159 }
150 160
151 161 /*!
152 162 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
153 163 */
154 164
155 165 QChartAxis::~QChartAxis()
156 166 {
157 167 }
158 168
159 169 /*!
160 170 Sets \a pen used to draw axis line and ticks.
161 171 */
162 172 void QChartAxis::setAxisPen(const QPen& pen)
163 173 {
164 174 m_axisPen=pen;
165 175 emit update(this);
166 176 }
167 177
168 178 /*!
169 179 Sets if axis and ticks are \a visible.
170 180 */
171 181 void QChartAxis::setAxisVisible(bool visible)
172 182 {
173 183 m_axisVisible=visible;
174 184 emit update(this);
175 185 }
176 186
177 187 /*!
178 188 Sets if grid is \a visible.
179 189 */
180 190 void QChartAxis::setGridVisible(bool visible)
181 191 {
182 192 m_gridVisible=visible;
183 193 emit update(this);
184 194 }
185 195
186 196 /*!
187 197 Sets \a pen used to draw grid.
188 198 */
189 199 void QChartAxis::setGridPen(const QPen& pen)
190 200 {
191 201 m_gridPen=pen;
192 202 emit update(this);
193 203 }
194 204
195 205 /*!
196 206 Sets if axis' labels are \a visible.
197 207 */
198 208 void QChartAxis::setLabelsVisible(bool visible)
199 209 {
200 210 m_labelsVisible=visible;
201 211 emit update(this);
202 212 }
203 213
204 214 /*!
205 215 Sets \a pen used to draw labels.
206 216 */
207 217 void QChartAxis::setLabelsPen(const QPen& pen)
208 218 {
209 219 m_labelsPen=pen;
210 220 emit update(this);
211 221 }
212 222
213 223 /*!
214 224 Sets \a brush used to draw labels.
215 225 */
216 226 void QChartAxis::setLabelsBrush(const QBrush& brush)
217 227 {
218 228 m_labelsBrush=brush;
219 229 emit update(this);
220 230 }
221 231
222 232 /*!
223 233 Sets \a font used to draw labels.
224 234 */
225 235 void QChartAxis::setLabelsFont(const QFont& font)
226 236 {
227 237 m_labelsFont=font;
228 238 emit update(this);
229 239 }
230 240
231 241 /*!
232 242 Sets \a angle for all the labels on given axis.
233 243 */
234 244 void QChartAxis::setLabelsAngle(int angle)
235 245 {
236 246 m_labelsAngle=angle;
237 247 emit update(this);
238 248 }
239 249
240 250 /*!
241 251 Sets if shades are \a visible.
242 252 */
243 253 void QChartAxis::setShadesVisible(bool visible)
244 254 {
245 255 m_shadesVisible=visible;
246 256 emit update(this);
247 257 }
248 258
249 259 /*!
250 260 Sets \a pen used to draw shades.
251 261 */
252 262 void QChartAxis::setShadesPen(const QPen& pen)
253 263 {
254 264 m_shadesPen=pen;
255 265 emit update(this);
256 266 }
257 267
258 268 /*!
259 269 Sets \a brush used to draw shades.
260 270 */
261 271 void QChartAxis::setShadesBrush(const QBrush& brush)
262 272 {
263 273 m_shadesBrush=brush;
264 274 emit update(this);
265 275 }
266 276
267 277 /*!
268 278 Sets \a opacity of the shades.
269 279 */
270 280 void QChartAxis::setShadesOpacity(qreal opacity)
271 281 {
272 282 m_shadesOpacity=opacity;
273 283 emit update(this);
274 284 }
275 285
276 286 /*!
277 Sets \a min value on the axis.
287 Sets \a min value on the axis.
278 288 */
279 289 void QChartAxis::setMin(qreal min)
280 290 {
281 if(m_min!=min){
282 m_min=min;
283 emit minChanged(m_min);
284 }
291 if(m_min!=min) {
292 m_min=min;
293 emit rangeChanged(this);
294 }
285 295 }
286 296
287 297 /*!
288 Sets \a max value on the axis.
298 Sets \a max value on the axis.
289 299 */
290 300 void QChartAxis::setMax(qreal max)
291 301 {
292 if(m_max!=max){
293 m_max=max;
294 emit maxChanged(m_max);
295 }
302 if(m_max!=max) {
303 m_max=max;
304 emit rangeChanged(this);
305 }
296 306 }
297 307
298 308 /*!
299 309 Sets range from \a min to \a max on the axis.
300 310 */
301 311 void QChartAxis::setRange(qreal min, qreal max)
302 312 {
303 setMin(min);
304 setMax(max);
313 setMin(min);
314 setMax(max);
315 }
316
317 void QChartAxis::updateRange(qreal min, qreal max)
318 {
319 if(m_max!=max){
320 emit maxChanged(max);
321 }
322
323 if(m_min!=min){
324 emit minChanged(min);
325 }
326
327 m_max=max;
328 m_min=min;
305 329 }
306 330
307 331 /*!
308 332 Sets \a count for ticks on the axis.
309 333 */
310 334 void QChartAxis::setTicksCount(int count)
311 335 {
312 336 m_ticksCount=count;
313 337 emit ticksChanged(this);
314 338 }
315 339
316 340 /*!
317 341 TODO: refactor me. Sets string \a label for \a value on the axis.
318 342 */
319 343 void QChartAxis::addAxisTickLabel(qreal value,const QString& label)
320 344 {
321 345 m_ticks.insert(value,label);
322 346 emit ticksChanged(this);
323 347 }
324 348
325 349 /*!
326 350 TODO: refactor me. Removes label for \a value on the axis.
327 351 */
328 352 void QChartAxis::removeAxisTickLabel(qreal value)
329 353 {
330 354 m_ticks.remove(value);
331 355 emit ticksChanged(this);
332 356 }
333 357
334 358 /*!
335 359 TODO: refactor me. Returns label for \a value on the axis.
336 360 */
337 361 QString QChartAxis::axisTickLabel(qreal value) const
338 362 {
339 363 return m_ticks.value(value);
340 364 }
341 365
342 366 /*!
343 367 TODO: refactor me. Removes all the string labels for on the axis.
344 368 */
345 369 void QChartAxis::clearAxisTickLabels()
346 370 {
347 371 m_ticks.clear();
348 372 emit ticksChanged(this);
349 373 }
350 374
351 375 #include "moc_qchartaxis.cpp"
352 376
353 377 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,105
1 1 #ifndef QCHARTAXIS_H_
2 2 #define QCHARTAXIS_H_
3 3
4 4 #include <qchartglobal.h>
5 5 #include <QPen>
6 6 #include <QFont>
7 7
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class QTCOMMERCIALCHART_EXPORT QChartAxis : public QObject
12 12 {
13 13 Q_OBJECT
14 14 public:
15 15 QChartAxis(QObject* parent =0);
16 16 ~QChartAxis();
17 17
18 18 //axis handling
19 19 bool isAxisVisible() const { return m_axisVisible;};
20 20 void setAxisVisible(bool visible);
21 21 void setAxisPen(const QPen& pen);
22 22 QPen axisPen() const { return m_axisPen;};
23 23
24 24 //grid handling
25 25 bool isGridVisible() const { return m_gridVisible;};
26 26 void setGridVisible(bool visible);
27 27 void setGridPen(const QPen& pen);
28 28 QPen gridPen() const {return m_gridPen;}
29 29
30 30 //labels handling
31 31 bool labelsVisible() const { return m_labelsVisible;};
32 32 void setLabelsVisible(bool visible);
33 33 void setLabelsPen(const QPen& pen);
34 34 QPen labelsPen() const { return m_labelsPen;}
35 35 void setLabelsBrush(const QBrush& brush);
36 36 QBrush labelsBrush() const { return m_labelsBrush;}
37 37 void setLabelsFont(const QFont& font);
38 38 QFont labelsFont() const { return m_labelsFont;}
39 39 void setLabelsAngle(int angle);
40 40 int labelsAngle() const { return m_labelsAngle;};
41 41
42 42 //shades handling
43 43 bool shadesVisible() const { return m_shadesVisible;};
44 44 void setShadesVisible(bool visible);
45 45 void setShadesPen(const QPen& pen);
46 46 QPen shadesPen() const { return m_shadesPen;}
47 47 void setShadesBrush(const QBrush& brush);
48 48 QBrush shadesBrush() const { return m_shadesBrush;}
49 49 void setShadesOpacity(qreal opacity);
50 50 qreal shadesOpacity() const { return m_shadesOpacity;}
51 51
52 52 //range handling
53 53 void setMin(qreal min);
54 54 qreal min() const { return m_min;};
55 55 void setMax(qreal max);
56 56 qreal max() const { return m_max;};
57 57 void setRange(qreal min, qreal max);
58 58
59 59 //ticks handling
60 60 void setTicksCount(int count);
61 61 int ticksCount() const { return m_ticksCount;}
62 62 void addAxisTickLabel(qreal value,const QString& label);
63 63 void removeAxisTickLabel(qreal value);
64 64 QString axisTickLabel(qreal value) const ;
65 65 void clearAxisTickLabels();
66 66
67 //internal
68 void updateRange(qreal min, qreal max);
69
67 70 signals:
68 71 void minChanged(qreal min);
69 72 void maxChanged(qreal max);
70 73 //private signal
71 74 void update(QChartAxis*);
75 void rangeChanged(QChartAxis*);
72 76 void ticksChanged(QChartAxis*);
73 77
74 78 private:
75 79 bool m_axisVisible;
76 80 QPen m_axisPen;
77 81 QBrush m_axisBrush;
78 82
79 83 bool m_gridVisible;
80 84 QPen m_gridPen;
81 85
82 86 bool m_labelsVisible;
83 87 QPen m_labelsPen;
84 88 QBrush m_labelsBrush;
85 89 QFont m_labelsFont;
86 90 int m_labelsAngle;
87 91
88 92 bool m_shadesVisible;
89 93 QPen m_shadesPen;
90 94 QBrush m_shadesBrush;
91 95 qreal m_shadesOpacity;
92 96
93 97 qreal m_min;
94 98 qreal m_max;
95 99
96 100 int m_ticksCount;
97 101 QMap<qreal, QString> m_ticks;
98 102 };
99 103
100 104 QTCOMMERCIALCHART_END_NAMESPACE
101 105 #endif /* QCHARTAXIS_H_ */
General Comments 0
You need to be logged in to leave comments. Login now