@@ -1,46 +1,51 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qxychartseries.h> |
|
5 | 5 | #include <qchart.h> |
|
6 | 6 | #include <cmath> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | #define PI 3.14159265358979 |
|
11 | 11 | |
|
12 | 12 | int main(int argc, char *argv[]) |
|
13 | 13 | { |
|
14 | 14 | QApplication a(argc, argv); |
|
15 | 15 | |
|
16 | 16 | QMainWindow window; |
|
17 | 17 | |
|
18 | 18 | QXYChartSeries* series0 = QXYChartSeries::create(); |
|
19 | 19 | QPen blue(Qt::blue); |
|
20 | 20 | blue.setWidth(3); |
|
21 | 21 | series0->setPen(blue); |
|
22 | 22 | QXYChartSeries* series1 = QXYChartSeries::create(); |
|
23 | 23 | QPen red(Qt::red); |
|
24 | 24 | red.setWidth(3); |
|
25 | 25 | series1->setPen(red); |
|
26 | 26 | |
|
27 | 27 | int numPoints = 100; |
|
28 | 28 | |
|
29 | 29 | for (int x = 0; x <= numPoints; ++x) { |
|
30 | 30 | series0->add(x, abs(sin(PI/50*x)*100)); |
|
31 | 31 | series1->add(x, abs(cos(PI/50*x)*100)); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | QChartView* chartView = new QChartView(&window); |
|
35 | 35 | chartView->setRenderHint(QPainter::Antialiasing); |
|
36 | 36 | chartView->setTitle("Custom color line chart example"); |
|
37 | 37 | chartView->addSeries(series0); |
|
38 | 38 | chartView->addSeries(series1); |
|
39 | chartView->setBackground(Qt::blue,Qt::yellow,QChart::HorizonatlGradientOrientation); | |
|
39 | ||
|
40 | QLinearGradient backgroundGradient; | |
|
41 | backgroundGradient.setColorAt(0.0, Qt::blue); | |
|
42 | backgroundGradient.setColorAt(1.0, Qt::yellow); | |
|
43 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
|
44 | chartView->setChartBackgroundBrush(backgroundGradient); | |
|
40 | 45 | |
|
41 | 46 | window.setCentralWidget(chartView); |
|
42 | 47 | window.resize(400, 300); |
|
43 | 48 | window.show(); |
|
44 | 49 | |
|
45 | 50 | return a.exec(); |
|
46 | 51 | } |
@@ -1,414 +1,433 | |||
|
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 "chartitem_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 <QGraphicsSceneResizeEvent> |
|
23 | 23 | #include <QDebug> |
|
24 | 24 | |
|
25 | 25 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
26 | 26 | |
|
27 | 27 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
28 | 28 | m_backgroundItem(0), |
|
29 | 29 | m_titleItem(0), |
|
30 | 30 | m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)), |
|
31 | 31 | m_plotDataIndex(0), |
|
32 | 32 | m_marginSize(0), |
|
33 | 33 | m_chartTheme(new ChartTheme(this)) |
|
34 | 34 | { |
|
35 | 35 | // TODO: the default theme? |
|
36 | 36 | setTheme(QChart::ChartThemeDefault); |
|
37 | 37 | |
|
38 | 38 | PlotDomain domain; |
|
39 | 39 | m_plotDomainList << domain; |
|
40 | 40 | m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this); |
|
41 | 41 | m_chartItems << m_axisXItem; |
|
42 | 42 | m_chartItems << m_axisYItem.at(0); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | QChart::~QChart(){} |
|
46 | 46 | |
|
47 | 47 | void QChart::addSeries(QChartSeries* series) |
|
48 | 48 | { |
|
49 | 49 | // TODO: we should check the series not already added |
|
50 | 50 | Q_ASSERT(series); |
|
51 | 51 | Q_ASSERT(series->type() != QChartSeries::SeriesTypeInvalid); |
|
52 | 52 | |
|
53 | 53 | m_chartSeries << series; |
|
54 | 54 | |
|
55 | 55 | m_plotDataIndex = 0 ; |
|
56 | 56 | m_plotDomainList.resize(1); |
|
57 | 57 | PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; |
|
58 | 58 | |
|
59 | 59 | switch(series->type()) |
|
60 | 60 | { |
|
61 | 61 | case QChartSeries::SeriesTypeLine: { |
|
62 | 62 | |
|
63 | 63 | QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); |
|
64 | 64 | |
|
65 | 65 | for (int i = 0 ; i < xyseries->count() ; i++) { |
|
66 | 66 | qreal x = xyseries->x(i); |
|
67 | 67 | qreal y = xyseries->y(i); |
|
68 | 68 | domain.m_minX = qMin(domain.m_minX,x); |
|
69 | 69 | domain.m_minY = qMin(domain.m_minY,y); |
|
70 | 70 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
71 | 71 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | XYLineChartItem* item = new XYLineChartItem(xyseries,this); | |
|
74 | XYLineChartItem* item = new XYLineChartItem(xyseries,0,this); | |
|
75 | 75 | |
|
76 | 76 | m_chartItems << item; |
|
77 | 77 | // TODO: |
|
78 | 78 | //m_chartTheme->addObserver(xyseries); |
|
79 | 79 | |
|
80 | 80 | break; |
|
81 | 81 | } |
|
82 | 82 | case QChartSeries::SeriesTypeBar: { |
|
83 | 83 | |
|
84 | 84 | qDebug() << "barSeries added"; |
|
85 | 85 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); |
|
86 | 86 | BarGroup* barGroup = new BarGroup(*barSeries,this); |
|
87 | 87 | |
|
88 | 88 | // Add some fugly colors for 5 fist series... |
|
89 | 89 | barGroup->addColor(QColor(255,0,0,128)); |
|
90 | 90 | barGroup->addColor(QColor(255,255,0,128)); |
|
91 | 91 | barGroup->addColor(QColor(0,255,0,128)); |
|
92 | 92 | barGroup->addColor(QColor(0,0,255,128)); |
|
93 | 93 | barGroup->addColor(QColor(255,128,0,128)); |
|
94 | 94 | |
|
95 | 95 | m_chartItems << barGroup; |
|
96 | 96 | childItems().append(barGroup); |
|
97 | 97 | |
|
98 | 98 | qreal x = barSeries->countColumns(); |
|
99 | 99 | qreal y = barSeries->max(); |
|
100 | 100 | domain.m_minX = qMin(domain.m_minX,x); |
|
101 | 101 | domain.m_minY = qMin(domain.m_minY,y); |
|
102 | 102 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
103 | 103 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
104 | 104 | m_axisXItem->setVisible(false); |
|
105 | 105 | break; |
|
106 | 106 | } |
|
107 | 107 | case QChartSeries::SeriesTypeStackedBar: { |
|
108 | 108 | |
|
109 | 109 | qDebug() << "barSeries added"; |
|
110 | 110 | StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); |
|
111 | 111 | StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this); |
|
112 | 112 | |
|
113 | 113 | // Add some fugly colors for 5 fist series... |
|
114 | 114 | stackedBarGroup->addColor(QColor(255,0,0,128)); |
|
115 | 115 | stackedBarGroup->addColor(QColor(255,255,0,128)); |
|
116 | 116 | stackedBarGroup->addColor(QColor(0,255,0,128)); |
|
117 | 117 | stackedBarGroup->addColor(QColor(0,0,255,128)); |
|
118 | 118 | stackedBarGroup->addColor(QColor(255,128,0,128)); |
|
119 | 119 | |
|
120 | 120 | m_chartItems << stackedBarGroup; |
|
121 | 121 | childItems().append(stackedBarGroup); |
|
122 | 122 | |
|
123 | 123 | qreal x = stackedBarSeries->countColumns(); |
|
124 | 124 | qreal y = stackedBarSeries->maxColumnSum(); |
|
125 | 125 | domain.m_minX = qMin(domain.m_minX,x); |
|
126 | 126 | domain.m_minY = qMin(domain.m_minY,y); |
|
127 | 127 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
128 | 128 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
129 | 129 | m_axisXItem->setVisible(false); |
|
130 | 130 | break; |
|
131 | 131 | } |
|
132 | 132 | case QChartSeries::SeriesTypePercentBar: { |
|
133 | 133 | |
|
134 | 134 | qDebug() << "barSeries added"; |
|
135 | 135 | PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); |
|
136 | 136 | PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this); |
|
137 | 137 | |
|
138 | 138 | // Add some fugly colors for 5 fist series... |
|
139 | 139 | percentBarGroup->addColor(QColor(255,0,0,128)); |
|
140 | 140 | percentBarGroup->addColor(QColor(255,255,0,128)); |
|
141 | 141 | percentBarGroup->addColor(QColor(0,255,0,128)); |
|
142 | 142 | percentBarGroup->addColor(QColor(0,0,255,128)); |
|
143 | 143 | percentBarGroup->addColor(QColor(255,128,0,128)); |
|
144 | 144 | |
|
145 | 145 | m_chartItems << percentBarGroup; |
|
146 | 146 | childItems().append(percentBarGroup); |
|
147 | 147 | |
|
148 | 148 | qreal x = percentBarSeries->countColumns(); |
|
149 | 149 | domain.m_minX = qMin(domain.m_minX,x); |
|
150 | 150 | domain.m_minY = 0; |
|
151 | 151 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
152 | 152 | domain.m_maxY = 100; |
|
153 | 153 | m_axisXItem->setVisible(false); |
|
154 | 154 | break; |
|
155 | 155 | } |
|
156 | 156 | case QChartSeries::SeriesTypeScatter: { |
|
157 | 157 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
158 | 158 | scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); |
|
159 | 159 | scatterSeries->d->setParentItem(this); |
|
160 | 160 | scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
161 | 161 | m_chartItems << scatterSeries->d; |
|
162 | 162 | m_chartTheme->addObserver(scatterSeries->d); |
|
163 | 163 | |
|
164 | 164 | foreach (qreal x, scatterSeries->d->m_x) { |
|
165 | 165 | domain.m_minX = qMin(domain.m_minX, x); |
|
166 | 166 | domain.m_maxX = qMax(domain.m_maxX, x); |
|
167 | 167 | } |
|
168 | 168 | foreach (qreal y, scatterSeries->d->m_y) { |
|
169 | 169 | domain.m_minY = qMin(domain.m_minY, y); |
|
170 | 170 | domain.m_maxY = qMax(domain.m_maxY, y); |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | break; |
|
174 | 174 | } |
|
175 | 175 | case QChartSeries::SeriesTypePie: { |
|
176 | 176 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
177 | 177 | pieSeries->d->setParentItem(this); |
|
178 | 178 | m_chartItems << pieSeries->d; |
|
179 | 179 | pieSeries->d->m_chartTheme = m_chartTheme; |
|
180 | 180 | m_chartTheme->addObserver(pieSeries->d); |
|
181 | 181 | break; |
|
182 | 182 | } |
|
183 | 183 | default: |
|
184 | 184 | break; |
|
185 | 185 | } |
|
186 | 186 | |
|
187 | 187 | // Update all the items to match the new visible area of the chart |
|
188 | 188 | foreach(ChartItem* i, m_chartItems) |
|
189 | 189 | i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) |
|
193 | 193 | { |
|
194 | 194 | // TODO: support also other types; not only scatter and pie |
|
195 | 195 | |
|
196 | 196 | QChartSeries *series(0); |
|
197 | 197 | |
|
198 | 198 | switch (type) { |
|
199 | 199 | case QChartSeries::SeriesTypeLine: { |
|
200 | 200 | series = QXYChartSeries::create(); |
|
201 | 201 | break; |
|
202 | 202 | } |
|
203 | 203 | case QChartSeries::SeriesTypeBar: { |
|
204 | 204 | series = new BarChartSeries(this); |
|
205 | 205 | break; |
|
206 | 206 | } |
|
207 | 207 | case QChartSeries::SeriesTypeStackedBar: { |
|
208 | 208 | series = new StackedBarChartSeries(this); |
|
209 | 209 | break; |
|
210 | 210 | } |
|
211 | 211 | case QChartSeries::SeriesTypePercentBar: { |
|
212 | 212 | series = new PercentBarChartSeries(this); |
|
213 | 213 | break; |
|
214 | 214 | } |
|
215 | 215 | case QChartSeries::SeriesTypeScatter: { |
|
216 | 216 | series = new QScatterSeries(this); |
|
217 | 217 | break; |
|
218 | 218 | } |
|
219 | 219 | case QChartSeries::SeriesTypePie: { |
|
220 | 220 | series = new QPieSeries(this); |
|
221 | 221 | break; |
|
222 | 222 | } |
|
223 | 223 | default: |
|
224 | 224 | Q_ASSERT(false); |
|
225 | 225 | break; |
|
226 | 226 | } |
|
227 | 227 | |
|
228 | 228 | addSeries(series); |
|
229 | 229 | return series; |
|
230 | 230 | } |
|
231 | 231 | |
|
232 | 232 | void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation) |
|
233 | 233 | { |
|
234 | 234 | |
|
235 | 235 | if(!m_backgroundItem){ |
|
236 | 236 | m_backgroundItem = new QGraphicsRectItem(this); |
|
237 | 237 | m_backgroundItem->setZValue(-1); |
|
238 | 238 | } |
|
239 | 239 | |
|
240 | 240 | m_bacgroundOrinetation = orientation; |
|
241 | 241 | m_backgroundGradient.setColorAt(0.0, startColor); |
|
242 | 242 | m_backgroundGradient.setColorAt(1.0, endColor); |
|
243 | m_backgroundGradient.setStart(0,0); | |
|
243 | m_backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
|
244 | 244 | |
|
245 | 245 | if(orientation == VerticalGradientOrientation){ |
|
246 |
m_backgroundGradient.setFinalStop(0, |
|
|
246 | m_backgroundGradient.setFinalStop(0,1); | |
|
247 | 247 | }else{ |
|
248 |
m_backgroundGradient.setFinalStop( |
|
|
248 | m_backgroundGradient.setFinalStop(1,0); | |
|
249 | 249 | } |
|
250 | 250 | |
|
251 | 251 | m_backgroundItem->setBrush(m_backgroundGradient); |
|
252 | 252 | m_backgroundItem->setPen(Qt::NoPen); |
|
253 | 253 | m_backgroundItem->update(); |
|
254 | 254 | } |
|
255 | 255 | |
|
256 | void QChart::setChartBackgroundBrush(const QBrush& brush) | |
|
257 | { | |
|
258 | ||
|
259 | if(!m_backgroundItem){ | |
|
260 | m_backgroundItem = new QGraphicsRectItem(this); | |
|
261 | m_backgroundItem->setZValue(-1); | |
|
262 | } | |
|
263 | ||
|
264 | m_backgroundItem->setBrush(brush); | |
|
265 | m_backgroundItem->update(); | |
|
266 | } | |
|
267 | ||
|
268 | void QChart::setChartBackgroundPen(const QPen& pen) | |
|
269 | { | |
|
270 | ||
|
271 | if(!m_backgroundItem){ | |
|
272 | m_backgroundItem = new QGraphicsRectItem(this); | |
|
273 | m_backgroundItem->setZValue(-1); | |
|
274 | } | |
|
275 | ||
|
276 | m_backgroundItem->setPen(pen); | |
|
277 | m_backgroundItem->update(); | |
|
278 | } | |
|
279 | ||
|
256 | 280 | void QChart::setTitle(const QString& title,const QFont& font) |
|
257 | 281 | { |
|
258 | 282 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); |
|
259 | 283 | m_titleItem->setPlainText(title); |
|
260 | 284 | m_titleItem->setFont(font); |
|
261 | 285 | } |
|
262 | 286 | |
|
263 | 287 | int QChart::margin() const |
|
264 | 288 | { |
|
265 | 289 | return m_marginSize; |
|
266 | 290 | } |
|
267 | 291 | |
|
268 | 292 | void QChart::setMargin(int margin) |
|
269 | 293 | { |
|
270 | 294 | m_marginSize = margin; |
|
271 | 295 | } |
|
272 | 296 | |
|
273 | 297 | void QChart::setTheme(QChart::ChartThemeId theme) |
|
274 | 298 | { |
|
275 | 299 | m_chartTheme->setTheme(theme); |
|
276 | 300 | setBackground(m_chartTheme->d->m_gradientStartColor, |
|
277 | 301 | m_chartTheme->d->m_gradientEndColor, |
|
278 | 302 | m_bacgroundOrinetation); |
|
279 | 303 | |
|
280 | 304 | // TODO: Move the controlling of the series presentations into private implementation of the |
|
281 | 305 | // series instead of QChart controlling themes for each |
|
282 | 306 | // In other words, the following should be used when creating xy series: |
|
283 | 307 | // m_chartTheme->addObserver(xyseries) |
|
284 | 308 | foreach (QChartSeries *series, m_chartSeries) { |
|
285 | 309 | if (series->type() == QChartSeries::SeriesTypeLine) { |
|
286 | 310 | QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series); |
|
287 | 311 | SeriesTheme seriesTheme = m_chartTheme->themeForSeries(); |
|
288 | 312 | xyseries->setPen(seriesTheme.linePen); |
|
289 | 313 | } |
|
290 | 314 | } |
|
291 | 315 | |
|
292 | 316 | update(); |
|
293 | 317 | } |
|
294 | 318 | |
|
295 | 319 | QChart::ChartThemeId QChart::theme() |
|
296 | 320 | { |
|
297 | 321 | return (QChart::ChartThemeId) m_chartTheme->d->m_currentTheme; |
|
298 | 322 | } |
|
299 | 323 | |
|
300 | 324 | void QChart::zoomInToRect(const QRectF& rectangle) |
|
301 | 325 | { |
|
302 | 326 | |
|
303 | 327 | if(!rectangle.isValid()) return; |
|
304 | 328 | |
|
305 | 329 | qreal margin = this->margin(); |
|
306 | 330 | |
|
307 | 331 | QRectF rect = rectangle.normalized(); |
|
308 | 332 | rect.translate(-margin, -margin); |
|
309 | 333 | |
|
310 | 334 | PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex]; |
|
311 | 335 | |
|
312 | 336 | PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin); |
|
313 | 337 | |
|
314 | 338 | m_plotDomainList.resize(m_plotDataIndex + 1); |
|
315 | 339 | m_plotDomainList<<domain; |
|
316 | 340 | m_plotDataIndex++; |
|
317 | 341 | |
|
318 | 342 | foreach (ChartItem* item, m_chartItems) |
|
319 | 343 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
320 | 344 | update(); |
|
321 | 345 | } |
|
322 | 346 | |
|
323 | 347 | void QChart::zoomIn() |
|
324 | 348 | { |
|
325 | 349 | if (m_plotDataIndex < m_plotDomainList.count() - 1) { |
|
326 | 350 | m_plotDataIndex++; |
|
327 | 351 | foreach (ChartItem* item, m_chartItems) |
|
328 | 352 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
329 | 353 | update(); |
|
330 | 354 | } else { |
|
331 | 355 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
332 | 356 | rect.setWidth(rect.width()/2); |
|
333 | 357 | rect.setHeight(rect.height()/2); |
|
334 | 358 | rect.moveCenter(m_rect.center()); |
|
335 | 359 | zoomInToRect(rect); |
|
336 | 360 | } |
|
337 | 361 | } |
|
338 | 362 | |
|
339 | 363 | void QChart::zoomOut() |
|
340 | 364 | { |
|
341 | 365 | if (m_plotDataIndex > 0) { |
|
342 | 366 | m_plotDataIndex--; |
|
343 | 367 | foreach (ChartItem* item, m_chartItems) |
|
344 | 368 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
345 | 369 | update(); |
|
346 | 370 | } |
|
347 | 371 | } |
|
348 | 372 | |
|
349 | 373 | void QChart::zoomReset() |
|
350 | 374 | { |
|
351 | 375 | if (m_plotDataIndex > 0) { |
|
352 | 376 | m_plotDataIndex = 0; |
|
353 | 377 | foreach (ChartItem* item, m_chartItems) |
|
354 | 378 | item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); |
|
355 | 379 | update(); |
|
356 | 380 | } |
|
357 | 381 | } |
|
358 | 382 | |
|
359 | 383 | void QChart::setAxisX(const QChartAxis& axis) |
|
360 | 384 | { |
|
361 | 385 | setAxis(m_axisXItem,axis); |
|
362 | 386 | } |
|
363 | 387 | void QChart::setAxisY(const QChartAxis& axis) |
|
364 | 388 | { |
|
365 | 389 | setAxis(m_axisYItem.at(0),axis); |
|
366 | 390 | } |
|
367 | 391 | |
|
368 | 392 | void QChart::setAxisY(const QList<QChartAxis>& axis) |
|
369 | 393 | { |
|
370 | 394 | //TODO not implemented |
|
371 | 395 | } |
|
372 | 396 | |
|
373 | 397 | void QChart::setAxis(AxisItem *item, const QChartAxis& axis) |
|
374 | 398 | { |
|
375 | 399 | item->setVisible(axis.isAxisVisible()); |
|
376 | 400 | } |
|
377 | 401 | |
|
378 | 402 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
379 | 403 | { |
|
380 | 404 | |
|
381 | 405 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
382 | 406 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
383 | 407 | |
|
384 | 408 | // recalculate title position |
|
385 | 409 | if (m_titleItem) { |
|
386 | 410 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
387 | 411 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
388 | 412 | } |
|
389 | 413 | |
|
390 | 414 | //recalculate background gradient |
|
391 | 415 | if (m_backgroundItem) { |
|
392 | 416 | m_backgroundItem->setRect(rect); |
|
393 | if (m_bacgroundOrinetation == HorizonatlGradientOrientation) | |
|
394 | m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0); | |
|
395 | else | |
|
396 | m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height()); | |
|
397 | m_backgroundItem->setBrush(m_backgroundGradient); | |
|
398 | 417 | } |
|
399 | 418 | |
|
400 | 419 | // resize and reposition childs |
|
401 | 420 | foreach (ChartItem *item, m_chartItems) { |
|
402 | 421 | item->setPos(rect.topLeft()); |
|
403 | 422 | item->setSize(rect.size()); |
|
404 | 423 | } |
|
405 | 424 | |
|
406 | 425 | QGraphicsWidget::resizeEvent(event); |
|
407 | 426 | update(); |
|
408 | 427 | } |
|
409 | 428 | |
|
410 | 429 | |
|
411 | 430 | |
|
412 | 431 | #include "moc_qchart.cpp" |
|
413 | 432 | |
|
414 | 433 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,100 +1,103 | |||
|
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 <QGraphicsWidget> |
|
7 | 7 | #include <QLinearGradient> |
|
8 | 8 | #include <QFont> |
|
9 | 9 | |
|
10 | 10 | class QGraphicsSceneResizeEvent; |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | class AxisItem; |
|
15 | 15 | class QChartSeries; |
|
16 | 16 | class PlotDomain; |
|
17 | 17 | class BarGroup; |
|
18 | 18 | class QChartAxis; |
|
19 | 19 | class ChartTheme; |
|
20 | 20 | class ChartItem; |
|
21 | 21 | |
|
22 | 22 | // TODO: We don't need to have QChart tied to QGraphicsItem: |
|
23 | 23 | //class QTCOMMERCIALCHART_EXPORT QChart |
|
24 | 24 | //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem { |
|
25 | 25 | // public: QChartGraphicsItem(QChart &chart); |
|
26 | 26 | |
|
27 | 27 | /*! |
|
28 | 28 | * TODO: define the responsibilities |
|
29 | 29 | */ |
|
30 | 30 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget |
|
31 | 31 | { |
|
32 | 32 | Q_OBJECT |
|
33 | 33 | public: |
|
34 | 34 | enum GradientOrientation { |
|
35 | 35 | HorizonatlGradientOrientation, |
|
36 | 36 | VerticalGradientOrientation |
|
37 | 37 | }; |
|
38 | 38 | enum ChartThemeId { |
|
39 | 39 | ChartThemeInvalid = -1, |
|
40 | 40 | /*! The default theme follows the GUI style of the Operating System */ |
|
41 | 41 | ChartThemeDefault, |
|
42 | 42 | ChartThemeVanilla, |
|
43 | 43 | ChartThemeIcy, |
|
44 | 44 | ChartThemeGrayscale, |
|
45 | 45 | //ChartThemeScientific, |
|
46 | 46 | ChartThemeUnnamed1 |
|
47 | 47 | }; |
|
48 | 48 | |
|
49 | 49 | public: |
|
50 | 50 | QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); |
|
51 | 51 | ~QChart(); |
|
52 | 52 | |
|
53 | 53 | void addSeries(QChartSeries* series); |
|
54 | 54 | //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type); |
|
55 | 55 | // TODO: who owns the series now? maybe owned by chart and returned a reference instead... |
|
56 | 56 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
57 | 57 | |
|
58 | 58 | void setMargin(int margin); |
|
59 | 59 | int margin() const; |
|
60 | 60 | void setTheme(QChart::ChartThemeId theme); |
|
61 | 61 | QChart::ChartThemeId theme(); |
|
62 | 62 | |
|
63 | 63 | void setTitle(const QString& title,const QFont& font = QFont()); |
|
64 | 64 | void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation); |
|
65 | 65 | |
|
66 | void setChartBackgroundBrush(const QBrush& brush); | |
|
67 | void setChartBackgroundPen(const QPen& pen); | |
|
68 | ||
|
66 | 69 | void zoomInToRect(const QRectF& rectangle); |
|
67 | 70 | void zoomIn(); |
|
68 | 71 | void zoomOut(); |
|
69 | 72 | void zoomReset(); |
|
70 | 73 | |
|
71 | 74 | void setAxisX(const QChartAxis& axis); |
|
72 | 75 | void setAxisY(const QChartAxis& axis); |
|
73 | 76 | void setAxisY(const QList<QChartAxis>& axis); |
|
74 | 77 | |
|
75 | 78 | protected: |
|
76 | 79 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
77 | 80 | |
|
78 | 81 | private: |
|
79 | 82 | void setAxis(AxisItem *item, const QChartAxis& axis); |
|
80 | 83 | |
|
81 | 84 | private: |
|
82 | 85 | Q_DISABLE_COPY(QChart) |
|
83 | 86 | QGraphicsRectItem* m_backgroundItem; |
|
84 | 87 | QLinearGradient m_backgroundGradient; |
|
85 | 88 | GradientOrientation m_bacgroundOrinetation; |
|
86 | 89 | QGraphicsTextItem* m_titleItem; |
|
87 | 90 | AxisItem* m_axisXItem; |
|
88 | 91 | QList<AxisItem*> m_axisYItem; |
|
89 | 92 | QRectF m_rect; |
|
90 | 93 | QList<QChartSeries *> m_chartSeries; |
|
91 | 94 | QList<ChartItem *> m_chartItems; |
|
92 | 95 | QVector<PlotDomain> m_plotDomainList; |
|
93 | 96 | int m_plotDataIndex; |
|
94 | 97 | int m_marginSize; |
|
95 | 98 | ChartTheme *m_chartTheme; |
|
96 | 99 | }; |
|
97 | 100 | |
|
98 | 101 | QTCOMMERCIALCHART_END_NAMESPACE |
|
99 | 102 | |
|
100 | 103 | #endif |
@@ -1,77 +1,85 | |||
|
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->resize(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 | 66 | |
|
67 | 67 | void QChartView::setTitle(const QString& title) |
|
68 | 68 | { |
|
69 | 69 | m_chart->setTitle(title); |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | void QChartView::setBackground(const QColor& startColor, const QColor& endColor, QChart::GradientOrientation orientation) |
|
73 | 73 | { |
|
74 | 74 | m_chart->setBackground(startColor,endColor,orientation); |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | void QChartView::setChartBackgroundBrush(const QBrush& brush) | |
|
78 | { | |
|
79 | m_chart->setChartBackgroundBrush(brush); | |
|
80 | } | |
|
81 | void QChartView::setChartBackgroundPen(const QPen& pen) | |
|
82 | { | |
|
83 | m_chart->setChartBackgroundPen(pen); | |
|
84 | } | |
|
77 | 85 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,46 +1,53 | |||
|
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 "qchart.h" |
|
7 | 7 | #include <QGraphicsView> |
|
8 | 8 | |
|
9 | 9 | class QGraphicsScene; |
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 12 | |
|
13 | 13 | class QChart; |
|
14 | 14 | |
|
15 | 15 | class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView |
|
16 | 16 | { |
|
17 | 17 | public: |
|
18 | 18 | explicit QChartView(QWidget *parent = 0); |
|
19 | 19 | ~QChartView(); |
|
20 | 20 | |
|
21 | 21 | //implement from QWidget |
|
22 | 22 | void resizeEvent(QResizeEvent *event); |
|
23 | 23 | |
|
24 | 24 | void addSeries(QChartSeries* series); |
|
25 | 25 | // Convenience function |
|
26 | 26 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
27 | 27 | |
|
28 | 28 | int margin() const; |
|
29 | 29 | void setTitle(const QString& title); |
|
30 | ||
|
31 | //Obsolete | |
|
30 | 32 | void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, QChart::GradientOrientation orientation = QChart::VerticalGradientOrientation); |
|
33 | ||
|
34 | ||
|
35 | void setChartBackgroundBrush(const QBrush& brush); | |
|
36 | void setChartBackgroundPen(const QPen& pen); | |
|
37 | ||
|
31 | 38 | void zoomInToRect(const QRect& rectangle); |
|
32 | 39 | void zoomIn(); |
|
33 | 40 | void zoomOut(); |
|
34 | 41 | |
|
35 | 42 | private: |
|
36 | 43 | QGraphicsScene *m_scene; |
|
37 | 44 | QChart* m_chart; |
|
38 | 45 | QPoint m_origin; |
|
39 | 46 | Q_DISABLE_COPY(QChartView) |
|
40 | 47 | |
|
41 | 48 | |
|
42 | 49 | }; |
|
43 | 50 | |
|
44 | 51 | QTCOMMERCIALCHART_END_NAMESPACE |
|
45 | 52 | |
|
46 | 53 | #endif // QCHARTWIDGET_H |
General Comments 0
You need to be logged in to leave comments.
Login now