|
1 | NO CONTENT: modified file, binary diff hidden |
@@ -4,28 +4,26 | |||
|
4 | 4 | \subtitle |
|
5 | 5 | |
|
6 | 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. |
|
|
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 | 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 | 15 | \snippet ../examples/legend/mainwidget.cpp 1 |
|
14 | 16 | |
|
15 | Here we create custom graphics scene, where we want to draw the detached legend. | |
|
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. | |
|
16 | 19 | |
|
17 | 20 | \snippet ../examples/legend/mainwidget.cpp 2 |
|
18 | 21 | |
|
19 | Add some series to the chart. | |
|
22 | Here we attach legend back to chart. The background is turned invisible. | |
|
20 | 23 | |
|
21 | 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 | 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 | 87 | m_legendSettings->setVisible(false); |
|
88 | 88 | |
|
89 | 89 | // Create chart view with the chart |
|
90 | //![1] | |
|
91 | 90 | m_chart = new QChart(); |
|
92 | 91 | m_chartView = new QChartView(m_chart, this); |
|
93 | //![1] | |
|
94 | 92 | |
|
95 | 93 | // Create layout for grid and detached legend |
|
96 | 94 | m_mainLayout = new QGridLayout(); |
@@ -103,7 +101,6 MainWidget::MainWidget(QWidget *parent) : | |||
|
103 | 101 | |
|
104 | 102 | void MainWidget::createSeries() |
|
105 | 103 | { |
|
106 | //![3] | |
|
107 | 104 | m_series = new QBarSeries(); |
|
108 | 105 | addBarset(); |
|
109 | 106 | addBarset(); |
@@ -112,13 +109,13 void MainWidget::createSeries() | |||
|
112 | 109 | |
|
113 | 110 | m_chart->addSeries(m_series); |
|
114 | 111 | m_chart->setTitle("Legend detach example"); |
|
115 | ||
|
112 | //![1] | |
|
116 | 113 | m_chart->legend()->setVisible(true); |
|
117 | 114 | m_chart->legend()->setAlignment(Qt::AlignBottom); |
|
118 | m_chart->axisY()->setNiceNumbersEnabled(true); | |
|
115 | //![1] | |
|
119 | 116 | |
|
117 | m_chart->axisY()->setNiceNumbersEnabled(true); | |
|
120 | 118 | m_chartView->setRenderHint(QPainter::Antialiasing); |
|
121 | //![3] | |
|
122 | 119 | } |
|
123 | 120 | |
|
124 | 121 | void MainWidget::showLegendSpinbox() |
@@ -152,13 +149,13 void MainWidget::hideLegendSpinbox() | |||
|
152 | 149 | |
|
153 | 150 | void MainWidget::detachLegend() |
|
154 | 151 | { |
|
155 |
//![ |
|
|
152 | //![2] | |
|
156 | 153 | QLegend *legend = m_chart->legend(); |
|
157 | 154 | legend->detachFromChart(); |
|
158 | 155 | |
|
159 | 156 | m_chart->legend()->setBackgroundVisible(true); |
|
160 | 157 | m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128))); |
|
161 |
//![ |
|
|
158 | //![2] | |
|
162 | 159 | |
|
163 | 160 | showLegendSpinbox(); |
|
164 | 161 | updateLegendLayout(); |
@@ -168,11 +165,11 void MainWidget::detachLegend() | |||
|
168 | 165 | |
|
169 | 166 | void MainWidget::attachLegend() |
|
170 | 167 | { |
|
171 |
//![ |
|
|
168 | //![3] | |
|
172 | 169 | QLegend *legend = m_chart->legend(); |
|
173 | 170 | legend->attachToChart(); |
|
174 | 171 | m_chart->legend()->setBackgroundVisible(false); |
|
175 |
//![ |
|
|
172 | //![3] | |
|
176 | 173 | |
|
177 | 174 | hideLegendSpinbox(); |
|
178 | 175 | update(); |
@@ -216,9 +213,11 void MainWidget::setLegendBottom() | |||
|
216 | 213 | |
|
217 | 214 | void MainWidget::updateLegendLayout() |
|
218 | 215 | { |
|
216 | //![4] | |
|
219 | 217 | m_chart->legend()->setGeometry(m_legendPosX->value() |
|
220 | 218 | ,m_legendPosY->value() |
|
221 | 219 | ,m_legendWidth->value() |
|
222 | 220 | ,m_legendHeight->value()); |
|
223 | 221 | m_chart->legend()->update(); |
|
222 | //![4] | |
|
224 | 223 | } |
General Comments 0
You need to be logged in to leave comments.
Login now