From 6a1519e364a34cad68d03e04bd37a31cbf3605c1 2012-02-14 16:04:19 From: Michal Klocek Date: 2012-02-14 16:04:19 Subject: [PATCH] Adds more axis handling * adds visibility , pen, brush support * adds example chartaxis --- diff --git a/example/axischart/axischart.pro b/example/axischart/axischart.pro new file mode 100644 index 0000000..2cd2444 --- /dev/null +++ b/example/axischart/axischart.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 = axisChart +TEMPLATE = app +QT += core gui +SOURCES += main.cpp + + + diff --git a/example/axischart/main.cpp b/example/axischart/main.cpp new file mode 100644 index 0000000..97ae989 --- /dev/null +++ b/example/axischart/main.cpp @@ -0,0 +1,56 @@ +#include +#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; + + QLineChartSeries* series0 = new QLineChartSeries(); + QPen blue(Qt::blue); + blue.setWidth(3); + series0->setPen(blue); + QLineChartSeries* series1 = new QLineChartSeries(); + QPen red(Qt::red); + red.setWidth(3); + series1->setPen(red); + + int numPoints = 100; + + for (int x = 0; x <= numPoints; ++x) { + series0->add(x, fabs(sin(PI/50*x)*100)); + series1->add(x, fabs(cos(PI/50*x)*100)); + } + + QChartView* chartView = new QChartView(&window); + + chartView->setRenderHint(QPainter::Antialiasing); + chartView->setTitle("Basic line chart example"); + chartView->addSeries(series0); + chartView->addSeries(series1); + chartView->setChartBackgroundBrush(Qt::white); + + QChartAxis axis = chartView->defaultAxisX(); + axis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); + axis.setGridPen(Qt::DashLine); + + chartView->setDefaultAxisX(axis); + //axis.setShadesBrush(Qt::gray); + chartView->setDefaultAxisY(axis); + + window.setCentralWidget(chartView); + window.resize(400, 300); + window.show(); + + return a.exec(); +} diff --git a/example/example.pro b/example/example.pro index ea3587e..069b5b7 100644 --- a/example/example.pro +++ b/example/example.pro @@ -7,4 +7,5 @@ SUBDIRS += linechart \ percentbarchart \ scatter \ piechart \ - dynamiclinechart + dynamiclinechart \ + axischart diff --git a/src/axisitem.cpp b/src/axisitem.cpp index 1502323..d20b7a0 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -3,18 +3,22 @@ #include #include -#define LABEL_PADDING 5 +static int label_padding = 5; QTCOMMERCIALCHART_BEGIN_NAMESPACE -AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) : - ChartItem(parent), - m_axis(axis), - m_ticks(4), - m_type(type) +AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : +ChartItem(parent), +m_ticks(4), +m_type(type), +m_labelsAngle(0), +m_shadesEnabled(true), +m_grid(this), +m_shades(this), +m_labels(this) { //initial initialization - handleAxisChanged(); + createItems(); } AxisItem::~AxisItem() @@ -26,148 +30,257 @@ QRectF AxisItem::boundingRect() const return m_rect; } +void AxisItem::createItems() +{ + for (int i = 0; i <= m_ticks; ++i) { + m_grid.addToGroup(new QGraphicsLineItem(this)); + m_labels.addToGroup(new QGraphicsSimpleTextItem(this)); + if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this)); + } +} + +void AxisItem::clear() +{ + foreach(QGraphicsItem* item , m_shades.childItems()) { + delete item; + } + + foreach(QGraphicsItem* item , m_grid.childItems()) { + delete item; + } + + foreach(QGraphicsItem* item , m_labels.childItems()) { + delete item; + } + +} + +void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ -/* -void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) +} + +void AxisItem::updateDomain() { - if (!m_rect.isValid()) - return; - if(m_type==X_AXIS) { + QList lines = m_grid.childItems(); + QList labels = m_labels.childItems(); + QList shades = m_shades.childItems(); - const qreal deltaX = m_rect.width() / m_ticks; + switch (m_type) + { + case X_AXIS: + { + const qreal deltaX = m_rect.width() / m_ticks; - for (int i = 0; i <= m_ticks; ++i) { + for (int i = 0; i <= m_ticks; ++i) { - int x = i * deltaX + m_rect.left(); + int x = i * deltaX + m_rect.left(); - if(i==0) x--; - if(i==m_ticks) x++; + qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks); - qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX() - / m_ticks); - painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1); - // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(x, m_rect.top(), x, m_rect.bottom()); - painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label)); - } - } + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + labelItem->setText(QString::number(label)); + QPointF center = labelItem->boundingRect().center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding); - if(m_type==Y_AXIS) { + if(i%2){ + QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); + rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height()); + } + } + } + break; - const qreal deltaY = (m_rect.height()) / m_ticks; + case Y_AXIS: + { + const qreal deltaY = m_rect.height()/ m_ticks; - for (int j = 0; j <= m_ticks; ++j) { + for (int i = 0; i <= m_ticks; ++i) { - int y = j * -deltaY + m_rect.bottom(); + int y = i * -deltaY + m_rect.bottom(); - if(j==0) y++; - if(j==m_ticks) y--; + qreal label = m_domain.m_minY + (i * m_domain.spanY()/ m_ticks); - qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() - / m_ticks); + QGraphicsLineItem *lineItem = static_cast(lines.at(i)); + lineItem->setLine(m_rect.left() , y, m_rect.right(), y); - painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y); - //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y); - //TODO : margin = 50 ; - painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20, - Qt::AlignRight | Qt::AlignVCenter, - QString::number(label)); - } - } + QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); + labelItem->setText(QString::number(label)); + QPointF center = labelItem->boundingRect().center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y()); - //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); + + if(i%2){ + QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); + rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY); + } + } + } + break; + default: + qDebug()<<"Unknown axis type"; + break; + } } -*/ -void AxisItem::createItems() + +void AxisItem::handleAxisChanged(const QChartAxis& axis) { + if(axis.isGridVisible()) { + setGridOpacity(100); + } + else { + setGridOpacity(0); + } - if(!m_rect.isValid()) return; + if(axis.isLabelsVisible()) + { + setLabelsOpacity(100); + } + else { + setLabelsOpacity(0); + } - switch (m_type) + if(axis.isShadesVisible()) { + setShadesOpacity(100); + } + else { + setShadesOpacity(0); + } + + switch(axis.labelsOrientation()) { - case X_AXIS: - { - const qreal deltaX = m_rect.width() / m_ticks; + case QChartAxis::LabelsOrientationHorizontal: + setLabelsAngle(0); + break; + case QChartAxis::LabelsOrientationVertical: + setLabelsAngle(90); + break; + case QChartAxis::LabelsOrientationSlide: + setLabelsAngle(-45); + break; + default: + break; + } - for (int i = 0; i <= m_ticks; ++i) { + setLabelsPen(axis.labelsPen()); + setLabelsBrush(axis.labelsBrush()); + setLabelsFont(axis.labelFont()); + setGridPen(axis.gridPen()); + setShadesPen(axis.shadesPen()); + setShadesBrush(axis.shadesBrush()); - int x = i * deltaX + m_rect.left(); +} - qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks); +void AxisItem::handleDomainChanged(const Domain& domain) +{ + m_domain = domain; + updateDomain(); + update(); +} - m_grid<boundingRect().center(); - text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING); - //text->rotate(-45); - m_labels<boundingRect().center(); - text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y()); +void AxisItem::setShadesOpacity(qreal opacity) +{ + m_shades.setOpacity(opacity); +} - m_labels<boundingRect().center(); + item->setRotation(angle); } + + m_labelsAngle=angle; } -void AxisItem::clear() +void AxisItem::setLabelsPen(const QPen& pen) { - qDeleteAll(m_shades); - m_shades.clear(); - qDeleteAll(m_grid); - m_grid.clear(); - qDeleteAll(m_labels); - m_labels.clear(); + foreach(QGraphicsItem* item , m_labels.childItems()) { + static_cast(item)->setPen(pen); + } } -void AxisItem::updateDomain() +void AxisItem::setLabelsBrush(const QBrush& brush) { - clear(); - createItems(); + foreach(QGraphicsItem* item , m_labels.childItems()) { + static_cast(item)->setBrush(brush); + } } -void AxisItem::handleAxisChanged() +void AxisItem::setLabelsFont(const QFont& font) { - //m_axis-> + foreach(QGraphicsItem* item , m_labels.childItems()) { + static_cast(item)->setFont(font); + } } -void AxisItem::handleDomainChanged(const Domain& domain) +void AxisItem::setShadesBrush(const QBrush& brush) { - m_domain = domain; - clear(); - createItems(); + foreach(QGraphicsItem* item , m_shades.childItems()) { + static_cast(item)->setBrush(brush); + } } -void AxisItem::handleGeometryChanged(const QRectF& rect) +void AxisItem::setShadesPen(const QPen& pen) { - Q_ASSERT(rect.isValid()); - m_rect = rect; + foreach(QGraphicsItem* item , m_shades.childItems()) { + static_cast(item)->setPen(pen); + } +} + +void AxisItem::setGridPen(const QPen& pen) +{ + foreach(QGraphicsItem* item , m_grid.childItems()) { + static_cast(item)->setPen(pen); + } +} + +void AxisItem::setTicks(int count) +{ + if(count!=m_ticks){ clear(); + m_ticks=count; createItems(); + } } //TODO "nice numbers algorithm" diff --git a/src/axisitem_p.h b/src/axisitem_p.h index ac4c8da..4317de4 100644 --- a/src/axisitem_p.h +++ b/src/axisitem_p.h @@ -15,46 +15,58 @@ class AxisItem : public QObject, public ChartItem public: enum AxisType{X_AXIS,Y_AXIS}; - AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0); + AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); ~AxisItem(); //from QGraphicsItem QRectF boundingRect() const; - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){}; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); -protected slots: - void handleAxisChanged(); - void handleDomainChanged(const Domain& domain); - void handleGeometryChanged(const QRectF& size); + AxisType axisType() const {return m_type;}; -protected: - void updateDomain(); + void setGridOpacity(qreal opacity); + qreal gridOpacity() const; -private: - void clear(); + void setLabelsOpacity(qreal opacity); + qreal labelsOpacity() const; -public: - AxisType axisType() const {return m_type;}; + void setShadesOpacity(qreal opacity); + qreal shadesOpacity() const; -protected: + void setLabelsAngle(int angle); + int labelsAngle()const { return m_labelsAngle; } + + void setTicks(int count); + int ticks() const { return m_ticks;} + void setShadesBrush(const QBrush& brush); + void setShadesPen(const QPen& pen); + void setGridPen(const QPen& pen); + + void setLabelsPen(const QPen& pen); + void setLabelsBrush(const QBrush& brush); + void setLabelsFont(const QFont& font); + +public slots: + void handleAxisChanged(const QChartAxis& axis); + void handleDomainChanged(const Domain& domain); + void handleGeometryChanged(const QRectF& size); +protected: + void updateDomain(); private: + void clear(); void createItems(); private: - - QChartAxis* m_axis; AxisType m_type; int m_ticks; Domain m_domain; QRectF m_rect; - - QPainterPath m_path; - - - QList m_grid; - QList m_shades; - QList m_labels; + int m_labelsAngle; + bool m_shadesEnabled; + QGraphicsItemGroup m_grid; + QGraphicsItemGroup m_shades; + QGraphicsItemGroup m_labels; }; diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 565ddcd..449080d 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -26,15 +26,13 @@ ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char m_chart(chart), m_dataset(dataset), m_chartTheme(0), +m_axisXItem(new AxisItem(AxisItem::X_AXIS,m_chart)), +m_axisYItem(new AxisItem(AxisItem::Y_AXIS,m_chart)), m_domainIndex(0), m_marginSize(0), -m_axisX(new QChartAxis(this)), -m_axisY(new QChartAxis(this)), m_rect(QRectF(QPoint(0,0),m_chart->size())) { setChartTheme(QChart::ChartThemeDefault); - m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart); - m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart); createConnections(); } @@ -46,14 +44,10 @@ void ChartPresenter::createConnections() { QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); - - QMapIterator i(m_axisItems); - - while (i.hasNext()) { - i.next(); - QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&))); - QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&))); - } + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisXItem,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisXItem,SLOT(handleDomainChanged(const Domain&))); + QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisYItem,SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisYItem,SLOT(handleDomainChanged(const Domain&))); } void ChartPresenter::handleGeometryChanged() @@ -206,58 +200,70 @@ void ChartPresenter::setChartTheme(QChart::ChartTheme theme) i.next(); index++; m_chartTheme->decorate(i.value(),i.key(),index); - } } + m_chartTheme->decorate(m_axisX, m_axisXItem); + m_chartTheme->decorate(m_axisY, m_axisYItem); + +} + QChart::ChartTheme ChartPresenter::chartTheme() { return m_chartTheme->id(); } -QChartAxis* ChartPresenter::axisX() +void ChartPresenter::setDefaultAxisX(const QChartAxis& axis) { - return m_axisX; + //if(m_axisX != axis) { + m_axisX = axis; + m_axisXItem->handleAxisChanged(m_axisX); + //} } -QChartAxis* ChartPresenter::axisY() +void ChartPresenter::setDefaultAxisY(const QChartAxis& axis) { - return m_axisY; + // if(m_axisY != axis) { + m_axisY = axis; + m_axisYItem->handleAxisChanged(m_axisY); + //} } -QChartAxis* ChartPresenter::addAxisX() +QChartAxis ChartPresenter::defaultAxisX() const { - //only one axis - if(m_axisX==0){ - m_axisX = new QChartAxis(this); - m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart); - } - return m_axisX; + return m_axisX; } -QChartAxis* ChartPresenter::addAxisY() +QChartAxis ChartPresenter::defaultAxisY() const { - if(m_axisY==0){ - m_axisY = new QChartAxis(this); - m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart); - return m_axisY; - } + return m_axisY; +} - QChartAxis* axis = new QChartAxis(this); - m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart); - return axis; +QChartAxis ChartPresenter::axisY(int id) const +{ + return m_axis.value(id); } -void ChartPresenter::removeAxis(QChartAxis* axis) +int ChartPresenter::addAxisY(const QChartAxis& axis) { - AxisItem* item = m_axisItems.take(axis); - if(item){ - delete item; - delete axis; + int key =0 ; + + while(m_axis.contains(key)){ + key++; + //TODO overflow } - //reset pointers to default ones - if(axis == m_axisX) m_axisX=0; - else if(axis == m_axisY) m_axisY=0; + + m_axis.insert(key,axis); + m_axisItems.insert(key,new AxisItem(AxisItem::Y_AXIS,m_chart)); + + return key; +} + + +void ChartPresenter::removeAxisY(int id) +{ + m_axis.remove(id); + delete m_axisItems.take(id); } #include "moc_chartpresenter_p.cpp" diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 816f056..b999918 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -3,6 +3,7 @@ #include "qchartglobal.h" #include "qchart.h" //becouse of QChart::ChartThemeId //TODO +#include "qchartaxis.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -33,11 +34,13 @@ public: void setChartTheme(QChart::ChartTheme theme); QChart::ChartTheme chartTheme(); - QChartAxis* axisX(); - QChartAxis* axisY(); - QChartAxis* addAxisX(); - QChartAxis* addAxisY(); - void removeAxis(QChartAxis* axis); + void setDefaultAxisX(const QChartAxis& axis); + void setDefaultAxisY(const QChartAxis& axis); + QChartAxis defaultAxisX() const; + QChartAxis defaultAxisY() const; + QChartAxis axisY(int id) const; + int addAxisY(const QChartAxis& axis); + void removeAxisY(int id); private: void createConnections(); @@ -48,19 +51,20 @@ public slots: void handleSeriesChanged(QChartSeries* series); //void handleDomainChanged(Domain oldDomain,Domain newDomain); void handleGeometryChanged(); - signals: void geometryChanged(const QRectF& rect); - private: QMap m_chartItems; - QMap m_axisItems; + QMap m_axisItems; + QMap m_axis; QChart* m_chart; ChartDataSet* m_dataset; QVector m_domains; ChartTheme *m_chartTheme; - QChartAxis* m_axisX; - QChartAxis* m_axisY; + QChartAxis m_axisX; + AxisItem* m_axisXItem; + QChartAxis m_axisY; + AxisItem* m_axisYItem; int m_domainIndex; int m_marginSize; QRectF m_rect; diff --git a/src/charttheme.cpp b/src/charttheme.cpp index b2928f9..8af356e 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -1,5 +1,6 @@ #include "charttheme_p.h" #include "qchart.h" +#include "qchartaxis.h" //series @@ -207,4 +208,13 @@ void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) } +void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) +{ + //TODO: dummy defults for now + + axis.setLabelsBrush(Qt::black); + axis.setLabelsPen(Qt::NoPen); + item->handleAxisChanged(axis); +} + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/charttheme_p.h b/src/charttheme_p.h index bc7d3d9..aea5290 100644 --- a/src/charttheme_p.h +++ b/src/charttheme_p.h @@ -34,6 +34,7 @@ public: void decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count); void decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count); void decorate(PiePresenter* item, QPieSeries* series, int count); + void decorate(QChartAxis& axis,AxisItem* item); protected: QChart::ChartTheme m_id; diff --git a/src/qchart.cpp b/src/qchart.cpp index 39e5916..1b36eb0 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -143,29 +143,39 @@ void QChart::zoomReset() m_presenter->zoomReset(); } -QChartAxis* QChart::axisX() +void QChart::setDefaultAxisX(const QChartAxis& axis) { - return m_presenter->axisX(); + m_presenter->setDefaultAxisX(axis); } -QChartAxis* QChart::axisY() +void QChart::setDefaultAxisY(const QChartAxis& axis) { - return m_presenter->axisY(); + m_presenter->setDefaultAxisY(axis); } -QChartAxis* QChart::addAxisX() +QChartAxis QChart::defaultAxisX() const { - return m_presenter->addAxisX(); + return m_presenter->defaultAxisX(); } -QChartAxis* QChart::addAxisY() +QChartAxis QChart::defaultAxisY() const { - return m_presenter->addAxisY(); + return m_presenter->defaultAxisY(); } -void QChart::removeAxis(QChartAxis* axis) +int QChart::addAxisY(const QChartAxis& axis) { - m_presenter->removeAxis(axis); + return m_presenter->addAxisY(axis); +} + +QChartAxis QChart::axisY(int id) const +{ + return m_presenter->axisY(id); +} + +void QChart::removeAxisY(int id) +{ + m_presenter->removeAxisY(id); } void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) diff --git a/src/qchart.h b/src/qchart.h index b881f57..b13dd8e 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -67,11 +67,13 @@ public: void zoomOut(); void zoomReset(); - QChartAxis* axisX(); - QChartAxis* axisY(); - QChartAxis* addAxisX(); - QChartAxis* addAxisY(); - void removeAxis(QChartAxis* axis); + void setDefaultAxisX(const QChartAxis& axis); + void setDefaultAxisY(const QChartAxis& axis); + QChartAxis defaultAxisX() const; + QChartAxis defaultAxisY() const; + QChartAxis axisY(int id) const; + int addAxisY(const QChartAxis& axis); + void removeAxisY(int id); protected: void resizeEvent(QGraphicsSceneResizeEvent *event); diff --git a/src/qchartaxis.cpp b/src/qchartaxis.cpp index 43e2a17..b7ca0b7 100644 --- a/src/qchartaxis.cpp +++ b/src/qchartaxis.cpp @@ -1,13 +1,12 @@ - #include "qchartaxis.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -QChartAxis::QChartAxis(QObject* parent):QObject(parent), +QChartAxis::QChartAxis(): m_axisVisible(true), -m_girdVisible(true), +m_gridVisible(true), m_labelsVisible(true), -m_rowShadesVisible(true) +m_shadesVisible(true) { // TODO Auto-generated constructor stub @@ -20,12 +19,12 @@ QChartAxis::~QChartAxis() void QChartAxis::setAxisPen(const QPen& pen) { - m_axisPen = pen; + m_axisPen=pen; } void QChartAxis::setAxisBrush(const QBrush& brush) { - m_axisBrush = brush; + m_axisBrush=brush; } void QChartAxis::setAxisVisible(bool visible) @@ -35,7 +34,12 @@ void QChartAxis::setAxisVisible(bool visible) void QChartAxis::setGridVisible(bool visible) { - m_girdVisible=visible; + m_gridVisible=visible; +} + +void QChartAxis::setGridPen(const QPen& pen) +{ + m_gridPen=pen; } void QChartAxis::setLabelsVisible(bool visible) @@ -43,11 +47,40 @@ void QChartAxis::setLabelsVisible(bool visible) m_labelsVisible=visible; } -void QChartAxis::setRowShadesVisible(bool visible) +void QChartAxis::setLabelsPen(const QPen& pen) +{ + m_labelsPen=pen; +} + +void QChartAxis::setLabelsBrush(const QBrush& brush) +{ + m_labelsBrush=brush; +} + +void QChartAxis::setLabelsFont(const QFont& font) +{ + m_labelsFont=font; +} + +void QChartAxis::setLabelsOrientation(LabelsOrientation orientation) +{ + m_labelsOrientation=orientation; +} + +void QChartAxis::setShadesVisible(bool visible) +{ + m_shadesVisible=visible; +} + +void QChartAxis::setShadesPen(const QPen& pen) +{ + m_shadesPen=pen; +} + +void QChartAxis::setShadesBrush(const QBrush& brush) { - m_rowShadesVisible=visible; + m_shadesBrush=brush; } -#include "moc_qchartaxis.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartaxis.h b/src/qchartaxis.h index 1582562..ace00c9 100644 --- a/src/qchartaxis.h +++ b/src/qchartaxis.h @@ -3,62 +3,52 @@ #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QChartAxis : public QObject +class QChartAxis { - Q_OBJECT - - Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged); - Q_PROPERTY(QPen axisPen READ axisPen WRITE setAxisPen NOTIFY axisPenChanged); - Q_PROPERTY(QBrush axisBrush READ axisBrush WRITE setAxisBrush NOTIFY axisBurshChanged); - - // Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged); public: - enum LabelOrientation{ HORIZONTAL, VERTICAL , SLIDE }; + enum LabelsOrientation{ LabelsOrientationHorizontal, LabelsOrientationVertical , LabelsOrientationSlide }; - QChartAxis(QObject* parent = 0); + QChartAxis(); virtual ~QChartAxis(); //axis bool isAxisVisible() const { return m_axisVisible;}; void setAxisVisible(bool visible); void setAxisPen(const QPen& pen); - const QPen& axisPen() const { return m_axisPen;}; + QPen axisPen() const { return m_axisPen;}; void setAxisBrush(const QBrush& brush); - const QBrush& axisBrush() const { return m_axisBrush;}; + QBrush axisBrush() const { return m_axisBrush;}; //grid - bool isGridVisible() const { return m_girdVisible;}; + bool isGridVisible() const { return m_gridVisible;}; void setGridVisible(bool visible); + void setGridPen(const QPen& pen); + QPen gridPen() const {return m_gridPen;} + //labels bool isLabelsVisible() const { return m_labelsVisible;}; void setLabelsVisible(bool visible); - - bool isRowShadesVisible() const { return m_rowShadesVisible;}; - void setRowShadesVisible(bool visible); - - /* - void setLabelFont(const QFont& font); - const QFont& labelFont(); - - void setLabelPen(const QPen& pen); - const QPen& labelPen(); - - void setGridPen(const QPen& pen); - const QPen& gridPen(); - - void setGridBrush(const QBrush& brush); - const QBrush& gridBrush(); - */ - - -signals: - void axisVisibilityChanged(); - void axisPenChanged(); - void axisBurshChanged(); + void setLabelsPen(const QPen& pen); + QPen labelsPen() const { return m_labelsPen;} + void setLabelsBrush(const QBrush& brush); + QBrush labelsBrush() const { return m_labelsBrush;} + void setLabelsFont(const QFont& font); + QFont labelFont() const { return m_labelsFont;} + void setLabelsOrientation(LabelsOrientation orientation); + LabelsOrientation labelsOrientation() const { return m_labelsOrientation;}; + + //shades + bool isShadesVisible() const { return m_shadesVisible;}; + void setShadesVisible(bool visible); + void setShadesPen(const QPen& pen); + QPen shadesPen() const { return m_shadesPen;} + void setShadesBrush(const QBrush& brush); + QBrush shadesBrush() const { return m_shadesBrush;} private: @@ -67,9 +57,19 @@ private: QPen m_axisPen; QBrush m_axisBrush; - bool m_girdVisible; + bool m_gridVisible; + QPen m_gridPen; + bool m_labelsVisible; - bool m_rowShadesVisible; + QPen m_labelsPen; + QBrush m_labelsBrush; + QFont m_labelsFont; + + bool m_shadesVisible; + QPen m_shadesPen; + QBrush m_shadesBrush; + + LabelsOrientation m_labelsOrientation; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 83e2af9..805b910 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -1,5 +1,6 @@ #include "qchartview.h" #include "qchart.h" +#include "qchartaxis.h" #include #include #include @@ -9,14 +10,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChartView::QChartView(QWidget *parent) : - QGraphicsView(parent), - m_scene(new QGraphicsScene()), - m_chart(new QChart()), - m_rubberBand(0), - m_verticalRubberBand(false), - m_horizonalRubberBand(false) -{ - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); +QGraphicsView(parent), +m_scene(new QGraphicsScene()), +m_chart(new QChart()), +m_rubberBand(0), +m_verticalRubberBand(false), +m_horizonalRubberBand(false) +{ + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setScene(m_scene); m_chart->setMargin(50); @@ -35,7 +36,6 @@ void QChartView::resizeEvent(QResizeEvent *event) QWidget::resizeEvent(event); } - void QChartView::addSeries(QChartSeries* series) { m_chart->addSeries(series); @@ -74,40 +74,39 @@ void QChartView::setTitle(const QString& title) void QChartView::setChartBackgroundBrush(const QBrush& brush) { - m_chart->setChartBackgroundBrush(brush); + m_chart->setChartBackgroundBrush(brush); } void QChartView::setChartBackgroundPen(const QPen& pen) { - m_chart->setChartBackgroundPen(pen); + m_chart->setChartBackgroundPen(pen); } - void QChartView::setRubberBandPolicy(const RubberBandPolicy policy) { - switch(policy){ + switch(policy) { case VerticalRubberBand: - m_verticalRubberBand = true; - m_horizonalRubberBand = false; - break; + m_verticalRubberBand = true; + m_horizonalRubberBand = false; + break; case HorizonalRubberBand: - m_verticalRubberBand = false; - m_horizonalRubberBand = true; - break; + m_verticalRubberBand = false; + m_horizonalRubberBand = true; + break; case RectangleRubberBand: - m_verticalRubberBand = true; - m_horizonalRubberBand = true; - break; + m_verticalRubberBand = true; + m_horizonalRubberBand = true; + break; case NoRubberBand: default: - delete m_rubberBand; - m_rubberBand=0; - m_horizonalRubberBand = false; - m_verticalRubberBand = false; - return; + delete m_rubberBand; + m_rubberBand=0; + m_horizonalRubberBand = false; + m_verticalRubberBand = false; + return; } - if(!m_rubberBand){ - m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); - m_rubberBand->setEnabled(true); + if(!m_rubberBand) { + m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); + m_rubberBand->setEnabled(true); } } @@ -119,7 +118,6 @@ QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const return NoRubberBand; } - void QChartView::mousePressEvent(QMouseEvent *event) { if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { @@ -138,7 +136,7 @@ void QChartView::mousePressEvent(QMouseEvent *event) void QChartView::mouseMoveEvent(QMouseEvent *event) { - if(m_rubberBand && m_rubberBand->isVisible()){ + if(m_rubberBand && m_rubberBand->isVisible()) { int margin = m_chart->margin(); QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); int width = event->pos().x() - m_rubberBandOrigin.x(); @@ -152,24 +150,26 @@ void QChartView::mouseMoveEvent(QMouseEvent *event) width= rect.width(); } m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized()); - } else { + } + else { QGraphicsView::mouseMoveEvent(event); } } void QChartView::mouseReleaseEvent(QMouseEvent *event) { - if(m_rubberBand){ - if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { - m_rubberBand->hide(); - QRect rect = m_rubberBand->geometry(); - m_chart->zoomInToRect(rect); - event->accept(); - } + if(m_rubberBand) { + if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { + m_rubberBand->hide(); + QRect rect = m_rubberBand->geometry(); + m_chart->zoomInToRect(rect); + event->accept(); + } - if(event->button()==Qt::RightButton) + if(event->button()==Qt::RightButton) m_chart->zoomReset(); - }else{ + } + else { QGraphicsView::mouseReleaseEvent(event); } } @@ -177,13 +177,13 @@ void QChartView::mouseReleaseEvent(QMouseEvent *event) void QChartView::keyPressEvent(QKeyEvent *event) { switch (event->key()) { - case Qt::Key_Plus: + case Qt::Key_Plus: zoomIn(); break; - case Qt::Key_Minus: + case Qt::Key_Minus: zoomOut(); break; - default: + default: QGraphicsView::keyPressEvent(event); break; } @@ -199,29 +199,38 @@ QChart::ChartTheme QChartView::chartTheme() const return m_chart->chartTheme(); } -QChartAxis* QChartView::axisX() +void QChartView::setDefaultAxisX(const QChartAxis& axis) +{ + m_chart->setDefaultAxisX(axis); +} + +void QChartView::setDefaultAxisY(const QChartAxis& axis) { - return m_chart->axisX(); + m_chart->setDefaultAxisY(axis); } -QChartAxis* QChartView::axisY() +QChartAxis QChartView::defaultAxisX() const { - return m_chart->axisY(); + return m_chart->defaultAxisX(); } -QChartAxis* QChartView::addAxisX() +QChartAxis QChartView::defaultAxisY() const { - return m_chart->addAxisX(); + return m_chart->defaultAxisY(); } -QChartAxis* QChartView::addAxisY() +int QChartView::addAxisY(const QChartAxis& axis) { - return m_chart->addAxisY(); + return m_chart->addAxisY(axis); } -void QChartView::removeAxis(QChartAxis* axis) +QChartAxis QChartView::axisY(int id) const { - m_chart->removeAxis(axis); + return m_chart->axisY(id); } +void QChartView::removeAxisY(int id) +{ + m_chart->removeAxisY(id); +} QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h index 24a4217..2d5e481 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -45,11 +45,13 @@ public: void setChartTheme(QChart::ChartTheme theme); QChart::ChartTheme chartTheme() const; - QChartAxis* axisX(); - QChartAxis* axisY(); - QChartAxis* addAxisX(); - QChartAxis* addAxisY(); - void removeAxis(QChartAxis* axis); + void setDefaultAxisX(const QChartAxis& axis); + void setDefaultAxisY(const QChartAxis& axis); + QChartAxis defaultAxisX() const; + QChartAxis defaultAxisY() const; + QChartAxis axisY(int id) const; + int addAxisY(const QChartAxis& axis); + void removeAxisY(int id); protected: void mousePressEvent(QMouseEvent *event);