##// END OF EJS Templates
Minor modifications to properties of abstract, area and bar series
Tero Ahola -
r1462:62d898329cbd
parent child
Show More
@@ -1,95 +1,95
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartview.h"
21 #include "chartview.h"
22 #include <QLineSeries>
22 #include <QLineSeries>
23 #include <QScatterSeries>
23 #include <QScatterSeries>
24 #include <QSplineSeries>
24 #include <QSplineSeries>
25 #include <QAreaSeries>
25 #include <QAreaSeries>
26 #include <QTime>
26 #include <QTime>
27
27
28 ChartView::ChartView(QChart* chart,QWidget* parent):QChartView(chart,parent),
28 ChartView::ChartView(QChart* chart,QWidget* parent):QChartView(chart,parent),
29 m_index(-1),m_chart(chart)
29 m_index(-1),m_chart(chart)
30 {
30 {
31 m_chart->setTitle("Charts presenter");
31 m_chart->setTitle("Charts presenter");
32 m_chart->setBackgroundDropShadowEnabled(false);
32 m_chart->setDropShadowEnabled(false);
33 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
33 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
34 m_timer.setInterval(3000);
34 m_timer.setInterval(3000);
35
35
36 //![1]
36 //![1]
37 QLineSeries* series0 = new QLineSeries();
37 QLineSeries* series0 = new QLineSeries();
38 series0->setName("line");
38 series0->setName("line");
39
39
40 QScatterSeries* series1 = new QScatterSeries();
40 QScatterSeries* series1 = new QScatterSeries();
41 series1->setName("scatter");
41 series1->setName("scatter");
42
42
43 QSplineSeries* series2 = new QSplineSeries();
43 QSplineSeries* series2 = new QSplineSeries();
44 series2->setName("spline");
44 series2->setName("spline");
45
45
46 QAreaSeries* series3 = new QAreaSeries(series0);
46 QAreaSeries* series3 = new QAreaSeries(series0);
47 series3->setName("area");
47 series3->setName("area");
48 //![1]
48 //![1]
49
49
50 //![2]
50 //![2]
51 int numPoints = 10;
51 int numPoints = 10;
52
52
53 for (int x = 0; x <= numPoints; ++x) {
53 for (int x = 0; x <= numPoints; ++x) {
54 qreal y = qrand() % 100;
54 qreal y = qrand() % 100;
55 series0->append(x,y);
55 series0->append(x,y);
56 series1->append(x,y);
56 series1->append(x,y);
57 series2->append(x,y);
57 series2->append(x,y);
58 }
58 }
59 //![2]
59 //![2]
60
60
61 //![3]
61 //![3]
62 m_series<<series0;
62 m_series<<series0;
63 m_titles<< m_chart->title()+": LineChart";
63 m_titles<< m_chart->title()+": LineChart";
64 m_series<<series1;
64 m_series<<series1;
65 m_titles<< m_chart->title()+": ScatterChart";
65 m_titles<< m_chart->title()+": ScatterChart";
66 m_series<<series2;
66 m_series<<series2;
67 m_titles<< m_chart->title()+": SplineChart";
67 m_titles<< m_chart->title()+": SplineChart";
68 m_series<<series3;
68 m_series<<series3;
69 m_titles<< m_chart->title()+": AreaChart";
69 m_titles<< m_chart->title()+": AreaChart";
70 //![3]
70 //![3]
71
71
72 m_timer.start();
72 m_timer.start();
73 handleTimeout();
73 handleTimeout();
74 }
74 }
75
75
76 ChartView::~ChartView()
76 ChartView::~ChartView()
77 {
77 {
78 if(m_series.size()==0) return;
78 if(m_series.size()==0) return;
79 m_chart->removeSeries(m_series.at(m_index));
79 m_chart->removeSeries(m_series.at(m_index));
80 m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone
80 m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone
81 qDeleteAll(m_series);
81 qDeleteAll(m_series);
82 }
82 }
83
83
84 //![4]
84 //![4]
85 void ChartView::handleTimeout()
85 void ChartView::handleTimeout()
86 {
86 {
87 if(m_series.size()==0) return;
87 if(m_series.size()==0) return;
88 if(m_index>=0)
88 if(m_index>=0)
89 m_chart->removeSeries(m_series.at(m_index));
89 m_chart->removeSeries(m_series.at(m_index));
90 m_index++;
90 m_index++;
91 m_index=m_index%m_series.size();
91 m_index=m_index%m_series.size();
92 m_chart->addSeries(m_series.at(m_index));
92 m_chart->addSeries(m_series.at(m_index));
93 m_chart->setTitle(m_titles.at(m_index));
93 m_chart->setTitle(m_titles.at(m_index));
94 }
94 }
95 //![4]
95 //![4]
@@ -1,56 +1,56
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartview.h"
21 #include "chartview.h"
22 #include <QScatterSeries>
22 #include <QScatterSeries>
23
23
24 ChartView::ChartView(QWidget *parent) :
24 ChartView::ChartView(QWidget *parent) :
25 QChartView(new QChart(), parent)
25 QChartView(new QChart(), parent)
26 {
26 {
27 //![1]
27 //![1]
28 QScatterSeries *series0 = new QScatterSeries();
28 QScatterSeries *series0 = new QScatterSeries();
29 series0->setName("scatter1");
29 series0->setName("scatter1");
30 series0->setMarkerShape(QScatterSeries::MarkerShapeCircle);
30 series0->setMarkerShape(QScatterSeries::MarkerShapeCircle);
31 series0->setMarkerSize(15.0);
31 series0->setMarkerSize(15.0);
32
32
33 QScatterSeries *series1 = new QScatterSeries();
33 QScatterSeries *series1 = new QScatterSeries();
34 series1->setName("scatter2");
34 series1->setName("scatter2");
35 series1->setMarkerShape(QScatterSeries::MarkerShapeCircle);
35 series1->setMarkerShape(QScatterSeries::MarkerShapeCircle);
36 series1->setMarkerSize(20.0);
36 series1->setMarkerSize(20.0);
37 //![1]
37 //![1]
38
38
39 //![2]
39 //![2]
40 series0->append(0, 6);
40 series0->append(0, 6);
41 series0->append(2, 4);
41 series0->append(2, 4);
42 series0->append(3, 8);
42 series0->append(3, 8);
43 series0->append(7, 4);
43 series0->append(7, 4);
44 series0->append(10, 5);
44 series0->append(10, 5);
45
45
46 *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2);
46 *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2);
47 //![2]
47 //![2]
48
48
49 //![3]
49 //![3]
50 setRenderHint(QPainter::Antialiasing);
50 setRenderHint(QPainter::Antialiasing);
51 chart()->addSeries(series0);
51 chart()->addSeries(series0);
52 chart()->addSeries(series1);
52 chart()->addSeries(series1);
53 chart()->setTitle("Simple scatterchart example");
53 chart()->setTitle("Simple scatterchart example");
54 chart()->setBackgroundDropShadowEnabled(false);
54 chart()->setDropShadowEnabled(false);
55 //![3]
55 //![3]
56 }
56 }
@@ -1,346 +1,355
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "declarativechart.h"
21 #include "declarativechart.h"
22 #include <QPainter>
22 #include <QPainter>
23 #include "declarativelineseries.h"
23 #include "declarativelineseries.h"
24 #include "declarativeareaseries.h"
24 #include "declarativeareaseries.h"
25 #include "declarativebarseries.h"
25 #include "declarativebarseries.h"
26 #include "declarativepieseries.h"
26 #include "declarativepieseries.h"
27 #include "declarativesplineseries.h"
27 #include "declarativesplineseries.h"
28 #include "declarativescatterseries.h"
28 #include "declarativescatterseries.h"
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 /*!
32 /*!
33 \qmlclass ChartView DeclarativeChart
33 \qmlclass ChartView DeclarativeChart
34
34
35 ChartView element is the parent that is responsible for showing different chart series types.
35 ChartView element is the parent that is responsible for showing different chart series types.
36
36
37 \section1 Example Usage
37 \section1 Example Usage
38
38
39 \beginfloatleft
39 \beginfloatleft
40 \image demos_qmlchart1.png
40 \image demos_qmlchart1.png
41 \endfloat
41 \endfloat
42 \clearfloat
42 \clearfloat
43 */
43 */
44
44
45 /*!
46 \qmlproperty Theme ChartView::theme
47 Theme defines the visual appearance of the chart, including for example colors, fonts, line
48 widths and chart background.
49 */
50
51 /*!
52 \qmlproperty Animation ChartView::animation
53 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
54 ChartView.SeriesAnimations or ChartView.AllAnimations.
55 */
56
57 /*!
58 \qmlproperty string ChartView::title
59 The title of the chart, shown on top of the chart.
60 */
61
62 /*!
63 \qmlproperty string ChartView::titleColor
64 The color of the title text.
65 */
66
67 /*!
68 \qmlproperty Axis ChartView::axisX
69 The x-axis of the chart.
70 */
71
72 /*!
73 \qmlproperty Axis ChartView::axisY
74 The default y-axis of the chart.
75 */
76
77 /*!
78 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
79 The y-axis of the series. This is the same as the default y-axis of the chart, unless you have
80 explicitly defined the series to have a non-default y-axis.
81 */
82
45 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
83 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
46 : QDeclarativeItem(parent),
84 : QDeclarativeItem(parent),
47 m_chart(new QChart(this))
85 m_chart(new QChart(this))
48 {
86 {
49 setFlag(QGraphicsItem::ItemHasNoContents, false);
87 setFlag(QGraphicsItem::ItemHasNoContents, false);
50 // m_chart->axisX()->setNiceNumbersEnabled(false);
88 // m_chart->axisX()->setNiceNumbersEnabled(false);
51 }
89 }
52
90
53 DeclarativeChart::~DeclarativeChart()
91 DeclarativeChart::~DeclarativeChart()
54 {
92 {
55 delete m_chart;
93 delete m_chart;
56 }
94 }
57
95
58 void DeclarativeChart::childEvent(QChildEvent *event)
96 void DeclarativeChart::childEvent(QChildEvent *event)
59 {
97 {
60 if (event->type() == QEvent::ChildAdded) {
98 if (event->type() == QEvent::ChildAdded) {
61 if (qobject_cast<QAbstractSeries *>(event->child())) {
99 if (qobject_cast<QAbstractSeries *>(event->child())) {
62 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
100 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
63 }
101 }
64 }
102 }
65 }
103 }
66
104
67 void DeclarativeChart::componentComplete()
105 void DeclarativeChart::componentComplete()
68 {
106 {
69 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
70 foreach(QObject *child, children()) {
107 foreach(QObject *child, children()) {
71 if (qobject_cast<QAbstractSeries *>(child)) {
108 if (qobject_cast<QAbstractSeries *>(child)) {
72 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
109 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
110 // TODO: how about optional y-axis?
73 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
111 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
74 }
112 }
75 }
113 }
76 // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max();
77
78 QDeclarativeItem::componentComplete();
114 QDeclarativeItem::componentComplete();
79 }
115 }
80
116
81 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
117 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
82 {
118 {
83 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
119 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
84 if (newGeometry.isValid()) {
120 if (newGeometry.isValid()) {
85 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
121 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
86 m_chart->resize(newGeometry.width(), newGeometry.height());
122 m_chart->resize(newGeometry.width(), newGeometry.height());
87 }
123 }
88 }
124 }
89 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
125 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
90 }
126 }
91
127
92 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
128 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
93 {
129 {
94 Q_UNUSED(option)
130 Q_UNUSED(option)
95 Q_UNUSED(widget)
131 Q_UNUSED(widget)
96
132
97 // TODO: optimized?
133 // TODO: optimized?
98 painter->setRenderHint(QPainter::Antialiasing, true);
134 painter->setRenderHint(QPainter::Antialiasing, true);
99 }
135 }
100
136
101 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
137 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
102 {
138 {
103 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
139 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
104 if (chartTheme != m_chart->theme()) {
140 if (chartTheme != m_chart->theme()) {
105 m_chart->setTheme(chartTheme);
141 m_chart->setTheme(chartTheme);
106 themeChanged();
142 themeChanged();
107 }
143 }
108 }
144 }
109
145
110 DeclarativeChart::Theme DeclarativeChart::theme()
146 DeclarativeChart::Theme DeclarativeChart::theme()
111 {
147 {
112 return (DeclarativeChart::Theme) m_chart->theme();
148 return (DeclarativeChart::Theme) m_chart->theme();
113 }
149 }
114
150
115 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
151 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
116 {
152 {
117 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
153 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
118 if (animationOptions != m_chart->animationOptions()) {
154 if (animationOptions != m_chart->animationOptions()) {
119 m_chart->setAnimationOptions(animationOptions);
155 m_chart->setAnimationOptions(animationOptions);
120 animationOptionsChanged();
156 animationOptionsChanged();
121 }
157 }
122 }
158 }
123
159
124 DeclarativeChart::Animation DeclarativeChart::animationOptions()
160 DeclarativeChart::Animation DeclarativeChart::animationOptions()
125 {
161 {
126 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
162 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
127 return DeclarativeChart::AllAnimations;
163 return DeclarativeChart::AllAnimations;
128 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
164 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
129 return DeclarativeChart::GridAxisAnimations;
165 return DeclarativeChart::GridAxisAnimations;
130 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
166 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
131 return DeclarativeChart::SeriesAnimations;
167 return DeclarativeChart::SeriesAnimations;
132 else
168 else
133 return DeclarativeChart::NoAnimation;
169 return DeclarativeChart::NoAnimation;
134 }
170 }
135
171
136 void DeclarativeChart::setTitle(QString title)
172 void DeclarativeChart::setTitle(QString title)
137 {
173 {
138 if (title != m_chart->title()) {
174 if (title != m_chart->title()) {
139 m_chart->setTitle(title);
175 m_chart->setTitle(title);
140 emit titleChanged();
176 emit titleChanged();
141 }
177 }
142 }
178 }
143 QString DeclarativeChart::title()
179 QString DeclarativeChart::title()
144 {
180 {
145 return m_chart->title();
181 return m_chart->title();
146 }
182 }
147
183
148 QAxis *DeclarativeChart::axisX()
184 QAxis *DeclarativeChart::axisX()
149 {
185 {
150 return m_chart->axisX();
186 return m_chart->axisX();
151 }
187 }
152
188
153 QAxis *DeclarativeChart::axisY()
189 QAxis *DeclarativeChart::axisY(QAbstractSeries *series)
154 {
190 {
155 return m_chart->axisY();
191 return m_chart->axisY(series);
156 }
192 }
157
193
158 QLegend *DeclarativeChart::legend()
194 QLegend *DeclarativeChart::legend()
159 {
195 {
160 return m_chart->legend();
196 return m_chart->legend();
161 }
197 }
162
198
163 QVariantList DeclarativeChart::axisXLabels()
199 QVariantList DeclarativeChart::axisXLabels()
164 {
200 {
165 QVariantList labels;
201 QVariantList labels;
166 foreach (qreal value, m_chart->axisX()->categories()->values()) {
202 foreach (qreal value, m_chart->axisX()->categories()->values()) {
167 labels.append(value);
203 labels.append(value);
168 labels.append(m_chart->axisX()->categories()->label(value));
204 labels.append(m_chart->axisX()->categories()->label(value));
169 }
205 }
170 return labels;
206 return labels;
171 }
207 }
172
208
173 void DeclarativeChart::setAxisXLabels(QVariantList list)
209 void DeclarativeChart::setAxisXLabels(QVariantList list)
174 {
210 {
175 QVariant value(QVariant::Invalid);
211 QVariant value(QVariant::Invalid);
176 foreach (QVariant element, list) {
212 foreach (QVariant element, list) {
177 if (value.isValid() && element.type() == QVariant::String) {
213 if (value.isValid() && element.type() == QVariant::String) {
178 m_chart->axisX()->categories()->insert(value.toDouble(), element.toString());
214 m_chart->axisX()->categories()->insert(value.toDouble(), element.toString());
179 value = QVariant(QVariant::Invalid);
215 value = QVariant(QVariant::Invalid);
180 } else {
216 } else {
181 if (element.canConvert(QVariant::Double))
217 if (element.canConvert(QVariant::Double))
182 value = element;
218 value = element;
183 }
219 }
184 }
220 }
185 emit axisLabelsChanged();
221 emit axisLabelsChanged();
186 }
222 }
187
223
188 void DeclarativeChart::setTitleColor(QColor color)
224 void DeclarativeChart::setTitleColor(QColor color)
189 {
225 {
190 QBrush b = m_chart->titleBrush();
226 QBrush b = m_chart->titleBrush();
191 if (color != b.color()) {
227 if (color != b.color()) {
192 b.setColor(color);
228 b.setColor(color);
193 m_chart->setTitleBrush(b);
229 m_chart->setTitleBrush(b);
194 emit titleColorChanged();
230 emit titleColorChanged();
195 }
231 }
196 }
232 }
197
233
198 QColor DeclarativeChart::titleColor()
234 QColor DeclarativeChart::titleColor()
199 {
235 {
200 return m_chart->titleBrush().color();
236 return m_chart->titleBrush().color();
201 }
237 }
202
238
203 void DeclarativeChart::setBackgroundColor(QColor color)
239 void DeclarativeChart::setBackgroundColor(QColor color)
204 {
240 {
205 QBrush b = m_chart->backgroundBrush();
241 QBrush b = m_chart->backgroundBrush();
206 if (color != b.color()) {
242 if (color != b.color()) {
207 b.setColor(color);
243 b.setColor(color);
208 m_chart->setBackgroundBrush(b);
244 m_chart->setBackgroundBrush(b);
209 emit backgroundColorChanged();
245 emit backgroundColorChanged();
210 }
246 }
211 }
247 }
212
248
213 QColor DeclarativeChart::backgroundColor()
249 QColor DeclarativeChart::backgroundColor()
214 {
250 {
215 return m_chart->backgroundBrush().color();
251 return m_chart->backgroundBrush().color();
216 }
252 }
217
253
218 int DeclarativeChart::count()
254 int DeclarativeChart::count()
219 {
255 {
220 return m_chart->series().count();
256 return m_chart->series().count();
221 }
257 }
222
258
223 void DeclarativeChart::setDropShadowEnabled(bool enabled)
259 void DeclarativeChart::setDropShadowEnabled(bool enabled)
224 {
260 {
225 if (enabled != m_chart->isBackgroundDropShadowEnabled()) {
261 if (enabled != m_chart->isDropShadowEnabled()) {
226 m_chart->setBackgroundDropShadowEnabled(enabled);
262 m_chart->setDropShadowEnabled(enabled);
227 dropShadowEnabledChanged(enabled);
263 dropShadowEnabledChanged(enabled);
228 }
264 }
229 }
265 }
230
266
231 bool DeclarativeChart::dropShadowEnabled()
267 bool DeclarativeChart::dropShadowEnabled()
232 {
268 {
233 return m_chart->isBackgroundDropShadowEnabled();
269 return m_chart->isDropShadowEnabled();
234 }
270 }
235
271
236 void DeclarativeChart::zoom(qreal factor)
272 void DeclarativeChart::zoom(qreal factor)
237 {
273 {
238 m_chart->zoom(factor);
274 m_chart->zoom(factor);
239 }
275 }
240
276
241 void DeclarativeChart::scrollLeft(qreal pixels)
277 void DeclarativeChart::scrollLeft(qreal pixels)
242 {
278 {
243 m_chart->scroll(QPointF(pixels, 0));
279 m_chart->scroll(QPointF(pixels, 0));
244 }
280 }
245
281
246 void DeclarativeChart::scrollRight(qreal pixels)
282 void DeclarativeChart::scrollRight(qreal pixels)
247 {
283 {
248 m_chart->scroll(QPointF(-pixels, 0));
284 m_chart->scroll(QPointF(-pixels, 0));
249 }
285 }
250
286
251 void DeclarativeChart::scrollUp(qreal pixels)
287 void DeclarativeChart::scrollUp(qreal pixels)
252 {
288 {
253 m_chart->scroll(QPointF(0, pixels));
289 m_chart->scroll(QPointF(0, pixels));
254 }
290 }
255
291
256 void DeclarativeChart::scrollDown(qreal pixels)
292 void DeclarativeChart::scrollDown(qreal pixels)
257 {
293 {
258 m_chart->scroll(QPointF(0, -pixels));
294 m_chart->scroll(QPointF(0, -pixels));
259 }
295 }
260
296
261 //void DeclarativeChart::scrollLeft(qreal ticks)
262 //{
263 // m_chart->scroll(QPointF(ticksToPixels(m_chart->axisX(), ticks), 0));
264 //}
265
266 //void DeclarativeChart::scrollRight(qreal ticks)
267 //{
268 // m_chart->scroll(QPointF(-ticksToPixels(m_chart->axisX(), ticks), 0));
269 //}
270
271 //void DeclarativeChart::scrollUp(qreal ticks)
272 //{
273 // m_chart->scroll(QPointF(0, ticksToPixels(m_chart->axisY(), ticks)));
274 //}
275
276 //void DeclarativeChart::scrollDown(qreal ticks)
277 //{
278 // m_chart->scroll(QPointF(0, -ticksToPixels(m_chart->axisY(), ticks)));
279 //}
280
281 //qreal DeclarativeChart::ticksToPixels(QAxis *axis, qreal ticks)
282 //{
283 // qreal tickCount = axis->max() - axis->min();
284 // qreal tickPixels = (m_chart->size().width() - m_chart->margins().width() * 2) / tickCount;
285 // return tickPixels * ticks;
286 //}
287
288 QAbstractSeries *DeclarativeChart::series(int index)
297 QAbstractSeries *DeclarativeChart::series(int index)
289 {
298 {
290 if (index < m_chart->series().count()) {
299 if (index < m_chart->series().count()) {
291 return m_chart->series().at(index);
300 return m_chart->series().at(index);
292 }
301 }
293 return 0;
302 return 0;
294 }
303 }
295
304
296 QAbstractSeries *DeclarativeChart::series(QString seriesName)
305 QAbstractSeries *DeclarativeChart::series(QString seriesName)
297 {
306 {
298 foreach(QAbstractSeries *series, m_chart->series()) {
307 foreach(QAbstractSeries *series, m_chart->series()) {
299 if (series->name() == seriesName)
308 if (series->name() == seriesName)
300 return series;
309 return series;
301 }
310 }
302 return 0;
311 return 0;
303 }
312 }
304
313
305 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
314 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
306 {
315 {
307 QAbstractSeries *series = 0;
316 QAbstractSeries *series = 0;
308 switch (type) {
317 switch (type) {
309 case DeclarativeChart::SeriesTypeLine:
318 case DeclarativeChart::SeriesTypeLine:
310 series = new DeclarativeLineSeries();
319 series = new DeclarativeLineSeries();
311 break;
320 break;
312 case DeclarativeChart::SeriesTypeArea:
321 case DeclarativeChart::SeriesTypeArea:
313 series = new DeclarativeAreaSeries();
322 series = new DeclarativeAreaSeries();
314 break;
323 break;
315 case DeclarativeChart::SeriesTypeBar:
324 case DeclarativeChart::SeriesTypeBar:
316 series = new DeclarativeBarSeries();
325 series = new DeclarativeBarSeries();
317 break;
326 break;
318 case DeclarativeChart::SeriesTypeStackedBar:
327 case DeclarativeChart::SeriesTypeStackedBar:
319 // TODO
328 // TODO
320 break;
329 break;
321 case DeclarativeChart::SeriesTypePercentBar:
330 case DeclarativeChart::SeriesTypePercentBar:
322 // TODO
331 // TODO
323 break;
332 break;
324 case DeclarativeChart::SeriesTypeGroupedBar:
333 case DeclarativeChart::SeriesTypeGroupedBar:
325 series = new DeclarativeGroupedBarSeries();
334 series = new DeclarativeGroupedBarSeries();
326 break;
335 break;
327 case DeclarativeChart::SeriesTypePie:
336 case DeclarativeChart::SeriesTypePie:
328 series = new DeclarativePieSeries();
337 series = new DeclarativePieSeries();
329 break;
338 break;
330 case DeclarativeChart::SeriesTypeScatter:
339 case DeclarativeChart::SeriesTypeScatter:
331 series = new DeclarativeScatterSeries();
340 series = new DeclarativeScatterSeries();
332 break;
341 break;
333 case DeclarativeChart::SeriesTypeSpline:
342 case DeclarativeChart::SeriesTypeSpline:
334 series = new DeclarativeSplineSeries();
343 series = new DeclarativeSplineSeries();
335 break;
344 break;
336 default:
345 default:
337 qWarning() << "Illegal series type";
346 qWarning() << "Illegal series type";
338 }
347 }
339 series->setName(name);
348 series->setName(name);
340 m_chart->addSeries(series);
349 m_chart->addSeries(series);
341 return series;
350 return series;
342 }
351 }
343
352
344 #include "moc_declarativechart.cpp"
353 #include "moc_declarativechart.cpp"
345
354
346 QTCOMMERCIALCHART_END_NAMESPACE
355 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,139 +1,139
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef DECLARATIVECHART_H
21 #ifndef DECLARATIVECHART_H
22 #define DECLARATIVECHART_H
22 #define DECLARATIVECHART_H
23
23
24 #include <QtCore/QtGlobal>
24 #include <QtCore/QtGlobal>
25 #include <QDeclarativeItem>
25 #include <QDeclarativeItem>
26 #include <qchart.h>
26 #include <qchart.h>
27 #include <QAxis>
27 #include <QAxis>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 // TODO: Derive from QChart for easier definition of properties?
31 // TODO: Derive from QChart for easier definition of properties?
32 class DeclarativeChart : public QDeclarativeItem
32 class DeclarativeChart : public QDeclarativeItem
33 // TODO: for QTQUICK2: extend QQuickPainterItem instead
33 // TODO: for QTQUICK2: extend QQuickPainterItem instead
34 //class DeclarativeChart : public QQuickPaintedItem, public Chart
34 //class DeclarativeChart : public QQuickPaintedItem, public Chart
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 Q_PROPERTY(Theme theme READ theme WRITE setTheme NOTIFY themeChanged)
37 Q_PROPERTY(Theme theme READ theme WRITE setTheme NOTIFY themeChanged)
38 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions NOTIFY animationOptionsChanged)
38 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions NOTIFY animationOptionsChanged)
39 Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
39 Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
40 Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged)
40 Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged)
41 Q_PROPERTY(QAxis *axisX READ axisX)
41 Q_PROPERTY(QAxis *axisX READ axisX)
42 Q_PROPERTY(QAxis *axisY READ axisY)
42 Q_PROPERTY(QAxis *axisY READ axisY)
43 Q_PROPERTY(QLegend *legend READ legend)
43 Q_PROPERTY(QLegend *legend READ legend)
44 // TODO: how to define axis labels? This is not very convenient
44 // TODO: how to define axis labels? This is not very convenient
45 Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels NOTIFY axisLabelsChanged)
45 Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels NOTIFY axisLabelsChanged)
46 Q_PROPERTY(int count READ count)
46 Q_PROPERTY(int count READ count)
47 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
47 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
48 Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged)
48 Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged)
49 Q_ENUMS(Animation)
49 Q_ENUMS(Animation)
50 Q_ENUMS(Theme)
50 Q_ENUMS(Theme)
51 Q_ENUMS(SeriesType)
51 Q_ENUMS(SeriesType)
52
52
53 public:
53 public:
54 // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api
54 // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api
55 enum Theme {
55 enum Theme {
56 ChartThemeLight = 0,
56 ChartThemeLight = 0,
57 ChartThemeBlueCerulean,
57 ChartThemeBlueCerulean,
58 ChartThemeDark,
58 ChartThemeDark,
59 ChartThemeBrownSand,
59 ChartThemeBrownSand,
60 ChartThemeBlueNcs,
60 ChartThemeBlueNcs,
61 ChartThemeHighContrast,
61 ChartThemeHighContrast,
62 ChartThemeBlueIcy
62 ChartThemeBlueIcy
63 };
63 };
64
64
65 enum Animation {
65 enum Animation {
66 NoAnimation = 0x0,
66 NoAnimation = 0x0,
67 GridAxisAnimations = 0x1,
67 GridAxisAnimations = 0x1,
68 SeriesAnimations =0x2,
68 SeriesAnimations =0x2,
69 AllAnimations = 0x3
69 AllAnimations = 0x3
70 };
70 };
71
71
72 enum SeriesType {
72 enum SeriesType {
73 SeriesTypeLine,
73 SeriesTypeLine,
74 SeriesTypeArea,
74 SeriesTypeArea,
75 SeriesTypeBar,
75 SeriesTypeBar,
76 SeriesTypeStackedBar,
76 SeriesTypeStackedBar,
77 SeriesTypePercentBar,
77 SeriesTypePercentBar,
78 SeriesTypeGroupedBar,
78 SeriesTypeGroupedBar,
79 SeriesTypePie,
79 SeriesTypePie,
80 SeriesTypeScatter,
80 SeriesTypeScatter,
81 SeriesTypeSpline
81 SeriesTypeSpline
82 };
82 };
83
83
84 public:
84 public:
85 DeclarativeChart(QDeclarativeItem *parent = 0);
85 DeclarativeChart(QDeclarativeItem *parent = 0);
86 ~DeclarativeChart();
86 ~DeclarativeChart();
87
87
88 public: // From QDeclarativeItem/QGraphicsItem
88 public: // From QDeclarativeItem/QGraphicsItem
89 void childEvent(QChildEvent *event);
89 void childEvent(QChildEvent *event);
90 void componentComplete();
90 void componentComplete();
91 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
91 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
92 void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
92 void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
93
93
94 public:
94 public:
95 void setTheme(DeclarativeChart::Theme theme);
95 void setTheme(DeclarativeChart::Theme theme);
96 DeclarativeChart::Theme theme();
96 DeclarativeChart::Theme theme();
97 void setAnimationOptions(DeclarativeChart::Animation animations);
97 void setAnimationOptions(DeclarativeChart::Animation animations);
98 DeclarativeChart::Animation animationOptions();
98 DeclarativeChart::Animation animationOptions();
99 void setTitle(QString title);
99 void setTitle(QString title);
100 QString title();
100 QString title();
101 QAxis *axisX();
101 QAxis *axisX();
102 QAxis *axisY();
102 Q_INVOKABLE QAxis *axisY(QAbstractSeries *series = 0);
103 QLegend *legend();
103 QLegend *legend();
104 QVariantList axisXLabels();
104 QVariantList axisXLabels();
105 void setAxisXLabels(QVariantList list);
105 void setAxisXLabels(QVariantList list);
106 void setTitleColor(QColor color);
106 void setTitleColor(QColor color);
107 QColor titleColor();
107 QColor titleColor();
108 void setBackgroundColor(QColor color);
108 void setBackgroundColor(QColor color);
109 QColor backgroundColor();
109 QColor backgroundColor();
110 int count();
110 int count();
111 void setDropShadowEnabled(bool enabled);
111 void setDropShadowEnabled(bool enabled);
112 bool dropShadowEnabled();
112 bool dropShadowEnabled();
113 Q_INVOKABLE void zoom(qreal factor);
113 Q_INVOKABLE void zoom(qreal factor);
114 Q_INVOKABLE void scrollLeft(qreal pixels);
114 Q_INVOKABLE void scrollLeft(qreal pixels);
115 Q_INVOKABLE void scrollRight(qreal pixels);
115 Q_INVOKABLE void scrollRight(qreal pixels);
116 Q_INVOKABLE void scrollUp(qreal pixels);
116 Q_INVOKABLE void scrollUp(qreal pixels);
117 Q_INVOKABLE void scrollDown(qreal pixels);
117 Q_INVOKABLE void scrollDown(qreal pixels);
118 Q_INVOKABLE QAbstractSeries *series(int index);
118 Q_INVOKABLE QAbstractSeries *series(int index);
119 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
119 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
120 Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = "");
120 Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = "");
121
121
122 Q_SIGNALS:
122 Q_SIGNALS:
123 void themeChanged();
123 void themeChanged();
124 void animationOptionsChanged();
124 void animationOptionsChanged();
125 void titleChanged();
125 void titleChanged();
126 void axisLabelsChanged();
126 void axisLabelsChanged();
127 void titleColorChanged();
127 void titleColorChanged();
128 void backgroundColorChanged();
128 void backgroundColorChanged();
129 void dropShadowEnabledChanged(bool enabled);
129 void dropShadowEnabledChanged(bool enabled);
130
130
131 public:
131 public:
132 // Extending QChart with DeclarativeChart is not possible because QObject does not support
132 // Extending QChart with DeclarativeChart is not possible because QObject does not support
133 // multi inheritance, so we now have a QChart as a member instead
133 // multi inheritance, so we now have a QChart as a member instead
134 QChart *m_chart;
134 QChart *m_chart;
135 };
135 };
136
136
137 QTCOMMERCIALCHART_END_NAMESPACE
137 QTCOMMERCIALCHART_END_NAMESPACE
138
138
139 #endif // DECLARATIVECHART_H
139 #endif // DECLARATIVECHART_H
@@ -1,291 +1,291
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qareaseries.h"
21 #include "qareaseries.h"
22 #include "qareaseries_p.h"
22 #include "qareaseries_p.h"
23 #include "qlineseries.h"
23 #include "qlineseries.h"
24 #include "areachartitem_p.h"
24 #include "areachartitem_p.h"
25 #include "legendmarker_p.h"
25 #include "legendmarker_p.h"
26 #include "domain_p.h"
26 #include "domain_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QAreaSeries
34 \class QAreaSeries
35 \brief The QAreaSeries class is used for making area charts.
35 \brief The QAreaSeries class is used for making area charts.
36
36
37 \mainclass
37 \mainclass
38
38
39 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
39 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
40 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
40 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
41 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
41 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
42 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
42 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
43 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
43 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
44
44
45 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
45 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
46 \image examples_areachart.png
46 \image examples_areachart.png
47 */
47 */
48
48
49 /*!
49 /*!
50 \fn QLineSeries* QAreaSeries::upperSeries() const
50 \property QAreaSeries::upperSeries
51 \brief Returns upperSeries used to define one of area boundaries.
51 \brief The upper one of the two line series used to define area series boundaries.
52 */
52 */
53
53
54 /*!
54 /*!
55 \fn QLineSeries* QAreaSeries::lowerSeries() const
55 \property QAreaSeries::lowerSeries
56 \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries
56 The lower one of the two line series used to define are series boundaries. Note if
57 this function return Null pointer.
57 QAreaSeries was counstucted wihtout a\ lowerSeries this is null.
58 */
58 */
59
59
60 /*!
60 /*!
61 \fn QPen QAreaSeries::pen() const
61 \fn QPen QAreaSeries::pen() const
62 \brief Returns the pen used to draw line for this series.
62 \brief Returns the pen used to draw line for this series.
63 \sa setPen()
63 \sa setPen()
64 */
64 */
65
65
66 /*!
66 /*!
67 \fn QPen QAreaSeries::brush() const
67 \fn QPen QAreaSeries::brush() const
68 \brief Returns the brush used to draw line for this series.
68 \brief Returns the brush used to draw line for this series.
69 \sa setBrush()
69 \sa setBrush()
70 */
70 */
71
71
72 /*!
72 /*!
73 \fn bool QAreaSeries::pointsVisible() const
73 \fn bool QAreaSeries::pointsVisible() const
74 \brief Returns if the points are drawn for this series.
74 \brief Returns if the points are drawn for this series.
75 \sa setPointsVisible()
75 \sa setPointsVisible()
76 */
76 */
77
77
78 /*!
78 /*!
79 \fn void QAreaSeries::clicked(const QPointF& point)
79 \fn void QAreaSeries::clicked(const QPointF& point)
80 \brief Signal is emitted when user clicks the \a point on area chart.
80 \brief Signal is emitted when user clicks the \a point on area chart.
81 */
81 */
82
82
83 /*!
83 /*!
84 \fn void QAreaSeries::selected()
84 \fn void QAreaSeries::selected()
85
85
86 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
86 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
87 implemented by the user of QAreaSeries API.
87 implemented by the user of QAreaSeries API.
88 */
88 */
89
89
90 /*!
90 /*!
91 \fn void QAreaSeriesPrivate::updated()
91 \fn void QAreaSeriesPrivate::updated()
92 \brief \internal
92 \brief \internal
93 */
93 */
94
94
95 /*!
95 /*!
96 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
96 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
97 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
97 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
98 When series object is added to QChartView or QChart instance ownerships is transferred.
98 When series object is added to QChartView or QChart instance ownerships is transferred.
99 */
99 */
100 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
100 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
101 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries,lowerSeries,this),upperSeries)
101 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries,lowerSeries,this),upperSeries)
102 {
102 {
103 }
103 }
104
104
105 /*!
105 /*!
106 Constructs area series object without upper or lower series with \a parent object.
106 Constructs area series object without upper or lower series with \a parent object.
107 */
107 */
108 QAreaSeries::QAreaSeries(QObject *parent)
108 QAreaSeries::QAreaSeries(QObject *parent)
109 : QAbstractSeries(*new QAreaSeriesPrivate(0, 0, this), parent)
109 : QAbstractSeries(*new QAreaSeriesPrivate(0, 0, this), parent)
110 {
110 {
111 }
111 }
112
112
113 /*!
113 /*!
114 Destroys the object. Series added to QChartView or QChart instances are owned by those,
114 Destroys the object. Series added to QChartView or QChart instances are owned by those,
115 and are deleted when mentioned object are destroyed.
115 and are deleted when mentioned object are destroyed.
116 */
116 */
117 QAreaSeries::~QAreaSeries()
117 QAreaSeries::~QAreaSeries()
118 {
118 {
119 }
119 }
120
120
121 /*!
121 /*!
122 Returns QChartSeries::SeriesTypeArea.
122 Returns QChartSeries::SeriesTypeArea.
123 */
123 */
124 QAbstractSeries::SeriesType QAreaSeries::type() const
124 QAbstractSeries::SeriesType QAreaSeries::type() const
125 {
125 {
126 return QAbstractSeries::SeriesTypeArea;
126 return QAbstractSeries::SeriesTypeArea;
127 }
127 }
128
128
129 /*!
129 /*!
130 Sets the \a series that is to be used as the area chart upper series.
130 Sets the \a series that is to be used as the area chart upper series.
131 */
131 */
132 void QAreaSeries::setUpperSeries(QLineSeries* series)
132 void QAreaSeries::setUpperSeries(QLineSeries* series)
133 {
133 {
134 Q_D(QAreaSeries);
134 Q_D(QAreaSeries);
135 d->m_upperSeries = series;
135 d->m_upperSeries = series;
136 }
136 }
137
137
138 QLineSeries* QAreaSeries::upperSeries() const
138 QLineSeries* QAreaSeries::upperSeries() const
139 {
139 {
140 Q_D(const QAreaSeries);
140 Q_D(const QAreaSeries);
141 return d->m_upperSeries;
141 return d->m_upperSeries;
142 }
142 }
143
143
144 /*!
144 /*!
145 Sets the \a series that is to be used as the area chart lower series.
145 Sets the \a series that is to be used as the area chart lower series.
146 */
146 */
147 void QAreaSeries::setLowerSeries(QLineSeries* series)
147 void QAreaSeries::setLowerSeries(QLineSeries* series)
148 {
148 {
149 Q_D(QAreaSeries);
149 Q_D(QAreaSeries);
150 d->m_lowerSeries = series;
150 d->m_lowerSeries = series;
151 }
151 }
152
152
153 QLineSeries* QAreaSeries::lowerSeries() const
153 QLineSeries* QAreaSeries::lowerSeries() const
154 {
154 {
155 Q_D(const QAreaSeries);
155 Q_D(const QAreaSeries);
156 return d->m_lowerSeries;
156 return d->m_lowerSeries;
157 }
157 }
158
158
159 /*!
159 /*!
160 Sets \a pen used for drawing area outline.
160 Sets \a pen used for drawing area outline.
161 */
161 */
162 void QAreaSeries::setPen(const QPen &pen)
162 void QAreaSeries::setPen(const QPen &pen)
163 {
163 {
164 Q_D(QAreaSeries);
164 Q_D(QAreaSeries);
165 if (d->m_pen != pen) {
165 if (d->m_pen != pen) {
166 d->m_pen = pen;
166 d->m_pen = pen;
167 emit d->updated();
167 emit d->updated();
168 }
168 }
169 }
169 }
170
170
171 QPen QAreaSeries::pen() const
171 QPen QAreaSeries::pen() const
172 {
172 {
173 Q_D(const QAreaSeries);
173 Q_D(const QAreaSeries);
174 return d->m_pen;
174 return d->m_pen;
175 }
175 }
176
176
177 /*!
177 /*!
178 Sets \a brush used for filling the area.
178 Sets \a brush used for filling the area.
179 */
179 */
180 void QAreaSeries::setBrush(const QBrush &brush)
180 void QAreaSeries::setBrush(const QBrush &brush)
181 {
181 {
182 Q_D(QAreaSeries);
182 Q_D(QAreaSeries);
183 if (d->m_brush != brush) {
183 if (d->m_brush != brush) {
184 d->m_brush = brush;
184 d->m_brush = brush;
185 emit d->updated();
185 emit d->updated();
186 }
186 }
187 }
187 }
188
188
189 QBrush QAreaSeries::brush() const
189 QBrush QAreaSeries::brush() const
190 {
190 {
191 Q_D(const QAreaSeries);
191 Q_D(const QAreaSeries);
192 return d->m_brush;
192 return d->m_brush;
193 }
193 }
194 /*!
194 /*!
195 Sets if data points are \a visible and should be drawn on line.
195 Sets if data points are \a visible and should be drawn on line.
196 */
196 */
197 void QAreaSeries::setPointsVisible(bool visible)
197 void QAreaSeries::setPointsVisible(bool visible)
198 {
198 {
199 Q_D(QAreaSeries);
199 Q_D(QAreaSeries);
200 if (d->m_pointsVisible != visible) {
200 if (d->m_pointsVisible != visible) {
201 d->m_pointsVisible = visible;
201 d->m_pointsVisible = visible;
202 emit d->updated();
202 emit d->updated();
203 }
203 }
204 }
204 }
205
205
206 bool QAreaSeries::pointsVisible() const
206 bool QAreaSeries::pointsVisible() const
207 {
207 {
208 Q_D(const QAreaSeries);
208 Q_D(const QAreaSeries);
209 return d->m_pointsVisible;
209 return d->m_pointsVisible;
210 }
210 }
211
211
212 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
212 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
213
213
214 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q) :
214 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q) :
215 QAbstractSeriesPrivate(q),
215 QAbstractSeriesPrivate(q),
216 m_upperSeries(upperSeries),
216 m_upperSeries(upperSeries),
217 m_lowerSeries(lowerSeries),
217 m_lowerSeries(lowerSeries),
218 m_pointsVisible(false)
218 m_pointsVisible(false)
219 {
219 {
220 }
220 }
221
221
222 void QAreaSeriesPrivate::scaleDomain(Domain& domain)
222 void QAreaSeriesPrivate::scaleDomain(Domain& domain)
223 {
223 {
224 Q_Q(QAreaSeries);
224 Q_Q(QAreaSeries);
225
225
226 qreal minX(domain.minX());
226 qreal minX(domain.minX());
227 qreal minY(domain.minY());
227 qreal minY(domain.minY());
228 qreal maxX(domain.maxX());
228 qreal maxX(domain.maxX());
229 qreal maxY(domain.maxY());
229 qreal maxY(domain.maxY());
230 int tickXCount(domain.tickXCount());
230 int tickXCount(domain.tickXCount());
231 int tickYCount(domain.tickYCount());
231 int tickYCount(domain.tickYCount());
232
232
233 QLineSeries* upperSeries = q->upperSeries();
233 QLineSeries* upperSeries = q->upperSeries();
234 QLineSeries* lowerSeries = q->lowerSeries();
234 QLineSeries* lowerSeries = q->lowerSeries();
235
235
236 const QList<QPointF>& points = upperSeries->points();
236 const QList<QPointF>& points = upperSeries->points();
237
237
238 for (int i = 0; i < points.count(); i++)
238 for (int i = 0; i < points.count(); i++)
239 {
239 {
240 qreal x = points[i].x();
240 qreal x = points[i].x();
241 qreal y = points[i].y();
241 qreal y = points[i].y();
242 minX = qMin(minX, x);
242 minX = qMin(minX, x);
243 minY = qMin(minY, y);
243 minY = qMin(minY, y);
244 maxX = qMax(maxX, x);
244 maxX = qMax(maxX, x);
245 maxY = qMax(maxY, y);
245 maxY = qMax(maxY, y);
246 }
246 }
247 if(lowerSeries) {
247 if(lowerSeries) {
248
248
249 const QList<QPointF>& points = lowerSeries->points();
249 const QList<QPointF>& points = lowerSeries->points();
250
250
251 for (int i = 0; i < points.count(); i++)
251 for (int i = 0; i < points.count(); i++)
252 {
252 {
253 qreal x = points[i].x();
253 qreal x = points[i].x();
254 qreal y = points[i].y();
254 qreal y = points[i].y();
255 minX = qMin(minX, x);
255 minX = qMin(minX, x);
256 minY = qMin(minY, y);
256 minY = qMin(minY, y);
257 maxX = qMax(maxX, x);
257 maxX = qMax(maxX, x);
258 maxY = qMax(maxY, y);
258 maxY = qMax(maxY, y);
259 }}
259 }}
260
260
261 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
261 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
262 }
262 }
263
263
264 Chart* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter)
264 Chart* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter)
265 {
265 {
266 Q_Q(QAreaSeries);
266 Q_Q(QAreaSeries);
267
267
268 AreaChartItem* area = new AreaChartItem(q,presenter);
268 AreaChartItem* area = new AreaChartItem(q,presenter);
269 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
269 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
270 area->upperLineItem()->setAnimator(presenter->animator());
270 area->upperLineItem()->setAnimator(presenter->animator());
271 area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem()));
271 area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem()));
272 if(q->lowerSeries()) {
272 if(q->lowerSeries()) {
273 area->lowerLineItem()->setAnimator(presenter->animator());
273 area->lowerLineItem()->setAnimator(presenter->animator());
274 area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem()));
274 area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem()));
275 }
275 }
276 }
276 }
277 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
277 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
278 return area;
278 return area;
279 }
279 }
280
280
281 QList<LegendMarker*> QAreaSeriesPrivate::createLegendMarker(QLegend* legend)
281 QList<LegendMarker*> QAreaSeriesPrivate::createLegendMarker(QLegend* legend)
282 {
282 {
283 Q_Q(QAreaSeries);
283 Q_Q(QAreaSeries);
284 QList<LegendMarker*> list;
284 QList<LegendMarker*> list;
285 return list << new AreaLegendMarker(q,legend);
285 return list << new AreaLegendMarker(q,legend);
286 }
286 }
287
287
288 #include "moc_qareaseries.cpp"
288 #include "moc_qareaseries.cpp"
289 #include "moc_qareaseries_p.cpp"
289 #include "moc_qareaseries_p.cpp"
290
290
291 QTCOMMERCIALCHART_END_NAMESPACE
291 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,74
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QAREASERIES_H
21 #ifndef QAREASERIES_H
22 #define QAREASERIES_H
22 #define QAREASERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qabstractseries.h>
25 #include <qabstractseries.h>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QLineSeries;
30 class QLineSeries;
31 class QAreaSeriesPrivate;
31 class QAreaSeriesPrivate;
32
32
33 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QAbstractSeries
33 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QAbstractSeries
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(QAbstractSeries *upperSeries READ upperSeries)
37 Q_PROPERTY(QAbstractSeries *lowerSeries READ lowerSeries)
36
38
37 public:
39 public:
38 explicit QAreaSeries(QObject *parent = 0);
40 explicit QAreaSeries(QObject *parent = 0);
39 explicit QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries = 0);
41 explicit QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries = 0);
40 ~QAreaSeries();
42 ~QAreaSeries();
41
43
42 public:
44 public:
43 QAbstractSeries::SeriesType type() const;
45 QAbstractSeries::SeriesType type() const;
44
46
45 void setUpperSeries(QLineSeries* series);
47 void setUpperSeries(QLineSeries* series);
46 QLineSeries* upperSeries() const;
48 QLineSeries* upperSeries() const;
47 void setLowerSeries(QLineSeries* series);
49 void setLowerSeries(QLineSeries* series);
48 QLineSeries* lowerSeries() const;
50 QLineSeries* lowerSeries() const;
49
51
50 void setPen(const QPen &pen);
52 void setPen(const QPen &pen);
51 QPen pen() const;
53 QPen pen() const;
52
54
53 void setBrush(const QBrush &brush);
55 void setBrush(const QBrush &brush);
54 QBrush brush() const;
56 QBrush brush() const;
55
57
56 void setPointsVisible(bool visible = true);
58 void setPointsVisible(bool visible = true);
57 bool pointsVisible() const;
59 bool pointsVisible() const;
58
60
59 Q_SIGNALS:
61 Q_SIGNALS:
60 void clicked(const QPointF &point);
62 void clicked(const QPointF &point);
61 void selected();
63 void selected();
62
64
63 private:
65 private:
64 Q_DECLARE_PRIVATE(QAreaSeries);
66 Q_DECLARE_PRIVATE(QAreaSeries);
65 Q_DISABLE_COPY(QAreaSeries);
67 Q_DISABLE_COPY(QAreaSeries);
66 friend class AreaLegendMarker;
68 friend class AreaLegendMarker;
67 friend class AreaChartItem;
69 friend class AreaChartItem;
68 };
70 };
69
71
70 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
71
73
72 #endif
74 #endif
@@ -1,213 +1,213
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qbarseries_p.h"
26 #include "qbarseries_p.h"
27 #include "qchart.h"
27 #include "qchart.h"
28 #include "chartpresenter_p.h"
28 #include "chartpresenter_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30 #include "chartdataset_p.h"
30 #include "chartdataset_p.h"
31 #include <QPainter>
31 #include <QPainter>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
36 ChartItem(presenter),
36 ChartItem(presenter),
37 m_series(series)
37 m_series(series)
38 {
38 {
39 setFlag(ItemClipsChildrenToShape);
39 setFlag(ItemClipsChildrenToShape);
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
44 setZValue(ChartPresenter::BarSeriesZValue);
44 setZValue(ChartPresenter::BarSeriesZValue);
45 handleDataStructureChanged();
45 handleDataStructureChanged();
46 }
46 }
47
47
48 BarChartItem::~BarChartItem()
48 BarChartItem::~BarChartItem()
49 {
49 {
50 }
50 }
51
51
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
53 {
53 {
54 Q_UNUSED(painter);
54 Q_UNUSED(painter);
55 Q_UNUSED(option);
55 Q_UNUSED(option);
56 Q_UNUSED(widget);
56 Q_UNUSED(widget);
57 }
57 }
58
58
59 QRectF BarChartItem::boundingRect() const
59 QRectF BarChartItem::boundingRect() const
60 {
60 {
61 return m_rect;
61 return m_rect;
62 }
62 }
63
63
64 void BarChartItem::handleDataStructureChanged()
64 void BarChartItem::handleDataStructureChanged()
65 {
65 {
66 foreach(QGraphicsItem *item, childItems()) {
66 foreach(QGraphicsItem *item, childItems()) {
67 delete item;
67 delete item;
68 }
68 }
69
69
70 m_bars.clear();
70 m_bars.clear();
71 m_labels.clear();
71 m_labels.clear();
72 m_layout.clear();
72 m_layout.clear();
73
73
74 bool labelsVisible = m_series->isLabelsVisible();
74 bool labelsVisible = m_series->isLabelsVisible();
75
75
76 // Create new graphic items for bars
76 // Create new graphic items for bars
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
78 for (int s = 0; s < m_series->count(); s++) {
79 QBarSet *set = m_series->d_func()->barsetAt(s);
79 QBarSet *set = m_series->d_func()->barsetAt(s);
80
80
81 // Bars
81 // Bars
82 Bar *bar = new Bar(set,c,this);
82 Bar *bar = new Bar(set,c,this);
83 m_bars.append(bar);
83 m_bars.append(bar);
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 m_layout.append(QRectF(0, 0, 0, 0));
86 m_layout.append(QRectF(0, 0, 0, 0));
87
87
88 // Labels
88 // Labels
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
90 label->setVisible(labelsVisible);
90 label->setVisible(labelsVisible);
91 m_labels.append(label);
91 m_labels.append(label);
92 }
92 }
93 }
93 }
94
94
95 // TODO: Is this the right place to call it?
95 // TODO: Is this the right place to call it?
96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
97 handleLayoutChanged();
97 handleLayoutChanged();
98 }
98 }
99
99
100 QVector<QRectF> BarChartItem::calculateLayout()
100 QVector<QRectF> BarChartItem::calculateLayout()
101 {
101 {
102 QVector<QRectF> layout;
102 QVector<QRectF> layout;
103
103
104 // Use temporary qreals for accuracy
104 // Use temporary qreals for accuracy
105 qreal categoryCount = m_series->d_func()->categoryCount();
105 qreal categoryCount = m_series->d_func()->categoryCount();
106 qreal setCount = m_series->barsetCount();
106 qreal setCount = m_series->count();
107 bool barsVisible = m_series->isVisible();
107 bool barsVisible = m_series->isVisible();
108
108
109 // Domain:
109 // Domain:
110 qreal width = geometry().width();
110 qreal width = geometry().width();
111 qreal height = geometry().height();
111 qreal height = geometry().height();
112 qreal rangeY = m_domainMaxY - m_domainMinY;
112 qreal rangeY = m_domainMaxY - m_domainMinY;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
114 qreal scaleY = (height / rangeY);
114 qreal scaleY = (height / rangeY);
115 qreal scaleX = (width / rangeX);
115 qreal scaleX = (width / rangeX);
116 qreal barWidth = scaleX * m_series->d_func()->barWidth();
116 qreal barWidth = scaleX * m_series->d_func()->barWidth();
117
117
118 int itemIndex(0);
118 int itemIndex(0);
119 for (int category = 0; category < categoryCount; category++) {
119 for (int category = 0; category < categoryCount; category++) {
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
121 for (int set = 0; set < setCount; set++) {
121 for (int set = 0; set < setCount; set++) {
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
124 qreal barHeight = barSet->at(category).y() * scaleY;
124 qreal barHeight = barSet->at(category).y() * scaleY;
125
125
126 Bar* bar = m_bars.at(itemIndex);
126 Bar* bar = m_bars.at(itemIndex);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128
128
129 layout.append(rect);
129 layout.append(rect);
130 bar->setPen(barSet->pen());
130 bar->setPen(barSet->pen());
131 bar->setBrush(barSet->brush());
131 bar->setBrush(barSet->brush());
132 bar->setVisible(barsVisible);
132 bar->setVisible(barsVisible);
133
133
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
135
135
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
137 label->setText(QString::number(barSet->at(category).y()));
137 label->setText(QString::number(barSet->at(category).y()));
138 } else {
138 } else {
139 label->setText(QString(""));
139 label->setText(QString(""));
140 }
140 }
141
141
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
144 label->setFont(barSet->labelFont());
144 label->setFont(barSet->labelFont());
145 label->setBrush(barSet->labelBrush());
145 label->setBrush(barSet->labelBrush());
146
146
147 itemIndex++;
147 itemIndex++;
148 }
148 }
149 }
149 }
150
150
151 return layout;
151 return layout;
152 }
152 }
153
153
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 {
155 {
156 if (animator()) {
156 if (animator()) {
157 animator()->updateLayout(this, m_layout, layout);
157 animator()->updateLayout(this, m_layout, layout);
158 } else {
158 } else {
159 setLayout(layout);
159 setLayout(layout);
160 update();
160 update();
161 }
161 }
162 }
162 }
163
163
164 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 void BarChartItem::setLayout(const QVector<QRectF> &layout)
165 {
165 {
166 if (layout.count() != m_bars.count())
166 if (layout.count() != m_bars.count())
167 return;
167 return;
168
168
169 m_layout = layout;
169 m_layout = layout;
170
170
171 for (int i=0; i < m_bars.count(); i++) {
171 for (int i=0; i < m_bars.count(); i++) {
172 m_bars.at(i)->setRect(layout.at(i));
172 m_bars.at(i)->setRect(layout.at(i));
173 }
173 }
174 }
174 }
175 //handlers
175 //handlers
176
176
177 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
177 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
178 {
178 {
179 m_domainMinX = minX;
179 m_domainMinX = minX;
180 m_domainMaxX = maxX;
180 m_domainMaxX = maxX;
181 m_domainMinY = minY;
181 m_domainMinY = minY;
182 m_domainMaxY = maxY;
182 m_domainMaxY = maxY;
183 handleLayoutChanged();
183 handleLayoutChanged();
184 }
184 }
185
185
186 void BarChartItem::handleGeometryChanged(const QRectF &rect)
186 void BarChartItem::handleGeometryChanged(const QRectF &rect)
187 {
187 {
188 prepareGeometryChange();
188 prepareGeometryChange();
189 m_rect = rect;
189 m_rect = rect;
190 handleLayoutChanged();
190 handleLayoutChanged();
191 }
191 }
192
192
193 void BarChartItem::handleLayoutChanged()
193 void BarChartItem::handleLayoutChanged()
194 {
194 {
195 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
195 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
196 // rect size zero.
196 // rect size zero.
197 return;
197 return;
198 }
198 }
199 QVector<QRectF> layout = calculateLayout();
199 QVector<QRectF> layout = calculateLayout();
200 applyLayout(layout);
200 applyLayout(layout);
201 }
201 }
202
202
203 void BarChartItem::handleLabelsVisibleChanged(bool visible)
203 void BarChartItem::handleLabelsVisibleChanged(bool visible)
204 {
204 {
205 foreach (QGraphicsSimpleTextItem* label, m_labels) {
205 foreach (QGraphicsSimpleTextItem* label, m_labels) {
206 label->setVisible(visible);
206 label->setVisible(visible);
207 }
207 }
208 update();
208 update();
209 }
209 }
210
210
211 #include "moc_barchartitem_p.cpp"
211 #include "moc_barchartitem_p.cpp"
212
212
213 QTCOMMERCIALCHART_END_NAMESPACE
213 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,91 +1,91
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "groupedbarchartitem_p.h"
21 #include "groupedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->count();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
50 qreal barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
55 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
57
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
59 xPos -= setCount*barWidth/2;
59 xPos -= setCount*barWidth/2;
60 xPos += set*barWidth;
60 xPos += set*barWidth;
61 qreal barHeight = barSet->at(category).y() * scaleY;
61 qreal barHeight = barSet->at(category).y() * scaleY;
62 Bar* bar = m_bars.at(itemIndex);
62 Bar* bar = m_bars.at(itemIndex);
63
63
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 layout.append(rect);
65 layout.append(rect);
66 bar->setPen(barSet->pen());
66 bar->setPen(barSet->pen());
67 bar->setBrush(barSet->brush());
67 bar->setBrush(barSet->brush());
68 bar->setVisible(barsVisible);
68 bar->setVisible(barsVisible);
69
69
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
71
71
72 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 if (!qFuzzyIsNull(barSet->at(category).y())) {
73 label->setText(QString::number(barSet->at(category).y()));
73 label->setText(QString::number(barSet->at(category).y()));
74 } else {
74 } else {
75 label->setText(QString(""));
75 label->setText(QString(""));
76 }
76 }
77
77
78 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
79 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 ,yPos - barHeight/2 - label->boundingRect().height()/2);
80 label->setFont(barSet->labelFont());
80 label->setFont(barSet->labelFont());
81 label->setBrush(barSet->labelBrush());
81 label->setBrush(barSet->labelBrush());
82
82
83 itemIndex++;
83 itemIndex++;
84 }
84 }
85 }
85 }
86 return layout;
86 return layout;
87 }
87 }
88
88
89 #include "moc_groupedbarchartitem_p.cpp"
89 #include "moc_groupedbarchartitem_p.cpp"
90
90
91 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,106
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "percentbarchartitem_p.h"
21 #include "percentbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarseries_p.h"
23 #include "qbarseries_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "chartanimator_p.h"
25 #include "chartanimator_p.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->count();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal barWidth = scaleX * m_series->d_func()->barWidth();
50 qreal barWidth = scaleX * m_series->d_func()->barWidth();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal percentage = (100 / colSum);
55 qreal percentage = (100 / colSum);
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
57 for (int set=0; set < setCount; set++) {
57 for (int set=0; set < setCount; set++) {
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
59
59
60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
61
61
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
64 bar->setPen(barSet->pen());
64 bar->setPen(barSet->pen());
65 bar->setBrush(barSet->brush());
65 bar->setBrush(barSet->brush());
66 bar->setVisible(barsVisible);
66 bar->setVisible(barsVisible);
67
67
68 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
68 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
69 layout.append(rect);
69 layout.append(rect);
70
70
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
72
72
73 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
73 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
74 int p = m_series->d_func()->percentageAt(set,category) * 100;
74 int p = m_series->d_func()->percentageAt(set,category) * 100;
75 QString vString(QString::number(p));
75 QString vString(QString::number(p));
76 vString.truncate(3);
76 vString.truncate(3);
77 vString.append("%");
77 vString.append("%");
78 label->setText(vString);
78 label->setText(vString);
79 } else {
79 } else {
80 label->setText(QString(""));
80 label->setText(QString(""));
81 }
81 }
82
82
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
84 ,yPos - barHeight/2 - label->boundingRect().height()/2);
84 ,yPos - barHeight/2 - label->boundingRect().height()/2);
85 label->setFont(barSet->labelFont());
85 label->setFont(barSet->labelFont());
86 label->setBrush(barSet->labelBrush());
86 label->setBrush(barSet->labelBrush());
87
87
88 itemIndex++;
88 itemIndex++;
89 yPos -= barHeight;
89 yPos -= barHeight;
90 }
90 }
91 }
91 }
92 return layout;
92 return layout;
93 }
93 }
94
94
95 void PercentBarChartItem::applyLayout(const QVector<QRectF> &layout)
95 void PercentBarChartItem::applyLayout(const QVector<QRectF> &layout)
96 {
96 {
97 if (animator()) {
97 if (animator()) {
98 animator()->updateLayout(this, m_layout, layout);
98 animator()->updateLayout(this, m_layout, layout);
99 } else {
99 } else {
100 setLayout(layout);
100 setLayout(layout);
101 update();
101 update();
102 }
102 }
103 }
103 }
104 #include "moc_percentbarchartitem_p.cpp"
104 #include "moc_percentbarchartitem_p.cpp"
105
105
106 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,701 +1,701
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QBarSeries
34 \class QBarSeries
35 \brief part of QtCommercial chart API.
35 \brief part of QtCommercial chart API.
36 \mainclass
36 \mainclass
37
37
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 shows the x-values.
41 shows the x-values.
42
42
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 \image examples_barchart.png
44 \image examples_barchart.png
45
45
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 */
47 */
48
48
49 /*!
49 /*!
50 \property QBarSeries::barWidth
50 \property QBarSeries::barWidth
51 \brief Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
51 \brief Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
52 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
52 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
53 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
53 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
54 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
54 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
55 because with grouped series it is more logical to set width of whole group and let the chart calculate correct
55 because with grouped series it is more logical to set width of whole group and let the chart calculate correct
56 width for bar.
56 width for bar.
57 \sa QGroupedBarSeries
57 \sa QGroupedBarSeries
58 */
58 */
59
59
60 /*!
60 /*!
61 \property QBarSeries::count
61 \property QBarSeries::count
62 \brief Holds the number of sets in series.
62 \brief Holds the number of sets in series.
63 */
63 */
64
64
65 /*!
65 /*!
66 \property QBarSeries::labelsVisible
66 \property QBarSeries::labelsVisible
67 \brief Defines the visibility of the labels in series
67 \brief Defines the visibility of the labels in series
68 */
68 */
69
69
70 /*!
70 /*!
71 \fn void QBarSeries::clicked(QBarSet *barset, int index)
71 \fn void QBarSeries::clicked(QBarSet *barset, int index)
72
72
73 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
73 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
74 Clicked bar inside set is indexed by \a index
74 Clicked bar inside set is indexed by \a index
75 */
75 */
76
76
77 /*!
77 /*!
78 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
78 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
79
79
80 The signal is emitted if mouse is hovered on top of series.
80 The signal is emitted if mouse is hovered on top of series.
81 Parameter \a barset is the pointer of barset, where hover happened.
81 Parameter \a barset is the pointer of barset, where hover happened.
82 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
82 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
83 */
83 */
84 /*!
84 /*!
85 \fn void QBarSeries::barWidthChanged()
85 \fn void QBarSeries::barWidthChanged()
86
86
87 This signal is emitted when bar width has been changed.
87 This signal is emitted when bar width has been changed.
88 */
88 */
89
89
90 /*!
90 /*!
91 \fn void QBarSeries::barsetCountChanged()
91 \fn void QBarSeries::countChanged()
92
92
93 This signal is emitted when barset count has been changed, for example by append or remove.
93 This signal is emitted when barset count has been changed, for example by append or remove.
94 */
94 */
95
95
96 /*!
96 /*!
97 \fn void QBarSeries::labelsVisibleChanged()
97 \fn void QBarSeries::labelsVisibleChanged()
98
98
99 This signal is emitted when labels visibility have changed.
99 This signal is emitted when labels visibility have changed.
100
100
101 \sa isLabelsVisible(), setLabelsVisible()
101 \sa isLabelsVisible(), setLabelsVisible()
102 */
102 */
103
103
104 /*!
104 /*!
105 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
105 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
106
106
107 This signal is emitted when \a sets have been added to the series.
107 This signal is emitted when \a sets have been added to the series.
108
108
109 \sa append(), insert()
109 \sa append(), insert()
110 */
110 */
111
111
112 /*!
112 /*!
113 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
113 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
114
114
115 This signal is emitted when \a sets have been removed from the series.
115 This signal is emitted when \a sets have been removed from the series.
116
116
117 \sa remove()
117 \sa remove()
118 */
118 */
119
119
120 /*!
120 /*!
121 Constructs empty QBarSeries.
121 Constructs empty QBarSeries.
122 QBarSeries is QObject which is a child of a \a parent.
122 QBarSeries is QObject which is a child of a \a parent.
123 */
123 */
124 QBarSeries::QBarSeries(QObject *parent) :
124 QBarSeries::QBarSeries(QObject *parent) :
125 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
125 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
126 {
126 {
127 }
127 }
128
128
129 /*!
129 /*!
130 Destructs barseries and owned barsets.
130 Destructs barseries and owned barsets.
131 */
131 */
132 QBarSeries::~QBarSeries()
132 QBarSeries::~QBarSeries()
133 {
133 {
134 Q_D(QBarSeries);
134 Q_D(QBarSeries);
135 if(d->m_dataset){
135 if(d->m_dataset){
136 d->m_dataset->removeSeries(this);
136 d->m_dataset->removeSeries(this);
137 }
137 }
138 }
138 }
139
139
140 /*!
140 /*!
141 \internal
141 \internal
142 */
142 */
143 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
143 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
144 QAbstractSeries(d,parent)
144 QAbstractSeries(d,parent)
145 {
145 {
146 }
146 }
147
147
148 /*!
148 /*!
149 Returns the type of series. Derived classes override this.
149 Returns the type of series. Derived classes override this.
150 */
150 */
151 QAbstractSeries::SeriesType QBarSeries::type() const
151 QAbstractSeries::SeriesType QBarSeries::type() const
152 {
152 {
153 return QAbstractSeries::SeriesTypeBar;
153 return QAbstractSeries::SeriesTypeBar;
154 }
154 }
155
155
156 /*!
156 /*!
157 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
157 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
158 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
158 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
159 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
159 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
160 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
160 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
161 because with grouped series it is more logical to set widht of whole group and let the chart calculate correct
161 because with grouped series it is more logical to set widht of whole group and let the chart calculate correct
162 width for bar.
162 width for bar.
163 \sa QGroupedBarSeries
163 \sa QGroupedBarSeries
164 */
164 */
165 void QBarSeries::setBarWidth(qreal width)
165 void QBarSeries::setBarWidth(qreal width)
166 {
166 {
167 Q_D(QBarSeries);
167 Q_D(QBarSeries);
168 d->setBarWidth(width);
168 d->setBarWidth(width);
169 emit barWidthChanged();
169 emit barWidthChanged();
170 }
170 }
171
171
172 /*!
172 /*!
173 Returns the width of bars.
173 Returns the width of bars.
174 */
174 */
175 qreal QBarSeries::barWidth() const
175 qreal QBarSeries::barWidth() const
176 {
176 {
177 Q_D(const QBarSeries);
177 Q_D(const QBarSeries);
178 return d->barWidth();
178 return d->barWidth();
179 }
179 }
180
180
181 /*!
181 /*!
182 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
182 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
183 Returns true, if appending succeeded.
183 Returns true, if appending succeeded.
184
184
185 */
185 */
186 bool QBarSeries::append(QBarSet *set)
186 bool QBarSeries::append(QBarSet *set)
187 {
187 {
188 Q_D(QBarSeries);
188 Q_D(QBarSeries);
189 bool success = d->append(set);
189 bool success = d->append(set);
190 if (success) {
190 if (success) {
191 QList<QBarSet*> sets;
191 QList<QBarSet*> sets;
192 sets.append(set);
192 sets.append(set);
193 emit barsetsAdded(sets);
193 emit barsetsAdded(sets);
194 emit barsetCountChanged();
194 emit countChanged();
195 }
195 }
196 return success;
196 return success;
197 }
197 }
198
198
199 /*!
199 /*!
200 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
200 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
201 Returns true, if set was removed.
201 Returns true, if set was removed.
202 */
202 */
203 bool QBarSeries::remove(QBarSet *set)
203 bool QBarSeries::remove(QBarSet *set)
204 {
204 {
205 Q_D(QBarSeries);
205 Q_D(QBarSeries);
206 bool success = d->remove(set);
206 bool success = d->remove(set);
207 if (success) {
207 if (success) {
208 QList<QBarSet*> sets;
208 QList<QBarSet*> sets;
209 sets.append(set);
209 sets.append(set);
210 emit barsetsRemoved(sets);
210 emit barsetsRemoved(sets);
211 emit barsetCountChanged();
211 emit countChanged();
212 }
212 }
213 return success;
213 return success;
214 }
214 }
215
215
216 /*!
216 /*!
217 Adds a list of barsets to series. Takes ownership of \a sets.
217 Adds a list of barsets to series. Takes ownership of \a sets.
218 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
218 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
219 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
219 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
220 and function returns false.
220 and function returns false.
221 */
221 */
222 bool QBarSeries::append(QList<QBarSet* > sets)
222 bool QBarSeries::append(QList<QBarSet* > sets)
223 {
223 {
224 Q_D(QBarSeries);
224 Q_D(QBarSeries);
225 bool success = d->append(sets);
225 bool success = d->append(sets);
226 if (success) {
226 if (success) {
227 emit barsetsAdded(sets);
227 emit barsetsAdded(sets);
228 emit barsetCountChanged();
228 emit countChanged();
229 }
229 }
230 return success;
230 return success;
231 }
231 }
232
232
233 /*!
233 /*!
234 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
234 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
235 Returns true, if inserting succeeded.
235 Returns true, if inserting succeeded.
236
236
237 */
237 */
238 bool QBarSeries::insert(int index, QBarSet *set)
238 bool QBarSeries::insert(int index, QBarSet *set)
239 {
239 {
240 Q_D(QBarSeries);
240 Q_D(QBarSeries);
241 bool success = d->insert(index, set);
241 bool success = d->insert(index, set);
242 if (success) {
242 if (success) {
243 QList<QBarSet*> sets;
243 QList<QBarSet*> sets;
244 sets.append(set);
244 sets.append(set);
245 emit barsetsAdded(sets);
245 emit barsetsAdded(sets);
246 emit barsetCountChanged();
246 emit countChanged();
247 }
247 }
248 return success;
248 return success;
249 }
249 }
250
250
251 /*!
251 /*!
252 Removes all of the bar sets from the series
252 Removes all of the bar sets from the series
253 */
253 */
254 void QBarSeries::clear()
254 void QBarSeries::clear()
255 {
255 {
256 Q_D(QBarSeries);
256 Q_D(QBarSeries);
257 QList<QBarSet *> sets = barSets();
257 QList<QBarSet *> sets = barSets();
258 bool success = d->remove(sets);
258 bool success = d->remove(sets);
259 if (success) {
259 if (success) {
260 emit barsetsRemoved(sets);
260 emit barsetsRemoved(sets);
261 emit barsetCountChanged();
261 emit countChanged();
262 }
262 }
263 }
263 }
264
264
265 /*!
265 /*!
266 Returns number of sets in series.
266 Returns number of sets in series.
267 */
267 */
268 int QBarSeries::barsetCount() const
268 int QBarSeries::count() const
269 {
269 {
270 Q_D(const QBarSeries);
270 Q_D(const QBarSeries);
271 return d->m_barSets.count();
271 return d->m_barSets.count();
272 }
272 }
273
273
274 /*!
274 /*!
275 Returns a list of sets in series. Keeps ownership of sets.
275 Returns a list of sets in series. Keeps ownership of sets.
276 */
276 */
277 QList<QBarSet*> QBarSeries::barSets() const
277 QList<QBarSet*> QBarSeries::barSets() const
278 {
278 {
279 Q_D(const QBarSeries);
279 Q_D(const QBarSeries);
280 return d->m_barSets;
280 return d->m_barSets;
281 }
281 }
282
282
283 /*!
283 /*!
284 Sets the visibility of labels in series to \a visible
284 Sets the visibility of labels in series to \a visible
285 */
285 */
286 void QBarSeries::setLabelsVisible(bool visible)
286 void QBarSeries::setLabelsVisible(bool visible)
287 {
287 {
288 Q_D(QBarSeries);
288 Q_D(QBarSeries);
289 if (d->m_labelsVisible != visible) {
289 if (d->m_labelsVisible != visible) {
290 d->setLabelsVisible(visible);
290 d->setLabelsVisible(visible);
291 emit labelsVisibleChanged();
291 emit labelsVisibleChanged();
292 }
292 }
293 }
293 }
294
294
295 /*!
295 /*!
296 Returns the visibility of labels
296 Returns the visibility of labels
297 */
297 */
298 bool QBarSeries::isLabelsVisible() const
298 bool QBarSeries::isLabelsVisible() const
299 {
299 {
300 Q_D(const QBarSeries);
300 Q_D(const QBarSeries);
301 return d->m_labelsVisible;
301 return d->m_labelsVisible;
302 }
302 }
303
303
304 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
304 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
305
305
306 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
306 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
307 QAbstractSeriesPrivate(q),
307 QAbstractSeriesPrivate(q),
308 m_barWidth(0.5), // Default value is 50% of category width
308 m_barWidth(0.5), // Default value is 50% of category width
309 m_labelsVisible(false),
309 m_labelsVisible(false),
310 m_visible(true)
310 m_visible(true)
311 {
311 {
312 }
312 }
313
313
314 void QBarSeriesPrivate::setCategories(QStringList categories)
314 void QBarSeriesPrivate::setCategories(QStringList categories)
315 {
315 {
316 m_categories = categories;
316 m_categories = categories;
317 }
317 }
318
318
319 void QBarSeriesPrivate::insertCategory(int index, const QString category)
319 void QBarSeriesPrivate::insertCategory(int index, const QString category)
320 {
320 {
321 m_categories.insert(index, category);
321 m_categories.insert(index, category);
322 emit categoriesUpdated();
322 emit categoriesUpdated();
323 }
323 }
324
324
325 void QBarSeriesPrivate::removeCategory(int index)
325 void QBarSeriesPrivate::removeCategory(int index)
326 {
326 {
327 m_categories.removeAt(index);
327 m_categories.removeAt(index);
328 emit categoriesUpdated();
328 emit categoriesUpdated();
329 }
329 }
330
330
331 int QBarSeriesPrivate::categoryCount() const
331 int QBarSeriesPrivate::categoryCount() const
332 {
332 {
333 if (m_categories.count() > 0) {
333 if (m_categories.count() > 0) {
334 return m_categories.count();
334 return m_categories.count();
335 }
335 }
336
336
337 // No categories defined. return count of longest set.
337 // No categories defined. return count of longest set.
338 int count = 0;
338 int count = 0;
339 for (int i=0; i<m_barSets.count(); i++) {
339 for (int i=0; i<m_barSets.count(); i++) {
340 if (m_barSets.at(i)->count() > count) {
340 if (m_barSets.at(i)->count() > count) {
341 count = m_barSets.at(i)->count();
341 count = m_barSets.at(i)->count();
342 }
342 }
343 }
343 }
344
344
345 return count;
345 return count;
346 }
346 }
347
347
348 QStringList QBarSeriesPrivate::categories() const
348 QStringList QBarSeriesPrivate::categories() const
349 {
349 {
350 if (m_categories.count() > 0) {
350 if (m_categories.count() > 0) {
351 return m_categories;
351 return m_categories;
352 }
352 }
353
353
354 // No categories defined. retun list of indices.
354 // No categories defined. retun list of indices.
355 QStringList categories;
355 QStringList categories;
356
356
357 int count = categoryCount();
357 int count = categoryCount();
358 for (int i = 0; i < count; i++) {
358 for (int i = 0; i < count; i++) {
359 categories.append(QString::number(i));
359 categories.append(QString::number(i));
360 }
360 }
361 return categories;
361 return categories;
362 }
362 }
363
363
364 void QBarSeriesPrivate::setBarWidth(qreal width)
364 void QBarSeriesPrivate::setBarWidth(qreal width)
365 {
365 {
366 if (width < 0.0) {
366 if (width < 0.0) {
367 width = 0.0;
367 width = 0.0;
368 }
368 }
369 m_barWidth = width;
369 m_barWidth = width;
370 emit updatedBars();
370 emit updatedBars();
371 }
371 }
372
372
373 qreal QBarSeriesPrivate::barWidth() const
373 qreal QBarSeriesPrivate::barWidth() const
374 {
374 {
375 return m_barWidth;
375 return m_barWidth;
376 }
376 }
377
377
378 QBarSet* QBarSeriesPrivate::barsetAt(int index)
378 QBarSet* QBarSeriesPrivate::barsetAt(int index)
379 {
379 {
380 return m_barSets.at(index);
380 return m_barSets.at(index);
381 }
381 }
382
382
383 void QBarSeriesPrivate::setVisible(bool visible)
383 void QBarSeriesPrivate::setVisible(bool visible)
384 {
384 {
385 m_visible = visible;
385 m_visible = visible;
386 emit updatedBars();
386 emit updatedBars();
387 }
387 }
388
388
389 void QBarSeriesPrivate::setLabelsVisible(bool visible)
389 void QBarSeriesPrivate::setLabelsVisible(bool visible)
390 {
390 {
391 m_labelsVisible = visible;
391 m_labelsVisible = visible;
392 emit labelsVisibleChanged(visible);
392 emit labelsVisibleChanged(visible);
393 }
393 }
394
394
395 QString QBarSeriesPrivate::categoryName(int category)
395 QString QBarSeriesPrivate::categoryName(int category)
396 {
396 {
397 if ((category >= 0) && (category < m_categories.count())) {
397 if ((category >= 0) && (category < m_categories.count())) {
398 return m_categories.at(category);
398 return m_categories.at(category);
399 }
399 }
400
400
401 return QString::number(category);
401 return QString::number(category);
402 }
402 }
403
403
404 qreal QBarSeriesPrivate::min()
404 qreal QBarSeriesPrivate::min()
405 {
405 {
406 if (m_barSets.count() <= 0) {
406 if (m_barSets.count() <= 0) {
407 return 0;
407 return 0;
408 }
408 }
409 qreal min = INT_MAX;
409 qreal min = INT_MAX;
410
410
411 for (int i = 0; i < m_barSets.count(); i++) {
411 for (int i = 0; i < m_barSets.count(); i++) {
412 int categoryCount = m_barSets.at(i)->count();
412 int categoryCount = m_barSets.at(i)->count();
413 for (int j = 0; j < categoryCount; j++) {
413 for (int j = 0; j < categoryCount; j++) {
414 qreal temp = m_barSets.at(i)->at(j).y();
414 qreal temp = m_barSets.at(i)->at(j).y();
415 if (temp < min)
415 if (temp < min)
416 min = temp;
416 min = temp;
417 }
417 }
418 }
418 }
419 return min;
419 return min;
420 }
420 }
421
421
422 qreal QBarSeriesPrivate::max()
422 qreal QBarSeriesPrivate::max()
423 {
423 {
424 if (m_barSets.count() <= 0) {
424 if (m_barSets.count() <= 0) {
425 return 0;
425 return 0;
426 }
426 }
427 qreal max = INT_MIN;
427 qreal max = INT_MIN;
428
428
429 for (int i = 0; i < m_barSets.count(); i++) {
429 for (int i = 0; i < m_barSets.count(); i++) {
430 int categoryCount = m_barSets.at(i)->count();
430 int categoryCount = m_barSets.at(i)->count();
431 for (int j = 0; j < categoryCount; j++) {
431 for (int j = 0; j < categoryCount; j++) {
432 qreal temp = m_barSets.at(i)->at(j).y();
432 qreal temp = m_barSets.at(i)->at(j).y();
433 if (temp > max)
433 if (temp > max)
434 max = temp;
434 max = temp;
435 }
435 }
436 }
436 }
437
437
438 return max;
438 return max;
439 }
439 }
440
440
441 qreal QBarSeriesPrivate::valueAt(int set, int category)
441 qreal QBarSeriesPrivate::valueAt(int set, int category)
442 {
442 {
443 if ((set < 0) || (set >= m_barSets.count())) {
443 if ((set < 0) || (set >= m_barSets.count())) {
444 // No set, no value.
444 // No set, no value.
445 return 0;
445 return 0;
446 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
446 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
447 // No category, no value.
447 // No category, no value.
448 return 0;
448 return 0;
449 }
449 }
450
450
451 return m_barSets.at(set)->at(category).y();
451 return m_barSets.at(set)->at(category).y();
452 }
452 }
453
453
454 qreal QBarSeriesPrivate::percentageAt(int set, int category)
454 qreal QBarSeriesPrivate::percentageAt(int set, int category)
455 {
455 {
456 if ((set < 0) || (set >= m_barSets.count())) {
456 if ((set < 0) || (set >= m_barSets.count())) {
457 // No set, no value.
457 // No set, no value.
458 return 0;
458 return 0;
459 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
459 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
460 // No category, no value.
460 // No category, no value.
461 return 0;
461 return 0;
462 }
462 }
463
463
464 qreal value = m_barSets.at(set)->at(category).y();
464 qreal value = m_barSets.at(set)->at(category).y();
465 qreal sum = categorySum(category);
465 qreal sum = categorySum(category);
466 if ( qFuzzyIsNull(sum) ) {
466 if ( qFuzzyIsNull(sum) ) {
467 return 0;
467 return 0;
468 }
468 }
469
469
470 return value / sum;
470 return value / sum;
471 }
471 }
472
472
473 qreal QBarSeriesPrivate::categorySum(int category)
473 qreal QBarSeriesPrivate::categorySum(int category)
474 {
474 {
475 qreal sum(0);
475 qreal sum(0);
476 int count = m_barSets.count(); // Count sets
476 int count = m_barSets.count(); // Count sets
477 for (int set = 0; set < count; set++) {
477 for (int set = 0; set < count; set++) {
478 if (category < m_barSets.at(set)->count())
478 if (category < m_barSets.at(set)->count())
479 sum += m_barSets.at(set)->at(category).y();
479 sum += m_barSets.at(set)->at(category).y();
480 }
480 }
481 return sum;
481 return sum;
482 }
482 }
483
483
484 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
484 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
485 {
485 {
486 qreal sum(0);
486 qreal sum(0);
487 int count = m_barSets.count(); // Count sets
487 int count = m_barSets.count(); // Count sets
488 for (int set = 0; set < count; set++) {
488 for (int set = 0; set < count; set++) {
489 if (category < m_barSets.at(set)->count())
489 if (category < m_barSets.at(set)->count())
490 sum += qAbs(m_barSets.at(set)->at(category).y());
490 sum += qAbs(m_barSets.at(set)->at(category).y());
491 }
491 }
492 return sum;
492 return sum;
493 }
493 }
494
494
495 qreal QBarSeriesPrivate::maxCategorySum()
495 qreal QBarSeriesPrivate::maxCategorySum()
496 {
496 {
497 qreal max = INT_MIN;
497 qreal max = INT_MIN;
498 int count = categoryCount();
498 int count = categoryCount();
499 for (int i = 0; i < count; i++) {
499 for (int i = 0; i < count; i++) {
500 qreal sum = categorySum(i);
500 qreal sum = categorySum(i);
501 if (sum > max)
501 if (sum > max)
502 max = sum;
502 max = sum;
503 }
503 }
504 return max;
504 return max;
505 }
505 }
506
506
507 qreal QBarSeriesPrivate::minX()
507 qreal QBarSeriesPrivate::minX()
508 {
508 {
509 if (m_barSets.count() <= 0) {
509 if (m_barSets.count() <= 0) {
510 return 0;
510 return 0;
511 }
511 }
512 qreal min = INT_MAX;
512 qreal min = INT_MAX;
513
513
514 for (int i = 0; i < m_barSets.count(); i++) {
514 for (int i = 0; i < m_barSets.count(); i++) {
515 int categoryCount = m_barSets.at(i)->count();
515 int categoryCount = m_barSets.at(i)->count();
516 for (int j = 0; j < categoryCount; j++) {
516 for (int j = 0; j < categoryCount; j++) {
517 qreal temp = m_barSets.at(i)->at(j).x();
517 qreal temp = m_barSets.at(i)->at(j).x();
518 if (temp < min)
518 if (temp < min)
519 min = temp;
519 min = temp;
520 }
520 }
521 }
521 }
522 return min;
522 return min;
523 }
523 }
524
524
525 qreal QBarSeriesPrivate::maxX()
525 qreal QBarSeriesPrivate::maxX()
526 {
526 {
527 if (m_barSets.count() <= 0) {
527 if (m_barSets.count() <= 0) {
528 return 0;
528 return 0;
529 }
529 }
530 qreal max = INT_MIN;
530 qreal max = INT_MIN;
531
531
532 for (int i = 0; i < m_barSets.count(); i++) {
532 for (int i = 0; i < m_barSets.count(); i++) {
533 int categoryCount = m_barSets.at(i)->count();
533 int categoryCount = m_barSets.at(i)->count();
534 for (int j = 0; j < categoryCount; j++) {
534 for (int j = 0; j < categoryCount; j++) {
535 qreal temp = m_barSets.at(i)->at(j).x();
535 qreal temp = m_barSets.at(i)->at(j).x();
536 if (temp > max)
536 if (temp > max)
537 max = temp;
537 max = temp;
538 }
538 }
539 }
539 }
540
540
541 return max;
541 return max;
542 }
542 }
543
543
544
544
545 void QBarSeriesPrivate::scaleDomain(Domain& domain)
545 void QBarSeriesPrivate::scaleDomain(Domain& domain)
546 {
546 {
547 qreal minX(domain.minX());
547 qreal minX(domain.minX());
548 qreal minY(domain.minY());
548 qreal minY(domain.minY());
549 qreal maxX(domain.maxX());
549 qreal maxX(domain.maxX());
550 qreal maxY(domain.maxY());
550 qreal maxY(domain.maxY());
551 int tickXCount(domain.tickXCount());
551 int tickXCount(domain.tickXCount());
552 int tickYCount(domain.tickYCount());
552 int tickYCount(domain.tickYCount());
553
553
554 qreal seriesMinX = this->minX();
554 qreal seriesMinX = this->minX();
555 qreal seriesMaxX = this->maxX();
555 qreal seriesMaxX = this->maxX();
556 qreal y = max();
556 qreal y = max();
557 minX = qMin(minX, seriesMinX - 0.5);
557 minX = qMin(minX, seriesMinX - 0.5);
558 minY = qMin(minY, y);
558 minY = qMin(minY, y);
559 maxX = qMax(maxX, seriesMaxX + 0.5);
559 maxX = qMax(maxX, seriesMaxX + 0.5);
560 maxY = qMax(maxY, y);
560 maxY = qMax(maxY, y);
561 tickXCount = categoryCount()+1;
561 tickXCount = categoryCount()+1;
562
562
563 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
563 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
564 }
564 }
565
565
566 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
566 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
567 {
567 {
568 Q_Q(QBarSeries);
568 Q_Q(QBarSeries);
569
569
570 BarChartItem* bar = new BarChartItem(q,presenter);
570 BarChartItem* bar = new BarChartItem(q,presenter);
571 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
571 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
572 presenter->animator()->addAnimation(bar);
572 presenter->animator()->addAnimation(bar);
573 }
573 }
574 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
574 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
575 return bar;
575 return bar;
576
576
577 }
577 }
578
578
579 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
579 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
580 {
580 {
581 Q_Q(QBarSeries);
581 Q_Q(QBarSeries);
582 QList<LegendMarker*> markers;
582 QList<LegendMarker*> markers;
583 foreach(QBarSet* set, q->barSets()) {
583 foreach(QBarSet* set, q->barSets()) {
584 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
584 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
585 markers << marker;
585 markers << marker;
586 }
586 }
587
587
588 return markers;
588 return markers;
589 }
589 }
590
590
591 bool QBarSeriesPrivate::append(QBarSet *set)
591 bool QBarSeriesPrivate::append(QBarSet *set)
592 {
592 {
593 Q_Q(QBarSeries);
593 Q_Q(QBarSeries);
594 if ((m_barSets.contains(set)) || (set == 0)) {
594 if ((m_barSets.contains(set)) || (set == 0)) {
595 // Fail if set is already in list or set is null.
595 // Fail if set is already in list or set is null.
596 return false;
596 return false;
597 }
597 }
598 m_barSets.append(set);
598 m_barSets.append(set);
599 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
599 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
600 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
600 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
601 emit restructuredBars(); // this notifies barchartitem
601 emit restructuredBars(); // this notifies barchartitem
602 if (m_dataset) {
602 if (m_dataset) {
603 m_dataset->updateSeries(q); // this notifies legend
603 m_dataset->updateSeries(q); // this notifies legend
604 }
604 }
605 return true;
605 return true;
606 }
606 }
607
607
608 bool QBarSeriesPrivate::remove(QBarSet *set)
608 bool QBarSeriesPrivate::remove(QBarSet *set)
609 {
609 {
610 Q_Q(QBarSeries);
610 Q_Q(QBarSeries);
611 if (!m_barSets.contains(set)) {
611 if (!m_barSets.contains(set)) {
612 // Fail if set is not in list
612 // Fail if set is not in list
613 return false;
613 return false;
614 }
614 }
615 m_barSets.removeOne(set);
615 m_barSets.removeOne(set);
616 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
616 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
617 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
617 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
618 emit restructuredBars(); // this notifies barchartitem
618 emit restructuredBars(); // this notifies barchartitem
619 if (m_dataset) {
619 if (m_dataset) {
620 m_dataset->updateSeries(q); // this notifies legend
620 m_dataset->updateSeries(q); // this notifies legend
621 }
621 }
622 return true;
622 return true;
623 }
623 }
624
624
625 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
625 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
626 {
626 {
627 Q_Q(QBarSeries);
627 Q_Q(QBarSeries);
628 foreach (QBarSet* set, sets) {
628 foreach (QBarSet* set, sets) {
629 if ((set == 0) || (m_barSets.contains(set))) {
629 if ((set == 0) || (m_barSets.contains(set))) {
630 // Fail if any of the sets is null or is already appended.
630 // Fail if any of the sets is null or is already appended.
631 return false;
631 return false;
632 }
632 }
633 if (sets.count(set) != 1) {
633 if (sets.count(set) != 1) {
634 // Also fail if same set is more than once in given list.
634 // Also fail if same set is more than once in given list.
635 return false;
635 return false;
636 }
636 }
637 }
637 }
638
638
639 foreach (QBarSet* set, sets) {
639 foreach (QBarSet* set, sets) {
640 m_barSets.append(set);
640 m_barSets.append(set);
641 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
641 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
642 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
642 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
643 }
643 }
644 emit restructuredBars(); // this notifies barchartitem
644 emit restructuredBars(); // this notifies barchartitem
645 if (m_dataset) {
645 if (m_dataset) {
646 m_dataset->updateSeries(q); // this notifies legend
646 m_dataset->updateSeries(q); // this notifies legend
647 }
647 }
648 return true;
648 return true;
649 }
649 }
650
650
651 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
651 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
652 {
652 {
653 Q_Q(QBarSeries);
653 Q_Q(QBarSeries);
654 if (sets.count() == 0) {
654 if (sets.count() == 0) {
655 return false;
655 return false;
656 }
656 }
657 foreach (QBarSet* set, sets) {
657 foreach (QBarSet* set, sets) {
658 if ((set == 0) || (!m_barSets.contains(set))) {
658 if ((set == 0) || (!m_barSets.contains(set))) {
659 // Fail if any of the sets is null or is not in series
659 // Fail if any of the sets is null or is not in series
660 return false;
660 return false;
661 }
661 }
662 if (sets.count(set) != 1) {
662 if (sets.count(set) != 1) {
663 // Also fail if same set is more than once in given list.
663 // Also fail if same set is more than once in given list.
664 return false;
664 return false;
665 }
665 }
666 }
666 }
667
667
668 foreach (QBarSet* set, sets) {
668 foreach (QBarSet* set, sets) {
669 m_barSets.removeOne(set);
669 m_barSets.removeOne(set);
670 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
670 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
671 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
671 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
672 }
672 }
673
673
674 emit restructuredBars(); // this notifies barchartitem
674 emit restructuredBars(); // this notifies barchartitem
675 if (m_dataset) {
675 if (m_dataset) {
676 m_dataset->updateSeries(q); // this notifies legend
676 m_dataset->updateSeries(q); // this notifies legend
677 }
677 }
678 return true;
678 return true;
679 }
679 }
680
680
681 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
681 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
682 {
682 {
683 Q_Q(QBarSeries);
683 Q_Q(QBarSeries);
684 if ((m_barSets.contains(set)) || (set == 0)) {
684 if ((m_barSets.contains(set)) || (set == 0)) {
685 // Fail if set is already in list or set is null.
685 // Fail if set is already in list or set is null.
686 return false;
686 return false;
687 }
687 }
688 m_barSets.insert(index, set);
688 m_barSets.insert(index, set);
689 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
689 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
690 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
690 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
691 emit restructuredBars(); // this notifies barchartitem
691 emit restructuredBars(); // this notifies barchartitem
692 if (m_dataset) {
692 if (m_dataset) {
693 m_dataset->updateSeries(q); // this notifies legend
693 m_dataset->updateSeries(q); // this notifies legend
694 }
694 }
695 return true;
695 return true;
696 }
696 }
697
697
698 #include "moc_qbarseries.cpp"
698 #include "moc_qbarseries.cpp"
699 #include "moc_qbarseries_p.cpp"
699 #include "moc_qbarseries_p.cpp"
700
700
701 QTCOMMERCIALCHART_END_NAMESPACE
701 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,83 +1,83
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class QBarSet;
29 class QBarSet;
30 class QBarSeriesPrivate;
30 class QBarSeriesPrivate;
31
31
32 // Container for series
32 // Container for series
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth NOTIFY barWidthChanged)
36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth NOTIFY barWidthChanged)
37 Q_PROPERTY(int count READ barsetCount NOTIFY barsetCountChanged)
37 Q_PROPERTY(int count READ count NOTIFY countChanged)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
39
39
40 public:
40 public:
41 explicit QBarSeries(QObject *parent = 0);
41 explicit QBarSeries(QObject *parent = 0);
42 virtual ~QBarSeries();
42 virtual ~QBarSeries();
43
43
44 QAbstractSeries::SeriesType type() const;
44 QAbstractSeries::SeriesType type() const;
45
45
46 void setBarWidth(qreal width);
46 void setBarWidth(qreal width);
47 qreal barWidth() const;
47 qreal barWidth() const;
48
48
49 bool append(QBarSet *set);
49 bool append(QBarSet *set);
50 bool remove(QBarSet *set);
50 bool remove(QBarSet *set);
51 bool append(QList<QBarSet* > sets);
51 bool append(QList<QBarSet* > sets);
52 bool insert(int index, QBarSet *set);
52 bool insert(int index, QBarSet *set);
53 int barsetCount() const;
53 int count() const;
54 QList<QBarSet*> barSets() const;
54 QList<QBarSet*> barSets() const;
55 void clear();
55 void clear();
56
56
57 void setLabelsVisible(bool visible = true);
57 void setLabelsVisible(bool visible = true);
58 bool isLabelsVisible() const;
58 bool isLabelsVisible() const;
59
59
60 protected:
60 protected:
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
62
62
63 Q_SIGNALS:
63 Q_SIGNALS:
64 void clicked(QBarSet *barset, int index);
64 void clicked(QBarSet *barset, int index);
65 void hovered(QBarSet* barset, bool status);
65 void hovered(QBarSet* barset, bool status);
66 void barWidthChanged();
66 void barWidthChanged();
67 void barsetCountChanged();
67 void countChanged();
68 void labelsVisibleChanged();
68 void labelsVisibleChanged();
69
69
70 void barsetsAdded(QList<QBarSet*> sets);
70 void barsetsAdded(QList<QBarSet*> sets);
71 void barsetsRemoved(QList<QBarSet*> sets);
71 void barsetsRemoved(QList<QBarSet*> sets);
72
72
73 protected:
73 protected:
74 Q_DECLARE_PRIVATE(QBarSeries)
74 Q_DECLARE_PRIVATE(QBarSeries)
75 friend class BarChartItem;
75 friend class BarChartItem;
76 friend class PercentBarChartItem;
76 friend class PercentBarChartItem;
77 friend class StackedBarChartItem;
77 friend class StackedBarChartItem;
78 friend class GroupedBarChartItem;
78 friend class GroupedBarChartItem;
79 };
79 };
80
80
81 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
82
82
83 #endif // BARSERIES_H
83 #endif // BARSERIES_H
@@ -1,101 +1,101
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "stackedbarchartitem_p.h"
21 #include "stackedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
31 BarChartItem(series, presenter)
31 BarChartItem(series, presenter)
32 {
32 {
33 }
33 }
34
34
35 QVector<QRectF> StackedBarChartItem::calculateLayout()
35 QVector<QRectF> StackedBarChartItem::calculateLayout()
36 {
36 {
37 QVector<QRectF> layout;
37 QVector<QRectF> layout;
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->count();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal barWidth = scaleX * m_series->d_func()->barWidth();
50 qreal barWidth = scaleX * m_series->d_func()->barWidth();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
55 for (int set=0; set < setCount; set++) {
55 for (int set=0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
57
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
59
59
60 qreal barHeight = barSet->at(category).y() * scaleY;
60 qreal barHeight = barSet->at(category).y() * scaleY;
61 Bar* bar = m_bars.at(itemIndex);
61 Bar* bar = m_bars.at(itemIndex);
62 bar->setPen(barSet->pen());
62 bar->setPen(barSet->pen());
63 bar->setBrush(barSet->brush());
63 bar->setBrush(barSet->brush());
64 bar->setVisible(barsVisible);
64 bar->setVisible(barsVisible);
65
65
66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
67 layout.append(rect);
67 layout.append(rect);
68
68
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70
70
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 label->setText(QString::number(barSet->at(category).y()));
72 label->setText(QString::number(barSet->at(category).y()));
73 } else {
73 } else {
74 label->setText(QString(""));
74 label->setText(QString(""));
75 }
75 }
76
76
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 label->setFont(barSet->labelFont());
79 label->setFont(barSet->labelFont());
80 label->setBrush(barSet->labelBrush());
80 label->setBrush(barSet->labelBrush());
81 itemIndex++;
81 itemIndex++;
82 yPos -= barHeight;
82 yPos -= barHeight;
83 }
83 }
84 }
84 }
85
85
86 return layout;
86 return layout;
87 }
87 }
88
88
89 void StackedBarChartItem::applyLayout(const QVector<QRectF> &layout)
89 void StackedBarChartItem::applyLayout(const QVector<QRectF> &layout)
90 {
90 {
91 if (animator()) {
91 if (animator()) {
92 animator()->updateLayout(this, m_layout, layout);
92 animator()->updateLayout(this, m_layout, layout);
93 } else {
93 } else {
94 setLayout(layout);
94 setLayout(layout);
95 update();
95 update();
96 }
96 }
97 }
97 }
98
98
99 #include "moc_stackedbarchartitem_p.cpp"
99 #include "moc_stackedbarchartitem_p.cpp"
100
100
101 QTCOMMERCIALCHART_END_NAMESPACE
101 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,383 +1,383
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "charttheme_p.h"
21 #include "charttheme_p.h"
22 #include "qchart.h"
22 #include "qchart.h"
23 #include "qchart_p.h"
23 #include "qchart_p.h"
24 #include "qchartview.h"
24 #include "qchartview.h"
25 #include "qlegend.h"
25 #include "qlegend.h"
26 #include "qaxis.h"
26 #include "qaxis.h"
27 #include <QTime>
27 #include <QTime>
28
28
29 //series
29 //series
30 #include "qbarset.h"
30 #include "qbarset.h"
31 #include "qbarseries.h"
31 #include "qbarseries.h"
32 #include "qstackedbarseries.h"
32 #include "qstackedbarseries.h"
33 #include "qpercentbarseries.h"
33 #include "qpercentbarseries.h"
34 #include "qlineseries.h"
34 #include "qlineseries.h"
35 #include "qareaseries.h"
35 #include "qareaseries.h"
36 #include "qscatterseries.h"
36 #include "qscatterseries.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieslice.h"
38 #include "qpieslice.h"
39 #include "qpieslice_p.h"
39 #include "qpieslice_p.h"
40 #include "qsplineseries.h"
40 #include "qsplineseries.h"
41
41
42 //items
42 //items
43 #include "chartaxis_p.h"
43 #include "chartaxis_p.h"
44 #include "barchartitem_p.h"
44 #include "barchartitem_p.h"
45 #include "stackedbarchartitem_p.h"
45 #include "stackedbarchartitem_p.h"
46 #include "percentbarchartitem_p.h"
46 #include "percentbarchartitem_p.h"
47 #include "linechartitem_p.h"
47 #include "linechartitem_p.h"
48 #include "areachartitem_p.h"
48 #include "areachartitem_p.h"
49 #include "scatterchartitem_p.h"
49 #include "scatterchartitem_p.h"
50 #include "piechartitem_p.h"
50 #include "piechartitem_p.h"
51 #include "splinechartitem_p.h"
51 #include "splinechartitem_p.h"
52
52
53 //themes
53 //themes
54 #include "chartthemesystem_p.h"
54 #include "chartthemesystem_p.h"
55 #include "chartthemelight_p.h"
55 #include "chartthemelight_p.h"
56 #include "chartthemebluecerulean_p.h"
56 #include "chartthemebluecerulean_p.h"
57 #include "chartthemedark_p.h"
57 #include "chartthemedark_p.h"
58 #include "chartthemebrownsand_p.h"
58 #include "chartthemebrownsand_p.h"
59 #include "chartthemebluencs_p.h"
59 #include "chartthemebluencs_p.h"
60 #include "chartthemehighcontrast_p.h"
60 #include "chartthemehighcontrast_p.h"
61 #include "chartthemeblueicy_p.h"
61 #include "chartthemeblueicy_p.h"
62
62
63 QTCOMMERCIALCHART_BEGIN_NAMESPACE
63 QTCOMMERCIALCHART_BEGIN_NAMESPACE
64
64
65 ChartTheme::ChartTheme(QChart::ChartTheme id) :
65 ChartTheme::ChartTheme(QChart::ChartTheme id) :
66 m_masterFont(QFont("arial", 14)),
66 m_masterFont(QFont("arial", 14)),
67 m_labelFont(QFont("arial", 10)),
67 m_labelFont(QFont("arial", 10)),
68 m_titleBrush(QColor(QRgb(0x000000))),
68 m_titleBrush(QColor(QRgb(0x000000))),
69 m_axisLinePen(QPen(QRgb(0x000000))),
69 m_axisLinePen(QPen(QRgb(0x000000))),
70 m_axisLabelBrush(QColor(QRgb(0x000000))),
70 m_axisLabelBrush(QColor(QRgb(0x000000))),
71 m_backgroundShadesPen(Qt::NoPen),
71 m_backgroundShadesPen(Qt::NoPen),
72 m_backgroundShadesBrush(Qt::NoBrush),
72 m_backgroundShadesBrush(Qt::NoBrush),
73 m_backgroundShades(BackgroundShadesNone),
73 m_backgroundShades(BackgroundShadesNone),
74 m_backgroundDropShadowEnabled(false),
74 m_backgroundDropShadowEnabled(false),
75 m_gridLinePen(QPen(QRgb(0x000000))),
75 m_gridLinePen(QPen(QRgb(0x000000))),
76 m_force(false)
76 m_force(false)
77 {
77 {
78 m_id = id;
78 m_id = id;
79 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
79 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
80 }
80 }
81
81
82
82
83 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
83 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
84 {
84 {
85 switch(theme) {
85 switch(theme) {
86 case QChart::ChartThemeLight:
86 case QChart::ChartThemeLight:
87 return new ChartThemeLight();
87 return new ChartThemeLight();
88 case QChart::ChartThemeBlueCerulean:
88 case QChart::ChartThemeBlueCerulean:
89 return new ChartThemeBlueCerulean();
89 return new ChartThemeBlueCerulean();
90 case QChart::ChartThemeDark:
90 case QChart::ChartThemeDark:
91 return new ChartThemeDark();
91 return new ChartThemeDark();
92 case QChart::ChartThemeBrownSand:
92 case QChart::ChartThemeBrownSand:
93 return new ChartThemeBrownSand();
93 return new ChartThemeBrownSand();
94 case QChart::ChartThemeBlueNcs:
94 case QChart::ChartThemeBlueNcs:
95 return new ChartThemeBlueNcs();
95 return new ChartThemeBlueNcs();
96 case QChart::ChartThemeHighContrast:
96 case QChart::ChartThemeHighContrast:
97 return new ChartThemeHighContrast();
97 return new ChartThemeHighContrast();
98 case QChart::ChartThemeBlueIcy:
98 case QChart::ChartThemeBlueIcy:
99 return new ChartThemeBlueIcy();
99 return new ChartThemeBlueIcy();
100 default:
100 default:
101 return new ChartThemeSystem();
101 return new ChartThemeSystem();
102 }
102 }
103 }
103 }
104
104
105 void ChartTheme::decorate(QChart *chart)
105 void ChartTheme::decorate(QChart *chart)
106 {
106 {
107 QBrush brush;
107 QBrush brush;
108
108
109 if(brush == chart->backgroundBrush() || m_force)
109 if(brush == chart->backgroundBrush() || m_force)
110 chart->setBackgroundBrush(m_chartBackgroundGradient);
110 chart->setBackgroundBrush(m_chartBackgroundGradient);
111 chart->setTitleFont(m_masterFont);
111 chart->setTitleFont(m_masterFont);
112 chart->setTitleBrush(m_titleBrush);
112 chart->setTitleBrush(m_titleBrush);
113 chart->setBackgroundDropShadowEnabled(m_backgroundDropShadowEnabled);
113 chart->setDropShadowEnabled(m_backgroundDropShadowEnabled);
114 }
114 }
115
115
116 void ChartTheme::decorate(QLegend *legend)
116 void ChartTheme::decorate(QLegend *legend)
117 {
117 {
118 QPen pen;
118 QPen pen;
119 QBrush brush;
119 QBrush brush;
120
120
121 if (pen == legend->pen() || m_force)
121 if (pen == legend->pen() || m_force)
122 legend->setPen(m_axisLinePen);
122 legend->setPen(m_axisLinePen);
123
123
124 if (brush == legend->brush() || m_force)
124 if (brush == legend->brush() || m_force)
125 legend->setBrush(m_chartBackgroundGradient);
125 legend->setBrush(m_chartBackgroundGradient);
126 }
126 }
127
127
128 void ChartTheme::decorate(QAreaSeries *series, int index)
128 void ChartTheme::decorate(QAreaSeries *series, int index)
129 {
129 {
130 QPen pen;
130 QPen pen;
131 QBrush brush;
131 QBrush brush;
132
132
133 if (pen == series->pen() || m_force){
133 if (pen == series->pen() || m_force){
134 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
134 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
135 pen.setWidthF(2);
135 pen.setWidthF(2);
136 series->setPen(pen);
136 series->setPen(pen);
137 }
137 }
138
138
139 if (brush == series->brush() || m_force) {
139 if (brush == series->brush() || m_force) {
140 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
140 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
141 series->setBrush(brush);
141 series->setBrush(brush);
142 }
142 }
143 }
143 }
144
144
145
145
146 void ChartTheme::decorate(QLineSeries *series,int index)
146 void ChartTheme::decorate(QLineSeries *series,int index)
147 {
147 {
148 QPen pen;
148 QPen pen;
149 if(pen == series->pen() || m_force ){
149 if(pen == series->pen() || m_force ){
150 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
150 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
151 pen.setWidthF(2);
151 pen.setWidthF(2);
152 series->setPen(pen);
152 series->setPen(pen);
153 }
153 }
154 }
154 }
155
155
156 void ChartTheme::decorate(QBarSeries *series, int index)
156 void ChartTheme::decorate(QBarSeries *series, int index)
157 {
157 {
158 QBrush brush;
158 QBrush brush;
159 QPen pen;
159 QPen pen;
160 QList<QBarSet *> sets = series->barSets();
160 QList<QBarSet *> sets = series->barSets();
161
161
162 qreal takeAtPos = 0.5;
162 qreal takeAtPos = 0.5;
163 qreal step = 0.2;
163 qreal step = 0.2;
164 if (sets.count() > 1 ) {
164 if (sets.count() > 1 ) {
165 step = 1.0 / (qreal) sets.count();
165 step = 1.0 / (qreal) sets.count();
166 if (sets.count() % m_seriesGradients.count())
166 if (sets.count() % m_seriesGradients.count())
167 step *= m_seriesGradients.count();
167 step *= m_seriesGradients.count();
168 else
168 else
169 step *= (m_seriesGradients.count() - 1);
169 step *= (m_seriesGradients.count() - 1);
170 }
170 }
171
171
172 for (int i(0); i < sets.count(); i++) {
172 for (int i(0); i < sets.count(); i++) {
173 int colorIndex = (index + i) % m_seriesGradients.count();
173 int colorIndex = (index + i) % m_seriesGradients.count();
174 if (i > 0 && i % m_seriesGradients.count() == 0) {
174 if (i > 0 && i % m_seriesGradients.count() == 0) {
175 // There is no dedicated base color for each sets, generate more colors
175 // There is no dedicated base color for each sets, generate more colors
176 takeAtPos += step;
176 takeAtPos += step;
177 if (takeAtPos == 1.0)
177 if (takeAtPos == 1.0)
178 takeAtPos += step;
178 takeAtPos += step;
179 takeAtPos -= (int) takeAtPos;
179 takeAtPos -= (int) takeAtPos;
180 }
180 }
181 if (brush == sets.at(i)->brush() || m_force )
181 if (brush == sets.at(i)->brush() || m_force )
182 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
182 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
183
183
184 // Pick label color from the opposite end of the gradient.
184 // Pick label color from the opposite end of the gradient.
185 // 0.3 as a boundary seems to work well.
185 // 0.3 as a boundary seems to work well.
186 if (takeAtPos < 0.3)
186 if (takeAtPos < 0.3)
187 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
187 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
188 else
188 else
189 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
189 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
190
190
191 if (pen == sets.at(i)->pen() || m_force) {
191 if (pen == sets.at(i)->pen() || m_force) {
192 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
192 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
193 sets.at(i)->setPen(c);
193 sets.at(i)->setPen(c);
194 }
194 }
195 }
195 }
196 }
196 }
197
197
198 void ChartTheme::decorate(QScatterSeries *series, int index)
198 void ChartTheme::decorate(QScatterSeries *series, int index)
199 {
199 {
200 QPen pen;
200 QPen pen;
201 QBrush brush;
201 QBrush brush;
202
202
203 if (pen == series->pen() || m_force) {
203 if (pen == series->pen() || m_force) {
204 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
204 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
205 pen.setWidthF(2);
205 pen.setWidthF(2);
206 series->setPen(pen);
206 series->setPen(pen);
207 }
207 }
208
208
209 if (brush == series->brush() || m_force) {
209 if (brush == series->brush() || m_force) {
210 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
210 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
211 series->setBrush(brush);
211 series->setBrush(brush);
212 }
212 }
213 }
213 }
214
214
215 void ChartTheme::decorate(QPieSeries *series, int index)
215 void ChartTheme::decorate(QPieSeries *series, int index)
216 {
216 {
217
217
218 for (int i(0); i < series->slices().count(); i++) {
218 for (int i(0); i < series->slices().count(); i++) {
219
219
220 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
220 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
221
221
222 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
222 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
223 qreal pos = (qreal) (i + 1) / (qreal) series->count();
223 qreal pos = (qreal) (i + 1) / (qreal) series->count();
224 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
224 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
225
225
226 QPieSlice *s = series->slices().at(i);
226 QPieSlice *s = series->slices().at(i);
227 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
227 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
228
228
229 if (d->m_data.m_slicePen.isThemed() || m_force)
229 if (d->m_data.m_slicePen.isThemed() || m_force)
230 d->setPen(penColor, true);
230 d->setPen(penColor, true);
231
231
232 if (d->m_data.m_sliceBrush.isThemed() || m_force)
232 if (d->m_data.m_sliceBrush.isThemed() || m_force)
233 d->setBrush(brushColor, true);
233 d->setBrush(brushColor, true);
234
234
235 if (d->m_data.m_labelBrush.isThemed() || m_force)
235 if (d->m_data.m_labelBrush.isThemed() || m_force)
236 d->setLabelBrush(QBrush(m_titleBrush.color()), true);
236 d->setLabelBrush(QBrush(m_titleBrush.color()), true);
237
237
238 if (d->m_data.m_labelFont.isThemed() || m_force)
238 if (d->m_data.m_labelFont.isThemed() || m_force)
239 d->setLabelFont(m_labelFont, true);
239 d->setLabelFont(m_labelFont, true);
240 }
240 }
241 }
241 }
242
242
243 void ChartTheme::decorate(QSplineSeries *series, int index)
243 void ChartTheme::decorate(QSplineSeries *series, int index)
244 {
244 {
245 QPen pen;
245 QPen pen;
246 if(pen == series->pen() || m_force){
246 if(pen == series->pen() || m_force){
247 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
247 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
248 pen.setWidthF(2);
248 pen.setWidthF(2);
249 series->setPen(pen);
249 series->setPen(pen);
250 }
250 }
251 }
251 }
252
252
253 void ChartTheme::decorate(QAxis *axis,bool axisX)
253 void ChartTheme::decorate(QAxis *axis,bool axisX)
254 {
254 {
255 QPen pen;
255 QPen pen;
256 QBrush brush;
256 QBrush brush;
257 QFont font;
257 QFont font;
258
258
259 if (axis->isAxisVisible()) {
259 if (axis->isAxisVisible()) {
260
260
261 if(brush == axis->labelsBrush() || m_force){
261 if(brush == axis->labelsBrush() || m_force){
262 axis->setLabelsBrush(m_axisLabelBrush);
262 axis->setLabelsBrush(m_axisLabelBrush);
263 }
263 }
264 if(pen == axis->labelsPen() || m_force){
264 if(pen == axis->labelsPen() || m_force){
265 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
265 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
266 }
266 }
267
267
268
268
269 if (axis->shadesVisible() || m_force) {
269 if (axis->shadesVisible() || m_force) {
270
270
271 if(brush == axis->shadesBrush() || m_force){
271 if(brush == axis->shadesBrush() || m_force){
272 axis->setShadesBrush(m_backgroundShadesBrush);
272 axis->setShadesBrush(m_backgroundShadesBrush);
273 }
273 }
274
274
275 if(pen == axis->shadesPen() || m_force){
275 if(pen == axis->shadesPen() || m_force){
276 axis->setShadesPen(m_backgroundShadesPen);
276 axis->setShadesPen(m_backgroundShadesPen);
277 }
277 }
278
278
279 if( m_force && (m_backgroundShades == BackgroundShadesBoth
279 if( m_force && (m_backgroundShades == BackgroundShadesBoth
280 || (m_backgroundShades == BackgroundShadesVertical && axisX)
280 || (m_backgroundShades == BackgroundShadesVertical && axisX)
281 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
281 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
282 axis->setShadesVisible(true);
282 axis->setShadesVisible(true);
283
283
284 }
284 }
285 }
285 }
286
286
287 if(pen == axis->axisPen() || m_force){
287 if(pen == axis->axisPen() || m_force){
288 axis->setAxisPen(m_axisLinePen);
288 axis->setAxisPen(m_axisLinePen);
289 }
289 }
290
290
291 if(pen == axis->gridLinePen() || m_force){
291 if(pen == axis->gridLinePen() || m_force){
292 axis->setGridLinePen(m_gridLinePen);
292 axis->setGridLinePen(m_gridLinePen);
293 }
293 }
294
294
295 if(font == axis->labelsFont() || m_force){
295 if(font == axis->labelsFont() || m_force){
296 axis->setLabelsFont(m_labelFont);
296 axis->setLabelsFont(m_labelFont);
297 }
297 }
298 }
298 }
299 }
299 }
300
300
301 void ChartTheme::generateSeriesGradients()
301 void ChartTheme::generateSeriesGradients()
302 {
302 {
303 // Generate gradients in HSV color space
303 // Generate gradients in HSV color space
304 foreach (const QColor& color, m_seriesColors) {
304 foreach (const QColor& color, m_seriesColors) {
305 QLinearGradient g;
305 QLinearGradient g;
306 qreal h = color.hsvHueF();
306 qreal h = color.hsvHueF();
307 qreal s = color.hsvSaturationF();
307 qreal s = color.hsvSaturationF();
308
308
309 // TODO: tune the algorithm to give nice results with most base colors defined in
309 // TODO: tune the algorithm to give nice results with most base colors defined in
310 // most themes. The rest of the gradients we can define manually in theme specific
310 // most themes. The rest of the gradients we can define manually in theme specific
311 // implementation.
311 // implementation.
312 QColor start = color;
312 QColor start = color;
313 start.setHsvF(h, 0.0, 1.0);
313 start.setHsvF(h, 0.0, 1.0);
314 g.setColorAt(0.0, start);
314 g.setColorAt(0.0, start);
315
315
316 g.setColorAt(0.5, color);
316 g.setColorAt(0.5, color);
317
317
318 QColor end = color;
318 QColor end = color;
319 end.setHsvF(h, s, 0.25);
319 end.setHsvF(h, s, 0.25);
320 g.setColorAt(1.0, end);
320 g.setColorAt(1.0, end);
321
321
322 m_seriesGradients << g;
322 m_seriesGradients << g;
323 }
323 }
324 }
324 }
325
325
326
326
327 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
327 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
328 {
328 {
329 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
329 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
330 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
330 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
331 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
331 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
332 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
332 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
333 QColor c;
333 QColor c;
334 c.setRgbF(r, g, b);
334 c.setRgbF(r, g, b);
335 return c;
335 return c;
336 }
336 }
337
337
338 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
338 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
339 {
339 {
340 Q_ASSERT(pos >= 0 && pos <= 1.0);
340 Q_ASSERT(pos >= 0 && pos <= 1.0);
341
341
342 QGradientStops stops = gradient.stops();
342 QGradientStops stops = gradient.stops();
343 int count = stops.count();
343 int count = stops.count();
344
344
345 // find previous stop relative to position
345 // find previous stop relative to position
346 QGradientStop prev = stops.first();
346 QGradientStop prev = stops.first();
347 for (int i = 0; i < count; i++) {
347 for (int i = 0; i < count; i++) {
348 QGradientStop stop = stops.at(i);
348 QGradientStop stop = stops.at(i);
349 if (pos > stop.first)
349 if (pos > stop.first)
350 prev = stop;
350 prev = stop;
351
351
352 // given position is actually a stop position?
352 // given position is actually a stop position?
353 if (pos == stop.first) {
353 if (pos == stop.first) {
354 //qDebug() << "stop color" << pos;
354 //qDebug() << "stop color" << pos;
355 return stop.second;
355 return stop.second;
356 }
356 }
357 }
357 }
358
358
359 // find next stop relative to position
359 // find next stop relative to position
360 QGradientStop next = stops.last();
360 QGradientStop next = stops.last();
361 for (int i = count - 1; i >= 0; i--) {
361 for (int i = count - 1; i >= 0; i--) {
362 QGradientStop stop = stops.at(i);
362 QGradientStop stop = stops.at(i);
363 if (pos < stop.first)
363 if (pos < stop.first)
364 next = stop;
364 next = stop;
365 }
365 }
366
366
367 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
367 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
368
368
369 qreal range = next.first - prev.first;
369 qreal range = next.first - prev.first;
370 qreal posDelta = pos - prev.first;
370 qreal posDelta = pos - prev.first;
371 qreal relativePos = posDelta / range;
371 qreal relativePos = posDelta / range;
372
372
373 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
373 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
374
374
375 return colorAt(prev.second, next.second, relativePos);
375 return colorAt(prev.second, next.second, relativePos);
376 }
376 }
377
377
378 void ChartTheme::setForced(bool enabled)
378 void ChartTheme::setForced(bool enabled)
379 {
379 {
380 m_force=enabled;
380 m_force=enabled;
381 }
381 }
382
382
383 QTCOMMERCIALCHART_END_NAMESPACE
383 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,173 +1,173
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qabstractseries.h"
21 #include "qabstractseries.h"
22 #include "qabstractseries_p.h"
22 #include "qabstractseries_p.h"
23 #include "chartdataset_p.h"
23 #include "chartdataset_p.h"
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27 /*!
27 /*!
28 \class QAbstractSeries
28 \class QAbstractSeries
29 \brief Base class for all QtCommercial Chart series.
29 \brief Base class for all QtCommercial Chart series.
30 \mainclass
30 \mainclass
31
31
32 Usually you use the series type specific inherited classes instead of the base class.
32 Usually you use the series type specific inherited classes instead of the base class.
33 \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries,
33 \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries,
34 QPercentBarSeries, QPieSeries
34 QPercentBarSeries, QPieSeries
35 */
35 */
36
36
37 /*!
37 /*!
38 \enum QAbstractSeries::SeriesType
38 \enum QAbstractSeries::SeriesType
39
39
40 The type of the series object.
40 The type of the series object.
41
41
42 \value SeriesTypeLine
42 \value SeriesTypeLine
43 \value SeriesTypeArea
43 \value SeriesTypeArea
44 \value SeriesTypeBar
44 \value SeriesTypeBar
45 \value SeriesTypeStackedBar
45 \value SeriesTypeStackedBar
46 \value SeriesTypePercentBar
46 \value SeriesTypePercentBar
47 \value SeriesTypeGroupedBar
47 \value SeriesTypeGroupedBar
48 \value SeriesTypePie
48 \value SeriesTypePie
49 \value SeriesTypeScatter
49 \value SeriesTypeScatter
50 \value SeriesTypeSpline
50 \value SeriesTypeSpline
51 */
51 */
52
52
53 /*!
53 /*!
54 \fn QSeriesType QAbstractSeries::type() const
54 \property QAbstractSeries::type
55 \brief The type of the series.
55 The type of the series.
56 */
56 */
57
57
58 /*!
58 /*!
59 \property QAbstractSeries::name
59 \property QAbstractSeries::name
60 \brief name of the series property
60 \brief name of the series property
61 */
61 */
62
62
63 /*!
63 /*!
64 \fn void QAbstractSeries::nameChanged()
64 \fn void QAbstractSeries::nameChanged()
65
65
66 This signal is emitted when the series name changes.
66 This signal is emitted when the series name changes.
67
67
68 \sa name
68 \sa name
69 */
69 */
70
70
71 /*!
71 /*!
72 \property QAbstractSeries::visible
72 \property QAbstractSeries::visible
73 \brief whether the series is visible or not; true by default.
73 \brief whether the series is visible or not; true by default.
74 */
74 */
75
75
76 /*!
76 /*!
77 \fn void QAbstractSeries::visibleChanged()
77 \fn void QAbstractSeries::visibleChanged()
78 Emitted when the series visibility changes.
78 Emitted when the series visibility changes.
79 */
79 */
80
80
81 /*!
81 /*!
82 \internal
82 \internal
83 \brief Constructs ChartSeries object with \a parent.
83 \brief Constructs ChartSeries object with \a parent.
84 */
84 */
85 QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) :
85 QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) :
86 QObject(parent),
86 QObject(parent),
87 d_ptr(&d)
87 d_ptr(&d)
88 {
88 {
89 }
89 }
90
90
91 /*!
91 /*!
92 \brief Virtual destructor for the chart series.
92 \brief Virtual destructor for the chart series.
93 */
93 */
94 QAbstractSeries::~QAbstractSeries()
94 QAbstractSeries::~QAbstractSeries()
95 {
95 {
96 if(d_ptr->m_dataset) qFatal("Still binded series detected !");
96 if(d_ptr->m_dataset) qFatal("Still binded series detected !");
97 }
97 }
98
98
99 /*!
99 /*!
100 \brief Sets a \a name for the series.
100 \brief Sets a \a name for the series.
101
101
102 The name of a series is shown in the legend for QXYSeries.
102 The name of a series is shown in the legend for QXYSeries.
103 \sa QChart::setTitle()
103 \sa QChart::setTitle()
104 \sa QPieSlice::setLabel()
104 \sa QPieSlice::setLabel()
105 \sa QBarSet::setName()
105 \sa QBarSet::setName()
106 */
106 */
107 void QAbstractSeries::setName(const QString& name)
107 void QAbstractSeries::setName(const QString& name)
108 {
108 {
109 if (name != d_ptr->m_name) {
109 if (name != d_ptr->m_name) {
110 d_ptr->m_name = name;
110 d_ptr->m_name = name;
111 emit nameChanged();
111 emit nameChanged();
112 }
112 }
113 }
113 }
114
114
115 /*!
115 /*!
116 \brief Returns the name of the series.
116 \brief Returns the name of the series.
117 \sa setName()
117 \sa setName()
118 */
118 */
119 QString QAbstractSeries::name() const
119 QString QAbstractSeries::name() const
120 {
120 {
121 return d_ptr->m_name;
121 return d_ptr->m_name;
122 }
122 }
123
123
124 /*!
124 /*!
125 Sets the visibility of series to \a visible
125 Sets the visibility of series to \a visible
126 */
126 */
127 void QAbstractSeries::setVisible(bool visible)
127 void QAbstractSeries::setVisible(bool visible)
128 {
128 {
129 if (visible != d_ptr->m_visible) {
129 if (visible != d_ptr->m_visible) {
130 d_ptr->m_visible = visible;
130 d_ptr->m_visible = visible;
131 emit visibleChanged();
131 emit visibleChanged();
132 }
132 }
133 }
133 }
134
134
135 /*!
135 /*!
136 Returns the visibility of series
136 Returns the visibility of series
137 */
137 */
138 bool QAbstractSeries::isVisible() const
138 bool QAbstractSeries::isVisible() const
139 {
139 {
140 return d_ptr->m_visible;
140 return d_ptr->m_visible;
141 }
141 }
142
142
143 /*!
143 /*!
144 \brief Returns the chart where series belongs to.
144 \brief Returns the chart where series belongs to.
145
145
146 Set automatically when the series is added to the chart
146 Set automatically when the series is added to the chart
147 and unset when the series is removed from the chart.
147 and unset when the series is removed from the chart.
148 */
148 */
149 QChart* QAbstractSeries::chart() const
149 QChart* QAbstractSeries::chart() const
150 {
150 {
151 return d_ptr->m_chart;
151 return d_ptr->m_chart;
152 }
152 }
153
153
154 ///////////////////////////////////////////////////////////////////////////////////////////////////
154 ///////////////////////////////////////////////////////////////////////////////////////////////////
155
155
156 QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q):
156 QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q):
157 q_ptr(q),
157 q_ptr(q),
158 m_chart(0),
158 m_chart(0),
159 m_dataset(0),
159 m_dataset(0),
160 m_visible(true)
160 m_visible(true)
161 {
161 {
162 }
162 }
163
163
164 QAbstractSeriesPrivate::~QAbstractSeriesPrivate()
164 QAbstractSeriesPrivate::~QAbstractSeriesPrivate()
165 {
165 {
166 }
166 }
167
167
168 #include "moc_qabstractseries.cpp"
168 #include "moc_qabstractseries.cpp"
169 #include "moc_qabstractseries_p.cpp"
169 #include "moc_qabstractseries_p.cpp"
170
170
171 QTCOMMERCIALCHART_END_NAMESPACE
171 QTCOMMERCIALCHART_END_NAMESPACE
172
172
173
173
@@ -1,78 +1,79
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QABSTRACTSERIES_H
21 #ifndef QABSTRACTSERIES_H
22 #define QABSTRACTSERIES_H
22 #define QABSTRACTSERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QObject>
25 #include <QObject>
26 #include <QPen>
26 #include <QPen>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 class QAbstractSeriesPrivate;
30 class QAbstractSeriesPrivate;
31 class QChart;
31 class QChart;
32
32
33 class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject
33 class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
36 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
37 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
37 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
38 Q_PROPERTY(SeriesType type READ type)
38 Q_ENUMS(SeriesType)
39 Q_ENUMS(SeriesType)
39
40
40 public:
41 public:
41 enum SeriesType {
42 enum SeriesType {
42 SeriesTypeLine,
43 SeriesTypeLine,
43 SeriesTypeArea,
44 SeriesTypeArea,
44 SeriesTypeBar,
45 SeriesTypeBar,
45 SeriesTypeStackedBar,
46 SeriesTypeStackedBar,
46 SeriesTypePercentBar,
47 SeriesTypePercentBar,
47 SeriesTypeGroupedBar,
48 SeriesTypeGroupedBar,
48 SeriesTypePie,
49 SeriesTypePie,
49 SeriesTypeScatter,
50 SeriesTypeScatter,
50 SeriesTypeSpline
51 SeriesTypeSpline
51 };
52 };
52
53
53 protected:
54 protected:
54 QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent = 0);
55 QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent = 0);
55
56
56 public:
57 public:
57 ~QAbstractSeries();
58 ~QAbstractSeries();
58 virtual SeriesType type() const = 0;
59 virtual SeriesType type() const = 0;
59 void setName(const QString& name);
60 void setName(const QString& name);
60 QString name() const;
61 QString name() const;
61 void setVisible(bool visible = true);
62 void setVisible(bool visible = true);
62 bool isVisible() const;
63 bool isVisible() const;
63 QChart* chart() const;
64 QChart* chart() const;
64
65
65 Q_SIGNALS:
66 Q_SIGNALS:
66 void nameChanged();
67 void nameChanged();
67 void visibleChanged();
68 void visibleChanged();
68
69
69 protected:
70 protected:
70 QScopedPointer<QAbstractSeriesPrivate> d_ptr;
71 QScopedPointer<QAbstractSeriesPrivate> d_ptr;
71 friend class ChartDataSet;
72 friend class ChartDataSet;
72 friend class ChartPresenter;
73 friend class ChartPresenter;
73 friend class QLegendPrivate;
74 friend class QLegendPrivate;
74 };
75 };
75
76
76 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
77
78
78 #endif
79 #endif
@@ -1,479 +1,479
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qchart.h"
21 #include "qchart.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include "legendscroller_p.h"
23 #include "legendscroller_p.h"
24 #include "qlegend_p.h"
24 #include "qlegend_p.h"
25 #include "chartbackground_p.h"
25 #include "chartbackground_p.h"
26 #include "qaxis.h"
26 #include "qaxis.h"
27 #include <QGraphicsScene>
27 #include <QGraphicsScene>
28 #include <QGraphicsSceneResizeEvent>
28 #include <QGraphicsSceneResizeEvent>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 /*!
32 /*!
33 \enum QChart::ChartTheme
33 \enum QChart::ChartTheme
34
34
35 This enum describes the theme used by the chart.
35 This enum describes the theme used by the chart.
36
36
37 \value ChartThemeLight The default theme
37 \value ChartThemeLight The default theme
38 \value ChartThemeBlueCerulean
38 \value ChartThemeBlueCerulean
39 \value ChartThemeDark
39 \value ChartThemeDark
40 \value ChartThemeBrownSand
40 \value ChartThemeBrownSand
41 \value ChartThemeBlueNcs
41 \value ChartThemeBlueNcs
42 \value ChartThemeHighContrast
42 \value ChartThemeHighContrast
43 \value ChartThemeBlueIcy
43 \value ChartThemeBlueIcy
44 */
44 */
45
45
46 /*!
46 /*!
47 \enum QChart::AnimationOption
47 \enum QChart::AnimationOption
48
48
49 For enabling/disabling animations. Defaults to NoAnimation.
49 For enabling/disabling animations. Defaults to NoAnimation.
50
50
51 \value NoAnimation
51 \value NoAnimation
52 \value GridAxisAnimations
52 \value GridAxisAnimations
53 \value SeriesAnimations
53 \value SeriesAnimations
54 \value AllAnimations
54 \value AllAnimations
55 */
55 */
56
56
57 /*!
57 /*!
58 \class QChart
58 \class QChart
59 \brief QtCommercial chart API.
59 \brief QtCommercial chart API.
60
60
61 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
61 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
62 representation of different types of series and other chart related objects like
62 representation of different types of series and other chart related objects like
63 QAxis and QLegend. If you simply want to show a chart in a layout, you can use the
63 QAxis and QLegend. If you simply want to show a chart in a layout, you can use the
64 convenience class QChartView instead of QChart.
64 convenience class QChartView instead of QChart.
65 \sa QChartView
65 \sa QChartView
66 */
66 */
67
67
68 /*!
68 /*!
69 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
69 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
70 */
70 */
71 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
71 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
72 d_ptr(new QChartPrivate())
72 d_ptr(new QChartPrivate())
73 {
73 {
74 d_ptr->m_dataset = new ChartDataSet(this);
74 d_ptr->m_dataset = new ChartDataSet(this);
75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
76 d_ptr->createConnections();
76 d_ptr->createConnections();
77 d_ptr->m_legend = new LegendScroller(this);
77 d_ptr->m_legend = new LegendScroller(this);
78 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
78 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
79 }
79 }
80
80
81 /*!
81 /*!
82 Destroys the object and it's children, like series and axis objects added to it.
82 Destroys the object and it's children, like series and axis objects added to it.
83 */
83 */
84 QChart::~QChart()
84 QChart::~QChart()
85 {
85 {
86 //delete first presenter , since this is a root of all the graphical items
86 //delete first presenter , since this is a root of all the graphical items
87 delete d_ptr->m_presenter;
87 delete d_ptr->m_presenter;
88 d_ptr->m_presenter=0;
88 d_ptr->m_presenter=0;
89 }
89 }
90
90
91 /*!
91 /*!
92 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
92 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
93 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
93 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
94 the y axis).
94 the y axis).
95
95
96 \sa removeSeries(), removeAllSeries()
96 \sa removeSeries(), removeAllSeries()
97 */
97 */
98 void QChart::addSeries(QAbstractSeries *series, QAxis *axisY)
98 void QChart::addSeries(QAbstractSeries *series, QAxis *axisY)
99 {
99 {
100 Q_ASSERT(series);
100 Q_ASSERT(series);
101 d_ptr->m_dataset->addSeries(series, axisY);
101 d_ptr->m_dataset->addSeries(series, axisY);
102 }
102 }
103
103
104 /*!
104 /*!
105 Removes the \a series specified in a perameter from the QChartView.
105 Removes the \a series specified in a perameter from the QChartView.
106 It releses its ownership of the specified QChartSeries object.
106 It releses its ownership of the specified QChartSeries object.
107 It does not delete the pointed QChartSeries data object
107 It does not delete the pointed QChartSeries data object
108 \sa addSeries(), removeAllSeries()
108 \sa addSeries(), removeAllSeries()
109 */
109 */
110 void QChart::removeSeries(QAbstractSeries *series)
110 void QChart::removeSeries(QAbstractSeries *series)
111 {
111 {
112 Q_ASSERT(series);
112 Q_ASSERT(series);
113 d_ptr->m_dataset->removeSeries(series);
113 d_ptr->m_dataset->removeSeries(series);
114 }
114 }
115
115
116 /*!
116 /*!
117 Removes all the QChartSeries that have been added to the QChartView
117 Removes all the QChartSeries that have been added to the QChartView
118 It also deletes the pointed QChartSeries data objects
118 It also deletes the pointed QChartSeries data objects
119 \sa addSeries(), removeSeries()
119 \sa addSeries(), removeSeries()
120 */
120 */
121 void QChart::removeAllSeries()
121 void QChart::removeAllSeries()
122 {
122 {
123 d_ptr->m_dataset->removeAllSeries();
123 d_ptr->m_dataset->removeAllSeries();
124 }
124 }
125
125
126 /*!
126 /*!
127 Sets the \a brush that is used for painting the background of the chart area.
127 Sets the \a brush that is used for painting the background of the chart area.
128 */
128 */
129 void QChart::setBackgroundBrush(const QBrush& brush)
129 void QChart::setBackgroundBrush(const QBrush& brush)
130 {
130 {
131 //TODO: refactor me
131 //TODO: refactor me
132 d_ptr->m_presenter->createChartBackgroundItem();
132 d_ptr->m_presenter->createChartBackgroundItem();
133 d_ptr->m_presenter->m_backgroundItem->setBrush(brush);
133 d_ptr->m_presenter->m_backgroundItem->setBrush(brush);
134 d_ptr->m_presenter->m_backgroundItem->update();
134 d_ptr->m_presenter->m_backgroundItem->update();
135 }
135 }
136
136
137 /*!
137 /*!
138 Gets the brush that is used for painting the background of the chart area.
138 Gets the brush that is used for painting the background of the chart area.
139 */
139 */
140 QBrush QChart::backgroundBrush() const
140 QBrush QChart::backgroundBrush() const
141 {
141 {
142 //TODO: refactor me
142 //TODO: refactor me
143 if (!d_ptr->m_presenter->m_backgroundItem) return QBrush();
143 if (!d_ptr->m_presenter->m_backgroundItem) return QBrush();
144 return (d_ptr->m_presenter->m_backgroundItem)->brush();
144 return (d_ptr->m_presenter->m_backgroundItem)->brush();
145 }
145 }
146
146
147 /*!
147 /*!
148 Sets the \a pen that is used for painting the background of the chart area.
148 Sets the \a pen that is used for painting the background of the chart area.
149 */
149 */
150 void QChart::setBackgroundPen(const QPen& pen)
150 void QChart::setBackgroundPen(const QPen& pen)
151 {
151 {
152 //TODO: refactor me
152 //TODO: refactor me
153 d_ptr->m_presenter->createChartBackgroundItem();
153 d_ptr->m_presenter->createChartBackgroundItem();
154 d_ptr->m_presenter->m_backgroundItem->setPen(pen);
154 d_ptr->m_presenter->m_backgroundItem->setPen(pen);
155 d_ptr->m_presenter->m_backgroundItem->update();
155 d_ptr->m_presenter->m_backgroundItem->update();
156 }
156 }
157
157
158 /*!
158 /*!
159 Gets the pen that is used for painting the background of the chart area.
159 Gets the pen that is used for painting the background of the chart area.
160 */
160 */
161 QPen QChart::backgroundPen() const
161 QPen QChart::backgroundPen() const
162 {
162 {
163 //TODO: refactor me
163 //TODO: refactor me
164 if (!d_ptr->m_presenter->m_backgroundItem) return QPen();
164 if (!d_ptr->m_presenter->m_backgroundItem) return QPen();
165 return d_ptr->m_presenter->m_backgroundItem->pen();
165 return d_ptr->m_presenter->m_backgroundItem->pen();
166 }
166 }
167
167
168 /*!
168 /*!
169 Sets the chart \a title. The description text that is drawn above the chart.
169 Sets the chart \a title. The description text that is drawn above the chart.
170 */
170 */
171 void QChart::setTitle(const QString& title)
171 void QChart::setTitle(const QString& title)
172 {
172 {
173 //TODO: refactor me
173 //TODO: refactor me
174 d_ptr->m_presenter->createChartTitleItem();
174 d_ptr->m_presenter->createChartTitleItem();
175 d_ptr->m_presenter->m_titleItem->setText(title);
175 d_ptr->m_presenter->m_titleItem->setText(title);
176 d_ptr->m_presenter->updateLayout();
176 d_ptr->m_presenter->updateLayout();
177 }
177 }
178
178
179 /*!
179 /*!
180 Returns the chart title. The description text that is drawn above the chart.
180 Returns the chart title. The description text that is drawn above the chart.
181 */
181 */
182 QString QChart::title() const
182 QString QChart::title() const
183 {
183 {
184 //TODO: refactor me
184 //TODO: refactor me
185 if (d_ptr->m_presenter->m_titleItem)
185 if (d_ptr->m_presenter->m_titleItem)
186 return d_ptr->m_presenter->m_titleItem->text();
186 return d_ptr->m_presenter->m_titleItem->text();
187 else
187 else
188 return QString();
188 return QString();
189 }
189 }
190
190
191 /*!
191 /*!
192 Sets the \a font that is used for drawing the chart description text that is rendered above the chart.
192 Sets the \a font that is used for drawing the chart description text that is rendered above the chart.
193 */
193 */
194 void QChart::setTitleFont(const QFont& font)
194 void QChart::setTitleFont(const QFont& font)
195 {
195 {
196 //TODO: refactor me
196 //TODO: refactor me
197 d_ptr->m_presenter->createChartTitleItem();
197 d_ptr->m_presenter->createChartTitleItem();
198 d_ptr->m_presenter->m_titleItem->setFont(font);
198 d_ptr->m_presenter->m_titleItem->setFont(font);
199 d_ptr->m_presenter->updateLayout();
199 d_ptr->m_presenter->updateLayout();
200 }
200 }
201
201
202 /*!
202 /*!
203 Gets the font that is used for drawing the chart description text that is rendered above the chart.
203 Gets the font that is used for drawing the chart description text that is rendered above the chart.
204 */
204 */
205 QFont QChart::titleFont() const
205 QFont QChart::titleFont() const
206 {
206 {
207 if (d_ptr->m_presenter->m_titleItem)
207 if (d_ptr->m_presenter->m_titleItem)
208 return d_ptr->m_presenter->m_titleItem->font();
208 return d_ptr->m_presenter->m_titleItem->font();
209 else
209 else
210 return QFont();
210 return QFont();
211 }
211 }
212
212
213 /*!
213 /*!
214 Sets the \a brush used for rendering the title text.
214 Sets the \a brush used for rendering the title text.
215 */
215 */
216 void QChart::setTitleBrush(const QBrush &brush)
216 void QChart::setTitleBrush(const QBrush &brush)
217 {
217 {
218 //TODO: refactor me
218 //TODO: refactor me
219 d_ptr->m_presenter->createChartTitleItem();
219 d_ptr->m_presenter->createChartTitleItem();
220 d_ptr->m_presenter->m_titleItem->setBrush(brush);
220 d_ptr->m_presenter->m_titleItem->setBrush(brush);
221 d_ptr->m_presenter->updateLayout();
221 d_ptr->m_presenter->updateLayout();
222 }
222 }
223
223
224 /*!
224 /*!
225 Returns the brush used for rendering the title text.
225 Returns the brush used for rendering the title text.
226 */
226 */
227 QBrush QChart::titleBrush() const
227 QBrush QChart::titleBrush() const
228 {
228 {
229 //TODO: refactor me
229 //TODO: refactor me
230 if (!d_ptr->m_presenter->m_titleItem) return QBrush();
230 if (!d_ptr->m_presenter->m_titleItem) return QBrush();
231 return d_ptr->m_presenter->m_titleItem->brush();
231 return d_ptr->m_presenter->m_titleItem->brush();
232 }
232 }
233
233
234 /*!
234 /*!
235 Sets the \a theme used by the chart for rendering the graphical representation of the data.
235 Sets the \a theme used by the chart for rendering the graphical representation of the data.
236
236
237 Note: changing the theme will overwrite all customizations (pen, brush, font, ect.) done to the series.
237 Note: changing the theme will overwrite all customizations (pen, brush, font, ect.) done to the series.
238 \sa theme()
238 \sa theme()
239 */
239 */
240 void QChart::setTheme(QChart::ChartTheme theme)
240 void QChart::setTheme(QChart::ChartTheme theme)
241 {
241 {
242 d_ptr->m_presenter->setTheme(theme);
242 d_ptr->m_presenter->setTheme(theme);
243 }
243 }
244
244
245 /*!
245 /*!
246 Returns the theme enum used by the chart.
246 Returns the theme enum used by the chart.
247 \sa ChartTheme, setTheme()
247 \sa ChartTheme, setTheme()
248 */
248 */
249 QChart::ChartTheme QChart::theme() const
249 QChart::ChartTheme QChart::theme() const
250 {
250 {
251 return d_ptr->m_presenter->theme();
251 return d_ptr->m_presenter->theme();
252 }
252 }
253
253
254 /*!
254 /*!
255 Zooms in the view by a factor of 2
255 Zooms in the view by a factor of 2
256 */
256 */
257 void QChart::zoomIn()
257 void QChart::zoomIn()
258 {
258 {
259 d_ptr->m_presenter->zoomIn(2.0);
259 d_ptr->m_presenter->zoomIn(2.0);
260 }
260 }
261
261
262 /*!
262 /*!
263 Zooms in the view to a maximum level at which \a rect is still fully visible.
263 Zooms in the view to a maximum level at which \a rect is still fully visible.
264 */
264 */
265 void QChart::zoomIn(const QRectF& rect)
265 void QChart::zoomIn(const QRectF& rect)
266 {
266 {
267 if (!rect.isValid()) return;
267 if (!rect.isValid()) return;
268 d_ptr->m_presenter->zoomIn(rect);
268 d_ptr->m_presenter->zoomIn(rect);
269 }
269 }
270
270
271 /*!
271 /*!
272 Restores the view zoom level to the previous one.
272 Restores the view zoom level to the previous one.
273 */
273 */
274 void QChart::zoomOut()
274 void QChart::zoomOut()
275 {
275 {
276 d_ptr->m_presenter->zoomOut(2.0);
276 d_ptr->m_presenter->zoomOut(2.0);
277 }
277 }
278
278
279 /*!
279 /*!
280 Zooms in the view by a \a factor.
280 Zooms in the view by a \a factor.
281
281
282 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
282 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
283 */
283 */
284 void QChart::zoom(qreal factor)
284 void QChart::zoom(qreal factor)
285 {
285 {
286 if (qFuzzyIsNull(factor))
286 if (qFuzzyIsNull(factor))
287 return;
287 return;
288
288
289 if (qFuzzyCompare(factor, 1.0))
289 if (qFuzzyCompare(factor, 1.0))
290 return;
290 return;
291
291
292 if (factor < 0)
292 if (factor < 0)
293 return;
293 return;
294
294
295 if (factor > 1.0)
295 if (factor > 1.0)
296 d_ptr->m_presenter->zoomIn(factor);
296 d_ptr->m_presenter->zoomIn(factor);
297 else
297 else
298 d_ptr->m_presenter->zoomOut(1.0 / factor);
298 d_ptr->m_presenter->zoomOut(1.0 / factor);
299 }
299 }
300
300
301 /*!
301 /*!
302 Returns the pointer to the x axis object of the chart
302 Returns the pointer to the x axis object of the chart
303 */
303 */
304 QAxis* QChart::axisX() const
304 QAxis* QChart::axisX() const
305 {
305 {
306 return d_ptr->m_dataset->axisX();
306 return d_ptr->m_dataset->axisX();
307 }
307 }
308
308
309 /*!
309 /*!
310 Returns the pointer to the y axis object of the \a series
310 Returns the pointer to the y axis object of the \a series
311 If no \a series is provided then default Y axis of the chart is returned.
311 If no \a series is provided then default Y axis of the chart is returned.
312 */
312 */
313 QAxis* QChart::axisY(QAbstractSeries *series) const
313 QAxis* QChart::axisY(QAbstractSeries *series) const
314 {
314 {
315 return d_ptr->m_dataset->axisY(series);
315 return d_ptr->m_dataset->axisY(series);
316 }
316 }
317
317
318 /*!
318 /*!
319 Returns the legend object of the chart. Ownership stays in chart.
319 Returns the legend object of the chart. Ownership stays in chart.
320 */
320 */
321 QLegend* QChart::legend() const
321 QLegend* QChart::legend() const
322 {
322 {
323 return d_ptr->m_legend;
323 return d_ptr->m_legend;
324 }
324 }
325
325
326 /*!
326 /*!
327 Returns the rect that contains information about margins (distance between chart widget edge and axes).
327 Returns the rect that contains information about margins (distance between chart widget edge and axes).
328 Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
328 Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
329 */
329 */
330 QRectF QChart::margins() const
330 QRectF QChart::margins() const
331 {
331 {
332 return d_ptr->m_presenter->margins();
332 return d_ptr->m_presenter->margins();
333 }
333 }
334
334
335
335
336 /*!
336 /*!
337 Resizes and updates the chart area using the \a event data
337 Resizes and updates the chart area using the \a event data
338 */
338 */
339 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
339 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
340 {
340 {
341 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
341 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
342 QGraphicsWidget::resizeEvent(event);
342 QGraphicsWidget::resizeEvent(event);
343 d_ptr->m_presenter->setGeometry(d_ptr->m_rect);
343 d_ptr->m_presenter->setGeometry(d_ptr->m_rect);
344 }
344 }
345
345
346 /*!
346 /*!
347 Sets animation \a options for the chart
347 Sets animation \a options for the chart
348 */
348 */
349 void QChart::setAnimationOptions(AnimationOptions options)
349 void QChart::setAnimationOptions(AnimationOptions options)
350 {
350 {
351 d_ptr->m_presenter->setAnimationOptions(options);
351 d_ptr->m_presenter->setAnimationOptions(options);
352 }
352 }
353
353
354 /*!
354 /*!
355 Returns animation options for the chart
355 Returns animation options for the chart
356 */
356 */
357 QChart::AnimationOptions QChart::animationOptions() const
357 QChart::AnimationOptions QChart::animationOptions() const
358 {
358 {
359 return d_ptr->m_presenter->animationOptions();
359 return d_ptr->m_presenter->animationOptions();
360 }
360 }
361
361
362 /*!
362 /*!
363 Scrolls the visible area of the chart to the left by the distance between two x axis ticks
363 Scrolls the visible area of the chart to the left by the distance between two x axis ticks
364 */
364 */
365 void QChart::scrollLeft()
365 void QChart::scrollLeft()
366 {
366 {
367 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
367 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
368 }
368 }
369
369
370 /*!
370 /*!
371 Scrolls the visible area of the chart to the right by the distance between two x axis ticks
371 Scrolls the visible area of the chart to the right by the distance between two x axis ticks
372 */
372 */
373 void QChart::scrollRight()
373 void QChart::scrollRight()
374 {
374 {
375 d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
375 d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
376 }
376 }
377
377
378 /*!
378 /*!
379 Scrolls the visible area of the chart up by the distance between two y axis ticks
379 Scrolls the visible area of the chart up by the distance between two y axis ticks
380 */
380 */
381 void QChart::scrollUp()
381 void QChart::scrollUp()
382 {
382 {
383 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
383 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
384 }
384 }
385
385
386 /*!
386 /*!
387 Scrolls the visible area of the chart down by the distance between two y axis ticks
387 Scrolls the visible area of the chart down by the distance between two y axis ticks
388 */
388 */
389 void QChart::scrollDown()
389 void QChart::scrollDown()
390 {
390 {
391 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
391 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
392 }
392 }
393
393
394 /*!
394 /*!
395 Scrolls the visible area of the chart by the distance defined in the \a delta.
395 Scrolls the visible area of the chart by the distance defined in the \a delta.
396 */
396 */
397 void QChart::scroll(const QPointF &delta)
397 void QChart::scroll(const QPointF &delta)
398 {
398 {
399 d_ptr->m_presenter->scroll(-delta.x(), delta.y());
399 d_ptr->m_presenter->scroll(-delta.x(), delta.y());
400 }
400 }
401
401
402 /*!
402 /*!
403 Sets the chart background visibility state to \a visible
403 Sets the chart background visibility state to \a visible
404 */
404 */
405 void QChart::setBackgroundVisible(bool visible)
405 void QChart::setBackgroundVisible(bool visible)
406 {
406 {
407 //TODO: refactor me
407 //TODO: refactor me
408 d_ptr->m_presenter->createChartBackgroundItem();
408 d_ptr->m_presenter->createChartBackgroundItem();
409 d_ptr->m_presenter->m_backgroundItem->setVisible(visible);
409 d_ptr->m_presenter->m_backgroundItem->setVisible(visible);
410 }
410 }
411
411
412 /*!
412 /*!
413 Returns the chart's background visibility state
413 Returns the chart's background visibility state
414 */
414 */
415 bool QChart::isBackgroundVisible() const
415 bool QChart::isBackgroundVisible() const
416 {
416 {
417 //TODO: refactor me
417 //TODO: refactor me
418 if (!d_ptr->m_presenter->m_backgroundItem)
418 if (!d_ptr->m_presenter->m_backgroundItem)
419 return false;
419 return false;
420
420
421 return d_ptr->m_presenter->m_backgroundItem->isVisible();
421 return d_ptr->m_presenter->m_backgroundItem->isVisible();
422 }
422 }
423
423
424 /*!
424 /*!
425 Sets the background drop shadow effect state to \a enabled.
425 Sets the background drop shadow effect state to \a enabled.
426 */
426 */
427 void QChart::setBackgroundDropShadowEnabled(bool enabled)
427 void QChart::setDropShadowEnabled(bool enabled)
428 {
428 {
429 d_ptr->m_presenter->createChartBackgroundItem();
429 d_ptr->m_presenter->createChartBackgroundItem();
430 d_ptr->m_presenter->m_backgroundItem->setDropShadowEnabled(enabled);
430 d_ptr->m_presenter->m_backgroundItem->setDropShadowEnabled(enabled);
431 }
431 }
432
432
433 /*!
433 /*!
434 Returns true if the drop shadow effect is enabled for the chart background.
434 Returns true if the drop shadow effect is enabled for the chart background.
435 */
435 */
436 bool QChart::isBackgroundDropShadowEnabled() const
436 bool QChart::isDropShadowEnabled() const
437 {
437 {
438 if (!d_ptr->m_presenter->m_backgroundItem)
438 if (!d_ptr->m_presenter->m_backgroundItem)
439 return false;
439 return false;
440
440
441 return d_ptr->m_presenter->m_backgroundItem->isDropShadowEnabled();
441 return d_ptr->m_presenter->m_backgroundItem->isDropShadowEnabled();
442 }
442 }
443
443
444 /*!
444 /*!
445 Returns all the series that are added to the chart.
445 Returns all the series that are added to the chart.
446
446
447 \sa addSeries(), removeSeries(), removeAllSeries()
447 \sa addSeries(), removeSeries(), removeAllSeries()
448 */
448 */
449 QList<QAbstractSeries*> QChart::series() const
449 QList<QAbstractSeries*> QChart::series() const
450 {
450 {
451 return d_ptr->m_dataset->series();
451 return d_ptr->m_dataset->series();
452 }
452 }
453
453
454 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
454 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
455
455
456 QChartPrivate::QChartPrivate():
456 QChartPrivate::QChartPrivate():
457 m_legend(0),
457 m_legend(0),
458 m_dataset(0),
458 m_dataset(0),
459 m_presenter(0)
459 m_presenter(0)
460 {
460 {
461
461
462 }
462 }
463
463
464 QChartPrivate::~QChartPrivate()
464 QChartPrivate::~QChartPrivate()
465 {
465 {
466
466
467 }
467 }
468
468
469 void QChartPrivate::createConnections()
469 void QChartPrivate::createConnections()
470 {
470 {
471 QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
471 QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
472 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries*)));
472 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries*)));
473 QObject::connect(m_dataset,SIGNAL(axisAdded(QAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAxis*,Domain*)));
473 QObject::connect(m_dataset,SIGNAL(axisAdded(QAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAxis*,Domain*)));
474 QObject::connect(m_dataset,SIGNAL(axisRemoved(QAxis*)),m_presenter,SLOT(handleAxisRemoved(QAxis*)));
474 QObject::connect(m_dataset,SIGNAL(axisRemoved(QAxis*)),m_presenter,SLOT(handleAxisRemoved(QAxis*)));
475 }
475 }
476
476
477 #include "moc_qchart.cpp"
477 #include "moc_qchart.cpp"
478
478
479 QTCOMMERCIALCHART_END_NAMESPACE
479 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,124 +1,124
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QCHART_H
21 #ifndef QCHART_H
22 #define QCHART_H
22 #define QCHART_H
23
23
24 #include <QAbstractSeries>
24 #include <QAbstractSeries>
25 #include <QLegend>
25 #include <QLegend>
26 #include <QGraphicsWidget>
26 #include <QGraphicsWidget>
27
27
28 class QGraphicsSceneResizeEvent;
28 class QGraphicsSceneResizeEvent;
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class QAbstractSeries;
32 class QAbstractSeries;
33 class QAxis;
33 class QAxis;
34 class QLegend;
34 class QLegend;
35 struct QChartPrivate;
35 struct QChartPrivate;
36
36
37 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
37 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
38 {
38 {
39 Q_OBJECT
39 Q_OBJECT
40 Q_ENUMS(ChartTheme)
40 Q_ENUMS(ChartTheme)
41 Q_ENUMS(AnimationOption)
41 Q_ENUMS(AnimationOption)
42
42
43 public:
43 public:
44 enum ChartTheme {
44 enum ChartTheme {
45 ChartThemeLight = 0,
45 ChartThemeLight = 0,
46 ChartThemeBlueCerulean,
46 ChartThemeBlueCerulean,
47 ChartThemeDark,
47 ChartThemeDark,
48 ChartThemeBrownSand,
48 ChartThemeBrownSand,
49 ChartThemeBlueNcs,
49 ChartThemeBlueNcs,
50 ChartThemeHighContrast,
50 ChartThemeHighContrast,
51 ChartThemeBlueIcy
51 ChartThemeBlueIcy
52 };
52 };
53
53
54 enum AnimationOption {
54 enum AnimationOption {
55 NoAnimation = 0x0,
55 NoAnimation = 0x0,
56 GridAxisAnimations = 0x1,
56 GridAxisAnimations = 0x1,
57 SeriesAnimations =0x2,
57 SeriesAnimations =0x2,
58 AllAnimations = 0x3
58 AllAnimations = 0x3
59 };
59 };
60
60
61 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
61 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
62
62
63 public:
63 public:
64 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
64 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
65 ~QChart();
65 ~QChart();
66
66
67 void addSeries(QAbstractSeries *series, QAxis *axisY = 0);
67 void addSeries(QAbstractSeries *series, QAxis *axisY = 0);
68 void removeSeries(QAbstractSeries *series);
68 void removeSeries(QAbstractSeries *series);
69 void removeAllSeries();
69 void removeAllSeries();
70 QList<QAbstractSeries*> series() const;
70 QList<QAbstractSeries*> series() const;
71
71
72 void setTheme(QChart::ChartTheme theme);
72 void setTheme(QChart::ChartTheme theme);
73 QChart::ChartTheme theme() const;
73 QChart::ChartTheme theme() const;
74
74
75 void setTitle(const QString& title);
75 void setTitle(const QString& title);
76 QString title() const;
76 QString title() const;
77 void setTitleFont(const QFont& font);
77 void setTitleFont(const QFont& font);
78 QFont titleFont() const;
78 QFont titleFont() const;
79 void setTitleBrush(const QBrush &brush);
79 void setTitleBrush(const QBrush &brush);
80 QBrush titleBrush() const;
80 QBrush titleBrush() const;
81
81
82 void setBackgroundBrush(const QBrush &brush);
82 void setBackgroundBrush(const QBrush &brush);
83 QBrush backgroundBrush() const;
83 QBrush backgroundBrush() const;
84 void setBackgroundPen(const QPen &pen);
84 void setBackgroundPen(const QPen &pen);
85 QPen backgroundPen() const;
85 QPen backgroundPen() const;
86 void setBackgroundVisible(bool visible = true);
86 void setBackgroundVisible(bool visible = true);
87 bool isBackgroundVisible() const;
87 bool isBackgroundVisible() const;
88 void setBackgroundDropShadowEnabled(bool enabled = true);
89 bool isBackgroundDropShadowEnabled() const;
90
88
89 void setDropShadowEnabled(bool enabled = true);
90 bool isDropShadowEnabled() const;
91 void setAnimationOptions(AnimationOptions options);
91 void setAnimationOptions(AnimationOptions options);
92 AnimationOptions animationOptions() const;
92 AnimationOptions animationOptions() const;
93
93
94 void zoomIn();
94 void zoomIn();
95 void zoomIn(const QRectF &rect);
95 void zoomIn(const QRectF &rect);
96 void zoomOut();
96 void zoomOut();
97 void zoom(qreal factor);
97 void zoom(qreal factor);
98 void scrollLeft();
98 void scrollLeft();
99 void scrollRight();
99 void scrollRight();
100 void scrollUp();
100 void scrollUp();
101 void scrollDown();
101 void scrollDown();
102 void scroll(const QPointF &delta);
102 void scroll(const QPointF &delta);
103
103
104 QAxis* axisX() const;
104 QAxis* axisX() const;
105 QAxis* axisY(QAbstractSeries* series = 0) const;
105 QAxis* axisY(QAbstractSeries* series = 0) const;
106
106
107 QLegend* legend() const;
107 QLegend* legend() const;
108 QRectF margins() const;
108 QRectF margins() const;
109
109
110 protected:
110 protected:
111 void resizeEvent(QGraphicsSceneResizeEvent *event);
111 void resizeEvent(QGraphicsSceneResizeEvent *event);
112
112
113 protected:
113 protected:
114 QScopedPointer<QChartPrivate> d_ptr;
114 QScopedPointer<QChartPrivate> d_ptr;
115 friend class QLegend;
115 friend class QLegend;
116 friend class ChartPresenter;
116 friend class ChartPresenter;
117 Q_DISABLE_COPY(QChart)
117 Q_DISABLE_COPY(QChart)
118 };
118 };
119
119
120 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
121
121
122 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
122 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
123
123
124 #endif
124 #endif
General Comments 0
You need to be logged in to leave comments. Login now