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