main.cpp
82 lines
| 2.3 KiB
| text/x-c
|
CppLexer
sauimone
|
r78 | #include <QApplication> | ||
#include <QMainWindow> | ||||
sauimone
|
r276 | #include <qchartview.h> | ||
sauimone
|
r338 | #include <qbarseries.h> | ||
sauimone
|
r169 | #include <qbarset.h> | ||
sauimone
|
r387 | #include <qchartaxis.h> | ||
sauimone
|
r377 | #include <QStringList> | ||
sauimone
|
r276 | |||
sauimone
|
r78 | QTCOMMERCIALCHART_USE_NAMESPACE | ||
int main(int argc, char *argv[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
QMainWindow window; | ||||
sauimone
|
r319 | //! [1] | ||
sauimone
|
r377 | // Define categories | ||
sauimone
|
r387 | QStringList categories; | ||
categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; | ||||
sauimone
|
r319 | //! [1] | ||
sauimone
|
r169 | |||
sauimone
|
r319 | //! [2] | ||
sauimone
|
r325 | // Create some test sets for chat | ||
sauimone
|
r169 | |||
sauimone
|
r296 | QBarSet *set0 = new QBarSet("Bub"); | ||
QBarSet *set1 = new QBarSet("Bob"); | ||||
QBarSet *set2 = new QBarSet("Guybrush"); | ||||
QBarSet *set3 = new QBarSet("Larry"); | ||||
QBarSet *set4 = new QBarSet("Zak"); | ||||
sauimone
|
r78 | |||
sauimone
|
r387 | *set0 << 1 << 2 << 3 << 4 << 5 << 6; | ||
*set1 << 5 << 0 << 0 << 4 << 0 << 7; | ||||
*set2 << 3 << 5 << 8 << 13 << 8 << 5; | ||||
*set3 << 5 << 6 << 7 << 3 << 4 << 5; | ||||
*set4 << 9 << 7 << 5 << 3 << 1 << 2; | ||||
sauimone
|
r325 | //! [2] | ||
//! [3] | ||||
// Create series and add sets to it | ||||
sauimone
|
r387 | QBarSeries* series= new QBarSeries(categories); | ||
sauimone
|
r173 | |||
sauimone
|
r276 | series->addBarSet(set0); | ||
series->addBarSet(set1); | ||||
series->addBarSet(set2); | ||||
series->addBarSet(set3); | ||||
series->addBarSet(set4); | ||||
sauimone
|
r319 | //! [3] | ||
sauimone
|
r325 | |||
//! [4] | ||||
sauimone
|
r425 | // Enable tooltip | ||
sauimone
|
r357 | series->setToolTipEnabled(); | ||
sauimone
|
r425 | |||
// Connect clicked signal of set to toggle floating values of set. | ||||
// Note that we leave QBarset "Zak" unconnected here, so clicking on it doesn't toggle values. | ||||
QObject::connect(set0,SIGNAL(clicked(QString)),set0,SIGNAL(toggleFloatingValues())); | ||||
QObject::connect(set1,SIGNAL(clicked(QString)),set1,SIGNAL(toggleFloatingValues())); | ||||
QObject::connect(set2,SIGNAL(clicked(QString)),set2,SIGNAL(toggleFloatingValues())); | ||||
QObject::connect(set3,SIGNAL(clicked(QString)),set3,SIGNAL(toggleFloatingValues())); | ||||
sauimone
|
r325 | //! [4] | ||
//! [5] | ||||
// Create view for chart and add series to it. Apply theme. | ||||
sauimone
|
r296 | |||
sauimone
|
r276 | QChartView* chartView = new QChartView(&window); | ||
chartView->addSeries(series); | ||||
sauimone
|
r396 | chartView->setChartTitle("simple barchart"); | ||
sauimone
|
r276 | chartView->setChartTheme(QChart::ChartThemeIcy); | ||
sauimone
|
r325 | //! [5] | ||
sauimone
|
r78 | |||
sauimone
|
r387 | //! [6] | ||
chartView->axisX()->setAxisVisible(false); | ||||
chartView->axisX()->setGridVisible(false); | ||||
chartView->axisX()->setLabelsVisible(false); | ||||
//! [6] | ||||
sauimone
|
r276 | window.setCentralWidget(chartView); | ||
sauimone
|
r387 | window.resize(400, 300); | ||
sauimone
|
r78 | window.show(); | ||
return a.exec(); | ||||
} | ||||