##// END OF EJS Templates
Labels for barchart to axis
sauimone -
r487:03622a1338c4
parent child
Show More
@@ -1,152 +1,152
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qstackedbarseries.h>
5 #include <qstackedbarseries.h>
6 #include <qbarset.h>
6 #include <qbarset.h>
7 #include <qchartaxis.h>
7 #include <qchartaxis.h>
8 #include <QStringList>
8 #include <QStringList>
9 #include <QDebug>
9 #include <QDebug>
10
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
12
13 //! [1]
13 //! [1]
14 class DrilldownBarSeries : public QStackedBarSeries
14 class DrilldownBarSeries : public QStackedBarSeries
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
18 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
19
19
20 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
20 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
21 {
21 {
22 mDrilldownSeries[category] = drilldownSeries;
22 mDrilldownSeries[category] = drilldownSeries;
23 }
23 }
24
24
25 DrilldownBarSeries* drilldownSeries(QString category)
25 DrilldownBarSeries* drilldownSeries(QString category)
26 {
26 {
27 return mDrilldownSeries[category];
27 return mDrilldownSeries[category];
28 }
28 }
29
29
30 public Q_SLOTS:
30 public Q_SLOTS:
31
31
32 private:
32 private:
33
33
34 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
34 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
35 };
35 };
36 //! [1]
36 //! [1]
37
37
38 //! [2]
38 //! [2]
39 class DrilldownChart : public QChartView
39 class DrilldownChart : public QChartView
40 {
40 {
41 Q_OBJECT
41 Q_OBJECT
42 public:
42 public:
43 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
43 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
44
44
45 void changeSeries(QSeries* series)
45 void changeSeries(QSeries* series)
46 {
46 {
47 if (m_currentSeries)
47 if (m_currentSeries)
48 removeSeries(m_currentSeries);
48 removeSeries(m_currentSeries);
49 m_currentSeries = series;
49 m_currentSeries = series;
50 addSeries(series);
50 addSeries(series);
51 setChartTitle(series->title());
51 setChartTitle(series->title());
52 }
52 }
53
53
54 public Q_SLOTS:
54 public Q_SLOTS:
55 void handleRightClick(QBarSet *barset, QString category)
55 void handleRightClick(QBarSet *barset, QString category)
56 {
56 {
57 // qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category;
57 // qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category;
58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
59 changeSeries(series->drilldownSeries(category));
59 changeSeries(series->drilldownSeries(category));
60 }
60 }
61
61
62 private:
62 private:
63 QSeries* m_currentSeries;
63 QSeries* m_currentSeries;
64 };
64 };
65 //! [2]
65 //! [2]
66
66
67 int main(int argc, char *argv[])
67 int main(int argc, char *argv[])
68 {
68 {
69 QApplication a(argc, argv);
69 QApplication a(argc, argv);
70 QMainWindow window;
70 QMainWindow window;
71
71
72 DrilldownChart* drilldownChart = new DrilldownChart(&window);
72 DrilldownChart* drilldownChart = new DrilldownChart(&window);
73 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
73 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
74
74
75 //! [3]
75 //! [3]
76 // Define categories
76 // Define categories
77 QStringList months;
77 QStringList months;
78 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
78 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
79 QStringList weeks;
79 QStringList weeks;
80 weeks << "week 1" << "week 2" << "week 3" << "week 4";
80 weeks << "week 1" << "week 2" << "week 3" << "week 4";
81 QStringList plants;
81 QStringList plants;
82 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
82 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
83 //! [3]
83 //! [3]
84
84
85 //! [4]
85 //! [4]
86 // Create drilldown structure
86 // Create drilldown structure
87 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
87 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
88 seasonSeries->setTitle("Crop by month - Season");
88 seasonSeries->setTitle("Crop by month - Season");
89
89
90 // Each month in season series has drilldown series for weekly data
90 // Each month in season series has drilldown series for weekly data
91 foreach (QString month, months) {
91 foreach (QString month, months) {
92
92
93 // Create drilldown series for every week
93 // Create drilldown series for every week
94 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
94 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
95 seasonSeries->mapDrilldownSeries(month, weeklySeries);
95 seasonSeries->mapDrilldownSeries(month, weeklySeries);
96
96
97 // Drilling down from weekly data brings us back to season data.
97 // Drilling down from weekly data brings us back to season data.
98 foreach (QString week, weeks) {
98 foreach (QString week, weeks) {
99 weeklySeries->mapDrilldownSeries(week, seasonSeries);
99 weeklySeries->mapDrilldownSeries(week, seasonSeries);
100 weeklySeries->setTitle(QString("Crop by week - " + month));
100 weeklySeries->setTitle(QString("Crop by week - " + month));
101 }
101 }
102
102
103 // Use right click signal to implement drilldown
103 // Use right click signal to implement drilldown
104 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
104 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
105 }
105 }
106
106
107 // Enable drilldown from season series using right click.
107 // Enable drilldown from season series using right click.
108 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
108 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
109 //! [4]
109 //! [4]
110
110
111 //! [5]
111 //! [5]
112 // Fill monthly and weekly series with data
112 // Fill monthly and weekly series with data
113 foreach (QString plant, plants) {
113 foreach (QString plant, plants) {
114 QBarSet* monthlyCrop = new QBarSet(plant);
114 QBarSet* monthlyCrop = new QBarSet(plant);
115 foreach (QString month, months) {
115 foreach (QString month, months) {
116 QBarSet* weeklyCrop = new QBarSet(plant);
116 QBarSet* weeklyCrop = new QBarSet(plant);
117 foreach (QString week, weeks ) {
117 foreach (QString week, weeks ) {
118 *weeklyCrop << (qrand() % 20);
118 *weeklyCrop << (qrand() % 20);
119 }
119 }
120 // Get the drilldown series from season series and add crop to it.
120 // Get the drilldown series from season series and add crop to it.
121 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
121 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
123 *monthlyCrop << weeklyCrop->total();
123 *monthlyCrop << weeklyCrop->total();
124
124
125 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
125 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
126 }
126 }
127 seasonSeries->addBarSet(monthlyCrop);
127 seasonSeries->addBarSet(monthlyCrop);
128 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
128 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
129 }
129 }
130 //! [5]
130 //! [5]
131
131
132 seasonSeries->setToolTipEnabled(true);
132 seasonSeries->setToolTipEnabled(true);
133
133
134 //! [6]
134 //! [6]
135 // Show season series in initial view
135 // Show season series in initial view
136 drilldownChart->changeSeries(seasonSeries);
136 drilldownChart->changeSeries(seasonSeries);
137 drilldownChart->setChartTitle(seasonSeries->title());
137 drilldownChart->setChartTitle(seasonSeries->title());
138 //! [6]
138 //! [6]
139
139
140 // Disable axis, since they don't really apply to bar chart
140 // Disable axis, since they don't really apply to bar chart
141 drilldownChart->axisX()->setAxisVisible(false);
141 // drilldownChart->axisX()->setAxisVisible(false);
142 drilldownChart->axisX()->setGridVisible(false);
142 drilldownChart->axisX()->setGridVisible(false);
143 drilldownChart->axisX()->setLabelsVisible(false);
143 // drilldownChart->axisX()->setLabelsVisible(false);
144
144
145 window.setCentralWidget(drilldownChart);
145 window.setCentralWidget(drilldownChart);
146 window.resize(400, 300);
146 window.resize(400, 300);
147 window.show();
147 window.show();
148
148
149 return a.exec();
149 return a.exec();
150 }
150 }
151
151
152 #include "main.moc"
152 #include "main.moc"
@@ -1,95 +1,94
1 #include "barpresenter_p.h"
1 #include "barpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) :
11 BarPresenterBase(series, parent)
11 BarPresenterBase(series, parent)
12 {
12 {
13 mBarWidth = 5;
14 }
13 }
15
14
16 void BarPresenter::layoutChanged()
15 void BarPresenter::layoutChanged()
17 {
16 {
18 // Scale bars to new layout
17 // Scale bars to new layout
19 // Layout for bars:
18 // Layout for bars:
20 if (mSeries->barsetCount() <= 0) {
19 if (mSeries->barsetCount() <= 0) {
21 qDebug() << "No sets in model!";
20 qDebug() << "No sets in model!";
22 return;
21 return;
23 }
22 }
24
23
25 if (childItems().count() == 0) {
24 if (childItems().count() == 0) {
26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
25 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
27 return;
26 return;
28 }
27 }
29
28
30 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
29 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
31 int categoryCount = mSeries->categoryCount();
30 int categoryCount = mSeries->categoryCount();
32 int setCount = mSeries->barsetCount();
31 int setCount = mSeries->barsetCount();
33
32
34 qreal tW = mWidth;
33 qreal tW = mWidth;
35 qreal tH = mHeight;
34 qreal tH = mHeight;
36 qreal tM = mSeries->max();
35 qreal tM = mSeries->max();
37 qreal scale = (tH/tM);
36 qreal scale = (tH/tM);
38 qreal tC = categoryCount + 1;
37 qreal tC = categoryCount + 1;
39 mBarWidth = tW / ((categoryCount * setCount) + tC);
38 mBarWidth = tW / ((categoryCount * setCount) + tC);
40 qreal xStepPerCategory = (tW/tC) + mBarWidth;
39 qreal xStepPerCategory = (tW/tC) + mBarWidth;
41
40
42 int itemIndex(0);
41 int itemIndex(0);
43 int labelIndex(0);
42 int labelIndex(0);
44
43
45 for (int category=0; category < categoryCount; category++) {
44 for (int category=0; category < categoryCount; category++) {
46 qreal xPos = xStepPerCategory * category;
45 qreal xPos = xStepPerCategory * category;
47 qreal yPos = mHeight;
46 qreal yPos = mHeight;
48 for (int set = 0; set < setCount; set++) {
47 for (int set = 0; set < setCount; set++) {
49 qreal barHeight = mSeries->valueAt(set,category) * scale;
48 qreal barHeight = mSeries->valueAt(set,category) * scale;
50 Bar* bar = mBars.at(itemIndex);
49 Bar* bar = mBars.at(itemIndex);
51
50
52 // TODO: width settable per bar?
51 // TODO: width settable per bar?
53 bar->resize(mBarWidth, barHeight);
52 bar->resize(mBarWidth, barHeight);
54 bar->setBrush(mSeries->barsetAt(set)->brush());
53 bar->setBrush(mSeries->barsetAt(set)->brush());
55 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
56 itemIndex++;
55 itemIndex++;
57 xPos += mBarWidth;
56 xPos += mBarWidth;
58 }
57 }
59
58
60 // TODO: Layout for labels, remove magic number
59 // TODO: Layout for labels, remove magic number
61 xPos = xStepPerCategory * category + mBarWidth/2;
60 xPos = xStepPerCategory * category + mBarWidth/2;
62 BarLabel* label = mLabels.at(labelIndex);
61 BarLabel* label = mLabels.at(labelIndex);
63 label->setPos(xPos, mHeight - 20);
62 label->setPos(xPos, mHeight - 20);
64 labelIndex++;
63 labelIndex++;
65 }
64 }
66
65
67 // Position floating values
66 // Position floating values
68 itemIndex = 0;
67 itemIndex = 0;
69 for (int category=0; category < mSeries->categoryCount(); category++) {
68 for (int category=0; category < mSeries->categoryCount(); category++) {
70 qreal xPos = xStepPerCategory * category + mBarWidth/2;
69 qreal xPos = xStepPerCategory * category + mBarWidth/2;
71 qreal yPos = mHeight;
70 qreal yPos = mHeight;
72 for (int set=0; set < mSeries->barsetCount(); set++) {
71 for (int set=0; set < mSeries->barsetCount(); set++) {
73 qreal barHeight = mSeries->valueAt(set,category) * scale;
72 qreal barHeight = mSeries->valueAt(set,category) * scale;
74 BarValue* value = mFloatingValues.at(itemIndex);
73 BarValue* value = mFloatingValues.at(itemIndex);
75
74
76 // TODO: remove hard coding, apply layout
75 // TODO: remove hard coding, apply layout
77 value->resize(100,50);
76 value->resize(100,50);
78 value->setPos(xPos, yPos-barHeight/2);
77 value->setPos(xPos, yPos-barHeight/2);
79 value->setPen(QPen(QColor(255,255,255,255)));
78 value->setPen(QPen(QColor(255,255,255,255)));
80
79
81 if (mSeries->valueAt(set,category) != 0) {
80 if (mSeries->valueAt(set,category) != 0) {
82 value->setValueString(QString::number(mSeries->valueAt(set,category)));
81 value->setValueString(QString::number(mSeries->valueAt(set,category)));
83 } else {
82 } else {
84 value->setValueString(QString(""));
83 value->setValueString(QString(""));
85 }
84 }
86
85
87 itemIndex++;
86 itemIndex++;
88 xPos += mBarWidth;
87 xPos += mBarWidth;
89 }
88 }
90 }
89 }
91 }
90 }
92
91
93 #include "moc_barpresenter_p.cpp"
92 #include "moc_barpresenter_p.cpp"
94
93
95 QTCOMMERCIALCHART_END_NAMESPACE
94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,30 +1,30
1 #ifndef BARPRESENTER_H
1 #ifndef BARPRESENTER_H
2 #define BARPRESENTER_H
2 #define BARPRESENTER_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "barpresenterbase_p.h"
5 #include "barpresenterbase_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QBarSeries;
10 class QBarSeries;
11
11
12 // Presenter for parallel bars. Grouping of bars is done on category basis.
12 // Presenter for parallel bars. Grouping of bars is done on category basis.
13 class BarPresenter : public BarPresenterBase
13 class BarPresenter : public BarPresenterBase
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 explicit BarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
17 explicit BarPresenter(QBarSeries *series, QChart *parent = 0);
18
18
19 private:
19 private:
20
20
21 // From BarPresenterBase
21 // From BarPresenterBase
22 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
22 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
23
23
24 private:
24 private:
25 // Data
25 // Data
26 };
26 };
27
27
28 QTCOMMERCIALCHART_END_NAMESPACE
28 QTCOMMERCIALCHART_END_NAMESPACE
29
29
30 #endif // BARPRESENTER_H
30 #endif // BARPRESENTER_H
@@ -1,150 +1,190
1 #include "barpresenterbase_p.h"
1 #include "barpresenterbase_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "barlabel_p.h"
4 #include "barlabel_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include "qbarseries.h"
7 #include "qbarseries.h"
8 #include "qchart.h"
9 #include "qchartaxis.h"
10 #include "qchartaxiscategories.h"
8 #include <QDebug>
11 #include <QDebug>
9 #include <QToolTip>
12 #include <QToolTip>
10
13
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
15
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
16 BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent)
14 : ChartItem(parent)
17 : ChartItem(parent)
15 ,mBarWidth(20) // TODO: remove hard coding, when we have layout code ready
16 ,mLayoutSet(false)
18 ,mLayoutSet(false)
17 ,mSeparatorsEnabled(false)
19 ,mSeparatorsEnabled(false)
18 ,mSeries(series)
20 ,mSeries(series)
21 ,mChart(parent)
19 {
22 {
20 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
23 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
24 initAxisLabels();
21 dataChanged();
25 dataChanged();
22 }
26 }
23
27
24 BarPresenterBase::~BarPresenterBase()
28 BarPresenterBase::~BarPresenterBase()
25 {
29 {
26 disconnect(this,SLOT(showToolTip(QPoint,QString)));
30 disconnect(this,SLOT(showToolTip(QPoint,QString)));
27 }
31 }
28
32
29 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
33 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
30 {
34 {
31 if (!mLayoutSet) {
35 if (!mLayoutSet) {
32 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
36 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
33 return;
37 return;
34 }
38 }
35 foreach(QGraphicsItem* i, childItems()) {
39 foreach(QGraphicsItem* i, childItems()) {
36 i->paint(painter,option,widget);
40 i->paint(painter,option,widget);
37 }
41 }
38 }
42 }
39
43
40 QRectF BarPresenterBase::boundingRect() const
44 QRectF BarPresenterBase::boundingRect() const
41 {
45 {
42 return QRectF(0,0,mWidth,mHeight);
46 return QRectF(0,0,mWidth,mHeight);
43 }
47 }
44
48
45 void BarPresenterBase::setBarWidth( int w )
46 {
47 mBarWidth = w;
48 }
49
50 void BarPresenterBase::dataChanged()
49 void BarPresenterBase::dataChanged()
51 {
50 {
52 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
51 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
53 // Delete old bars
52 // Delete old bars
54 foreach (QGraphicsItem* item, childItems()) {
53 foreach (QGraphicsItem* item, childItems()) {
55 delete item;
54 delete item;
56 }
55 }
57
56
58 mBars.clear();
57 mBars.clear();
59 mLabels.clear();
58 mLabels.clear();
60 mSeparators.clear();
59 mSeparators.clear();
61 mFloatingValues.clear();
60 mFloatingValues.clear();
62
61
63 // Create new graphic items for bars
62 // Create new graphic items for bars
64 for (int c=0; c<mSeries->categoryCount(); c++) {
63 for (int c=0; c<mSeries->categoryCount(); c++) {
65 QString category = mSeries->categoryName(c);
64 QString category = mSeries->categoryName(c);
66 for (int s=0; s<mSeries->barsetCount(); s++) {
65 for (int s=0; s<mSeries->barsetCount(); s++) {
67 QBarSet *set = mSeries->barsetAt(s);
66 QBarSet *set = mSeries->barsetAt(s);
68 Bar *bar = new Bar(category,this);
67 Bar *bar = new Bar(category,this);
69 childItems().append(bar);
68 childItems().append(bar);
70 mBars.append(bar);
69 mBars.append(bar);
71 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
70 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
72 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
71 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
73 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
72 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
74 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
73 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
75 }
74 }
76 }
75 }
77
76
78 // Create labels
77 // Create labels
78 /*
79 int count = mSeries->categoryCount();
79 int count = mSeries->categoryCount();
80 for (int i=0; i<count; i++) {
80 for (int i=0; i<count; i++) {
81 BarLabel* label = new BarLabel(this);
81 BarLabel* label = new BarLabel(this);
82 label->set(mSeries->categoryName(i));
82 label->set(mSeries->categoryName(i));
83 childItems().append(label);
83 childItems().append(label);
84 mLabels.append(label);
84 mLabels.append(label);
85 }
85 }
86 */
86
87
87 // Create separators
88 // Create separators
88 count = mSeries->categoryCount() - 1; // There is one less separator than columns
89 int count = mSeries->categoryCount() - 1; // There is one less separator than columns
89 for (int i=0; i<count; i++) {
90 for (int i=0; i<count; i++) {
90 Separator* sep = new Separator(this);
91 Separator* sep = new Separator(this);
91 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
92 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
92 sep->setVisible(mSeparatorsEnabled);
93 sep->setVisible(mSeparatorsEnabled);
93 childItems().append(sep);
94 childItems().append(sep);
94 mSeparators.append(sep);
95 mSeparators.append(sep);
95 }
96 }
96
97
97 // Create floating values
98 // Create floating values
98 for (int category=0; category<mSeries->categoryCount(); category++) {
99 for (int category=0; category<mSeries->categoryCount(); category++) {
99 for (int s=0; s<mSeries->barsetCount(); s++) {
100 for (int s=0; s<mSeries->barsetCount(); s++) {
100 QBarSet *set = mSeries->barsetAt(s);
101 QBarSet *set = mSeries->barsetAt(s);
101 BarValue *value = new BarValue(*set, this);
102 BarValue *value = new BarValue(*set, this);
102 childItems().append(value);
103 childItems().append(value);
103 mFloatingValues.append(value);
104 mFloatingValues.append(value);
104 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
105 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
105 }
106 }
106 }
107 }
107 }
108 }
108
109
110 void BarPresenterBase::initAxisLabels()
111 {
112 int count = mSeries->categoryCount();
113 if (0 == count) {
114 return;
115 }
116
117 mChart->axisX()->setTicksCount(count);
118
119 qreal min = 0;
120 qreal max = mSeries->categoryCount();
121
122 mChart->axisX()->setMin(min);
123 mChart->axisX()->setMax(max);
124 qreal step = (max-min)/count;
125 QChartAxisCategories& categories = mChart->axisX()->categories();
126 categories.clear();
127 for (int i=0; i<count; i++) {
128 qDebug() << "initAxisLabels" << min << mSeries->categoryName(i);
129 categories.insert(min,mSeries->categoryName(i));
130 min += step;
131 }
132 mChart->axisX()->setLabelsVisible(true);
133 }
134
109 //handlers
135 //handlers
110
136
111 void BarPresenterBase::handleModelChanged(int index)
137 void BarPresenterBase::handleModelChanged(int index)
112 {
138 {
113 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
139 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
114 dataChanged();
140 dataChanged();
115 }
141 }
116
142
117 void BarPresenterBase::handleDomainChanged(const Domain& domain)
143 void BarPresenterBase::handleDomainChanged(const Domain& domain)
118 {
144 {
119 // qDebug() << "BarPresenterBase::handleDomainChanged";
145 qDebug() << "BarPresenterBase::handleDomainChanged";
120 // TODO: Figure out the use case for this.
146 /*
121 // Affects the size of visible item, so layout is changed.
147 int count = mSeries->categoryCount();
122 // layoutChanged();
148 if (0 == count) {
149 return;
150 }
151
152 // Position labels to domain
153 qreal min = domain.minX();
154 qreal max = domain.maxX();
155 qreal step = (max-min)/count;
156 QChartAxisCategories& categories = mChart->axisX()->categories();
157 categories.clear();
158 for (int i=0; i<count; i++) {
159 categories.insert(min,mSeries->categoryName(i));
160 min += step;
161 }
162 */
123 }
163 }
124
164
125 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
165 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
126 {
166 {
127 mWidth = rect.width();
167 mWidth = rect.width();
128 mHeight = rect.height();
168 mHeight = rect.height();
129 layoutChanged();
169 layoutChanged();
130 mLayoutSet = true;
170 mLayoutSet = true;
131 setPos(rect.topLeft());
171 setPos(rect.topLeft());
132 }
172 }
133
173
134 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
174 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
135 {
175 {
136 // TODO: cool tooltip instead of default
176 // TODO: cool tooltip instead of default
137 QToolTip::showText(pos,tip);
177 QToolTip::showText(pos,tip);
138 }
178 }
139
179
140 void BarPresenterBase::enableSeparators(bool enabled)
180 void BarPresenterBase::enableSeparators(bool enabled)
141 {
181 {
142 for (int i=0; i<mSeparators.count(); i++) {
182 for (int i=0; i<mSeparators.count(); i++) {
143 mSeparators.at(i)->setVisible(enabled);
183 mSeparators.at(i)->setVisible(enabled);
144 }
184 }
145 mSeparatorsEnabled = enabled;
185 mSeparatorsEnabled = enabled;
146 }
186 }
147
187
148 #include "moc_barpresenterbase_p.cpp"
188 #include "moc_barpresenterbase_p.cpp"
149
189
150 QTCOMMERCIALCHART_END_NAMESPACE
190 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,68 +1,70
1 #ifndef BARPRESENTERBASE_H
1 #ifndef BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qbarseries.h"
5 #include "qbarseries.h"
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class Bar;
12 class Bar;
13 class BarLabel;
13 class BarLabel;
14 class Separator;
14 class Separator;
15 class BarValue;
15 class BarValue;
16 class QChartAxisCategories;
17 class QChart;
16
18
17 // Common implemantation of different presenters. Not to be instantiated.
19 // Common implemantation of different presenters. Not to be instantiated.
18 // TODO: combine this with BarPresenter and derive other presenters from it?
20 // TODO: combine this with BarPresenter and derive other presenters from it?
19 class BarPresenterBase : public QObject, public ChartItem
21 class BarPresenterBase : public QObject, public ChartItem
20 {
22 {
21 Q_OBJECT
23 Q_OBJECT
22 public:
24 public:
23 BarPresenterBase(QBarSeries *series, QGraphicsItem *parent = 0);
25 BarPresenterBase(QBarSeries *series, QChart *parent = 0);
24 virtual ~BarPresenterBase();
26 virtual ~BarPresenterBase();
25
27
26 public:
28 public:
27 // From QGraphicsItem
29 // From QGraphicsItem
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
31 QRectF boundingRect() const;
30
32
31 // TODO: these may change with layout awarness.
32 void setBarWidth( int w );
33
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
33 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
34 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
35 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
37
36
37 protected:
38 void initAxisLabels();
39
38 public slots:
40 public slots:
39 void handleModelChanged(int index);
41 void handleModelChanged(int index);
40 void handleDomainChanged(const Domain& domain);
42 void handleDomainChanged(const Domain& domain);
41 void handleGeometryChanged(const QRectF& size);
43 void handleGeometryChanged(const QRectF& size);
42
44
43 // Internal slots
45 // Internal slots
44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
46 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45 void enableSeparators(bool enabled);
47 void enableSeparators(bool enabled);
46
48
47 protected:
49 protected:
48
50
49 // TODO: consider these.
51 // TODO: consider these.
50 int mHeight; // Layout spesific
52 int mHeight; // Layout spesific
51 int mWidth;
53 int mWidth;
52 qreal mBarWidth;
54 qreal mBarWidth;
53
55
54 bool mLayoutSet; // True, if component has been laid out.
56 bool mLayoutSet; // True, if component has been laid out.
55 bool mSeparatorsEnabled;
57 bool mSeparatorsEnabled;
56
58
57 // Not owned.
59 // Not owned.
58 QBarSeries* mSeries;
60 QBarSeries* mSeries;
59 QList<Bar*> mBars;
61 QList<Bar*> mBars;
60 QList<BarLabel*> mLabels;
62 QList<BarLabel*> mLabels;
61 QList<Separator*> mSeparators;
63 QList<Separator*> mSeparators;
62 QList<BarValue*> mFloatingValues;
64 QList<BarValue*> mFloatingValues;
63
65 QChart* mChart;
64 };
66 };
65
67
66 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
67
69
68 #endif // BARPRESENTERBASE_H
70 #endif // BARPRESENTERBASE_H
@@ -1,110 +1,110
1 #include "percentbarpresenter_p.h"
1 #include "percentbarpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11
11
12 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
12 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) :
13 BarPresenterBase(series, parent)
13 BarPresenterBase(series, parent)
14 {
14 {
15 }
15 }
16
16
17 void PercentBarPresenter::layoutChanged()
17 void PercentBarPresenter::layoutChanged()
18 {
18 {
19 // Scale bars to new layout
19 // Scale bars to new layout
20 // Layout for bars:
20 // Layout for bars:
21 if (mSeries->barsetCount() <= 0) {
21 if (mSeries->barsetCount() <= 0) {
22 qDebug() << "No sets in model!";
22 qDebug() << "No sets in model!";
23 // Nothing to do.
23 // Nothing to do.
24 return;
24 return;
25 }
25 }
26
26
27 if (childItems().count() == 0) {
27 if (childItems().count() == 0) {
28 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
28 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
29 return;
29 return;
30 }
30 }
31
31
32 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
32 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
33 qreal tW = mWidth;
33 qreal tW = mWidth;
34 qreal tC = mSeries->categoryCount() + 1;
34 qreal tC = mSeries->categoryCount() + 1;
35 qreal cC = mSeries->categoryCount() * 2 + 1;
35 qreal cC = mSeries->categoryCount() * 2 + 1;
36 mBarWidth = tW / cC;
36 mBarWidth = tW / cC;
37 qreal xStep = (tW/tC);
37 qreal xStep = (tW/tC);
38 qreal xPos = ((tW/tC) - mBarWidth / 2);
38 qreal xPos = ((tW/tC) - mBarWidth / 2);
39 qreal h = mHeight;
39 qreal h = mHeight;
40
40
41 int itemIndex(0);
41 int itemIndex(0);
42 int labelIndex(0);
42 int labelIndex(0);
43 for (int category = 0; category < mSeries->categoryCount(); category++) {
43 for (int category = 0; category < mSeries->categoryCount(); category++) {
44 qreal colSum = mSeries->categorySum(category);
44 qreal colSum = mSeries->categorySum(category);
45 qreal scale = (h / colSum);
45 qreal scale = (h / colSum);
46 qreal yPos = h;
46 qreal yPos = h;
47 for (int set=0; set < mSeries->barsetCount(); set++) {
47 for (int set=0; set < mSeries->barsetCount(); set++) {
48 qreal barHeight = mSeries->valueAt(set, category) * scale;
48 qreal barHeight = mSeries->valueAt(set, category) * scale;
49 Bar* bar = mBars.at(itemIndex);
49 Bar* bar = mBars.at(itemIndex);
50
50
51 // TODO: width settable per bar?
51 // TODO: width settable per bar?
52 bar->resize(mBarWidth, barHeight);
52 bar->resize(mBarWidth, barHeight);
53 bar->setBrush(mSeries->barsetAt(set)->brush());
53 bar->setBrush(mSeries->barsetAt(set)->brush());
54 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
55 itemIndex++;
55 itemIndex++;
56 yPos -= barHeight;
56 yPos -= barHeight;
57 }
57 }
58
58
59 // TODO: Layout for labels, remove magic number
59 // TODO: Layout for labels, remove magic number
60 BarLabel* label = mLabels.at(labelIndex);
60 BarLabel* label = mLabels.at(labelIndex);
61 label->setPos(xPos, mHeight + 20);
61 label->setPos(xPos, mHeight + 20);
62 labelIndex++;
62 labelIndex++;
63 xPos += xStep;
63 xPos += xStep;
64 }
64 }
65
65
66 // Position separators
66 // Position separators
67 xPos = xStep + xStep/2;
67 xPos = xStep + xStep/2;
68 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
68 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
69 Separator* sep = mSeparators.at(s);
69 Separator* sep = mSeparators.at(s);
70 sep->setPos(xPos,0);
70 sep->setPos(xPos,0);
71 sep->setSize(QSizeF(1,mHeight));
71 sep->setSize(QSizeF(1,mHeight));
72 xPos += xStep;
72 xPos += xStep;
73 }
73 }
74
74
75 // Position floating values
75 // Position floating values
76 itemIndex = 0;
76 itemIndex = 0;
77 xPos = (tW/tC);
77 xPos = (tW/tC);
78 for (int category=0; category < mSeries->categoryCount(); category++) {
78 for (int category=0; category < mSeries->categoryCount(); category++) {
79 qreal yPos = h;
79 qreal yPos = h;
80 qreal colSum = mSeries->categorySum(category);
80 qreal colSum = mSeries->categorySum(category);
81 qreal scale = (h / colSum);
81 qreal scale = (h / colSum);
82 for (int set=0; set < mSeries->barsetCount(); set++) {
82 for (int set=0; set < mSeries->barsetCount(); set++) {
83 qreal barHeight = mSeries->valueAt(set,category) * scale;
83 qreal barHeight = mSeries->valueAt(set,category) * scale;
84 BarValue* value = mFloatingValues.at(itemIndex);
84 BarValue* value = mFloatingValues.at(itemIndex);
85
85
86 // TODO: remove hard coding, apply layout
86 // TODO: remove hard coding, apply layout
87 value->resize(100,50);
87 value->resize(100,50);
88 value->setPos(xPos, yPos-barHeight/2);
88 value->setPos(xPos, yPos-barHeight/2);
89 value->setPen(QPen(QColor(255,255,255,255)));
89 value->setPen(QPen(QColor(255,255,255,255)));
90
90
91 if (mSeries->valueAt(set,category) != 0) {
91 if (mSeries->valueAt(set,category) != 0) {
92 int p = mSeries->percentageAt(set,category) * 100;
92 int p = mSeries->percentageAt(set,category) * 100;
93 QString vString(QString::number(p));
93 QString vString(QString::number(p));
94 vString.truncate(3);
94 vString.truncate(3);
95 vString.append("%");
95 vString.append("%");
96 value->setValueString(vString);
96 value->setValueString(vString);
97 } else {
97 } else {
98 value->setValueString(QString(""));
98 value->setValueString(QString(""));
99 }
99 }
100
100
101 itemIndex++;
101 itemIndex++;
102 yPos -= barHeight;
102 yPos -= barHeight;
103 }
103 }
104 xPos += xStep;
104 xPos += xStep;
105 }
105 }
106 }
106 }
107
107
108 #include "moc_percentbarpresenter_p.cpp"
108 #include "moc_percentbarpresenter_p.cpp"
109
109
110 QTCOMMERCIALCHART_END_NAMESPACE
110 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,29 +1,29
1 #ifndef PERCENTBARPRESENTER_H
1 #ifndef PERCENTBARPRESENTER_H
2 #define PERCENTBARPRESENTER_H
2 #define PERCENTBARPRESENTER_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "bar_p.h"
5 #include "bar_p.h"
6 #include "qpercentbarseries.h"
6 #include "qpercentbarseries.h"
7 #include "barpresenterbase_p.h"
7 #include "barpresenterbase_p.h"
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class PercentBarPresenter : public BarPresenterBase
12 class PercentBarPresenter : public BarPresenterBase
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
16 PercentBarPresenter(QBarSeries *series, QChart *parent = 0);
17
17
18 private:
18 private:
19
19
20 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
20 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
21
21
22 private:
22 private:
23
23
24 // Data
24 // Data
25 };
25 };
26
26
27 QTCOMMERCIALCHART_END_NAMESPACE
27 QTCOMMERCIALCHART_END_NAMESPACE
28
28
29 #endif // PERCENTBARPRESENTER_H
29 #endif // PERCENTBARPRESENTER_H
@@ -1,221 +1,222
1 #include <QDebug>
1 #include <QDebug>
2 #include "qbarseries.h"
2 #include "qbarseries.h"
3 #include "qbarset.h"
3 #include "qbarset.h"
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 /*!
8 /*!
9 \class QBarSeries
9 \class QBarSeries
10 \brief part of QtCommercial chart API.
10 \brief part of QtCommercial chart API.
11
11
12 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
12 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
13 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
13 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
14 by QStringList.
14 by QStringList.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn virtual QSeriesType QBarSeries::type() const
22 \fn virtual QSeriesType QBarSeries::type() const
23 \brief Returns type of series.
23 \brief Returns type of series.
24 \sa QSeries, QSeriesType
24 \sa QSeries, QSeriesType
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
28 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
29 \brief \internal \a pos \a tip
29 \brief \internal \a pos \a tip
30 */
30 */
31
31
32 /*!
32 /*!
33 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
33 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
34 QBarSeries is QObject which is a child of a \a parent.
34 QBarSeries is QObject which is a child of a \a parent.
35 */
35 */
36 QBarSeries::QBarSeries(QStringList categories, QObject *parent)
36 QBarSeries::QBarSeries(QStringList categories, QObject *parent)
37 : QSeries(parent)
37 : QSeries(parent)
38 ,mModel(new BarChartModel(categories, this))
38 ,mModel(new BarChartModel(categories, this))
39 {
39 {
40 }
40 }
41
41
42 /*!
42 /*!
43 Adds a set of bars to series. Takes ownership of \a set.
43 Adds a set of bars to series. Takes ownership of \a set.
44 Connects the clicked(QString) and rightClicked(QString) signals
44 Connects the clicked(QString) and rightClicked(QString) signals
45 of \a set to this series
45 of \a set to this series
46 */
46 */
47 void QBarSeries::addBarSet(QBarSet *set)
47 void QBarSeries::addBarSet(QBarSet *set)
48 {
48 {
49 mModel->addBarSet(set);
49 mModel->addBarSet(set);
50 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
50 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
51 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
51 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
52 }
52 }
53
53
54 /*!
54 /*!
55 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
55 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
56 Disconnects the clicked(QString) and rightClicked(QString) signals
56 Disconnects the clicked(QString) and rightClicked(QString) signals
57 of \a set from this series
57 of \a set from this series
58 */
58 */
59 void QBarSeries::removeBarSet(QBarSet *set)
59 void QBarSeries::removeBarSet(QBarSet *set)
60 {
60 {
61 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
61 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
62 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
62 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
63 mModel->removeBarSet(set);
63 mModel->removeBarSet(set);
64 }
64 }
65
65
66 /*!
66 /*!
67 Returns number of sets in series.
67 Returns number of sets in series.
68 */
68 */
69 int QBarSeries::barsetCount()
69 int QBarSeries::barsetCount()
70 {
70 {
71 return mModel->barsetCount();
71 return mModel->barsetCount();
72 }
72 }
73
73
74 /*!
74 /*!
75 Returns number of categories in series
75 Returns number of categories in series
76 */
76 */
77 int QBarSeries::categoryCount()
77 int QBarSeries::categoryCount()
78 {
78 {
79 return mModel->categoryCount();
79 return mModel->categoryCount();
80 }
80 }
81
81
82 /*!
82 /*!
83 Returns a list of sets in series. Keeps ownership of sets.
83 Returns a list of sets in series. Keeps ownership of sets.
84 */
84 */
85 QList<QBarSet*> QBarSeries::barSets()
85 QList<QBarSet*> QBarSeries::barSets()
86 {
86 {
87 return mModel->barSets();
87 return mModel->barSets();
88 }
88 }
89
89
90 /*!
90 /*!
91 \internal \a index
91 \internal \a index
92 */
92 */
93 QBarSet* QBarSeries::barsetAt(int index)
93 QBarSet* QBarSeries::barsetAt(int index)
94 {
94 {
95 return mModel->setAt(index);
95 return mModel->setAt(index);
96 }
96 }
97
97
98 /*!
98 /*!
99 Returns legend of series.
99 Returns legend of series.
100 */
100 */
101 QList<QSeries::Legend> QBarSeries::legend()
101 QList<QSeries::Legend> QBarSeries::legend()
102 {
102 {
103 return mModel->legend();
103 return mModel->legend();
104 }
104 }
105
105
106 /*!
106 /*!
107 \internal \a category
107 \internal \a category
108 */
108 */
109 QString QBarSeries::categoryName(int category)
109 QString QBarSeries::categoryName(int category)
110 {
110 {
111 return mModel->categoryName(category);
111 return mModel->categoryName(category);
112 }
112 }
113
113
114 /*!
114 /*!
115 Enables or disables tooltip depending on parameter \a enabled.
115 Enables or disables tooltip depending on parameter \a enabled.
116 Tooltip shows the name of set, when mouse is hovering on top of bar.
116 Tooltip shows the name of set, when mouse is hovering on top of bar.
117 Calling without parameter \a enabled, enables the tooltip
117 Calling without parameter \a enabled, enables the tooltip
118 */
118 */
119 void QBarSeries::setToolTipEnabled(bool enabled)
119 void QBarSeries::setToolTipEnabled(bool enabled)
120 {
120 {
121 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
121 if (enabled) {
122 if (enabled) {
122 for (int i=0; i<mModel->barsetCount(); i++) {
123 for (int i=0; i<mModel->barsetCount(); i++) {
123 QBarSet *set = mModel->setAt(i);
124 QBarSet *set = mModel->setAt(i);
124 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
125 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
125 }
126 }
126 } else {
127 } else {
127 for (int i=0; i<mModel->barsetCount(); i++) {
128 for (int i=0; i<mModel->barsetCount(); i++) {
128 QBarSet *set = mModel->setAt(i);
129 QBarSet *set = mModel->setAt(i);
129 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
130 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
130 }
131 }
131 }
132 }
132 }
133 }
133
134
134 /*!
135 /*!
135 Enables or disables separators depending on parameter \a enabled.
136 Enables or disables separators depending on parameter \a enabled.
136 Separators are visual elements that are drawn between categories.
137 Separators are visual elements that are drawn between categories.
137 Calling without parameter \a enabled, enables the separators
138 Calling without parameter \a enabled, enables the separators
138 */
139 */
139 void QBarSeries::setSeparatorsEnabled(bool enabled)
140 void QBarSeries::setSeparatorsEnabled(bool enabled)
140 {
141 {
141 // TODO: toggle
142 // TODO: toggle
142 // emit separatorsEnabled(enabled);
143 // emit separatorsEnabled(enabled);
143 }
144 }
144
145
145
146
146 /*!
147 /*!
147 \internal \a category
148 \internal \a category
148 */
149 */
149 void QBarSeries::barsetClicked(QString category)
150 void QBarSeries::barsetClicked(QString category)
150 {
151 {
151 emit clicked(qobject_cast<QBarSet*>(sender()), category);
152 emit clicked(qobject_cast<QBarSet*>(sender()), category);
152 }
153 }
153
154
154 /*!
155 /*!
155 \internal \a category
156 \internal \a category
156 */
157 */
157 void QBarSeries::barsetRightClicked(QString category)
158 void QBarSeries::barsetRightClicked(QString category)
158 {
159 {
159 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
160 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
160 }
161 }
161
162
162
163
163 /*!
164 /*!
164 \internal
165 \internal
165 */
166 */
166 qreal QBarSeries::min()
167 qreal QBarSeries::min()
167 {
168 {
168 return mModel->min();
169 return mModel->min();
169 }
170 }
170
171
171 /*!
172 /*!
172 \internal
173 \internal
173 */
174 */
174 qreal QBarSeries::max()
175 qreal QBarSeries::max()
175 {
176 {
176 return mModel->max();
177 return mModel->max();
177 }
178 }
178
179
179 /*!
180 /*!
180 \internal \a set \a category
181 \internal \a set \a category
181 */
182 */
182 qreal QBarSeries::valueAt(int set, int category)
183 qreal QBarSeries::valueAt(int set, int category)
183 {
184 {
184 return mModel->valueAt(set,category);
185 return mModel->valueAt(set,category);
185 }
186 }
186
187
187 /*!
188 /*!
188 \internal \a set \a category
189 \internal \a set \a category
189 */
190 */
190 qreal QBarSeries::percentageAt(int set, int category)
191 qreal QBarSeries::percentageAt(int set, int category)
191 {
192 {
192 return mModel->percentageAt(set,category);
193 return mModel->percentageAt(set,category);
193 }
194 }
194
195
195 /*!
196 /*!
196 \internal \a category
197 \internal \a category
197 */
198 */
198 qreal QBarSeries::categorySum(int category)
199 qreal QBarSeries::categorySum(int category)
199 {
200 {
200 return mModel->categorySum(category);
201 return mModel->categorySum(category);
201 }
202 }
202
203
203 /*!
204 /*!
204 \internal
205 \internal
205 */
206 */
206 qreal QBarSeries::maxCategorySum()
207 qreal QBarSeries::maxCategorySum()
207 {
208 {
208 return mModel->maxCategorySum();
209 return mModel->maxCategorySum();
209 }
210 }
210
211
211 /*!
212 /*!
212 \internal
213 \internal
213 */
214 */
214 BarChartModel& QBarSeries::model()
215 BarChartModel& QBarSeries::model()
215 {
216 {
216 return *mModel;
217 return *mModel;
217 }
218 }
218
219
219 #include "moc_qbarseries.cpp"
220 #include "moc_qbarseries.cpp"
220
221
221 QTCOMMERCIALCHART_END_NAMESPACE
222 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,113 +1,113
1 #include "stackedbarpresenter_p.h"
1 #include "stackedbarpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QChart *parent) :
12 BarPresenterBase(series,parent)
12 BarPresenterBase(series,parent)
13 {
13 {
14 }
14 }
15
15
16 StackedBarPresenter::~StackedBarPresenter()
16 StackedBarPresenter::~StackedBarPresenter()
17 {
17 {
18 }
18 }
19
19
20
20
21 void StackedBarPresenter::layoutChanged()
21 void StackedBarPresenter::layoutChanged()
22 {
22 {
23 // Scale bars to new layout
23 // Scale bars to new layout
24 // Layout for bars:
24 // Layout for bars:
25 if (mSeries->barsetCount() <= 0) {
25 if (mSeries->barsetCount() <= 0) {
26 qDebug() << "No sets in model!";
26 qDebug() << "No sets in model!";
27 // Nothing to do.
27 // Nothing to do.
28 return;
28 return;
29 }
29 }
30
30
31 if (mSeries->categoryCount() == 0) {
31 if (mSeries->categoryCount() == 0) {
32 qDebug() << "No categories in model!";
32 qDebug() << "No categories in model!";
33 // Nothing to do
33 // Nothing to do
34 return;
34 return;
35 }
35 }
36
36
37 if (childItems().count() == 0) {
37 if (childItems().count() == 0) {
38 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
38 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
39 return;
39 return;
40 }
40 }
41
41
42 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
42 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
43 qreal maxSum = mSeries->maxCategorySum();
43 qreal maxSum = mSeries->maxCategorySum();
44 qreal h = mHeight;
44 qreal h = mHeight;
45 qreal scale = (h / maxSum);
45 qreal scale = (h / maxSum);
46 qreal tW = mWidth;
46 qreal tW = mWidth;
47 qreal tC = mSeries->categoryCount() + 1;
47 qreal tC = mSeries->categoryCount() + 1;
48 qreal cC = mSeries->categoryCount() * 2 + 1;
48 qreal cC = mSeries->categoryCount() * 2 + 1;
49 mBarWidth = tW / cC;
49 mBarWidth = tW / cC;
50 qreal xStep = (tW/tC);
50 qreal xStep = (tW/tC);
51 qreal xPos = ((tW/tC) - mBarWidth / 2);
51 qreal xPos = ((tW/tC) - mBarWidth / 2);
52
52
53 int itemIndex(0);
53 int itemIndex(0);
54 int labelIndex(0);
54 int labelIndex(0);
55 for (int category = 0; category < mSeries->categoryCount(); category++) {
55 for (int category = 0; category < mSeries->categoryCount(); category++) {
56 qreal yPos = h;
56 qreal yPos = h;
57 for (int set=0; set < mSeries->barsetCount(); set++) {
57 for (int set=0; set < mSeries->barsetCount(); set++) {
58 qreal barHeight = mSeries->valueAt(set, category) * scale;
58 qreal barHeight = mSeries->valueAt(set, category) * scale;
59 Bar* bar = mBars.at(itemIndex);
59 Bar* bar = mBars.at(itemIndex);
60
60
61 bar->resize(mBarWidth, barHeight);
61 bar->resize(mBarWidth, barHeight);
62 bar->setBrush(mSeries->barsetAt(set)->brush());
62 bar->setBrush(mSeries->barsetAt(set)->brush());
63 bar->setPos(xPos, yPos-barHeight);
63 bar->setPos(xPos, yPos-barHeight);
64 itemIndex++;
64 itemIndex++;
65 yPos -= barHeight;
65 yPos -= barHeight;
66 }
66 }
67
67
68 // TODO: Layout for labels, remove magic number
68 // TODO: Layout for labels, remove magic number
69 BarLabel* label = mLabels.at(labelIndex);
69 // BarLabel* label = mLabels.at(labelIndex);
70 label->setPos(xPos, mHeight + 20);
70 // label->setPos(xPos, mHeight + 20);
71 labelIndex++;
71 labelIndex++;
72 xPos += xStep;
72 xPos += xStep;
73 }
73 }
74
74
75 // Position separators
75 // Position separators
76 xPos = xStep + xStep/2;
76 xPos = xStep + xStep/2;
77 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
77 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
78 Separator* sep = mSeparators.at(s);
78 Separator* sep = mSeparators.at(s);
79 sep->setPos(xPos,0);
79 sep->setPos(xPos,0);
80 sep->setSize(QSizeF(1,mHeight));
80 sep->setSize(QSizeF(1,mHeight));
81 xPos += xStep;
81 xPos += xStep;
82 }
82 }
83
83
84 // Position floating values
84 // Position floating values
85 itemIndex = 0;
85 itemIndex = 0;
86 xPos = (tW/tC);
86 xPos = (tW/tC);
87 for (int category=0; category < mSeries->categoryCount(); category++) {
87 for (int category=0; category < mSeries->categoryCount(); category++) {
88 qreal yPos = h;
88 qreal yPos = h;
89 for (int set=0; set < mSeries->barsetCount(); set++) {
89 for (int set=0; set < mSeries->barsetCount(); set++) {
90 qreal barHeight = mSeries->valueAt(set,category) * scale;
90 qreal barHeight = mSeries->valueAt(set,category) * scale;
91 BarValue* value = mFloatingValues.at(itemIndex);
91 BarValue* value = mFloatingValues.at(itemIndex);
92
92
93 // TODO: remove hard coding, apply layout
93 // TODO: remove hard coding, apply layout
94 value->resize(100,50);
94 value->resize(100,50);
95 value->setPos(xPos, yPos-barHeight/2);
95 value->setPos(xPos, yPos-barHeight/2);
96 value->setPen(QPen(QColor(255,255,255,255)));
96 value->setPen(QPen(QColor(255,255,255,255)));
97
97
98 if (mSeries->valueAt(set,category) != 0) {
98 if (mSeries->valueAt(set,category) != 0) {
99 value->setValueString(QString::number(mSeries->valueAt(set,category)));
99 value->setValueString(QString::number(mSeries->valueAt(set,category)));
100 } else {
100 } else {
101 value->setValueString(QString(""));
101 value->setValueString(QString(""));
102 }
102 }
103
103
104 itemIndex++;
104 itemIndex++;
105 yPos -= barHeight;
105 yPos -= barHeight;
106 }
106 }
107 xPos += xStep;
107 xPos += xStep;
108 }
108 }
109 }
109 }
110
110
111 #include "moc_stackedbarpresenter_p.cpp"
111 #include "moc_stackedbarpresenter_p.cpp"
112
112
113 QTCOMMERCIALCHART_END_NAMESPACE
113 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,28 +1,28
1 #ifndef STACKEDBARPRESENTER_H
1 #ifndef STACKEDBARPRESENTER_H
2 #define STACKEDBARPRESENTER_H
2 #define STACKEDBARPRESENTER_H
3
3
4 #include "barpresenterbase_p.h"
4 #include "barpresenterbase_p.h"
5 #include "qstackedbarseries.h"
5 #include "qstackedbarseries.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class StackedBarPresenter : public BarPresenterBase
10 class StackedBarPresenter : public BarPresenterBase
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0);
14 StackedBarPresenter(QBarSeries *series, QChart *parent = 0);
15 ~StackedBarPresenter();
15 ~StackedBarPresenter();
16
16
17 private:
17 private:
18 // From BarPresenterBase
18 // From BarPresenterBase
19 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
19 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
20
20
21 private:
21 private:
22
22
23 // Data
23 // Data
24 };
24 };
25
25
26 QTCOMMERCIALCHART_END_NAMESPACE
26 QTCOMMERCIALCHART_END_NAMESPACE
27
27
28 #endif // STACKEDBARPRESENTER_H
28 #endif // STACKEDBARPRESENTER_H
General Comments 0
You need to be logged in to leave comments. Login now