diff --git a/example/linechart/main.cpp b/example/linechart/main.cpp index 2e328fe..5aac987 100644 --- a/example/linechart/main.cpp +++ b/example/linechart/main.cpp @@ -33,6 +33,7 @@ int main(int argc, char *argv[]) QChartView* chartView = new QChartView(&window); chartView->setRenderHint(QPainter::Antialiasing); + chartView->setTitle("Example: sin & cos functions"); chartView->addSeries(series0); chartView->addSeries(series1); diff --git a/src/qchart.cpp b/src/qchart.cpp index ba4310c..fe196cd 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -17,9 +17,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), - m_background(0), - m_title(0), - m_axisX(new AxisItem(AxisItem::X_AXIS,this)), + m_backgroundItem(0), + m_titleItem(0), + m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)), m_plotDataIndex(0), m_marginSize(0) { @@ -28,9 +28,9 @@ QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), PlotDomain domain; m_plotDomainList<boundingRect().center(); + m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); } //recalculate background gradient - if(m_background){ - m_background->setRect(rect); + if(m_backgroundItem){ + m_backgroundItem->setRect(rect); if(m_bacgroundOrinetation==HorizonatlGradientOrientation) - m_backgroundGradient.setFinalStop(m_background->rect().width(),0); + m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0); else - m_backgroundGradient.setFinalStop(0,m_background->rect().height()); + m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height()); - m_background->setBrush(m_backgroundGradient); + m_backgroundItem->setBrush(m_backgroundGradient); } //resize elements @@ -197,9 +199,9 @@ void QChart::setSize(const QSize& size) void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation) { - if(!m_background){ - m_background = new QGraphicsRectItem(this); - m_background->setZValue(-1); + if(!m_backgroundItem){ + m_backgroundItem = new QGraphicsRectItem(this); + m_backgroundItem->setZValue(-1); } m_bacgroundOrinetation = orientation; @@ -213,15 +215,16 @@ void QChart::setBackground(const QColor& startColor, const QColor& endColor, Gra m_backgroundGradient.setFinalStop(m_rect.width(),0); } - m_background->setBrush(m_backgroundGradient); - m_background->setPen(Qt::NoPen); - m_background->update(); + m_backgroundItem->setBrush(m_backgroundGradient); + m_backgroundItem->setPen(Qt::NoPen); + m_backgroundItem->update(); } -void QChart::setTitle(const QString& title) +void QChart::setTitle(const QString& title,const QFont& font) { - if(!m_title) m_title = new QGraphicsTextItem(this); - m_title->setPlainText(title); + if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); + m_titleItem->setPlainText(title); + m_titleItem->setFont(font); } int QChart::margin() const @@ -290,9 +293,9 @@ void QChart::setTheme(QChart::ChartThemeId theme) break; } - if(m_background){ - m_background->setBrush(m_backgroundGradient); - m_background->setPen(Qt::NoPen); + if(m_backgroundItem){ + m_backgroundItem->setBrush(m_backgroundGradient); + m_backgroundItem->setPen(Qt::NoPen); } foreach(QChartSeries* series, m_chartSeries) { @@ -370,11 +373,11 @@ void QChart::zoomOut() void QChart::setAxisX(const QChartAxis& axis) { - setAxis(m_axisX,axis); + setAxis(m_axisXItem,axis); } void QChart::setAxisY(const QChartAxis& axis) { - setAxis(m_axisY.at(0),axis); + setAxis(m_axisYItem.at(0),axis); } void QChart::setAxisY(const QList& axis) diff --git a/src/qchart.h b/src/qchart.h index 0adf5f6..7f897d3 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -5,6 +5,7 @@ #include #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -59,7 +60,7 @@ public: int margin() const; void setTheme(QChart::ChartThemeId theme); - void setTitle(const QString& title); + void setTitle(const QString& title,const QFont& font = QFont()); void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation); void zoomInToRect(const QRect& rectangle); @@ -82,12 +83,12 @@ private: QColor nextColor(); Q_DISABLE_COPY(QChart) - QGraphicsRectItem* m_background; + QGraphicsRectItem* m_backgroundItem; QLinearGradient m_backgroundGradient; GradientOrientation m_bacgroundOrinetation; - QGraphicsTextItem* m_title; - AxisItem* m_axisX; - QList m_axisY; + QGraphicsTextItem* m_titleItem; + AxisItem* m_axisXItem; + QList m_axisYItem; QRect m_rect; QList m_chartSeries; QVector m_plotDomainList;