##// END OF EJS Templates
updated legend documentation
sauimone -
r1447:b0784ac08186
parent child
Show More
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -4,28 +4,26
4 \subtitle
4 \subtitle
5
5
6 This example shows how to detach legend from chart and how to attach it back. By default the chart
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.
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. The behaviour of legend can be inspected by running the legend example.
8 In example we use barseries where we add or remove barsets. The legend reflects the changes in series. Legend can be detached or attached back to chart and its alignment can be modified.
9 When legend is detached, it can be resized and positioned freely.
8
10
9 \image examples_legend_detach.png
11 \image examples_legend_detach.png
10
12
11 First we create our chart as usual.
13 Here we turn legend visible and set its alignment to the bottom of the chart.
12
14
13 \snippet ../examples/legend/mainwidget.cpp 1
15 \snippet ../examples/legend/mainwidget.cpp 1
14
15 Here we create custom graphics scene, where we want to draw the detached legend.
16
16
17 This snippet shows how to detach the legend from chart. After detaching, we turn its background to visible and set a different color to it. This makes it easier to see, how the items inside legend are arranged
18 in detached mode.
19
17 \snippet ../examples/legend/mainwidget.cpp 2
20 \snippet ../examples/legend/mainwidget.cpp 2
18
19 Add some series to the chart.
20
21
22 Here we attach legend back to chart. The background is turned invisible.
23
21 \snippet ../examples/legend/mainwidget.cpp 3
24 \snippet ../examples/legend/mainwidget.cpp 3
22
25
23 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.
26 This shows how we set the detached legend dimensions. After setting new values, we call update to show changes on screen.
24
27
25 \snippet ../examples/legend/mainwidget.cpp 4
28 \snippet ../examples/legend/mainwidget.cpp 4
26
27 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.
28
29 \snippet ../examples/legend/mainwidget.cpp 5
30
31 */
29 */
@@ -87,10 +87,8 MainWidget::MainWidget(QWidget *parent) :
87 m_legendSettings->setVisible(false);
87 m_legendSettings->setVisible(false);
88
88
89 // Create chart view with the chart
89 // Create chart view with the chart
90 //![1]
91 m_chart = new QChart();
90 m_chart = new QChart();
92 m_chartView = new QChartView(m_chart, this);
91 m_chartView = new QChartView(m_chart, this);
93 //![1]
94
92
95 // Create layout for grid and detached legend
93 // Create layout for grid and detached legend
96 m_mainLayout = new QGridLayout();
94 m_mainLayout = new QGridLayout();
@@ -103,7 +101,6 MainWidget::MainWidget(QWidget *parent) :
103
101
104 void MainWidget::createSeries()
102 void MainWidget::createSeries()
105 {
103 {
106 //![3]
107 m_series = new QBarSeries();
104 m_series = new QBarSeries();
108 addBarset();
105 addBarset();
109 addBarset();
106 addBarset();
@@ -112,13 +109,13 void MainWidget::createSeries()
112
109
113 m_chart->addSeries(m_series);
110 m_chart->addSeries(m_series);
114 m_chart->setTitle("Legend detach example");
111 m_chart->setTitle("Legend detach example");
115
112 //![1]
116 m_chart->legend()->setVisible(true);
113 m_chart->legend()->setVisible(true);
117 m_chart->legend()->setAlignment(Qt::AlignBottom);
114 m_chart->legend()->setAlignment(Qt::AlignBottom);
118 m_chart->axisY()->setNiceNumbersEnabled(true);
115 //![1]
119
116
117 m_chart->axisY()->setNiceNumbersEnabled(true);
120 m_chartView->setRenderHint(QPainter::Antialiasing);
118 m_chartView->setRenderHint(QPainter::Antialiasing);
121 //![3]
122 }
119 }
123
120
124 void MainWidget::showLegendSpinbox()
121 void MainWidget::showLegendSpinbox()
@@ -152,13 +149,13 void MainWidget::hideLegendSpinbox()
152
149
153 void MainWidget::detachLegend()
150 void MainWidget::detachLegend()
154 {
151 {
155 //![4]
152 //![2]
156 QLegend *legend = m_chart->legend();
153 QLegend *legend = m_chart->legend();
157 legend->detachFromChart();
154 legend->detachFromChart();
158
155
159 m_chart->legend()->setBackgroundVisible(true);
156 m_chart->legend()->setBackgroundVisible(true);
160 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
157 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
161 //![4]
158 //![2]
162
159
163 showLegendSpinbox();
160 showLegendSpinbox();
164 updateLegendLayout();
161 updateLegendLayout();
@@ -168,11 +165,11 void MainWidget::detachLegend()
168
165
169 void MainWidget::attachLegend()
166 void MainWidget::attachLegend()
170 {
167 {
171 //![5]
168 //![3]
172 QLegend *legend = m_chart->legend();
169 QLegend *legend = m_chart->legend();
173 legend->attachToChart();
170 legend->attachToChart();
174 m_chart->legend()->setBackgroundVisible(false);
171 m_chart->legend()->setBackgroundVisible(false);
175 //![5]
172 //![3]
176
173
177 hideLegendSpinbox();
174 hideLegendSpinbox();
178 update();
175 update();
@@ -216,9 +213,11 void MainWidget::setLegendBottom()
216
213
217 void MainWidget::updateLegendLayout()
214 void MainWidget::updateLegendLayout()
218 {
215 {
216 //![4]
219 m_chart->legend()->setGeometry(m_legendPosX->value()
217 m_chart->legend()->setGeometry(m_legendPosX->value()
220 ,m_legendPosY->value()
218 ,m_legendPosY->value()
221 ,m_legendWidth->value()
219 ,m_legendWidth->value()
222 ,m_legendHeight->value());
220 ,m_legendHeight->value());
223 m_chart->legend()->update();
221 m_chart->legend()->update();
222 //![4]
224 }
223 }
General Comments 0
You need to be logged in to leave comments. Login now