diff --git a/example/colorlinechart/main.cpp b/example/colorlinechart/main.cpp index f0335a0..21a4fb6 100644 --- a/example/colorlinechart/main.cpp +++ b/example/colorlinechart/main.cpp @@ -36,7 +36,12 @@ int main(int argc, char *argv[]) chartView->setTitle("Custom color line chart example"); chartView->addSeries(series0); chartView->addSeries(series1); - chartView->setBackground(Qt::blue,Qt::yellow,QChart::HorizonatlGradientOrientation); + + QLinearGradient backgroundGradient; + backgroundGradient.setColorAt(0.0, Qt::blue); + backgroundGradient.setColorAt(1.0, Qt::yellow); + backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); + chartView->setChartBackgroundBrush(backgroundGradient); window.setCentralWidget(chartView); window.resize(400, 300); diff --git a/src/qchart.cpp b/src/qchart.cpp index 492afc1..58c0739 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -71,7 +71,7 @@ void QChart::addSeries(QChartSeries* series) domain.m_maxY = qMax(domain.m_maxY,y); } - XYLineChartItem* item = new XYLineChartItem(xyseries,this); + XYLineChartItem* item = new XYLineChartItem(xyseries,0,this); m_chartItems << item; // TODO: @@ -240,12 +240,12 @@ void QChart::setBackground(const QColor& startColor, const QColor& endColor, Gra m_bacgroundOrinetation = orientation; m_backgroundGradient.setColorAt(0.0, startColor); m_backgroundGradient.setColorAt(1.0, endColor); - m_backgroundGradient.setStart(0,0); + m_backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); if(orientation == VerticalGradientOrientation){ - m_backgroundGradient.setFinalStop(0,m_rect.height()); + m_backgroundGradient.setFinalStop(0,1); }else{ - m_backgroundGradient.setFinalStop(m_rect.width(),0); + m_backgroundGradient.setFinalStop(1,0); } m_backgroundItem->setBrush(m_backgroundGradient); @@ -253,6 +253,30 @@ void QChart::setBackground(const QColor& startColor, const QColor& endColor, Gra m_backgroundItem->update(); } +void QChart::setChartBackgroundBrush(const QBrush& brush) +{ + + if(!m_backgroundItem){ + m_backgroundItem = new QGraphicsRectItem(this); + m_backgroundItem->setZValue(-1); + } + + m_backgroundItem->setBrush(brush); + m_backgroundItem->update(); +} + +void QChart::setChartBackgroundPen(const QPen& pen) +{ + + if(!m_backgroundItem){ + m_backgroundItem = new QGraphicsRectItem(this); + m_backgroundItem->setZValue(-1); + } + + m_backgroundItem->setPen(pen); + m_backgroundItem->update(); +} + void QChart::setTitle(const QString& title,const QFont& font) { if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); @@ -390,11 +414,6 @@ void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) //recalculate background gradient if (m_backgroundItem) { m_backgroundItem->setRect(rect); - if (m_bacgroundOrinetation == HorizonatlGradientOrientation) - m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(), 0); - else - m_backgroundGradient.setFinalStop(0, m_backgroundItem->rect().height()); - m_backgroundItem->setBrush(m_backgroundGradient); } // resize and reposition childs diff --git a/src/qchart.h b/src/qchart.h index 3e2ee69..5a1daee 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -63,6 +63,9 @@ public: void setTitle(const QString& title,const QFont& font = QFont()); void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation); + void setChartBackgroundBrush(const QBrush& brush); + void setChartBackgroundPen(const QPen& pen); + void zoomInToRect(const QRectF& rectangle); void zoomIn(); void zoomOut(); diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 426eb7f..753c4b3 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -74,4 +74,12 @@ void QChartView::setBackground(const QColor& startColor, const QColor& endColor, m_chart->setBackground(startColor,endColor,orientation); } +void QChartView::setChartBackgroundBrush(const QBrush& brush) +{ + m_chart->setChartBackgroundBrush(brush); +} +void QChartView::setChartBackgroundPen(const QPen& pen) +{ + m_chart->setChartBackgroundPen(pen); +} QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h index 0d0d8d2..1a6d6fc 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -27,7 +27,14 @@ public: int margin() const; void setTitle(const QString& title); + + //Obsolete void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, QChart::GradientOrientation orientation = QChart::VerticalGradientOrientation); + + + void setChartBackgroundBrush(const QBrush& brush); + void setChartBackgroundPen(const QPen& pen); + void zoomInToRect(const QRect& rectangle); void zoomIn(); void zoomOut();