##// END OF EJS Templates
Qml ChartView properties; legend to use Qt alignments
Tero Ahola -
r1357:13ffac597ff0
parent child
Show More
@@ -0,0 +1,94
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
23
24
25 Flow {
26 id: flow
27 spacing: 5
28 flow: Flow.TopToBottom
29 property variant series // TODO: rename to chart
30
31 Connections {
32 target: series
33 onVisibleChanged: console.log("chart.onVisibleChanged: " + series.visible);
34 onThemeChanged: console.log("chart.onThemeChanged: " + series.theme);
35 onLegendChanged: console.log("chart.onLegendChanged: " + series.legend);
36 onAnimationOptionsChanged: console.log("chart.onAnimationOptionsChanged: " + series.animationOptions);
37 onTitleColorChanged: console.log("chart.onTitleColorChanged: " + series.titleColor);
38 onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor);
39 }
40 Connections {
41 target: series.legend
42 onAlignmentChanged: console.log("legend.onAlignmentChanged: " + series.legend.alignment);
43 onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible);
44 }
45
46 Button {
47 text: "visible"
48 onClicked: series.visible = !series.visible;
49 }
50 Button {
51 text: "theme +"
52 onClicked: series.theme++;
53 }
54 Button {
55 text: "theme -"
56 onClicked: series.theme--;
57 }
58 Button {
59 text: "legend top"
60 onClicked: series.legend.alignment ^= Qt.AlignTop;
61 }
62 Button {
63 text: "legend bottom"
64 onClicked: series.legend.alignment ^= Qt.AlignBottom;
65 }
66 Button {
67 text: "legend left"
68 onClicked: series.legend.alignment ^= Qt.AlignLeft;
69 }
70 Button {
71 text: "legend right"
72 onClicked: series.legend.alignment ^= Qt.AlignRight;
73 }
74 Button {
75 text: "legend visible"
76 onClicked: series.legend.visible = !series.legend.visible;
77 }
78 Button {
79 text: "animation opt +"
80 onClicked: series.animationOptions++;
81 }
82 Button {
83 text: "animation opt -"
84 onClicked: series.animationOptions--;
85 }
86 Button {
87 text: "title color"
88 onClicked: series.titleColor = main.nextColor();
89 }
90 Button {
91 text: "background color"
92 onClicked: series.backgroundColor = main.nextColor();
93 }
94 }
@@ -167,10 +167,10 QComboBox* ThemeWidget::createLegendBox() const
167 {
167 {
168 QComboBox* legendComboBox = new QComboBox();
168 QComboBox* legendComboBox = new QComboBox();
169 legendComboBox->addItem("No Legend ", 0);
169 legendComboBox->addItem("No Legend ", 0);
170 legendComboBox->addItem("Legend Top", QLegend::AlignmentTop);
170 legendComboBox->addItem("Legend Top", Qt::AlignTop);
171 legendComboBox->addItem("Legend Bottom", QLegend::AlignmentBottom);
171 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
172 legendComboBox->addItem("Legend Left", QLegend::AlignmentLeft);
172 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
173 legendComboBox->addItem("Legend Right", QLegend::AlignmentRight);
173 legendComboBox->addItem("Legend Right", Qt::AlignRight);
174 return legendComboBox;
174 return legendComboBox;
175 }
175 }
176
176
@@ -361,7 +361,7 void ThemeWidget::updateUI()
361 chartView->chart()->setAnimationOptions(options);
361 chartView->chart()->setAnimationOptions(options);
362 }
362 }
363
363
364 QLegend::Alignments alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
364 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
365
365
366 if (!alignment) {
366 if (!alignment) {
367 foreach (QChartView *chartView, m_charts) {
367 foreach (QChartView *chartView, m_charts) {
@@ -62,7 +62,7 int main(int argc, char *argv[])
62
62
63 //![4]
63 //![4]
64 chart->legend()->setVisible(true);
64 chart->legend()->setVisible(true);
65 chart->legend()->setAlignment(QLegend::AlignmentBottom);
65 chart->legend()->setAlignment(Qt::AlignBottom);
66 chart->axisY()->setNiceNumbersEnabled(true);
66 chart->axisY()->setNiceNumbersEnabled(true);
67 //![4]
67 //![4]
68
68
@@ -70,7 +70,7 int main(int argc, char *argv[])
70
70
71 //![5]
71 //![5]
72 chart->legend()->setVisible(true);
72 chart->legend()->setVisible(true);
73 chart->legend()->setAlignment(QLegend::AlignmentBottom);
73 chart->legend()->setAlignment(Qt::AlignBottom);
74 chart->axisY()->setNiceNumbersEnabled(true);
74 chart->axisY()->setNiceNumbersEnabled(true);
75 //![5]
75 //![5]
76
76
@@ -66,7 +66,7 void MainWidget::createSeries()
66 m_chart->setTitle("Legend detach example");
66 m_chart->setTitle("Legend detach example");
67
67
68 m_chart->legend()->setVisible(true);
68 m_chart->legend()->setVisible(true);
69 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
69 m_chart->legend()->setAlignment(Qt::AlignBottom);
70 m_chart->axisY()->setNiceNumbersEnabled(true);
70 m_chart->axisY()->setNiceNumbersEnabled(true);
71
71
72 m_chartView->setRenderHint(QPainter::Antialiasing);
72 m_chartView->setRenderHint(QPainter::Antialiasing);
@@ -68,7 +68,7 int main(int argc, char *argv[])
68
68
69 //![5]
69 //![5]
70 chart->legend()->setVisible(true);
70 chart->legend()->setVisible(true);
71 chart->legend()->setAlignment(QLegend::AlignmentBottom);
71 chart->legend()->setAlignment(Qt::AlignBottom);
72 //![5]
72 //![5]
73
73
74 //![6]
74 //![6]
@@ -41,7 +41,7 int main(int argc, char *argv[])
41 chart->setTheme(QChart::ChartThemeLight);
41 chart->setTheme(QChart::ChartThemeLight);
42 chart->setAnimationOptions(QChart::AllAnimations);
42 chart->setAnimationOptions(QChart::AllAnimations);
43 chart->legend()->setVisible(true);
43 chart->legend()->setVisible(true);
44 chart->legend()->setAlignment(QLegend::AlignmentRight);
44 chart->legend()->setAlignment(Qt::AlignRight);
45
45
46 QPieSeries* yearSeries = new QPieSeries(&window);
46 QPieSeries* yearSeries = new QPieSeries(&window);
47 yearSeries->setName("Sales by year - All");
47 yearSeries->setName("Sales by year - All");
@@ -68,7 +68,7 int main(int argc, char *argv[])
68
68
69 //![5]
69 //![5]
70 chart->legend()->setVisible(true);
70 chart->legend()->setVisible(true);
71 chart->legend()->setAlignment(QLegend::AlignmentBottom);
71 chart->legend()->setAlignment(Qt::AlignBottom);
72 chart->axisY()->setNiceNumbersEnabled(true);
72 chart->axisY()->setNiceNumbersEnabled(true);
73 //![5]
73 //![5]
74
74
@@ -104,7 +104,7 int main(int argc, char *argv[])
104 drilldownChart->axisX()->setGridLineVisible(false);
104 drilldownChart->axisX()->setGridLineVisible(false);
105 drilldownChart->axisY()->setNiceNumbersEnabled(true);
105 drilldownChart->axisY()->setNiceNumbersEnabled(true);
106 drilldownChart->legend()->setVisible(true);
106 drilldownChart->legend()->setVisible(true);
107 drilldownChart->legend()->setAlignment(QLegend::AlignmentBottom);
107 drilldownChart->legend()->setAlignment(Qt::AlignBottom);
108 //! [7]
108 //! [7]
109
109
110 QChartView *chartView = new QChartView(drilldownChart);
110 QChartView *chartView = new QChartView(drilldownChart);
@@ -31,8 +31,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
32 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
33 : QDeclarativeItem(parent),
33 : QDeclarativeItem(parent),
34 m_chart(new QChart(this)),
34 m_chart(new QChart(this))
35 m_legend(LegendTop)
36 {
35 {
37 setFlag(QGraphicsItem::ItemHasNoContents, false);
36 setFlag(QGraphicsItem::ItemHasNoContents, false);
38 // m_chart->axisX()->setNiceNumbersEnabled(false);
37 // m_chart->axisX()->setNiceNumbersEnabled(false);
@@ -88,7 +87,11 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *
88
87
89 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
88 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
90 {
89 {
91 m_chart->setTheme((QChart::ChartTheme) theme);
90 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
91 if (chartTheme != m_chart->theme()) {
92 m_chart->setTheme(chartTheme);
93 themeChanged();
94 }
92 }
95 }
93
96
94 DeclarativeChart::Theme DeclarativeChart::theme()
97 DeclarativeChart::Theme DeclarativeChart::theme()
@@ -98,7 +101,11 DeclarativeChart::Theme DeclarativeChart::theme()
98
101
99 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
102 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
100 {
103 {
101 m_chart->setAnimationOptions((QChart::AnimationOption) animations);
104 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
105 if (animationOptions != m_chart->animationOptions()) {
106 m_chart->setAnimationOptions(animationOptions);
107 animationOptionsChanged();
108 }
102 }
109 }
103
110
104 DeclarativeChart::Animation DeclarativeChart::animationOptions()
111 DeclarativeChart::Animation DeclarativeChart::animationOptions()
@@ -113,40 +120,16 DeclarativeChart::Animation DeclarativeChart::animationOptions()
113 return DeclarativeChart::NoAnimation;
120 return DeclarativeChart::NoAnimation;
114 }
121 }
115
122
116 void DeclarativeChart::setLegend(DeclarativeChart::Legend legend)
123 void DeclarativeChart::setTitle(QString title)
117 {
124 {
118 if (legend != m_legend) {
125 if (title != m_chart->title()) {
119 m_legend = legend;
126 m_chart->setTitle(title);
120 switch (m_legend) {
127 emit titleChanged();
121 case LegendDisabled:
122 m_chart->legend()->setVisible(false);
123 break;
124 case LegendTop:
125 m_chart->legend()->setVisible(true);
126 m_chart->legend()->setAlignment(QLegend::AlignmentTop);
127 break;
128 case LegendBottom:
129 m_chart->legend()->setVisible(true);
130 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
131 break;
132 case LegendLeft:
133 m_chart->legend()->setVisible(true);
134 m_chart->legend()->setAlignment(QLegend::AlignmentLeft);
135 break;
136 case LegendRight:
137 m_chart->legend()->setVisible(true);
138 m_chart->legend()->setAlignment(QLegend::AlignmentRight);
139 break;
140 default:
141 m_chart->legend()->setVisible(false);
142 break;
143 }
128 }
144 }
129 }
145 }
130 QString DeclarativeChart::title()
146
147 DeclarativeChart::Legend DeclarativeChart::legend()
148 {
131 {
149 return m_legend;
132 return m_chart->title();
150 }
133 }
151
134
152 QAxis *DeclarativeChart::axisX()
135 QAxis *DeclarativeChart::axisX()
@@ -159,6 +142,11 QAxis *DeclarativeChart::axisY()
159 return m_chart->axisY();
142 return m_chart->axisY();
160 }
143 }
161
144
145 QLegend *DeclarativeChart::legend()
146 {
147 return m_chart->legend();
148 }
149
162 QVariantList DeclarativeChart::axisXLabels()
150 QVariantList DeclarativeChart::axisXLabels()
163 {
151 {
164 QVariantList labels;
152 QVariantList labels;
@@ -181,6 +169,37 void DeclarativeChart::setAxisXLabels(QVariantList list)
181 value = element;
169 value = element;
182 }
170 }
183 }
171 }
172 emit axisLabelsChanged();
173 }
174
175 void DeclarativeChart::setTitleColor(QColor color)
176 {
177 QBrush b = m_chart->titleBrush();
178 if (color != b.color()) {
179 b.setColor(color);
180 m_chart->setTitleBrush(b);
181 emit titleColorChanged();
182 }
183 }
184
185 QColor DeclarativeChart::titleColor()
186 {
187 return m_chart->titleBrush().color();
188 }
189
190 void DeclarativeChart::setBackgroundColor(QColor color)
191 {
192 QBrush b = m_chart->backgroundBrush();
193 if (color != b.color()) {
194 b.setColor(color);
195 m_chart->setBackgroundBrush(b);
196 emit backgroundColorChanged();
197 }
198 }
199
200 QColor DeclarativeChart::backgroundColor()
201 {
202 return m_chart->backgroundBrush().color();
184 }
203 }
185
204
186 int DeclarativeChart::count()
205 int DeclarativeChart::count()
@@ -34,18 +34,19 class DeclarativeChart : public QDeclarativeItem
34 //class DeclarativeChart : public QQuickPaintedItem, public Chart
34 //class DeclarativeChart : public QQuickPaintedItem, public Chart
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 Q_PROPERTY(Theme theme READ theme WRITE setTheme)
37 Q_PROPERTY(Theme theme READ theme WRITE setTheme NOTIFY themeChanged)
38 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions)
38 Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions NOTIFY animationOptionsChanged)
39 Q_PROPERTY(QString title READ title WRITE setTitle)
39 Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
40 Q_PROPERTY(Legend legend READ legend WRITE setLegend)
41 Q_PROPERTY(QAxis *axisX READ axisX)
40 Q_PROPERTY(QAxis *axisX READ axisX)
42 Q_PROPERTY(QAxis *axisY READ axisY)
41 Q_PROPERTY(QAxis *axisY READ axisY)
42 Q_PROPERTY(QLegend *legend READ legend)
43 // TODO: how to define axis labels? This is not very convenient
43 // TODO: how to define axis labels? This is not very convenient
44 Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels)
44 Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels NOTIFY axisLabelsChanged)
45 Q_PROPERTY(int count READ count)
45 Q_PROPERTY(int count READ count)
46 Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged)
47 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
46 Q_ENUMS(Animation)
48 Q_ENUMS(Animation)
47 Q_ENUMS(Theme)
49 Q_ENUMS(Theme)
48 Q_ENUMS(Legend)
49 Q_ENUMS(SeriesType)
50 Q_ENUMS(SeriesType)
50
51
51 public:
52 public:
@@ -67,14 +68,6 public:
67 AllAnimations = 0x3
68 AllAnimations = 0x3
68 };
69 };
69
70
70 enum Legend {
71 LegendDisabled = 0,
72 LegendTop,
73 LegendBottom,
74 LegendLeft,
75 LegendRight
76 };
77
78 enum SeriesType {
71 enum SeriesType {
79 SeriesTypeLine,
72 SeriesTypeLine,
80 SeriesTypeArea,
73 SeriesTypeArea,
@@ -102,24 +95,34 public:
102 DeclarativeChart::Theme theme();
95 DeclarativeChart::Theme theme();
103 void setAnimationOptions(DeclarativeChart::Animation animations);
96 void setAnimationOptions(DeclarativeChart::Animation animations);
104 DeclarativeChart::Animation animationOptions();
97 DeclarativeChart::Animation animationOptions();
105 void setTitle(QString title) {m_chart->setTitle(title);}
98 void setTitle(QString title);
106 QString title() { return m_chart->title();}
99 QString title();
107 void setLegend(DeclarativeChart::Legend legend);
108 DeclarativeChart::Legend legend();
109 QAxis *axisX();
100 QAxis *axisX();
110 QAxis *axisY();
101 QAxis *axisY();
102 QLegend *legend();
111 QVariantList axisXLabels();
103 QVariantList axisXLabels();
112 void setAxisXLabels(QVariantList list);
104 void setAxisXLabels(QVariantList list);
105 void setTitleColor(QColor color);
106 QColor titleColor();
107 void setBackgroundColor(QColor color);
108 QColor backgroundColor();
113 int count();
109 int count();
114 Q_INVOKABLE QAbstractSeries *series(int index);
110 Q_INVOKABLE QAbstractSeries *series(int index);
115 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
111 Q_INVOKABLE QAbstractSeries *series(QString seriesName);
116 Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = "");
112 Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = "");
117
113
114 Q_SIGNALS:
115 void themeChanged();
116 void animationOptionsChanged();
117 void titleChanged();
118 void axisLabelsChanged();
119 void titleColorChanged();
120 void backgroundColorChanged();
121
118 public:
122 public:
119 // Extending QChart with DeclarativeChart is not possible because QObject does not support
123 // Extending QChart with DeclarativeChart is not possible because QObject does not support
120 // multi inheritance, so we now have a QChart as a member instead
124 // multi inheritance, so we now have a QChart as a member instead
121 QChart *m_chart;
125 QChart *m_chart;
122 Legend m_legend;
123 };
126 };
124
127
125 QTCOMMERCIALCHART_END_NAMESPACE
128 QTCOMMERCIALCHART_END_NAMESPACE
@@ -67,6 +67,8 public:
67 qmlRegisterType<QHBarModelMapper>(uri, 1, 0, "HBarModelMapper");
67 qmlRegisterType<QHBarModelMapper>(uri, 1, 0, "HBarModelMapper");
68 qmlRegisterType<QVBarModelMapper>(uri, 1, 0, "VBarModelMapper");
68 qmlRegisterType<QVBarModelMapper>(uri, 1, 0, "VBarModelMapper");
69
69
70 qmlRegisterUncreatableType<QLegend>(uri, 1, 0, "Legend",
71 QLatin1String("Trying to create uncreatable: Legend."));
70 qmlRegisterUncreatableType<QScatterSeries>(uri, 1, 0, "QScatterSeries",
72 qmlRegisterUncreatableType<QScatterSeries>(uri, 1, 0, "QScatterSeries",
71 QLatin1String("Trying to create uncreatable: QScatterSeries."));
73 QLatin1String("Trying to create uncreatable: QScatterSeries."));
72 qmlRegisterUncreatableType<QPieSeries>(uri, 1, 0, "QPieSeries",
74 qmlRegisterUncreatableType<QPieSeries>(uri, 1, 0, "QPieSeries",
@@ -323,7 +323,7 void ChartPresenter::updateLayout()
323 // Reserve some space for legend
323 // Reserve some space for legend
324 switch (legend->alignment()) {
324 switch (legend->alignment()) {
325
325
326 case QLegend::AlignmentTop: {
326 case Qt::AlignTop: {
327 int ledgendSize = legend->minHeight();
327 int ledgendSize = legend->minHeight();
328 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
328 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
329 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
329 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
@@ -331,7 +331,7 void ChartPresenter::updateLayout()
331 titlePadding = m_marginTiny + m_marginTiny;
331 titlePadding = m_marginTiny + m_marginTiny;
332 break;
332 break;
333 }
333 }
334 case QLegend::AlignmentBottom: {
334 case Qt::AlignBottom: {
335 int ledgendSize = legend->minHeight();
335 int ledgendSize = legend->minHeight();
336 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
336 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
337 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
337 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
@@ -339,7 +339,7 void ChartPresenter::updateLayout()
339 titlePadding = m_chartMargins.top()/2;
339 titlePadding = m_chartMargins.top()/2;
340 break;
340 break;
341 }
341 }
342 case QLegend::AlignmentLeft: {
342 case Qt::AlignLeft: {
343 int ledgendSize = legend->minWidth();
343 int ledgendSize = legend->minWidth();
344 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
344 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
345 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
345 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
@@ -347,7 +347,7 void ChartPresenter::updateLayout()
347 titlePadding = m_chartMargins.top()/2;
347 titlePadding = m_chartMargins.top()/2;
348 break;
348 break;
349 }
349 }
350 case QLegend::AlignmentRight: {
350 case Qt::AlignRight: {
351 int ledgendSize = legend->minWidth();
351 int ledgendSize = legend->minWidth();
352 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
352 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
353 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
353 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
@@ -62,14 +62,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
62 */
62 */
63
63
64 /*!
64 /*!
65 \enum QLegend::Alignment
65 \fn void QLegend::alignmentChanged()
66
66 Emitted when the alignment of the legend changes.
67 This enum describes the possible position for legend inside chart.
68
69 \value AlignmentTop
70 \value AlignmentBottom
71 \value AlignmentLeft
72 \value AlignmentRight
73 */
67 */
74
68
75 /*!
69 /*!
@@ -168,21 +162,30 QPen QLegend::pen() const
168 }
162 }
169
163
170 /*!
164 /*!
171 Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart.
165 \property QLegend::alignment
166 \brief The alignment of the legend.
167 */
168
169 /*!
170 Sets the \a alignment for legend. Legend paints on the defined position in chart. The following alignments are
171 supported: Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result
172 is undefined.
173
172 \sa QLegend::Alignment
174 \sa QLegend::Alignment
173 */
175 */
174 void QLegend::setAlignment(QLegend::Alignments alignment)
176 void QLegend::setAlignment(Qt::Alignment alignment)
175 {
177 {
176 if(d_ptr->m_alignment!=alignment) {
178 if(d_ptr->m_alignment!=alignment) {
177 d_ptr->m_alignment = alignment;
179 d_ptr->m_alignment = alignment;
178 d_ptr->updateLayout();
180 d_ptr->updateLayout();
181 alignmentChanged();
179 }
182 }
180 }
183 }
181
184
182 /*!
185 /*!
183 Returns the preferred layout for legend
186 Returns the preferred layout for legend
184 */
187 */
185 QLegend::Alignments QLegend::alignment() const
188 Qt::Alignment QLegend::alignment() const
186 {
189 {
187 return d_ptr->m_alignment;
190 return d_ptr->m_alignment;
188 }
191 }
@@ -297,7 +300,7 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend
297 m_presenter(presenter),
300 m_presenter(presenter),
298 m_chart(chart),
301 m_chart(chart),
299 m_markers(new QGraphicsItemGroup(q)),
302 m_markers(new QGraphicsItemGroup(q)),
300 m_alignment(QLegend::AlignmentTop),
303 m_alignment(Qt::AlignTop),
301 m_offsetX(0),
304 m_offsetX(0),
302 m_offsetY(0),
305 m_offsetY(0),
303 m_minWidth(0),
306 m_minWidth(0),
@@ -320,8 +323,8 void QLegendPrivate::setOffset(qreal x, qreal y)
320
323
321 switch(m_alignment) {
324 switch(m_alignment) {
322
325
323 case QLegend::AlignmentTop:
326 case Qt::AlignTop:
324 case QLegend::AlignmentBottom: {
327 case Qt::AlignBottom: {
325 if(m_width<=m_rect.width()) return;
328 if(m_width<=m_rect.width()) return;
326
329
327 if (x != m_offsetX) {
330 if (x != m_offsetX) {
@@ -330,8 +333,8 void QLegendPrivate::setOffset(qreal x, qreal y)
330 }
333 }
331 break;
334 break;
332 }
335 }
333 case QLegend::AlignmentLeft:
336 case Qt::AlignLeft:
334 case QLegend::AlignmentRight: {
337 case Qt::AlignRight: {
335
338
336 if(m_height<=m_rect.height()) return;
339 if(m_height<=m_rect.height()) return;
337
340
@@ -357,8 +360,8 void QLegendPrivate::updateLayout()
357
360
358 switch(m_alignment) {
361 switch(m_alignment) {
359
362
360 case QLegend::AlignmentTop:
363 case Qt::AlignTop:
361 case QLegend::AlignmentBottom: {
364 case Qt::AlignBottom: {
362 QPointF point = m_rect.topLeft();
365 QPointF point = m_rect.topLeft();
363 m_width = 0;
366 m_width = 0;
364 foreach (QGraphicsItem *item, items) {
367 foreach (QGraphicsItem *item, items) {
@@ -379,8 +382,8 void QLegendPrivate::updateLayout()
379 m_height=m_minHeight;
382 m_height=m_minHeight;
380 }
383 }
381 break;
384 break;
382 case QLegend::AlignmentLeft:
385 case Qt::AlignLeft:
383 case QLegend::AlignmentRight: {
386 case Qt::AlignRight: {
384 QPointF point = m_rect.topLeft();
387 QPointF point = m_rect.topLeft();
385 m_height = 0;
388 m_height = 0;
386 foreach (QGraphicsItem *item, items) {
389 foreach (QGraphicsItem *item, items) {
@@ -43,17 +43,7 class QLegendPrivate;
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46 public:
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
47
48 // We only support these alignments (for now)
49 enum Alignment {
50 AlignmentTop = Qt::AlignTop,
51 AlignmentBottom = Qt::AlignBottom,
52 AlignmentLeft = Qt::AlignLeft,
53 AlignmentRight = Qt::AlignRight
54 };
55
56 Q_DECLARE_FLAGS(Alignments, Alignment)
57
47
58 private:
48 private:
59 explicit QLegend(QChart *chart);
49 explicit QLegend(QChart *chart);
@@ -70,8 +60,8 public:
70 void setPen(const QPen &pen);
60 void setPen(const QPen &pen);
71 QPen pen() const;
61 QPen pen() const;
72
62
73 void setAlignment(QLegend::Alignments alignment);
63 void setAlignment(Qt::Alignment alignment);
74 QLegend::Alignments alignment() const;
64 Qt::Alignment alignment() const;
75
65
76 void detachFromChart();
66 void detachFromChart();
77 void attachToChart();
67 void attachToChart();
@@ -91,6 +81,9 protected:
91 void hideEvent(QHideEvent *event);
81 void hideEvent(QHideEvent *event);
92 void showEvent(QShowEvent *event);
82 void showEvent(QShowEvent *event);
93
83
84 Q_SIGNALS:
85 void alignmentChanged();
86
94 private:
87 private:
95 QScopedPointer<QLegendPrivate> d_ptr;
88 QScopedPointer<QLegendPrivate> d_ptr;
96 Q_DISABLE_COPY(QLegend);
89 Q_DISABLE_COPY(QLegend);
@@ -60,7 +60,7 private:
60 ChartPresenter *m_presenter;
60 ChartPresenter *m_presenter;
61 QChart* m_chart;
61 QChart* m_chart;
62 QGraphicsItemGroup* m_markers;
62 QGraphicsItemGroup* m_markers;
63 QLegend::Alignments m_alignment;
63 Qt::Alignment m_alignment;
64 QBrush m_brush;
64 QBrush m_brush;
65 QPen m_pen;
65 QPen m_pen;
66 QRectF m_rect;
66 QRectF m_rect;
@@ -21,16 +21,14
21 import QtQuick 1.0
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
22 import QtCommercial.Chart 1.0
23
23
24 Rectangle {
25 anchors.fill: parent
26
27 ChartView {
24 ChartView {
28 title: "spline series"
25 id: chartView
26 title: "chart"
29 anchors.fill: parent
27 anchors.fill: parent
28 property variant series: chartView
30
29
31 SplineSeries {
30 LineSeries {
32 id: daSeries
31 name: "line"
33 name: "spline 1"
34 XyPoint { x: 0; y: 0 }
32 XyPoint { x: 0; y: 0 }
35 XyPoint { x: 1.1; y: 2.1 }
33 XyPoint { x: 1.1; y: 2.1 }
36 XyPoint { x: 1.9; y: 3.3 }
34 XyPoint { x: 1.9; y: 3.3 }
@@ -39,15 +37,4 Rectangle {
39 XyPoint { x: 3.4; y: 3.0 }
37 XyPoint { x: 3.4; y: 3.0 }
40 XyPoint { x: 4.1; y: 3.3 }
38 XyPoint { x: 4.1; y: 3.3 }
41 }
39 }
42
43 SplineSeries {
44 name: "spline 2"
45 XyPoint { x: 1.1; y: 1.1 }
46 XyPoint { x: 1.9; y: 2.3 }
47 XyPoint { x: 2.1; y: 1.1 }
48 XyPoint { x: 2.9; y: 3.9 }
49 XyPoint { x: 3.4; y: 2.0 }
50 XyPoint { x: 4.1; y: 2.3 }
51 }
52 }
53 }
40 }
@@ -26,7 +26,7 Rectangle {
26 width: parent.width
26 width: parent.width
27 height: parent.height
27 height: parent.height
28 property int viewNumber: 0
28 property int viewNumber: 0
29 property int viewCount: 9
29 property int viewCount: 10
30 property variant colors: ["#637D74", "#403D3A", "#8C3B3B", "#AB6937", "#D4A960"]
30 property variant colors: ["#637D74", "#403D3A", "#8C3B3B", "#AB6937", "#D4A960"]
31 property int colorIndex: 0
31 property int colorIndex: 0
32
32
@@ -38,30 +38,33 Rectangle {
38
38
39 onViewNumberChanged: {
39 onViewNumberChanged: {
40 if (viewNumber == 0) {
40 if (viewNumber == 0) {
41 chartLoader.source = "Chart.qml";
42 editorLoader.source = "ChartEditor.qml";
43 } else if (viewNumber == 1) {
41 chartLoader.source = "PieChart.qml";
44 chartLoader.source = "PieChart.qml";
42 editorLoader.source = "PieEditor.qml";
45 editorLoader.source = "PieEditor.qml";
43 } else if (viewNumber == 1) {
46 } else if (viewNumber == 2) {
44 chartLoader.source = "LineChart.qml";
47 chartLoader.source = "LineChart.qml";
45 editorLoader.source = "LineEditor.qml";
48 editorLoader.source = "LineEditor.qml";
46 } else if (viewNumber == 2) {
49 } else if (viewNumber == 3) {
47 chartLoader.source = "SplineChart.qml";
50 chartLoader.source = "SplineChart.qml";
48 editorLoader.source = "LineEditor.qml";
51 editorLoader.source = "LineEditor.qml";
49 } else if (viewNumber == 3) {
52 } else if (viewNumber == 4) {
50 chartLoader.source = "ScatterChart.qml";
53 chartLoader.source = "ScatterChart.qml";
51 editorLoader.source = "ScatterEditor.qml";
54 editorLoader.source = "ScatterEditor.qml";
52 } else if (viewNumber == 4) {
55 } else if (viewNumber == 5) {
53 chartLoader.source = "AreaChart.qml";
56 chartLoader.source = "AreaChart.qml";
54 editorLoader.source = "AreaEditor.qml";
57 editorLoader.source = "AreaEditor.qml";
55 } else if (viewNumber == 5) {
58 } else if (viewNumber == 6) {
56 chartLoader.source = "BarChart.qml";
59 chartLoader.source = "BarChart.qml";
57 editorLoader.source = "BarEditor.qml";
60 editorLoader.source = "BarEditor.qml";
58 } else if (viewNumber == 6) {
61 } else if (viewNumber == 7) {
59 chartLoader.source = "GroupedBarChart.qml";
62 chartLoader.source = "GroupedBarChart.qml";
60 editorLoader.source = "BarEditor.qml";
63 editorLoader.source = "BarEditor.qml";
61 } else if (viewNumber == 7) {
64 } else if (viewNumber == 8) {
62 chartLoader.source = "StackedBarChart.qml";
65 chartLoader.source = "StackedBarChart.qml";
63 editorLoader.source = "BarEditor.qml";
66 editorLoader.source = "BarEditor.qml";
64 } else if (viewNumber == 8) {
67 } else if (viewNumber == 9) {
65 chartLoader.source = "PercentBarChart.qml";
68 chartLoader.source = "PercentBarChart.qml";
66 editorLoader.source = "BarEditor.qml";
69 editorLoader.source = "BarEditor.qml";
67 } else {
70 } else {
@@ -80,7 +83,7 Rectangle {
80 id: chartLoader
83 id: chartLoader
81 width: main.width - editorLoader.width
84 width: main.width - editorLoader.width
82 height: parent.height
85 height: parent.height
83 source: "PieChart.qml"
86 source: "Chart.qml"
84 onStatusChanged: {
87 onStatusChanged: {
85 console.log("chartLoader.status: " + status + " " + source);
88 console.log("chartLoader.status: " + status + " " + source);
86 if (status == Loader.Ready && editorLoader.status == Loader.Ready)
89 if (status == Loader.Ready && editorLoader.status == Loader.Ready)
@@ -92,7 +95,7 Rectangle {
92 id: editorLoader
95 id: editorLoader
93 width: 280
96 width: 280
94 height: parent.height
97 height: parent.height
95 source: "PieEditor.qml"
98 source: "ChartEditor.qml"
96 onStatusChanged: {
99 onStatusChanged: {
97 console.log("editorLoader.status: " + status + " " + source);
100 console.log("editorLoader.status: " + status + " " + source);
98 if (status == Loader.Ready && chartLoader.status == Loader.Ready)
101 if (status == Loader.Ready && chartLoader.status == Loader.Ready)
@@ -17,5 +17,7
17 <file>qml/qmlchartproperties/GroupedBarChart.qml</file>
17 <file>qml/qmlchartproperties/GroupedBarChart.qml</file>
18 <file>qml/qmlchartproperties/StackedBarChart.qml</file>
18 <file>qml/qmlchartproperties/StackedBarChart.qml</file>
19 <file>qml/qmlchartproperties/PercentBarChart.qml</file>
19 <file>qml/qmlchartproperties/PercentBarChart.qml</file>
20 <file>qml/qmlchartproperties/Chart.qml</file>
21 <file>qml/qmlchartproperties/ChartEditor.qml</file>
20 </qresource>
22 </qresource>
21 </RCC>
23 </RCC>
General Comments 0
You need to be logged in to leave comments. Login now