From 84e7b4b4f9e06e7a43fc226382910e858dd2cfd7 2012-01-26 15:27:37 From: Michal Klocek Date: 2012-01-26 15:27:37 Subject: [PATCH] Add gradient bacground support * support for vertical horizonstal gradieent * user defined colorts * minor. fix axis geomoetry --- diff --git a/example/colorlinechart/main.cpp b/example/colorlinechart/main.cpp index 2a9e30c..c5d44d1 100644 --- a/example/colorlinechart/main.cpp +++ b/example/colorlinechart/main.cpp @@ -32,9 +32,10 @@ int main(int argc, char *argv[]) } QChartView* chartView = new QChartView(&window); + chartView->setRenderHint(QPainter::Antialiasing); chartView->addSeries(series0); chartView->addSeries(series1); - //chartView->setBackgroundColor(Qt::yellow); + chartView->setBackground(Qt::blue,Qt::yellow,QChart::HorizonatlGradientOrientation); window.setCentralWidget(chartView); window.resize(400, 300); diff --git a/src/axisitem.cpp b/src/axisitem.cpp index 3aea090..8de869c 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -120,13 +120,9 @@ void AxisItem::createItems() int x = i * deltaX + m_rect.left(); - //last grid outside chart rect - if(i==0) x--; - if(i==m_ticks) x++; - qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks); - m_grid<boundingRect().center(); @@ -144,14 +140,10 @@ void AxisItem::createItems() int y = j * -deltaY + m_rect.bottom(); - //last grid outside chart rect - if(j==0) y++; - if(j==m_ticks) y--; - qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() / m_ticks); - m_grid<boundingRect().center(); diff --git a/src/qchart.cpp b/src/qchart.cpp index 0a731dc..ba4310c 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -17,15 +17,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), - m_background(new QGraphicsRectItem()), - m_title(new QGraphicsTextItem(this)), + m_background(0), + m_title(0), m_axisX(new AxisItem(AxisItem::X_AXIS,this)), m_plotDataIndex(0), m_marginSize(0) { // TODO: the default theme? //setTheme(QChart::ChartThemeVanilla); - m_backgroundGradient.setColorAt(0.0, Qt::white); PlotDomain domain; m_plotDomainList<setRect(rect); - m_backgroundGradient.setFinalStop(0,m_background->rect().height()); - m_background->setBrush(m_backgroundGradient); - m_background->setPen(Qt::NoPen); + if(m_background){ + m_background->setRect(rect); + if(m_bacgroundOrinetation==HorizonatlGradientOrientation) + m_backgroundGradient.setFinalStop(m_background->rect().width(),0); + else + m_backgroundGradient.setFinalStop(0,m_background->rect().height()); + + m_background->setBrush(m_backgroundGradient); + } //resize elements foreach (ChartItem* item ,m_chartItems) { @@ -186,10 +194,25 @@ void QChart::setSize(const QSize& size) update(); } -void QChart::setBackgroundColor(const QColor& color) +void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation) { - m_backgroundGradient.setColorAt( 0.0, Qt::white); - m_backgroundGradient.setColorAt( 1.0, color); + + if(!m_background){ + m_background = new QGraphicsRectItem(this); + m_background->setZValue(-1); + } + + m_bacgroundOrinetation = orientation; + m_backgroundGradient.setColorAt( 0.0, startColor); + m_backgroundGradient.setColorAt( 1.0, endColor); + m_backgroundGradient.setStart(0,0); + + if(orientation == VerticalGradientOrientation){ + m_backgroundGradient.setFinalStop(0,m_rect.height()); + }else{ + m_backgroundGradient.setFinalStop(m_rect.width(),0); + } + m_background->setBrush(m_backgroundGradient); m_background->setPen(Qt::NoPen); m_background->update(); @@ -197,6 +220,7 @@ void QChart::setBackgroundColor(const QColor& color) void QChart::setTitle(const QString& title) { + if(!m_title) m_title = new QGraphicsTextItem(this); m_title->setPlainText(title); } @@ -266,8 +290,10 @@ void QChart::setTheme(QChart::ChartThemeId theme) break; } + if(m_background){ m_background->setBrush(m_backgroundGradient); m_background->setPen(Qt::NoPen); + } foreach(QChartSeries* series, m_chartSeries) { // TODO: other series interested on themes? diff --git a/src/qchart.h b/src/qchart.h index 4708336..0adf5f6 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -27,6 +27,10 @@ class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject { Q_OBJECT public: + enum GradientOrientation { + HorizonatlGradientOrientation, + VerticalGradientOrientation + }; enum ChartThemeId { /*! The default theme follows the GUI style of the Operating System */ ChartThemeDefault = 0, @@ -56,7 +60,7 @@ public: void setTheme(QChart::ChartThemeId theme); void setTitle(const QString& title); - void setBackgroundColor(const QColor& color); + void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, GradientOrientation orientation = VerticalGradientOrientation); void zoomInToRect(const QRect& rectangle); void zoomIn(); @@ -80,6 +84,7 @@ private: Q_DISABLE_COPY(QChart) QGraphicsRectItem* m_background; QLinearGradient m_backgroundGradient; + GradientOrientation m_bacgroundOrinetation; QGraphicsTextItem* m_title; AxisItem* m_axisX; QList m_axisY; diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 5ec3e58..0c2dbaf 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -69,9 +69,9 @@ void QChartView::setTitle(const QString& title) m_chart->setTitle(title); } -void QChartView::setBackgroundColor(const QColor& color) +void QChartView::setBackground(const QColor& startColor, const QColor& endColor, QChart::GradientOrientation orientation) { - m_chart->setBackgroundColor(color); + m_chart->setBackground(startColor,endColor,orientation); } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h index 68cde12..0d0d8d2 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -3,6 +3,7 @@ #include "qchartglobal.h" #include "qchartseries.h" +#include "qchart.h" #include class QGraphicsScene; @@ -26,7 +27,7 @@ public: int margin() const; void setTitle(const QString& title); - void setBackgroundColor(const QColor& color); + void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, QChart::GradientOrientation orientation = QChart::VerticalGradientOrientation); void zoomInToRect(const QRect& rectangle); void zoomIn(); void zoomOut(); diff --git a/src/xylinechart/qxychartseries.cpp b/src/xylinechart/qxychartseries.cpp index 02ad4f6..564ba07 100644 --- a/src/xylinechart/qxychartseries.cpp +++ b/src/xylinechart/qxychartseries.cpp @@ -52,10 +52,6 @@ void QXYChartSeries::setPen(const QPen& pen) m_pen=pen; } -void QXYChartSeries::setBrush(const QBrush& brush) -{ - m_brush=brush; -} QDebug operator<< (QDebug debug, const QXYChartSeries series) { diff --git a/src/xylinechart/qxychartseries.h b/src/xylinechart/qxychartseries.h index b8e9343..6a154c3 100644 --- a/src/xylinechart/qxychartseries.h +++ b/src/xylinechart/qxychartseries.h @@ -27,8 +27,6 @@ public: void setPen(const QPen& pen); const QPen& pen() const { return m_pen;} - void setBrush(const QBrush& brush); - const QBrush& brush() const { return m_brush;} int count() const; qreal x(int pos) const; @@ -38,7 +36,6 @@ public: private: QList m_x; QList m_y; - QBrush m_brush; QPen m_pen; }; diff --git a/src/xylinechart/xylinechartitem.cpp b/src/xylinechart/xylinechartitem.cpp index 5aa9010..e307fa7 100644 --- a/src/xylinechart/xylinechartitem.cpp +++ b/src/xylinechart/xylinechartitem.cpp @@ -60,7 +60,7 @@ void XYLineChartItem::updateGeometry() m_pathItem->setPath(path); m_pathItem->setPen(m_series->pen()); - m_pathItem->setBrush(m_series->brush()); + m_pathItem->setBrush(Qt::NoBrush); }