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