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