From 989e7d47a51ad7350bc01e169dc9123c35e01511 2012-03-22 16:07:08 From: Michal Klocek Date: 2012-03-22 16:07:08 Subject: [PATCH] Adds custom categories + example --- diff --git a/examples/axischart/main.cpp b/examples/axischart/main.cpp index 33a5702..804f48f 100644 --- a/examples/axischart/main.cpp +++ b/examples/axischart/main.cpp @@ -5,12 +5,9 @@ #include #include #include -#include QTCOMMERCIALCHART_USE_NAMESPACE -#define PI 3.14159265358979 - int main(int argc, char *argv[]) { QApplication a(argc, argv); @@ -21,24 +18,14 @@ int main(int argc, char *argv[]) QPen blue(Qt::blue); blue.setWidth(3); series0->setPen(blue); - QLineSeries* series1 = new QLineSeries(); - 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)); - } + *series0 << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10,5); QChartView* chartView = new QChartView(&window); chartView->setRenderHint(QPainter::Antialiasing); - chartView->setChartTitle("This is custom axis chart example"); + chartView->setChartTitle("Simple axis example"); chartView->addSeries(series0); - chartView->addSeries(series1); QLinearGradient backgroundGradient; backgroundGradient.setColorAt(0.0, Qt::white); @@ -47,32 +34,30 @@ int main(int argc, char *argv[]) chartView->setChartBackgroundBrush(backgroundGradient); QChartAxis* axisX = chartView->axisX(); - axisX->setLabelsAngle(45); - axisX->setGridLinePen(red); - axisX->setGridLineVisible(false); - axisX->setShadesVisible(true); - - QChartAxisCategories* categoriesX = axisX->categories(); - categoriesX->insert(0,"low"); - categoriesX->insert(50,"medium"); - categoriesX->insert(100,"High"); - - // axisX->setMin(-10); - // axisX->setMax(2200); + axisX->setRange(0,10); + axisX->setTicksCount(4); + axisX->setGridLineVisible(true); + axisX->setShadesVisible(false); QChartAxis* axisY = chartView->axisY(); + axisY->setRange(0,10); axisY->setLabelsAngle(45); - axisY->setShadesBrush(Qt::yellow); - axisY->setShadesVisible(true); + axisY->setTicksCount(4); + axisY->setShadesPen(Qt::NoPen); + axisY->setShadesOpacity(0.5); + axisY->setShadesBrush(Qt::blue); axisY->setGridLineVisible(false); + axisY->setShadesVisible(true); - QChartAxisCategories* categoriesY = axisY->categories(); - categoriesY->insert(0,"low"); - categoriesY->insert(50,"medium"); - categoriesY->insert(100,"High"); + QChartAxisCategories* categoriesX = axisX->categories(); + categoriesX->insert(1,"low"); + categoriesX->insert(5,"optimal"); + categoriesX->insert(10,"high"); - // axisY->setMin(-10); - // axisY->setMax(200); + QChartAxisCategories* categoriesY = axisY->categories(); + categoriesY->insert(1,"slow"); + categoriesY->insert(5,"med"); + categoriesY->insert(10,"fast"); window.setCentralWidget(chartView); window.resize(400, 300); diff --git a/src/axis/axisitem.cpp b/src/axis/axisitem.cpp index 545fdf4..af2e0a7 100644 --- a/src/axis/axisitem.cpp +++ b/src/axis/axisitem.cpp @@ -93,8 +93,15 @@ bool Axis::createLabels(QStringList& labels,qreal min, qreal max,int ticks) cons } } else { + QList values = categories->values(); for(int i=0; i< ticks; i++) { - int value = ceil(min + (i * (max - min)/ (ticks-1))); + qreal value = (min + (i * (max - min)/ (ticks-1))); + int j=0; + for(; j value) break; + } + if(j!=0) value=values.at(j-1); + QString label = categories->label(value); labels << label; } @@ -269,7 +276,6 @@ void Axis::setLayout(QVector& layout) QGraphicsLineItem *lineItem = static_cast(lines.at(i)); lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - if(!categories){ labelItem->setText(ticksList.at(i)); QPointF center = labelItem->boundingRect().center(); @@ -301,10 +307,19 @@ void Axis::setLayout(QVector& layout) QGraphicsLineItem *lineItem = static_cast(lines.at(i)); lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - labelItem->setText(ticksList.at(i)); - QPointF center = labelItem->boundingRect().center(); - labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); + + if(!categories){ + labelItem->setText(ticksList.at(i)); + QPointF center = labelItem->boundingRect().center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); + } else if(i>0){ + labelItem->setText(ticksList.at(i)); + QPointF center = labelItem->boundingRect().center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y()); + } + if((i+1)%2 && i>1) { QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); diff --git a/src/axis/axisitem_p.h b/src/axis/axisitem_p.h index 18d4338..11e1b1b 100644 --- a/src/axis/axisitem_p.h +++ b/src/axis/axisitem_p.h @@ -1,7 +1,6 @@ #ifndef AXISITEM_H_ #define AXISITEM_H_ -#include "domain_p.h" #include "chart_p.h" #include diff --git a/src/axis/qchartaxiscategories.cpp b/src/axis/qchartaxiscategories.cpp index 1f7d35b..6b336a3 100644 --- a/src/axis/qchartaxiscategories.cpp +++ b/src/axis/qchartaxiscategories.cpp @@ -28,22 +28,30 @@ void QChartAxisCategories::insert(qreal value,QString label) m_map.insert(value,label); emit updated(); } + void QChartAxisCategories::remove(qreal value) { m_map.remove(value); emit updated(); } + void QChartAxisCategories::clear() { m_map.clear(); emit updated(); } + int QChartAxisCategories::count() { return m_map.count(); emit updated(); } +QList QChartAxisCategories::values() const +{ + return m_map.keys(); +} + QString QChartAxisCategories::label(qreal value) const { return m_map.value(value); diff --git a/src/axis/qchartaxiscategories.h b/src/axis/qchartaxiscategories.h index 897e611..149a0b3 100644 --- a/src/axis/qchartaxiscategories.h +++ b/src/axis/qchartaxiscategories.h @@ -17,6 +17,7 @@ public: void insert(const QBarCategories& category); void insert(qreal value,QString label); void remove(qreal value); + QList values() const; QString label(qreal value) const; void clear(); int count(); diff --git a/src/chart_p.h b/src/chart_p.h index 1e7d123..7d6b14c 100644 --- a/src/chart_p.h +++ b/src/chart_p.h @@ -2,7 +2,6 @@ #define CHART_H_ #include "qchartglobal.h" -#include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE