diff --git a/demos/qmlchart/qml/qmlchart/View1.qml b/demos/qmlchart/qml/qmlchart/View1.qml index 2f179a8..1332bf6 100644 --- a/demos/qmlchart/qml/qmlchart/View1.qml +++ b/demos/qmlchart/qml/qmlchart/View1.qml @@ -28,6 +28,7 @@ Rectangle { title: "Car brand shares in Finland" anchors.fill: parent theme: Chart.ChartThemeLight + legend: Chart.LegendBottom PieSeries { horizontalPosition: 0.2 diff --git a/demos/qmlchart/qml/qmlchart/View3.qml b/demos/qmlchart/qml/qmlchart/View3.qml index 32092cb..faa869c 100644 --- a/demos/qmlchart/qml/qmlchart/View3.qml +++ b/demos/qmlchart/qml/qmlchart/View3.qml @@ -28,6 +28,7 @@ Rectangle { title: "NHL All-Star Team Players" anchors.fill: parent theme: Chart.ChartThemeHighContrast + legend: Chart.LegendTop AreaSeries { name: "Finnish" diff --git a/qmlplugin/declarativechart.cpp b/qmlplugin/declarativechart.cpp index 2506707..633bb52 100644 --- a/qmlplugin/declarativechart.cpp +++ b/qmlplugin/declarativechart.cpp @@ -25,7 +25,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) : QDeclarativeItem(parent), - m_chart(new QChart(this)) + m_chart(new QChart(this)), + m_legend(LegendDisabled) { m_chart->setAnimationOptions(QChart::SeriesAnimations); setFlag(QGraphicsItem::ItemHasNoContents, false); @@ -73,6 +74,42 @@ QChart::AnimationOption DeclarativeChart::animationOptions() return QChart::NoAnimation; } +void DeclarativeChart::setLegend(ChartLegend legend) +{ + if (legend != m_legend) { + m_legend = legend; + switch (m_legend) { + case LegendDisabled: + m_chart->legend()->setVisible(false); + break; + case LegendTop: + m_chart->legend()->setVisible(true); + m_chart->legend()->setAlignment(QLegend::AlignmentTop); + break; + case LegendBottom: + m_chart->legend()->setVisible(true); + m_chart->legend()->setAlignment(QLegend::AlignmentBottom); + break; + case LegendLeft: + m_chart->legend()->setVisible(true); + m_chart->legend()->setAlignment(QLegend::AlignmentLeft); + break; + case LegendRight: + m_chart->legend()->setVisible(true); + m_chart->legend()->setAlignment(QLegend::AlignmentRight); + break; + default: + m_chart->legend()->setVisible(false); + break; + } + } +} + +DeclarativeChart::ChartLegend DeclarativeChart::legend() +{ + return m_legend; +} + #include "moc_declarativechart.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/qmlplugin/declarativechart.h b/qmlplugin/declarativechart.h index 45ad501..9229056 100644 --- a/qmlplugin/declarativechart.h +++ b/qmlplugin/declarativechart.h @@ -36,6 +36,17 @@ class DeclarativeChart : public QDeclarativeItem Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme) Q_PROPERTY(QChart::AnimationOption animationOptions READ animationOptions WRITE setAnimationOptions) Q_PROPERTY(QString title READ title WRITE setTitle) + Q_PROPERTY(ChartLegend legend READ legend WRITE setLegend) + Q_ENUMS(ChartLegend) + +public: + enum ChartLegend { + LegendDisabled = 0, + LegendTop, + LegendBottom, + LegendLeft, + LegendRight + }; public: DeclarativeChart(QDeclarativeItem *parent = 0); @@ -52,11 +63,14 @@ public: QChart::AnimationOption animationOptions(); void setTitle(QString title) {m_chart->setTitle(title);} QString title() { return m_chart->title();} + void setLegend(ChartLegend legend); + ChartLegend legend(); public: // Extending QChart with DeclarativeChart is not possible because QObject does not support // multi inheritance, so we now have a QChart as a member instead QChart *m_chart; + ChartLegend m_legend; }; QTCOMMERCIALCHART_END_NAMESPACE