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