@@ -1,153 +1,149 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartglobal.h> |
|
4 | 4 | #include <qchartview.h> |
|
5 | 5 | #include <qstackedbarseries.h> |
|
6 | 6 | #include <qbarset.h> |
|
7 | 7 | #include <qchartaxis.h> |
|
8 | 8 | #include <qlegend.h> |
|
9 | 9 | #include <QStringList> |
|
10 | 10 | #include <QDebug> |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | //! [1] |
|
15 | 15 | class DrilldownBarSeries : public QStackedBarSeries |
|
16 | 16 | { |
|
17 | 17 | Q_OBJECT |
|
18 | 18 | public: |
|
19 | 19 | DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {} |
|
20 | 20 | |
|
21 | 21 | void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries) |
|
22 | 22 | { |
|
23 | 23 | mDrilldownSeries[category] = drilldownSeries; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | DrilldownBarSeries* drilldownSeries(QString category) |
|
27 | 27 | { |
|
28 | 28 | return mDrilldownSeries[category]; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | public Q_SLOTS: |
|
32 | 32 | |
|
33 | 33 | private: |
|
34 | 34 | |
|
35 | 35 | QMap<QString, DrilldownBarSeries*> mDrilldownSeries; |
|
36 | 36 | }; |
|
37 | 37 | //! [1] |
|
38 | 38 | |
|
39 | 39 | //! [2] |
|
40 | 40 | class DrilldownChart : public QChartView |
|
41 | 41 | { |
|
42 | 42 | Q_OBJECT |
|
43 | 43 | public: |
|
44 | 44 | explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {} |
|
45 | 45 | |
|
46 | 46 | void changeSeries(QSeries* series) |
|
47 | 47 | { |
|
48 | 48 | if (m_currentSeries) |
|
49 | 49 | removeSeries(m_currentSeries); |
|
50 | 50 | m_currentSeries = series; |
|
51 | 51 | addSeries(series); |
|
52 | 52 | setChartTitle(series->title()); |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | public Q_SLOTS: |
|
56 | 56 | void handleRightClick(QBarSet *barset, QString category) |
|
57 | 57 | { |
|
58 | // qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category; | |
|
59 | 58 | DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender()); |
|
60 | 59 | changeSeries(series->drilldownSeries(category)); |
|
61 | 60 | } |
|
62 | 61 | |
|
63 | 62 | private: |
|
64 | 63 | QSeries* m_currentSeries; |
|
65 | 64 | }; |
|
66 | 65 | //! [2] |
|
67 | 66 | |
|
68 | 67 | int main(int argc, char *argv[]) |
|
69 | 68 | { |
|
70 | 69 | QApplication a(argc, argv); |
|
71 | 70 | QMainWindow window; |
|
72 | 71 | |
|
73 | 72 | DrilldownChart* drilldownChart = new DrilldownChart(&window); |
|
74 | 73 | drilldownChart->setChartTheme(QChart::ChartThemeIcy); |
|
75 | 74 | |
|
76 | 75 | //! [3] |
|
77 | 76 | // Define categories |
|
78 | 77 | QStringList months; |
|
79 | 78 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; |
|
80 | 79 | QStringList weeks; |
|
81 | 80 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; |
|
82 | 81 | QStringList plants; |
|
83 | 82 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; |
|
84 | 83 | //! [3] |
|
85 | 84 | |
|
86 | 85 | //! [4] |
|
87 | 86 | // Create drilldown structure |
|
88 | 87 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); |
|
89 | 88 | seasonSeries->setTitle("Crop by month - Season"); |
|
90 | 89 | |
|
91 | 90 | // Each month in season series has drilldown series for weekly data |
|
92 | 91 | foreach (QString month, months) { |
|
93 | 92 | |
|
94 | 93 | // Create drilldown series for every week |
|
95 | 94 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); |
|
96 | 95 | seasonSeries->mapDrilldownSeries(month, weeklySeries); |
|
97 | 96 | |
|
98 | 97 | // Drilling down from weekly data brings us back to season data. |
|
99 | 98 | foreach (QString week, weeks) { |
|
100 | 99 | weeklySeries->mapDrilldownSeries(week, seasonSeries); |
|
101 | 100 | weeklySeries->setTitle(QString("Crop by week - " + month)); |
|
102 | 101 | } |
|
103 | 102 | |
|
104 | 103 | // Use right click signal to implement drilldown |
|
105 | 104 | QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); |
|
106 | 105 | } |
|
107 | 106 | |
|
108 | 107 | // Enable drilldown from season series using right click. |
|
109 | 108 | QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); |
|
110 | 109 | //! [4] |
|
111 | 110 | |
|
112 | 111 | //! [5] |
|
113 | 112 | // Fill monthly and weekly series with data |
|
114 | 113 | foreach (QString plant, plants) { |
|
115 | 114 | QBarSet* monthlyCrop = new QBarSet(plant); |
|
116 | 115 | foreach (QString month, months) { |
|
117 | 116 | QBarSet* weeklyCrop = new QBarSet(plant); |
|
118 | 117 | foreach (QString week, weeks ) { |
|
119 | 118 | *weeklyCrop << (qrand() % 20); |
|
120 | 119 | } |
|
121 | 120 | // Get the drilldown series from season series and add crop to it. |
|
122 | 121 | seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop); |
|
123 | 122 | seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); |
|
124 | 123 | *monthlyCrop << weeklyCrop->total(); |
|
125 | 124 | |
|
126 | 125 | QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); |
|
127 | 126 | } |
|
128 | 127 | seasonSeries->addBarSet(monthlyCrop); |
|
129 | 128 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); |
|
130 | 129 | } |
|
131 | 130 | //! [5] |
|
132 | 131 | |
|
133 | 132 | seasonSeries->setToolTipEnabled(true); |
|
134 | 133 | |
|
135 | 134 | //! [6] |
|
136 | 135 | // Show season series in initial view |
|
137 | 136 | drilldownChart->changeSeries(seasonSeries); |
|
138 | 137 | drilldownChart->setChartTitle(seasonSeries->title()); |
|
139 | 138 | //! [6] |
|
140 | 139 | |
|
141 | 140 | drilldownChart->axisX()->setGridLineVisible(false); |
|
142 | 141 | |
|
143 | QLegend* l = drilldownChart->legend(); | |
|
144 | l->handleGeometryChanged(QRectF(50,270,300,20)); | |
|
145 | ||
|
146 | 142 | window.setCentralWidget(drilldownChart); |
|
147 | 143 | window.resize(400, 300); |
|
148 | 144 | window.show(); |
|
149 | 145 | |
|
150 | 146 | return a.exec(); |
|
151 | 147 | } |
|
152 | 148 | |
|
153 | 149 | #include "main.moc" |
@@ -1,344 +1,355 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | #include "qlegend.h" |
|
4 | 4 | #include "chartpresenter_p.h" |
|
5 | 5 | #include "chartdataset_p.h" |
|
6 | 6 | #include <QGraphicsScene> |
|
7 | 7 | #include <QGraphicsSceneResizeEvent> |
|
8 | 8 | #include <QDebug> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | /*! |
|
13 | 13 | \enum QChart::ChartTheme |
|
14 | 14 | |
|
15 | 15 | This enum describes the theme used by the chart. |
|
16 | 16 | |
|
17 | 17 | \value ChartThemeDefault Follows the GUI style of the Operating System |
|
18 | 18 | \value ChartThemeVanilla |
|
19 | 19 | \value ChartThemeIcy |
|
20 | 20 | \value ChartThemeGrayscale |
|
21 | 21 | \value ChartThemeScientific |
|
22 | 22 | \value ChartThemeBlueCerulean |
|
23 | 23 | \value ChartThemeCount Not really a theme; the total count of themes. |
|
24 | 24 | */ |
|
25 | 25 | |
|
26 | 26 | /*! |
|
27 | 27 | \enum QChart::AnimationOption |
|
28 | 28 | |
|
29 | 29 | For enabling/disabling animations. Defaults to NoAnimation. |
|
30 | 30 | |
|
31 | 31 | \value NoAnimation |
|
32 | 32 | \value GridAxisAnimations |
|
33 | 33 | \value SeriesAnimations |
|
34 | 34 | \value AllAnimations |
|
35 | 35 | */ |
|
36 | 36 | |
|
37 | 37 | /*! |
|
38 | 38 | \class QChart |
|
39 | 39 | \brief QtCommercial chart API. |
|
40 | 40 | |
|
41 | 41 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
42 | 42 | representation of different types of QChartSeries and other chart related objects like |
|
43 | 43 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
44 | 44 | convenience class QChartView instead of QChart. |
|
45 | 45 | \sa QChartView |
|
46 | 46 | */ |
|
47 | 47 | |
|
48 | 48 | /*! |
|
49 | 49 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. |
|
50 | 50 | */ |
|
51 | 51 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
52 | 52 | m_backgroundItem(0), |
|
53 | 53 | m_titleItem(0), |
|
54 | 54 | m_legend(new QLegend(this)), |
|
55 | 55 | m_dataset(new ChartDataSet(this)), |
|
56 | 56 | m_presenter(new ChartPresenter(this,m_dataset)) |
|
57 | 57 | { |
|
58 | 58 | connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
59 | 59 | connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | /*! |
|
63 | 63 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
64 | 64 | */ |
|
65 | 65 | QChart::~QChart() |
|
66 | 66 | { |
|
67 | 67 | disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
68 | 68 | disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. |
|
73 | 73 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
74 | 74 | the y axis). |
|
75 | 75 | */ |
|
76 | 76 | void QChart::addSeries(QSeries* series, QChartAxis* axisY) |
|
77 | 77 | { |
|
78 | 78 | m_dataset->addSeries(series, axisY); |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | /*! |
|
82 | 82 | Removes the \a series specified in a perameter from the QChartView. |
|
83 | 83 | It releses its ownership of the specified QChartSeries object. |
|
84 | 84 | It does not delete the pointed QChartSeries data object |
|
85 | 85 | \sa addSeries(), removeAllSeries() |
|
86 | 86 | */ |
|
87 | 87 | void QChart::removeSeries(QSeries* series) |
|
88 | 88 | { |
|
89 | 89 | m_dataset->removeSeries(series); |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | /*! |
|
93 | 93 | Removes all the QChartSeries that have been added to the QChartView |
|
94 | 94 | It also deletes the pointed QChartSeries data objects |
|
95 | 95 | \sa addSeries(), removeSeries() |
|
96 | 96 | */ |
|
97 | 97 | void QChart::removeAllSeries() |
|
98 | 98 | { |
|
99 | 99 | m_dataset->removeAllSeries(); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | /*! |
|
103 | 103 | Sets the \a brush that is used for painting the background of the chart area. |
|
104 | 104 | */ |
|
105 | 105 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
106 | 106 | { |
|
107 | 107 | createChartBackgroundItem(); |
|
108 | 108 | m_backgroundItem->setBrush(brush); |
|
109 | 109 | m_backgroundItem->update(); |
|
110 | 110 | } |
|
111 | 111 | |
|
112 | 112 | /*! |
|
113 | 113 | Sets the \a pen that is used for painting the background of the chart area. |
|
114 | 114 | */ |
|
115 | 115 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
116 | 116 | { |
|
117 | 117 | createChartBackgroundItem(); |
|
118 | 118 | m_backgroundItem->setPen(pen); |
|
119 | 119 | m_backgroundItem->update(); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | /*! |
|
123 | 123 | Sets the chart \a title. The description text that is drawn above the chart. |
|
124 | 124 | */ |
|
125 | 125 | void QChart::setChartTitle(const QString& title) |
|
126 | 126 | { |
|
127 | 127 | createChartTitleItem(); |
|
128 | 128 | m_titleItem->setText(title); |
|
129 | 129 | updateLayout(); |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | /*! |
|
133 | 133 | Returns the chart title. The description text that is drawn above the chart. |
|
134 | 134 | */ |
|
135 | 135 | QString QChart::chartTitle() const |
|
136 | 136 | { |
|
137 | 137 | if(m_titleItem) |
|
138 | 138 | return m_titleItem->text(); |
|
139 | 139 | else |
|
140 | 140 | return QString(); |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | /*! |
|
144 | 144 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
145 | 145 | */ |
|
146 | 146 | void QChart::setChartTitleFont(const QFont& font) |
|
147 | 147 | { |
|
148 | 148 | createChartTitleItem(); |
|
149 | 149 | m_titleItem->setFont(font); |
|
150 | 150 | updateLayout(); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | /*! |
|
154 | 154 | Sets the \a brush used for rendering the title text. |
|
155 | 155 | */ |
|
156 | 156 | void QChart::setChartTitleBrush(const QBrush &brush) |
|
157 | 157 | { |
|
158 | 158 | createChartTitleItem(); |
|
159 | 159 | m_titleItem->setBrush(brush); |
|
160 | 160 | updateLayout(); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | /*! |
|
164 | 164 | Returns the brush used for rendering the title text. |
|
165 | 165 | */ |
|
166 | 166 | QBrush QChart::chartTitleBrush() |
|
167 | 167 | { |
|
168 | 168 | createChartTitleItem(); |
|
169 | 169 | return m_titleItem->brush(); |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | void QChart::createChartBackgroundItem() |
|
173 | 173 | { |
|
174 | 174 | if(!m_backgroundItem) { |
|
175 | 175 | m_backgroundItem = new QGraphicsRectItem(this); |
|
176 | 176 | m_backgroundItem->setPen(Qt::NoPen); |
|
177 | 177 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
178 | 178 | } |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | void QChart::createChartTitleItem() |
|
182 | 182 | { |
|
183 | 183 | if(!m_titleItem) { |
|
184 | 184 | m_titleItem = new QGraphicsSimpleTextItem(this); |
|
185 | 185 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
186 | 186 | } |
|
187 | 187 | } |
|
188 | 188 | |
|
189 | 189 | /*! |
|
190 | 190 | Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. |
|
191 | 191 | \sa setMargin() |
|
192 | 192 | */ |
|
193 | 193 | int QChart::margin() const |
|
194 | 194 | { |
|
195 | 195 | return m_presenter->margin(); |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | /*! |
|
199 | 199 | Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. |
|
200 | 200 | \sa margin() |
|
201 | 201 | */ |
|
202 | 202 | void QChart::setMargin(int margin) |
|
203 | 203 | { |
|
204 | 204 | m_presenter->setMargin(margin); |
|
205 | 205 | updateLayout(); |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | /*! |
|
209 | 209 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
210 | 210 | \sa ChartTheme, chartTheme() |
|
211 | 211 | */ |
|
212 | 212 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
213 | 213 | { |
|
214 | 214 | m_presenter->setChartTheme(theme); |
|
215 | 215 | } |
|
216 | 216 | |
|
217 | 217 | /*! |
|
218 | 218 | Returns the theme enum used by the chart. |
|
219 | 219 | \sa ChartTheme, setChartTheme() |
|
220 | 220 | */ |
|
221 | 221 | QChart::ChartTheme QChart::chartTheme() const |
|
222 | 222 | { |
|
223 | 223 | return m_presenter->chartTheme(); |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | /*! |
|
227 | 227 | Zooms in the view by a factor of 2 |
|
228 | 228 | */ |
|
229 | 229 | void QChart::zoomIn() |
|
230 | 230 | { |
|
231 | 231 | m_presenter->zoomIn(); |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | /*! |
|
235 | 235 | Zooms in the view to a maximum level at which \a rect is still fully visible. |
|
236 | 236 | */ |
|
237 | 237 | void QChart::zoomIn(const QRectF& rect) |
|
238 | 238 | { |
|
239 | 239 | |
|
240 | 240 | if(!rect.isValid()) return; |
|
241 | 241 | m_presenter->zoomIn(rect); |
|
242 | 242 | } |
|
243 | 243 | |
|
244 | 244 | /*! |
|
245 | 245 | Restores the view zoom level to the previous one. |
|
246 | 246 | */ |
|
247 | 247 | void QChart::zoomOut() |
|
248 | 248 | { |
|
249 | 249 | m_presenter->zoomOut(); |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | /*! |
|
253 | 253 | Resets to the default view. |
|
254 | 254 | */ |
|
255 | 255 | void QChart::zoomReset() |
|
256 | 256 | { |
|
257 | 257 | m_presenter->zoomReset(); |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | /*! |
|
261 | 261 | Returns the pointer to the x axis object of the chart |
|
262 | 262 | */ |
|
263 | 263 | QChartAxis* QChart::axisX() const |
|
264 | 264 | { |
|
265 | 265 | return m_dataset->axisX(); |
|
266 | 266 | } |
|
267 | 267 | |
|
268 | 268 | /*! |
|
269 | 269 | Returns the pointer to the y axis object of the chart |
|
270 | 270 | */ |
|
271 | 271 | QChartAxis* QChart::axisY() const |
|
272 | 272 | { |
|
273 | 273 | return m_dataset->axisY(); |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | /*! |
|
277 | 277 | Returns the legend object of the chart |
|
278 | 278 | */ |
|
279 | 279 | QLegend* QChart::legend() |
|
280 | 280 | { |
|
281 | 281 | return m_legend; |
|
282 | 282 | } |
|
283 | 283 | |
|
284 | 284 | /*! |
|
285 | 285 | Resizes and updates the chart area using the \a event data |
|
286 | 286 | */ |
|
287 | 287 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
288 | 288 | { |
|
289 | 289 | |
|
290 | 290 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
291 | 291 | updateLayout(); |
|
292 | 292 | QGraphicsWidget::resizeEvent(event); |
|
293 | 293 | update(); |
|
294 | 294 | } |
|
295 | 295 | |
|
296 | 296 | /*! |
|
297 | 297 | Sets animation \a options for the chart |
|
298 | 298 | */ |
|
299 | 299 | void QChart::setAnimationOptions(AnimationOptions options) |
|
300 | 300 | { |
|
301 | 301 | m_presenter->setAnimationOptions(options); |
|
302 | 302 | } |
|
303 | 303 | |
|
304 | 304 | /*! |
|
305 | 305 | Returns animation options for the chart |
|
306 | 306 | */ |
|
307 | 307 | QChart::AnimationOptions QChart::animationOptions() const |
|
308 | 308 | { |
|
309 | 309 | return m_presenter->animationOptions(); |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | void QChart::scroll(int dx,int dy) |
|
313 | 313 | { |
|
314 | 314 | //temporary |
|
315 | 315 | if(dx>0) |
|
316 | 316 | m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); |
|
317 | 317 | if(dx<0) |
|
318 | 318 | m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); |
|
319 | 319 | if(dy>0) |
|
320 | 320 | m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1)); |
|
321 | 321 | if(dy<0) |
|
322 | 322 | m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1)); |
|
323 | 323 | } |
|
324 | 324 | |
|
325 | 325 | void QChart::updateLayout() |
|
326 | 326 | { |
|
327 | 327 | if(!m_rect.isValid()) return; |
|
328 | 328 | |
|
329 | 329 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
330 | 330 | |
|
331 | 331 | // recalculate title position |
|
332 | 332 | if (m_titleItem) { |
|
333 | 333 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
334 | 334 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
335 | 335 | } |
|
336 | 336 | |
|
337 | 337 | //recalculate background gradient |
|
338 | 338 | if (m_backgroundItem) { |
|
339 | 339 | m_backgroundItem->setRect(rect); |
|
340 | 340 | } |
|
341 | ||
|
342 | // recalculate legend position | |
|
343 | // TODO: better layout | |
|
344 | if (m_legend) { | |
|
345 | QRectF boundingRect(m_rect.adjusted(margin(), | |
|
346 | rect.height() + margin() + margin()/2 - m_legend->minimumSize().height()/2, | |
|
347 | -margin(), | |
|
348 | -margin()/2 + m_legend->minimumSize().height()/2)); | |
|
349 | m_legend->handleGeometryChanged(boundingRect); | |
|
350 | qDebug() << "legend rect:" << m_legend->boundingRect(); | |
|
351 | } | |
|
341 | 352 | } |
|
342 | 353 | #include "moc_qchart.cpp" |
|
343 | 354 | |
|
344 | 355 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,221 +1,232 | |||
|
1 | 1 | #include "qchartglobal.h" |
|
2 | 2 | #include "qlegend.h" |
|
3 | 3 | #include "qseries.h" |
|
4 | 4 | #include "legendmarker_p.h" |
|
5 | 5 | #include "qxyseries.h" |
|
6 | 6 | #include "qlineseries.h" |
|
7 | 7 | #include "qareaseries.h" |
|
8 | 8 | #include "qscatterseries.h" |
|
9 | 9 | #include "qsplineseries.h" |
|
10 | 10 | #include "qbarseries.h" |
|
11 | 11 | #include "qstackedbarseries.h" |
|
12 | 12 | #include "qpercentbarseries.h" |
|
13 | 13 | #include "qbarset.h" |
|
14 | 14 | #include "qpieseries.h" |
|
15 | 15 | #include "qpieslice.h" |
|
16 | 16 | #include <QPainter> |
|
17 | 17 | #include <QPen> |
|
18 | 18 | |
|
19 | 19 | #include <QGraphicsSceneEvent> |
|
20 | 20 | |
|
21 | 21 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
22 | 22 | |
|
23 | 23 | QLegend::QLegend(QGraphicsItem *parent) |
|
24 | 24 | : QGraphicsObject(parent) |
|
25 | 25 | ,mBoundingRect(0,0,1,1) |
|
26 | 26 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? |
|
27 | ,mMinimumSize(50,20) // TODO: magic numbers | |
|
27 | 28 | { |
|
28 | 29 | } |
|
29 | 30 | |
|
30 | 31 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
31 | 32 | { |
|
32 | 33 | painter->setBrush(mBackgroundBrush); |
|
33 | 34 | painter->drawRect(mBoundingRect); |
|
34 | 35 | |
|
35 | 36 | foreach(LegendMarker* m, mMarkers) { |
|
36 | 37 | QRectF r = m->boundingRect(); |
|
37 | 38 | painter->setBrush(m->brush()); |
|
38 | 39 | painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name()); |
|
39 | 40 | } |
|
40 | 41 | } |
|
41 | 42 | |
|
42 | 43 | QRectF QLegend::boundingRect() const |
|
43 | 44 | { |
|
44 | 45 | return mBoundingRect; |
|
45 | 46 | } |
|
46 | 47 | |
|
47 | 48 | void QLegend::setBackgroundBrush(const QBrush& brush) |
|
48 | 49 | { |
|
49 | 50 | mBackgroundBrush = brush; |
|
50 | 51 | } |
|
51 | 52 | |
|
52 | 53 | QBrush QLegend::backgroundBrush() const |
|
53 | 54 | { |
|
54 | 55 | return mBackgroundBrush; |
|
55 | 56 | } |
|
56 | 57 | |
|
58 | QSizeF QLegend::minimumSize() const | |
|
59 | { | |
|
60 | return mMinimumSize; | |
|
61 | } | |
|
62 | ||
|
63 | void QLegend::setMinimumSize(const QSizeF size) | |
|
64 | { | |
|
65 | mMinimumSize = size; | |
|
66 | } | |
|
67 | ||
|
57 | 68 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) |
|
58 | 69 | { |
|
59 | 70 | mSeriesList.append(series); |
|
60 | 71 | |
|
61 | 72 | switch (series->type()) |
|
62 | 73 | { |
|
63 | 74 | case QSeries::SeriesTypeLine: { |
|
64 | 75 | |
|
65 | 76 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
66 | 77 | createMarker(lineSeries); |
|
67 | 78 | break; |
|
68 | 79 | } |
|
69 | 80 | case QSeries::SeriesTypeArea: { |
|
70 | 81 | |
|
71 | 82 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
72 | 83 | createMarker(areaSeries->upperSeries()); |
|
73 | 84 | if(areaSeries->lowerSeries()) |
|
74 | 85 | createMarker(areaSeries->lowerSeries()); |
|
75 | 86 | break; |
|
76 | 87 | } |
|
77 | 88 | |
|
78 | 89 | case QSeries::SeriesTypeBar: { |
|
79 | 90 | |
|
80 | 91 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
81 | 92 | createMarkers(barSeries); |
|
82 | 93 | break; |
|
83 | 94 | } |
|
84 | 95 | |
|
85 | 96 | case QSeries::SeriesTypeStackedBar: { |
|
86 | 97 | |
|
87 | 98 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
88 | 99 | createMarkers(stackedBarSeries); |
|
89 | 100 | break; |
|
90 | 101 | } |
|
91 | 102 | |
|
92 | 103 | case QSeries::SeriesTypePercentBar: { |
|
93 | 104 | |
|
94 | 105 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
95 | 106 | createMarkers(percentBarSeries); |
|
96 | 107 | break; |
|
97 | 108 | } |
|
98 | 109 | |
|
99 | 110 | case QSeries::SeriesTypeScatter: { |
|
100 | 111 | |
|
101 | 112 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
102 | 113 | createMarker(scatterSeries); |
|
103 | 114 | break; |
|
104 | 115 | } |
|
105 | 116 | |
|
106 | 117 | case QSeries::SeriesTypePie: { |
|
107 | 118 | |
|
108 | 119 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
109 | 120 | createMarkers(pieSeries); |
|
110 | 121 | break; |
|
111 | 122 | } |
|
112 | 123 | |
|
113 | 124 | case QSeries::SeriesTypeSpline: { |
|
114 | 125 | |
|
115 | 126 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
116 | 127 | createMarker(splineSeries); |
|
117 | 128 | break; |
|
118 | 129 | } |
|
119 | 130 | default: { |
|
120 | 131 | qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented."; |
|
121 | 132 | break; |
|
122 | 133 | } |
|
123 | 134 | } |
|
124 | 135 | |
|
125 | 136 | layoutChanged(); |
|
126 | 137 | } |
|
127 | 138 | |
|
128 | 139 | void QLegend::handleSeriesRemoved(QSeries* series) |
|
129 | 140 | { |
|
130 | 141 | if (series->type() == QSeries::SeriesTypeArea) |
|
131 | 142 | { |
|
132 | 143 | // This is special case. Area series has upper and lower series, which each have markers |
|
133 | 144 | QAreaSeries* s = static_cast<QAreaSeries*> (series); |
|
134 | 145 | deleteMarkers(s->upperSeries()); |
|
135 | 146 | deleteMarkers(s->lowerSeries()); |
|
136 | 147 | } else { |
|
137 | 148 | deleteMarkers(series); |
|
138 | 149 | } |
|
139 | 150 | |
|
140 | 151 | mSeriesList.removeOne(series); |
|
141 | 152 | layoutChanged(); |
|
142 | 153 | } |
|
143 | 154 | |
|
144 | 155 | void QLegend::handleGeometryChanged(const QRectF& size) |
|
145 | 156 | { |
|
146 | 157 | mBoundingRect = size; |
|
147 | 158 | layoutChanged(); |
|
148 | 159 | } |
|
149 | 160 | |
|
150 | 161 | void QLegend::deleteMarkers(QSeries *series) |
|
151 | 162 | { |
|
152 | 163 | // Search all markers that belong to given series and delete them. |
|
153 | 164 | foreach (LegendMarker *m, mMarkers) { |
|
154 | 165 | if (m->series() == series) { |
|
155 | 166 | mMarkers.removeOne(m); |
|
156 | 167 | delete m; |
|
157 | 168 | } |
|
158 | 169 | } |
|
159 | 170 | } |
|
160 | 171 | |
|
161 | 172 | void QLegend::createMarker(QXYSeries* series) |
|
162 | 173 | { |
|
163 | 174 | LegendMarker* marker = new LegendMarker(series,this); |
|
164 | 175 | marker->setName(series->name()); |
|
165 | 176 | marker->setBrush(series->brush()); |
|
166 | 177 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); |
|
167 | 178 | mMarkers.append(marker); |
|
168 | 179 | childItems().append(marker); |
|
169 | 180 | } |
|
170 | 181 | |
|
171 | 182 | void QLegend::createMarkers(QBarSeries *series) |
|
172 | 183 | { |
|
173 | 184 | foreach(QBarSet* s, series->barSets()) { |
|
174 | 185 | LegendMarker* marker = new LegendMarker(series,s,this); |
|
175 | 186 | marker->setName(s->name()); |
|
176 | 187 | marker->setBrush(s->brush()); |
|
177 | 188 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); |
|
178 | 189 | mMarkers.append(marker); |
|
179 | 190 | childItems().append(marker); |
|
180 | 191 | } |
|
181 | 192 | } |
|
182 | 193 | |
|
183 | 194 | void QLegend::createMarkers(QPieSeries *series) |
|
184 | 195 | { |
|
185 | 196 | foreach(QPieSlice* s, series->slices()) { |
|
186 | 197 | LegendMarker* marker = new LegendMarker(series,s,this); |
|
187 | 198 | marker->setName(s->label()); |
|
188 | 199 | marker->setBrush(s->sliceBrush()); |
|
189 | 200 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); |
|
190 | 201 | mMarkers.append(marker); |
|
191 | 202 | childItems().append(marker); |
|
192 | 203 | } |
|
193 | 204 | } |
|
194 | 205 | |
|
195 | 206 | void QLegend::layoutChanged() |
|
196 | 207 | { |
|
197 | 208 | // Calculate layout for markers and text |
|
198 | 209 | if (mMarkers.count() <= 0) { |
|
199 | 210 | // Nothing to do |
|
200 | 211 | return; |
|
201 | 212 | } |
|
202 | 213 | |
|
203 | 214 | // TODO: marker defined by series. |
|
204 | 215 | QSizeF markerSize(10,10); |
|
205 | 216 | |
|
206 | 217 | // TODO: better layout, this is just concept. |
|
207 | 218 | // Leave some space around markers like this: | x x x x | |
|
208 | 219 | qreal steps = mMarkers.count(); |
|
209 | 220 | |
|
210 | 221 | qreal xStep = mBoundingRect.width() / steps; |
|
211 | 222 | qreal yStep = mBoundingRect.height() / steps; |
|
212 | 223 | qreal x = mBoundingRect.x() + 5; |
|
213 | 224 | qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2; |
|
214 | 225 | foreach (LegendMarker* m, mMarkers) { |
|
215 | 226 | m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height())); |
|
216 | 227 | x += xStep; |
|
217 | 228 | } |
|
218 | 229 | } |
|
219 | 230 | |
|
220 | 231 | #include "moc_qlegend.cpp" |
|
221 | 232 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,61 +1,65 | |||
|
1 | 1 | #ifndef QLEGEND_H |
|
2 | 2 | #define QLEGEND_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qseries.h" |
|
6 | 6 | #include <QGraphicsObject> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class Domain; |
|
11 | 11 | class LegendMarker; |
|
12 | 12 | class QPieSlice; |
|
13 | 13 | class QXYSeries; |
|
14 | 14 | class QBarSet; |
|
15 | 15 | class QBarSeries; |
|
16 | 16 | class QPieSeries; |
|
17 | 17 | |
|
18 | 18 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject |
|
19 | 19 | { |
|
20 | 20 | Q_OBJECT |
|
21 | 21 | public: |
|
22 | 22 | |
|
23 | 23 | explicit QLegend(QGraphicsItem *parent = 0); |
|
24 | 24 | |
|
25 | 25 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
26 | 26 | QRectF boundingRect() const; |
|
27 | 27 | |
|
28 | 28 | void setBackgroundBrush(const QBrush& brush); |
|
29 | 29 | QBrush backgroundBrush() const; |
|
30 | 30 | |
|
31 | QSizeF minimumSize() const; | |
|
32 | void setMinimumSize(const QSizeF size); | |
|
33 | ||
|
31 | 34 | signals: |
|
32 | 35 | // for interactions. |
|
33 | 36 | void clicked(QSeries* series, Qt::MouseButton button); |
|
34 | 37 | void clicked(QBarSet* barset, Qt::MouseButton button); |
|
35 | 38 | void clicked(QPieSlice* slice, Qt::MouseButton button); |
|
36 | 39 | |
|
37 | 40 | public slots: |
|
38 | 41 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
39 | 42 | void handleSeriesRemoved(QSeries* series); |
|
40 | 43 | void handleGeometryChanged(const QRectF& size); |
|
41 | 44 | |
|
42 | 45 | private: |
|
43 | 46 | // PIMPL ---> |
|
44 | 47 | void createMarker(QXYSeries* series); |
|
45 | 48 | void createMarkers(QBarSeries* series); |
|
46 | 49 | void createMarkers(QPieSeries* series); |
|
47 | 50 | void deleteMarkers(QSeries* series); |
|
48 | 51 | void layoutChanged(); |
|
49 | 52 | // <--- PIMPL |
|
50 | 53 | |
|
51 | 54 | |
|
52 | 55 | QRectF mBoundingRect; |
|
53 | 56 | QList<QSeries*> mSeriesList; |
|
54 | 57 | QList<LegendMarker*> mMarkers; |
|
55 | 58 | |
|
56 | 59 | QBrush mBackgroundBrush; |
|
60 | QSizeF mMinimumSize; | |
|
57 | 61 | }; |
|
58 | 62 | |
|
59 | 63 | QTCOMMERCIALCHART_END_NAMESPACE |
|
60 | 64 | |
|
61 | 65 | #endif // QLEGEND_H |
@@ -1,55 +1,56 | |||
|
1 | 1 | #ifndef QSERIES_H |
|
2 | 2 | #define QSERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include <QAbstractItemModel> |
|
7 | 7 | #include <QPen> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class QTCOMMERCIALCHART_EXPORT QSeries : public QObject |
|
12 | 12 | { |
|
13 | 13 | Q_OBJECT |
|
14 | 14 | public: |
|
15 | 15 | enum QSeriesType { |
|
16 | 16 | SeriesTypeLine, |
|
17 | 17 | SeriesTypeArea, |
|
18 | 18 | SeriesTypeBar, |
|
19 | 19 | SeriesTypeStackedBar, |
|
20 | 20 | SeriesTypePercentBar, |
|
21 | 21 | SeriesTypePie, |
|
22 | 22 | SeriesTypeScatter, |
|
23 | 23 | SeriesTypeSpline |
|
24 | 24 | }; |
|
25 | 25 | |
|
26 | 26 | // Helper class to contain legend and color for it |
|
27 | 27 | // TODO: This is actually quite close to current LegendMarker.. combine them? |
|
28 | 28 | class LegendEntry { |
|
29 | 29 | public: |
|
30 | 30 | QString mName; |
|
31 | 31 | QBrush mBrush; |
|
32 | 32 | }; |
|
33 | 33 | |
|
34 | 34 | protected: |
|
35 | 35 | QSeries(QObject *parent = 0) : QObject(parent) {} |
|
36 | 36 | |
|
37 | 37 | public: |
|
38 | 38 | virtual ~QSeries() {} |
|
39 | 39 | virtual QSeriesType type() const = 0; |
|
40 | 40 | QString name() { return QString("TODO: Name QSeries"); } |
|
41 | 41 | // TODO |
|
42 | 42 | virtual bool setModel(QAbstractItemModel* /*model*/) { return false; } |
|
43 | 43 | |
|
44 | // TODO: consider this | |
|
44 | 45 | virtual QList<QSeries::LegendEntry> legendEntries() { QList<QSeries::LegendEntry> l; return l; } |
|
45 | 46 | |
|
46 | 47 | void setTitle(QString title) { m_title = title; } |
|
47 | 48 | QString title() { return m_title; } |
|
48 | 49 | |
|
49 | 50 | private: |
|
50 | 51 | QString m_title; |
|
51 | 52 | }; |
|
52 | 53 | |
|
53 | 54 | QTCOMMERCIALCHART_END_NAMESPACE |
|
54 | 55 | |
|
55 | 56 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now