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