##// END OF EJS Templates
added labels to series, intergrated with test app. minor hack to test app
sauimone -
r167:023d2c8150a8
parent child
Show More
@@ -25,6 +25,7 int BarChartModel::addData(QList<qreal> data)
25 DataContainer* c = new DataContainer(data,mRunningId);
25 DataContainer* c = new DataContainer(data,mRunningId);
26 mDataModel.append(c);
26 mDataModel.append(c);
27 mRunningId++;
27 mRunningId++;
28 emit modelUpdated();
28 return mRunningId-1;
29 return mRunningId-1;
29 }
30 }
30
31
@@ -37,6 +38,7 void BarChartModel::removeData(int id)
37 delete c;
38 delete c;
38 }
39 }
39 }
40 }
41 emit modelUpdated();
40 }
42 }
41
43
42 int BarChartModel::countRows()
44 int BarChartModel::countRows()
@@ -66,18 +68,18 int BarChartModel::countTotalItems()
66 return total;
68 return total;
67 }
69 }
68
70
69 int BarChartModel::min()
71 qreal BarChartModel::min()
70 {
72 {
71 // qDebug() << "BarChartModel::min";
73 // qDebug() << "BarChartModel::min";
72 Q_ASSERT(mDataModel.count() > 0);
74 Q_ASSERT(mDataModel.count() > 0);
73 // TODO: make min and max members and update them when data changes.
75 // TODO: make min and max members and update them when data changes.
74 // This is slower since they are checked every time, even if data is same since previous call.
76 // This is slower since they are checked every time, even if data is same since previous call.
75 int min = INT_MAX;
77 qreal min = INT_MAX;
76
78
77 for (int i=0; i <mDataModel.count(); i++) {
79 for (int i=0; i <mDataModel.count(); i++) {
78 int itemCount = mDataModel.at(i)->countColumns();
80 int itemCount = mDataModel.at(i)->countColumns();
79 for (int j=0; j<itemCount; j++) {
81 for (int j=0; j<itemCount; j++) {
80 int temp = mDataModel.at(i)->valueAt(j);
82 qreal temp = mDataModel.at(i)->valueAt(j);
81 if (temp < min) {
83 if (temp < min) {
82 min = temp;
84 min = temp;
83 }
85 }
@@ -86,19 +88,19 int BarChartModel::min()
86 return min;
88 return min;
87 }
89 }
88
90
89 int BarChartModel::max()
91 qreal BarChartModel::max()
90 {
92 {
91 // qDebug() << "BarChartModel::max";
93 // qDebug() << "BarChartModel::max";
92 Q_ASSERT(mDataModel.count() > 0);
94 Q_ASSERT(mDataModel.count() > 0);
93
95
94 // TODO: make min and max members and update them when data changes.
96 // TODO: make min and max members and update them when data changes.
95 // This is slower since they are checked every time, even if data is same since previous call.
97 // This is slower since they are checked every time, even if data is same since previous call.
96 int max = INT_MIN;
98 qreal max = INT_MIN;
97
99
98 for (int i=0; i <mDataModel.count(); i++) {
100 for (int i=0; i <mDataModel.count(); i++) {
99 int itemCount = mDataModel.at(i)->countColumns();
101 int itemCount = mDataModel.at(i)->countColumns();
100 for (int j=0; j<itemCount; j++) {
102 for (int j=0; j<itemCount; j++) {
101 int temp = mDataModel.at(i)->valueAt(j);
103 qreal temp = mDataModel.at(i)->valueAt(j);
102 if (temp > max) {
104 if (temp > max) {
103 max = temp;
105 max = temp;
104 }
106 }
@@ -24,9 +24,8 public:
24 int countColumns(); // Maximum number of items in series
24 int countColumns(); // Maximum number of items in series
25 int countTotalItems(); // Total items in all series. Includes empty items.
25 int countTotalItems(); // Total items in all series. Includes empty items.
26
26
27 // TODO: qreal these
27 qreal max(); // Maximum value of all series
28 int max(); // Maximum value of all series
28 qreal min(); // Minimum value of all series
29 int min(); // Minimum value of all series
30 qreal valueAt(int series, int item);
29 qreal valueAt(int series, int item);
31
30
32 qreal columnSum(int column);
31 qreal columnSum(int column);
@@ -8,50 +8,64 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
9 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
10 : QChartSeries(parent)
10 : QChartSeries(parent)
11 ,mModel(*(new BarChartModel(this))) // TODO: is this ok?
11 ,mModel(new BarChartModel(this))
12 {
12 {
13 }
13 }
14
14
15 int BarChartSeriesBase::addData(QList<qreal> data)
15 int BarChartSeriesBase::addData(QList<qreal> data)
16 {
16 {
17 return mModel.addData(data);
17 return mModel->addData(data);
18 }
18 }
19
19
20 void BarChartSeriesBase::removeData(int id)
20 void BarChartSeriesBase::removeData(int id)
21 {
21 {
22 mModel.removeData(id);
22 mModel->removeData(id);
23 }
24
25 void BarChartSeriesBase::setLabels(QList<QString> labels)
26 {
27 mLabels = labels;
23 }
28 }
24
29
25 qreal BarChartSeriesBase::min()
30 qreal BarChartSeriesBase::min()
26 {
31 {
27 return mModel.min();
32 return mModel->min();
28 }
33 }
29
34
30 qreal BarChartSeriesBase::max()
35 qreal BarChartSeriesBase::max()
31 {
36 {
32 return mModel.max();
37 return mModel->max();
33 }
38 }
34
39
35 int BarChartSeriesBase::countColumns()
40 int BarChartSeriesBase::countColumns()
36 {
41 {
37 return mModel.countColumns();
42 return mModel->countColumns();
38 }
43 }
39
44
40 qreal BarChartSeriesBase::valueAt(int series, int item)
45 qreal BarChartSeriesBase::valueAt(int series, int item)
41 {
46 {
42 qDebug() << "BarChartSeriesBase::valueAt" << series << item;
47 // qDebug() << "BarChartSeriesBase::valueAt" << series << item;
43 return mModel.valueAt(series,item);
48 return mModel->valueAt(series,item);
44 }
49 }
45
50
46 qreal BarChartSeriesBase::maxColumnSum()
51 qreal BarChartSeriesBase::maxColumnSum()
47 {
52 {
48 qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum();
53 // qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel->maxColumnSum();
49 return mModel.maxColumnSum();
54 return mModel->maxColumnSum();
50 }
55 }
51
56
52 BarChartModel& BarChartSeriesBase::model()
57 BarChartModel& BarChartSeriesBase::model()
53 {
58 {
54 return mModel;
59 return *mModel;
60 }
61
62 QString BarChartSeriesBase::label(int item)
63 {
64 if ((item>=0) && (item < mLabels.count())) {
65 return mLabels.at(item);
66 }
67
68 return QString("");
55 }
69 }
56
70
57 #include "moc_barchartseriesbase.cpp"
71 #include "moc_barchartseriesbase.cpp"
@@ -26,7 +26,9 public:
26 // Returns id for vector.
26 // Returns id for vector.
27 int addData(QList<qreal> data);
27 int addData(QList<qreal> data);
28 void removeData(int id);
28 void removeData(int id);
29 void setLabels(QList<QString> labels);
29
30
31 // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil.
30 qreal min();
32 qreal min();
31 qreal max();
33 qreal max();
32 int countColumns(); // Count items in one series.
34 int countColumns(); // Count items in one series.
@@ -34,6 +36,7 public:
34 qreal maxColumnSum();
36 qreal maxColumnSum();
35
37
36 BarChartModel& model();
38 BarChartModel& model();
39 QString label(int item);
37
40
38 signals:
41 signals:
39 void changed(int index);
42 void changed(int index);
@@ -42,9 +45,9 public Q_SLOTS:
42
45
43 private:
46 private:
44
47
45 BarChartModel& mModel;
48 BarChartModel* mModel;
46 BarGroupBase* mBarGroup;
47
49
50 QList<QString> mLabels;
48 };
51 };
49
52
50 QTCOMMERCIALCHART_END_NAMESPACE
53 QTCOMMERCIALCHART_END_NAMESPACE
@@ -32,9 +32,8 void BarGroup::layoutChanged()
32
32
33 qreal tW = mWidth;
33 qreal tW = mWidth;
34 qreal tH = mHeight;
34 qreal tH = mHeight;
35 qreal tM = mMax;
35 qreal tM = mModel.max();
36 qreal scale = (tH/tM);
36 qreal scale = (tH/tM);
37
38 qreal tC = itemCount+1;
37 qreal tC = itemCount+1;
39 qreal xStepPerSeries = (tW/tC);
38 qreal xStepPerSeries = (tW/tC);
40
39
@@ -12,10 +12,11 BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent)
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 ,mLayoutSet(false)
13 ,mLayoutSet(false)
14 ,mLayoutDirty(true)
14 ,mLayoutDirty(true)
15 ,mTheme(0)
16 ,mSeparatorsVisible(true)
15 ,mSeparatorsVisible(true)
17 ,mModel(series.model())
16 ,mModel(series.model())
17 ,mSeries(series)
18 {
18 {
19 dataChanged();
19 }
20 }
20
21
21 void BarGroupBase::setSeparatorsVisible(bool visible)
22 void BarGroupBase::setSeparatorsVisible(bool visible)
@@ -82,8 +83,8 void BarGroupBase::dataChanged()
82 int count = mModel.countColumns(); // mSeries.countColumns();
83 int count = mModel.countColumns(); // mSeries.countColumns();
83 for (int i=0; i<count; i++) {
84 for (int i=0; i<count; i++) {
84 BarLabel* label = new BarLabel(this);
85 BarLabel* label = new BarLabel(this);
85 QString text("Label " + QString::number(i));
86 // QString text("Label " + QString::number(i));
86 label->set(text);
87 label->set(mSeries.label(i));
87 childItems().append(label);
88 childItems().append(label);
88 }
89 }
89
90
@@ -102,14 +103,17 void BarGroupBase::dataChanged()
102
103
103 void BarGroupBase::handleModelChanged(int index)
104 void BarGroupBase::handleModelChanged(int index)
104 {
105 {
105 // qDebug() << "BarGroupBase::handleModelChanged" << index;
106 qDebug() << "BarGroupBase::handleModelChanged" << index;
106 dataChanged();
107 dataChanged();
107 }
108 }
108
109
109 void BarGroupBase::handleDomainChanged(const Domain& domain)
110 void BarGroupBase::handleDomainChanged(const Domain& domain)
110 {
111 {
111 // qDebug() << "BarGroupBase::handleDomainChanged";
112 // qDebug() << "BarGroupBase::handleDomainChanged";
112 dataChanged();
113
114 // TODO: Figure out the use case for this.
115 // Affects the size of visible item, so layout is changed.
116 // layoutChanged();
113 }
117 }
114
118
115 void BarGroupBase::handleGeometryChanged(const QRectF& rect)
119 void BarGroupBase::handleGeometryChanged(const QRectF& rect)
@@ -1,10 +1,7
1 #ifndef BARGROUPBASE_H
1 #ifndef BARGROUPBASE_H
2 #define BARGROUPBASE_H
2 #define BARGROUPBASE_H
3
3
4 #include "charttheme_p.h"
5 #include "chartitem_p.h"
4 #include "chartitem_p.h"
6 //#include "barlabel_p.h"
7 //#include "bar_p.h"
8 #include "barchartseriesbase.h"
5 #include "barchartseriesbase.h"
9 #include "barchartmodel_p.h"
6 #include "barchartmodel_p.h"
10 #include <QGraphicsItem>
7 #include <QGraphicsItem>
@@ -20,21 +17,18 public:
20 void setSeparatorsVisible(bool visible = true);
17 void setSeparatorsVisible(bool visible = true);
21
18
22 public: // From ChartItem
19 public: // From ChartItem
23 void setSize(const QSizeF &size){};
20 // void setSize(const QSizeF &size){};
24 void setPlotDomain(const PlotDomain& data){};
25
21
26 // From QGraphicsItem
22 // From QGraphicsItem
27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 QRectF boundingRect() const;
24 QRectF boundingRect() const;
29
25
30 // From ChartThemeObserver
31 // void themeChanged(ChartTheme *theme);
32
33 // TODO: these may change with layout awarness.
26 // TODO: these may change with layout awarness.
34 void setBarWidth( int w );
27 void setBarWidth( int w );
35 int addColor( QColor color );
28 int addColor( QColor color );
36 void resetColors();
29 void resetColors();
37
30
31 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
38 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
32 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
39 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
33 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
40
34
@@ -43,12 +37,11 protected slots:
43 void handleDomainChanged(const Domain& domain);
37 void handleDomainChanged(const Domain& domain);
44 void handleGeometryChanged(const QRectF& size);
38 void handleGeometryChanged(const QRectF& size);
45
39
46
47 protected:
40 protected:
48
41
49 // TODO: consider these.
42 // TODO: consider these.
50 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
43 //int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
51 int mMax;
44 //int mMax;
52
45
53 int mHeight; // Layout spesific
46 int mHeight; // Layout spesific
54 int mWidth;
47 int mWidth;
@@ -58,11 +51,9 protected:
58 bool mLayoutDirty;
51 bool mLayoutDirty;
59
52
60 QList<QColor> mColors; // List of colors for series for now
53 QList<QColor> mColors; // List of colors for series for now
61
62 ChartTheme* mTheme;
63 bool mSeparatorsVisible;
54 bool mSeparatorsVisible;
64
65 BarChartModel& mModel;
55 BarChartModel& mModel;
56 BarChartSeriesBase& mSeries;
66
57
67 };
58 };
68
59
@@ -51,9 +51,6 void StackedBarGroup::layoutChanged()
51 qreal barHeight = mModel.valueAt(row, column) * scale;
51 qreal barHeight = mModel.valueAt(row, column) * scale;
52 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
52 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
53
53
54 // TODO: width settable per bar?
55 // TODO: theme stuff
56 // mTheme->themeForSeries();
57 bar->resize(mBarDefaultWidth, barHeight);
54 bar->resize(mBarDefaultWidth, barHeight);
58 bar->setColor(mColors.at(row));
55 bar->setColor(mColors.at(row));
59 bar->setPos(xPos, yPos-barHeight);
56 bar->setPos(xPos, yPos-barHeight);
@@ -60,8 +60,8 void ChartDataSet::addSeries(QChartSeries* series)
60 domain.m_minY = qMin(domain.m_minY,y);
60 domain.m_minY = qMin(domain.m_minY,y);
61 domain.m_maxX = qMax(domain.m_maxX,x);
61 domain.m_maxX = qMax(domain.m_maxX,x);
62 domain.m_maxY = qMax(domain.m_maxY,y);
62 domain.m_maxY = qMax(domain.m_maxY,y);
63 }
64 break;
63 break;
64 }
65 case QChartSeries::SeriesTypeStackedBar: {
65 case QChartSeries::SeriesTypeStackedBar: {
66
66
67 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
67 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
@@ -71,8 +71,8 void ChartDataSet::addSeries(QChartSeries* series)
71 domain.m_minY = qMin(domain.m_minY,y);
71 domain.m_minY = qMin(domain.m_minY,y);
72 domain.m_maxX = qMax(domain.m_maxX,x);
72 domain.m_maxX = qMax(domain.m_maxX,x);
73 domain.m_maxY = qMax(domain.m_maxY,y);
73 domain.m_maxY = qMax(domain.m_maxY,y);
74 }
75 break;
74 break;
75 }
76 case QChartSeries::SeriesTypePercentBar: {
76 case QChartSeries::SeriesTypePercentBar: {
77
77
78 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
78 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
@@ -81,8 +81,8 void ChartDataSet::addSeries(QChartSeries* series)
81 domain.m_minY = 0;
81 domain.m_minY = 0;
82 domain.m_maxX = qMax(domain.m_maxX,x);
82 domain.m_maxX = qMax(domain.m_maxX,x);
83 domain.m_maxY = 100;
83 domain.m_maxY = 100;
84 }
85 break;
84 break;
85 }
86
86
87 case QChartSeries::SeriesTypePie: {
87 case QChartSeries::SeriesTypePie: {
88 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
88 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
@@ -128,6 +128,7 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int coun
128
128
129 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
129 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
130 {
130 {
131 item->resetColors();
131 for (int i=0; i< m_seriesColor.count(); i++) {
132 for (int i=0; i<m_seriesColor.count(); i++) {
132 item->addColor(m_seriesColor.at(i));
133 item->addColor(m_seriesColor.at(i));
133 }
134 }
@@ -140,6 +141,7 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
140
141
141 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
142 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
142 {
143 {
144 item->resetColors();
143 for (int i=0; i< m_seriesColor.count(); i++) {
145 for (int i=0; i< m_seriesColor.count(); i++) {
144 item->addColor(m_seriesColor.at(i));
146 item->addColor(m_seriesColor.at(i));
145 }
147 }
@@ -152,6 +154,7 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,i
152
154
153 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
155 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
154 {
156 {
157 item->resetColors();
155 for (int i=0; i< m_seriesColor.count(); i++) {
158 for (int i=0; i< m_seriesColor.count(); i++) {
156 item->addColor(m_seriesColor.at(i));
159 item->addColor(m_seriesColor.at(i));
157 }
160 }
@@ -251,6 +251,9 void MainWidget::addSeries(QString series, QString data)
251 series0->addData(data2);
251 series0->addData(data2);
252 series0->addData(data3);
252 series0->addData(data3);
253 series0->addData(data4);
253 series0->addData(data4);
254 QList<QString> labels;
255 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
256 series0->setLabels(labels);
254 m_chartWidget->addSeries(series0);
257 m_chartWidget->addSeries(series0);
255 newSeries = series0;
258 newSeries = series0;
256 } else if (series == "StackedBar") {
259 } else if (series == "StackedBar") {
@@ -261,6 +264,9 void MainWidget::addSeries(QString series, QString data)
261 series0->addData(data2);
264 series0->addData(data2);
262 series0->addData(data3);
265 series0->addData(data3);
263 series0->addData(data4);
266 series0->addData(data4);
267 QList<QString> labels;
268 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
269 series0->setLabels(labels);
264 m_chartWidget->addSeries(series0);
270 m_chartWidget->addSeries(series0);
265 newSeries = series0;
271 newSeries = series0;
266 } else if (series == "PercentBar") {
272 } else if (series == "PercentBar") {
@@ -271,6 +277,9 void MainWidget::addSeries(QString series, QString data)
271 series0->addData(data2);
277 series0->addData(data2);
272 series0->addData(data3);
278 series0->addData(data3);
273 series0->addData(data4);
279 series0->addData(data4);
280 QList<QString> labels;
281 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
282 series0->setLabels(labels);
274 m_chartWidget->addSeries(series0);
283 m_chartWidget->addSeries(series0);
275 newSeries = series0;
284 newSeries = series0;
276 } else {
285 } else {
@@ -384,6 +393,10 void MainWidget::changeChartTheme(int themeIndex)
384 {
393 {
385 qDebug() << "changeChartTheme: " << themeIndex;
394 qDebug() << "changeChartTheme: " << themeIndex;
386 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
395 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
396 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
397 QSize s = size();
398 s.setWidth(s.width()+1);
399 resize(s);
387 }
400 }
388
401
389 void MainWidget::setPieSizeFactor(double size)
402 void MainWidget::setPieSizeFactor(double size)
General Comments 0
You need to be logged in to leave comments. Login now