From c7c3c4960b214516088eaeb2afc06885c376d5e6 2012-01-25 14:41:48 From: Michal Klocek Date: 2012-01-25 14:41:48 Subject: [PATCH] Add background to chart * adds QGraphicsRectItem as background item * setters for color * example colorlinechart --- diff --git a/example/colorlinechart/colorlinechart.pro b/example/colorlinechart/colorlinechart.pro new file mode 100644 index 0000000..784936a --- /dev/null +++ b/example/colorlinechart/colorlinechart.pro @@ -0,0 +1,15 @@ +!include( ../../common.pri ) { + error( "Couldn't find the common.pri file!" ) +} + +!include( ../../integrated.pri ) { + error( "Couldn't find the integrated.pri file !") +} + +TARGET = colorlineChart +TEMPLATE = app +QT += core gui +SOURCES += main.cpp + + + diff --git a/example/colorlinechart/main.cpp b/example/colorlinechart/main.cpp new file mode 100644 index 0000000..5b8a53b --- /dev/null +++ b/example/colorlinechart/main.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +#define PI 3.14159265358979 + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + QMainWindow window; + + QXYChartSeries* series0 = QXYChartSeries::create(); + series0->setColor(Qt::blue); + QXYChartSeries* series1 = QXYChartSeries::create(); + series1->setColor(Qt::red); + + int numPoints = 100; + + for (int x = 0; x <= numPoints; ++x) { + series0->add(x, abs(sin(PI/50*x)*100)); + series1->add(x, abs(cos(PI/50*x)*100)); + } + + QChartView* chartView = new QChartView(&window); + chartView->addSeries(series0); + chartView->addSeries(series1); + chartView->setBackgroundColor(Qt::yellow); + + window.setCentralWidget(chartView); + window.resize(400, 300); + window.show(); + + return a.exec(); +} diff --git a/example/example.pro b/example/example.pro index 8930819..c358c53 100644 --- a/example/example.pro +++ b/example/example.pro @@ -1,3 +1,4 @@ TEMPLATE = subdirs SUBDIRS += linechart \ - zoomlinechart + zoomlinechart \ + colorlinechart diff --git a/src/qchart.cpp b/src/qchart.cpp index e6c888b..c16e0f0 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -17,6 +17,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), + m_background(new QGraphicsRectItem(this)), + m_title(new QGraphicsTextItem(this)), m_axisX(new AxisItem(AxisItem::X_AXIS,this)), m_axisY(new AxisItem(AxisItem::Y_AXIS,this)), m_plotDataIndex(0), @@ -146,6 +148,13 @@ void QChart::setSize(const QSize& size) m_rect = QRect(QPoint(0,0),size); QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); + + //recalculate background gradient + m_background->setRect(rect); + m_backgroundGradient.setFinalStop(0,m_background->rect().height()); + m_background->setBrush(m_backgroundGradient); + + //resize elements foreach (ChartItem* item ,m_chartItems) { item->setPos(rect.topLeft()); item->setSize(rect.size()); @@ -160,6 +169,20 @@ void QChart::setSize(const QSize& size) update(); } +void QChart::setBackgroundColor(const QColor& color) +{ + m_backgroundGradient.setColorAt( 0.0, Qt::white); + m_backgroundGradient.setColorAt( 1.0, color); + m_background->setBrush(m_backgroundGradient); + m_background->setPen(Qt::NoPen); + m_background->update(); +} + +void QChart::setTitle(const QString& title) +{ + m_title->setPlainText(title); +} + int QChart::margin() const { return m_marginSize; diff --git a/src/qchart.h b/src/qchart.h index fc47cdd..a2a85dc 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -4,6 +4,7 @@ #include #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -49,6 +50,9 @@ public: int margin() const; void setTheme(QChart::ChartTheme theme); + void setTitle(const QString& title); + void setBackgroundColor(const QColor& color); + void zoomInToRect(const QRect& rectangle); void zoomIn(); void zoomOut(); @@ -60,6 +64,9 @@ signals: private: Q_DISABLE_COPY(QChart) + QGraphicsRectItem* m_background; + QLinearGradient m_backgroundGradient; + QGraphicsTextItem* m_title; AxisItem* m_axisX; AxisItem* m_axisY; QRect m_rect; @@ -71,7 +78,6 @@ private: int m_plotDataIndex; int m_marginSize; QList m_themeColors; - QList m_BarGroupItems; }; diff --git a/src/qchartview.cpp b/src/qchartview.cpp index bab01c4..5ec3e58 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -63,4 +63,15 @@ int QChartView::margin() const { return m_chart->margin(); } + +void QChartView::setTitle(const QString& title) +{ + m_chart->setTitle(title); +} + +void QChartView::setBackgroundColor(const QColor& color) +{ + m_chart->setBackgroundColor(color); +} + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h index 3dc64ab..68cde12 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -25,6 +25,8 @@ public: QChartSeries* createSeries(QChartSeries::QChartSeriesType type); int margin() const; + void setTitle(const QString& title); + void setBackgroundColor(const QColor& color); void zoomInToRect(const QRect& rectangle); void zoomIn(); void zoomOut();