@@ -1,150 +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 | #include <qlegend.h> | |
|
9 | 8 | #include <QStringList> |
|
10 | 9 | #include <QDebug> |
|
11 | 10 | |
|
12 | 11 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
13 | 12 | |
|
14 | 13 | //! [1] |
|
15 | 14 | class DrilldownBarSeries : public QStackedBarSeries |
|
16 | 15 | { |
|
17 | 16 | Q_OBJECT |
|
18 | 17 | public: |
|
19 | 18 | DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {} |
|
20 | 19 | |
|
21 | 20 | void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries) |
|
22 | 21 | { |
|
23 | 22 | mDrilldownSeries[category] = drilldownSeries; |
|
24 | 23 | } |
|
25 | 24 | |
|
26 | 25 | DrilldownBarSeries* drilldownSeries(QString category) |
|
27 | 26 | { |
|
28 | 27 | return mDrilldownSeries[category]; |
|
29 | 28 | } |
|
30 | 29 | |
|
31 | 30 | public Q_SLOTS: |
|
32 | 31 | |
|
33 | 32 | private: |
|
34 | 33 | |
|
35 | 34 | QMap<QString, DrilldownBarSeries*> mDrilldownSeries; |
|
36 | 35 | }; |
|
37 | 36 | //! [1] |
|
38 | 37 | |
|
39 | 38 | //! [2] |
|
40 | 39 | class DrilldownChart : public QChartView |
|
41 | 40 | { |
|
42 | 41 | Q_OBJECT |
|
43 | 42 | public: |
|
44 | 43 | explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {} |
|
45 | 44 | |
|
46 | 45 | void changeSeries(QSeries* series) |
|
47 | 46 | { |
|
48 | 47 | if (m_currentSeries) |
|
49 | 48 | removeSeries(m_currentSeries); |
|
50 | 49 | m_currentSeries = series; |
|
51 | 50 | addSeries(series); |
|
52 | 51 | setChartTitle(series->title()); |
|
53 | 52 | } |
|
54 | 53 | |
|
55 | 54 | public Q_SLOTS: |
|
56 | 55 | void handleRightClick(QBarSet *barset, QString category) |
|
57 | 56 | { |
|
58 | 57 | Q_UNUSED(barset) |
|
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 | 142 | window.setCentralWidget(drilldownChart); |
|
144 | 143 | window.resize(400, 300); |
|
145 | 144 | window.show(); |
|
146 | 145 | |
|
147 | 146 | return a.exec(); |
|
148 | 147 | } |
|
149 | 148 | |
|
150 | 149 | #include "main.moc" |
@@ -1,395 +1,396 | |||
|
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 "chartbackground_p.h" |
|
7 | 7 | #include <QGraphicsScene> |
|
8 | 8 | #include <QGraphicsSceneResizeEvent> |
|
9 | 9 | #include <QDebug> |
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 12 | |
|
13 | 13 | /*! |
|
14 | 14 | \enum QChart::ChartTheme |
|
15 | 15 | |
|
16 | 16 | This enum describes the theme used by the chart. |
|
17 | 17 | |
|
18 | 18 | \value ChartThemeDefault Follows the GUI style of the Operating System |
|
19 | 19 | \value ChartThemeVanilla |
|
20 | 20 | \value ChartThemeIcy |
|
21 | 21 | \value ChartThemeGrayscale |
|
22 | 22 | \value ChartThemeScientific |
|
23 | 23 | \value ChartThemeBlueCerulean |
|
24 | 24 | \value ChartThemeLight |
|
25 | 25 | \value ChartThemeCount Not really a theme; the total count of themes. |
|
26 | 26 | */ |
|
27 | 27 | |
|
28 | 28 | /*! |
|
29 | 29 | \enum QChart::AnimationOption |
|
30 | 30 | |
|
31 | 31 | For enabling/disabling animations. Defaults to NoAnimation. |
|
32 | 32 | |
|
33 | 33 | \value NoAnimation |
|
34 | 34 | \value GridAxisAnimations |
|
35 | 35 | \value SeriesAnimations |
|
36 | 36 | \value AllAnimations |
|
37 | 37 | */ |
|
38 | 38 | |
|
39 | 39 | /*! |
|
40 | 40 | \class QChart |
|
41 | 41 | \brief QtCommercial chart API. |
|
42 | 42 | |
|
43 | 43 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
44 | 44 | representation of different types of QChartSeries and other chart related objects like |
|
45 | 45 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
46 | 46 | convenience class QChartView instead of QChart. |
|
47 | 47 | \sa QChartView |
|
48 | 48 | */ |
|
49 | 49 | |
|
50 | 50 | /*! |
|
51 | 51 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. |
|
52 | 52 | */ |
|
53 | 53 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
54 | 54 | m_backgroundItem(0), |
|
55 | 55 | m_titleItem(0), |
|
56 | 56 | m_legend(new QLegend(this)), |
|
57 | 57 | m_dataset(new ChartDataSet(this)), |
|
58 | 58 | m_presenter(new ChartPresenter(this,m_dataset)), |
|
59 | 59 | m_padding(50), |
|
60 | 60 | m_backgroundPadding(10) |
|
61 | 61 | { |
|
62 | 62 | connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
63 | 63 | connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | /*! |
|
67 | 67 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
68 | 68 | */ |
|
69 | 69 | QChart::~QChart() |
|
70 | 70 | { |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | /*! |
|
74 | 74 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. |
|
75 | 75 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
76 | 76 | the y axis). |
|
77 | 77 | */ |
|
78 | 78 | void QChart::addSeries(QSeries* series, QChartAxis* axisY) |
|
79 | 79 | { |
|
80 | 80 | m_dataset->addSeries(series, axisY); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | /*! |
|
84 | 84 | Removes the \a series specified in a perameter from the QChartView. |
|
85 | 85 | It releses its ownership of the specified QChartSeries object. |
|
86 | 86 | It does not delete the pointed QChartSeries data object |
|
87 | 87 | \sa addSeries(), removeAllSeries() |
|
88 | 88 | */ |
|
89 | 89 | void QChart::removeSeries(QSeries* series) |
|
90 | 90 | { |
|
91 | 91 | m_dataset->removeSeries(series); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | /*! |
|
95 | 95 | Removes all the QChartSeries that have been added to the QChartView |
|
96 | 96 | It also deletes the pointed QChartSeries data objects |
|
97 | 97 | \sa addSeries(), removeSeries() |
|
98 | 98 | */ |
|
99 | 99 | void QChart::removeAllSeries() |
|
100 | 100 | { |
|
101 | 101 | m_dataset->removeAllSeries(); |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | /*! |
|
105 | 105 | Sets the \a brush that is used for painting the background of the chart area. |
|
106 | 106 | */ |
|
107 | 107 | void QChart::setBackgroundBrush(const QBrush& brush) |
|
108 | 108 | { |
|
109 | 109 | createChartBackgroundItem(); |
|
110 | 110 | m_backgroundItem->setBrush(brush); |
|
111 | 111 | m_backgroundItem->update(); |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | QBrush QChart::backgroundBrush() const |
|
115 | 115 | { |
|
116 | 116 | if(!m_backgroundItem) return QBrush(); |
|
117 | 117 | return m_backgroundItem->brush(); |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | /*! |
|
121 | 121 | Sets the \a pen that is used for painting the background of the chart area. |
|
122 | 122 | */ |
|
123 | 123 | void QChart::setBackgroundPen(const QPen& pen) |
|
124 | 124 | { |
|
125 | 125 | createChartBackgroundItem(); |
|
126 | 126 | m_backgroundItem->setPen(pen); |
|
127 | 127 | m_backgroundItem->update(); |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | QPen QChart::backgroundPen() const |
|
131 | 131 | { |
|
132 | 132 | if(!m_backgroundItem) return QPen(); |
|
133 | 133 | return m_backgroundItem->pen(); |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | /*! |
|
137 | 137 | Sets the chart \a title. The description text that is drawn above the chart. |
|
138 | 138 | */ |
|
139 | 139 | void QChart::setTitle(const QString& title) |
|
140 | 140 | { |
|
141 | 141 | createChartTitleItem(); |
|
142 | 142 | m_titleItem->setText(title); |
|
143 | 143 | updateLayout(); |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | /*! |
|
147 | 147 | Returns the chart title. The description text that is drawn above the chart. |
|
148 | 148 | */ |
|
149 | 149 | QString QChart::title() const |
|
150 | 150 | { |
|
151 | 151 | if(m_titleItem) |
|
152 | 152 | return m_titleItem->text(); |
|
153 | 153 | else |
|
154 | 154 | return QString(); |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | /*! |
|
158 | 158 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
159 | 159 | */ |
|
160 | 160 | void QChart::setTitleFont(const QFont& font) |
|
161 | 161 | { |
|
162 | 162 | createChartTitleItem(); |
|
163 | 163 | m_titleItem->setFont(font); |
|
164 | 164 | updateLayout(); |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | /*! |
|
168 | 168 | Sets the \a brush used for rendering the title text. |
|
169 | 169 | */ |
|
170 | 170 | void QChart::setTitleBrush(const QBrush &brush) |
|
171 | 171 | { |
|
172 | 172 | createChartTitleItem(); |
|
173 | 173 | m_titleItem->setBrush(brush); |
|
174 | 174 | updateLayout(); |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | /*! |
|
178 | 178 | Returns the brush used for rendering the title text. |
|
179 | 179 | */ |
|
180 | 180 | QBrush QChart::titleBrush() const |
|
181 | 181 | { |
|
182 | 182 | if(!m_titleItem) return QBrush(); |
|
183 | 183 | return m_titleItem->brush(); |
|
184 | 184 | } |
|
185 | 185 | |
|
186 | 186 | void QChart::createChartBackgroundItem() |
|
187 | 187 | { |
|
188 | 188 | if(!m_backgroundItem) { |
|
189 | 189 | m_backgroundItem = new ChartBackground(this); |
|
190 | 190 | m_backgroundItem->setPen(Qt::NoPen); |
|
191 | 191 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
192 | 192 | } |
|
193 | 193 | } |
|
194 | 194 | |
|
195 | 195 | void QChart::createChartTitleItem() |
|
196 | 196 | { |
|
197 | 197 | if(!m_titleItem) { |
|
198 | 198 | m_titleItem = new QGraphicsSimpleTextItem(this); |
|
199 | 199 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
200 | 200 | } |
|
201 | 201 | } |
|
202 | 202 | |
|
203 | 203 | /*! |
|
204 | 204 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
205 | 205 | \sa ChartTheme, chartTheme() |
|
206 | 206 | */ |
|
207 | 207 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
208 | 208 | { |
|
209 | 209 | m_presenter->setChartTheme(theme); |
|
210 | 210 | } |
|
211 | 211 | |
|
212 | 212 | /*! |
|
213 | 213 | Returns the theme enum used by the chart. |
|
214 | 214 | \sa ChartTheme, setChartTheme() |
|
215 | 215 | */ |
|
216 | 216 | QChart::ChartTheme QChart::chartTheme() const |
|
217 | 217 | { |
|
218 | 218 | return m_presenter->chartTheme(); |
|
219 | 219 | } |
|
220 | 220 | |
|
221 | 221 | /*! |
|
222 | 222 | Zooms in the view by a factor of 2 |
|
223 | 223 | */ |
|
224 | 224 | void QChart::zoomIn() |
|
225 | 225 | { |
|
226 | 226 | m_presenter->zoomIn(); |
|
227 | 227 | } |
|
228 | 228 | |
|
229 | 229 | /*! |
|
230 | 230 | Zooms in the view to a maximum level at which \a rect is still fully visible. |
|
231 | 231 | */ |
|
232 | 232 | void QChart::zoomIn(const QRectF& rect) |
|
233 | 233 | { |
|
234 | 234 | |
|
235 | 235 | if(!rect.isValid()) return; |
|
236 | 236 | m_presenter->zoomIn(rect); |
|
237 | 237 | } |
|
238 | 238 | |
|
239 | 239 | /*! |
|
240 | 240 | Restores the view zoom level to the previous one. |
|
241 | 241 | */ |
|
242 | 242 | void QChart::zoomOut() |
|
243 | 243 | { |
|
244 | 244 | m_presenter->zoomOut(); |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | /*! |
|
248 | 248 | Resets to the default view. |
|
249 | 249 | */ |
|
250 | 250 | void QChart::zoomReset() |
|
251 | 251 | { |
|
252 | 252 | m_presenter->zoomReset(); |
|
253 | 253 | } |
|
254 | 254 | |
|
255 | 255 | /*! |
|
256 | 256 | Returns the pointer to the x axis object of the chart |
|
257 | 257 | */ |
|
258 | 258 | QChartAxis* QChart::axisX() const |
|
259 | 259 | { |
|
260 | 260 | return m_dataset->axisX(); |
|
261 | 261 | } |
|
262 | 262 | |
|
263 | 263 | /*! |
|
264 | 264 | Returns the pointer to the y axis object of the chart |
|
265 | 265 | */ |
|
266 | 266 | QChartAxis* QChart::axisY() const |
|
267 | 267 | { |
|
268 | 268 | return m_dataset->axisY(); |
|
269 | 269 | } |
|
270 | 270 | |
|
271 | 271 | /*! |
|
272 | Returns the legend object of the chart | |
|
272 | Returns the legend object of the chart. Ownership stays in chart. | |
|
273 | 273 | */ |
|
274 | QLegend* QChart::legend() | |
|
274 | QLegend* QChart::legend() const | |
|
275 | 275 | { |
|
276 | 276 | return m_legend; |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | /*! |
|
280 | 280 | Resizes and updates the chart area using the \a event data |
|
281 | 281 | */ |
|
282 | 282 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
283 | 283 | { |
|
284 | 284 | |
|
285 | 285 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
286 | 286 | updateLayout(); |
|
287 | 287 | QGraphicsWidget::resizeEvent(event); |
|
288 | 288 | update(); |
|
289 | 289 | } |
|
290 | 290 | |
|
291 | 291 | /*! |
|
292 | 292 | Sets animation \a options for the chart |
|
293 | 293 | */ |
|
294 | 294 | void QChart::setAnimationOptions(AnimationOptions options) |
|
295 | 295 | { |
|
296 | 296 | m_presenter->setAnimationOptions(options); |
|
297 | 297 | } |
|
298 | 298 | |
|
299 | 299 | /*! |
|
300 | 300 | Returns animation options for the chart |
|
301 | 301 | */ |
|
302 | 302 | QChart::AnimationOptions QChart::animationOptions() const |
|
303 | 303 | { |
|
304 | 304 | return m_presenter->animationOptions(); |
|
305 | 305 | } |
|
306 | 306 | |
|
307 | 307 | void QChart::scrollLeft() |
|
308 | 308 | { |
|
309 | 309 | m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | void QChart::scrollRight() |
|
313 | 313 | { |
|
314 | 314 | m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); |
|
315 | 315 | } |
|
316 | 316 | void QChart::scrollUp() |
|
317 | 317 | { |
|
318 | 318 | m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1)); |
|
319 | 319 | } |
|
320 | 320 | void QChart::scrollDown() |
|
321 | 321 | { |
|
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(m_padding,m_padding, -m_padding, -m_padding); |
|
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 + m_padding/2); |
|
335 | 335 | } |
|
336 | 336 | |
|
337 | 337 | //recalculate background gradient |
|
338 | 338 | if (m_backgroundItem) { |
|
339 | 339 | m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding)); |
|
340 | 340 | } |
|
341 | 341 | |
|
342 | 342 | // recalculate legend position |
|
343 | 343 | if (m_legend) { |
|
344 | m_legend->setMaximumSize(rect.size()); | |
|
345 |
m_legend->set |
|
|
346 | m_legend->setPreferredLayout(QLegend::PreferredLayoutHorizontal); | |
|
344 | if (m_legend->parentObject() == this) { | |
|
345 | m_legend->setMaximumSize(rect.size()); | |
|
346 | m_legend->setPos(rect.topLeft()); | |
|
347 | } | |
|
347 | 348 | } |
|
348 | 349 | } |
|
349 | 350 | |
|
350 | 351 | |
|
351 | 352 | int QChart::padding() const |
|
352 | 353 | { |
|
353 | 354 | return m_padding; |
|
354 | 355 | } |
|
355 | 356 | |
|
356 | 357 | void QChart::setPadding(int padding) |
|
357 | 358 | { |
|
358 | 359 | if(m_padding==padding){ |
|
359 | 360 | m_padding = padding; |
|
360 | 361 | m_presenter->handleGeometryChanged(); |
|
361 | 362 | updateLayout(); |
|
362 | 363 | } |
|
363 | 364 | } |
|
364 | 365 | |
|
365 | 366 | void QChart::setBackgroundPadding(int padding) |
|
366 | 367 | { |
|
367 | 368 | if(m_backgroundPadding!=padding){ |
|
368 | 369 | m_backgroundPadding = padding; |
|
369 | 370 | updateLayout(); |
|
370 | 371 | } |
|
371 | 372 | } |
|
372 | 373 | |
|
373 | 374 | void QChart::setBackgroundDiameter(int diameter) |
|
374 | 375 | { |
|
375 | 376 | createChartBackgroundItem(); |
|
376 | 377 | m_backgroundItem->setDimeter(diameter); |
|
377 | 378 | m_backgroundItem->update(); |
|
378 | 379 | } |
|
379 | 380 | |
|
380 | 381 | void QChart::setBackgroundVisible(bool visible) |
|
381 | 382 | { |
|
382 | 383 | createChartBackgroundItem(); |
|
383 | 384 | m_backgroundItem->setVisible(visible); |
|
384 | 385 | } |
|
385 | 386 | |
|
386 | 387 | bool QChart::isBackgroundVisible() const |
|
387 | 388 | { |
|
388 | 389 | if(!m_backgroundItem) return false; |
|
389 | 390 | return m_backgroundItem->isVisible(); |
|
390 | 391 | } |
|
391 | 392 | |
|
392 | 393 | |
|
393 | 394 | #include "moc_qchart.cpp" |
|
394 | 395 | |
|
395 | 396 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,126 +1,124 | |||
|
1 | 1 | #ifndef QCHART_H |
|
2 | 2 | #define QCHART_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <qseries.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 QSeries; |
|
16 | 16 | class PlotDomain; |
|
17 | 17 | class BarPresenter; |
|
18 | 18 | class QChartAxis; |
|
19 | 19 | class ChartTheme; |
|
20 | 20 | class ChartItem; |
|
21 | 21 | class ChartDataSet; |
|
22 | 22 | class ChartPresenter; |
|
23 | 23 | class QLegend; |
|
24 | 24 | class ChartBackground; |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget |
|
28 | 28 | { |
|
29 | 29 | Q_OBJECT |
|
30 | 30 | public: |
|
31 | 31 | enum ChartTheme { |
|
32 | 32 | ChartThemeDefault, |
|
33 | 33 | ChartThemeLight, |
|
34 | 34 | ChartThemeBlueCerulean, |
|
35 | 35 | ChartThemeDark, |
|
36 | 36 | ChartThemeBrownSand, |
|
37 | 37 | ChartThemeBlueNcs, |
|
38 | 38 | ChartThemeVanilla, |
|
39 | 39 | ChartThemeIcy, |
|
40 | 40 | ChartThemeGrayscale, |
|
41 | 41 | ChartThemeScientific, |
|
42 | 42 | ChartThemeCount |
|
43 | 43 | }; |
|
44 | 44 | |
|
45 | 45 | enum AnimationOption { |
|
46 | 46 | NoAnimation = 0x0, |
|
47 | 47 | GridAxisAnimations = 0x1, |
|
48 | 48 | SeriesAnimations =0x2, |
|
49 | 49 | AllAnimations = 0x3 |
|
50 | 50 | }; |
|
51 | 51 | Q_DECLARE_FLAGS(AnimationOptions, AnimationOption) |
|
52 | 52 | |
|
53 | 53 | public: |
|
54 | 54 | QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); |
|
55 | 55 | ~QChart(); |
|
56 | 56 | |
|
57 | 57 | void addSeries(QSeries* series, QChartAxis* axisY = 0); |
|
58 | 58 | void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached |
|
59 | 59 | void removeAllSeries(); // deletes series and axis |
|
60 | 60 | |
|
61 | 61 | void setChartTheme(QChart::ChartTheme theme); |
|
62 | 62 | QChart::ChartTheme chartTheme() const; |
|
63 | 63 | |
|
64 | 64 | void setTitle(const QString& title); |
|
65 | 65 | QString title() const; |
|
66 | 66 | void setTitleFont(const QFont& font); |
|
67 | 67 | QFont titleFont() const; |
|
68 | 68 | void setTitleBrush(const QBrush &brush); |
|
69 | 69 | QBrush titleBrush() const; |
|
70 | 70 | void setBackgroundBrush(const QBrush& brush); |
|
71 | 71 | QBrush backgroundBrush() const; |
|
72 | 72 | void setBackgroundPen(const QPen& pen); |
|
73 | 73 | QPen backgroundPen() const; |
|
74 | 74 | |
|
75 | 75 | void setBackgroundVisible(bool visible); |
|
76 | 76 | bool isBackgroundVisible() const; |
|
77 | 77 | |
|
78 | 78 | void setAnimationOptions(AnimationOptions options); |
|
79 | 79 | AnimationOptions animationOptions() const; |
|
80 | 80 | |
|
81 | 81 | void zoomIn(); |
|
82 | 82 | void zoomIn(const QRectF& rect); |
|
83 | 83 | void zoomOut(); |
|
84 | 84 | void zoomReset(); |
|
85 | 85 | void scrollLeft(); |
|
86 | 86 | void scrollRight(); |
|
87 | 87 | void scrollUp(); |
|
88 | 88 | void scrollDown(); |
|
89 | 89 | |
|
90 | 90 | QChartAxis* axisX() const; |
|
91 | 91 | QChartAxis* axisY() const; |
|
92 | 92 | |
|
93 | // TODO: take (and give) legend instead of this. | |
|
94 | QLegend* legend(); | |
|
95 | ||
|
93 | QLegend* legend() const; | |
|
96 | 94 | |
|
97 | 95 | int padding() const; |
|
98 | 96 | |
|
99 | 97 | protected: |
|
100 | 98 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
101 | 99 | |
|
102 | 100 | private: |
|
103 | 101 | inline void createChartBackgroundItem(); |
|
104 | 102 | inline void createChartTitleItem(); |
|
105 | 103 | void setPadding(int padding); |
|
106 | 104 | void setBackgroundPadding(int padding); |
|
107 | 105 | void setBackgroundDiameter(int diameter); |
|
108 | 106 | void updateLayout(); |
|
109 | 107 | |
|
110 | 108 | private: |
|
111 | 109 | Q_DISABLE_COPY(QChart) |
|
112 | 110 | ChartBackground* m_backgroundItem; |
|
113 | 111 | QGraphicsSimpleTextItem* m_titleItem; |
|
114 | 112 | QRectF m_rect; |
|
115 | 113 | QLegend* m_legend; |
|
116 | 114 | ChartDataSet *m_dataset; |
|
117 | 115 | ChartPresenter *m_presenter; |
|
118 | 116 | int m_padding; |
|
119 | 117 | int m_backgroundPadding; |
|
120 | 118 | }; |
|
121 | 119 | |
|
122 | 120 | QTCOMMERCIALCHART_END_NAMESPACE |
|
123 | 121 | |
|
124 | 122 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions) |
|
125 | 123 | |
|
126 | 124 | #endif |
@@ -1,492 +1,491 | |||
|
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 "chartpresenter_p.h" |
|
17 | 17 | #include <QPainter> |
|
18 | 18 | #include <QPen> |
|
19 | 19 | |
|
20 | 20 | #include <QGraphicsSceneEvent> |
|
21 | 21 | |
|
22 | 22 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
23 | 23 | |
|
24 | 24 | QLegend::QLegend(QGraphicsItem *parent) |
|
25 | 25 | : QGraphicsObject(parent) |
|
26 | 26 | ,mPos(0,0) |
|
27 | 27 | ,mSize(0,0) |
|
28 | 28 | ,mMinimumSize(50,20) // TODO: magic numbers |
|
29 | 29 | ,mMaximumSize(150,100) |
|
30 | 30 | ,m_brush(Qt::darkGray) // TODO: from theme? |
|
31 | 31 | ,mPreferredLayout(QLegend::PreferredLayoutVertical) |
|
32 | 32 | { |
|
33 |
|
|
|
33 | setVisible(false); | |
|
34 | 34 | setZValue(ChartPresenter::LegendZValue); |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
38 | 38 | { |
|
39 | 39 | Q_UNUSED(option) |
|
40 | 40 | Q_UNUSED(widget) |
|
41 | 41 | |
|
42 | 42 | painter->setOpacity(0.5); |
|
43 | 43 | painter->setPen(m_pen); |
|
44 | 44 | painter->setBrush(m_brush); |
|
45 | 45 | painter->drawRect(boundingRect()); |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | QRectF QLegend::boundingRect() const |
|
49 | 49 | { |
|
50 | 50 | return QRectF(mPos,mSize); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | void QLegend::setBrush(const QBrush& brush) |
|
54 | 54 | { |
|
55 | 55 | if(m_brush!=brush){ |
|
56 | 56 | m_brush = brush; |
|
57 | 57 | update(); |
|
58 | 58 | } |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | QBrush QLegend::brush() const |
|
62 | 62 | { |
|
63 | 63 | return m_brush; |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | void QLegend::setPen(const QPen& pen) |
|
67 | 67 | { |
|
68 | 68 | if(m_pen!=pen){ |
|
69 | 69 | m_pen = pen; |
|
70 | 70 | update(); |
|
71 | 71 | } |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | QPen QLegend::pen() const |
|
75 | 75 | { |
|
76 | 76 | return m_pen; |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | void QLegend::setPreferredLayout(QLegend::PreferredLayout preferred) |
|
80 | 80 | { |
|
81 | 81 | mPreferredLayout = preferred; |
|
82 | 82 | layoutChanged(); |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | QSizeF QLegend::maximumSize() const |
|
86 | 86 | { |
|
87 | 87 | return mMaximumSize; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | void QLegend::setMaximumSize(const QSizeF size) |
|
91 | 91 | { |
|
92 | 92 | mMaximumSize = size; |
|
93 | layoutChanged(); | |
|
93 | 94 | } |
|
94 | 95 | |
|
95 | 96 | void QLegend::setSize(const QSizeF size) |
|
96 | 97 | { |
|
97 | 98 | mSize = size; |
|
98 | 99 | if (mSize.width() > mMaximumSize.width()) { |
|
99 | 100 | mSize.setWidth(mMaximumSize.width()); |
|
100 | 101 | } |
|
101 | 102 | if (mSize.height() > mMaximumSize.height()) { |
|
102 | 103 | mSize.setHeight(mMaximumSize.height()); |
|
103 | 104 | } |
|
104 | 105 | } |
|
105 | 106 | |
|
106 | 107 | void QLegend::setPos(const QPointF &pos) |
|
107 | 108 | { |
|
108 | 109 | mPos = pos; |
|
110 | layoutChanged(); | |
|
109 | 111 | } |
|
110 | 112 | |
|
111 | 113 | void QLegend::handleSeriesAdded(QSeries* series, Domain* domain) |
|
112 | 114 | { |
|
113 | 115 | Q_UNUSED(domain) |
|
114 | 116 | |
|
115 | // mSeriesList.append(series); | |
|
116 | 117 | createMarkers(series); |
|
117 | 118 | connectSeries(series); |
|
118 | 119 | layoutChanged(); |
|
119 | 120 | } |
|
120 | 121 | |
|
121 | 122 | void QLegend::handleSeriesRemoved(QSeries* series) |
|
122 | 123 | { |
|
123 | 124 | disconnectSeries(series); |
|
124 | 125 | |
|
125 | 126 | if (series->type() == QSeries::SeriesTypeArea) |
|
126 | 127 | { |
|
127 | 128 | // This is special case. Area series has upper and lower series, which each have markers |
|
128 | 129 | QAreaSeries* s = static_cast<QAreaSeries*> (series); |
|
129 | 130 | deleteMarkers(s->upperSeries()); |
|
130 | 131 | deleteMarkers(s->lowerSeries()); |
|
131 | 132 | } else { |
|
132 | 133 | deleteMarkers(series); |
|
133 | 134 | } |
|
134 | 135 | |
|
135 | // mSeriesList.removeOne(series); | |
|
136 | 136 | layoutChanged(); |
|
137 | 137 | } |
|
138 | 138 | |
|
139 | 139 | void QLegend::handleAdded(QList<QPieSlice*> slices) |
|
140 | 140 | { |
|
141 | 141 | QPieSeries* series = static_cast<QPieSeries*> (sender()); |
|
142 | 142 | foreach(QPieSlice* s, slices) { |
|
143 | 143 | LegendMarker* marker = new LegendMarker(series,s,this); |
|
144 | 144 | marker->setName(s->label()); |
|
145 | 145 | marker->setBrush(s->sliceBrush()); |
|
146 | 146 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); |
|
147 | 147 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); |
|
148 | 148 | connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater())); |
|
149 | 149 | connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed())); |
|
150 | 150 | mMarkers.append(marker); |
|
151 | 151 | childItems().append(marker); |
|
152 | 152 | } |
|
153 | 153 | layoutChanged(); |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | void QLegend::handleRemoved(QList<QPieSlice *> slices) |
|
157 | 157 | { |
|
158 | 158 | Q_UNUSED(slices) |
|
159 | 159 | // Propably no need to listen for this, since removed slices are deleted and we listen destroyed signal |
|
160 | 160 | // qDebug() << "QLegend::handleRemoved(QList<QPieSlice*> slices) count:" << slices.count(); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | |
|
164 | 164 | void QLegend::handleMarkerDestroyed() |
|
165 | 165 | { |
|
166 | 166 | // TODO: what if more than one markers are destroyed and we update layout after first one? |
|
167 | 167 | LegendMarker* m = static_cast<LegendMarker*> (sender()); |
|
168 | 168 | mMarkers.removeOne(m); |
|
169 | 169 | layoutChanged(); |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | void QLegend::connectSeries(QSeries *series) |
|
173 | 173 | { |
|
174 | 174 | // Connect relevant signals from series |
|
175 | 175 | switch (series->type()) |
|
176 | 176 | { |
|
177 | 177 | case QSeries::SeriesTypeLine: { |
|
178 | 178 | // QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
179 | 179 | break; |
|
180 | 180 | } |
|
181 | 181 | case QSeries::SeriesTypeArea: { |
|
182 | 182 | // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
183 | 183 | break; |
|
184 | 184 | } |
|
185 | 185 | case QSeries::SeriesTypeBar: { |
|
186 | 186 | // QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
187 | 187 | break; |
|
188 | 188 | } |
|
189 | 189 | case QSeries::SeriesTypeStackedBar: { |
|
190 | 190 | // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
191 | 191 | break; |
|
192 | 192 | } |
|
193 | 193 | case QSeries::SeriesTypePercentBar: { |
|
194 | 194 | // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
195 | 195 | break; |
|
196 | 196 | } |
|
197 | 197 | case QSeries::SeriesTypeScatter: { |
|
198 | 198 | // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
199 | 199 | break; |
|
200 | 200 | } |
|
201 | 201 | case QSeries::SeriesTypePie: { |
|
202 | 202 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
203 | 203 | connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); |
|
204 | 204 | // connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>))); |
|
205 | 205 | break; |
|
206 | 206 | } |
|
207 | 207 | case QSeries::SeriesTypeSpline: { |
|
208 | 208 | // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
209 | 209 | break; |
|
210 | 210 | } |
|
211 | 211 | default: { |
|
212 | 212 | qDebug()<< "QLegend::connectSeries" << series->type() << "not implemented."; |
|
213 | 213 | break; |
|
214 | 214 | } |
|
215 | 215 | } |
|
216 | 216 | } |
|
217 | 217 | |
|
218 | 218 | void QLegend::disconnectSeries(QSeries *series) |
|
219 | 219 | { |
|
220 | 220 | // Connect relevant signals from series |
|
221 | 221 | switch (series->type()) |
|
222 | 222 | { |
|
223 | 223 | case QSeries::SeriesTypeLine: { |
|
224 | 224 | // QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
225 | 225 | break; |
|
226 | 226 | } |
|
227 | 227 | case QSeries::SeriesTypeArea: { |
|
228 | 228 | // QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
229 | 229 | break; |
|
230 | 230 | } |
|
231 | 231 | case QSeries::SeriesTypeBar: { |
|
232 | 232 | // QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
233 | 233 | break; |
|
234 | 234 | } |
|
235 | 235 | case QSeries::SeriesTypeStackedBar: { |
|
236 | 236 | // QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
237 | 237 | break; |
|
238 | 238 | } |
|
239 | 239 | case QSeries::SeriesTypePercentBar: { |
|
240 | 240 | // QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
241 | 241 | break; |
|
242 | 242 | } |
|
243 | 243 | case QSeries::SeriesTypeScatter: { |
|
244 | 244 | // QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
245 | 245 | break; |
|
246 | 246 | } |
|
247 | 247 | case QSeries::SeriesTypePie: { |
|
248 | 248 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
249 | 249 | disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); |
|
250 | 250 | // disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleRemoved(QList<QPieSlice*>))); |
|
251 | 251 | break; |
|
252 | 252 | } |
|
253 | 253 | case QSeries::SeriesTypeSpline: { |
|
254 | 254 | // QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
255 | 255 | break; |
|
256 | 256 | } |
|
257 | 257 | default: { |
|
258 | 258 | qDebug()<< "QLegend::disconnectSeries" << series->type() << "not implemented."; |
|
259 | 259 | break; |
|
260 | 260 | } |
|
261 | 261 | } |
|
262 | 262 | } |
|
263 | 263 | |
|
264 | 264 | void QLegend::createMarkers(QSeries *series) |
|
265 | 265 | { |
|
266 | 266 | switch (series->type()) |
|
267 | 267 | { |
|
268 | 268 | case QSeries::SeriesTypeLine: { |
|
269 | 269 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
270 | 270 | appendMarkers(lineSeries); |
|
271 | 271 | break; |
|
272 | 272 | } |
|
273 | 273 | case QSeries::SeriesTypeArea: { |
|
274 | 274 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
275 | 275 | appendMarkers(areaSeries->upperSeries()); |
|
276 | 276 | if(areaSeries->lowerSeries()) |
|
277 | 277 | appendMarkers(areaSeries->lowerSeries()); |
|
278 | 278 | break; |
|
279 | 279 | } |
|
280 | 280 | |
|
281 | 281 | case QSeries::SeriesTypeBar: { |
|
282 | 282 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
283 | 283 | appendMarkers(barSeries); |
|
284 | 284 | break; |
|
285 | 285 | } |
|
286 | 286 | |
|
287 | 287 | case QSeries::SeriesTypeStackedBar: { |
|
288 | 288 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
289 | 289 | appendMarkers(stackedBarSeries); |
|
290 | 290 | break; |
|
291 | 291 | } |
|
292 | 292 | |
|
293 | 293 | case QSeries::SeriesTypePercentBar: { |
|
294 | 294 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
295 | 295 | appendMarkers(percentBarSeries); |
|
296 | 296 | break; |
|
297 | 297 | } |
|
298 | 298 | |
|
299 | 299 | case QSeries::SeriesTypeScatter: { |
|
300 | 300 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
301 | 301 | appendMarkers(scatterSeries); |
|
302 | 302 | break; |
|
303 | 303 | } |
|
304 | 304 | |
|
305 | 305 | case QSeries::SeriesTypePie: { |
|
306 | 306 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
307 | 307 | appendMarkers(pieSeries); |
|
308 | 308 | break; |
|
309 | 309 | } |
|
310 | 310 | |
|
311 | 311 | case QSeries::SeriesTypeSpline: { |
|
312 | 312 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
313 | 313 | appendMarkers(splineSeries); |
|
314 | 314 | break; |
|
315 | 315 | } |
|
316 | 316 | default: { |
|
317 | 317 | qDebug()<< "QLegend::createMarkers" << series->type() << "not implemented."; |
|
318 | 318 | break; |
|
319 | 319 | } |
|
320 | 320 | } |
|
321 | 321 | } |
|
322 | 322 | |
|
323 | 323 | void QLegend::appendMarkers(QXYSeries* series) |
|
324 | 324 | { |
|
325 | 325 | LegendMarker* marker = new LegendMarker(series,this); |
|
326 | 326 | marker->setName(series->name()); |
|
327 | 327 | marker->setBrush(series->brush()); |
|
328 | 328 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); |
|
329 | 329 | connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed())); |
|
330 | 330 | mMarkers.append(marker); |
|
331 | 331 | childItems().append(marker); |
|
332 | 332 | } |
|
333 | 333 | |
|
334 | 334 | void QLegend::appendMarkers(QBarSeries *series) |
|
335 | 335 | { |
|
336 | 336 | foreach(QBarSet* s, series->barSets()) { |
|
337 | 337 | LegendMarker* marker = new LegendMarker(series,s,this); |
|
338 | 338 | marker->setName(s->name()); |
|
339 | 339 | marker->setBrush(s->brush()); |
|
340 | 340 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); |
|
341 | 341 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); |
|
342 | 342 | connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed())); |
|
343 | 343 | mMarkers.append(marker); |
|
344 | 344 | childItems().append(marker); |
|
345 | 345 | } |
|
346 | 346 | } |
|
347 | 347 | |
|
348 | 348 | void QLegend::appendMarkers(QPieSeries *series) |
|
349 | 349 | { |
|
350 | 350 | foreach(QPieSlice* s, series->slices()) { |
|
351 | 351 | LegendMarker* marker = new LegendMarker(series,s,this); |
|
352 | 352 | marker->setName(s->label()); |
|
353 | 353 | marker->setBrush(s->sliceBrush()); |
|
354 | 354 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); |
|
355 | 355 | connect(s,SIGNAL(changed()),marker,SLOT(changed())); |
|
356 | 356 | connect(s,SIGNAL(destroyed()),marker,SLOT(deleteLater())); |
|
357 | 357 | connect(marker,SIGNAL(destroyed()),this,SLOT(handleMarkerDestroyed())); |
|
358 | 358 | mMarkers.append(marker); |
|
359 | 359 | childItems().append(marker); |
|
360 | 360 | } |
|
361 | 361 | } |
|
362 | 362 | |
|
363 | 363 | void QLegend::deleteMarkers(QSeries *series) |
|
364 | 364 | { |
|
365 | 365 | // Search all markers that belong to given series and delete them. |
|
366 | 366 | foreach (LegendMarker *m, mMarkers) { |
|
367 | 367 | if (m->series() == series) { |
|
368 | 368 | mMarkers.removeOne(m); |
|
369 | 369 | delete m; |
|
370 | 370 | } |
|
371 | 371 | } |
|
372 | 372 | } |
|
373 | 373 | |
|
374 | 374 | void QLegend::layoutChanged() |
|
375 | 375 | { |
|
376 | 376 | // Calculate layout for markers and text |
|
377 | qDebug() << "Marker count:" << mMarkers.count(); | |
|
378 | 377 | if (mMarkers.count() <= 0) { |
|
379 | 378 | // Nothing to do |
|
380 | 379 | return; |
|
381 | 380 | } |
|
382 | 381 | |
|
383 | 382 | // Find out widest item. |
|
384 | 383 | qreal itemMaxWidth = 0; |
|
385 | 384 | qreal itemMaxHeight = 0; |
|
386 | 385 | foreach (LegendMarker* m, mMarkers) { |
|
387 | 386 | if (m->boundingRect().width() > itemMaxWidth) { |
|
388 | 387 | itemMaxWidth = m->boundingRect().width(); |
|
389 | 388 | } |
|
390 | 389 | if (m->boundingRect().height() > itemMaxHeight) { |
|
391 | 390 | itemMaxHeight = m->boundingRect().height(); |
|
392 | 391 | } |
|
393 | 392 | } |
|
394 | 393 | |
|
395 | 394 | int maxHorizontalItems = boundingRect().width() / itemMaxWidth; |
|
396 | 395 | int maxVerticalItems = boundingRect().height() / itemMaxHeight; |
|
397 | 396 | |
|
398 | 397 | if (mMarkers.count() > maxHorizontalItems * maxVerticalItems) { |
|
399 | 398 | // TODO: overlapping layout |
|
400 | 399 | qDebug() << "Warning. Not enough space to layout all legend items properly."; |
|
401 | 400 | } |
|
402 | 401 | |
|
403 | 402 | qreal margin = 5; |
|
404 | 403 | qreal totalWidth = 0; |
|
405 | 404 | qreal totalHeight = 0; |
|
406 | 405 | switch (mPreferredLayout) |
|
407 | 406 | { |
|
408 | 407 | case QLegend::PreferredLayoutHorizontal: { |
|
409 | 408 | /* |
|
410 | 409 | qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); |
|
411 | 410 | if (xStep > itemMaxWidth) { |
|
412 | 411 | xStep = itemMaxWidth; |
|
413 | 412 | } |
|
414 | 413 | qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); |
|
415 | 414 | if (yStep > itemMaxHeight) { |
|
416 | 415 | yStep = itemMaxHeight; |
|
417 | 416 | }*/ |
|
418 | 417 | qreal xStep = itemMaxWidth; |
|
419 | 418 | qreal yStep = itemMaxHeight; |
|
420 | 419 | qreal x = mPos.x() + margin; |
|
421 | 420 | qreal y = mPos.y() + margin; |
|
422 | 421 | int row = 1; |
|
423 | 422 | int column = 0; |
|
424 | 423 | int maxRows = 1; |
|
425 | 424 | int maxColumns = 1; |
|
426 | 425 | foreach (LegendMarker* m, mMarkers) { |
|
427 | 426 | maxRows = row; |
|
428 | 427 | m->setPos(x,y); |
|
429 | 428 | x += xStep; |
|
430 | 429 | column++; |
|
431 | 430 | if (column > maxColumns) { |
|
432 | 431 | maxColumns = column; |
|
433 | 432 | } |
|
434 | 433 | if ((x + itemMaxWidth + margin*2) > (mPos.x() + mMaximumSize.width())) { |
|
435 | 434 | x = mPos.x() + margin; |
|
436 | 435 | y += yStep; |
|
437 | 436 | row++; |
|
438 | 437 | column = 0; |
|
439 | 438 | } |
|
440 | 439 | } |
|
441 | 440 | totalWidth = maxColumns * itemMaxWidth + margin * 2; |
|
442 | 441 | totalHeight = maxRows * itemMaxHeight + margin * 2; |
|
443 | 442 | break; |
|
444 | 443 | } |
|
445 | 444 | case QLegend::PreferredLayoutVertical: { |
|
446 | 445 | /* |
|
447 | 446 | qreal xStep = mMaximumSize.width() / (mMarkers.count()+1); |
|
448 | 447 | if (xStep > itemMaxWidth) { |
|
449 | 448 | xStep = itemMaxWidth; |
|
450 | 449 | } |
|
451 | 450 | qreal yStep = mMaximumSize.height() / (mMarkers.count()+1); |
|
452 | 451 | if (yStep > itemMaxHeight) { |
|
453 | 452 | yStep = itemMaxHeight; |
|
454 | 453 | }*/ |
|
455 | 454 | qreal xStep = itemMaxWidth; |
|
456 | 455 | qreal yStep = itemMaxHeight; |
|
457 | 456 | qreal x = mPos.x() + margin; |
|
458 | 457 | qreal y = mPos.y() + margin; |
|
459 | 458 | int row = 0; |
|
460 | 459 | int column = 1; |
|
461 | 460 | int maxRows = 1; |
|
462 | 461 | int maxColumns = 1; |
|
463 | 462 | foreach (LegendMarker* m, mMarkers) { |
|
464 | 463 | maxColumns = column; |
|
465 | 464 | m->setPos(x,y); |
|
466 | 465 | y += yStep; |
|
467 | 466 | row++; |
|
468 | 467 | if (row > maxRows) { |
|
469 | 468 | maxRows = row; |
|
470 | 469 | } |
|
471 | 470 | if ((y + itemMaxHeight + margin*2) > (mPos.y() + mMaximumSize.height())) { |
|
472 | 471 | y = mPos.y() + margin; |
|
473 | 472 | x += xStep; |
|
474 | 473 | column++; |
|
475 | 474 | row = 0; |
|
476 | 475 | } |
|
477 | 476 | } |
|
478 | 477 | totalWidth = maxColumns * itemMaxWidth + margin * 2; |
|
479 | 478 | totalHeight = maxRows * itemMaxHeight + margin * 2; |
|
480 | 479 | break; |
|
481 | 480 | } |
|
482 | 481 | default: { |
|
483 | 482 | break; |
|
484 | 483 | } |
|
485 | 484 | } |
|
486 | 485 | |
|
487 | 486 | mSize.setWidth(totalWidth); |
|
488 | 487 | mSize.setHeight(totalHeight); |
|
489 | 488 | } |
|
490 | 489 | |
|
491 | 490 | #include "moc_qlegend.cpp" |
|
492 | 491 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,87 +1,86 | |||
|
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 | enum PreferredLayout { |
|
24 | 24 | PreferredLayoutHorizontal, |
|
25 | 25 | PreferredLayoutVertical |
|
26 | 26 | }; |
|
27 | 27 | |
|
28 | 28 | explicit QLegend(QGraphicsItem *parent = 0); |
|
29 | 29 | |
|
30 | 30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
31 | 31 | QRectF boundingRect() const; |
|
32 | 32 | |
|
33 | 33 | void setBrush(const QBrush& brush); |
|
34 | 34 | QBrush brush() const; |
|
35 | 35 | |
|
36 | 36 | void setPen(const QPen& pen); |
|
37 | 37 | QPen pen() const; |
|
38 | 38 | |
|
39 | 39 | void setPreferredLayout(QLegend::PreferredLayout preferred); |
|
40 | 40 | |
|
41 | 41 | QSizeF maximumSize() const; |
|
42 | 42 | void setMaximumSize(const QSizeF size); |
|
43 | 43 | |
|
44 | 44 | void setSize(const QSizeF size); |
|
45 | 45 | void setPos(const QPointF &pos); |
|
46 | 46 | |
|
47 | 47 | signals: |
|
48 | 48 | // for interactions. |
|
49 | 49 | void clicked(QSeries* series, Qt::MouseButton button); |
|
50 | 50 | void clicked(QBarSet* barset, Qt::MouseButton button); |
|
51 | 51 | void clicked(QPieSlice* slice, Qt::MouseButton button); |
|
52 | 52 | |
|
53 | 53 | public slots: |
|
54 | 54 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
55 | 55 | void handleSeriesRemoved(QSeries* series); |
|
56 | 56 | void handleAdded(QList<QPieSlice*> slices); |
|
57 | 57 | void handleRemoved(QList<QPieSlice*> slices); |
|
58 | 58 | void handleMarkerDestroyed(); |
|
59 | 59 | |
|
60 | 60 | private: |
|
61 | 61 | // PIMPL ---> |
|
62 | 62 | void connectSeries(QSeries* series); |
|
63 | 63 | void disconnectSeries(QSeries* series); |
|
64 | 64 | void createMarkers(QSeries* series); |
|
65 | 65 | void appendMarkers(QXYSeries* series); // All line series are derived from QXYSeries, so this works for now |
|
66 | 66 | void appendMarkers(QBarSeries* series); |
|
67 | 67 | void appendMarkers(QPieSeries* series); |
|
68 | 68 | void deleteMarkers(QSeries* series); |
|
69 | 69 | void layoutChanged(); |
|
70 | 70 | // <--- PIMPL |
|
71 | 71 | |
|
72 | 72 | QPointF mPos; |
|
73 | 73 | QSizeF mSize; |
|
74 | 74 | QSizeF mMinimumSize; |
|
75 | 75 | QSizeF mMaximumSize; |
|
76 | 76 | |
|
77 | // QList<QSeries*> mSeriesList; | |
|
78 | 77 | QList<LegendMarker*> mMarkers; |
|
79 | 78 | |
|
80 | 79 | QBrush m_brush; |
|
81 | 80 | QPen m_pen; |
|
82 | 81 | QLegend::PreferredLayout mPreferredLayout; |
|
83 | 82 | }; |
|
84 | 83 | |
|
85 | 84 | QTCOMMERCIALCHART_END_NAMESPACE |
|
86 | 85 | |
|
87 | 86 | #endif // QLEGEND_H |
@@ -1,71 +1,66 | |||
|
1 | 1 | #include "qseries.h" |
|
2 | 2 | |
|
3 | 3 | /*! |
|
4 | 4 | \class QSeries |
|
5 | 5 | \brief Base class for all QtCommercial Chart series. |
|
6 | 6 | \mainclass |
|
7 | 7 | |
|
8 | 8 | Usually you use the series type specific inherited classes instead of the base class. |
|
9 | 9 | \sa QScatterSeries |
|
10 | 10 | */ |
|
11 | 11 | |
|
12 | 12 | /*! |
|
13 | 13 | \enum QSeries::QSeriesType |
|
14 | 14 | |
|
15 | 15 | The type of the series object. |
|
16 | 16 | |
|
17 | 17 | \value SeriesTypeLine |
|
18 | 18 | \value SeriesTypeArea |
|
19 | 19 | \value SeriesTypeBar |
|
20 | 20 | \value SeriesTypeStackedBar |
|
21 | 21 | \value SeriesTypePercentBar |
|
22 | 22 | \value SeriesTypePie |
|
23 | 23 | \value SeriesTypeScatter |
|
24 | 24 | \value SeriesTypeSpline |
|
25 | 25 | */ |
|
26 | 26 | |
|
27 | 27 | /*! |
|
28 | 28 | \fn QSeries::QSeries(QObject *parent) |
|
29 | 29 | \brief Constructs ChartSeries object with \a parent. |
|
30 | 30 | */ |
|
31 | 31 | |
|
32 | 32 | /*! |
|
33 | 33 | \fn QSeries::~QSeries() |
|
34 | 34 | \brief Virtual destructor for the chart series. |
|
35 | 35 | */ |
|
36 | 36 | |
|
37 | 37 | /*! |
|
38 | 38 | \fn QSeriesType QSeries::type() const |
|
39 | 39 | \brief The type of the series. |
|
40 | 40 | */ |
|
41 | 41 | |
|
42 | 42 | /*! |
|
43 | 43 | \fn bool QSeries::setModel(QAbstractItemModel *model) |
|
44 | 44 | \brief Use the \a model to provide data for the series. The model overrides possible user data |
|
45 | 45 | set with QChartSeries type specific data setters. For example if you call both |
|
46 | 46 | QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is |
|
47 | 47 | used by the series. Returns true if the model is valid for the series. |
|
48 | 48 | */ |
|
49 | 49 | |
|
50 | 50 | /*! |
|
51 | \fn QList<QSeries::Legend> QSeries::legend() | |
|
52 | \brief Returns the legend of the series. If series is empty, empty list is returned. | |
|
53 | */ | |
|
54 | ||
|
55 | /*! | |
|
56 | 51 | \fn void QSeries::setTitle(QString title) |
|
57 | 52 | \brief Sets a \a title for the series. |
|
58 | 53 | |
|
59 | 54 | This is not used directly by the chart itself. It is up to the user to use this as for example |
|
60 | 55 | chart title. |
|
61 | 56 | \sa QChart::setChartTitle() |
|
62 | 57 | */ |
|
63 | 58 | |
|
64 | 59 | /*! |
|
65 | 60 | \fn QString QSeries::title() |
|
66 | 61 | \brief Returns the title of the series. |
|
67 | 62 | */ |
|
68 | 63 | |
|
69 | 64 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
70 | 65 | #include "moc_qseries.cpp" |
|
71 | 66 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now