1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "mainwidget.h"
#include "dataseriedialog.h"
#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QScatterSeries>
#include <QtCharts/QLineSeries>
#include <QtCharts/QAreaSeries>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QBarSeries>
#include <QtCharts/QStackedBarSeries>
#include <QtCharts/QPercentBarSeries>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QMessageBox>
#include <QtCore/QDebug>
#include <QtCore/QRandomGenerator>
#include <QtGui/QStandardItemModel>
#include <QtCharts/QBarCategoryAxis>
#include <QtWidgets/QOpenGLWidget>
#include <qmath.h>
QT_CHARTS_USE_NAMESPACE
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
m_addSerieDialog(0),
m_chart(0)
{
m_chart = new QChart();
// Grid layout for the controls for configuring the chart widget
QGridLayout *grid = new QGridLayout();
QPushButton *addSeriesButton = new QPushButton("Add series");
connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
grid->addWidget(addSeriesButton, 0, 1);
initBackroundCombo(grid);
initScaleControls(grid);
initThemeCombo(grid);
initCheckboxes(grid);
// add row with empty label to make all the other rows static
grid->addWidget(new QLabel(""), grid->rowCount(), 0);
grid->setRowStretch(grid->rowCount() - 1, 1);
// Create chart view with the chart
m_chartView = new QChartView(m_chart, this);
m_chartView->setRubberBand(QChartView::HorizontalRubberBand);
// Another grid layout as a main layout
QGridLayout *mainLayout = new QGridLayout();
mainLayout->addLayout(grid, 0, 0);
mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
setLayout(mainLayout);
}
// Combo box for selecting the chart's background
void MainWidget::initBackroundCombo(QGridLayout *grid)
{
QComboBox *backgroundCombo = new QComboBox(this);
backgroundCombo->addItem("Color");
backgroundCombo->addItem("Gradient");
backgroundCombo->addItem("Image");
connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
this, SLOT(backgroundChanged(int)));
grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
}
// Scale related controls (auto-scale vs. manual min-max values)
void MainWidget::initScaleControls(QGridLayout *grid)
{
m_autoScaleCheck = new QCheckBox("Automatic scaling");
connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
// Allow setting also non-sense values (like -2147483648 and 2147483647)
m_xMinSpin = new QSpinBox();
m_xMinSpin->setMinimum(INT_MIN);
m_xMinSpin->setMaximum(INT_MAX);
m_xMinSpin->setValue(0);
connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
m_xMaxSpin = new QSpinBox();
m_xMaxSpin->setMinimum(INT_MIN);
m_xMaxSpin->setMaximum(INT_MAX);
m_xMaxSpin->setValue(10);
connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
m_yMinSpin = new QSpinBox();
m_yMinSpin->setMinimum(INT_MIN);
m_yMinSpin->setMaximum(INT_MAX);
m_yMinSpin->setValue(0);
connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
m_yMaxSpin = new QSpinBox();
m_yMaxSpin->setMinimum(INT_MIN);
m_yMaxSpin->setMaximum(INT_MAX);
m_yMaxSpin->setValue(10);
connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
m_autoScaleCheck->setChecked(true);
}
// Combo box for selecting theme
void MainWidget::initThemeCombo(QGridLayout *grid)
{
QComboBox *chartTheme = new QComboBox();
chartTheme->addItem("Default");
chartTheme->addItem("Light");
chartTheme->addItem("Blue Cerulean");
chartTheme->addItem("Dark");
chartTheme->addItem("Brown Sand");
chartTheme->addItem("Blue NCS");
chartTheme->addItem("High Contrast");
chartTheme->addItem("Blue Icy");
chartTheme->addItem("Qt");
connect(chartTheme, SIGNAL(currentIndexChanged(int)),
this, SLOT(changeChartTheme(int)));
grid->addWidget(new QLabel("Chart theme:"), 8, 0);
grid->addWidget(chartTheme, 8, 1);
}
// Different check boxes for customizing chart
void MainWidget::initCheckboxes(QGridLayout *grid)
{
// TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
// connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
zoomCheckBox->setChecked(true);
grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
aliasCheckBox->setChecked(false);
grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
QCheckBox *openGLCheckBox = new QCheckBox("Use QOpenGLWidget");
connect(openGLCheckBox, SIGNAL(toggled(bool)), this, SLOT(openGLToggled(bool)));
openGLCheckBox->setChecked(false);
grid->addWidget(openGLCheckBox, grid->rowCount(), 0);
}
void MainWidget::antiAliasToggled(bool enabled)
{
m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
}
void MainWidget::openGLToggled(bool enabled)
{
if (enabled) {
QSurfaceFormat f = QSurfaceFormat::defaultFormat();
f.setSamples(4);
QSurfaceFormat::setDefaultFormat(f);
QOpenGLWidget *g = new QOpenGLWidget();
m_chartView->setViewport(g);
} else {
m_chartView->setViewport(0);
}
}
void MainWidget::addSeries()
{
if (!m_addSerieDialog) {
m_addSerieDialog = new DataSerieDialog(this);
connect(m_addSerieDialog, SIGNAL(accepted(QString,int,int,QString,bool)),
this, SLOT(addSeries(QString,int,int,QString,bool)));
}
m_addSerieDialog->exec();
}
QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
{
QList<RealList> testData;
for (int j(0); j < columnCount; j++) {
QList <qreal> newColumn;
for (int i(0); i < rowCount; i++) {
if (dataCharacteristics == "Sin") {
newColumn.append(std::abs(sin(M_PI / 50 * i) * 100));
} else if (dataCharacteristics == "Sin + random") {
newColumn.append(std::abs(sin(M_PI / 50 * i) * 100) + QRandomGenerator::global()->bounded(5));
} else if (dataCharacteristics == "Random") {
newColumn.append(QRandomGenerator::global()->bounded(11.0));
} else if (dataCharacteristics == "Linear") {
//newColumn.append(i * (j + 1.0));
// TODO: temporary hack to make pie work; prevent zero values:
newColumn.append(i * (j + 1.0) + 0.1);
} else { // "constant"
newColumn.append((j + 1.0));
}
}
testData.append(newColumn);
}
return testData;
}
QStringList MainWidget::generateLabels(int count)
{
QStringList result;
for (int i(0); i < count; i++)
result.append("label" + QString::number(i));
return result;
}
void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
{
qDebug() << "addSeries: " << seriesName
<< " columnCount: " << columnCount
<< " rowCount: " << rowCount
<< " dataCharacteristics: " << dataCharacteristics
<< " labels enabled: " << labelsEnabled;
m_defaultSeriesName = seriesName;
QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
// Line series and scatter series use similar data
if (seriesName == "Line") {
for (int j(0); j < data.count(); j ++) {
QList<qreal> column = data.at(j);
QLineSeries *series = new QLineSeries();
series->setName("line" + QString::number(j));
for (int i(0); i < column.count(); i++)
series->append(i, column.at(i));
m_chart->addSeries(series);
}
} else if (seriesName == "Area") {
// TODO: lower series for the area?
for (int j(0); j < data.count(); j ++) {
QList<qreal> column = data.at(j);
QLineSeries *lineSeries = new QLineSeries();
for (int i(0); i < column.count(); i++)
lineSeries->append(i, column.at(i));
QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
areaSeries->setName("area" + QString::number(j));
m_chart->addSeries(areaSeries);
}
} else if (seriesName == "Scatter") {
for (int j(0); j < data.count(); j++) {
QList<qreal> column = data.at(j);
QScatterSeries *series = new QScatterSeries();
series->setName("scatter" + QString::number(j));
for (int i(0); i < column.count(); i++)
series->append(i, column.at(i));
m_chart->addSeries(series);
}
} else if (seriesName == "Pie") {
QStringList labels = generateLabels(rowCount);
for (int j(0); j < data.count(); j++) {
QPieSeries *series = new QPieSeries();
QList<qreal> column = data.at(j);
for (int i(0); i < column.count(); i++)
series->append(labels.at(i), column.at(i));
m_chart->addSeries(series);
}
} else if (seriesName == "Bar"
|| seriesName == "Stacked bar"
|| seriesName == "Percent bar") {
QStringList category;
QStringList labels = generateLabels(rowCount);
foreach (QString label, labels)
category << label;
QAbstractBarSeries* series = 0;
if (seriesName == "Bar") {
series = new QBarSeries(this);
QBarCategoryAxis* axis = new QBarCategoryAxis();
axis->append(category);
m_chart->setAxisX(axis,series);
} else if (seriesName == "Stacked bar") {
series = new QStackedBarSeries(this);
QBarCategoryAxis* axis = new QBarCategoryAxis();
axis->append(category);
m_chart->setAxisX(axis,series);
} else {
series = new QPercentBarSeries(this);
QBarCategoryAxis* axis = new QBarCategoryAxis();
axis->append(category);
m_chart->setAxisX(axis,series);
}
for (int j(0); j < data.count(); j++) {
QList<qreal> column = data.at(j);
QBarSet *set = new QBarSet("set" + QString::number(j));
for (int i(0); i < column.count(); i++)
*set << column.at(i);
series->append(set);
}
m_chart->addSeries(series);
} else if (seriesName == "Spline") {
for (int j(0); j < data.count(); j ++) {
QList<qreal> column = data.at(j);
QSplineSeries *series = new QSplineSeries();
series->setName("spline" + QString::number(j));
for (int i(0); i < column.count(); i++)
series->append(i, column.at(i));
m_chart->addSeries(series);
}
}
m_chart->createDefaultAxes();
}
void MainWidget::backgroundChanged(int itemIndex)
{
qDebug() << "backgroundChanged: " << itemIndex;
}
void MainWidget::autoScaleChanged(int value)
{
if (value) {
// TODO: enable auto scaling
} else {
// TODO: set scaling manually (and disable auto scaling)
}
m_xMinSpin->setEnabled(!value);
m_xMaxSpin->setEnabled(!value);
m_yMinSpin->setEnabled(!value);
m_yMaxSpin->setEnabled(!value);
}
void MainWidget::xMinChanged(int value)
{
qDebug() << "xMinChanged: " << value;
}
void MainWidget::xMaxChanged(int value)
{
qDebug() << "xMaxChanged: " << value;
}
void MainWidget::yMinChanged(int value)
{
qDebug() << "yMinChanged: " << value;
}
void MainWidget::yMaxChanged(int value)
{
qDebug() << "yMaxChanged: " << value;
}
void MainWidget::changeChartTheme(int themeIndex)
{
qDebug() << "changeChartTheme: " << themeIndex;
if (themeIndex == 0)
m_chart->setTheme(QChart::ChartThemeLight);
else
m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1));
}
|