summaryrefslogtreecommitdiffstats
path: root/src/qchart.cpp
blob: 8b31af0caaee4cfda498789ddbf9671190f91bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Commercial Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qchart.h"
#include "qchart_p.h"
#include "legendscroller_p.h"
#include "qlegend_p.h"
#include "chartbackground_p.h"
#include "qabstractaxis.h"
#include "chartlayout_p.h"
#include <QGraphicsScene>
#include <QGraphicsSceneResizeEvent>

QTCOMMERCIALCHART_BEGIN_NAMESPACE

/*!
 \enum QChart::ChartTheme

 This enum describes the theme used by the chart.

 \value ChartThemeLight The default theme
 \value ChartThemeBlueCerulean
 \value ChartThemeDark
 \value ChartThemeBrownSand
 \value ChartThemeBlueNcs
 \value ChartThemeHighContrast
 \value ChartThemeBlueIcy
 */

/*!
 \enum QChart::AnimationOption

 For enabling/disabling animations. Defaults to NoAnimation.

 \value NoAnimation
 \value GridAxisAnimations
 \value SeriesAnimations
 \value AllAnimations
 */

/*!
 \class QChart
 \brief QtCommercial chart API.

 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
 representation of different types of series and other chart related objects like
 QAxis and QLegend. If you simply want to show a chart in a layout, you can use the
 convenience class QChartView instead of QChart.
 \sa QChartView
 */

/*!
 \property QChart::animationOptions
 The animation \a options for the chart. Animations are enabled/disabled based on this setting.
 */

/*!
 \property QChart::backgroundVisible
 Whether the chart background is visible or not.
 \sa setBackgroundBrush(), setBackgroundPen()
 */

/*!
 \property QChart::dropShadowEnabled
 If set to true, the background drop shadow effect is enabled. If set to false, it is disabled. Note that the drop
 shadow effect depends on theme, which means the setting may be changed if you switch to another theme.
 */

/*!
 \property QChart::minimumMargins
 Minimum margins between the plot area (axes) and the edge of the chart widget.
 */

/*!
 \property QChart::theme
 Theme is a built-in collection of UI style related settings applied for all visual elements of a chart, like colors,
 pens, brushes and fonts of series, axes, title and legend. \l {Chart themes demo} shows an example with a few
 different themes.
 Note: changing the theme will overwrite all customizations previously applied to the series.
 */

/*!
 \property QChart::title
 Title is the name (label) of a chart. It is shown as a headline on top of the chart.
 */

/*!
 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
 */
QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
    : QGraphicsWidget(parent, wFlags),
      d_ptr(new QChartPrivate())
{
    d_ptr->m_dataset = new ChartDataSet(this);
    d_ptr->m_presenter = new ChartPresenter(this, d_ptr->m_dataset);
    d_ptr->createConnections();
    d_ptr->m_legend = new LegendScroller(this);
    d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
    //connect(d_ptr->m_presenter, SIGNAL(marginsChanged(QRectF)), this, SIGNAL(marginsChanged(QRectF)));
    setLayout(d_ptr->m_presenter->layout());
}

/*!
 Destroys the object and it's children, like series and axis objects added to it.
 */
QChart::~QChart()
{
    //delete first presenter , since this is a root of all the graphical items
    setLayout(0);
    delete d_ptr->m_presenter;
    d_ptr->m_presenter = 0;
}

/*!
 Adds the \a series onto the chart and takes the ownership of the object.
 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
 the y axis).

 \sa removeSeries(), removeAllSeries()
 */
void QChart::addSeries(QAbstractSeries *series)
{
    Q_ASSERT(series);
    d_ptr->m_dataset->addSeries(series);
}

/*!
 Removes the \a series specified in a perameter from the QChartView.
 It releses its ownership of the specified QChartSeries object.
 It does not delete the pointed QChartSeries data object
 \sa addSeries(), removeAllSeries()
 */
void QChart::removeSeries(QAbstractSeries *series)
{
    Q_ASSERT(series);
    d_ptr->m_dataset->removeSeries(series);
}

/*!
 Removes all the QChartSeries that have been added to the QChartView
 It also deletes the pointed QChartSeries data objects
 \sa addSeries(), removeSeries()
 */
void QChart::removeAllSeries()
{
    d_ptr->m_dataset->removeAllSeries();
}

/*!
 Sets the \a brush that is used for painting the background of the chart area.
 */
void QChart::setBackgroundBrush(const QBrush &brush)
{
    d_ptr->m_presenter->setBackgroundBrush(brush);
}

/*!
    Gets the brush that is used for painting the background of the chart area.
 */
QBrush QChart::backgroundBrush() const
{
    return d_ptr->m_presenter->backgroundBrush();
}

/*!
 Sets the \a pen that is used for painting the background of the chart area.
 */
void QChart::setBackgroundPen(const QPen &pen)
{
    d_ptr->m_presenter->setBackgroundPen(pen);
}

/*!
    Gets the pen that is used for painting the background of the chart area.
 */
QPen QChart::backgroundPen() const
{
    return d_ptr->m_presenter->backgroundPen();
}

/*!
 Sets the chart \a title. The description text that is drawn above the chart.
 */
void QChart::setTitle(const QString &title)
{
    d_ptr->m_presenter->setTitle(title);
}

/*!
 Returns the chart title. The description text that is drawn above the chart.
 */
QString QChart::title() const
{
    return d_ptr->m_presenter->title();
}

/*!
 Sets the \a font that is used for drawing the chart description text that is rendered above the chart.
 */
void QChart::setTitleFont(const QFont &font)
{
    d_ptr->m_presenter->setTitleFont(font);
}

/*!
    Gets the font that is used for drawing the chart description text that is rendered above the chart.
 */
QFont QChart::titleFont() const
{
    return d_ptr->m_presenter->titleFont();
}

/*!
 Sets the \a brush used for rendering the title text.
 */
void QChart::setTitleBrush(const QBrush &brush)
{
    d_ptr->m_presenter->setTitleBrush(brush);
}

/*!
 Returns the brush used for rendering the title text.
 */
QBrush QChart::titleBrush() const
{
    return d_ptr->m_presenter->titleBrush();
}

void QChart::setTheme(QChart::ChartTheme theme)
{
    d_ptr->m_presenter->setTheme(theme);
}

QChart::ChartTheme QChart::theme() const
{
    return d_ptr->m_presenter->theme();
}

/*!
 Zooms in the view by a factor of 2
 */
void QChart::zoomIn()
{
    d_ptr->m_presenter->zoomIn(2.0);
}

/*!
 Zooms in the view to a maximum level at which \a rect is still fully visible.
 */
void QChart::zoomIn(const QRectF &rect)
{
    if (!rect.isValid())
        return;
    d_ptr->m_presenter->zoomIn(rect);
}

/*!
 Restores the view zoom level to the previous one.
 */
void QChart::zoomOut()
{
    d_ptr->m_presenter->zoomOut(2.0);
}

/*!
 Zooms in the view by a \a factor.

 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
 */
void QChart::zoom(qreal factor)
{
    if (qFuzzyCompare(factor, 0))
        return;

    if (qFuzzyCompare(factor, (qreal)1.0))
        return;

    if (factor < 0)
        return;

    if (factor > 1.0)
        d_ptr->m_presenter->zoomIn(factor);
    else
        d_ptr->m_presenter->zoomOut(1.0 / factor);
}

/*!
 Returns the pointer to the x axis object of the chart asociated with the specified \a series
 If no series is provided then pointer to currently visible axis is provided
 */
QAbstractAxis *QChart::axisX(QAbstractSeries *series) const
{
    return d_ptr->m_dataset->axisX(series);
}

/*!
 Returns the pointer to the y axis object of the chart asociated with the specified \a series
 If no series is provided then pointer to currently visible axis is provided
 */
QAbstractAxis *QChart::axisY(QAbstractSeries *series) const
{
    return d_ptr->m_dataset->axisY(series);
}

/*!
 NOTICE: This function has to be called after series has been added to the chart if no customized axes are set to the chart. Otherwise axisX(), axisY() calls return NULL.

 Creates the axes for the chart based on the series that has already been added to the chart.

\table
     \header
         \o Series type
         \o X-axis
         \o Y-axis
     \row
         \o QXYSeries
         \o QValueAxis
         \o QValueAxis
     \row
         \o QBarSeries
         \o QBarCategoryAxis
         \o QValueAxis
     \row
         \o QPieSeries
         \o None
         \o None
     \endtable

 If there are several QXYSeries derived series added to the chart and no other series type has been added then only one pair of axes is created.
 If there are sevaral series added of different types then each series gets its own axes pair.

 NOTICE: if there is more than one x and y axes created then no axis is drawn by default and one needs to choose explicitly which axis should be shown.

 Axis specifix to the series can be later obtained from the chart by providing the series as the parameter of axisX(), axisY() function calls.
 QPieSeries does not create any axes.

 \sa axisX(), axisY(), setAxisX(), setAxisY()
 */
void QChart::createDefaultAxes()
{
    d_ptr->m_dataset->createDefaultAxes();
}

/*!
 Returns the legend object of the chart. Ownership stays in chart.
 */
QLegend *QChart::legend() const
{
    return d_ptr->m_legend;
}

/*!
  Sets the minimum \a margins between the plot area (axes) and the edge of the chart widget.
  Deprecated. Use setMargins().
*/
void QChart::setMinimumMargins(const QMargins &margins)
{
    qWarning() << "QChart::setMinimumMargins is deprecated. Use QChart::setMargins instead.";
    d_ptr->m_presenter->layout()->setMargins(margins);
}

/*!
    Returns the rect that contains information about margins (distance between chart widget edge and axes).
    Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
    Deprecated. Use margins().
 */
QMargins QChart::minimumMargins() const
{
    qWarning() << "QChart::minimumMargins is deprecated. Use QChart::margins instead.";
    return d_ptr->m_presenter->layout()->margins();
}

/*!
  Sets the minimum \a margins between the plot area (axes) and the edge of the chart widget.
*/
void QChart::setMargins(const QMargins &margins)
{
    d_ptr->m_presenter->layout()->setMargins(margins);
}

/*!
    Returns the rect that contains information about margins (distance between chart widget edge and axes).
    Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
 */
QMargins QChart::margins() const
{
    return d_ptr->m_presenter->layout()->margins();
}

/*!
    Returns the the rect within which the drawing of the chart is done.
    It does not include the area defines by margins.
 */
QRectF QChart::plotArea() const
{
    return d_ptr->m_presenter->layout()->chartsGeometry();
}

///*!
//    TODO: Dummy.
//    Adjest the ranges of the axes so that all the data of the specified \a series is visible
// */
//void QChart::adjustViewToSeries(QAbstractSeries* series)
//{
//    //
//}

/*!
 Sets animation \a options for the chart
 */
void QChart::setAnimationOptions(AnimationOptions options)
{
    d_ptr->m_presenter->setAnimationOptions(options);
}

QChart::AnimationOptions QChart::animationOptions() const
{
    return d_ptr->m_presenter->animationOptions();
}

/*!
    Scrolls the visible area of the chart by the distance defined in the \a dx and \a dy.
 */
void QChart::scroll(qreal dx, qreal dy)
{
    d_ptr->m_presenter->scroll(dx, dy);
}

void QChart::setBackgroundVisible(bool visible)
{
    d_ptr->m_presenter->setBackgroundVisible(visible);
}

bool QChart::isBackgroundVisible() const
{
    return d_ptr->m_presenter->isBackgroundVisible();
}

void QChart::setDropShadowEnabled(bool enabled)
{
    d_ptr->m_presenter->setBackgroundDropShadowEnabled(enabled);
}

bool QChart::isDropShadowEnabled() const
{
    return d_ptr->m_presenter->isBackgroundDropShadowEnabled();
}

/*!
  Returns all the series that are added to the chart.

  \sa addSeries(), removeSeries(), removeAllSeries()
*/
QList<QAbstractSeries *> QChart::series() const
{
    return d_ptr->m_dataset->series();
}

/*!
  Sets \a axis to the chart, which will control the presentation of the \a series

   \sa axisX(), axisY(), setAxisY(), createDefaultAxes()
*/
void QChart::setAxisX(QAbstractAxis *axis , QAbstractSeries *series)
{
    if (axis->alignment() == Qt::AlignLeft || axis->alignment() == Qt::AlignRight)
        return;
    d_ptr->m_dataset->setAxis(series, axis, Qt::Horizontal);
}

/*!
  Sets \a axis to the chart, which will control the presentation of the \a series

   \sa axisX(), axisY(), setAxisX(), createDefaultAxes()
*/
void QChart::setAxisY(QAbstractAxis *axis , QAbstractSeries *series)
{
    if (axis->alignment() == Qt::AlignTop || axis->alignment() == Qt::AlignBottom)
        return;
    d_ptr->m_dataset->setAxis(series, axis, Qt::Vertical);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QChartPrivate::QChartPrivate():
    m_legend(0),
    m_dataset(0),
    m_presenter(0)
{

}

QChartPrivate::~QChartPrivate()
{

}

void QChartPrivate::createConnections()
{
    QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
    QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_presenter, SLOT(handleSeriesRemoved(QAbstractSeries*)));
    QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*)), m_presenter, SLOT(handleAxisAdded(QAbstractAxis*,Domain*)));
    QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_presenter, SLOT(handleAxisRemoved(QAbstractAxis*)));
    //QObject::connect(m_presenter, SIGNAL(marginsChanged(QRectF)), q_ptr, SIGNAL(marginsChanged(QRectF)));
}

#include "moc_qchart.cpp"

QTCOMMERCIALCHART_END_NAMESPACE