##// END OF EJS Templates
Updated spline nad legend example
Marek Rosa -
r1585:2ba1d7112404
parent child
Show More
@@ -1,64 +1,65
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 "chart.h"
21 #include "chart.h"
22 #include <QAbstractAxis>
22 #include <QAbstractAxis>
23 #include <QSplineSeries>
23 #include <QSplineSeries>
24 #include <QTime>
24 #include <QTime>
25
25
26 Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
26 Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 :QChart(parent, wFlags),
27 :QChart(parent, wFlags),
28 m_step(1),
28 m_step(1),
29 m_x(0),
29 m_x(0),
30 m_y(1)
30 m_y(1)
31 {
31 {
32 qsrand((uint) QTime::currentTime().msec());
32 qsrand((uint) QTime::currentTime().msec());
33
33
34 QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
34 QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
35 m_timer.setInterval(1000);
35 m_timer.setInterval(1000);
36
36
37 m_series = new QSplineSeries(this);
37 m_series = new QSplineSeries(this);
38 QPen green(Qt::red);
38 QPen green(Qt::red);
39 green.setWidth(3);
39 green.setWidth(3);
40 m_series->setPen(green);
40 m_series->setPen(green);
41 m_series->append(m_x, m_y);
41 m_series->append(m_x, m_y);
42
42
43 addSeries(m_series);
43 addSeries(m_series);
44 createDefaultAxes();
44
45
45 axisY()->setRange(-5, 5);
46 axisY()->setRange(-5, 5);
46 axisX()->setRange(-9, 1);
47 axisX()->setRange(-9, 1);
47 //TODO:axisX()->setTicksCount(11);
48 //TODO:axisX()->setTicksCount(11);
48
49
49 m_timer.start();
50 m_timer.start();
50 }
51 }
51
52
52 Chart::~Chart()
53 Chart::~Chart()
53 {
54 {
54
55
55 }
56 }
56
57
57 void Chart::handleTimeout()
58 void Chart::handleTimeout()
58 {
59 {
59 m_x += m_step;
60 m_x += m_step;
60 m_y = qrand() % 5 - 2.5;
61 m_y = qrand() % 5 - 2.5;
61 m_series->append(m_x, m_y);
62 m_series->append(m_x, m_y);
62 scroll(10,0);
63 scroll(10,0);
63 if(m_x==100) m_timer.stop();
64 if(m_x==100) m_timer.stop();
64 }
65 }
@@ -1,261 +1,262
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 "mainwidget.h"
21 #include "mainwidget.h"
22 #include <QChart>
22 #include <QChart>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QLabel>
25 #include <QLabel>
26 #include <QDebug>
26 #include <QDebug>
27 #include <QBarSet>
27 #include <QBarSet>
28 #include <QGroupedBarSeries>
28 #include <QGroupedBarSeries>
29 #include <QLegend>
29 #include <QLegend>
30 #include <QFormLayout>
30 #include <QFormLayout>
31
31
32 QTCOMMERCIALCHART_USE_NAMESPACE
32 QTCOMMERCIALCHART_USE_NAMESPACE
33
33
34 MainWidget::MainWidget(QWidget *parent) :
34 MainWidget::MainWidget(QWidget *parent) :
35 QWidget(parent)
35 QWidget(parent)
36 {
36 {
37 // Create buttons for ui
37 // Create buttons for ui
38 m_buttonLayout = new QGridLayout();
38 m_buttonLayout = new QGridLayout();
39 QPushButton *detachLegendButton = new QPushButton("detach legend");
39 QPushButton *detachLegendButton = new QPushButton("detach legend");
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
45
45
46 QPushButton *addSetButton = new QPushButton("add barset");
46 QPushButton *addSetButton = new QPushButton("add barset");
47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
48 m_buttonLayout->addWidget(addSetButton, 2, 0);
48 m_buttonLayout->addWidget(addSetButton, 2, 0);
49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
52
52
53 QPushButton *leftButton = new QPushButton("Align legend left");
53 QPushButton *leftButton = new QPushButton("Align legend left");
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
55 m_buttonLayout->addWidget(leftButton, 4, 0);
55 m_buttonLayout->addWidget(leftButton, 4, 0);
56
56
57 QPushButton *rightButton = new QPushButton("Align legend right");
57 QPushButton *rightButton = new QPushButton("Align legend right");
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
59 m_buttonLayout->addWidget(rightButton, 5, 0);
59 m_buttonLayout->addWidget(rightButton, 5, 0);
60
60
61 QPushButton *topButton = new QPushButton("Align legend top");
61 QPushButton *topButton = new QPushButton("Align legend top");
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
63 m_buttonLayout->addWidget(topButton, 6, 0);
63 m_buttonLayout->addWidget(topButton, 6, 0);
64
64
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
68
68
69 QPushButton *boldButton = new QPushButton("Toggle bold");
69 QPushButton *boldButton = new QPushButton("Toggle bold");
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
71 m_buttonLayout->addWidget(boldButton, 8, 0);
71 m_buttonLayout->addWidget(boldButton, 8, 0);
72
72
73 QPushButton *italicButton = new QPushButton("Toggle italic");
73 QPushButton *italicButton = new QPushButton("Toggle italic");
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
75 m_buttonLayout->addWidget(italicButton, 9, 0);
75 m_buttonLayout->addWidget(italicButton, 9, 0);
76
76
77 m_legendPosX = new QDoubleSpinBox();
77 m_legendPosX = new QDoubleSpinBox();
78 m_legendPosY = new QDoubleSpinBox();
78 m_legendPosY = new QDoubleSpinBox();
79 m_legendWidth = new QDoubleSpinBox();
79 m_legendWidth = new QDoubleSpinBox();
80 m_legendHeight = new QDoubleSpinBox();
80 m_legendHeight = new QDoubleSpinBox();
81
81
82 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
82 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
86
86
87 QFormLayout* legendLayout = new QFormLayout();
87 QFormLayout* legendLayout = new QFormLayout();
88 legendLayout->addRow("Horizontal position", m_legendPosX);
88 legendLayout->addRow("Horizontal position", m_legendPosX);
89 legendLayout->addRow("Vertical position", m_legendPosY);
89 legendLayout->addRow("Vertical position", m_legendPosY);
90 legendLayout->addRow("Width", m_legendWidth);
90 legendLayout->addRow("Width", m_legendWidth);
91 legendLayout->addRow("Height", m_legendHeight);
91 legendLayout->addRow("Height", m_legendHeight);
92 m_legendSettings = new QGroupBox("Detached legend");
92 m_legendSettings = new QGroupBox("Detached legend");
93 m_legendSettings->setLayout(legendLayout);
93 m_legendSettings->setLayout(legendLayout);
94 m_buttonLayout->addWidget(m_legendSettings);
94 m_buttonLayout->addWidget(m_legendSettings);
95 m_legendSettings->setVisible(false);
95 m_legendSettings->setVisible(false);
96
96
97 // Create chart view with the chart
97 // Create chart view with the chart
98 m_chart = new QChart();
98 m_chart = new QChart();
99 m_chartView = new QChartView(m_chart, this);
99 m_chartView = new QChartView(m_chart, this);
100
100
101 // Create spinbox to modify font size
101 // Create spinbox to modify font size
102 m_fontSize = new QDoubleSpinBox();
102 m_fontSize = new QDoubleSpinBox();
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
105
105
106 QFormLayout* fontLayout = new QFormLayout();
106 QFormLayout* fontLayout = new QFormLayout();
107 fontLayout->addRow("Legend font size", m_fontSize);
107 fontLayout->addRow("Legend font size", m_fontSize);
108
108
109 // Create layout for grid and detached legend
109 // Create layout for grid and detached legend
110 m_mainLayout = new QGridLayout();
110 m_mainLayout = new QGridLayout();
111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
112 m_mainLayout->addLayout(fontLayout,1,0);
112 m_mainLayout->addLayout(fontLayout,1,0);
113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
114 setLayout(m_mainLayout);
114 setLayout(m_mainLayout);
115
115
116 createSeries();
116 createSeries();
117 }
117 }
118
118
119 void MainWidget::createSeries()
119 void MainWidget::createSeries()
120 {
120 {
121 m_series = new QGroupedBarSeries();
121 m_series = new QGroupedBarSeries();
122 addBarset();
122 addBarset();
123 addBarset();
123 addBarset();
124 addBarset();
124 addBarset();
125 addBarset();
125 addBarset();
126
126
127 m_chart->addSeries(m_series);
127 m_chart->addSeries(m_series);
128 m_chart->setTitle("Legend detach example");
128 m_chart->setTitle("Legend detach example");
129 m_chart->createDefaultAxes();
129 //![1]
130 //![1]
130 m_chart->legend()->setVisible(true);
131 m_chart->legend()->setVisible(true);
131 m_chart->legend()->setAlignment(Qt::AlignBottom);
132 m_chart->legend()->setAlignment(Qt::AlignBottom);
132 //![1]
133 //![1]
133
134
134 //TODO:m_chart->axisY()->setNiceNumbersEnabled(true);
135 //TODO:m_chart->axisY()->setNiceNumbersEnabled(true);
135 m_chartView->setRenderHint(QPainter::Antialiasing);
136 m_chartView->setRenderHint(QPainter::Antialiasing);
136 }
137 }
137
138
138 void MainWidget::showLegendSpinbox()
139 void MainWidget::showLegendSpinbox()
139 {
140 {
140 m_legendSettings->setVisible(true);
141 m_legendSettings->setVisible(true);
141 QRectF chartViewRect = m_chartView->rect();
142 QRectF chartViewRect = m_chartView->rect();
142
143
143 m_legendPosX->setMinimum(0);
144 m_legendPosX->setMinimum(0);
144 m_legendPosX->setMaximum(chartViewRect.width());
145 m_legendPosX->setMaximum(chartViewRect.width());
145 m_legendPosX->setValue(150);
146 m_legendPosX->setValue(150);
146
147
147 m_legendPosY->setMinimum(0);
148 m_legendPosY->setMinimum(0);
148 m_legendPosY->setMaximum(chartViewRect.height());
149 m_legendPosY->setMaximum(chartViewRect.height());
149 m_legendPosY->setValue(150);
150 m_legendPosY->setValue(150);
150
151
151 m_legendWidth->setMinimum(0);
152 m_legendWidth->setMinimum(0);
152 m_legendWidth->setMaximum(chartViewRect.width());
153 m_legendWidth->setMaximum(chartViewRect.width());
153 m_legendWidth->setValue(150);
154 m_legendWidth->setValue(150);
154
155
155 m_legendHeight->setMinimum(0);
156 m_legendHeight->setMinimum(0);
156 m_legendHeight->setMaximum(chartViewRect.height());
157 m_legendHeight->setMaximum(chartViewRect.height());
157 m_legendHeight->setValue(75);
158 m_legendHeight->setValue(75);
158 }
159 }
159
160
160 void MainWidget::hideLegendSpinbox()
161 void MainWidget::hideLegendSpinbox()
161 {
162 {
162 m_legendSettings->setVisible(false);
163 m_legendSettings->setVisible(false);
163 }
164 }
164
165
165
166
166 void MainWidget::detachLegend()
167 void MainWidget::detachLegend()
167 {
168 {
168 //![2]
169 //![2]
169 QLegend *legend = m_chart->legend();
170 QLegend *legend = m_chart->legend();
170 legend->detachFromChart();
171 legend->detachFromChart();
171
172
172 m_chart->legend()->setBackgroundVisible(true);
173 m_chart->legend()->setBackgroundVisible(true);
173 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
174 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
174 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
175 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
175 //![2]
176 //![2]
176
177
177 showLegendSpinbox();
178 showLegendSpinbox();
178 updateLegendLayout();
179 updateLegendLayout();
179 update();
180 update();
180 }
181 }
181
182
182
183
183 void MainWidget::attachLegend()
184 void MainWidget::attachLegend()
184 {
185 {
185 //![3]
186 //![3]
186 QLegend *legend = m_chart->legend();
187 QLegend *legend = m_chart->legend();
187 legend->attachToChart();
188 legend->attachToChart();
188 m_chart->legend()->setBackgroundVisible(false);
189 m_chart->legend()->setBackgroundVisible(false);
189 //![3]
190 //![3]
190
191
191 hideLegendSpinbox();
192 hideLegendSpinbox();
192 update();
193 update();
193 }
194 }
194
195
195 void MainWidget::addBarset()
196 void MainWidget::addBarset()
196 {
197 {
197 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
198 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
198 qreal delta = m_series->count() * 0.1;
199 qreal delta = m_series->count() * 0.1;
199 *barSet << 1 + delta << 2 + delta << 3 + delta << 4 + delta;
200 *barSet << 1 + delta << 2 + delta << 3 + delta << 4 + delta;
200 m_series->append(barSet);
201 m_series->append(barSet);
201 }
202 }
202
203
203 void MainWidget::removeBarset()
204 void MainWidget::removeBarset()
204 {
205 {
205 QList<QBarSet*> sets = m_series->barSets();
206 QList<QBarSet*> sets = m_series->barSets();
206 if (sets.count() > 0) {
207 if (sets.count() > 0) {
207 m_series->remove(sets.at(sets.count()-1));
208 m_series->remove(sets.at(sets.count()-1));
208 }
209 }
209 }
210 }
210
211
211 void MainWidget::setLegendLeft()
212 void MainWidget::setLegendLeft()
212 {
213 {
213 m_chart->legend()->setAlignment(Qt::AlignLeft);
214 m_chart->legend()->setAlignment(Qt::AlignLeft);
214 }
215 }
215
216
216 void MainWidget::setLegendRight()
217 void MainWidget::setLegendRight()
217 {
218 {
218 m_chart->legend()->setAlignment(Qt::AlignRight);
219 m_chart->legend()->setAlignment(Qt::AlignRight);
219 }
220 }
220
221
221 void MainWidget::setLegendTop()
222 void MainWidget::setLegendTop()
222 {
223 {
223 m_chart->legend()->setAlignment(Qt::AlignTop);
224 m_chart->legend()->setAlignment(Qt::AlignTop);
224 }
225 }
225
226
226 void MainWidget::setLegendBottom()
227 void MainWidget::setLegendBottom()
227 {
228 {
228 m_chart->legend()->setAlignment(Qt::AlignBottom);
229 m_chart->legend()->setAlignment(Qt::AlignBottom);
229 }
230 }
230
231
231 void MainWidget::toggleBold()
232 void MainWidget::toggleBold()
232 {
233 {
233 QFont font = m_chart->legend()->font();
234 QFont font = m_chart->legend()->font();
234 font.setBold(!font.bold());
235 font.setBold(!font.bold());
235 m_chart->legend()->setFont(font);
236 m_chart->legend()->setFont(font);
236 }
237 }
237
238
238 void MainWidget::toggleItalic()
239 void MainWidget::toggleItalic()
239 {
240 {
240 QFont font = m_chart->legend()->font();
241 QFont font = m_chart->legend()->font();
241 font.setItalic(!font.italic());
242 font.setItalic(!font.italic());
242 m_chart->legend()->setFont(font);
243 m_chart->legend()->setFont(font);
243 }
244 }
244
245
245 void MainWidget::fontSizeChanged()
246 void MainWidget::fontSizeChanged()
246 {
247 {
247 QFont font = m_chart->legend()->font();
248 QFont font = m_chart->legend()->font();
248 font.setPointSizeF(m_fontSize->value());
249 font.setPointSizeF(m_fontSize->value());
249 m_chart->legend()->setFont(font);
250 m_chart->legend()->setFont(font);
250 }
251 }
251
252
252 void MainWidget::updateLegendLayout()
253 void MainWidget::updateLegendLayout()
253 {
254 {
254 //![4]
255 //![4]
255 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
256 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
256 ,m_legendPosY->value()
257 ,m_legendPosY->value()
257 ,m_legendWidth->value()
258 ,m_legendWidth->value()
258 ,m_legendHeight->value()));
259 ,m_legendHeight->value()));
259 m_chart->legend()->update();
260 m_chart->legend()->update();
260 //![4]
261 //![4]
261 }
262 }
General Comments 0
You need to be logged in to leave comments. Login now