diff --git a/doc/src/examples.qdoc b/doc/src/examples.qdoc index 6b3550e..3f8fd93 100644 --- a/doc/src/examples.qdoc +++ b/doc/src/examples.qdoc @@ -20,6 +20,7 @@
  • Line Chart example
  • Pie Chart example
  • GDP Chart example
  • +
  • Spline Chart example
  • diff --git a/example/gdpbarchart/widget.cpp b/example/gdpbarchart/widget.cpp index 32bda7a..3281e96 100644 --- a/example/gdpbarchart/widget.cpp +++ b/example/gdpbarchart/widget.cpp @@ -87,6 +87,7 @@ Widget::Widget(QWidget *parent) // hide axis X labels QChartAxis* axis = chartArea->axisX(); +// axis-> // axis->setLabelsVisible(false); // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); @@ -211,7 +212,7 @@ void Widget::refreshChart() // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); chartArea->addSeries(series); } - chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); + chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1); } } diff --git a/example/splinechart/splinewidget.cpp b/example/splinechart/splinewidget.cpp index 80a5f99..3b45ce0 100644 --- a/example/splinechart/splinewidget.cpp +++ b/example/splinechart/splinewidget.cpp @@ -3,80 +3,67 @@ #include "qlineseries.h" #include #include +#include "qchartaxis.h" +#include QTCOMMERCIALCHART_USE_NAMESPACE SplineWidget::SplineWidget(QWidget *parent) : QWidget(parent) { + qsrand(time(NULL)); + //! [1] //create QSplineSeries series = new QSplineSeries(this); + //! [1] + + //! [2] + // customize the series presentation settings + QPen seriesPen(Qt::blue); + seriesPen.setWidth(3); + series->setPen(seriesPen); + //! [2] + + //! [add points to series] + //add data points to the series series->add(QPointF(150, 100)); series->add(QPointF(200, 130)); series->add(QPointF(250, 120)); series->add(QPointF(300, 140)); - series->add(QPointF(350, 100)); - series->add(QPointF(400, 120)); - series->add(QPointF(450, 150)); - // series->add(QPointF(600, 150)); - series->add(QPointF(500, 145)); - series->add(QPointF(550, 170)); - series->add(QPointF(600, 190)); - series->add(QPointF(650, 210)); - series->add(QPointF(700, 190)); - series->add(QPointF(750, 180)); - series->add(QPointF(800, 170)); - -// series->calculateControlPoints(); - - QSplineSeries* series2 = new QSplineSeries(this); - qsrand(time(NULL)); - // for (int i = 0; i < 100; i++) - // { - // series2->add(QPointF(i*7, qrand()%600)); - // } - int k = 10; - for (int i = 0; i < 25; i++) - { - if (k > 60) - { - k = 10; - } - series2->add(QPointF(i*50, k)); - k +=10; - } + series->add(QPointF(350, 160)); + //! [add points to series] +// series->add(QPointF(400, 120)); +// series->add(QPointF(450, 150)); +// series->add(QPointF(500, 145)); +// series->add(QPointF(550, 170)); +// series->add(QPointF(600, 190)); +// series->add(QPointF(650, 210)); +// series->add(QPointF(700, 190)); +// series->add(QPointF(750, 180)); +// series->add(QPointF(800, 170)); -// series2->calculateControlPoints(); - - // QLineSeries* lineSeries = new QLineSeries; - // for (int i = 0; i < series->count() - 1; i++) - // { - // lineSeries->add(series->at(i).x(), series->at(i).y()); - // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y()); - // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y()); - // } + //! [3] + // create chart view + QChartView* chart = new QChartView; + chart->addSeries(series); - // QLineChartSeries* lineSeries2 = new QLineChartSeries; - // lineSeries2->add(10, 50); - // lineSeries2->add(30, 15); - // lineSeries2->add(60, 40); - // lineSeries2->add(90, 70); - // lineSeries2->add(100, 20); + chart->axisX()->setRange(0, 1500); + chart->axisY()->setRange(0, 400); - //create chart view - QChartView* chart = new QChartView; chart->setMinimumSize(800,600); - // chart->setGeometry(50, 50, 400, 300); - chart->addSeries(series); - chart->addSeries(series2); + //! [3] - //add new item + //! [4] + //add new data point button QPushButton* addButton = new QPushButton("Add new point"); connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); - QPushButton* removeButton = new QPushButton("Remove point from the end"); + // remove the last data point in the series + QPushButton* removeButton = new QPushButton("Remove point"); connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); + //! [4] + //! [5] //butttons layout QVBoxLayout* buttonsLayout = new QVBoxLayout; buttonsLayout->addWidget(addButton); @@ -87,21 +74,26 @@ SplineWidget::SplineWidget(QWidget *parent) mainLayout->addWidget(chart, 1, 0); mainLayout->addLayout(buttonsLayout, 1, 1); setLayout(mainLayout); + //! [5] } +//! [add point] void SplineWidget::addNewPoint() { if (series->count() > 0) - series->add(QPointF(series->x(series->count() - 1) + 50, series->y(series->count() - 1) - 50 + qrand()%100)); + series->add(QPointF(series->x(series->count() - 1) + 20 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100))); else series->add(QPointF(50, 50 + qrand()%50)); } +//! [add point] +//! [remove point] void SplineWidget::removePoint() { if (series->count() > 0) series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); } +//! [remove point] SplineWidget::~SplineWidget() { diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 744bf41..4c9ebed 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -304,6 +304,15 @@ void ChartTheme::decorate(SplinePresenter* presenter, QSplineSeries* series, int Q_ASSERT(presenter); Q_ASSERT(series); + QPen pen; + if(pen != series->pen()){ + presenter->setPen(series->pen()); + return; + } + pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); + pen.setWidthF(series->pen().widthF()); + presenter->setPen(pen); + // QColor color = m_seriesColor.at(count % m_seriesColor.size()); // TODO: define alpha in the theme? or in the series? //color.setAlpha(120); diff --git a/src/linechart/qlineseries.cpp b/src/linechart/qlineseries.cpp index a41b0ec..2d0ed6f 100644 --- a/src/linechart/qlineseries.cpp +++ b/src/linechart/qlineseries.cpp @@ -124,9 +124,9 @@ void QLineSeries::replace(const QPointF& point) void QLineSeries::remove(qreal x) { int index = m_x.indexOf(x); - m_x.remove(index); - m_y.remove(index); emit pointRemoved(index); + m_x.remove(index); + m_y.remove(index); } /*! diff --git a/src/splinechart/qsplineseries.h b/src/splinechart/qsplineseries.h index 89218f6..9385638 100644 --- a/src/splinechart/qsplineseries.h +++ b/src/splinechart/qsplineseries.h @@ -20,6 +20,11 @@ class QSplineSeries : public QLineSeries // int count() const { return m_x.size(); } QPointF controlPoint(int index) const { return m_controlPoints[index]; } + // TODO: allow the user to set custom control points +// void setCustomControlPoints(QList controlPoints); +// bool calculateControlPointsAutomatically(); +// void setCalculateControlPointsAutomatically(); + private: void calculateControlPoints(); QList getFirstControlPoints(QList rhs); diff --git a/src/splinechart/splinepresenter.cpp b/src/splinechart/splinepresenter.cpp index f6b658a..259db42 100644 --- a/src/splinechart/splinepresenter.cpp +++ b/src/splinechart/splinepresenter.cpp @@ -29,8 +29,8 @@ void SplinePresenter::applyGeometry(QVector& points) const QPointF& point = points.at(0); splinePath.moveTo(point); - QSplineSeries* splineSeries = qobject_cast(m_series); - for (int i = 0; i < splineSeries->count() - 1; i++) +// QSplineSeries* splineSeries = qobject_cast(m_series); + for (int i = 0; i < points.size() - 1; i++) { const QPointF& point = points.at(i + 1); splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point); @@ -56,7 +56,7 @@ void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o for (int i = 0; i < splineSeries->count() - 1; i++) { painter->setPen(Qt::red); - painter->drawEllipse(m_points[i], 4, 4); + painter->drawEllipse(m_points[i], 2, 2); painter->setPen(Qt::blue); // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); @@ -67,7 +67,7 @@ void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o if (m_points.count() > 0) { painter->setPen(Qt::red); - painter->drawEllipse(m_points[m_points.count() - 1], 4, 4); + painter->drawEllipse(m_points[m_points.count() - 1], 2, 2); } painter->restore(); }