##// END OF EJS Templates
Add gradient bacground support...
Michal Klocek -
r86:84e7b4b4f9e0
parent child
Show More
@@ -32,9 +32,10 int main(int argc, char *argv[])
32 }
32 }
33
33
34 QChartView* chartView = new QChartView(&window);
34 QChartView* chartView = new QChartView(&window);
35 chartView->setRenderHint(QPainter::Antialiasing);
35 chartView->addSeries(series0);
36 chartView->addSeries(series0);
36 chartView->addSeries(series1);
37 chartView->addSeries(series1);
37 //chartView->setBackgroundColor(Qt::yellow);
38 chartView->setBackground(Qt::blue,Qt::yellow,QChart::HorizonatlGradientOrientation);
38
39
39 window.setCentralWidget(chartView);
40 window.setCentralWidget(chartView);
40 window.resize(400, 300);
41 window.resize(400, 300);
@@ -120,13 +120,9 void AxisItem::createItems()
120
120
121 int x = i * deltaX + m_rect.left();
121 int x = i * deltaX + m_rect.left();
122
122
123 //last grid outside chart rect
124 if(i==0) x--;
125 if(i==m_ticks) x++;
126
127 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
123 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
128
124
129 m_grid<<new QGraphicsLineItem(x, m_rect.top()-1, x, m_rect.bottom()+1+LABEL_PADDING,this);
125 m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this);
130
126
131 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
127 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
132 QPointF center = text->boundingRect().center();
128 QPointF center = text->boundingRect().center();
@@ -144,14 +140,10 void AxisItem::createItems()
144
140
145 int y = j * -deltaY + m_rect.bottom();
141 int y = j * -deltaY + m_rect.bottom();
146
142
147 //last grid outside chart rect
148 if(j==0) y++;
149 if(j==m_ticks) y--;
150
151 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
143 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
152 / m_ticks);
144 / m_ticks);
153
145
154 m_grid<<new QGraphicsLineItem(m_rect.left()- 1 - LABEL_PADDING , y, m_rect.right()+1, y,this);
146 m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this);
155 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
147 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
156 QPointF center = text->boundingRect().center();
148 QPointF center = text->boundingRect().center();
157
149
@@ -17,15 +17,14
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18
18
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 m_background(new QGraphicsRectItem()),
20 m_background(0),
21 m_title(new QGraphicsTextItem(this)),
21 m_title(0),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
23 m_plotDataIndex(0),
23 m_plotDataIndex(0),
24 m_marginSize(0)
24 m_marginSize(0)
25 {
25 {
26 // TODO: the default theme?
26 // TODO: the default theme?
27 //setTheme(QChart::ChartThemeVanilla);
27 //setTheme(QChart::ChartThemeVanilla);
28 m_backgroundGradient.setColorAt(0.0, Qt::white);
29
28
30 PlotDomain domain;
29 PlotDomain domain;
31 m_plotDomainList<<domain;
30 m_plotDomainList<<domain;
@@ -164,12 +163,21 void QChart::setSize(const QSize& size)
164 m_rect = QRect(QPoint(0,0),size);
163 m_rect = QRect(QPoint(0,0),size);
165 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
164 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
166
165
166 //recaculate title
167 if(m_title){
168
169 }
167
170
168 //recalculate background gradient
171 //recalculate background gradient
169 m_background->setRect(rect);
172 if(m_background){
170 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
173 m_background->setRect(rect);
171 m_background->setBrush(m_backgroundGradient);
174 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
172 m_background->setPen(Qt::NoPen);
175 m_backgroundGradient.setFinalStop(m_background->rect().width(),0);
176 else
177 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
178
179 m_background->setBrush(m_backgroundGradient);
180 }
173
181
174 //resize elements
182 //resize elements
175 foreach (ChartItem* item ,m_chartItems) {
183 foreach (ChartItem* item ,m_chartItems) {
@@ -186,10 +194,25 void QChart::setSize(const QSize& size)
186 update();
194 update();
187 }
195 }
188
196
189 void QChart::setBackgroundColor(const QColor& color)
197 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
190 {
198 {
191 m_backgroundGradient.setColorAt( 0.0, Qt::white);
199
192 m_backgroundGradient.setColorAt( 1.0, color);
200 if(!m_background){
201 m_background = new QGraphicsRectItem(this);
202 m_background->setZValue(-1);
203 }
204
205 m_bacgroundOrinetation = orientation;
206 m_backgroundGradient.setColorAt( 0.0, startColor);
207 m_backgroundGradient.setColorAt( 1.0, endColor);
208 m_backgroundGradient.setStart(0,0);
209
210 if(orientation == VerticalGradientOrientation){
211 m_backgroundGradient.setFinalStop(0,m_rect.height());
212 }else{
213 m_backgroundGradient.setFinalStop(m_rect.width(),0);
214 }
215
193 m_background->setBrush(m_backgroundGradient);
216 m_background->setBrush(m_backgroundGradient);
194 m_background->setPen(Qt::NoPen);
217 m_background->setPen(Qt::NoPen);
195 m_background->update();
218 m_background->update();
@@ -197,6 +220,7 void QChart::setBackgroundColor(const QColor& color)
197
220
198 void QChart::setTitle(const QString& title)
221 void QChart::setTitle(const QString& title)
199 {
222 {
223 if(!m_title) m_title = new QGraphicsTextItem(this);
200 m_title->setPlainText(title);
224 m_title->setPlainText(title);
201 }
225 }
202
226
@@ -266,8 +290,10 void QChart::setTheme(QChart::ChartThemeId theme)
266 break;
290 break;
267 }
291 }
268
292
293 if(m_background){
269 m_background->setBrush(m_backgroundGradient);
294 m_background->setBrush(m_backgroundGradient);
270 m_background->setPen(Qt::NoPen);
295 m_background->setPen(Qt::NoPen);
296 }
271
297
272 foreach(QChartSeries* series, m_chartSeries) {
298 foreach(QChartSeries* series, m_chartSeries) {
273 // TODO: other series interested on themes?
299 // TODO: other series interested on themes?
@@ -27,6 +27,10 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
27 {
27 {
28 Q_OBJECT
28 Q_OBJECT
29 public:
29 public:
30 enum GradientOrientation {
31 HorizonatlGradientOrientation,
32 VerticalGradientOrientation
33 };
30 enum ChartThemeId {
34 enum ChartThemeId {
31 /*! The default theme follows the GUI style of the Operating System */
35 /*! The default theme follows the GUI style of the Operating System */
32 ChartThemeDefault = 0,
36 ChartThemeDefault = 0,
@@ -56,7 +60,7 public:
56 void setTheme(QChart::ChartThemeId theme);
60 void setTheme(QChart::ChartThemeId theme);
57
61
58 void setTitle(const QString& title);
62 void setTitle(const QString& title);
59 void setBackgroundColor(const QColor& color);
63 void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation);
60
64
61 void zoomInToRect(const QRect& rectangle);
65 void zoomInToRect(const QRect& rectangle);
62 void zoomIn();
66 void zoomIn();
@@ -80,6 +84,7 private:
80 Q_DISABLE_COPY(QChart)
84 Q_DISABLE_COPY(QChart)
81 QGraphicsRectItem* m_background;
85 QGraphicsRectItem* m_background;
82 QLinearGradient m_backgroundGradient;
86 QLinearGradient m_backgroundGradient;
87 GradientOrientation m_bacgroundOrinetation;
83 QGraphicsTextItem* m_title;
88 QGraphicsTextItem* m_title;
84 AxisItem* m_axisX;
89 AxisItem* m_axisX;
85 QList<AxisItem*> m_axisY;
90 QList<AxisItem*> m_axisY;
@@ -69,9 +69,9 void QChartView::setTitle(const QString& title)
69 m_chart->setTitle(title);
69 m_chart->setTitle(title);
70 }
70 }
71
71
72 void QChartView::setBackgroundColor(const QColor& color)
72 void QChartView::setBackground(const QColor& startColor, const QColor& endColor, QChart::GradientOrientation orientation)
73 {
73 {
74 m_chart->setBackgroundColor(color);
74 m_chart->setBackground(startColor,endColor,orientation);
75 }
75 }
76
76
77 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,6 +3,7
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchartseries.h"
5 #include "qchartseries.h"
6 #include "qchart.h"
6 #include <QGraphicsView>
7 #include <QGraphicsView>
7
8
8 class QGraphicsScene;
9 class QGraphicsScene;
@@ -26,7 +27,7 public:
26
27
27 int margin() const;
28 int margin() const;
28 void setTitle(const QString& title);
29 void setTitle(const QString& title);
29 void setBackgroundColor(const QColor& color);
30 void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, QChart::GradientOrientation orientation = QChart::VerticalGradientOrientation);
30 void zoomInToRect(const QRect& rectangle);
31 void zoomInToRect(const QRect& rectangle);
31 void zoomIn();
32 void zoomIn();
32 void zoomOut();
33 void zoomOut();
@@ -52,10 +52,6 void QXYChartSeries::setPen(const QPen& pen)
52 m_pen=pen;
52 m_pen=pen;
53 }
53 }
54
54
55 void QXYChartSeries::setBrush(const QBrush& brush)
56 {
57 m_brush=brush;
58 }
59
55
60 QDebug operator<< (QDebug debug, const QXYChartSeries series)
56 QDebug operator<< (QDebug debug, const QXYChartSeries series)
61 {
57 {
@@ -27,8 +27,6 public:
27
27
28 void setPen(const QPen& pen);
28 void setPen(const QPen& pen);
29 const QPen& pen() const { return m_pen;}
29 const QPen& pen() const { return m_pen;}
30 void setBrush(const QBrush& brush);
31 const QBrush& brush() const { return m_brush;}
32
30
33 int count() const;
31 int count() const;
34 qreal x(int pos) const;
32 qreal x(int pos) const;
@@ -38,7 +36,6 public:
38 private:
36 private:
39 QList<qreal> m_x;
37 QList<qreal> m_x;
40 QList<qreal> m_y;
38 QList<qreal> m_y;
41 QBrush m_brush;
42 QPen m_pen;
39 QPen m_pen;
43
40
44 };
41 };
@@ -60,7 +60,7 void XYLineChartItem::updateGeometry()
60
60
61 m_pathItem->setPath(path);
61 m_pathItem->setPath(path);
62 m_pathItem->setPen(m_series->pen());
62 m_pathItem->setPen(m_series->pen());
63 m_pathItem->setBrush(m_series->brush());
63 m_pathItem->setBrush(Qt::NoBrush);
64 }
64 }
65
65
66
66
General Comments 0
You need to be logged in to leave comments. Login now