@@ -0,0 +1,94 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | import QtQuick 1.0 | |
|
22 | import QtCommercial.Chart 1.0 | |
|
23 | ||
|
24 | ||
|
25 | Flow { | |
|
26 | id: flow | |
|
27 | spacing: 5 | |
|
28 | flow: Flow.TopToBottom | |
|
29 | property variant series // TODO: rename to chart | |
|
30 | ||
|
31 | Connections { | |
|
32 | target: series | |
|
33 | onVisibleChanged: console.log("chart.onVisibleChanged: " + series.visible); | |
|
34 | onThemeChanged: console.log("chart.onThemeChanged: " + series.theme); | |
|
35 | onLegendChanged: console.log("chart.onLegendChanged: " + series.legend); | |
|
36 | onAnimationOptionsChanged: console.log("chart.onAnimationOptionsChanged: " + series.animationOptions); | |
|
37 | onTitleColorChanged: console.log("chart.onTitleColorChanged: " + series.titleColor); | |
|
38 | onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor); | |
|
39 | } | |
|
40 | Connections { | |
|
41 | target: series.legend | |
|
42 | onAlignmentChanged: console.log("legend.onAlignmentChanged: " + series.legend.alignment); | |
|
43 | onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible); | |
|
44 | } | |
|
45 | ||
|
46 | Button { | |
|
47 | text: "visible" | |
|
48 | onClicked: series.visible = !series.visible; | |
|
49 | } | |
|
50 | Button { | |
|
51 | text: "theme +" | |
|
52 | onClicked: series.theme++; | |
|
53 | } | |
|
54 | Button { | |
|
55 | text: "theme -" | |
|
56 | onClicked: series.theme--; | |
|
57 | } | |
|
58 | Button { | |
|
59 | text: "legend top" | |
|
60 | onClicked: series.legend.alignment ^= Qt.AlignTop; | |
|
61 | } | |
|
62 | Button { | |
|
63 | text: "legend bottom" | |
|
64 | onClicked: series.legend.alignment ^= Qt.AlignBottom; | |
|
65 | } | |
|
66 | Button { | |
|
67 | text: "legend left" | |
|
68 | onClicked: series.legend.alignment ^= Qt.AlignLeft; | |
|
69 | } | |
|
70 | Button { | |
|
71 | text: "legend right" | |
|
72 | onClicked: series.legend.alignment ^= Qt.AlignRight; | |
|
73 | } | |
|
74 | Button { | |
|
75 | text: "legend visible" | |
|
76 | onClicked: series.legend.visible = !series.legend.visible; | |
|
77 | } | |
|
78 | Button { | |
|
79 | text: "animation opt +" | |
|
80 | onClicked: series.animationOptions++; | |
|
81 | } | |
|
82 | Button { | |
|
83 | text: "animation opt -" | |
|
84 | onClicked: series.animationOptions--; | |
|
85 | } | |
|
86 | Button { | |
|
87 | text: "title color" | |
|
88 | onClicked: series.titleColor = main.nextColor(); | |
|
89 | } | |
|
90 | Button { | |
|
91 | text: "background color" | |
|
92 | onClicked: series.backgroundColor = main.nextColor(); | |
|
93 | } | |
|
94 | } |
@@ -1,377 +1,377 | |||
|
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 "themewidget.h" |
|
22 | 22 | |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QPieSeries> |
|
25 | 25 | #include <QPieSlice> |
|
26 | 26 | #include <QBarSeries> |
|
27 | 27 | #include <QPercentBarSeries> |
|
28 | 28 | #include <QStackedBarSeries> |
|
29 | 29 | #include <QBarSet> |
|
30 | 30 | #include <QLineSeries> |
|
31 | 31 | #include <QSplineSeries> |
|
32 | 32 | #include <QScatterSeries> |
|
33 | 33 | #include <QAreaSeries> |
|
34 | 34 | #include <QLegend> |
|
35 | 35 | #include <QGridLayout> |
|
36 | 36 | #include <QFormLayout> |
|
37 | 37 | #include <QComboBox> |
|
38 | 38 | #include <QSpinBox> |
|
39 | 39 | #include <QCheckBox> |
|
40 | 40 | #include <QGroupBox> |
|
41 | 41 | #include <QLabel> |
|
42 | 42 | #include <QTime> |
|
43 | 43 | |
|
44 | 44 | ThemeWidget::ThemeWidget(QWidget* parent) : |
|
45 | 45 | QWidget(parent), |
|
46 | 46 | m_listCount(3), |
|
47 | 47 | m_valueMax(10), |
|
48 | 48 | m_valueCount(7), |
|
49 | 49 | m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)), |
|
50 | 50 | m_themeComboBox(createThemeBox()), |
|
51 | 51 | m_antialiasCheckBox(new QCheckBox("Anti-aliasing")), |
|
52 | 52 | m_animatedComboBox(createAnimationBox()), |
|
53 | 53 | m_legendComboBox(createLegendBox()) |
|
54 | 54 | { |
|
55 | 55 | connectSignals(); |
|
56 | 56 | // create layout |
|
57 | 57 | QGridLayout* baseLayout = new QGridLayout(); |
|
58 | 58 | QHBoxLayout *settingsLayout = new QHBoxLayout(); |
|
59 | 59 | settingsLayout->addWidget(new QLabel("Theme:")); |
|
60 | 60 | settingsLayout->addWidget(m_themeComboBox); |
|
61 | 61 | settingsLayout->addWidget(new QLabel("Animation:")); |
|
62 | 62 | settingsLayout->addWidget(m_animatedComboBox); |
|
63 | 63 | settingsLayout->addWidget(new QLabel("Legend:")); |
|
64 | 64 | settingsLayout->addWidget(m_legendComboBox); |
|
65 | 65 | settingsLayout->addWidget(m_antialiasCheckBox); |
|
66 | 66 | settingsLayout->addStretch(); |
|
67 | 67 | baseLayout->addLayout(settingsLayout, 0, 0, 1, 3); |
|
68 | 68 | |
|
69 | 69 | //create charts |
|
70 | 70 | |
|
71 | 71 | QChartView *chartView; |
|
72 | 72 | |
|
73 | 73 | chartView = new QChartView(createAreaChart()); |
|
74 | 74 | baseLayout->addWidget(chartView, 1, 0); |
|
75 | 75 | m_charts << chartView; |
|
76 | 76 | |
|
77 | 77 | chartView = new QChartView(createBarChart(m_valueCount)); |
|
78 | 78 | baseLayout->addWidget(chartView, 1, 1); |
|
79 | 79 | m_charts << chartView; |
|
80 | 80 | |
|
81 | 81 | chartView = new QChartView(createLineChart()); |
|
82 | 82 | baseLayout->addWidget(chartView, 1, 2); |
|
83 | 83 | m_charts << chartView; |
|
84 | 84 | |
|
85 | 85 | chartView = new QChartView(createPieChart()); |
|
86 | 86 | chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen... |
|
87 | 87 | baseLayout->addWidget(chartView, 2, 0); |
|
88 | 88 | m_charts << chartView; |
|
89 | 89 | |
|
90 | 90 | chartView = new QChartView(createSplineChart()); |
|
91 | 91 | baseLayout->addWidget(chartView, 2, 1); |
|
92 | 92 | m_charts << chartView; |
|
93 | 93 | |
|
94 | 94 | chartView = new QChartView(createScatterChart()); |
|
95 | 95 | baseLayout->addWidget(chartView, 2, 2); |
|
96 | 96 | m_charts << chartView; |
|
97 | 97 | |
|
98 | 98 | setLayout(baseLayout); |
|
99 | 99 | |
|
100 | 100 | // Set defaults |
|
101 | 101 | m_antialiasCheckBox->setChecked(true); |
|
102 | 102 | updateUI(); |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | ThemeWidget::~ThemeWidget() |
|
106 | 106 | { |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | void ThemeWidget::connectSignals() |
|
110 | 110 | { |
|
111 | 111 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
112 | 112 | connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); |
|
113 | 113 | connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
114 | 114 | connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const |
|
118 | 118 | { |
|
119 | 119 | DataTable dataTable; |
|
120 | 120 | |
|
121 | 121 | // set seed for random stuff |
|
122 | 122 | qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); |
|
123 | 123 | |
|
124 | 124 | // generate random data |
|
125 | 125 | for (int i(0); i < listCount; i++) { |
|
126 | 126 | DataList dataList; |
|
127 | 127 | qreal yValue(0); |
|
128 | 128 | for (int j(0); j < valueCount; j++) { |
|
129 | 129 | yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount; |
|
130 | 130 | QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount), |
|
131 | 131 | yValue); |
|
132 | 132 | QString label = "Slice " + QString::number(i) + ":" + QString::number(j); |
|
133 | 133 | dataList << Data(value, label); |
|
134 | 134 | } |
|
135 | 135 | dataTable << dataList; |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | return dataTable; |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | QComboBox* ThemeWidget::createThemeBox() const |
|
142 | 142 | { |
|
143 | 143 | // settings layout |
|
144 | 144 | QComboBox* themeComboBox = new QComboBox(); |
|
145 | 145 | themeComboBox->addItem("Light", QChart::ChartThemeLight); |
|
146 | 146 | themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean); |
|
147 | 147 | themeComboBox->addItem("Dark", QChart::ChartThemeDark); |
|
148 | 148 | themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand); |
|
149 | 149 | themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs); |
|
150 | 150 | themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast); |
|
151 | 151 | themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy); |
|
152 | 152 | return themeComboBox; |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | QComboBox* ThemeWidget::createAnimationBox() const |
|
156 | 156 | { |
|
157 | 157 | // settings layout |
|
158 | 158 | QComboBox* animationComboBox = new QComboBox(); |
|
159 | 159 | animationComboBox->addItem("No Animations", QChart::NoAnimation); |
|
160 | 160 | animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations); |
|
161 | 161 | animationComboBox->addItem("Series Animations", QChart::SeriesAnimations); |
|
162 | 162 | animationComboBox->addItem("All Animations", QChart::AllAnimations); |
|
163 | 163 | return animationComboBox; |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | QComboBox* ThemeWidget::createLegendBox() const |
|
167 | 167 | { |
|
168 | 168 | QComboBox* legendComboBox = new QComboBox(); |
|
169 | 169 | legendComboBox->addItem("No Legend ", 0); |
|
170 |
legendComboBox->addItem("Legend Top", Q |
|
|
171 |
legendComboBox->addItem("Legend Bottom", Q |
|
|
172 |
legendComboBox->addItem("Legend Left", Q |
|
|
173 |
legendComboBox->addItem("Legend Right", Q |
|
|
170 | legendComboBox->addItem("Legend Top", Qt::AlignTop); | |
|
171 | legendComboBox->addItem("Legend Bottom", Qt::AlignBottom); | |
|
172 | legendComboBox->addItem("Legend Left", Qt::AlignLeft); | |
|
173 | legendComboBox->addItem("Legend Right", Qt::AlignRight); | |
|
174 | 174 | return legendComboBox; |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | QChart* ThemeWidget::createAreaChart() const |
|
178 | 178 | { |
|
179 | 179 | QChart *chart = new QChart(); |
|
180 | 180 | chart->axisX()->setNiceNumbersEnabled(true); |
|
181 | 181 | chart->axisY()->setNiceNumbersEnabled(true); |
|
182 | 182 | chart->setTitle("Area chart"); |
|
183 | 183 | |
|
184 | 184 | // The lower series initialized to zero values |
|
185 | 185 | QLineSeries *lowerSeries = 0; |
|
186 | 186 | QString name("Series "); |
|
187 | 187 | int nameIndex = 0; |
|
188 | 188 | for (int i(0); i < m_dataTable.count(); i++) { |
|
189 | 189 | QLineSeries *upperSeries = new QLineSeries(chart); |
|
190 | 190 | for (int j(0); j < m_dataTable[i].count(); j++) { |
|
191 | 191 | Data data = m_dataTable[i].at(j); |
|
192 | 192 | if (lowerSeries){ |
|
193 | 193 | const QList<QPointF>& points = lowerSeries->points(); |
|
194 | 194 | upperSeries->append(QPointF(j, points[i].y() + data.first.y())); |
|
195 | 195 | }else |
|
196 | 196 | upperSeries->append(QPointF(j, data.first.y())); |
|
197 | 197 | } |
|
198 | 198 | QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries); |
|
199 | 199 | area->setName(name + QString::number(nameIndex)); |
|
200 | 200 | nameIndex++; |
|
201 | 201 | chart->addSeries(area); |
|
202 | 202 | lowerSeries = upperSeries; |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | return chart; |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | QChart* ThemeWidget::createBarChart(int valueCount) const |
|
209 | 209 | { |
|
210 | 210 | QChart* chart = new QChart(); |
|
211 | 211 | chart->axisX()->setNiceNumbersEnabled(true); |
|
212 | 212 | chart->axisY()->setNiceNumbersEnabled(true); |
|
213 | 213 | chart->setTitle("Bar chart"); |
|
214 | 214 | |
|
215 | 215 | QStringList categories; |
|
216 | 216 | for (int i(0); i < valueCount; i++) |
|
217 | 217 | categories << QString::number(i); |
|
218 | 218 | |
|
219 | 219 | chart->axisX()->categories()->insert(categories); |
|
220 | 220 | |
|
221 | 221 | QStackedBarSeries* series = new QStackedBarSeries(chart); |
|
222 | 222 | for (int i(0); i < m_dataTable.count(); i++) { |
|
223 | 223 | QBarSet *set = new QBarSet("Bar set " + QString::number(i)); |
|
224 | 224 | foreach (Data data, m_dataTable[i]) |
|
225 | 225 | *set << data.first.y(); |
|
226 | 226 | series->append(set); |
|
227 | 227 | } |
|
228 | 228 | chart->addSeries(series); |
|
229 | 229 | |
|
230 | 230 | return chart; |
|
231 | 231 | } |
|
232 | 232 | |
|
233 | 233 | QChart* ThemeWidget::createLineChart() const |
|
234 | 234 | { |
|
235 | 235 | QChart* chart = new QChart(); |
|
236 | 236 | chart->axisX()->setNiceNumbersEnabled(true); |
|
237 | 237 | chart->axisY()->setNiceNumbersEnabled(true); |
|
238 | 238 | chart->setTitle("Line chart"); |
|
239 | 239 | |
|
240 | 240 | QString name("Series "); |
|
241 | 241 | int nameIndex = 0; |
|
242 | 242 | foreach (DataList list, m_dataTable) { |
|
243 | 243 | QLineSeries *series = new QLineSeries(chart); |
|
244 | 244 | foreach (Data data, list) |
|
245 | 245 | series->append(data.first); |
|
246 | 246 | series->setName(name + QString::number(nameIndex)); |
|
247 | 247 | nameIndex++; |
|
248 | 248 | chart->addSeries(series); |
|
249 | 249 | } |
|
250 | 250 | |
|
251 | 251 | return chart; |
|
252 | 252 | } |
|
253 | 253 | |
|
254 | 254 | QChart* ThemeWidget::createPieChart() const |
|
255 | 255 | { |
|
256 | 256 | QChart* chart = new QChart(); |
|
257 | 257 | chart->setTitle("Pie chart"); |
|
258 | 258 | |
|
259 | 259 | qreal pieSize = 1.0 / m_dataTable.count(); |
|
260 | 260 | for (int i = 0; i < m_dataTable.count(); i++) { |
|
261 | 261 | QPieSeries *series = new QPieSeries(chart); |
|
262 | 262 | foreach (Data data, m_dataTable[i]) { |
|
263 | 263 | QPieSlice *slice = series->append(data.second, data.first.y()); |
|
264 | 264 | if (data == m_dataTable[i].first()) { |
|
265 | 265 | slice->setLabelVisible(); |
|
266 | 266 | slice->setExploded(); |
|
267 | 267 | } |
|
268 | 268 | } |
|
269 | 269 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); |
|
270 | 270 | series->setPieSize(pieSize); |
|
271 | 271 | series->setHorizontalPosition(hPos); |
|
272 | 272 | series->setVerticalPosition(0.5); |
|
273 | 273 | chart->addSeries(series); |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | return chart; |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | QChart* ThemeWidget::createSplineChart() const |
|
280 | 280 | { // spine chart |
|
281 | 281 | QChart* chart = new QChart(); |
|
282 | 282 | chart->axisX()->setNiceNumbersEnabled(true); |
|
283 | 283 | chart->axisY()->setNiceNumbersEnabled(true); |
|
284 | 284 | chart->setTitle("Spline chart"); |
|
285 | 285 | QString name("Series "); |
|
286 | 286 | int nameIndex = 0; |
|
287 | 287 | foreach (DataList list, m_dataTable) { |
|
288 | 288 | QSplineSeries *series = new QSplineSeries(chart); |
|
289 | 289 | foreach (Data data, list) |
|
290 | 290 | series->append(data.first); |
|
291 | 291 | series->setName(name + QString::number(nameIndex)); |
|
292 | 292 | nameIndex++; |
|
293 | 293 | chart->addSeries(series); |
|
294 | 294 | } |
|
295 | 295 | return chart; |
|
296 | 296 | } |
|
297 | 297 | |
|
298 | 298 | QChart* ThemeWidget::createScatterChart() const |
|
299 | 299 | { // scatter chart |
|
300 | 300 | QChart* chart = new QChart(); |
|
301 | 301 | chart->axisX()->setNiceNumbersEnabled(true); |
|
302 | 302 | chart->axisY()->setNiceNumbersEnabled(true); |
|
303 | 303 | chart->setTitle("Scatter chart"); |
|
304 | 304 | QString name("Series "); |
|
305 | 305 | int nameIndex = 0; |
|
306 | 306 | foreach (DataList list, m_dataTable) { |
|
307 | 307 | QScatterSeries *series = new QScatterSeries(chart); |
|
308 | 308 | foreach (Data data, list) |
|
309 | 309 | series->append(data.first); |
|
310 | 310 | series->setName(name + QString::number(nameIndex)); |
|
311 | 311 | nameIndex++; |
|
312 | 312 | chart->addSeries(series); |
|
313 | 313 | } |
|
314 | 314 | return chart; |
|
315 | 315 | } |
|
316 | 316 | |
|
317 | 317 | void ThemeWidget::updateUI() |
|
318 | 318 | { |
|
319 | 319 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); |
|
320 | 320 | |
|
321 | 321 | if (m_charts.at(0)->chart()->theme() != theme) { |
|
322 | 322 | foreach (QChartView *chartView, m_charts) |
|
323 | 323 | chartView->chart()->setTheme(theme); |
|
324 | 324 | |
|
325 | 325 | QPalette pal = window()->palette(); |
|
326 | 326 | if (theme == QChart::ChartThemeLight) { |
|
327 | 327 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
328 | 328 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
329 | 329 | } else if (theme == QChart::ChartThemeDark) { |
|
330 | 330 | pal.setColor(QPalette::Window, QRgb(0x121218)); |
|
331 | 331 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
332 | 332 | } else if (theme == QChart::ChartThemeBlueCerulean) { |
|
333 | 333 | pal.setColor(QPalette::Window, QRgb(0x40434a)); |
|
334 | 334 | pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6)); |
|
335 | 335 | } else if (theme == QChart::ChartThemeBrownSand) { |
|
336 | 336 | pal.setColor(QPalette::Window, QRgb(0x9e8965)); |
|
337 | 337 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
338 | 338 | } else if (theme == QChart::ChartThemeBlueNcs) { |
|
339 | 339 | pal.setColor(QPalette::Window, QRgb(0x018bba)); |
|
340 | 340 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
341 | 341 | } else if (theme == QChart::ChartThemeHighContrast) { |
|
342 | 342 | pal.setColor(QPalette::Window, QRgb(0xffab03)); |
|
343 | 343 | pal.setColor(QPalette::WindowText, QRgb(0x181818)); |
|
344 | 344 | } else if (theme == QChart::ChartThemeBlueIcy) { |
|
345 | 345 | pal.setColor(QPalette::Window, QRgb(0xcee7f0)); |
|
346 | 346 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
347 | 347 | } else { |
|
348 | 348 | pal.setColor(QPalette::Window, QRgb(0xf0f0f0)); |
|
349 | 349 | pal.setColor(QPalette::WindowText, QRgb(0x404044)); |
|
350 | 350 | } |
|
351 | 351 | window()->setPalette(pal); |
|
352 | 352 | } |
|
353 | 353 | |
|
354 | 354 | bool checked = m_antialiasCheckBox->isChecked(); |
|
355 | 355 | foreach (QChartView *chart, m_charts) |
|
356 | 356 | chart->setRenderHint(QPainter::Antialiasing, checked); |
|
357 | 357 | |
|
358 | 358 | QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); |
|
359 | 359 | if (m_charts.at(0)->chart()->animationOptions() != options) { |
|
360 | 360 | foreach (QChartView *chartView, m_charts) |
|
361 | 361 | chartView->chart()->setAnimationOptions(options); |
|
362 | 362 | } |
|
363 | 363 | |
|
364 |
Q |
|
|
364 | Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); | |
|
365 | 365 | |
|
366 | 366 | if (!alignment) { |
|
367 | 367 | foreach (QChartView *chartView, m_charts) { |
|
368 | 368 | chartView->chart()->legend()->hide(); |
|
369 | 369 | } |
|
370 | 370 | } else { |
|
371 | 371 | foreach (QChartView *chartView, m_charts) { |
|
372 | 372 | chartView->chart()->legend()->setAlignment(alignment); |
|
373 | 373 | chartView->chart()->legend()->show(); |
|
374 | 374 | } |
|
375 | 375 | } |
|
376 | 376 | } |
|
377 | 377 |
@@ -1,82 +1,82 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QApplication> |
|
22 | 22 | #include <QMainWindow> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QBarSeries> |
|
25 | 25 | #include <QBarSet> |
|
26 | 26 | #include <QLegend> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | int main(int argc, char *argv[]) |
|
31 | 31 | { |
|
32 | 32 | QApplication a(argc, argv); |
|
33 | 33 | |
|
34 | 34 | //![1] |
|
35 | 35 | QBarSet *set0 = new QBarSet("Jane"); |
|
36 | 36 | QBarSet *set1 = new QBarSet("John"); |
|
37 | 37 | QBarSet *set2 = new QBarSet("Axel"); |
|
38 | 38 | QBarSet *set3 = new QBarSet("Mary"); |
|
39 | 39 | QBarSet *set4 = new QBarSet("Samantha"); |
|
40 | 40 | |
|
41 | 41 | *set0 << QPointF(0.0, 1) << QPointF(1.0, 2) << QPointF(2.4, 3) << QPointF(3.0, 4) << QPointF(4.0, 5) << QPointF(5.0, 6); |
|
42 | 42 | *set1 << QPointF(0.1, 2) << QPointF(1.2, 3) << QPointF(2.45, 4) << QPointF(3.2, 5) << QPointF(4.2, 6) << QPointF(5.2, 7); |
|
43 | 43 | *set2 << QPointF(0.2, 3) << QPointF(1.4, 4) << QPointF(2.50, 5) << QPointF(3.4, 6) << QPointF(4.4, 7) << QPointF(5.4, 8); |
|
44 | 44 | *set3 << QPointF(0.3, 4) << QPointF(1.6, 5) << QPointF(2.55, 6) << QPointF(3.6, 7) << QPointF(4.6, 8) << QPointF(5.6, 9); |
|
45 | 45 | *set4 << QPointF(0.4, 5) << QPointF(1.8, 6) << QPointF(2.6, 7) << QPointF(3.8, 8) << QPointF(4.8, 9) << QPointF(5.8, 10); |
|
46 | 46 | //![1] |
|
47 | 47 | |
|
48 | 48 | //![2] |
|
49 | 49 | QBarSeries* series = new QBarSeries(); |
|
50 | 50 | series->append(set0); |
|
51 | 51 | series->append(set1); |
|
52 | 52 | series->append(set2); |
|
53 | 53 | series->append(set3); |
|
54 | 54 | series->append(set4); |
|
55 | 55 | //![2] |
|
56 | 56 | |
|
57 | 57 | //![3] |
|
58 | 58 | QChart* chart = new QChart(); |
|
59 | 59 | chart->addSeries(series); |
|
60 | 60 | chart->setTitle("Simple barchart example"); |
|
61 | 61 | //![3] |
|
62 | 62 | |
|
63 | 63 | //![4] |
|
64 | 64 | chart->legend()->setVisible(true); |
|
65 |
chart->legend()->setAlignment(Q |
|
|
65 | chart->legend()->setAlignment(Qt::AlignBottom); | |
|
66 | 66 | chart->axisY()->setNiceNumbersEnabled(true); |
|
67 | 67 | //![4] |
|
68 | 68 | |
|
69 | 69 | //![5] |
|
70 | 70 | QChartView* chartView = new QChartView(chart); |
|
71 | 71 | chartView->setRenderHint(QPainter::Antialiasing); |
|
72 | 72 | //![5] |
|
73 | 73 | |
|
74 | 74 | //![6] |
|
75 | 75 | QMainWindow window; |
|
76 | 76 | window.setCentralWidget(chartView); |
|
77 | 77 | window.resize(400, 300); |
|
78 | 78 | window.show(); |
|
79 | 79 | //![6] |
|
80 | 80 | |
|
81 | 81 | return a.exec(); |
|
82 | 82 | } |
@@ -1,90 +1,90 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QApplication> |
|
22 | 22 | #include <QMainWindow> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QGroupedBarSeries> |
|
25 | 25 | #include <QBarSet> |
|
26 | 26 | #include <QLegend> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | int main(int argc, char *argv[]) |
|
31 | 31 | { |
|
32 | 32 | QApplication a(argc, argv); |
|
33 | 33 | |
|
34 | 34 | //![1] |
|
35 | 35 | QBarSet *set0 = new QBarSet("Jane"); |
|
36 | 36 | QBarSet *set1 = new QBarSet("John"); |
|
37 | 37 | QBarSet *set2 = new QBarSet("Axel"); |
|
38 | 38 | QBarSet *set3 = new QBarSet("Mary"); |
|
39 | 39 | QBarSet *set4 = new QBarSet("Samantha"); |
|
40 | 40 | |
|
41 | 41 | *set0 << 1 << 2 << 3 << 4 << 5 << 6; |
|
42 | 42 | *set1 << 5 << 0 << 0 << 4 << 0 << 7; |
|
43 | 43 | *set2 << 3 << 5 << 8 << 13 << 8 << 5; |
|
44 | 44 | *set3 << 5 << 6 << 7 << 3 << 4 << 5; |
|
45 | 45 | *set4 << 9 << 7 << 5 << 3 << 1 << 2; |
|
46 | 46 | //![1] |
|
47 | 47 | |
|
48 | 48 | //![2] |
|
49 | 49 | QGroupedBarSeries* series = new QGroupedBarSeries(); |
|
50 | 50 | // series->setCategories(categories); |
|
51 | 51 | series->append(set0); |
|
52 | 52 | series->append(set1); |
|
53 | 53 | series->append(set2); |
|
54 | 54 | series->append(set3); |
|
55 | 55 | series->append(set4); |
|
56 | 56 | |
|
57 | 57 | //![2] |
|
58 | 58 | |
|
59 | 59 | //![3] |
|
60 | 60 | QChart* chart = new QChart(); |
|
61 | 61 | chart->addSeries(series); |
|
62 | 62 | chart->setTitle("Simple grouped barchart example"); |
|
63 | 63 | //![3] |
|
64 | 64 | |
|
65 | 65 | //![4] |
|
66 | 66 | QStringList categories; |
|
67 | 67 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; |
|
68 | 68 | chart->axisX()->categories()->insert(categories); |
|
69 | 69 | //![4] |
|
70 | 70 | |
|
71 | 71 | //![5] |
|
72 | 72 | chart->legend()->setVisible(true); |
|
73 |
chart->legend()->setAlignment(Q |
|
|
73 | chart->legend()->setAlignment(Qt::AlignBottom); | |
|
74 | 74 | chart->axisY()->setNiceNumbersEnabled(true); |
|
75 | 75 | //![5] |
|
76 | 76 | |
|
77 | 77 | //![6] |
|
78 | 78 | QChartView* chartView = new QChartView(chart); |
|
79 | 79 | chartView->setRenderHint(QPainter::Antialiasing); |
|
80 | 80 | //![6] |
|
81 | 81 | |
|
82 | 82 | //![7] |
|
83 | 83 | QMainWindow window; |
|
84 | 84 | window.setCentralWidget(chartView); |
|
85 | 85 | window.resize(400, 300); |
|
86 | 86 | window.show(); |
|
87 | 87 | //![7] |
|
88 | 88 | |
|
89 | 89 | return a.exec(); |
|
90 | 90 | } |
@@ -1,129 +1,129 | |||
|
1 | 1 | #include "mainwidget.h" |
|
2 | 2 | #include <QChart> |
|
3 | 3 | #include <QChartView> |
|
4 | 4 | #include <QPushButton> |
|
5 | 5 | #include <QLabel> |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | #include <QBarSet> |
|
8 | 8 | #include <QBarSeries> |
|
9 | 9 | #include <QLegend> |
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
12 | 12 | |
|
13 | 13 | MainWidget::MainWidget(QWidget *parent) : |
|
14 | 14 | QWidget(parent) |
|
15 | 15 | { |
|
16 | 16 | // Create buttons for ui |
|
17 | 17 | m_buttonLayout = new QGridLayout(); |
|
18 | 18 | QPushButton *detachLegendButton = new QPushButton("detach legend"); |
|
19 | 19 | connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend())); |
|
20 | 20 | m_buttonLayout->addWidget(detachLegendButton, 0, 0); |
|
21 | 21 | QPushButton *attachLegendButton = new QPushButton("attach legend"); |
|
22 | 22 | connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend())); |
|
23 | 23 | m_buttonLayout->addWidget(attachLegendButton, 1, 0); |
|
24 | 24 | |
|
25 | 25 | QPushButton *addSetButton = new QPushButton("add barset"); |
|
26 | 26 | connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset())); |
|
27 | 27 | m_buttonLayout->addWidget(addSetButton, 2, 0); |
|
28 | 28 | QPushButton *removeBarsetButton = new QPushButton("remove barset"); |
|
29 | 29 | connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset())); |
|
30 | 30 | m_buttonLayout->addWidget(removeBarsetButton, 3, 0); |
|
31 | 31 | |
|
32 | 32 | // Create chart view with the chart |
|
33 | 33 | //![1] |
|
34 | 34 | m_chart = new QChart(); |
|
35 | 35 | m_chartView = new QChartView(m_chart, this); |
|
36 | 36 | m_chartView->setRubberBand(QChartView::HorizonalRubberBand); |
|
37 | 37 | //![1] |
|
38 | 38 | |
|
39 | 39 | // Create custom scene and view, where detached legend will be drawn |
|
40 | 40 | //![2] |
|
41 | 41 | m_customView = new QGraphicsView(this); |
|
42 | 42 | m_customScene = new QGraphicsScene(this); |
|
43 | 43 | m_customView->setScene(m_customScene); |
|
44 | 44 | //![2] |
|
45 | 45 | |
|
46 | 46 | // Create layout for grid and detached legend |
|
47 | 47 | m_mainLayout = new QGridLayout(); |
|
48 | 48 | m_mainLayout->addLayout(m_buttonLayout, 0, 0); |
|
49 | 49 | m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1); |
|
50 | 50 | m_mainLayout->addWidget(m_customView, 0, 2, 3, 1); |
|
51 | 51 | setLayout(m_mainLayout); |
|
52 | 52 | |
|
53 | 53 | createSeries(); |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | void MainWidget::createSeries() |
|
57 | 57 | { |
|
58 | 58 | //![3] |
|
59 | 59 | m_series = new QBarSeries(); |
|
60 | 60 | addBarset(); |
|
61 | 61 | addBarset(); |
|
62 | 62 | addBarset(); |
|
63 | 63 | addBarset(); |
|
64 | 64 | |
|
65 | 65 | m_chart->addSeries(m_series); |
|
66 | 66 | m_chart->setTitle("Legend detach example"); |
|
67 | 67 | |
|
68 | 68 | m_chart->legend()->setVisible(true); |
|
69 |
m_chart->legend()->setAlignment(Q |
|
|
69 | m_chart->legend()->setAlignment(Qt::AlignBottom); | |
|
70 | 70 | m_chart->axisY()->setNiceNumbersEnabled(true); |
|
71 | 71 | |
|
72 | 72 | m_chartView->setRenderHint(QPainter::Antialiasing); |
|
73 | 73 | //![3] |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | void MainWidget::detachLegend() |
|
77 | 77 | { |
|
78 | 78 | // Detach legend from chart and |
|
79 | 79 | // put legend to our custom scene |
|
80 | 80 | //![4] |
|
81 | 81 | QLegend *legend = m_chart->legend(); |
|
82 | 82 | legend->detachFromChart(); |
|
83 | 83 | legend->setGeometry(m_customView->rect()); |
|
84 | 84 | m_customScene->addItem(legend); |
|
85 | 85 | //![4] |
|
86 | 86 | |
|
87 | 87 | // This forces redraw |
|
88 | 88 | QSize delta(1,1); |
|
89 | 89 | resize(size() + delta); |
|
90 | 90 | resize(size() - delta); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | void MainWidget::attachLegend() |
|
95 | 95 | { |
|
96 | 96 | // Remove legend from custom scene and put it back to chartview scene. |
|
97 | 97 | // Attach legend back to chart, so that layout works. |
|
98 | 98 | |
|
99 | 99 | //![5] |
|
100 | 100 | QLegend *legend = m_chart->legend(); |
|
101 | 101 | |
|
102 | 102 | if (m_customScene->items().contains(legend)) { |
|
103 | 103 | m_customScene->removeItem(legend); |
|
104 | 104 | m_chartView->scene()->addItem(legend); |
|
105 | 105 | legend->attachToChart(); |
|
106 | 106 | } |
|
107 | 107 | //![5] |
|
108 | 108 | |
|
109 | 109 | // This forces redraw |
|
110 | 110 | QSize delta(1,1); |
|
111 | 111 | resize(size() + delta); |
|
112 | 112 | resize(size() - delta); |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | void MainWidget::addBarset() |
|
116 | 116 | { |
|
117 | 117 | QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount())); |
|
118 | 118 | qreal delta = m_series->barsetCount() * 0.1; |
|
119 | 119 | *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta); |
|
120 | 120 | m_series->append(barSet); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | void MainWidget::removeBarset() |
|
124 | 124 | { |
|
125 | 125 | QList<QBarSet*> sets = m_series->barSets(); |
|
126 | 126 | if (sets.count() > 0) { |
|
127 | 127 | m_series->remove(sets.at(sets.count()-1)); |
|
128 | 128 | } |
|
129 | 129 | } |
@@ -1,88 +1,88 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QApplication> |
|
22 | 22 | #include <QMainWindow> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QPercentBarSeries> |
|
25 | 25 | #include <QBarSet> |
|
26 | 26 | #include <QLegend> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | int main(int argc, char *argv[]) |
|
31 | 31 | { |
|
32 | 32 | QApplication a(argc, argv); |
|
33 | 33 | |
|
34 | 34 | //![1] |
|
35 | 35 | QBarSet *set0 = new QBarSet("Jane"); |
|
36 | 36 | QBarSet *set1 = new QBarSet("John"); |
|
37 | 37 | QBarSet *set2 = new QBarSet("Axel"); |
|
38 | 38 | QBarSet *set3 = new QBarSet("Mary"); |
|
39 | 39 | QBarSet *set4 = new QBarSet("Samantha"); |
|
40 | 40 | |
|
41 | 41 | *set0 << 1 << 2 << 3 << 4 << 5 << 6; |
|
42 | 42 | *set1 << 5 << 0 << 0 << 4 << 0 << 7; |
|
43 | 43 | *set2 << 3 << 5 << 8 << 13 << 8 << 5; |
|
44 | 44 | *set3 << 5 << 6 << 7 << 3 << 4 << 5; |
|
45 | 45 | *set4 << 9 << 7 << 5 << 3 << 1 << 2; |
|
46 | 46 | //![1] |
|
47 | 47 | |
|
48 | 48 | //![2] |
|
49 | 49 | QPercentBarSeries* series = new QPercentBarSeries(); |
|
50 | 50 | series->append(set0); |
|
51 | 51 | series->append(set1); |
|
52 | 52 | series->append(set2); |
|
53 | 53 | series->append(set3); |
|
54 | 54 | series->append(set4); |
|
55 | 55 | //![2] |
|
56 | 56 | |
|
57 | 57 | //![3] |
|
58 | 58 | QChart* chart = new QChart(); |
|
59 | 59 | chart->addSeries(series); |
|
60 | 60 | chart->setTitle("Simple percentbarchart example"); |
|
61 | 61 | //![3] |
|
62 | 62 | |
|
63 | 63 | //![4] |
|
64 | 64 | QStringList categories; |
|
65 | 65 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; |
|
66 | 66 | chart->axisX()->categories()->insert(categories); |
|
67 | 67 | //![4] |
|
68 | 68 | |
|
69 | 69 | //![5] |
|
70 | 70 | chart->legend()->setVisible(true); |
|
71 |
chart->legend()->setAlignment(Q |
|
|
71 | chart->legend()->setAlignment(Qt::AlignBottom); | |
|
72 | 72 | //![5] |
|
73 | 73 | |
|
74 | 74 | //![6] |
|
75 | 75 | QChartView* chartView = new QChartView(chart); |
|
76 | 76 | chartView->setRenderHint(QPainter::Antialiasing); |
|
77 | 77 | //![6] |
|
78 | 78 | |
|
79 | 79 | //![7] |
|
80 | 80 | QMainWindow window; |
|
81 | 81 | window.setCentralWidget(chartView); |
|
82 | 82 | window.resize(400, 300); |
|
83 | 83 | window.show(); |
|
84 | 84 | //![7] |
|
85 | 85 | |
|
86 | 86 | return a.exec(); |
|
87 | 87 | } |
|
88 | 88 |
@@ -1,77 +1,77 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "drilldownchart.h" |
|
22 | 22 | #include "drilldownslice.h" |
|
23 | 23 | #include <QtGui/QApplication> |
|
24 | 24 | #include <QMainWindow> |
|
25 | 25 | #include <QTime> |
|
26 | 26 | #include <QChartView> |
|
27 | 27 | #include <QLegend> |
|
28 | 28 | #include <QPieSeries> |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | int main(int argc, char *argv[]) |
|
33 | 33 | { |
|
34 | 34 | QApplication a(argc, argv); |
|
35 | 35 | |
|
36 | 36 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
37 | 37 | |
|
38 | 38 | QMainWindow window; |
|
39 | 39 | |
|
40 | 40 | DrilldownChart* chart = new DrilldownChart(); |
|
41 | 41 | chart->setTheme(QChart::ChartThemeLight); |
|
42 | 42 | chart->setAnimationOptions(QChart::AllAnimations); |
|
43 | 43 | chart->legend()->setVisible(true); |
|
44 |
chart->legend()->setAlignment(Q |
|
|
44 | chart->legend()->setAlignment(Qt::AlignRight); | |
|
45 | 45 | |
|
46 | 46 | QPieSeries* yearSeries = new QPieSeries(&window); |
|
47 | 47 | yearSeries->setName("Sales by year - All"); |
|
48 | 48 | |
|
49 | 49 | QList<QString> months; |
|
50 | 50 | months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
51 | 51 | QList<QString> names; |
|
52 | 52 | names << "Jane" << "John" << "Axel" << "Mary" << "Susan" << "Bob"; |
|
53 | 53 | |
|
54 | 54 | foreach (QString name, names) { |
|
55 | 55 | QPieSeries* series = new QPieSeries(&window); |
|
56 | 56 | series->setName("Sales by month - " + name); |
|
57 | 57 | |
|
58 | 58 | foreach (QString month, months) |
|
59 | 59 | *series << new DrilldownSlice(qrand() % 1000, month, yearSeries); |
|
60 | 60 | |
|
61 | 61 | QObject::connect(series, SIGNAL(clicked(QPieSlice*)), chart, SLOT(handleSliceClicked(QPieSlice*))); |
|
62 | 62 | |
|
63 | 63 | *yearSeries << new DrilldownSlice(series->sum(), name, series); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), chart, SLOT(handleSliceClicked(QPieSlice*))); |
|
67 | 67 | |
|
68 | 68 | chart->changeSeries(yearSeries); |
|
69 | 69 | |
|
70 | 70 | QChartView* chartView = new QChartView(chart); |
|
71 | 71 | chartView->setRenderHint(QPainter::Antialiasing); |
|
72 | 72 | window.setCentralWidget(chartView); |
|
73 | 73 | window.resize(800, 500); |
|
74 | 74 | window.show(); |
|
75 | 75 | |
|
76 | 76 | return a.exec(); |
|
77 | 77 | } |
@@ -1,89 +1,89 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QApplication> |
|
22 | 22 | #include <QMainWindow> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QStackedBarSeries> |
|
25 | 25 | #include <QBarSet> |
|
26 | 26 | #include <QLegend> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | int main(int argc, char *argv[]) |
|
31 | 31 | { |
|
32 | 32 | QApplication a(argc, argv); |
|
33 | 33 | |
|
34 | 34 | //![1] |
|
35 | 35 | QBarSet *set0 = new QBarSet("Jane"); |
|
36 | 36 | QBarSet *set1 = new QBarSet("John"); |
|
37 | 37 | QBarSet *set2 = new QBarSet("Axel"); |
|
38 | 38 | QBarSet *set3 = new QBarSet("Mary"); |
|
39 | 39 | QBarSet *set4 = new QBarSet("Samantha"); |
|
40 | 40 | |
|
41 | 41 | *set0 << 1 << 2 << 3 << 4 << 5 << 6; |
|
42 | 42 | *set1 << 5 << 0 << 0 << 4 << 0 << 7; |
|
43 | 43 | *set2 << 3 << 5 << 8 << 13 << 8 << 5; |
|
44 | 44 | *set3 << 5 << 6 << 7 << 3 << 4 << 5; |
|
45 | 45 | *set4 << 9 << 7 << 5 << 3 << 1 << 2; |
|
46 | 46 | //![1] |
|
47 | 47 | |
|
48 | 48 | //![2] |
|
49 | 49 | QStackedBarSeries* series = new QStackedBarSeries(); |
|
50 | 50 | series->append(set0); |
|
51 | 51 | series->append(set1); |
|
52 | 52 | series->append(set2); |
|
53 | 53 | series->append(set3); |
|
54 | 54 | series->append(set4); |
|
55 | 55 | //![2] |
|
56 | 56 | |
|
57 | 57 | //![3] |
|
58 | 58 | QChart* chart = new QChart(); |
|
59 | 59 | chart->addSeries(series); |
|
60 | 60 | chart->setTitle("Simple stackedbarchart example"); |
|
61 | 61 | //![3] |
|
62 | 62 | |
|
63 | 63 | //![4] |
|
64 | 64 | QStringList categories; |
|
65 | 65 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; |
|
66 | 66 | chart->axisX()->categories()->insert(categories); |
|
67 | 67 | //![4] |
|
68 | 68 | |
|
69 | 69 | //![5] |
|
70 | 70 | chart->legend()->setVisible(true); |
|
71 |
chart->legend()->setAlignment(Q |
|
|
71 | chart->legend()->setAlignment(Qt::AlignBottom); | |
|
72 | 72 | chart->axisY()->setNiceNumbersEnabled(true); |
|
73 | 73 | //![5] |
|
74 | 74 | |
|
75 | 75 | //![6] |
|
76 | 76 | QChartView* chartView = new QChartView(chart); |
|
77 | 77 | chartView->setRenderHint(QPainter::Antialiasing); |
|
78 | 78 | //![6] |
|
79 | 79 | |
|
80 | 80 | //![7] |
|
81 | 81 | QMainWindow window; |
|
82 | 82 | window.setCentralWidget(chartView); |
|
83 | 83 | window.resize(400, 300); |
|
84 | 84 | window.show(); |
|
85 | 85 | //![7] |
|
86 | 86 | |
|
87 | 87 | return a.exec(); |
|
88 | 88 | } |
|
89 | 89 |
@@ -1,117 +1,117 | |||
|
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 <QtGui/QApplication> |
|
22 | 22 | #include <QMainWindow> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QBarSet> |
|
25 | 25 | #include <QLegend> |
|
26 | 26 | #include "drilldownseries.h" |
|
27 | 27 | #include "drilldownchart.h" |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
30 | 30 | |
|
31 | 31 | int main(int argc, char *argv[]) |
|
32 | 32 | { |
|
33 | 33 | //! [1] |
|
34 | 34 | QApplication a(argc, argv); |
|
35 | 35 | QMainWindow window; |
|
36 | 36 | //! [1] |
|
37 | 37 | |
|
38 | 38 | //! [2] |
|
39 | 39 | DrilldownChart* drilldownChart = new DrilldownChart(); |
|
40 | 40 | drilldownChart->setTheme(QChart::ChartThemeBlueIcy); |
|
41 | 41 | drilldownChart->setAnimationOptions(QChart::SeriesAnimations); |
|
42 | 42 | //! [2] |
|
43 | 43 | |
|
44 | 44 | //! [3] |
|
45 | 45 | // Define categories |
|
46 | 46 | QStringList months; |
|
47 | 47 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; |
|
48 | 48 | QStringList weeks; |
|
49 | 49 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; |
|
50 | 50 | QStringList plants; |
|
51 | 51 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; |
|
52 | 52 | //! [3] |
|
53 | 53 | |
|
54 | 54 | //! [4] |
|
55 | 55 | // Create drilldown structure |
|
56 | 56 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); |
|
57 | 57 | seasonSeries->setName("Crop by month - Season"); |
|
58 | 58 | |
|
59 | 59 | // Each month in season series has drilldown series for weekly data |
|
60 | 60 | for (int month=0; month < months.count(); month++) { |
|
61 | 61 | |
|
62 | 62 | // Create drilldown series for every week |
|
63 | 63 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); |
|
64 | 64 | seasonSeries->mapDrilldownSeries(month, weeklySeries); |
|
65 | 65 | |
|
66 | 66 | // Drilling down from weekly data brings us back to season data. |
|
67 | 67 | for (int week=0; week < weeks.count(); week++) { |
|
68 | 68 | weeklySeries->mapDrilldownSeries(week, seasonSeries); |
|
69 | 69 | weeklySeries->setName(QString("Crop by week - " + months.at(month))); |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | // Use right click signal to implement drilldown |
|
73 | 73 | QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int))); |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | // Enable drilldown from season series using right click. |
|
77 | 77 | QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int))); |
|
78 | 78 | //! [4] |
|
79 | 79 | |
|
80 | 80 | //! [5] |
|
81 | 81 | // Fill monthly and weekly series with data |
|
82 | 82 | foreach (QString plant, plants) { |
|
83 | 83 | QBarSet* monthlyCrop = new QBarSet(plant); |
|
84 | 84 | for (int month=0; month<months.count(); month++) { |
|
85 | 85 | QBarSet* weeklyCrop = new QBarSet(plant); |
|
86 | 86 | for (int week=0; week<weeks.count(); week++) { |
|
87 | 87 | *weeklyCrop << (qrand() % 20); |
|
88 | 88 | } |
|
89 | 89 | // Get the drilldown series from season series and add crop to it. |
|
90 | 90 | seasonSeries->drilldownSeries(month)->append(weeklyCrop); |
|
91 | 91 | *monthlyCrop << weeklyCrop->sum(); |
|
92 | 92 | } |
|
93 | 93 | seasonSeries->append(monthlyCrop); |
|
94 | 94 | } |
|
95 | 95 | //! [5] |
|
96 | 96 | |
|
97 | 97 | //! [6] |
|
98 | 98 | // Show season series in initial view |
|
99 | 99 | drilldownChart->changeSeries(seasonSeries); |
|
100 | 100 | drilldownChart->setTitle(seasonSeries->name()); |
|
101 | 101 | //! [6] |
|
102 | 102 | |
|
103 | 103 | //! [7] |
|
104 | 104 | drilldownChart->axisX()->setGridLineVisible(false); |
|
105 | 105 | drilldownChart->axisY()->setNiceNumbersEnabled(true); |
|
106 | 106 | drilldownChart->legend()->setVisible(true); |
|
107 |
drilldownChart->legend()->setAlignment(Q |
|
|
107 | drilldownChart->legend()->setAlignment(Qt::AlignBottom); | |
|
108 | 108 | //! [7] |
|
109 | 109 | |
|
110 | 110 | QChartView *chartView = new QChartView(drilldownChart); |
|
111 | 111 | window.setCentralWidget(chartView); |
|
112 | 112 | window.resize(400, 300); |
|
113 | 113 | window.show(); |
|
114 | 114 | |
|
115 | 115 | return a.exec(); |
|
116 | 116 | } |
|
117 | 117 |
@@ -1,249 +1,268 | |||
|
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 | DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) |
|
33 | 33 | : QDeclarativeItem(parent), |
|
34 |
m_chart(new QChart(this)) |
|
|
35 | m_legend(LegendTop) | |
|
34 | m_chart(new QChart(this)) | |
|
36 | 35 | { |
|
37 | 36 | setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
38 | 37 | // m_chart->axisX()->setNiceNumbersEnabled(false); |
|
39 | 38 | } |
|
40 | 39 | |
|
41 | 40 | DeclarativeChart::~DeclarativeChart() |
|
42 | 41 | { |
|
43 | 42 | delete m_chart; |
|
44 | 43 | } |
|
45 | 44 | |
|
46 | 45 | void DeclarativeChart::childEvent(QChildEvent *event) |
|
47 | 46 | { |
|
48 | 47 | if (event->type() == QEvent::ChildAdded) { |
|
49 | 48 | if (qobject_cast<QAbstractSeries *>(event->child())) { |
|
50 | 49 | m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child())); |
|
51 | 50 | } |
|
52 | 51 | } |
|
53 | 52 | } |
|
54 | 53 | |
|
55 | 54 | void DeclarativeChart::componentComplete() |
|
56 | 55 | { |
|
57 | 56 | // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max(); |
|
58 | 57 | foreach(QObject *child, children()) { |
|
59 | 58 | if (qobject_cast<QAbstractSeries *>(child)) { |
|
60 | 59 | // qDebug() << "DeclarativeChart::componentComplete(), add: " << child; |
|
61 | 60 | m_chart->addSeries(qobject_cast<QAbstractSeries *>(child)); |
|
62 | 61 | } |
|
63 | 62 | } |
|
64 | 63 | // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max(); |
|
65 | 64 | |
|
66 | 65 | QDeclarativeItem::componentComplete(); |
|
67 | 66 | } |
|
68 | 67 | |
|
69 | 68 | void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) |
|
70 | 69 | { |
|
71 | 70 | // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height(); |
|
72 | 71 | if (newGeometry.isValid()) { |
|
73 | 72 | if (newGeometry.width() > 0 && newGeometry.height() > 0) { |
|
74 | 73 | m_chart->resize(newGeometry.width(), newGeometry.height()); |
|
75 | 74 | } |
|
76 | 75 | } |
|
77 | 76 | QDeclarativeItem::geometryChanged(newGeometry, oldGeometry); |
|
78 | 77 | } |
|
79 | 78 | |
|
80 | 79 | void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
81 | 80 | { |
|
82 | 81 | Q_UNUSED(option) |
|
83 | 82 | Q_UNUSED(widget) |
|
84 | 83 | |
|
85 | 84 | // TODO: optimized? |
|
86 | 85 | painter->setRenderHint(QPainter::Antialiasing, true); |
|
87 | 86 | } |
|
88 | 87 | |
|
89 | 88 | void DeclarativeChart::setTheme(DeclarativeChart::Theme theme) |
|
90 | 89 | { |
|
91 |
|
|
|
90 | QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme; | |
|
91 | if (chartTheme != m_chart->theme()) { | |
|
92 | m_chart->setTheme(chartTheme); | |
|
93 | themeChanged(); | |
|
94 | } | |
|
92 | 95 | } |
|
93 | 96 | |
|
94 | 97 | DeclarativeChart::Theme DeclarativeChart::theme() |
|
95 | 98 | { |
|
96 | 99 | return (DeclarativeChart::Theme) m_chart->theme(); |
|
97 | 100 | } |
|
98 | 101 | |
|
99 | 102 | void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations) |
|
100 | 103 | { |
|
101 |
|
|
|
104 | QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations; | |
|
105 | if (animationOptions != m_chart->animationOptions()) { | |
|
106 | m_chart->setAnimationOptions(animationOptions); | |
|
107 | animationOptionsChanged(); | |
|
108 | } | |
|
102 | 109 | } |
|
103 | 110 | |
|
104 | 111 | DeclarativeChart::Animation DeclarativeChart::animationOptions() |
|
105 | 112 | { |
|
106 | 113 | if (m_chart->animationOptions().testFlag(QChart::AllAnimations)) |
|
107 | 114 | return DeclarativeChart::AllAnimations; |
|
108 | 115 | else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations)) |
|
109 | 116 | return DeclarativeChart::GridAxisAnimations; |
|
110 | 117 | else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations)) |
|
111 | 118 | return DeclarativeChart::SeriesAnimations; |
|
112 | 119 | else |
|
113 | 120 | return DeclarativeChart::NoAnimation; |
|
114 | 121 | } |
|
115 | 122 | |
|
116 |
void DeclarativeChart::set |
|
|
117 | { | |
|
118 |
if (le |
|
|
119 | m_legend = legend; | |
|
120 |
|
|
|
121 | case LegendDisabled: | |
|
122 | m_chart->legend()->setVisible(false); | |
|
123 | break; | |
|
124 | case LegendTop: | |
|
125 | m_chart->legend()->setVisible(true); | |
|
126 | m_chart->legend()->setAlignment(QLegend::AlignmentTop); | |
|
127 | break; | |
|
128 | case LegendBottom: | |
|
129 | m_chart->legend()->setVisible(true); | |
|
130 | m_chart->legend()->setAlignment(QLegend::AlignmentBottom); | |
|
131 | break; | |
|
132 | case LegendLeft: | |
|
133 | m_chart->legend()->setVisible(true); | |
|
134 | m_chart->legend()->setAlignment(QLegend::AlignmentLeft); | |
|
135 | break; | |
|
136 | case LegendRight: | |
|
137 | m_chart->legend()->setVisible(true); | |
|
138 | m_chart->legend()->setAlignment(QLegend::AlignmentRight); | |
|
139 | break; | |
|
140 | default: | |
|
141 | m_chart->legend()->setVisible(false); | |
|
142 | break; | |
|
143 | } | |
|
123 | void DeclarativeChart::setTitle(QString title) | |
|
124 | { | |
|
125 | if (title != m_chart->title()) { | |
|
126 | m_chart->setTitle(title); | |
|
127 | emit titleChanged(); | |
|
144 | 128 | } |
|
145 | 129 | } |
|
146 | ||
|
147 | DeclarativeChart::Legend DeclarativeChart::legend() | |
|
130 | QString DeclarativeChart::title() | |
|
148 | 131 | { |
|
149 |
return m_ |
|
|
132 | return m_chart->title(); | |
|
150 | 133 | } |
|
151 | 134 | |
|
152 | 135 | QAxis *DeclarativeChart::axisX() |
|
153 | 136 | { |
|
154 | 137 | return m_chart->axisX(); |
|
155 | 138 | } |
|
156 | 139 | |
|
157 | 140 | QAxis *DeclarativeChart::axisY() |
|
158 | 141 | { |
|
159 | 142 | return m_chart->axisY(); |
|
160 | 143 | } |
|
161 | 144 | |
|
145 | QLegend *DeclarativeChart::legend() | |
|
146 | { | |
|
147 | return m_chart->legend(); | |
|
148 | } | |
|
149 | ||
|
162 | 150 | QVariantList DeclarativeChart::axisXLabels() |
|
163 | 151 | { |
|
164 | 152 | QVariantList labels; |
|
165 | 153 | foreach (qreal value, m_chart->axisX()->categories()->values()) { |
|
166 | 154 | labels.append(value); |
|
167 | 155 | labels.append(m_chart->axisX()->categories()->label(value)); |
|
168 | 156 | } |
|
169 | 157 | return labels; |
|
170 | 158 | } |
|
171 | 159 | |
|
172 | 160 | void DeclarativeChart::setAxisXLabels(QVariantList list) |
|
173 | 161 | { |
|
174 | 162 | QVariant value(QVariant::Invalid); |
|
175 | 163 | foreach (QVariant element, list) { |
|
176 | 164 | if (value.isValid() && element.type() == QVariant::String) { |
|
177 | 165 | m_chart->axisX()->categories()->insert(value.toDouble(), element.toString()); |
|
178 | 166 | value = QVariant(QVariant::Invalid); |
|
179 | 167 | } else { |
|
180 | 168 | if (element.canConvert(QVariant::Double)) |
|
181 | 169 | value = element; |
|
182 | 170 | } |
|
183 | 171 | } |
|
172 | emit axisLabelsChanged(); | |
|
173 | } | |
|
174 | ||
|
175 | void DeclarativeChart::setTitleColor(QColor color) | |
|
176 | { | |
|
177 | QBrush b = m_chart->titleBrush(); | |
|
178 | if (color != b.color()) { | |
|
179 | b.setColor(color); | |
|
180 | m_chart->setTitleBrush(b); | |
|
181 | emit titleColorChanged(); | |
|
182 | } | |
|
183 | } | |
|
184 | ||
|
185 | QColor DeclarativeChart::titleColor() | |
|
186 | { | |
|
187 | return m_chart->titleBrush().color(); | |
|
188 | } | |
|
189 | ||
|
190 | void DeclarativeChart::setBackgroundColor(QColor color) | |
|
191 | { | |
|
192 | QBrush b = m_chart->backgroundBrush(); | |
|
193 | if (color != b.color()) { | |
|
194 | b.setColor(color); | |
|
195 | m_chart->setBackgroundBrush(b); | |
|
196 | emit backgroundColorChanged(); | |
|
197 | } | |
|
198 | } | |
|
199 | ||
|
200 | QColor DeclarativeChart::backgroundColor() | |
|
201 | { | |
|
202 | return m_chart->backgroundBrush().color(); | |
|
184 | 203 | } |
|
185 | 204 | |
|
186 | 205 | int DeclarativeChart::count() |
|
187 | 206 | { |
|
188 | 207 | return m_chart->series().count(); |
|
189 | 208 | } |
|
190 | 209 | |
|
191 | 210 | QAbstractSeries *DeclarativeChart::series(int index) |
|
192 | 211 | { |
|
193 | 212 | if (index < m_chart->series().count()) { |
|
194 | 213 | return m_chart->series().at(index); |
|
195 | 214 | } |
|
196 | 215 | return 0; |
|
197 | 216 | } |
|
198 | 217 | |
|
199 | 218 | QAbstractSeries *DeclarativeChart::series(QString seriesName) |
|
200 | 219 | { |
|
201 | 220 | foreach(QAbstractSeries *series, m_chart->series()) { |
|
202 | 221 | if (series->name() == seriesName) |
|
203 | 222 | return series; |
|
204 | 223 | } |
|
205 | 224 | return 0; |
|
206 | 225 | } |
|
207 | 226 | |
|
208 | 227 | QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name) |
|
209 | 228 | { |
|
210 | 229 | QAbstractSeries *series = 0; |
|
211 | 230 | switch (type) { |
|
212 | 231 | case DeclarativeChart::SeriesTypeLine: |
|
213 | 232 | series = new DeclarativeLineSeries(); |
|
214 | 233 | break; |
|
215 | 234 | case DeclarativeChart::SeriesTypeArea: |
|
216 | 235 | series = new DeclarativeAreaSeries(); |
|
217 | 236 | break; |
|
218 | 237 | case DeclarativeChart::SeriesTypeBar: |
|
219 | 238 | series = new DeclarativeBarSeries(); |
|
220 | 239 | break; |
|
221 | 240 | case DeclarativeChart::SeriesTypeStackedBar: |
|
222 | 241 | // TODO |
|
223 | 242 | break; |
|
224 | 243 | case DeclarativeChart::SeriesTypePercentBar: |
|
225 | 244 | // TODO |
|
226 | 245 | break; |
|
227 | 246 | case DeclarativeChart::SeriesTypeGroupedBar: |
|
228 | 247 | series = new DeclarativeGroupedBarSeries(); |
|
229 | 248 | break; |
|
230 | 249 | case DeclarativeChart::SeriesTypePie: |
|
231 | 250 | series = new DeclarativePieSeries(); |
|
232 | 251 | break; |
|
233 | 252 | case DeclarativeChart::SeriesTypeScatter: |
|
234 | 253 | series = new DeclarativeScatterSeries(); |
|
235 | 254 | break; |
|
236 | 255 | case DeclarativeChart::SeriesTypeSpline: |
|
237 | 256 | series = new DeclarativeSplineSeries(); |
|
238 | 257 | break; |
|
239 | 258 | default: |
|
240 | 259 | qWarning() << "Illegal series type"; |
|
241 | 260 | } |
|
242 | 261 | series->setName(name); |
|
243 | 262 | m_chart->addSeries(series); |
|
244 | 263 | return series; |
|
245 | 264 | } |
|
246 | 265 | |
|
247 | 266 | #include "moc_declarativechart.cpp" |
|
248 | 267 | |
|
249 | 268 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,127 +1,130 | |||
|
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 | Q_PROPERTY(Theme theme READ theme WRITE setTheme) | |
|
38 | Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions) | |
|
39 | Q_PROPERTY(QString title READ title WRITE setTitle) | |
|
40 | Q_PROPERTY(Legend legend READ legend WRITE setLegend) | |
|
37 | Q_PROPERTY(Theme theme READ theme WRITE setTheme NOTIFY themeChanged) | |
|
38 | Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions NOTIFY animationOptionsChanged) | |
|
39 | Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) | |
|
41 | 40 | Q_PROPERTY(QAxis *axisX READ axisX) |
|
42 | 41 | Q_PROPERTY(QAxis *axisY READ axisY) |
|
42 | Q_PROPERTY(QLegend *legend READ legend) | |
|
43 | 43 | // TODO: how to define axis labels? This is not very convenient |
|
44 | Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels) | |
|
44 | Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels NOTIFY axisLabelsChanged) | |
|
45 | 45 | Q_PROPERTY(int count READ count) |
|
46 | Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged) | |
|
47 | Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged) | |
|
46 | 48 | Q_ENUMS(Animation) |
|
47 | 49 | Q_ENUMS(Theme) |
|
48 | Q_ENUMS(Legend) | |
|
49 | 50 | Q_ENUMS(SeriesType) |
|
50 | 51 | |
|
51 | 52 | public: |
|
52 | 53 | // duplicating enums from QChart to make the QML api namings 1-to-1 with the C++ api |
|
53 | 54 | enum Theme { |
|
54 | 55 | ChartThemeLight = 0, |
|
55 | 56 | ChartThemeBlueCerulean, |
|
56 | 57 | ChartThemeDark, |
|
57 | 58 | ChartThemeBrownSand, |
|
58 | 59 | ChartThemeBlueNcs, |
|
59 | 60 | ChartThemeHighContrast, |
|
60 | 61 | ChartThemeBlueIcy |
|
61 | 62 | }; |
|
62 | 63 | |
|
63 | 64 | enum Animation { |
|
64 | 65 | NoAnimation = 0x0, |
|
65 | 66 | GridAxisAnimations = 0x1, |
|
66 | 67 | SeriesAnimations =0x2, |
|
67 | 68 | AllAnimations = 0x3 |
|
68 | 69 | }; |
|
69 | 70 | |
|
70 | enum Legend { | |
|
71 | LegendDisabled = 0, | |
|
72 | LegendTop, | |
|
73 | LegendBottom, | |
|
74 | LegendLeft, | |
|
75 | LegendRight | |
|
76 | }; | |
|
77 | ||
|
78 | 71 | enum SeriesType { |
|
79 | 72 | SeriesTypeLine, |
|
80 | 73 | SeriesTypeArea, |
|
81 | 74 | SeriesTypeBar, |
|
82 | 75 | SeriesTypeStackedBar, |
|
83 | 76 | SeriesTypePercentBar, |
|
84 | 77 | SeriesTypeGroupedBar, |
|
85 | 78 | SeriesTypePie, |
|
86 | 79 | SeriesTypeScatter, |
|
87 | 80 | SeriesTypeSpline |
|
88 | 81 | }; |
|
89 | 82 | |
|
90 | 83 | public: |
|
91 | 84 | DeclarativeChart(QDeclarativeItem *parent = 0); |
|
92 | 85 | ~DeclarativeChart(); |
|
93 | 86 | |
|
94 | 87 | public: // From QDeclarativeItem/QGraphicsItem |
|
95 | 88 | void childEvent(QChildEvent *event); |
|
96 | 89 | void componentComplete(); |
|
97 | 90 | void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); |
|
98 | 91 | void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
99 | 92 | |
|
100 | 93 | public: |
|
101 | 94 | void setTheme(DeclarativeChart::Theme theme); |
|
102 | 95 | DeclarativeChart::Theme theme(); |
|
103 | 96 | void setAnimationOptions(DeclarativeChart::Animation animations); |
|
104 | 97 | DeclarativeChart::Animation animationOptions(); |
|
105 |
void setTitle(QString title) |
|
|
106 | QString title() { return m_chart->title();} | |
|
107 | void setLegend(DeclarativeChart::Legend legend); | |
|
108 | DeclarativeChart::Legend legend(); | |
|
98 | void setTitle(QString title); | |
|
99 | QString title(); | |
|
109 | 100 | QAxis *axisX(); |
|
110 | 101 | QAxis *axisY(); |
|
102 | QLegend *legend(); | |
|
111 | 103 | QVariantList axisXLabels(); |
|
112 | 104 | void setAxisXLabels(QVariantList list); |
|
105 | void setTitleColor(QColor color); | |
|
106 | QColor titleColor(); | |
|
107 | void setBackgroundColor(QColor color); | |
|
108 | QColor backgroundColor(); | |
|
113 | 109 | int count(); |
|
114 | 110 | Q_INVOKABLE QAbstractSeries *series(int index); |
|
115 | 111 | Q_INVOKABLE QAbstractSeries *series(QString seriesName); |
|
116 | 112 | Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = ""); |
|
117 | 113 | |
|
114 | Q_SIGNALS: | |
|
115 | void themeChanged(); | |
|
116 | void animationOptionsChanged(); | |
|
117 | void titleChanged(); | |
|
118 | void axisLabelsChanged(); | |
|
119 | void titleColorChanged(); | |
|
120 | void backgroundColorChanged(); | |
|
121 | ||
|
118 | 122 | public: |
|
119 | 123 | // Extending QChart with DeclarativeChart is not possible because QObject does not support |
|
120 | 124 | // multi inheritance, so we now have a QChart as a member instead |
|
121 | 125 | QChart *m_chart; |
|
122 | Legend m_legend; | |
|
123 | 126 | }; |
|
124 | 127 | |
|
125 | 128 | QTCOMMERCIALCHART_END_NAMESPACE |
|
126 | 129 | |
|
127 | 130 | #endif // DECLARATIVECHART_H |
@@ -1,99 +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 <QtDeclarative/qdeclarativeextensionplugin.h> |
|
22 | 22 | #include <QtDeclarative/qdeclarative.h> |
|
23 | 23 | #include "qchart.h" |
|
24 | 24 | #include "qaxiscategories.h" |
|
25 | 25 | #include "declarativechart.h" |
|
26 | 26 | #include "declarativexypoint.h" |
|
27 | 27 | #include "declarativelineseries.h" |
|
28 | 28 | #include "declarativesplineseries.h" |
|
29 | 29 | #include "declarativeareaseries.h" |
|
30 | 30 | #include "declarativescatterseries.h" |
|
31 | 31 | #include "declarativebarseries.h" |
|
32 | 32 | #include "declarativepieseries.h" |
|
33 | 33 | #include <QVXYModelMapper> |
|
34 | 34 | #include <QHXYModelMapper> |
|
35 | 35 | #include <QHPieModelMapper> |
|
36 | 36 | #include <QVPieModelMapper> |
|
37 | 37 | #include <QHBarModelMapper> |
|
38 | 38 | #include <QVBarModelMapper> |
|
39 | 39 | |
|
40 | 40 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
41 | 41 | |
|
42 | 42 | class ChartQmlPlugin : public QDeclarativeExtensionPlugin |
|
43 | 43 | { |
|
44 | 44 | Q_OBJECT |
|
45 | 45 | public: |
|
46 | 46 | virtual void registerTypes(const char *uri) |
|
47 | 47 | { |
|
48 | 48 | Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart")); |
|
49 | 49 | |
|
50 | 50 | qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView"); |
|
51 | 51 | qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint"); |
|
52 | 52 | qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries"); |
|
53 | 53 | qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries"); |
|
54 | 54 | qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries"); |
|
55 | 55 | qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries"); |
|
56 | 56 | qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries"); |
|
57 | 57 | qmlRegisterType<DeclarativeGroupedBarSeries>(uri, 1, 0, "GroupedBarSeries"); |
|
58 | 58 | qmlRegisterType<DeclarativeStackedBarSeries>(uri, 1, 0, "StackedBarSeries"); |
|
59 | 59 | qmlRegisterType<DeclarativePercentBarSeries>(uri, 1, 0, "PercentBarSeries"); |
|
60 | 60 | qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries"); |
|
61 | 61 | qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice"); |
|
62 | 62 | qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet"); |
|
63 | 63 | qmlRegisterType<QHXYModelMapper>(uri, 1, 0, "HXYModelMapper"); |
|
64 | 64 | qmlRegisterType<QVXYModelMapper>(uri, 1, 0, "VXYModelMapper"); |
|
65 | 65 | qmlRegisterType<QHPieModelMapper>(uri, 1, 0, "HPieModelMapper"); |
|
66 | 66 | qmlRegisterType<QVPieModelMapper>(uri, 1, 0, "VPieModelMapper"); |
|
67 | 67 | qmlRegisterType<QHBarModelMapper>(uri, 1, 0, "HBarModelMapper"); |
|
68 | 68 | qmlRegisterType<QVBarModelMapper>(uri, 1, 0, "VBarModelMapper"); |
|
69 | 69 | |
|
70 | qmlRegisterUncreatableType<QLegend>(uri, 1, 0, "Legend", | |
|
71 | QLatin1String("Trying to create uncreatable: Legend.")); | |
|
70 | 72 | qmlRegisterUncreatableType<QScatterSeries>(uri, 1, 0, "QScatterSeries", |
|
71 | 73 | QLatin1String("Trying to create uncreatable: QScatterSeries.")); |
|
72 | 74 | qmlRegisterUncreatableType<QPieSeries>(uri, 1, 0, "QPieSeries", |
|
73 | 75 | QLatin1String("Trying to create uncreatable: QPieSeries.")); |
|
74 | 76 | qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel", |
|
75 | 77 | QLatin1String("Trying to create uncreatable: AbstractItemModel.")); |
|
76 | 78 | qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper", |
|
77 | 79 | QLatin1String("Trying to create uncreatable: XYModelMapper.")); |
|
78 | 80 | qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper", |
|
79 | 81 | QLatin1String("Trying to create uncreatable: PieModelMapper.")); |
|
80 | 82 | qmlRegisterUncreatableType<QBarModelMapper>(uri, 1, 0, "BarModelMapper", |
|
81 | 83 | QLatin1String("Trying to create uncreatable: BarModelMapper.")); |
|
82 | 84 | qmlRegisterUncreatableType<QAbstractSeries>(uri, 1, 0, "AbstractSeries", |
|
83 | 85 | QLatin1String("Trying to create uncreatable: AbstractSeries.")); |
|
84 | 86 | qmlRegisterUncreatableType<QAxis>(uri, 1, 0, "Axis", |
|
85 | 87 | QLatin1String("Trying to create uncreatable: Axis.")); |
|
86 | 88 | qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper", |
|
87 | 89 | QLatin1String("Trying to create uncreatable: PieModelMapper.")); |
|
88 | 90 | qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper", |
|
89 | 91 | QLatin1String("Trying to create uncreatable: XYModelMapper.")); |
|
90 | 92 | } |
|
91 | 93 | }; |
|
92 | 94 | |
|
93 | 95 | #include "plugin.moc" |
|
94 | 96 | |
|
95 | 97 | QTCOMMERCIALCHART_END_NAMESPACE |
|
96 | 98 | |
|
97 | 99 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
98 | 100 | |
|
99 | 101 | Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin)) |
@@ -1,429 +1,429 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | #include "chartpresenter_p.h" |
|
21 | 21 | #include "qchart.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include "qaxis.h" |
|
24 | 24 | #include "chartdataset_p.h" |
|
25 | 25 | #include "charttheme_p.h" |
|
26 | 26 | #include "chartanimator_p.h" |
|
27 | 27 | #include "chartanimation_p.h" |
|
28 | 28 | #include "qabstractseries_p.h" |
|
29 | 29 | #include "qareaseries.h" |
|
30 | 30 | #include "chartaxis_p.h" |
|
31 | 31 | #include "chartaxisx_p.h" |
|
32 | 32 | #include "chartaxisy_p.h" |
|
33 | 33 | #include "areachartitem_p.h" |
|
34 | 34 | #include "chartbackground_p.h" |
|
35 | 35 | #include <QTimer> |
|
36 | 36 | |
|
37 | 37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
38 | 38 | |
|
39 | 39 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
40 | 40 | m_chart(chart), |
|
41 | 41 | m_animator(0), |
|
42 | 42 | m_dataset(dataset), |
|
43 | 43 | m_chartTheme(0), |
|
44 | 44 | m_chartRect(QRectF(QPoint(0,0),m_chart->size())), |
|
45 | 45 | m_options(QChart::NoAnimation), |
|
46 | 46 | m_minLeftMargin(0), |
|
47 | 47 | m_minBottomMargin(0), |
|
48 | 48 | m_state(ShowState), |
|
49 | 49 | m_backgroundItem(0), |
|
50 | 50 | m_titleItem(0), |
|
51 | 51 | m_marginBig(60), |
|
52 | 52 | m_marginSmall(20), |
|
53 | 53 | m_marginTiny(10), |
|
54 | 54 | m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) |
|
55 | 55 | { |
|
56 | 56 | |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | ChartPresenter::~ChartPresenter() |
|
60 | 60 | { |
|
61 | 61 | delete m_chartTheme; |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | void ChartPresenter::setGeometry(const QRectF& rect) |
|
65 | 65 | { |
|
66 | 66 | m_rect = rect; |
|
67 | 67 | Q_ASSERT(m_rect.isValid()); |
|
68 | 68 | updateLayout(); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void ChartPresenter::setMinimumMarginWidth(ChartAxis* axis, qreal width) |
|
72 | 72 | { |
|
73 | 73 | switch(axis->axisType()){ |
|
74 | 74 | case ChartAxis::X_AXIS: |
|
75 | 75 | { |
|
76 | 76 | if(width>m_chartRect.width()+ m_chartMargins.left()) { |
|
77 | 77 | m_minLeftMargin= width - m_chartRect.width(); |
|
78 | 78 | updateLayout(); |
|
79 | 79 | } |
|
80 | 80 | break; |
|
81 | 81 | } |
|
82 | 82 | case ChartAxis::Y_AXIS: |
|
83 | 83 | { |
|
84 | 84 | |
|
85 | 85 | if(m_minLeftMargin!=width){ |
|
86 | 86 | m_minLeftMargin= width; |
|
87 | 87 | updateLayout(); |
|
88 | 88 | } |
|
89 | 89 | break; |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | } |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | void ChartPresenter::setMinimumMarginHeight(ChartAxis* axis, qreal height) |
|
96 | 96 | { |
|
97 | 97 | switch(axis->axisType()){ |
|
98 | 98 | case ChartAxis::X_AXIS: |
|
99 | 99 | { |
|
100 | 100 | if(m_minBottomMargin!=height) { |
|
101 | 101 | m_minBottomMargin= height; |
|
102 | 102 | updateLayout(); |
|
103 | 103 | } |
|
104 | 104 | break; |
|
105 | 105 | } |
|
106 | 106 | case ChartAxis::Y_AXIS: |
|
107 | 107 | { |
|
108 | 108 | |
|
109 | 109 | if(height>m_chartMargins.bottom()+m_chartRect.height()){ |
|
110 | 110 | m_minBottomMargin= height - m_chartRect.height(); |
|
111 | 111 | updateLayout(); |
|
112 | 112 | } |
|
113 | 113 | break; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | } |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain) |
|
120 | 120 | { |
|
121 | 121 | ChartAxis* item; |
|
122 | 122 | |
|
123 | 123 | if(axis == m_dataset->axisX()){ |
|
124 | 124 | item = new ChartAxisX(axis,this); |
|
125 | 125 | }else{ |
|
126 | 126 | item = new ChartAxisY(axis,this); |
|
127 | 127 | } |
|
128 | 128 | |
|
129 | 129 | if(m_options.testFlag(QChart::GridAxisAnimations)){ |
|
130 | 130 | item->setAnimator(m_animator); |
|
131 | 131 | item->setAnimation(new AxisAnimation(item)); |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | if(axis==m_dataset->axisX()){ |
|
135 | 135 | m_chartTheme->decorate(axis,true); |
|
136 | 136 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
137 | 137 | //initialize |
|
138 | 138 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); |
|
139 | 139 | |
|
140 | 140 | } |
|
141 | 141 | else{ |
|
142 | 142 | m_chartTheme->decorate(axis,false); |
|
143 | 143 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
144 | 144 | //initialize |
|
145 | 145 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); |
|
146 | 146 | } |
|
147 | 147 | |
|
148 | 148 | QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); |
|
149 | 149 | //initialize |
|
150 | 150 | if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); |
|
151 | 151 | m_axisItems.insert(axis, item); |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | void ChartPresenter::handleAxisRemoved(QAxis* axis) |
|
155 | 155 | { |
|
156 | 156 | ChartAxis* item = m_axisItems.take(axis); |
|
157 | 157 | Q_ASSERT(item); |
|
158 | 158 | if(m_animator) m_animator->removeAnimation(item); |
|
159 | 159 | delete item; |
|
160 | 160 | } |
|
161 | 161 | |
|
162 | 162 | |
|
163 | 163 | void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain) |
|
164 | 164 | { |
|
165 | 165 | Chart *item = series->d_ptr->createGraphics(this); |
|
166 | 166 | Q_ASSERT(item); |
|
167 | 167 | QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); |
|
168 | 168 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
169 | 169 | //initialize |
|
170 | 170 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
171 | 171 | if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); |
|
172 | 172 | m_chartItems.insert(series,item); |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series) |
|
176 | 176 | { |
|
177 | 177 | Chart* item = m_chartItems.take(series); |
|
178 | 178 | Q_ASSERT(item); |
|
179 | 179 | if(m_animator) { |
|
180 | 180 | //small hack to handle area animations |
|
181 | 181 | if(series->type() == QAbstractSeries::SeriesTypeArea){ |
|
182 | 182 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
183 | 183 | AreaChartItem* area = static_cast<AreaChartItem*>(item); |
|
184 | 184 | m_animator->removeAnimation(area->upperLineItem()); |
|
185 | 185 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); |
|
186 | 186 | }else |
|
187 | 187 | m_animator->removeAnimation(item); |
|
188 | 188 | } |
|
189 | 189 | delete item; |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) |
|
193 | 193 | { |
|
194 | 194 | if(m_chartTheme && m_chartTheme->id() == theme) return; |
|
195 | 195 | delete m_chartTheme; |
|
196 | 196 | m_chartTheme = ChartTheme::createTheme(theme); |
|
197 | 197 | m_chartTheme->setForced(force); |
|
198 | 198 | m_chartTheme->decorate(m_chart); |
|
199 | 199 | m_chartTheme->decorate(m_chart->legend()); |
|
200 | 200 | resetAllElements(); |
|
201 | 201 | |
|
202 | 202 | // We do not want "force" to stay on. |
|
203 | 203 | // Bar/pie are calling decorate when adding/removing slices/bars which means |
|
204 | 204 | // that to preserve users colors "force" must not be on. |
|
205 | 205 | m_chartTheme->setForced(false); |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | QChart::ChartTheme ChartPresenter::theme() |
|
209 | 209 | { |
|
210 | 210 | return m_chartTheme->id(); |
|
211 | 211 | } |
|
212 | 212 | |
|
213 | 213 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
214 | 214 | { |
|
215 | 215 | if(m_options!=options) { |
|
216 | 216 | |
|
217 | 217 | m_options=options; |
|
218 | 218 | |
|
219 | 219 | if(m_options!=QChart::NoAnimation && !m_animator) { |
|
220 | 220 | m_animator= new ChartAnimator(this); |
|
221 | 221 | } |
|
222 | 222 | resetAllElements(); |
|
223 | 223 | } |
|
224 | 224 | |
|
225 | 225 | } |
|
226 | 226 | |
|
227 | 227 | void ChartPresenter::resetAllElements() |
|
228 | 228 | { |
|
229 | 229 | QList<QAxis *> axisList = m_axisItems.uniqueKeys(); |
|
230 | 230 | QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys(); |
|
231 | 231 | |
|
232 | 232 | foreach(QAxis *axis, axisList) { |
|
233 | 233 | handleAxisRemoved(axis); |
|
234 | 234 | handleAxisAdded(axis,m_dataset->domain(axis)); |
|
235 | 235 | } |
|
236 | 236 | foreach(QAbstractSeries *series, seriesList) { |
|
237 | 237 | handleSeriesRemoved(series); |
|
238 | 238 | handleSeriesAdded(series,m_dataset->domain(series)); |
|
239 | 239 | // m_dataset->removeSeries(series); |
|
240 | 240 | // m_dataset->addSeries(series); |
|
241 | 241 | } |
|
242 | 242 | } |
|
243 | 243 | |
|
244 | 244 | void ChartPresenter::zoomIn(qreal factor) |
|
245 | 245 | { |
|
246 | 246 | QRectF rect = chartGeometry(); |
|
247 | 247 | rect.setWidth(rect.width()/factor); |
|
248 | 248 | rect.setHeight(rect.height()/factor); |
|
249 | 249 | rect.moveCenter(chartGeometry().center()); |
|
250 | 250 | zoomIn(rect); |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
254 | 254 | { |
|
255 | 255 | QRectF r = rect.normalized(); |
|
256 | 256 | r.translate(-m_chartMargins.topLeft()); |
|
257 | 257 | if (!r.isValid()) |
|
258 | 258 | return; |
|
259 | 259 | |
|
260 | 260 | m_state = ZoomInState; |
|
261 | 261 | m_statePoint = QPointF(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); |
|
262 | 262 | m_dataset->zoomInDomain(r,chartGeometry().size()); |
|
263 | 263 | m_state = ShowState; |
|
264 | 264 | } |
|
265 | 265 | |
|
266 | 266 | void ChartPresenter::zoomOut(qreal factor) |
|
267 | 267 | { |
|
268 | 268 | m_state = ZoomOutState; |
|
269 | 269 | |
|
270 | 270 | QRectF chartRect; |
|
271 | 271 | chartRect.setSize(chartGeometry().size()); |
|
272 | 272 | |
|
273 | 273 | QRectF rect; |
|
274 | 274 | rect.setSize(chartRect.size()/factor); |
|
275 | 275 | rect.moveCenter(chartRect.center()); |
|
276 | 276 | if (!rect.isValid()) |
|
277 | 277 | return; |
|
278 | 278 | m_statePoint = QPointF(rect.center().x()/chartGeometry().width(),rect.center().y()/chartGeometry().height()); |
|
279 | 279 | m_dataset->zoomOutDomain(rect, chartRect.size()); |
|
280 | 280 | m_state = ShowState; |
|
281 | 281 | } |
|
282 | 282 | |
|
283 | 283 | void ChartPresenter::scroll(qreal dx,qreal dy) |
|
284 | 284 | { |
|
285 | 285 | if(dx<0) m_state=ScrollLeftState; |
|
286 | 286 | if(dx>0) m_state=ScrollRightState; |
|
287 | 287 | if(dy<0) m_state=ScrollUpState; |
|
288 | 288 | if(dy>0) m_state=ScrollDownState; |
|
289 | 289 | |
|
290 | 290 | m_dataset->scrollDomain(dx,dy,chartGeometry().size()); |
|
291 | 291 | m_state = ShowState; |
|
292 | 292 | } |
|
293 | 293 | |
|
294 | 294 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
295 | 295 | { |
|
296 | 296 | return m_options; |
|
297 | 297 | } |
|
298 | 298 | |
|
299 | 299 | void ChartPresenter::updateLayout() |
|
300 | 300 | { |
|
301 | 301 | if (!m_rect.isValid()) return; |
|
302 | 302 | |
|
303 | 303 | // recalculate title size |
|
304 | 304 | |
|
305 | 305 | QSize titleSize; |
|
306 | 306 | int titlePadding=0; |
|
307 | 307 | |
|
308 | 308 | if (m_titleItem) { |
|
309 | 309 | titleSize= m_titleItem->boundingRect().size().toSize(); |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | //defaults |
|
313 | 313 | m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig)); |
|
314 | 314 | titlePadding = m_chartMargins.top()/2; |
|
315 | 315 | |
|
316 | 316 | QLegend* legend = m_chart->d_ptr->m_legend; |
|
317 | 317 | |
|
318 | 318 | // recalculate legend position |
|
319 | 319 | if (legend != 0 && legend->isAttachedToChart() && legend->isEnabled()) { |
|
320 | 320 | |
|
321 | 321 | QRect legendRect; |
|
322 | 322 | |
|
323 | 323 | // Reserve some space for legend |
|
324 | 324 | switch (legend->alignment()) { |
|
325 | 325 | |
|
326 |
case Q |
|
|
326 | case Qt::AlignTop: { | |
|
327 | 327 | int ledgendSize = legend->minHeight(); |
|
328 | 328 | int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny; |
|
329 | 329 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
330 | 330 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny)); |
|
331 | 331 | titlePadding = m_marginTiny + m_marginTiny; |
|
332 | 332 | break; |
|
333 | 333 | } |
|
334 |
case Q |
|
|
334 | case Qt::AlignBottom: { | |
|
335 | 335 | int ledgendSize = legend->minHeight(); |
|
336 | 336 | int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin; |
|
337 | 337 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding)); |
|
338 | 338 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall)); |
|
339 | 339 | titlePadding = m_chartMargins.top()/2; |
|
340 | 340 | break; |
|
341 | 341 | } |
|
342 |
case Q |
|
|
342 | case Qt::AlignLeft: { | |
|
343 | 343 | int ledgendSize = legend->minWidth(); |
|
344 | 344 | int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin; |
|
345 | 345 | m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
346 | 346 | m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom())); |
|
347 | 347 | titlePadding = m_chartMargins.top()/2; |
|
348 | 348 | break; |
|
349 | 349 | } |
|
350 |
case Q |
|
|
350 | case Qt::AlignRight: { | |
|
351 | 351 | int ledgendSize = legend->minWidth(); |
|
352 | 352 | int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny; |
|
353 | 353 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom())); |
|
354 | 354 | m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom())); |
|
355 | 355 | titlePadding = m_chartMargins.top()/2; |
|
356 | 356 | break; |
|
357 | 357 | } |
|
358 | 358 | default: { |
|
359 | 359 | break; |
|
360 | 360 | } |
|
361 | 361 | } |
|
362 | 362 | } |
|
363 | 363 | |
|
364 | 364 | if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom())) |
|
365 | 365 | { |
|
366 | 366 | m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom())); |
|
367 | 367 | return; |
|
368 | 368 | } |
|
369 | 369 | |
|
370 | 370 | |
|
371 | 371 | // recalculate title position |
|
372 | 372 | if (m_titleItem) { |
|
373 | 373 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
374 | 374 | m_titleItem->setPos(center.x(),titlePadding); |
|
375 | 375 | } |
|
376 | 376 | |
|
377 | 377 | //recalculate background gradient |
|
378 | 378 | if (m_backgroundItem) { |
|
379 | 379 | m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); |
|
380 | 380 | } |
|
381 | 381 | |
|
382 | 382 | |
|
383 | 383 | QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); |
|
384 | 384 | |
|
385 | 385 | legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); |
|
386 | 386 | |
|
387 | 387 | if(m_chartRect!=chartRect && chartRect.isValid()){ |
|
388 | 388 | m_chartRect=chartRect; |
|
389 | 389 | emit geometryChanged(m_chartRect); |
|
390 | 390 | } |
|
391 | 391 | |
|
392 | 392 | } |
|
393 | 393 | |
|
394 | 394 | void ChartPresenter::createChartBackgroundItem() |
|
395 | 395 | { |
|
396 | 396 | if (!m_backgroundItem) { |
|
397 | 397 | m_backgroundItem = new ChartBackground(rootItem()); |
|
398 | 398 | m_backgroundItem->setPen(Qt::NoPen); |
|
399 | 399 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
400 | 400 | } |
|
401 | 401 | } |
|
402 | 402 | |
|
403 | 403 | void ChartPresenter::createChartTitleItem() |
|
404 | 404 | { |
|
405 | 405 | if (!m_titleItem) { |
|
406 | 406 | m_titleItem = new QGraphicsSimpleTextItem(rootItem()); |
|
407 | 407 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
408 | 408 | } |
|
409 | 409 | } |
|
410 | 410 | |
|
411 | 411 | void ChartPresenter::handleAnimationFinished() |
|
412 | 412 | { |
|
413 | 413 | m_animations.removeAll(qobject_cast<ChartAnimation*>(sender())); |
|
414 | 414 | if(m_animations.empty()) emit animationsFinished(); |
|
415 | 415 | } |
|
416 | 416 | |
|
417 | 417 | void ChartPresenter::startAnimation(ChartAnimation* animation) |
|
418 | 418 | { |
|
419 | 419 | if (animation->state() != QAbstractAnimation::Stopped) animation->stop(); |
|
420 | 420 | QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection); |
|
421 | 421 | if(!m_animations.isEmpty()){ |
|
422 | 422 | m_animations.append(animation); |
|
423 | 423 | } |
|
424 | 424 | QTimer::singleShot(0, animation, SLOT(start())); |
|
425 | 425 | } |
|
426 | 426 | |
|
427 | 427 | #include "moc_chartpresenter_p.cpp" |
|
428 | 428 | |
|
429 | 429 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,476 +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 "qlegend.h" |
|
22 | 22 | #include "qlegend_p.h" |
|
23 | 23 | #include "qabstractseries.h" |
|
24 | 24 | #include "qabstractseries_p.h" |
|
25 | 25 | #include "qchart_p.h" |
|
26 | 26 | |
|
27 | 27 | #include "legendmarker_p.h" |
|
28 | 28 | #include "qxyseries.h" |
|
29 | 29 | #include "qlineseries.h" |
|
30 | 30 | #include "qareaseries.h" |
|
31 | 31 | #include "qscatterseries.h" |
|
32 | 32 | #include "qsplineseries.h" |
|
33 | 33 | #include "qbarseries.h" |
|
34 | 34 | #include "qstackedbarseries.h" |
|
35 | 35 | #include "qpercentbarseries.h" |
|
36 | 36 | #include "qbarset.h" |
|
37 | 37 | #include "qpieseries.h" |
|
38 | 38 | #include "qpieseries_p.h" |
|
39 | 39 | #include "qpieslice.h" |
|
40 | 40 | #include "chartpresenter_p.h" |
|
41 | 41 | #include <QPainter> |
|
42 | 42 | #include <QPen> |
|
43 | 43 | #include <QTimer> |
|
44 | 44 | |
|
45 | 45 | #include <QGraphicsSceneEvent> |
|
46 | 46 | |
|
47 | 47 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \class QLegend |
|
51 | 51 | \brief part of QtCommercial chart API. |
|
52 | 52 | \mainclass |
|
53 | 53 | |
|
54 | 54 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when |
|
55 | 55 | series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and |
|
56 | 56 | handle the drawing manually. |
|
57 | 57 | User isn't supposed to create or delete legend objects, but can reference it via QChart class. |
|
58 | 58 | |
|
59 | 59 | \image examples_percentbarchart_legend.png |
|
60 | 60 | |
|
61 | 61 | \sa QChart |
|
62 | 62 | */ |
|
63 | 63 | |
|
64 | 64 | /*! |
|
65 |
|
|
|
66 | ||
|
67 | This enum describes the possible position for legend inside chart. | |
|
68 | ||
|
69 | \value AlignmentTop | |
|
70 | \value AlignmentBottom | |
|
71 | \value AlignmentLeft | |
|
72 | \value AlignmentRight | |
|
73 | */ | |
|
65 | \fn void QLegend::alignmentChanged() | |
|
66 | Emitted when the alignment of the legend changes. | |
|
67 | */ | |
|
74 | 68 | |
|
75 | 69 | /*! |
|
76 | 70 | \fn qreal QLegend::minWidth() const |
|
77 | 71 | Returns minimum width of the legend |
|
78 | 72 | */ |
|
79 | 73 | |
|
80 | 74 | /*! |
|
81 | 75 | \fn qreal QLegend::minHeight() const |
|
82 | 76 | Returns minimum height of the legend |
|
83 | 77 | */ |
|
84 | 78 | |
|
85 | 79 | /*! |
|
86 | 80 | Constructs the legend object and sets the parent to \a parent |
|
87 | 81 | */ |
|
88 | 82 | |
|
89 | 83 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), |
|
90 | 84 | d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) |
|
91 | 85 | { |
|
92 | 86 | setZValue(ChartPresenter::LegendZValue); |
|
93 | 87 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
94 | 88 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); |
|
95 | 89 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*))); |
|
96 | 90 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); |
|
97 | 91 | } |
|
98 | 92 | |
|
99 | 93 | /*! |
|
100 | 94 | Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. |
|
101 | 95 | */ |
|
102 | 96 | QLegend::~QLegend() |
|
103 | 97 | { |
|
104 | 98 | } |
|
105 | 99 | |
|
106 | 100 | /*! |
|
107 | 101 | Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. |
|
108 | 102 | */ |
|
109 | 103 | |
|
110 | 104 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
111 | 105 | { |
|
112 | 106 | Q_UNUSED(option) |
|
113 | 107 | Q_UNUSED(widget) |
|
114 | 108 | if(!d_ptr->m_backgroundVisible) return; |
|
115 | 109 | |
|
116 | 110 | painter->setOpacity(opacity()); |
|
117 | 111 | painter->setPen(d_ptr->m_pen); |
|
118 | 112 | painter->setBrush(d_ptr->m_brush); |
|
119 | 113 | painter->drawRect(boundingRect()); |
|
120 | 114 | } |
|
121 | 115 | |
|
122 | 116 | /*! |
|
123 | 117 | Bounding rect of legend. |
|
124 | 118 | */ |
|
125 | 119 | |
|
126 | 120 | QRectF QLegend::boundingRect() const |
|
127 | 121 | { |
|
128 | 122 | return d_ptr->m_rect; |
|
129 | 123 | } |
|
130 | 124 | |
|
131 | 125 | /*! |
|
132 | 126 | Sets the \a brush of legend. Brush affects the background of legend. |
|
133 | 127 | */ |
|
134 | 128 | void QLegend::setBrush(const QBrush &brush) |
|
135 | 129 | { |
|
136 | 130 | if (d_ptr->m_brush != brush) { |
|
137 | 131 | d_ptr->m_brush = brush; |
|
138 | 132 | update(); |
|
139 | 133 | } |
|
140 | 134 | } |
|
141 | 135 | |
|
142 | 136 | /*! |
|
143 | 137 | Returns the brush used by legend. |
|
144 | 138 | */ |
|
145 | 139 | QBrush QLegend::brush() const |
|
146 | 140 | { |
|
147 | 141 | return d_ptr->m_brush; |
|
148 | 142 | } |
|
149 | 143 | |
|
150 | 144 | /*! |
|
151 | 145 | Sets the \a pen of legend. Pen affects the legend borders. |
|
152 | 146 | */ |
|
153 | 147 | void QLegend::setPen(const QPen &pen) |
|
154 | 148 | { |
|
155 | 149 | if (d_ptr->m_pen != pen) { |
|
156 | 150 | d_ptr->m_pen = pen; |
|
157 | 151 | update(); |
|
158 | 152 | } |
|
159 | 153 | } |
|
160 | 154 | |
|
161 | 155 | /*! |
|
162 | 156 | Returns the pen used by legend |
|
163 | 157 | */ |
|
164 | 158 | |
|
165 | 159 | QPen QLegend::pen() const |
|
166 | 160 | { |
|
167 | 161 | return d_ptr->m_pen; |
|
168 | 162 | } |
|
169 | 163 | |
|
170 | 164 | /*! |
|
171 | Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart. | |
|
172 | \sa QLegend::Alignment | |
|
165 | \property QLegend::alignment | |
|
166 | \brief The alignment of the legend. | |
|
167 | */ | |
|
168 | ||
|
169 | /*! | |
|
170 | Sets the \a alignment for legend. Legend paints on the defined position in chart. The following alignments are | |
|
171 | supported: Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result | |
|
172 | is undefined. | |
|
173 | ||
|
174 | \sa QLegend::Alignment | |
|
173 | 175 | */ |
|
174 |
void QLegend::setAlignment(Q |
|
|
176 | void QLegend::setAlignment(Qt::Alignment alignment) | |
|
175 | 177 | { |
|
176 | 178 | if(d_ptr->m_alignment!=alignment) { |
|
177 | 179 | d_ptr->m_alignment = alignment; |
|
178 | 180 | d_ptr->updateLayout(); |
|
181 | alignmentChanged(); | |
|
179 | 182 | } |
|
180 | 183 | } |
|
181 | 184 | |
|
182 | 185 | /*! |
|
183 | 186 | Returns the preferred layout for legend |
|
184 | 187 | */ |
|
185 |
Q |
|
|
188 | Qt::Alignment QLegend::alignment() const | |
|
186 | 189 | { |
|
187 | 190 | return d_ptr->m_alignment; |
|
188 | 191 | } |
|
189 | 192 | |
|
190 | 193 | /*! |
|
191 | 194 | Detaches the legend from chart. Chart won't change layout of the legend. |
|
192 | 195 | */ |
|
193 | 196 | void QLegend::detachFromChart() |
|
194 | 197 | { |
|
195 | 198 | d_ptr->m_attachedToChart = false; |
|
196 | 199 | } |
|
197 | 200 | |
|
198 | 201 | /*! |
|
199 | 202 | Attaches the legend to chart. Chart may change layout of the legend. |
|
200 | 203 | */ |
|
201 | 204 | void QLegend::attachToChart() |
|
202 | 205 | { |
|
203 | 206 | d_ptr->attachToChart(); |
|
204 | 207 | } |
|
205 | 208 | |
|
206 | 209 | /*! |
|
207 | 210 | Returns true, if legend is attached to chart. |
|
208 | 211 | */ |
|
209 | 212 | bool QLegend::isAttachedToChart() |
|
210 | 213 | { |
|
211 | 214 | return d_ptr->m_attachedToChart; |
|
212 | 215 | } |
|
213 | 216 | |
|
214 | 217 | /*! |
|
215 | 218 | Sets the legend's scrolling offset to value defined by \a point. |
|
216 | 219 | */ |
|
217 | 220 | void QLegend::setOffset(const QPointF& point) |
|
218 | 221 | { |
|
219 | 222 | d_ptr->setOffset(point.x(),point.y()); |
|
220 | 223 | } |
|
221 | 224 | |
|
222 | 225 | /*! |
|
223 | 226 | Returns the legend's scrolling offset. |
|
224 | 227 | */ |
|
225 | 228 | QPointF QLegend::offset() const |
|
226 | 229 | { |
|
227 | 230 | return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY); |
|
228 | 231 | } |
|
229 | 232 | |
|
230 | 233 | /*! |
|
231 | 234 | Sets the visibility of legend background to \a visible |
|
232 | 235 | */ |
|
233 | 236 | void QLegend::setBackgroundVisible(bool visible) |
|
234 | 237 | { |
|
235 | 238 | if(d_ptr->m_backgroundVisible!=visible) |
|
236 | 239 | { |
|
237 | 240 | d_ptr->m_backgroundVisible=visible; |
|
238 | 241 | update(); |
|
239 | 242 | } |
|
240 | 243 | } |
|
241 | 244 | |
|
242 | 245 | /*! |
|
243 | 246 | Returns the visibility of legend background |
|
244 | 247 | */ |
|
245 | 248 | bool QLegend::isBackgroundVisible() const |
|
246 | 249 | { |
|
247 | 250 | return d_ptr->m_backgroundVisible; |
|
248 | 251 | } |
|
249 | 252 | |
|
250 | 253 | /*! |
|
251 | 254 | \internal \a event see QGraphicsWidget for details |
|
252 | 255 | */ |
|
253 | 256 | void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
254 | 257 | { |
|
255 | 258 | const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); |
|
256 | 259 | QGraphicsWidget::resizeEvent(event); |
|
257 | 260 | if(d_ptr->m_rect != rect) { |
|
258 | 261 | d_ptr->m_rect = rect; |
|
259 | 262 | d_ptr->updateLayout(); |
|
260 | 263 | } |
|
261 | 264 | } |
|
262 | 265 | |
|
263 | 266 | /*! |
|
264 | 267 | \internal \a event see QGraphicsWidget for details |
|
265 | 268 | */ |
|
266 | 269 | void QLegend::hideEvent(QHideEvent *event) |
|
267 | 270 | { |
|
268 | 271 | QGraphicsWidget::hideEvent(event); |
|
269 | 272 | setEnabled(false); |
|
270 | 273 | d_ptr->updateLayout(); |
|
271 | 274 | } |
|
272 | 275 | |
|
273 | 276 | /*! |
|
274 | 277 | \internal \a event see QGraphicsWidget for details |
|
275 | 278 | */ |
|
276 | 279 | void QLegend::showEvent(QShowEvent *event) |
|
277 | 280 | { |
|
278 | 281 | QGraphicsWidget::showEvent(event); |
|
279 | 282 | setEnabled(true); |
|
280 | 283 | d_ptr->updateLayout(); |
|
281 | 284 | } |
|
282 | 285 | |
|
283 | 286 | qreal QLegend::minWidth() const |
|
284 | 287 | { |
|
285 | 288 | return d_ptr->m_minWidth; |
|
286 | 289 | } |
|
287 | 290 | |
|
288 | 291 | qreal QLegend::minHeight() const |
|
289 | 292 | { |
|
290 | 293 | return d_ptr->m_minHeight; |
|
291 | 294 | } |
|
292 | 295 | |
|
293 | 296 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
294 | 297 | |
|
295 | 298 | QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q): |
|
296 | 299 | q_ptr(q), |
|
297 | 300 | m_presenter(presenter), |
|
298 | 301 | m_chart(chart), |
|
299 | 302 | m_markers(new QGraphicsItemGroup(q)), |
|
300 |
m_alignment(Q |
|
|
303 | m_alignment(Qt::AlignTop), | |
|
301 | 304 | m_offsetX(0), |
|
302 | 305 | m_offsetY(0), |
|
303 | 306 | m_minWidth(0), |
|
304 | 307 | m_minHeight(0), |
|
305 | 308 | m_width(0), |
|
306 | 309 | m_height(0), |
|
307 | 310 | m_attachedToChart(true), |
|
308 | 311 | m_backgroundVisible(false) |
|
309 | 312 | { |
|
310 | 313 | |
|
311 | 314 | } |
|
312 | 315 | |
|
313 | 316 | QLegendPrivate::~QLegendPrivate() |
|
314 | 317 | { |
|
315 | 318 | |
|
316 | 319 | } |
|
317 | 320 | |
|
318 | 321 | void QLegendPrivate::setOffset(qreal x, qreal y) |
|
319 | 322 | { |
|
320 | 323 | |
|
321 | 324 | switch(m_alignment) { |
|
322 | 325 | |
|
323 |
case Q |
|
|
324 |
case Q |
|
|
326 | case Qt::AlignTop: | |
|
327 | case Qt::AlignBottom: { | |
|
325 | 328 | if(m_width<=m_rect.width()) return; |
|
326 | 329 | |
|
327 | 330 | if (x != m_offsetX) { |
|
328 | 331 | m_offsetX = qBound(qreal(0), x, m_width - m_rect.width()); |
|
329 | 332 | m_markers->setPos(-m_offsetX,m_rect.top()); |
|
330 | 333 | } |
|
331 | 334 | break; |
|
332 | 335 | } |
|
333 |
case Q |
|
|
334 |
case Q |
|
|
336 | case Qt::AlignLeft: | |
|
337 | case Qt::AlignRight: { | |
|
335 | 338 | |
|
336 | 339 | if(m_height<=m_rect.height()) return; |
|
337 | 340 | |
|
338 | 341 | if (y != m_offsetY) { |
|
339 | 342 | m_offsetY = qBound(qreal(0), y, m_height - m_rect.height()); |
|
340 | 343 | m_markers->setPos(m_rect.left(),-m_offsetY); |
|
341 | 344 | } |
|
342 | 345 | break; |
|
343 | 346 | } |
|
344 | 347 | } |
|
345 | 348 | } |
|
346 | 349 | |
|
347 | 350 | |
|
348 | 351 | void QLegendPrivate::updateLayout() |
|
349 | 352 | { |
|
350 | 353 | m_offsetX=0; |
|
351 | 354 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
352 | 355 | |
|
353 | 356 | if(items.isEmpty()) return; |
|
354 | 357 | |
|
355 | 358 | m_minWidth=0; |
|
356 | 359 | m_minHeight=0; |
|
357 | 360 | |
|
358 | 361 | switch(m_alignment) { |
|
359 | 362 | |
|
360 |
case Q |
|
|
361 |
case Q |
|
|
363 | case Qt::AlignTop: | |
|
364 | case Qt::AlignBottom: { | |
|
362 | 365 | QPointF point = m_rect.topLeft(); |
|
363 | 366 | m_width = 0; |
|
364 | 367 | foreach (QGraphicsItem *item, items) { |
|
365 | 368 | item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2); |
|
366 | 369 | const QRectF& rect = item->boundingRect(); |
|
367 | 370 | qreal w = rect.width(); |
|
368 | 371 | m_minWidth=qMax(m_minWidth,w); |
|
369 | 372 | m_minHeight=qMax(m_minHeight,rect.height()); |
|
370 | 373 | m_width+=w; |
|
371 | 374 | point.setX(point.x() + w); |
|
372 | 375 | } |
|
373 | 376 | if(m_width<m_rect.width()) { |
|
374 | 377 | m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); |
|
375 | 378 | } |
|
376 | 379 | else { |
|
377 | 380 | m_markers->setPos(m_rect.topLeft()); |
|
378 | 381 | } |
|
379 | 382 | m_height=m_minHeight; |
|
380 | 383 | } |
|
381 | 384 | break; |
|
382 |
case Q |
|
|
383 |
case Q |
|
|
385 | case Qt::AlignLeft: | |
|
386 | case Qt::AlignRight: { | |
|
384 | 387 | QPointF point = m_rect.topLeft(); |
|
385 | 388 | m_height = 0; |
|
386 | 389 | foreach (QGraphicsItem *item, items) { |
|
387 | 390 | item->setPos(point); |
|
388 | 391 | const QRectF& rect = item->boundingRect(); |
|
389 | 392 | qreal h = rect.height(); |
|
390 | 393 | m_minWidth=qMax(m_minWidth,rect.width()); |
|
391 | 394 | m_minHeight=qMax(m_minHeight,h); |
|
392 | 395 | m_height+=h; |
|
393 | 396 | point.setY(point.y() + h); |
|
394 | 397 | } |
|
395 | 398 | if(m_height<m_rect.height()) { |
|
396 | 399 | m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); |
|
397 | 400 | } |
|
398 | 401 | else { |
|
399 | 402 | m_markers->setPos(m_rect.topLeft()); |
|
400 | 403 | } |
|
401 | 404 | m_width=m_minWidth; |
|
402 | 405 | } |
|
403 | 406 | break; |
|
404 | 407 | } |
|
405 | 408 | |
|
406 | 409 | if (m_attachedToChart) { |
|
407 | 410 | m_presenter->updateLayout(); |
|
408 | 411 | } |
|
409 | 412 | } |
|
410 | 413 | |
|
411 | 414 | void QLegendPrivate::attachToChart() |
|
412 | 415 | { |
|
413 | 416 | m_attachedToChart = true; |
|
414 | 417 | q_ptr->setParent(m_chart); |
|
415 | 418 | } |
|
416 | 419 | |
|
417 | 420 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) |
|
418 | 421 | { |
|
419 | 422 | Q_UNUSED(domain) |
|
420 | 423 | |
|
421 | 424 | QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr); |
|
422 | 425 | foreach(LegendMarker* marker, markers) |
|
423 | 426 | m_markers->addToGroup(marker); |
|
424 | 427 | |
|
425 | 428 | if(series->type() == QAbstractSeries::SeriesTypePie) { |
|
426 | 429 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
427 | 430 | QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
428 | 431 | QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
429 | 432 | } |
|
430 | 433 | |
|
431 | 434 | updateLayout(); |
|
432 | 435 | } |
|
433 | 436 | |
|
434 | 437 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) |
|
435 | 438 | { |
|
436 | 439 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
437 | 440 | |
|
438 | 441 | foreach (QGraphicsItem *markers, items) { |
|
439 | 442 | LegendMarker *marker = static_cast<LegendMarker*>(markers); |
|
440 | 443 | if (marker->series() == series) { |
|
441 | 444 | delete marker; |
|
442 | 445 | } |
|
443 | 446 | } |
|
444 | 447 | |
|
445 | 448 | if(series->type() == QAbstractSeries::SeriesTypePie) |
|
446 | 449 | { |
|
447 | 450 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
448 | 451 | QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
449 | 452 | QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
450 | 453 | } |
|
451 | 454 | |
|
452 | 455 | updateLayout(); |
|
453 | 456 | } |
|
454 | 457 | |
|
455 | 458 | void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series) |
|
456 | 459 | { |
|
457 | 460 | // TODO: find out which markers are are added or removed. Update them |
|
458 | 461 | // TODO: better implementation |
|
459 | 462 | handleSeriesRemoved(series); |
|
460 | 463 | Domain domain; |
|
461 | 464 | handleSeriesAdded(series, &domain); |
|
462 | 465 | } |
|
463 | 466 | |
|
464 | 467 | void QLegendPrivate::handleUpdatePieSeries() |
|
465 | 468 | { |
|
466 | 469 | //TODO: reimplement to be optimal |
|
467 | 470 | QPieSeries* series = qobject_cast<QPieSeries *> (sender()); |
|
468 | 471 | Q_ASSERT(series); |
|
469 | 472 | handleSeriesRemoved(series); |
|
470 | 473 | handleSeriesAdded(series, 0); |
|
471 | 474 | } |
|
472 | 475 | |
|
473 | 476 | #include "moc_qlegend.cpp" |
|
474 | 477 | #include "moc_qlegend_p.cpp" |
|
475 | 478 | |
|
476 | 479 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,102 +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 | #ifndef QLEGEND_H |
|
22 | 22 | #define QLEGEND_H |
|
23 | 23 | |
|
24 | 24 | #include <QChartGlobal> |
|
25 | 25 | #include <QGraphicsWidget> |
|
26 | 26 | #include <QPen> |
|
27 | 27 | #include <QBrush> |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 30 | |
|
31 | 31 | class Domain; |
|
32 | 32 | class LegendMarker; |
|
33 | 33 | class QPieSlice; |
|
34 | 34 | class QXYSeries; |
|
35 | 35 | class QBarSet; |
|
36 | 36 | class QBarSeries; |
|
37 | 37 | class QPieSeries; |
|
38 | 38 | class QAreaSeries; |
|
39 | 39 | class LegendScrollButton; |
|
40 | 40 | class QChart; |
|
41 | 41 | class QLegendPrivate; |
|
42 | 42 | |
|
43 | 43 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget |
|
44 | 44 | { |
|
45 | 45 | Q_OBJECT |
|
46 | public: | |
|
47 | ||
|
48 | // We only support these alignments (for now) | |
|
49 | enum Alignment { | |
|
50 | AlignmentTop = Qt::AlignTop, | |
|
51 | AlignmentBottom = Qt::AlignBottom, | |
|
52 | AlignmentLeft = Qt::AlignLeft, | |
|
53 | AlignmentRight = Qt::AlignRight | |
|
54 | }; | |
|
55 | ||
|
56 | Q_DECLARE_FLAGS(Alignments, Alignment) | |
|
46 | Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) | |
|
57 | 47 | |
|
58 | 48 | private: |
|
59 | 49 | explicit QLegend(QChart *chart); |
|
60 | 50 | |
|
61 | 51 | public: |
|
62 | 52 | ~QLegend(); |
|
63 | 53 | |
|
64 | 54 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
65 | 55 | QRectF boundingRect() const; |
|
66 | 56 | |
|
67 | 57 | void setBrush(const QBrush &brush); |
|
68 | 58 | QBrush brush() const; |
|
69 | 59 | |
|
70 | 60 | void setPen(const QPen &pen); |
|
71 | 61 | QPen pen() const; |
|
72 | 62 | |
|
73 |
void setAlignment(Q |
|
|
74 |
Q |
|
|
63 | void setAlignment(Qt::Alignment alignment); | |
|
64 | Qt::Alignment alignment() const; | |
|
75 | 65 | |
|
76 | 66 | void detachFromChart(); |
|
77 | 67 | void attachToChart(); |
|
78 | 68 | bool isAttachedToChart(); |
|
79 | 69 | |
|
80 | 70 | qreal minWidth() const; |
|
81 | 71 | qreal minHeight() const; |
|
82 | 72 | |
|
83 | 73 | void setBackgroundVisible(bool visible = true); |
|
84 | 74 | bool isBackgroundVisible() const; |
|
85 | 75 | |
|
86 | 76 | void setOffset(const QPointF& point); |
|
87 | 77 | QPointF offset() const; |
|
88 | 78 | |
|
89 | 79 | protected: |
|
90 | 80 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
91 | 81 | void hideEvent(QHideEvent *event); |
|
92 | 82 | void showEvent(QShowEvent *event); |
|
93 | 83 | |
|
84 | Q_SIGNALS: | |
|
85 | void alignmentChanged(); | |
|
86 | ||
|
94 | 87 | private: |
|
95 | 88 | QScopedPointer<QLegendPrivate> d_ptr; |
|
96 | 89 | Q_DISABLE_COPY(QLegend); |
|
97 | 90 | friend class LegendScroller; |
|
98 | 91 | }; |
|
99 | 92 | |
|
100 | 93 | QTCOMMERCIALCHART_END_NAMESPACE |
|
101 | 94 | |
|
102 | 95 | #endif // QLEGEND_H |
@@ -1,82 +1,82 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | // W A R N I N G |
|
22 | 22 | // ------------- |
|
23 | 23 | // |
|
24 | 24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
25 | 25 | // implementation detail. This header file may change from version to |
|
26 | 26 | // version without notice, or even be removed. |
|
27 | 27 | // |
|
28 | 28 | // We mean it. |
|
29 | 29 | |
|
30 | 30 | #ifndef QLEGEND_P_H |
|
31 | 31 | #define QLEGEND_P_H |
|
32 | 32 | |
|
33 | 33 | #include "qlegend.h" |
|
34 | 34 | |
|
35 | 35 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
36 | 36 | |
|
37 | 37 | class QChart; |
|
38 | 38 | class ChartPresenter; |
|
39 | 39 | class QAbstractSeries; |
|
40 | 40 | |
|
41 | 41 | class QLegendPrivate : public QObject |
|
42 | 42 | { |
|
43 | 43 | Q_OBJECT |
|
44 | 44 | public: |
|
45 | 45 | QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q); |
|
46 | 46 | ~QLegendPrivate(); |
|
47 | 47 | |
|
48 | 48 | void setOffset(qreal x, qreal y); |
|
49 | 49 | void updateLayout(); |
|
50 | 50 | void attachToChart(); |
|
51 | 51 | |
|
52 | 52 | public Q_SLOTS: |
|
53 | 53 | void handleSeriesAdded(QAbstractSeries *series, Domain *domain); |
|
54 | 54 | void handleSeriesRemoved(QAbstractSeries *series); |
|
55 | 55 | void handleSeriesUpdated(QAbstractSeries *series); |
|
56 | 56 | void handleUpdatePieSeries(); //TODO remove this function |
|
57 | 57 | |
|
58 | 58 | private: |
|
59 | 59 | QLegend *q_ptr; |
|
60 | 60 | ChartPresenter *m_presenter; |
|
61 | 61 | QChart* m_chart; |
|
62 | 62 | QGraphicsItemGroup* m_markers; |
|
63 |
Q |
|
|
63 | Qt::Alignment m_alignment; | |
|
64 | 64 | QBrush m_brush; |
|
65 | 65 | QPen m_pen; |
|
66 | 66 | QRectF m_rect; |
|
67 | 67 | qreal m_offsetX; |
|
68 | 68 | qreal m_offsetY; |
|
69 | 69 | qreal m_minWidth; |
|
70 | 70 | qreal m_minHeight; |
|
71 | 71 | qreal m_width; |
|
72 | 72 | qreal m_height; |
|
73 | 73 | bool m_attachedToChart; |
|
74 | 74 | bool m_backgroundVisible; |
|
75 | 75 | |
|
76 | 76 | friend class QLegend; |
|
77 | 77 | |
|
78 | 78 | }; |
|
79 | 79 | |
|
80 | 80 | QTCOMMERCIALCHART_END_NAMESPACE |
|
81 | 81 | |
|
82 | 82 | #endif |
@@ -1,53 +1,40 | |||
|
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 | import QtQuick 1.0 |
|
22 | 22 | import QtCommercial.Chart 1.0 |
|
23 | 23 | |
|
24 | Rectangle { | |
|
24 | ChartView { | |
|
25 | id: chartView | |
|
26 | title: "chart" | |
|
25 | 27 | anchors.fill: parent |
|
28 | property variant series: chartView | |
|
26 | 29 | |
|
27 | ChartView { | |
|
28 |
|
|
|
29 | anchors.fill: parent | |
|
30 | ||
|
31 | SplineSeries { | |
|
32 | id: daSeries | |
|
33 | name: "spline 1" | |
|
34 |
|
|
|
35 |
|
|
|
36 | XyPoint { x: 1.9; y: 3.3 } | |
|
37 | XyPoint { x: 2.1; y: 2.1 } | |
|
38 | XyPoint { x: 2.9; y: 4.9 } | |
|
39 | XyPoint { x: 3.4; y: 3.0 } | |
|
40 | XyPoint { x: 4.1; y: 3.3 } | |
|
41 | } | |
|
42 | ||
|
43 | SplineSeries { | |
|
44 | name: "spline 2" | |
|
45 | XyPoint { x: 1.1; y: 1.1 } | |
|
46 | XyPoint { x: 1.9; y: 2.3 } | |
|
47 | XyPoint { x: 2.1; y: 1.1 } | |
|
48 | XyPoint { x: 2.9; y: 3.9 } | |
|
49 | XyPoint { x: 3.4; y: 2.0 } | |
|
50 | XyPoint { x: 4.1; y: 2.3 } | |
|
51 | } | |
|
30 | LineSeries { | |
|
31 | name: "line" | |
|
32 | XyPoint { x: 0; y: 0 } | |
|
33 | XyPoint { x: 1.1; y: 2.1 } | |
|
34 | XyPoint { x: 1.9; y: 3.3 } | |
|
35 | XyPoint { x: 2.1; y: 2.1 } | |
|
36 | XyPoint { x: 2.9; y: 4.9 } | |
|
37 | XyPoint { x: 3.4; y: 3.0 } | |
|
38 | XyPoint { x: 4.1; y: 3.3 } | |
|
52 | 39 | } |
|
53 | 40 | } |
@@ -1,125 +1,128 | |||
|
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 | import QtQuick 1.0 |
|
22 | 22 | import QtCommercial.Chart 1.0 |
|
23 | 23 | |
|
24 | 24 | Rectangle { |
|
25 | 25 | id: main |
|
26 | 26 | width: parent.width |
|
27 | 27 | height: parent.height |
|
28 | 28 | property int viewNumber: 0 |
|
29 |
property int viewCount: |
|
|
29 | property int viewCount: 10 | |
|
30 | 30 | property variant colors: ["#637D74", "#403D3A", "#8C3B3B", "#AB6937", "#D4A960"] |
|
31 | 31 | property int colorIndex: 0 |
|
32 | 32 | |
|
33 | 33 | function nextColor() { |
|
34 | 34 | colorIndex++; |
|
35 | 35 | console.log("color: " + colors[colorIndex % colors.length]); |
|
36 | 36 | return colors[colorIndex % colors.length]; |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | onViewNumberChanged: { |
|
40 | 40 | if (viewNumber == 0) { |
|
41 | chartLoader.source = "Chart.qml"; | |
|
42 | editorLoader.source = "ChartEditor.qml"; | |
|
43 | } else if (viewNumber == 1) { | |
|
41 | 44 | chartLoader.source = "PieChart.qml"; |
|
42 | 45 | editorLoader.source = "PieEditor.qml"; |
|
43 |
} else if (viewNumber == |
|
|
46 | } else if (viewNumber == 2) { | |
|
44 | 47 | chartLoader.source = "LineChart.qml"; |
|
45 | 48 | editorLoader.source = "LineEditor.qml"; |
|
46 |
} else if (viewNumber == |
|
|
49 | } else if (viewNumber == 3) { | |
|
47 | 50 | chartLoader.source = "SplineChart.qml"; |
|
48 | 51 | editorLoader.source = "LineEditor.qml"; |
|
49 |
} else if (viewNumber == |
|
|
52 | } else if (viewNumber == 4) { | |
|
50 | 53 | chartLoader.source = "ScatterChart.qml"; |
|
51 | 54 | editorLoader.source = "ScatterEditor.qml"; |
|
52 |
} else if (viewNumber == |
|
|
55 | } else if (viewNumber == 5) { | |
|
53 | 56 | chartLoader.source = "AreaChart.qml"; |
|
54 | 57 | editorLoader.source = "AreaEditor.qml"; |
|
55 |
} else if (viewNumber == |
|
|
58 | } else if (viewNumber == 6) { | |
|
56 | 59 | chartLoader.source = "BarChart.qml"; |
|
57 | 60 | editorLoader.source = "BarEditor.qml"; |
|
58 |
} else if (viewNumber == |
|
|
61 | } else if (viewNumber == 7) { | |
|
59 | 62 | chartLoader.source = "GroupedBarChart.qml"; |
|
60 | 63 | editorLoader.source = "BarEditor.qml"; |
|
61 |
} else if (viewNumber == |
|
|
64 | } else if (viewNumber == 8) { | |
|
62 | 65 | chartLoader.source = "StackedBarChart.qml"; |
|
63 | 66 | editorLoader.source = "BarEditor.qml"; |
|
64 |
} else if (viewNumber == |
|
|
67 | } else if (viewNumber == 9) { | |
|
65 | 68 | chartLoader.source = "PercentBarChart.qml"; |
|
66 | 69 | editorLoader.source = "BarEditor.qml"; |
|
67 | 70 | } else { |
|
68 | 71 | console.log("Illegal view number"); |
|
69 | 72 | } |
|
70 | 73 | } |
|
71 | 74 | |
|
72 | 75 | Row { |
|
73 | 76 | anchors.top: parent.top |
|
74 | 77 | anchors.bottom: buttonRow.top |
|
75 | 78 | anchors.bottomMargin: 10 |
|
76 | 79 | anchors.left: parent.left |
|
77 | 80 | anchors.right: parent.right |
|
78 | 81 | |
|
79 | 82 | Loader { |
|
80 | 83 | id: chartLoader |
|
81 | 84 | width: main.width - editorLoader.width |
|
82 | 85 | height: parent.height |
|
83 |
source: " |
|
|
86 | source: "Chart.qml" | |
|
84 | 87 | onStatusChanged: { |
|
85 | 88 | console.log("chartLoader.status: " + status + " " + source); |
|
86 | 89 | if (status == Loader.Ready && editorLoader.status == Loader.Ready) |
|
87 | 90 | editorLoader.item.series = chartLoader.item.series; |
|
88 | 91 | } |
|
89 | 92 | } |
|
90 | 93 | |
|
91 | 94 | Loader { |
|
92 | 95 | id: editorLoader |
|
93 | 96 | width: 280 |
|
94 | 97 | height: parent.height |
|
95 |
source: " |
|
|
98 | source: "ChartEditor.qml" | |
|
96 | 99 | onStatusChanged: { |
|
97 | 100 | console.log("editorLoader.status: " + status + " " + source); |
|
98 | 101 | if (status == Loader.Ready && chartLoader.status == Loader.Ready) |
|
99 | 102 | editorLoader.item.series = chartLoader.item.series; |
|
100 | 103 | } |
|
101 | 104 | } |
|
102 | 105 | } |
|
103 | 106 | |
|
104 | 107 | Row { |
|
105 | 108 | id: buttonRow |
|
106 | 109 | height: 40 |
|
107 | 110 | anchors.bottom: parent.bottom |
|
108 | 111 | anchors.horizontalCenter: parent.horizontalCenter |
|
109 | 112 | spacing: 10 |
|
110 | 113 | |
|
111 | 114 | Button { |
|
112 | 115 | text: "previous" |
|
113 | 116 | onClicked: { |
|
114 | 117 | viewNumber = (viewNumber + viewCount - 1) % viewCount; |
|
115 | 118 | } |
|
116 | 119 | } |
|
117 | 120 | |
|
118 | 121 | Button { |
|
119 | 122 | text: "next" |
|
120 | 123 | onClicked: { |
|
121 | 124 | viewNumber = (viewNumber + 1) % viewCount; |
|
122 | 125 | } |
|
123 | 126 | } |
|
124 | 127 | } |
|
125 | 128 | } |
@@ -1,21 +1,23 | |||
|
1 | 1 | <RCC> |
|
2 | 2 | <qresource prefix="/"> |
|
3 | 3 | <file>qml/qmlchartproperties/loader.qml</file> |
|
4 | 4 | <file>qml/qmlchartproperties/main.qml</file> |
|
5 | 5 | <file>qml/qmlchartproperties/Button.qml</file> |
|
6 | 6 | <file>qml/qmlchartproperties/PieChart.qml</file> |
|
7 | 7 | <file>qml/qmlchartproperties/PieEditor.qml</file> |
|
8 | 8 | <file>qml/qmlchartproperties/LineChart.qml</file> |
|
9 | 9 | <file>qml/qmlchartproperties/LineEditor.qml</file> |
|
10 | 10 | <file>qml/qmlchartproperties/SplineChart.qml</file> |
|
11 | 11 | <file>qml/qmlchartproperties/ScatterChart.qml</file> |
|
12 | 12 | <file>qml/qmlchartproperties/AreaChart.qml</file> |
|
13 | 13 | <file>qml/qmlchartproperties/BarChart.qml</file> |
|
14 | 14 | <file>qml/qmlchartproperties/BarEditor.qml</file> |
|
15 | 15 | <file>qml/qmlchartproperties/ScatterEditor.qml</file> |
|
16 | 16 | <file>qml/qmlchartproperties/AreaEditor.qml</file> |
|
17 | 17 | <file>qml/qmlchartproperties/GroupedBarChart.qml</file> |
|
18 | 18 | <file>qml/qmlchartproperties/StackedBarChart.qml</file> |
|
19 | 19 | <file>qml/qmlchartproperties/PercentBarChart.qml</file> |
|
20 | <file>qml/qmlchartproperties/Chart.qml</file> | |
|
21 | <file>qml/qmlchartproperties/ChartEditor.qml</file> | |
|
20 | 22 | </qresource> |
|
21 | 23 | </RCC> |
General Comments 0
You need to be logged in to leave comments.
Login now