main.cpp
114 lines
| 3.1 KiB
| text/x-c
|
CppLexer
|
r406 | #include <QtGui/QApplication> | ||
#include <QMainWindow> | ||||
#include <qchartglobal.h> | ||||
#include <qchartview.h> | ||||
#include <qpieseries.h> | ||||
#include <qpieslice.h> | ||||
#include <QTime> | ||||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
class DrilldownSlice : public QPieSlice | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) | ||||
:m_drilldownSeries(drilldownSeries), | ||||
m_prefix(prefix) | ||||
{ | ||||
setValue(value); | ||||
setLabelVisible(true); | ||||
updateLabel(); | ||||
connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); | ||||
} | ||||
QSeries* drilldownSeries() const { return m_drilldownSeries; } | ||||
public Q_SLOTS: | ||||
void updateLabel() | ||||
{ | ||||
QString label = m_prefix; | ||||
|
r408 | label += " " + QString::number(this->value())+ "e ("; | ||
|
r406 | label += QString::number(this->percentage()*100, 'f', 1) + "%)"; | ||
setLabel(label); | ||||
} | ||||
private: | ||||
QSeries* m_drilldownSeries; | ||||
QString m_prefix; | ||||
}; | ||||
class DrilldownChart : public QChartView | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {} | ||||
void changeSeries(QSeries* series) | ||||
{ | ||||
|
r617 | // NOTE: if the series is owned by the chart it will be deleted | ||
// here the "window" owns the series... | ||||
|
r406 | if (m_currentSeries) | ||
removeSeries(m_currentSeries); | ||||
m_currentSeries = series; | ||||
addSeries(series); | ||||
setChartTitle(series->title()); | ||||
} | ||||
public Q_SLOTS: | ||||
void handleSliceClicked(QPieSlice* slice) | ||||
{ | ||||
DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice); | ||||
changeSeries(drilldownSlice->drilldownSeries()); | ||||
} | ||||
private: | ||||
QSeries* m_currentSeries; | ||||
}; | ||||
int main(int argc, char *argv[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | ||||
QMainWindow window; | ||||
DrilldownChart* drilldownChart = new DrilldownChart(&window); | ||||
drilldownChart->setRenderHint(QPainter::Antialiasing); | ||||
drilldownChart->setChartTheme(QChart::ChartThemeVanilla); | ||||
|
r621 | drilldownChart->setAnimationOptions(QChart::AllAnimations); | ||
|
r406 | |||
|
r617 | QPieSeries* yearSeries = new QPieSeries(&window); | ||
|
r406 | yearSeries->setTitle("Sales by year - All"); | ||
QList<QString> months; | ||||
months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; | ||||
|
r408 | QList<QString> names; | ||
names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob"; | ||||
foreach (QString name, names) { | ||||
|
r617 | QPieSeries* series = new QPieSeries(&window); | ||
|
r408 | series->setTitle("Sales by month - " + name); | ||
foreach (QString month, months) | ||||
*series << new DrilldownSlice(qrand() % 1000, month, yearSeries); | ||||
QObject::connect(series, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | ||||
*yearSeries << new DrilldownSlice(series->total(), name, series); | ||||
} | ||||
|
r406 | |||
QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | ||||
drilldownChart->changeSeries(yearSeries); | ||||
window.setCentralWidget(drilldownChart); | ||||
|
r617 | window.resize(800, 600); | ||
|
r406 | window.show(); | ||
return a.exec(); | ||||
} | ||||
#include "main.moc" | ||||