##// END OF EJS Templates
prototyping separators with stacked bars
sauimone -
r119:9025093c4c9d
parent child
Show More
@@ -1,163 +1,192
1 #include "stackedbargroup.h"
1 #include "stackedbargroup.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include <QDebug>
4 #include <QDebug>
5 #include <QPainter>
5
6
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8
8 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
9 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
9 ChartItem(parent)
10 ChartItem(parent)
10 ,mSeries(series)
11 ,mSeries(series)
11 ,mLayoutSet(false)
12 ,mLayoutSet(false)
12 ,mLayoutDirty(true)
13 ,mLayoutDirty(true)
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mTheme(0)
15 ,mTheme(0)
16 ,mSeparatorsVisible(true)
15 {
17 {
16 dataChanged();
18 dataChanged();
17 }
19 }
18
20
21 void StackedBarGroup::setSeparatorsVisible(bool visible)
22 {
23 mSeparatorsVisible = visible;
24 }
19
25
20 void StackedBarGroup::setSize(const QSizeF& size)
26 void StackedBarGroup::setSize(const QSizeF& size)
21 {
27 {
22 // qDebug() << "StackedBarGroup::setSize";
23 mWidth = size.width();
28 mWidth = size.width();
24 mHeight = size.height();
29 mHeight = size.height();
25 layoutChanged();
30 layoutChanged();
26 mLayoutSet = true;
31 mLayoutSet = true;
27 }
32 }
28
33
29 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
34 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
30 {
35 {
31 qDebug() << "StackedBarGroup::setPlotDomain";
36 qDebug() << "StackedBarGroup::setPlotDomain";
32 // TODO:
37 // TODO:
33 }
38 }
34
39
35 void StackedBarGroup::themeChanged(ChartTheme *theme)
40 void StackedBarGroup::themeChanged(ChartTheme *theme)
36 {
41 {
37 mTheme = theme;
42 mTheme = theme;
38 }
43 }
39
44
40 void StackedBarGroup::setBarWidth( int w )
45 void StackedBarGroup::setBarWidth( int w )
41 {
46 {
42 mBarDefaultWidth = w;
47 mBarDefaultWidth = w;
43 }
48 }
44
49
45 int StackedBarGroup::addColor( QColor color )
50 int StackedBarGroup::addColor( QColor color )
46 {
51 {
47 int colorIndex = mColors.count();
52 int colorIndex = mColors.count();
48 mColors.append(color);
53 mColors.append(color);
49 return colorIndex;
54 return colorIndex;
50 }
55 }
51
56
52 void StackedBarGroup::resetColors()
57 void StackedBarGroup::resetColors()
53 {
58 {
54 mColors.clear();
59 mColors.clear();
55 }
60 }
56
61
57 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
62 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
58 {
63 {
59 if (!mLayoutSet) {
64 if (!mLayoutSet) {
60 qDebug() << "QBarChart::paint called without layout set. Aborting.";
65 qDebug() << "QBarChart::paint called without layout set. Aborting.";
61 return;
66 return;
62 }
67 }
63 if (mLayoutDirty) {
68 if (mLayoutDirty) {
64 // Layout or data has changed. Need to redraw.
69 // Layout or data has changed. Need to redraw.
65 foreach(QGraphicsItem* i, childItems()) {
70 foreach(QGraphicsItem* i, childItems()) {
66 i->paint(painter,option,widget);
71 i->paint(painter,option,widget);
67 }
72 }
68 mLayoutDirty = false;
73 if (mSeparatorsVisible) {
69 //TODO: draw labels.
74 //TODO: own class for separators (graphicsitem), because they may have style etc later.
75 // this is just to see that the positions are calculated correctly.
76 QPen pen(QColor(0,0,255,255));
77 painter->setPen(pen);
78 for (int i=0; i<mSeparatorPositions.count(); i++ ) {
79 qreal xp = mSeparatorPositions.at(i);
80 painter->drawLine(xp,0,xp,mHeight);
81 }
82 }
83 // mLayoutDirty = false;
70 }
84 }
71
85
72 }
86 }
73
87
74 QRectF StackedBarGroup::boundingRect() const
88 QRectF StackedBarGroup::boundingRect() const
75 {
89 {
76 return QRectF(0,0,mWidth,mHeight);
90 return QRectF(0,0,mWidth,mHeight);
77 }
91 }
78
92
79
93
80 void StackedBarGroup::dataChanged()
94 void StackedBarGroup::dataChanged()
81 {
95 {
82 qDebug() << "QBarChart::dataChanged mSeries";
96 qDebug() << "QBarChart::dataChanged";
83
97
84 // Find out maximum and minimum of all series
98 // Find out maximum and minimum of all series
85 mMax = mSeries.max();
99 mMax = mSeries.max();
86 mMin = mSeries.min();
100 mMin = mSeries.min();
87
101
88 // Delete old bars
102 // Delete old bars
89 // Is this correct way to delete childItems?
103 // Is this correct way to delete childItems?
90 foreach (QGraphicsItem* item, childItems()) {
104 foreach (QGraphicsItem* item, childItems()) {
91 delete item;
105 delete item;
92 }
106 }
93
107
94 // Create new graphic items for bars
108 // Create new graphic items for bars
95 int totalItems = mSeries.countTotalItems();
109 int totalItems = mSeries.countTotalItems();
96 for (int i=0; i<totalItems; i++) {
110 for (int i=0; i<totalItems; i++) {
97 Bar *bar = new Bar(this);
111 Bar *bar = new Bar(this);
98 childItems().append(bar);
112 childItems().append(bar);
99 }
113 }
100
114
101 // TODO: labels from series. This creates just some example labels
115 // TODO: labels from series. This creates just some example labels
102 int count = mSeries.countColumns();
116 int count = mSeries.countColumns();
103 for (int i=0; i<count; i++) {
117 for (int i=0; i<count; i++) {
104 BarLabel* label = new BarLabel(this);
118 BarLabel* label = new BarLabel(this);
105 QString text("Label " + QString::number(i));
119 QString text("Label " + QString::number(i));
106 label->set(text);
120 label->set(text);
107 childItems().append(label);
121 childItems().append(label);
108 }
122 }
109
123
124 mSeparatorPositions.clear();
125
110 // TODO: if (autolayout) { layoutChanged() } or something
126 // TODO: if (autolayout) { layoutChanged() } or something
111 mLayoutDirty = true;
127 mLayoutDirty = true;
112 }
128 }
113
129
114 void StackedBarGroup::layoutChanged()
130 void StackedBarGroup::layoutChanged()
115 {
131 {
116 // Scale bars to new layout
132 // Scale bars to new layout
117 // Layout for bars:
133 // Layout for bars:
118 if (mSeries.countRows() <= 0) {
134 if (mSeries.countRows() <= 0) {
119 // Nothing to do.
135 // Nothing to do.
120 return;
136 return;
121 }
137 }
122
138
139 if (mSeries.countColumns() == 0) {
140 // Nothing to do
141 return;
142 }
143
123 // TODO: better way to auto-layout
144 // TODO: better way to auto-layout
124 // Use reals for accurancy (we might get some compiler warnings... :)
145 // Use reals for accurancy (we might get some compiler warnings... :)
146 // TODO: use temp variable for column count...
125 qreal maxSum = mSeries.maxColumnSum();
147 qreal maxSum = mSeries.maxColumnSum();
126 qreal h = mHeight;
148 qreal h = mHeight;
127 qreal scale = (h / maxSum);
149 qreal scale = (h / maxSum);
128
150
129 int count = mSeries.countColumns();
130 int itemIndex(0);
151 int itemIndex(0);
131 qreal tW = mWidth;
152 qreal tW = mWidth;
132 qreal tC = count+1;
153 qreal tC = mSeries.countColumns() + 1;
133 qreal xStep = (tW/tC);
154 qreal xStep = (tW/tC);
134 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
155 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
135 int labelIndex = mSeries.countColumns() * mSeries.countRows();
156 int labelIndex = mSeries.countColumns() * mSeries.countRows();
136
157
137 for (int column = 0; column < mSeries.countColumns(); column++) {
158 for (int column = 0; column < mSeries.countColumns(); column++) {
138 qreal yPos = h;
159 qreal yPos = h;
139 for (int row=0; row < mSeries.countRows(); row++) {
160 for (int row=0; row < mSeries.countRows(); row++) {
140 qreal barHeight = mSeries.valueAt(row, column) * scale;
161 qreal barHeight = mSeries.valueAt(row, column) * scale;
141 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
162 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
142
163
143 // TODO: width settable per bar?
164 // TODO: width settable per bar?
144 // TODO: how to get color for series(x) from theme?
165 // TODO: how to get color for series(x) from theme?
145 // mTheme->themeForSeries();
166 // mTheme->themeForSeries();
146 bar->resize(mBarDefaultWidth, barHeight);
167 bar->resize(mBarDefaultWidth, barHeight);
147 bar->setColor(mColors.at(row));
168 bar->setColor(mColors.at(row));
148 bar->setPos(xPos, yPos-barHeight);
169 bar->setPos(xPos, yPos-barHeight);
149 itemIndex++;
170 itemIndex++;
150 yPos -= barHeight;
171 yPos -= barHeight;
151 }
172 }
152
173
153 // TODO: Layout for labels, remove magic number
174 // TODO: Layout for labels, remove magic number
154 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
175 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
155 label->setPos(xPos, mHeight + 20);
176 label->setPos(xPos, mHeight + 20);
156 labelIndex++;
177 labelIndex++;
157 xPos += xStep;
178 xPos += xStep;
158 }
179 }
159
180
181 // Position separators
182 mSeparatorPositions.clear();
183 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
184 for (int s=0; s < mSeries.countColumns() - 1; s++) {
185 mSeparatorPositions.append(xPos);
186 xPos += xStep;
187 }
188
160 mLayoutDirty = true;
189 mLayoutDirty = true;
161 }
190 }
162
191
163 QTCOMMERCIALCHART_END_NAMESPACE
192 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,65 +1,69
1 #ifndef STACKEDBARGROUP_H
1 #ifndef STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
3
3
4 #include "charttheme_p.h"
4 #include "charttheme_p.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include "barlabel_p.h"
6 #include "barlabel_p.h"
7 #include "bar_p.h"
7 #include "bar_p.h"
8 #include "stackedbarchartseries.h"
8 #include "stackedbarchartseries.h"
9 #include <QGraphicsItem>
9 #include <QGraphicsItem>
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem
13 // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem
14 class StackedBarGroup : public ChartItem, public ChartThemeObserver
14 class StackedBarGroup : public ChartItem, public ChartThemeObserver
15 {
15 {
16 public:
16 public:
17 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
17 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
18
18
19 // Bar chart spesific
20 void setSeparatorsVisible(bool visible = true);
21
19 public: // From ChartItem
22 public: // From ChartItem
20 void setSize(const QSizeF &size);
23 void setSize(const QSizeF &size);
21 void setPlotDomain(const PlotDomain& data);
24 void setPlotDomain(const PlotDomain& data);
22
25
23 // From ChartThemeObserver
26 // From ChartThemeObserver
24 void themeChanged(ChartTheme *theme);
27 void themeChanged(ChartTheme *theme);
25
28
26 public: // Layout "api"
29 public: // Layout "api"
27 void setPos(qreal x, qreal y);
30 void setPos(qreal x, qreal y);
28 void setBarWidth( int w ); // Default width for each bar
31 void setBarWidth( int w ); // Default width for each bar
29
32
30 int addColor( QColor color );
33 int addColor( QColor color );
31 void resetColors();
34 void resetColors();
32
35
33 // From QGraphicsItem
36 // From QGraphicsItem
34 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
37 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
35 QRectF boundingRect() const;
38 QRectF boundingRect() const;
36
39
37 private:
40 private:
38
41
39 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
42 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
40 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
43 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
41
44
42 private:
45 private:
43
46
44 // Data
47 // Data
45 StackedBarChartSeries& mSeries;
48 StackedBarChartSeries& mSeries;
46 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
49 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
47 int mMax;
50 int mMax;
48
51
49 int mHeight; // Layout spesific
52 int mHeight; // Layout spesific
50 int mWidth;
53 int mWidth;
51 int mBarDefaultWidth;
54 int mBarDefaultWidth;
52
55
53 bool mLayoutSet; // True, if component has been laid out.
56 bool mLayoutSet; // True, if component has been laid out.
54 bool mLayoutDirty;
57 bool mLayoutDirty;
55
58
56 QList<QColor> mColors; // List of colors for series for now
59 QList<QColor> mColors; // List of colors for series for now
57
60
58 ChartTheme* mTheme;
61 ChartTheme* mTheme;
59 // QList<BarLabel*> mLabels;
62 bool mSeparatorsVisible;
63 QList<qreal> mSeparatorPositions;
60
64
61 };
65 };
62
66
63 QTCOMMERCIALCHART_END_NAMESPACE
67 QTCOMMERCIALCHART_END_NAMESPACE
64
68
65 #endif // STACKEDBARGROUP_H
69 #endif // STACKEDBARGROUP_H
@@ -1,402 +1,405
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qpieseries.h"
5 #include "qpieseries.h"
6 #include "qpieseries_p.h"
6 #include "qpieseries_p.h"
7 #include "qxychartseries.h"
7 #include "qxychartseries.h"
8 #include "qchartaxis.h"
8 #include "qchartaxis.h"
9 #include "barchartseries.h"
9 #include "barchartseries.h"
10 #include "bargroup.h"
10 #include "bargroup.h"
11 #include "stackedbarchartseries.h"
11 #include "stackedbarchartseries.h"
12 #include "stackedbargroup.h"
12 #include "stackedbargroup.h"
13 #include "percentbarchartseries.h"
13 #include "percentbarchartseries.h"
14 #include "percentbargroup.h"
14 #include "percentbargroup.h"
15 #include "charttheme_p.h"
15 #include "charttheme_p.h"
16 #include "chartitem_p.h"
16 #include "chartitem_p.h"
17
17
18 #include "xylinechartitem_p.h"
18 #include "xylinechartitem_p.h"
19 #include "plotdomain_p.h"
19 #include "plotdomain_p.h"
20 #include "axisitem_p.h"
20 #include "axisitem_p.h"
21 #include <QGraphicsScene>
21 #include <QGraphicsScene>
22 #include <QGraphicsSceneResizeEvent>
22 #include <QGraphicsSceneResizeEvent>
23 #include <QDebug>
23 #include <QDebug>
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
27 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
28 m_backgroundItem(0),
28 m_backgroundItem(0),
29 m_titleItem(0),
29 m_titleItem(0),
30 m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)),
30 m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)),
31 m_plotDataIndex(0),
31 m_plotDataIndex(0),
32 m_marginSize(0),
32 m_marginSize(0),
33 m_chartTheme(new ChartTheme(this))
33 m_chartTheme(new ChartTheme(this))
34 {
34 {
35 // TODO: the default theme?
35 // TODO: the default theme?
36 setTheme(QChart::ChartThemeDefault);
36 setTheme(QChart::ChartThemeDefault);
37
37
38 PlotDomain domain;
38 PlotDomain domain;
39 m_plotDomainList << domain;
39 m_plotDomainList << domain;
40 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
40 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
41 m_chartItems << m_axisXItem;
41 m_chartItems << m_axisXItem;
42 m_chartItems << m_axisYItem.at(0);
42 m_chartItems << m_axisYItem.at(0);
43 }
43 }
44
44
45 QChart::~QChart(){}
45 QChart::~QChart(){}
46
46
47 void QChart::addSeries(QChartSeries* series)
47 void QChart::addSeries(QChartSeries* series)
48 {
48 {
49 // TODO: we should check the series not already added
49 // TODO: we should check the series not already added
50
50
51 m_chartSeries << series;
51 m_chartSeries << series;
52
52
53 m_plotDataIndex = 0 ;
53 m_plotDataIndex = 0 ;
54 m_plotDomainList.resize(1);
54 m_plotDomainList.resize(1);
55 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
55 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
56
56
57 switch(series->type())
57 switch(series->type())
58 {
58 {
59 case QChartSeries::SeriesTypeLine: {
59 case QChartSeries::SeriesTypeLine: {
60
60
61 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
61 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
62
62
63 for (int i = 0 ; i < xyseries->count() ; i++) {
63 for (int i = 0 ; i < xyseries->count() ; i++) {
64 qreal x = xyseries->x(i);
64 qreal x = xyseries->x(i);
65 qreal y = xyseries->y(i);
65 qreal y = xyseries->y(i);
66 domain.m_minX = qMin(domain.m_minX,x);
66 domain.m_minX = qMin(domain.m_minX,x);
67 domain.m_minY = qMin(domain.m_minY,y);
67 domain.m_minY = qMin(domain.m_minY,y);
68 domain.m_maxX = qMax(domain.m_maxX,x);
68 domain.m_maxX = qMax(domain.m_maxX,x);
69 domain.m_maxY = qMax(domain.m_maxY,y);
69 domain.m_maxY = qMax(domain.m_maxY,y);
70 }
70 }
71
71
72 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
72 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
73
73
74 m_chartItems << item;
74 m_chartItems << item;
75 // TODO:
75 // TODO:
76 //m_chartTheme->addObserver(xyseries);
76 //m_chartTheme->addObserver(xyseries);
77
77
78 break;
78 break;
79 }
79 }
80 case QChartSeries::SeriesTypeBar: {
80 case QChartSeries::SeriesTypeBar: {
81
81
82 qDebug() << "barSeries added";
82 qDebug() << "barSeries added";
83 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
83 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
84 BarGroup* barGroup = new BarGroup(*barSeries,this);
84 BarGroup* barGroup = new BarGroup(*barSeries,this);
85
85
86 // Add some fugly colors for 5 fist series...
86 // Add some fugly colors for 5 fist series...
87 barGroup->addColor(QColor(255,0,0,128));
87 barGroup->addColor(QColor(255,0,0,128));
88 barGroup->addColor(QColor(255,255,0,128));
88 barGroup->addColor(QColor(255,255,0,128));
89 barGroup->addColor(QColor(0,255,0,128));
89 barGroup->addColor(QColor(0,255,0,128));
90 barGroup->addColor(QColor(0,0,255,128));
90 barGroup->addColor(QColor(0,0,255,128));
91 barGroup->addColor(QColor(255,128,0,128));
91 barGroup->addColor(QColor(255,128,0,128));
92
92
93 m_chartItems << barGroup;
93 m_chartItems << barGroup;
94 childItems().append(barGroup);
94 childItems().append(barGroup);
95
95
96 qreal x = barSeries->countColumns();
96 qreal x = barSeries->countColumns();
97 qreal y = barSeries->max();
97 qreal y = barSeries->max();
98 domain.m_minX = qMin(domain.m_minX,x);
98 domain.m_minX = qMin(domain.m_minX,x);
99 domain.m_minY = qMin(domain.m_minY,y);
99 domain.m_minY = qMin(domain.m_minY,y);
100 domain.m_maxX = qMax(domain.m_maxX,x);
100 domain.m_maxX = qMax(domain.m_maxX,x);
101 domain.m_maxY = qMax(domain.m_maxY,y);
101 domain.m_maxY = qMax(domain.m_maxY,y);
102 m_axisXItem->setVisible(false);
102 break;
103 break;
103 }
104 }
104 case QChartSeries::SeriesTypeStackedBar: {
105 case QChartSeries::SeriesTypeStackedBar: {
105
106
106 qDebug() << "barSeries added";
107 qDebug() << "barSeries added";
107 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
108 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
108 StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this);
109 StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this);
109
110
110 // Add some fugly colors for 5 fist series...
111 // Add some fugly colors for 5 fist series...
111 stackedBarGroup->addColor(QColor(255,0,0,128));
112 stackedBarGroup->addColor(QColor(255,0,0,128));
112 stackedBarGroup->addColor(QColor(255,255,0,128));
113 stackedBarGroup->addColor(QColor(255,255,0,128));
113 stackedBarGroup->addColor(QColor(0,255,0,128));
114 stackedBarGroup->addColor(QColor(0,255,0,128));
114 stackedBarGroup->addColor(QColor(0,0,255,128));
115 stackedBarGroup->addColor(QColor(0,0,255,128));
115 stackedBarGroup->addColor(QColor(255,128,0,128));
116 stackedBarGroup->addColor(QColor(255,128,0,128));
116
117
117 m_chartItems << stackedBarGroup;
118 m_chartItems << stackedBarGroup;
118 childItems().append(stackedBarGroup);
119 childItems().append(stackedBarGroup);
119
120
120 qreal x = stackedBarSeries->countColumns();
121 qreal x = stackedBarSeries->countColumns();
121 qreal y = stackedBarSeries->maxColumnSum();
122 qreal y = stackedBarSeries->maxColumnSum();
122 domain.m_minX = qMin(domain.m_minX,x);
123 domain.m_minX = qMin(domain.m_minX,x);
123 domain.m_minY = qMin(domain.m_minY,y);
124 domain.m_minY = qMin(domain.m_minY,y);
124 domain.m_maxX = qMax(domain.m_maxX,x);
125 domain.m_maxX = qMax(domain.m_maxX,x);
125 domain.m_maxY = qMax(domain.m_maxY,y);
126 domain.m_maxY = qMax(domain.m_maxY,y);
127 m_axisXItem->setVisible(false);
126 break;
128 break;
127 }
129 }
128 case QChartSeries::SeriesTypePercentBar: {
130 case QChartSeries::SeriesTypePercentBar: {
129
131
130 qDebug() << "barSeries added";
132 qDebug() << "barSeries added";
131 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
133 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
132 PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this);
134 PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this);
133
135
134 // Add some fugly colors for 5 fist series...
136 // Add some fugly colors for 5 fist series...
135 percentBarGroup->addColor(QColor(255,0,0,128));
137 percentBarGroup->addColor(QColor(255,0,0,128));
136 percentBarGroup->addColor(QColor(255,255,0,128));
138 percentBarGroup->addColor(QColor(255,255,0,128));
137 percentBarGroup->addColor(QColor(0,255,0,128));
139 percentBarGroup->addColor(QColor(0,255,0,128));
138 percentBarGroup->addColor(QColor(0,0,255,128));
140 percentBarGroup->addColor(QColor(0,0,255,128));
139 percentBarGroup->addColor(QColor(255,128,0,128));
141 percentBarGroup->addColor(QColor(255,128,0,128));
140
142
141 m_chartItems << percentBarGroup;
143 m_chartItems << percentBarGroup;
142 childItems().append(percentBarGroup);
144 childItems().append(percentBarGroup);
143
145
144 qreal x = percentBarSeries->countColumns();
146 qreal x = percentBarSeries->countColumns();
145 domain.m_minX = qMin(domain.m_minX,x);
147 domain.m_minX = qMin(domain.m_minX,x);
146 domain.m_minY = 0;
148 domain.m_minY = 0;
147 domain.m_maxX = qMax(domain.m_maxX,x);
149 domain.m_maxX = qMax(domain.m_maxX,x);
148 domain.m_maxY = 100;
150 domain.m_maxY = 100;
151 m_axisXItem->setVisible(false);
149 break;
152 break;
150 }
153 }
151 case QChartSeries::SeriesTypeScatter: {
154 case QChartSeries::SeriesTypeScatter: {
152 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
155 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
153 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
156 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
154 scatterSeries->d->setParentItem(this);
157 scatterSeries->d->setParentItem(this);
155 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
158 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
156 m_chartItems << scatterSeries->d;
159 m_chartItems << scatterSeries->d;
157 m_chartTheme->addObserver(scatterSeries->d);
160 m_chartTheme->addObserver(scatterSeries->d);
158
161
159 foreach (qreal x, scatterSeries->d->m_x) {
162 foreach (qreal x, scatterSeries->d->m_x) {
160 domain.m_minX = qMin(domain.m_minX, x);
163 domain.m_minX = qMin(domain.m_minX, x);
161 domain.m_maxX = qMax(domain.m_maxX, x);
164 domain.m_maxX = qMax(domain.m_maxX, x);
162 }
165 }
163 foreach (qreal y, scatterSeries->d->m_y) {
166 foreach (qreal y, scatterSeries->d->m_y) {
164 domain.m_minY = qMin(domain.m_minY, y);
167 domain.m_minY = qMin(domain.m_minY, y);
165 domain.m_maxY = qMax(domain.m_maxY, y);
168 domain.m_maxY = qMax(domain.m_maxY, y);
166 }
169 }
167
170
168 break;
171 break;
169 }
172 }
170 case QChartSeries::SeriesTypePie: {
173 case QChartSeries::SeriesTypePie: {
171 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
174 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
172 pieSeries->d->setParentItem(this);
175 pieSeries->d->setParentItem(this);
173 m_chartItems << pieSeries->d;
176 m_chartItems << pieSeries->d;
174 pieSeries->d->m_chartTheme = m_chartTheme;
177 pieSeries->d->m_chartTheme = m_chartTheme;
175 m_chartTheme->addObserver(pieSeries->d);
178 m_chartTheme->addObserver(pieSeries->d);
176 break;
179 break;
177 }
180 }
178 }
181 }
179
182
180 // Update all the items to match the new visible area of the chart
183 // Update all the items to match the new visible area of the chart
181 foreach(ChartItem* i, m_chartItems)
184 foreach(ChartItem* i, m_chartItems)
182 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
185 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
183 }
186 }
184
187
185 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
188 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
186 {
189 {
187 // TODO: support also other types; not only scatter and pie
190 // TODO: support also other types; not only scatter and pie
188
191
189 QChartSeries *series(0);
192 QChartSeries *series(0);
190
193
191 switch (type) {
194 switch (type) {
192 case QChartSeries::SeriesTypeLine: {
195 case QChartSeries::SeriesTypeLine: {
193 series = QXYChartSeries::create();
196 series = QXYChartSeries::create();
194 break;
197 break;
195 }
198 }
196 case QChartSeries::SeriesTypeBar: {
199 case QChartSeries::SeriesTypeBar: {
197 series = new BarChartSeries(this);
200 series = new BarChartSeries(this);
198 break;
201 break;
199 }
202 }
200 case QChartSeries::SeriesTypeStackedBar: {
203 case QChartSeries::SeriesTypeStackedBar: {
201 series = new StackedBarChartSeries(this);
204 series = new StackedBarChartSeries(this);
202 break;
205 break;
203 }
206 }
204 case QChartSeries::SeriesTypePercentBar: {
207 case QChartSeries::SeriesTypePercentBar: {
205 series = new PercentBarChartSeries(this);
208 series = new PercentBarChartSeries(this);
206 break;
209 break;
207 }
210 }
208 case QChartSeries::SeriesTypeScatter: {
211 case QChartSeries::SeriesTypeScatter: {
209 series = new QScatterSeries(this);
212 series = new QScatterSeries(this);
210 break;
213 break;
211 }
214 }
212 case QChartSeries::SeriesTypePie: {
215 case QChartSeries::SeriesTypePie: {
213 series = new QPieSeries(this);
216 series = new QPieSeries(this);
214 break;
217 break;
215 }
218 }
216 default:
219 default:
217 Q_ASSERT(false);
220 Q_ASSERT(false);
218 break;
221 break;
219 }
222 }
220
223
221 addSeries(series);
224 addSeries(series);
222 return series;
225 return series;
223 }
226 }
224
227
225 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
228 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
226 {
229 {
227
230
228 if(!m_backgroundItem){
231 if(!m_backgroundItem){
229 m_backgroundItem = new QGraphicsRectItem(this);
232 m_backgroundItem = new QGraphicsRectItem(this);
230 m_backgroundItem->setZValue(-1);
233 m_backgroundItem->setZValue(-1);
231 }
234 }
232
235
233 m_bacgroundOrinetation = orientation;
236 m_bacgroundOrinetation = orientation;
234 m_backgroundGradient.setColorAt(0.0, startColor);
237 m_backgroundGradient.setColorAt(0.0, startColor);
235 m_backgroundGradient.setColorAt(1.0, endColor);
238 m_backgroundGradient.setColorAt(1.0, endColor);
236 m_backgroundGradient.setStart(0,0);
239 m_backgroundGradient.setStart(0,0);
237
240
238 if(orientation == VerticalGradientOrientation){
241 if(orientation == VerticalGradientOrientation){
239 m_backgroundGradient.setFinalStop(0,m_rect.height());
242 m_backgroundGradient.setFinalStop(0,m_rect.height());
240 }else{
243 }else{
241 m_backgroundGradient.setFinalStop(m_rect.width(),0);
244 m_backgroundGradient.setFinalStop(m_rect.width(),0);
242 }
245 }
243
246
244 m_backgroundItem->setBrush(m_backgroundGradient);
247 m_backgroundItem->setBrush(m_backgroundGradient);
245 m_backgroundItem->setPen(Qt::NoPen);
248 m_backgroundItem->setPen(Qt::NoPen);
246 m_backgroundItem->update();
249 m_backgroundItem->update();
247 }
250 }
248
251
249 void QChart::setTitle(const QString& title,const QFont& font)
252 void QChart::setTitle(const QString& title,const QFont& font)
250 {
253 {
251 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
254 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
252 m_titleItem->setPlainText(title);
255 m_titleItem->setPlainText(title);
253 m_titleItem->setFont(font);
256 m_titleItem->setFont(font);
254 }
257 }
255
258
256 int QChart::margin() const
259 int QChart::margin() const
257 {
260 {
258 return m_marginSize;
261 return m_marginSize;
259 }
262 }
260
263
261 void QChart::setMargin(int margin)
264 void QChart::setMargin(int margin)
262 {
265 {
263 m_marginSize = margin;
266 m_marginSize = margin;
264 }
267 }
265
268
266 void QChart::setTheme(QChart::ChartThemeId theme)
269 void QChart::setTheme(QChart::ChartThemeId theme)
267 {
270 {
268 m_chartTheme->setTheme(theme);
271 m_chartTheme->setTheme(theme);
269 setBackground(m_chartTheme->d->m_gradientStartColor,
272 setBackground(m_chartTheme->d->m_gradientStartColor,
270 m_chartTheme->d->m_gradientEndColor,
273 m_chartTheme->d->m_gradientEndColor,
271 m_bacgroundOrinetation);
274 m_bacgroundOrinetation);
272
275
273 // TODO: Move the controlling of the series presentations into private implementation of the
276 // TODO: Move the controlling of the series presentations into private implementation of the
274 // series instead of QChart controlling themes for each
277 // series instead of QChart controlling themes for each
275 // In other words, the following should be used when creating xy series:
278 // In other words, the following should be used when creating xy series:
276 // m_chartTheme->addObserver(xyseries)
279 // m_chartTheme->addObserver(xyseries)
277 foreach (QChartSeries *series, m_chartSeries) {
280 foreach (QChartSeries *series, m_chartSeries) {
278 if (series->type() == QChartSeries::SeriesTypeLine) {
281 if (series->type() == QChartSeries::SeriesTypeLine) {
279 QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series);
282 QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series);
280 SeriesTheme seriesTheme = m_chartTheme->themeForSeries();
283 SeriesTheme seriesTheme = m_chartTheme->themeForSeries();
281 xyseries->setPen(seriesTheme.linePen);
284 xyseries->setPen(seriesTheme.linePen);
282 }
285 }
283 }
286 }
284
287
285 update();
288 update();
286 }
289 }
287
290
288 void QChart::zoomInToRect(const QRectF& rectangle)
291 void QChart::zoomInToRect(const QRectF& rectangle)
289 {
292 {
290
293
291 if(!rectangle.isValid()) return;
294 if(!rectangle.isValid()) return;
292
295
293 qreal margin = this->margin();
296 qreal margin = this->margin();
294
297
295 QRectF rect = rectangle.normalized();
298 QRectF rect = rectangle.normalized();
296 rect.translate(-margin, -margin);
299 rect.translate(-margin, -margin);
297
300
298 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
301 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
299
302
300 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
303 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
301
304
302 m_plotDomainList.resize(m_plotDataIndex + 1);
305 m_plotDomainList.resize(m_plotDataIndex + 1);
303 m_plotDomainList<<domain;
306 m_plotDomainList<<domain;
304 m_plotDataIndex++;
307 m_plotDataIndex++;
305
308
306 foreach (ChartItem* item, m_chartItems)
309 foreach (ChartItem* item, m_chartItems)
307 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
310 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
308 update();
311 update();
309 }
312 }
310
313
311 void QChart::zoomIn()
314 void QChart::zoomIn()
312 {
315 {
313 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
316 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
314 m_plotDataIndex++;
317 m_plotDataIndex++;
315 foreach (ChartItem* item, m_chartItems)
318 foreach (ChartItem* item, m_chartItems)
316 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
319 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
317 update();
320 update();
318 } else {
321 } else {
319 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
322 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
320 rect.setWidth(rect.width()/2);
323 rect.setWidth(rect.width()/2);
321 rect.setHeight(rect.height()/2);
324 rect.setHeight(rect.height()/2);
322 rect.moveCenter(m_rect.center());
325 rect.moveCenter(m_rect.center());
323 zoomInToRect(rect);
326 zoomInToRect(rect);
324 }
327 }
325 }
328 }
326
329
327 void QChart::zoomOut()
330 void QChart::zoomOut()
328 {
331 {
329 if (m_plotDataIndex > 0) {
332 if (m_plotDataIndex > 0) {
330 m_plotDataIndex--;
333 m_plotDataIndex--;
331 foreach (ChartItem* item, m_chartItems)
334 foreach (ChartItem* item, m_chartItems)
332 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
335 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
333 update();
336 update();
334 }
337 }
335 }
338 }
336
339
337 void QChart::zoomReset()
340 void QChart::zoomReset()
338 {
341 {
339 if (m_plotDataIndex > 0) {
342 if (m_plotDataIndex > 0) {
340 m_plotDataIndex = 0;
343 m_plotDataIndex = 0;
341 foreach (ChartItem* item, m_chartItems)
344 foreach (ChartItem* item, m_chartItems)
342 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
345 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
343 update();
346 update();
344 }
347 }
345 }
348 }
346
349
347 void QChart::setAxisX(const QChartAxis& axis)
350 void QChart::setAxisX(const QChartAxis& axis)
348 {
351 {
349 setAxis(m_axisXItem,axis);
352 setAxis(m_axisXItem,axis);
350 }
353 }
351 void QChart::setAxisY(const QChartAxis& axis)
354 void QChart::setAxisY(const QChartAxis& axis)
352 {
355 {
353 setAxis(m_axisYItem.at(0),axis);
356 setAxis(m_axisYItem.at(0),axis);
354 }
357 }
355
358
356 void QChart::setAxisY(const QList<QChartAxis>& axis)
359 void QChart::setAxisY(const QList<QChartAxis>& axis)
357 {
360 {
358 //TODO not implemented
361 //TODO not implemented
359 }
362 }
360
363
361 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
364 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
362 {
365 {
363 item->setVisible(axis.isAxisVisible());
366 item->setVisible(axis.isAxisVisible());
364 }
367 }
365
368
366 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
369 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
367 {
370 {
368
371
369 m_rect = QRectF(QPoint(0,0),event->newSize());
372 m_rect = QRectF(QPoint(0,0),event->newSize());
370 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
373 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
371
374
372 // recalculate title position
375 // recalculate title position
373 if (m_titleItem) {
376 if (m_titleItem) {
374 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
377 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
375 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
378 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
376 }
379 }
377
380
378 //recalculate background gradient
381 //recalculate background gradient
379 if (m_backgroundItem) {
382 if (m_backgroundItem) {
380 m_backgroundItem->setRect(rect);
383 m_backgroundItem->setRect(rect);
381 if (m_bacgroundOrinetation == HorizonatlGradientOrientation)
384 if (m_bacgroundOrinetation == HorizonatlGradientOrientation)
382 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0);
385 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0);
383 else
386 else
384 m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height());
387 m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height());
385 m_backgroundItem->setBrush(m_backgroundGradient);
388 m_backgroundItem->setBrush(m_backgroundGradient);
386 }
389 }
387
390
388 // resize and reposition childs
391 // resize and reposition childs
389 foreach (ChartItem *item, m_chartItems) {
392 foreach (ChartItem *item, m_chartItems) {
390 item->setPos(rect.topLeft());
393 item->setPos(rect.topLeft());
391 item->setSize(rect.size());
394 item->setSize(rect.size());
392 }
395 }
393
396
394 QGraphicsWidget::resizeEvent(event);
397 QGraphicsWidget::resizeEvent(event);
395 update();
398 update();
396 }
399 }
397
400
398
401
399
402
400 #include "moc_qchart.cpp"
403 #include "moc_qchart.cpp"
401
404
402 QTCOMMERCIALCHART_END_NAMESPACE
405 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now