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