|
1 | NO CONTENT: modified file, binary diff hidden |
@@ -1,31 +1,29 | |||
|
1 | 1 | /*! |
|
2 | 2 | \example examples/legend |
|
3 | 3 | \title Legend Example |
|
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 | ||
|
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 | 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 | 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. | |
|
24 | ||
|
26 | This shows how we set the detached legend dimensions. After setting new values, we call update to show changes on screen. | |
|
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 | */ |
@@ -1,224 +1,223 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "mainwidget.h" |
|
22 | 22 | #include <QChart> |
|
23 | 23 | #include <QChartView> |
|
24 | 24 | #include <QPushButton> |
|
25 | 25 | #include <QLabel> |
|
26 | 26 | #include <QDebug> |
|
27 | 27 | #include <QBarSet> |
|
28 | 28 | #include <QBarSeries> |
|
29 | 29 | #include <QLegend> |
|
30 | 30 | #include <QFormLayout> |
|
31 | 31 | |
|
32 | 32 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
33 | 33 | |
|
34 | 34 | MainWidget::MainWidget(QWidget *parent) : |
|
35 | 35 | QWidget(parent) |
|
36 | 36 | { |
|
37 | 37 | // Create buttons for ui |
|
38 | 38 | m_buttonLayout = new QGridLayout(); |
|
39 | 39 | QPushButton *detachLegendButton = new QPushButton("detach legend"); |
|
40 | 40 | connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend())); |
|
41 | 41 | m_buttonLayout->addWidget(detachLegendButton, 0, 0); |
|
42 | 42 | QPushButton *attachLegendButton = new QPushButton("attach legend"); |
|
43 | 43 | connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend())); |
|
44 | 44 | m_buttonLayout->addWidget(attachLegendButton, 1, 0); |
|
45 | 45 | |
|
46 | 46 | QPushButton *addSetButton = new QPushButton("add barset"); |
|
47 | 47 | connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset())); |
|
48 | 48 | m_buttonLayout->addWidget(addSetButton, 2, 0); |
|
49 | 49 | QPushButton *removeBarsetButton = new QPushButton("remove barset"); |
|
50 | 50 | connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset())); |
|
51 | 51 | m_buttonLayout->addWidget(removeBarsetButton, 3, 0); |
|
52 | 52 | |
|
53 | 53 | QPushButton *leftButton = new QPushButton("Align legend left"); |
|
54 | 54 | connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft())); |
|
55 | 55 | m_buttonLayout->addWidget(leftButton, 4, 0); |
|
56 | 56 | |
|
57 | 57 | QPushButton *rightButton = new QPushButton("Align legend right"); |
|
58 | 58 | connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight())); |
|
59 | 59 | m_buttonLayout->addWidget(rightButton, 5, 0); |
|
60 | 60 | |
|
61 | 61 | QPushButton *topButton = new QPushButton("Align legend top"); |
|
62 | 62 | connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop())); |
|
63 | 63 | m_buttonLayout->addWidget(topButton, 6, 0); |
|
64 | 64 | |
|
65 | 65 | QPushButton *bottomButton = new QPushButton("Align legend bottom"); |
|
66 | 66 | connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom())); |
|
67 | 67 | m_buttonLayout->addWidget(bottomButton, 7, 0); |
|
68 | 68 | |
|
69 | 69 | m_legendPosX = new QDoubleSpinBox(); |
|
70 | 70 | m_legendPosY = new QDoubleSpinBox(); |
|
71 | 71 | m_legendWidth = new QDoubleSpinBox(); |
|
72 | 72 | m_legendHeight = new QDoubleSpinBox(); |
|
73 | 73 | |
|
74 | 74 | connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); |
|
75 | 75 | connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); |
|
76 | 76 | connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); |
|
77 | 77 | connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); |
|
78 | 78 | |
|
79 | 79 | QFormLayout* legendLayout = new QFormLayout(); |
|
80 | 80 | legendLayout->addRow("Horizontal position", m_legendPosX); |
|
81 | 81 | legendLayout->addRow("Vertical position", m_legendPosY); |
|
82 | 82 | legendLayout->addRow("Width", m_legendWidth); |
|
83 | 83 | legendLayout->addRow("Height", m_legendHeight); |
|
84 | 84 | m_legendSettings = new QGroupBox("Detached legend"); |
|
85 | 85 | m_legendSettings->setLayout(legendLayout); |
|
86 | 86 | m_buttonLayout->addWidget(m_legendSettings); |
|
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(); |
|
97 | 95 | m_mainLayout->addLayout(m_buttonLayout, 0, 0); |
|
98 | 96 | m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1); |
|
99 | 97 | setLayout(m_mainLayout); |
|
100 | 98 | |
|
101 | 99 | createSeries(); |
|
102 | 100 | } |
|
103 | 101 | |
|
104 | 102 | void MainWidget::createSeries() |
|
105 | 103 | { |
|
106 | //![3] | |
|
107 | 104 | m_series = new QBarSeries(); |
|
108 | 105 | addBarset(); |
|
109 | 106 | addBarset(); |
|
110 | 107 | addBarset(); |
|
111 | 108 | addBarset(); |
|
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() |
|
125 | 122 | { |
|
126 | 123 | m_legendSettings->setVisible(true); |
|
127 | 124 | QRectF chartViewRect = m_chartView->rect(); |
|
128 | 125 | QRectF legendRect = m_chart->legend()->boundingRect(); |
|
129 | 126 | |
|
130 | 127 | m_legendPosX->setMinimum(0); |
|
131 | 128 | m_legendPosX->setMaximum(chartViewRect.width()); |
|
132 | 129 | m_legendPosX->setValue(150); |
|
133 | 130 | |
|
134 | 131 | m_legendPosY->setMinimum(0); |
|
135 | 132 | m_legendPosY->setMaximum(chartViewRect.height()); |
|
136 | 133 | m_legendPosY->setValue(150); |
|
137 | 134 | |
|
138 | 135 | m_legendWidth->setMinimum(0); |
|
139 | 136 | m_legendWidth->setMaximum(chartViewRect.width()); |
|
140 | 137 | m_legendWidth->setValue(150); |
|
141 | 138 | |
|
142 | 139 | m_legendHeight->setMinimum(0); |
|
143 | 140 | m_legendHeight->setMaximum(chartViewRect.height()); |
|
144 | 141 | m_legendHeight->setValue(75); |
|
145 | 142 | } |
|
146 | 143 | |
|
147 | 144 | void MainWidget::hideLegendSpinbox() |
|
148 | 145 | { |
|
149 | 146 | m_legendSettings->setVisible(false); |
|
150 | 147 | } |
|
151 | 148 | |
|
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(); |
|
165 | 162 | update(); |
|
166 | 163 | } |
|
167 | 164 | |
|
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(); |
|
179 | 176 | } |
|
180 | 177 | |
|
181 | 178 | void MainWidget::addBarset() |
|
182 | 179 | { |
|
183 | 180 | QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount())); |
|
184 | 181 | qreal delta = m_series->barsetCount() * 0.1; |
|
185 | 182 | *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); |
|
186 | 183 | m_series->append(barSet); |
|
187 | 184 | } |
|
188 | 185 | |
|
189 | 186 | void MainWidget::removeBarset() |
|
190 | 187 | { |
|
191 | 188 | QList<QBarSet*> sets = m_series->barSets(); |
|
192 | 189 | if (sets.count() > 0) { |
|
193 | 190 | m_series->remove(sets.at(sets.count()-1)); |
|
194 | 191 | } |
|
195 | 192 | } |
|
196 | 193 | |
|
197 | 194 | void MainWidget::setLegendLeft() |
|
198 | 195 | { |
|
199 | 196 | m_chart->legend()->setAlignment(Qt::AlignLeft); |
|
200 | 197 | } |
|
201 | 198 | |
|
202 | 199 | void MainWidget::setLegendRight() |
|
203 | 200 | { |
|
204 | 201 | m_chart->legend()->setAlignment(Qt::AlignRight); |
|
205 | 202 | } |
|
206 | 203 | |
|
207 | 204 | void MainWidget::setLegendTop() |
|
208 | 205 | { |
|
209 | 206 | m_chart->legend()->setAlignment(Qt::AlignTop); |
|
210 | 207 | } |
|
211 | 208 | |
|
212 | 209 | void MainWidget::setLegendBottom() |
|
213 | 210 | { |
|
214 | 211 | m_chart->legend()->setAlignment(Qt::AlignBottom); |
|
215 | 212 | } |
|
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