#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include QTCOMMERCIALCHART_USE_NAMESPACE typedef QPair Data; typedef QList DataList; typedef QList DataTable; class MainWidget : public QWidget { Q_OBJECT public: explicit MainWidget(QWidget* parent = 0) :QWidget(parent) { // set seed for random stuff qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); // generate random data int listCount = 3; int valueMax = 100; int valueCount = 50; for (int i=0; iaddItem("Default", QChart::ChartThemeDefault); m_themeComboBox->addItem("Vanilla", QChart::ChartThemeVanilla); m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy); m_themeComboBox->addItem("Grayscale", QChart::ChartThemeGrayscale); m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific); connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme())); baseLayout->addWidget(new QLabel("Theme:"), 0, 0); baseLayout->addWidget(m_themeComboBox, 0, 1); // area chart QChartView *chart = new QChartView(); chart->setChartTitle("Area chart"); chart->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chart, 1, 0); { QLineSeries *series1 = new QLineSeries(chart); foreach (Data data, m_dataTable.first()) series1->add(data.first); QLineSeries *series2 = new QLineSeries(chart); foreach (Data data, m_dataTable.last()) series2->add(data.first); QAreaSeries *series = new QAreaSeries(series1, series2); chart->addSeries(series); } m_charts << chart; // bar chart chart = new QChartView(); chart->setChartTitle("bar chart"); chart->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chart, 1, 1); m_charts << chart; // line chart chart = new QChartView(); chart->setChartTitle("line chart"); chart->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chart, 1, 2); foreach (DataList list, m_dataTable) { QLineSeries *series = new QLineSeries(chart); foreach (Data data, list) series->add(data.first); chart->addSeries(series); } m_charts << chart; // pie chart chart = new QChartView(); chart->setChartTitle("pie chart"); chart->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chart, 2, 0); qreal pieSize = 1.0 / m_dataTable.count(); for (int i=0; iadd(data.first.x(), data.second); qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); series->setPieSize(pieSize); series->setPiePosition(hPos, 0.5); chart->addSeries(series); } m_charts << chart; // spine chart chart = new QChartView(); chart->setChartTitle("spline chart"); chart->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chart, 2, 1); foreach (DataList list, m_dataTable) { QSplineSeries *series = new QSplineSeries(chart); foreach (Data data, list) series->add(data.first); chart->addSeries(series); } m_charts << chart; // scatter chart chart = new QChartView(); chart->setChartTitle("scatter chart"); chart->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chart, 2, 2); foreach (DataList list, m_dataTable) { QScatterSeries *series = new QScatterSeries(chart); foreach (Data data, list) series->add(data.first); chart->addSeries(series); } m_charts << chart; setLayout(baseLayout); } public Q_SLOTS: void updateTheme() { QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); foreach (QChartView *chart, m_charts) chart->setChartTheme(theme); } private: QList m_charts; QComboBox *m_themeComboBox; DataTable m_dataTable; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow window; MainWidget* widget = new MainWidget(); window.setCentralWidget(widget); window.resize(900, 600); window.show(); return a.exec(); } #include "main.moc"