##// END OF EJS Templates
Make stackedbarchartdrilldown compile
Make stackedbarchartdrilldown compile

File last commit:

r798:2c85b9e659bd
r829:f9360d7ac284
Show More
main.cpp
115 lines | 3.2 KiB | text/x-c | CppLexer
Jani Honkonen
Add drilldown example for piechart
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;
Jani Honkonen
Refactor piechartdrilldown example a bit
r408 label += " " + QString::number(this->value())+ "e (";
Jani Honkonen
Add drilldown example for piechart
r406 label += QString::number(this->percentage()*100, 'f', 1) + "%)";
setLabel(label);
}
private:
QSeries* m_drilldownSeries;
QString m_prefix;
};
Jani Honkonen
make piechartdrilldown compile again
r798 class DrilldownChart : public QChart
Jani Honkonen
Add drilldown example for piechart
r406 {
Q_OBJECT
public:
Jani Honkonen
make piechartdrilldown compile again
r798 explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0):QChart(parent, wFlags), m_currentSeries(0) {}
Jani Honkonen
Add drilldown example for piechart
r406
void changeSeries(QSeries* series)
{
Jani Honkonen
Fix piechartdrilldown bug. Chart was deleting the series from us.
r617 // NOTE: if the series is owned by the chart it will be deleted
// here the "window" owns the series...
Jani Honkonen
Add drilldown example for piechart
r406 if (m_currentSeries)
removeSeries(m_currentSeries);
m_currentSeries = series;
addSeries(series);
Jani Honkonen
make piechartdrilldown compile again
r798 setTitle(series->name());
Jani Honkonen
Add drilldown example for piechart
r406 }
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;
Jani Honkonen
make piechartdrilldown compile again
r798 DrilldownChart* drilldownChart = new DrilldownChart();
drilldownChart->setTheme(QChart::ChartThemeLight);
Jani Honkonen
Refactoring pie series and animations.
r621 drilldownChart->setAnimationOptions(QChart::AllAnimations);
Jani Honkonen
Add drilldown example for piechart
r406
Jani Honkonen
Fix piechartdrilldown bug. Chart was deleting the series from us.
r617 QPieSeries* yearSeries = new QPieSeries(&window);
Tero Ahola
Fixed build error caused by QSeries name change
r733 yearSeries->setName("Sales by year - All");
Jani Honkonen
Add drilldown example for piechart
r406
QList<QString> months;
months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
Jani Honkonen
Refactor piechartdrilldown example a bit
r408 QList<QString> names;
names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob";
foreach (QString name, names) {
Jani Honkonen
Fix piechartdrilldown bug. Chart was deleting the series from us.
r617 QPieSeries* series = new QPieSeries(&window);
Tero Ahola
Fixed build error caused by QSeries name change
r733 series->setName("Sales by month - " + name);
Jani Honkonen
Refactor piechartdrilldown example a bit
r408
foreach (QString month, months)
*series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
Jani Honkonen
Add mousebuttons to pie clicked signals
r707 QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*)));
Jani Honkonen
Refactor piechartdrilldown example a bit
r408
*yearSeries << new DrilldownSlice(series->total(), name, series);
}
Jani Honkonen
Add drilldown example for piechart
r406
Jani Honkonen
Add mousebuttons to pie clicked signals
r707 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*)));
Jani Honkonen
Add drilldown example for piechart
r406
drilldownChart->changeSeries(yearSeries);
Jani Honkonen
make piechartdrilldown compile again
r798 QChartView* chartView = new QChartView(drilldownChart);
chartView->setRenderHint(QPainter::Antialiasing);
window.setCentralWidget(chartView);
Jani Honkonen
Fix piechartdrilldown bug. Chart was deleting the series from us.
r617 window.resize(800, 600);
Jani Honkonen
Add drilldown example for piechart
r406 window.show();
return a.exec();
}
#include "main.moc"