diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp index 1667f0c..34d7e84 100644 --- a/src/charts/axis/chartaxiselement.cpp +++ b/src/charts/axis/chartaxiselement.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -105,6 +106,13 @@ void ChartAxisElement::connectSlots() this, SLOT(handleMinorGridVisibleChanged(bool))); QObject::connect(axis(), SIGNAL(minorGridLinePenChanged(const QPen&)), this, SLOT(handleMinorGridPenChanged(const QPen&))); + + if (axis()->type() == QAbstractAxis::AxisTypeCategory) { + QCategoryAxis *categoryAxis = static_cast(axis()); + QObject::connect(categoryAxis, + SIGNAL(labelsPositionChanged(QCategoryAxis::AxisLabelsPosition)), + this, SLOT(handleLabelsPositionChanged())); + } } void ChartAxisElement::handleArrowVisibleChanged(bool visible) @@ -127,6 +135,12 @@ void ChartAxisElement::handleMinorGridVisibleChanged(bool visible) m_minorGrid->setVisible(visible); } +void ChartAxisElement::handleLabelsPositionChanged() +{ + QGraphicsLayoutItem::updateGeometry(); + presenter()->layout()->invalidate(); +} + void ChartAxisElement::handleLabelsVisibleChanged(bool visible) { QGraphicsLayoutItem::updateGeometry(); diff --git a/src/charts/axis/chartaxiselement_p.h b/src/charts/axis/chartaxiselement_p.h index 3ca2797..1827542 100644 --- a/src/charts/axis/chartaxiselement_p.h +++ b/src/charts/axis/chartaxiselement_p.h @@ -132,7 +132,7 @@ public Q_SLOTS: void handleReverseChanged(bool reverse); void handleMinorArrowVisibleChanged(bool visible); void handleMinorGridVisibleChanged(bool visible); - + void handleLabelsPositionChanged(); Q_SIGNALS: void clicked(); diff --git a/src/charts/axis/polarchartaxisangular.cpp b/src/charts/axis/polarchartaxisangular.cpp index b09ebc0..d1e6547 100644 --- a/src/charts/axis/polarchartaxisangular.cpp +++ b/src/charts/axis/polarchartaxisangular.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -93,7 +94,7 @@ void PolarChartAxisAngular::updateGeometry() } qreal labelCoordinate = angularCoordinate; - qreal labelVisible = currentTickVisible; + bool labelVisible = currentTickVisible; if (intervalAxis()) { qreal farEdge; if (i == (layout.size() - 1)) @@ -105,12 +106,23 @@ void PolarChartAxisAngular::updateGeometry() if (nextTickVisible) labelCoordinate = qMax(qreal(0.0), labelCoordinate); - labelCoordinate = (labelCoordinate + farEdge) / 2.0; - // Don't display label once the category gets too small near the axis - if (labelCoordinate < 5.0 || labelCoordinate > 355.0) - labelVisible = false; - else - labelVisible = true; + bool centeredLabel = true; + if (axis()->type() == QAbstractAxis::AxisTypeCategory) { + QCategoryAxis *categoryAxis = static_cast(axis()); + if (categoryAxis->labelsPosition() == QCategoryAxis::AxisLabelsPositionOnValue) + centeredLabel = false; + } + if (centeredLabel) { + labelCoordinate = (labelCoordinate + farEdge) / 2.0; + // Don't display label once the category gets too small near the axis + if (labelCoordinate < 5.0 || labelCoordinate > 355.0) + labelVisible = false; + else + labelVisible = true; + } else { + labelVisible = nextTickVisible; + labelCoordinate = farEdge; + } } // Need this also in label calculations, so determine it first diff --git a/src/charts/axis/polarchartaxisradial.cpp b/src/charts/axis/polarchartaxisradial.cpp index af22a03..8cff0c2 100644 --- a/src/charts/axis/polarchartaxisradial.cpp +++ b/src/charts/axis/polarchartaxisradial.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include QT_CHARTS_BEGIN_NAMESPACE @@ -85,8 +86,9 @@ void PolarChartAxisRadial::updateGeometry() } qreal labelCoordinate = radialCoordinate; - qreal labelVisible = currentTickVisible; + bool labelVisible = currentTickVisible; qreal labelPad = labelPadding() / 2.0; + bool centeredLabel = true; // Only used with interval axes if (intervalAxis()) { qreal farEdge; if (i == (layout.size() - 1)) @@ -98,11 +100,21 @@ void PolarChartAxisRadial::updateGeometry() if (nextTickVisible) labelCoordinate = qMax(qreal(0.0), labelCoordinate); - labelCoordinate = (labelCoordinate + farEdge) / 2.0; - if (labelCoordinate > 0.0 && labelCoordinate < radius) - labelVisible = true; - else - labelVisible = false; + if (axis()->type() == QAbstractAxis::AxisTypeCategory) { + QCategoryAxis *categoryAxis = static_cast(axis()); + if (categoryAxis->labelsPosition() == QCategoryAxis::AxisLabelsPositionOnValue) + centeredLabel = false; + } + if (centeredLabel) { + labelCoordinate = (labelCoordinate + farEdge) / 2.0; + if (labelCoordinate > 0.0 && labelCoordinate < radius) + labelVisible = true; + else + labelVisible = false; + } else { + labelVisible = nextTickVisible; + labelCoordinate = farEdge; + } } // Radial axis label @@ -118,7 +130,7 @@ void PolarChartAxisRadial::updateGeometry() boundingRect.moveCenter(labelCenter); QPointF positionDiff(labelRect.topLeft() - boundingRect.topLeft()); QPointF labelPoint = center; - if (intervalAxis()) + if (intervalAxis() && centeredLabel) labelPoint += QPointF(labelPad, -labelCoordinate - (boundingRect.height() / 2.0)); else labelPoint += QPointF(labelPad, labelPad - labelCoordinate); diff --git a/tests/manual/polarcharttest/mainwindow.cpp b/tests/manual/polarcharttest/mainwindow.cpp index c8ae0ea..e2fff3b 100644 --- a/tests/manual/polarcharttest/mainwindow.cpp +++ b/tests/manual/polarcharttest/mainwindow.cpp @@ -127,44 +127,84 @@ MainWindow::MainWindow(QWidget *parent) : ui->angularAxisComboBox->setCurrentIndex(int(m_angularAxisMode)); ui->radialAxisComboBox->setCurrentIndex(int(m_radialAxisMode)); - connect(ui->angularTicksSpin, SIGNAL(valueChanged(int)), this, SLOT(angularTicksChanged(int))); - connect(ui->radialTicksSpin, SIGNAL(valueChanged(int)), this, SLOT(radialTicksChanged(int))); - connect(ui->angularMinorTicksSpin, SIGNAL(valueChanged(int)), this, SLOT(angularMinorTicksChanged(int))); - connect(ui->radialMinorTicksSpin, SIGNAL(valueChanged(int)), this, SLOT(radialMinorTicksChanged(int))); - connect(ui->anglesSpin, SIGNAL(valueChanged(int)), this, SLOT(anglesChanged(int))); - connect(ui->radialMinSpin, SIGNAL(valueChanged(double)), this, SLOT(radialMinChanged(double))); - connect(ui->radialMaxSpin, SIGNAL(valueChanged(double)), this, SLOT(radialMaxChanged(double))); - connect(ui->angularMinSpin, SIGNAL(valueChanged(double)), this, SLOT(angularMinChanged(double))); - connect(ui->angularMaxSpin, SIGNAL(valueChanged(double)), this, SLOT(angularMaxChanged(double))); - connect(ui->angularShadesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(angularShadesIndexChanged(int))); - connect(ui->radialShadesComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(radialShadesIndexChanged(int))); - connect(ui->animationsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(animationIndexChanged(int))); - connect(ui->labelFormatEdit, SIGNAL(textEdited(QString)), this, SLOT(labelFormatEdited(QString))); - connect(ui->labelFontComboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(labelFontChanged(QFont))); - connect(ui->labelFontSizeSpin, SIGNAL(valueChanged(int)), this, SLOT(labelFontSizeChanged(int))); - connect(ui->labelComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(labelsIndexChanged(int))); - connect(ui->titleFontComboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(titleFontChanged(QFont))); - connect(ui->titleFontSizeSpin, SIGNAL(valueChanged(int)), this, SLOT(titleFontSizeChanged(int))); - connect(ui->titleComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(titleIndexChanged(int))); - connect(ui->gridComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(gridIndexChanged(int))); - connect(ui->minorGridComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(minorGridIndexChanged(int))); - connect(ui->arrowComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(arrowIndexChanged(int))); - connect(ui->logBaseSpin, SIGNAL(valueChanged(double)), this, SLOT(logBaseChanged(double))); - connect(ui->angularAxisComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(angularAxisIndexChanged(int))); - connect(ui->radialAxisComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(radialAxisIndexChanged(int))); - connect(ui->niceNumbersCheckBox, SIGNAL(clicked()), this, SLOT(niceNumbersChecked())); - connect(ui->dateFormatEdit, SIGNAL(textEdited(QString)), this, SLOT(dateFormatEdited(QString))); - connect(ui->moreCategoriesCheckBox, SIGNAL(clicked()), this, SLOT(moreCategoriesChecked())); - connect(ui->series1checkBox, SIGNAL(clicked()), this, SLOT(series1CheckBoxChecked())); - connect(ui->series2checkBox, SIGNAL(clicked()), this, SLOT(series2CheckBoxChecked())); - connect(ui->series3checkBox, SIGNAL(clicked()), this, SLOT(series3CheckBoxChecked())); - connect(ui->series4checkBox, SIGNAL(clicked()), this, SLOT(series4CheckBoxChecked())); - connect(ui->series5checkBox, SIGNAL(clicked()), this, SLOT(series5CheckBoxChecked())); - connect(ui->series6checkBox, SIGNAL(clicked()), this, SLOT(series6CheckBoxChecked())); - connect(ui->series7checkBox, SIGNAL(clicked()), this, SLOT(series7CheckBoxChecked())); - connect(ui->themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(themeIndexChanged(int))); - connect(ui->backgroundComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(backgroundIndexChanged(int))); - connect(ui->plotAreaComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(plotAreaIndexChanged(int))); + connect(ui->angularTicksSpin, SIGNAL(valueChanged(int)), + this, SLOT(angularTicksChanged(int))); + connect(ui->radialTicksSpin, SIGNAL(valueChanged(int)), + this, SLOT(radialTicksChanged(int))); + connect(ui->angularMinorTicksSpin, SIGNAL(valueChanged(int)), + this, SLOT(angularMinorTicksChanged(int))); + connect(ui->radialMinorTicksSpin, SIGNAL(valueChanged(int)), + this, SLOT(radialMinorTicksChanged(int))); + connect(ui->anglesSpin, SIGNAL(valueChanged(int)), + this, SLOT(anglesChanged(int))); + connect(ui->radialMinSpin, SIGNAL(valueChanged(double)), + this, SLOT(radialMinChanged(double))); + connect(ui->radialMaxSpin, SIGNAL(valueChanged(double)), + this, SLOT(radialMaxChanged(double))); + connect(ui->angularMinSpin, SIGNAL(valueChanged(double)), + this, SLOT(angularMinChanged(double))); + connect(ui->angularMaxSpin, SIGNAL(valueChanged(double)), + this, SLOT(angularMaxChanged(double))); + connect(ui->angularShadesComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(angularShadesIndexChanged(int))); + connect(ui->radialShadesComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(radialShadesIndexChanged(int))); + connect(ui->animationsComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(animationIndexChanged(int))); + connect(ui->labelFormatEdit, SIGNAL(textEdited(QString)), + this, SLOT(labelFormatEdited(QString))); + connect(ui->labelFontComboBox, SIGNAL(currentFontChanged(QFont)), + this, SLOT(labelFontChanged(QFont))); + connect(ui->labelFontSizeSpin, SIGNAL(valueChanged(int)), + this, SLOT(labelFontSizeChanged(int))); + connect(ui->labelComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(labelsIndexChanged(int))); + connect(ui->titleFontComboBox, SIGNAL(currentFontChanged(QFont)), + this, SLOT(titleFontChanged(QFont))); + connect(ui->titleFontSizeSpin, SIGNAL(valueChanged(int)), + this, SLOT(titleFontSizeChanged(int))); + connect(ui->titleComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(titleIndexChanged(int))); + connect(ui->gridComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(gridIndexChanged(int))); + connect(ui->minorGridComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(minorGridIndexChanged(int))); + connect(ui->arrowComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(arrowIndexChanged(int))); + connect(ui->logBaseSpin, SIGNAL(valueChanged(double)), + this, SLOT(logBaseChanged(double))); + connect(ui->angularAxisComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(angularAxisIndexChanged(int))); + connect(ui->radialAxisComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(radialAxisIndexChanged(int))); + connect(ui->niceNumbersCheckBox, SIGNAL(clicked()), + this, SLOT(niceNumbersChecked())); + connect(ui->dateFormatEdit, SIGNAL(textEdited(QString)), + this, SLOT(dateFormatEdited(QString))); + connect(ui->moreCategoriesCheckBox, SIGNAL(clicked()), + this, SLOT(moreCategoriesChecked())); + connect(ui->categoryLabelLocationCheckBox, SIGNAL(clicked()), + this, SLOT(categoryLabelLocationChecked())); + connect(ui->series1checkBox, SIGNAL(clicked()), + this, SLOT(series1CheckBoxChecked())); + connect(ui->series2checkBox, SIGNAL(clicked()), + this, SLOT(series2CheckBoxChecked())); + connect(ui->series3checkBox, SIGNAL(clicked()), + this, SLOT(series3CheckBoxChecked())); + connect(ui->series4checkBox, SIGNAL(clicked()), + this, SLOT(series4CheckBoxChecked())); + connect(ui->series5checkBox, SIGNAL(clicked()), + this, SLOT(series5CheckBoxChecked())); + connect(ui->series6checkBox, SIGNAL(clicked()), + this, SLOT(series6CheckBoxChecked())); + connect(ui->series7checkBox, SIGNAL(clicked()), + this, SLOT(series7CheckBoxChecked())); + connect(ui->themeComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(themeIndexChanged(int))); + connect(ui->backgroundComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(backgroundIndexChanged(int))); + connect(ui->plotAreaComboBox, SIGNAL(currentIndexChanged(int)), + this, SLOT(plotAreaIndexChanged(int))); ui->chartView->setChart(m_chart); ui->chartView->setRenderHint(QPainter::Antialiasing); @@ -1039,6 +1079,11 @@ void MainWindow::moreCategoriesChecked() m_moreCategories = ui->moreCategoriesCheckBox->isChecked(); } +void MainWindow::categoryLabelLocationChecked() +{ + applyCategories(); +} + void MainWindow::series1CheckBoxChecked() { if (ui->series1checkBox->isChecked()) @@ -1206,6 +1251,10 @@ void MainWindow::applyCategories() angCatAxis->remove("Cat D"); angCatAxis->remove("Cat E"); } + if (ui->categoryLabelLocationCheckBox->isChecked()) + angCatAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); + else + angCatAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionCenter); } if (m_radialAxisMode == AxisModeCategory) { @@ -1231,5 +1280,9 @@ void MainWindow::applyCategories() radCatAxis->remove("Cat 4"); radCatAxis->remove("Cat 5"); } + if (ui->categoryLabelLocationCheckBox->isChecked()) + radCatAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue); + else + radCatAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionCenter); } } diff --git a/tests/manual/polarcharttest/mainwindow.h b/tests/manual/polarcharttest/mainwindow.h index d267481..ec5b2e7 100644 --- a/tests/manual/polarcharttest/mainwindow.h +++ b/tests/manual/polarcharttest/mainwindow.h @@ -79,6 +79,7 @@ public slots: void niceNumbersChecked(); void dateFormatEdited(const QString &text); void moreCategoriesChecked(); + void categoryLabelLocationChecked(); void series1CheckBoxChecked(); void series2CheckBoxChecked(); void series3CheckBoxChecked(); diff --git a/tests/manual/polarcharttest/mainwindow.ui b/tests/manual/polarcharttest/mainwindow.ui index 58ee203..e438b5a 100644 --- a/tests/manual/polarcharttest/mainwindow.ui +++ b/tests/manual/polarcharttest/mainwindow.ui @@ -688,23 +688,23 @@ 10 - 692 - 198 - 19 + 680 + 191 + 31 - - - + + + - Nice Numbers + More Categories - - + + - More Categories + Nice Numbers @@ -714,7 +714,7 @@ 10 - 729 + 740 221 22 @@ -899,6 +899,19 @@ + + + + 10 + 710 + 142 + 17 + + + + Category Label On Tick + +