From 4417e87c4247efff281b3cf4b24a1e0e910797d7 2012-03-07 13:04:36 From: Tero Ahola Date: 2012-03-07 13:04:36 Subject: [PATCH] Added data getter for XY series; example for customizing the looks --- diff --git a/examples/customcolors/customcolors.pro b/examples/customcolors/customcolors.pro new file mode 100644 index 0000000..19154ee --- /dev/null +++ b/examples/customcolors/customcolors.pro @@ -0,0 +1,11 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +TARGET = customcolors +TEMPLATE = app + +SOURCES += main.cpp\ + mainwindow.cpp + +HEADERS += mainwindow.h diff --git a/examples/customcolors/main.cpp b/examples/customcolors/main.cpp new file mode 100644 index 0000000..9ae175b --- /dev/null +++ b/examples/customcolors/main.cpp @@ -0,0 +1,11 @@ +#include +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/examples/customcolors/mainwindow.cpp b/examples/customcolors/mainwindow.cpp new file mode 100644 index 0000000..6697ff5 --- /dev/null +++ b/examples/customcolors/mainwindow.cpp @@ -0,0 +1,95 @@ +#include "mainwindow.h" +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + // Here's the set of company's colors used throughout the example + m_companyColor1 = "#b90020"; + m_companyColor2 = "#6d0013"; + m_companyColor3 = "#d5d5d5"; + m_companyColor4 = "#fcfcfc"; + + resize(400, 300); + //setWindowFlags(Qt::FramelessWindowHint); + + // Create chart view + m_chartView = new QChartView(this); + setCentralWidget(m_chartView); + m_chartView->setChartTitle("Custom colors example"); + m_chartView->setRenderHint(QPainter::Antialiasing); + + // Create line series + m_line = new QLineSeries(); + m_line->add(0.0, 1.1); + m_line->add(1.0, 2.3); + m_line->add(2.0, 2.1); + m_line->add(3.0, 3.3); + m_chartView->addSeries(m_line); + + // Create scatter series with the same data + m_scatter = new QScatterSeries(); + m_scatter->add(m_line->data()); + m_chartView->addSeries(m_scatter); + + // Create pie series using color 2; use different fill styles for each pie slice + m_pie = new QPieSeries(); + m_pie->add(1.1, "1"); + m_pie->add(2.1, "2"); + m_pie->add(3.0, "3"); + m_pie->setPositionFactors(0.7, 0.7); + m_pie->setSizeFactor(0.5); + m_chartView->addSeries(m_pie); + + connect(&m_timer, SIGNAL(timeout()), this, SLOT(customize())); + m_timer.setInterval(1500); + m_timer.setSingleShot(false); + m_timer.start(); +} + +MainWindow::~MainWindow() +{ +} + +void MainWindow::customize() +{ + // Customize chart background + // Use a gradient from color 3 to color 4 for chart background + QLinearGradient chartGradient(0, 0, 0, 300); + chartGradient.setColorAt(0.0, m_companyColor3); + chartGradient.setColorAt(0.5, m_companyColor4); + chartGradient.setColorAt(1.0, m_companyColor3); + m_chartView->setChartBackgroundBrush(chartGradient); + m_chartView->setBackgroundBrush(m_companyColor4); + + // Customize chart axis + QPen color1Pen(m_companyColor1, 4.0); + m_chartView->axisX()->setAxisPen(color1Pen); + m_chartView->axisY()->setAxisPen(color1Pen); + + // Customize series + m_line->setPen(color1Pen); + m_scatter->setPen(color1Pen); + m_scatter->setBrush(m_companyColor3); + for (int i(0); i < m_pie->slices().count(); i++) { + Qt::BrushStyle style = static_cast(i + 1); + m_pie->slices().at(i)->setSliceBrush(QBrush(m_companyColor2, style)); + m_pie->slices().at(i)->setSlicePen(color1Pen); + } + + // Calculate new colors to be used on the next update for the series + m_companyColor1.setRed((m_companyColor1.red() + 25) % 255); + m_companyColor1.setGreen((m_companyColor1.green() + 25) % 255); + m_companyColor1.setBlue((m_companyColor1.blue() + 25) % 255); + m_companyColor2.setRed((m_companyColor2.red() + 25) % 255); + m_companyColor2.setGreen((m_companyColor2.green() + 25) % 255); + m_companyColor2.setBlue((m_companyColor2.blue() + 25) % 255); +} diff --git a/examples/customcolors/mainwindow.h b/examples/customcolors/mainwindow.h new file mode 100644 index 0000000..394ba97 --- /dev/null +++ b/examples/customcolors/mainwindow.h @@ -0,0 +1,40 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChartView; +class QLineSeries; +class QScatterSeries; +class QPieSeries; +QTCOMMERCIALCHART_END_NAMESPACE + +QTCOMMERCIALCHART_USE_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private Q_SLOTS: + void customize(); + +private: + QColor m_companyColor1; + QColor m_companyColor2; + QColor m_companyColor3; + QColor m_companyColor4; + QChartView *m_chartView; + QLineSeries *m_line; + QScatterSeries *m_scatter; + QPieSeries *m_pie; + QTimer m_timer; +}; + +#endif // MAINWINDOW_H diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index e46f709..8b1ad2c 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -161,6 +161,18 @@ int QXYSeries::count() const } /*! + Returns the data points of the series. +*/ +QList QXYSeries::data() +{ + QList data; + for (int i(0); i < m_x.count() && i < m_y.count(); i++) + data.append(QPointF(m_x.at(i), m_y.at(i))); + return data; +} + + +/*! Sets \a pen used for drawing points on the chart. If the pen is not defined, the pen from chart theme is used. \sa QChart::setChartTheme() diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index f665038..e852aa0 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -11,47 +11,45 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries { - Q_OBJECT + Q_OBJECT protected: - QXYSeries(QObject* parent=0); - virtual ~QXYSeries(); + QXYSeries(QObject* parent=0); + virtual ~QXYSeries(); public: - void add(qreal x, qreal y); - void add(const QPointF& point); + void add(qreal x, qreal y); + void add(const QPointF& point); void add(const QList points); - void replace(qreal x,qreal y); - void replace(const QPointF& point); - void remove(qreal x); - void remove(const QPointF& point); - void removeAll(); - - int count() const; - qreal x(int pos) const; - qreal y(int pos) const; - - QXYSeries& operator << (const QPointF &point); - QXYSeries& operator << (const QList points); - - void setPen(const QPen& pen); - QPen pen() const {return m_pen;} + void replace(qreal x,qreal y); + void replace(const QPointF& point); + void remove(qreal x); + void remove(const QPointF& point); + void removeAll(); + + int count() const; + qreal x(int pos) const; + qreal y(int pos) const; + QList data(); + + QXYSeries& operator << (const QPointF &point); + QXYSeries& operator << (const QList points); + + void setPen(const QPen& pen); + QPen pen() const {return m_pen;} void setBrush(const QBrush& pen); QBrush brush() const {return m_brush;} signals: - void updated(); - void pointReplaced(int index); - void pointRemoved(int index); - void pointAdded(int index); - + void updated(); + void pointReplaced(int index); + void pointRemoved(int index); + void pointAdded(int index); protected: - QVector m_x; - QVector m_y; - - QPen m_pen; - QBrush m_brush; - + QVector m_x; + QVector m_y; + QPen m_pen; + QBrush m_brush; }; QTCOMMERCIALCHART_END_NAMESPACE