##// END OF EJS Templates
legend example to documentation. minor legend fixes
sauimone -
r1300:6bd3a3b0fd91
parent child
Show More
@@ -0,0 +1,29
1 /*!
2 \example examples/legend
3 \title Legend Example
4 \subtitle
5
6 This example shows how to detach legend from chart and how to attach it back. By default the chart
7 draws the legend inside same view with the chart. In some cases user may want to draw legend to somewhere else. To make this possible the legend can be detached from the chart. Detaching means that chart doesn't draw the legend or try to change it's layout. Detached can then be drawn where user wants, for example in different graphics scene. In this example we do so.
8
9 First we create our chart as usual.
10
11 \snippet ../examples/legend/mainwidget.cpp 1
12
13 Here we create custom graphics scene, where we want to draw the detached legend.
14
15 \snippet ../examples/legend/mainwidget.cpp 2
16
17 Add some series to the chart.
18
19 \snippet ../examples/legend/mainwidget.cpp 3
20
21 Here we detach the legend. After detaching, we set new geometry for it and add it to the custom scene. The custom scene adopts the legend and becomes new parent for it. Note that chart will still update the contents of the legend.
22
23 \snippet ../examples/legend/mainwidget.cpp 4
24
25 To attach legend back to chart we first remove it from the custom scene. Then we add legend to the scene, where chart is. Last we attach legend to chart. Attaching legend to chart automatically sets the QChart as parent for legend.
26
27 \snippet ../examples/legend/mainwidget.cpp 5
28
29 */
@@ -17,6 +17,7
17 <li><a href="examples-barchart.html">Bar chart</a></li>
17 <li><a href="examples-barchart.html">Bar chart</a></li>
18 <li><a href="examples-customchart.html">Custom chart</a></li>
18 <li><a href="examples-customchart.html">Custom chart</a></li>
19 <li><a href="examples-groupedbarchart.html">Grouped bar chart</a></li>
19 <li><a href="examples-groupedbarchart.html">Grouped bar chart</a></li>
20 <li><a href="examples-legend.html">Legend</a></li>
20 <li><a href="examples-linechart.html">Line chart</a></li>
21 <li><a href="examples-linechart.html">Line chart</a></li>
21 <li><a href="examples-modeldata.html">Model data</a></li>
22 <li><a href="examples-modeldata.html">Model data</a></li>
22 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
23 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
@@ -13,10 +13,7 QTCOMMERCIALCHART_USE_NAMESPACE
13 MainWidget::MainWidget(QWidget *parent) :
13 MainWidget::MainWidget(QWidget *parent) :
14 QWidget(parent)
14 QWidget(parent)
15 {
15 {
16 m_setCount = 0;
16 // Create buttons for ui
17 m_chart = new QChart();
18
19 //![1]
20 m_buttonLayout = new QGridLayout();
17 m_buttonLayout = new QGridLayout();
21 QPushButton *detachLegendButton = new QPushButton("detach legend");
18 QPushButton *detachLegendButton = new QPushButton("detach legend");
22 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
19 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
@@ -31,12 +28,15 MainWidget::MainWidget(QWidget *parent) :
31 QPushButton *removeBarsetButton = new QPushButton("remove barset");
28 QPushButton *removeBarsetButton = new QPushButton("remove barset");
32 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
29 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
33 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
30 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
34 //![1]
35
31
36 // Create chart view with the chart
32 // Create chart view with the chart
33 //![1]
34 m_chart = new QChart();
37 m_chartView = new QChartView(m_chart, this);
35 m_chartView = new QChartView(m_chart, this);
38 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
36 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
37 //![1]
39
38
39 // Create custom scene and view, where detached legend will be drawn
40 //![2]
40 //![2]
41 m_customView = new QGraphicsView(this);
41 m_customView = new QGraphicsView(this);
42 m_customScene = new QGraphicsScene(this);
42 m_customScene = new QGraphicsScene(this);
@@ -55,6 +55,7 MainWidget::MainWidget(QWidget *parent) :
55
55
56 void MainWidget::createSeries()
56 void MainWidget::createSeries()
57 {
57 {
58 //![3]
58 m_series = new QBarSeries();
59 m_series = new QBarSeries();
59 addBarset();
60 addBarset();
60 addBarset();
61 addBarset();
@@ -69,54 +70,51 void MainWidget::createSeries()
69 m_chart->axisY()->setNiceNumbersEnabled(true);
70 m_chart->axisY()->setNiceNumbersEnabled(true);
70
71
71 m_chartView->setRenderHint(QPainter::Antialiasing);
72 m_chartView->setRenderHint(QPainter::Antialiasing);
73 //![3]
72 }
74 }
73
75
74 void MainWidget::detachLegend()
76 void MainWidget::detachLegend()
75 {
77 {
76 //![3]
78 // Detach legend from chart and
77 // Detach legend from chart
79 // put legend to our custom scene
80 //![4]
78 QLegend *legend = m_chart->legend();
81 QLegend *legend = m_chart->legend();
79 legend->detachFromChart();
82 legend->detachFromChart();
80 legend->setGeometry(m_customView->rect());
83 legend->setGeometry(m_customView->rect());
81 // legend->setAlignment(QLegend::AlignmentLeft);
82
83 // Put legend to our custom scene
84 m_customScene->addItem(legend);
84 m_customScene->addItem(legend);
85 //![3]
85 //![4]
86
86
87 // This causes redraw
87 // This forces redraw
88 QSize size(1,1);
88 QSize delta(1,1);
89 this->resize(this->size() + size);
89 resize(size() + delta);
90 this->resize(this->size() - size);
90 resize(size() - delta);
91 }
91 }
92
92
93
93
94 void MainWidget::attachLegend()
94 void MainWidget::attachLegend()
95 {
95 {
96 //![4]
96 // Remove legend from custom scene and put it back to chartview scene.
97 // Attach legend back to chart, so that layout works.
98
99 //![5]
97 QLegend *legend = m_chart->legend();
100 QLegend *legend = m_chart->legend();
98
101
99 if (m_customScene->items().contains(legend)) {
102 if (m_customScene->items().contains(legend)) {
100 // Remove legend from custom scene and put it back to chartview scene.
101 // Attach legend back to chart, so that layout works.
102 m_customScene->removeItem(legend);
103 m_customScene->removeItem(legend);
103 legend->setParent(m_chart);
104 m_chartView->scene()->addItem(legend);
104 m_chartView->scene()->addItem(legend);
105 // legend->setAlignment(QLegend::AlignmentBottom);
106 legend->attachToChart();
105 legend->attachToChart();
107 }
106 }
108 //![4]
107 //![5]
109
108
110 // This causes redraw
109 // This forces redraw
111 QSize size(1,1);
110 QSize delta(1,1);
112 this->resize(this->size() + size);
111 resize(size() + delta);
113 this->resize(this->size() - size);
112 resize(size() - delta);
114 }
113 }
115
114
116 void MainWidget::addBarset()
115 void MainWidget::addBarset()
117 {
116 {
118 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_setCount));
117 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount()));
119 m_setCount++;
120 qreal delta = m_series->barsetCount() * 0.1;
118 qreal delta = m_series->barsetCount() * 0.1;
121 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
119 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
122 m_series->append(barSet);
120 m_series->append(barSet);
@@ -33,17 +33,12 private:
33 QChart *m_chart;
33 QChart *m_chart;
34 QBarSeries *m_series;
34 QBarSeries *m_series;
35
35
36 QGraphicsScene *m_scene;
37 QChartView *m_chartView;
36 QChartView *m_chartView;
38 QGridLayout *m_mainLayout;
37 QGridLayout *m_mainLayout;
39 QGridLayout *m_buttonLayout;
38 QGridLayout *m_buttonLayout;
40
39
41 QGraphicsView *m_customView;
40 QGraphicsView *m_customView;
42 QGraphicsScene *m_customScene;
41 QGraphicsScene *m_customScene;
43 QGraphicsGridLayout *m_customLayout;
44
45
46 int m_setCount;
47 };
42 };
48
43
49 #endif // MAINWIDGET_H
44 #endif // MAINWIDGET_H
@@ -316,7 +316,7 void ChartPresenter::updateLayout()
316 QLegend* legend = m_chart->d_ptr->m_legend;
316 QLegend* legend = m_chart->d_ptr->m_legend;
317
317
318 // recalculate legend position
318 // recalculate legend position
319 if (legend->isAttachedToChart() && legend->isEnabled()) {
319 if (legend != 0 && legend->isAttachedToChart() && legend->isEnabled()) {
320
320
321 QRect legendRect;
321 QRect legendRect;
322
322
@@ -87,7 +87,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
87 */
87 */
88
88
89 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
89 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
90 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this))
90 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
91 {
91 {
92 setZValue(ChartPresenter::LegendZValue);
92 setZValue(ChartPresenter::LegendZValue);
93 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
93 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
@@ -200,7 +200,7 void QLegend::detachFromChart()
200 */
200 */
201 void QLegend::attachToChart()
201 void QLegend::attachToChart()
202 {
202 {
203 d_ptr->m_attachedToChart = true;
203 d_ptr->attachToChart();
204 }
204 }
205
205
206 /*!
206 /*!
@@ -292,9 +292,10 qreal QLegend::minHeight() const
292
292
293 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
294
294
295 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter,QLegend *q):
295 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
296 q_ptr(q),
296 q_ptr(q),
297 m_presenter(presenter),
297 m_presenter(presenter),
298 m_chart(chart),
298 m_markers(new QGraphicsItemGroup(q)),
299 m_markers(new QGraphicsItemGroup(q)),
299 m_alignment(QLegend::AlignmentTop),
300 m_alignment(QLegend::AlignmentTop),
300 m_offsetX(0),
301 m_offsetX(0),
@@ -407,6 +408,12 void QLegendPrivate::updateLayout()
407 }
408 }
408 }
409 }
409
410
411 void QLegendPrivate::attachToChart()
412 {
413 m_attachedToChart = true;
414 q_ptr->setParent(m_chart);
415 }
416
410 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
417 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
411 {
418 {
412 Q_UNUSED(domain)
419 Q_UNUSED(domain)
@@ -34,6 +34,7
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QChart;
37 class ChartPresenter;
38 class ChartPresenter;
38 class QAbstractSeries;
39 class QAbstractSeries;
39
40
@@ -41,11 +42,12 class QLegendPrivate : public QObject
41 {
42 {
42 Q_OBJECT
43 Q_OBJECT
43 public:
44 public:
44 QLegendPrivate(ChartPresenter *chart,QLegend *q);
45 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
45 ~QLegendPrivate();
46 ~QLegendPrivate();
46
47
47 void setOffset(qreal x, qreal y);
48 void setOffset(qreal x, qreal y);
48 void updateLayout();
49 void updateLayout();
50 void attachToChart();
49
51
50 public Q_SLOTS:
52 public Q_SLOTS:
51 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
53 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
@@ -56,6 +58,7 public Q_SLOTS:
56 private:
58 private:
57 QLegend *q_ptr;
59 QLegend *q_ptr;
58 ChartPresenter *m_presenter;
60 ChartPresenter *m_presenter;
61 QChart* m_chart;
59 QGraphicsItemGroup* m_markers;
62 QGraphicsItemGroup* m_markers;
60 QLegend::Alignments m_alignment;
63 QLegend::Alignments m_alignment;
61 QBrush m_brush;
64 QBrush m_brush;
General Comments 0
You need to be logged in to leave comments. Login now