##// END OF EJS Templates
Add background to chart...
Michal Klocek -
r69:c7c3c4960b21
parent child
Show More
@@ -0,0 +1,15
1 !include( ../../common.pri ) {
2 error( "Couldn't find the common.pri file!" )
3 }
4
5 !include( ../../integrated.pri ) {
6 error( "Couldn't find the integrated.pri file !")
7 }
8
9 TARGET = colorlineChart
10 TEMPLATE = app
11 QT += core gui
12 SOURCES += main.cpp
13
14
15
@@ -0,0 +1,40
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <qchartview.h>
4 #include <qxychartseries.h>
5 #include <qchart.h>
6 #include <cmath>
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
10 #define PI 3.14159265358979
11
12 int main(int argc, char *argv[])
13 {
14 QApplication a(argc, argv);
15
16 QMainWindow window;
17
18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
20 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
22
23 int numPoints = 100;
24
25 for (int x = 0; x <= numPoints; ++x) {
26 series0->add(x, abs(sin(PI/50*x)*100));
27 series1->add(x, abs(cos(PI/50*x)*100));
28 }
29
30 QChartView* chartView = new QChartView(&window);
31 chartView->addSeries(series0);
32 chartView->addSeries(series1);
33 chartView->setBackgroundColor(Qt::yellow);
34
35 window.setCentralWidget(chartView);
36 window.resize(400, 300);
37 window.show();
38
39 return a.exec();
40 }
@@ -1,3 +1,4
1 1 TEMPLATE = subdirs
2 2 SUBDIRS += linechart \
3 zoomlinechart
3 zoomlinechart \
4 colorlinechart
@@ -1,264 +1,287
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 "qxychartseries.h"
7 7
8 8 #include "barchartseries.h"
9 9 #include "bargroup.h"
10 10
11 11 #include "xylinechartitem_p.h"
12 12 #include "plotdomain_p.h"
13 13 #include "axisitem_p.h"
14 14 #include <QGraphicsScene>
15 15 #include <QDebug>
16 16
17 17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 18
19 19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 m_background(new QGraphicsRectItem(this)),
21 m_title(new QGraphicsTextItem(this)),
20 22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
21 23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
22 24 m_plotDataIndex(0),
23 25 m_marginSize(0)
24 26 {
25 27 // TODO: the default theme?
26 28 setTheme(QChart::ChartThemeVanilla);
27 29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
28 30 PlotDomain domain;
29 31 m_plotDomainList<<domain;
30 32
31 33 m_chartItems<<m_axisX;
32 34 m_chartItems<<m_axisY;
33 35 }
34 36
35 37 QChart::~QChart(){}
36 38
37 39 QRectF QChart::boundingRect() const
38 40 {
39 41 return m_rect;
40 42 }
41 43
42 44 void QChart::addSeries(QChartSeries* series)
43 45 {
44 46 // TODO: we should check the series not already added
45 47
46 48 m_series<<series;
47 49
48 50 switch(series->type())
49 51 {
50 52 case QChartSeries::SeriesTypeLine: {
51 53
52 54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
53 55 // Use color defined by theme in case the series does not define a custom color
54 56 if (!xyseries->color().isValid() && m_themeColors.count())
55 57 xyseries->setColor(m_themeColors.takeFirst());
56 58
57 59 m_plotDataIndex = 0 ;
58 60 m_plotDomainList.resize(1);
59 61
60 62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
61 63
62 64 for (int i = 0 ; i < xyseries->count() ; i++)
63 65 {
64 66 qreal x = xyseries->x(i);
65 67 qreal y = xyseries->y(i);
66 68 domain.m_minX = qMin(domain.m_minX,x);
67 69 domain.m_minY = qMin(domain.m_minY,y);
68 70 domain.m_maxX = qMax(domain.m_maxX,x);
69 71 domain.m_maxY = qMax(domain.m_maxY,y);
70 72 }
71 73
72 74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
73 75
74 76 m_chartItems<<item;
75 77
76 78 foreach(ChartItem* i ,m_chartItems)
77 79 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
78 80
79 81 break;
80 82 }
81 83 case QChartSeries::SeriesTypeBar: {
82 84
83 85 qDebug() << "barSeries added";
84 86 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
85 87
86 88 // Who owns the series?
87 89 BarGroup* group = new BarGroup(*barSeries, this);
88 90 scene()->addItem(group);
89 91 m_BarGroupItems.append(group); // If we need to access group later
90 92 break;
91 93 }
92 94 case QChartSeries::SeriesTypeScatter: {
93 95 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
94 96 connect(this, SIGNAL(sizeChanged(QRectF)),
95 97 scatterSeries, SLOT(chartSizeChanged(QRectF)));
96 98 scatterSeries->d->setParentItem(this);
97 99 QColor nextColor = m_themeColors.takeFirst();
98 100 nextColor.setAlpha(150); // TODO: default opacity?
99 101 scatterSeries->setMarkerColor(nextColor);
100 102 }
101 103 case QChartSeries::SeriesTypePie: {
102 104 // TODO: we now have also a list of y values as a parameter, it is ignored
103 105 // we should use a generic data class instead of list of x and y values
104 106 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
105 107 connect(this, SIGNAL(sizeChanged(QRectF)),
106 108 pieSeries, SLOT(chartSizeChanged(QRectF)));
107 109 // TODO: how to define the color for all the slices of a pie?
108 110 }
109 111 }
110 112 }
111 113
112 114 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
113 115 {
114 116 // TODO: support also other types; not only scatter and pie
115 117
116 118 QChartSeries *series(0);
117 119
118 120 switch (type) {
119 121 case QChartSeries::SeriesTypeLine: {
120 122 series = QXYChartSeries::create();
121 123 break;
122 124 }
123 125 case QChartSeries::SeriesTypeBar: {
124 126 series = new BarChartSeries(this);
125 127 break;
126 128 }
127 129 case QChartSeries::SeriesTypeScatter: {
128 130 series = new QScatterSeries(this);
129 131 break;
130 132 }
131 133 case QChartSeries::SeriesTypePie: {
132 134 series = new QPieSeries(this);
133 135 break;
134 136 }
135 137 default:
136 138 Q_ASSERT(false);
137 139 break;
138 140 }
139 141
140 142 addSeries(series);
141 143 return series;
142 144 }
143 145
144 146 void QChart::setSize(const QSize& size)
145 147 {
146 148 m_rect = QRect(QPoint(0,0),size);
147 149 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
148 150
151
152 //recalculate background gradient
153 m_background->setRect(rect);
154 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
155 m_background->setBrush(m_backgroundGradient);
156
157 //resize elements
149 158 foreach (ChartItem* item ,m_chartItems) {
150 159 item->setPos(rect.topLeft());
151 160 item->setSize(rect.size());
152 161
153 162 }
154 163 // TODO: TTD for setting scale
155 164 //emit scaleChanged(100, 100);
156 165 // TODO: calculate the origo
157 166 // TODO: not sure if emitting a signal here is the best from performance point of view
158 167 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
159 168
160 169 update();
161 170 }
162 171
172 void QChart::setBackgroundColor(const QColor& color)
173 {
174 m_backgroundGradient.setColorAt( 0.0, Qt::white);
175 m_backgroundGradient.setColorAt( 1.0, color);
176 m_background->setBrush(m_backgroundGradient);
177 m_background->setPen(Qt::NoPen);
178 m_background->update();
179 }
180
181 void QChart::setTitle(const QString& title)
182 {
183 m_title->setPlainText(title);
184 }
185
163 186 int QChart::margin() const
164 187 {
165 188 return m_marginSize;
166 189 }
167 190
168 191 void QChart::setMargin(int margin)
169 192 {
170 193 m_marginSize = margin;
171 194 }
172 195
173 196 void QChart::setTheme(QChart::ChartTheme theme)
174 197 {
175 198 // TODO: define color themes
176 199 switch (theme) {
177 200 case ChartThemeVanilla:
178 201 m_themeColors.append(QColor(255, 238, 174));
179 202 m_themeColors.append(QColor(228, 228, 160));
180 203 m_themeColors.append(QColor(228, 179, 160));
181 204 m_themeColors.append(QColor(180, 151, 18));
182 205 m_themeColors.append(QColor(252, 252, 37));
183 206 break;
184 207 case ChartThemeIcy:
185 208 m_themeColors.append(QColor(255, 238, 174));
186 209 m_themeColors.append(QColor(228, 228, 160));
187 210 m_themeColors.append(QColor(228, 179, 160));
188 211 m_themeColors.append(QColor(180, 151, 18));
189 212 m_themeColors.append(QColor(252, 252, 37));
190 213 break;
191 214 case ChartThemeGrayscale:
192 215 m_themeColors.append(QColor(255, 238, 174));
193 216 m_themeColors.append(QColor(228, 228, 160));
194 217 m_themeColors.append(QColor(228, 179, 160));
195 218 m_themeColors.append(QColor(180, 151, 18));
196 219 m_themeColors.append(QColor(252, 252, 37));
197 220 break;
198 221 default:
199 222 Q_ASSERT(false);
200 223 break;
201 224 }
202 225
203 226 // TODO: update coloring of different elements to match the selected theme
204 227 }
205 228
206 229 void QChart::zoomInToRect(const QRect& rectangle)
207 230 {
208 231
209 232 if(!rectangle.isValid()) return;
210 233
211 234 qreal margin = this->margin();
212 235
213 236 QRect rect = rectangle.normalized();
214 237 rect.translate(-margin, -margin);
215 238
216 239 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
217 240 PlotDomain newDomain;
218 241
219 242 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
220 243 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
221 244
222 245 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
223 246 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
224 247 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
225 248 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
226 249
227 250 m_plotDomainList.resize(m_plotDataIndex + 1);
228 251 m_plotDomainList<<newDomain;
229 252 m_plotDataIndex++;
230 253
231 254 foreach (ChartItem* item ,m_chartItems)
232 255 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
233 256 update();
234 257 }
235 258
236 259 void QChart::zoomIn()
237 260 {
238 261 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
239 262 m_plotDataIndex++;
240 263 foreach (ChartItem* item ,m_chartItems)
241 264 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
242 265 update();
243 266 }else{
244 267 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
245 268 rect.setWidth(rect.width()/2);
246 269 rect.setHeight(rect.height()/2);
247 270 rect.moveCenter(m_rect.center());
248 271 zoomInToRect(rect);
249 272 }
250 273 }
251 274
252 275 void QChart::zoomOut()
253 276 {
254 277 if (m_plotDataIndex > 0) {
255 278 m_plotDataIndex--;
256 279 foreach (ChartItem* item ,m_chartItems)
257 280 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
258 281 update();
259 282 }
260 283 }
261 284
262 285 #include "moc_qchart.cpp"
263 286
264 287 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,80 +1,86
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsObject>
7 #include <QLinearGradient>
7 8
8 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 10
10 11 class AxisItem;
11 12 class QChartSeries;
12 13 class PlotDomain;
13 14 class ChartItem;
14 15 class BarGroup;
15 16
16 17 // TODO: We don't need to have QChart tied to QGraphicsItem:
17 18 //class QTCOMMERCIALCHART_EXPORT QChart
18 19 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
19 20 // public: QChartGraphicsItem(QChart &chart);
20 21
21 22 /*!
22 23 * TODO: define the responsibilities
23 24 */
24 25 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
25 26 {
26 27 Q_OBJECT
27 28 public:
28 29 enum ChartTheme {
29 30 ChartThemeVanilla = 0,
30 31 ChartThemeIcy,
31 32 ChartThemeGrayscale
32 33 };
33 34
34 35 public:
35 36 QChart(QGraphicsObject* parent = 0);
36 37 ~QChart();
37 38
38 39 //from QGraphicsItem
39 40 QRectF boundingRect() const;
40 41 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
41 42
42 43 void addSeries(QChartSeries* series);
43 44 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
44 45 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
45 46 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
46 47
47 48 void setSize(const QSize& size);
48 49 void setMargin(int margin);
49 50 int margin() const;
50 51 void setTheme(QChart::ChartTheme theme);
51 52
53 void setTitle(const QString& title);
54 void setBackgroundColor(const QColor& color);
55
52 56 void zoomInToRect(const QRect& rectangle);
53 57 void zoomIn();
54 58 void zoomOut();
55 59
56 60 signals:
57 61 //TODO chage to const QSize& size
58 62 void sizeChanged(QRectF rect);
59 63 void scaleChanged(qreal xscale, qreal yscale);
60 64
61 65 private:
62 66 Q_DISABLE_COPY(QChart)
67 QGraphicsRectItem* m_background;
68 QLinearGradient m_backgroundGradient;
69 QGraphicsTextItem* m_title;
63 70 AxisItem* m_axisX;
64 71 AxisItem* m_axisY;
65 72 QRect m_rect;
66 73 QList<const QChartSeries*> m_series;
67 74 QVector<PlotDomain> m_plotDomainList;
68 75 QList<ChartItem*> m_chartItems;
69 76 //TODO: remove
70 77 QList<QGraphicsItem*> m_items;
71 78 int m_plotDataIndex;
72 79 int m_marginSize;
73 80 QList<QColor> m_themeColors;
74
75 81 QList<BarGroup*> m_BarGroupItems;
76 82 };
77 83
78 84 QTCOMMERCIALCHART_END_NAMESPACE
79 85
80 86 #endif
@@ -1,66 +1,77
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include <QGraphicsView>
4 4 #include <QGraphicsScene>
5 5 #include <QRubberBand>
6 6 #include <QResizeEvent>
7 7 #include <QDebug>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 QChartView::QChartView(QWidget *parent) :
12 12 QGraphicsView(parent),
13 13 m_scene(new QGraphicsScene()),
14 14 m_chart(new QChart())
15 15 {
16 16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
18 18 setScene(m_scene);
19 19 m_chart->setMargin(50);
20 20 m_scene->addItem(m_chart);
21 21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
22 22 }
23 23
24 24 QChartView::~QChartView()
25 25 {
26 26 }
27 27
28 28 void QChartView::resizeEvent(QResizeEvent *event)
29 29 {
30 30 m_scene->setSceneRect(0,0,size().width(),size().height());
31 31 m_chart->setSize(size());
32 32 QWidget::resizeEvent(event);
33 33 }
34 34
35 35
36 36 void QChartView::addSeries(QChartSeries* series)
37 37 {
38 38 m_chart->addSeries(series);
39 39 }
40 40
41 41 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
42 42 {
43 43
44 44 return m_chart->createSeries(type);
45 45 }
46 46
47 47 void QChartView::zoomInToRect(const QRect& rectangle)
48 48 {
49 49 m_chart->zoomInToRect(rectangle);
50 50 }
51 51
52 52 void QChartView::zoomIn()
53 53 {
54 54 m_chart->zoomIn();
55 55 }
56 56
57 57 void QChartView::zoomOut()
58 58 {
59 59 m_chart->zoomOut();
60 60 }
61 61
62 62 int QChartView::margin() const
63 63 {
64 64 return m_chart->margin();
65 65 }
66
67 void QChartView::setTitle(const QString& title)
68 {
69 m_chart->setTitle(title);
70 }
71
72 void QChartView::setBackgroundColor(const QColor& color)
73 {
74 m_chart->setBackgroundColor(color);
75 }
76
66 77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,45
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include <QGraphicsView>
7 7
8 8 class QGraphicsScene;
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 class QChart;
13 13
14 14 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
15 15 {
16 16 public:
17 17 explicit QChartView(QWidget *parent = 0);
18 18 ~QChartView();
19 19
20 20 //implement from QWidget
21 21 void resizeEvent(QResizeEvent *event);
22 22
23 23 void addSeries(QChartSeries* series);
24 24 // Convenience function
25 25 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
26 26
27 27 int margin() const;
28 void setTitle(const QString& title);
29 void setBackgroundColor(const QColor& color);
28 30 void zoomInToRect(const QRect& rectangle);
29 31 void zoomIn();
30 32 void zoomOut();
31 33
32 34 private:
33 35 QGraphicsScene *m_scene;
34 36 QChart* m_chart;
35 37 QPoint m_origin;
36 38 Q_DISABLE_COPY(QChartView)
37 39
38 40
39 41 };
40 42
41 43 QTCOMMERCIALCHART_END_NAMESPACE
42 44
43 45 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now