diff --git a/examples/examples.pro b/examples/examples.pro index 72d5d4f..e13e5ab 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -30,4 +30,5 @@ SUBDIRS += \ donut \ donutbreakdown \ scrollchart \ - datetimeaxis + datetimeaxis \ + populationpyramid diff --git a/examples/populationpyramid/main.cpp b/examples/populationpyramid/main.cpp new file mode 100644 index 0000000..933f699 --- /dev/null +++ b/examples/populationpyramid/main.cpp @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + +//![1] + QBarSet *male = new QBarSet("Male"); + QBarSet *female = new QBarSet("Female"); + + // From wikipedia + *male << -145596 << -149894 << -167327 << -164118 << -170710 << -169408 << -158395 << -176975 << -191803 << -191198 << -196815 + << -207826 << -145517 << -113204 << -90986 << -70909 << -40013 << -15887 << -5769; + + *female << 139353 << 143522 << 161212 << 156824 << 163226 << 161766 << 150595 << 170779 << 185979 << 187897 << 196158 + << 209062 << 152460 << 129230 << 114778 << 109611 << 83845 << 44605 << 22328; + +//![1] + +//![2] + QHorizontalBarSeries *series = new QHorizontalBarSeries(); + series->append(male); + series->append(female); + series->setBarWidth(0.5); + series->setGrouping(false); +//![2] + +//![3] + QChart* chart = new QChart(); + chart->addSeries(series); + chart->setTitle("Population of Finland in 2005 by age group"); + chart->createDefaultAxes(); + chart->setAnimationOptions(QChart::SeriesAnimations); +//![3] + +//![4] + QStringList categories; + categories << "0-4" << "5-9" << "10-14" << "15-19" << "20-24" << "25-29" << "30-34" << "35-39" << "40-44" << "45-49" + << "50-54" << "55-59" << "60-64" << "65-69" << "70-74" << "75-79" << "80-84" << "85-89" << "90-"; + + QBarCategoriesAxis* axis = new QBarCategoriesAxis(); + axis->append(categories); + chart->setAxisY(axis,series); +//![4] + +//![5] + chart->legend()->setVisible(true); + chart->legend()->setAlignment(Qt::AlignBottom); +//![5] + +//![6] + QChartView* chartView = new QChartView(chart); + chartView->setRenderHint(QPainter::Antialiasing); +//![6] + +//![7] + QMainWindow window; + window.setCentralWidget(chartView); + window.resize(400, 800); + window.show(); +//![7] + + return a.exec(); +} diff --git a/examples/populationpyramid/populationpyramid.pro b/examples/populationpyramid/populationpyramid.pro new file mode 100644 index 0000000..155b83e --- /dev/null +++ b/examples/populationpyramid/populationpyramid.pro @@ -0,0 +1,6 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +TARGET = populationpyramid +SOURCES += main.cpp diff --git a/src/barchart/abstractbarchartitem_p.h b/src/barchart/abstractbarchartitem_p.h index c9d3a37..f45c7e2 100644 --- a/src/barchart/abstractbarchartitem_p.h +++ b/src/barchart/abstractbarchartitem_p.h @@ -81,8 +81,7 @@ protected: AbstractBarAnimation *m_animation; - // Not owned. - QAbstractBarSeries *m_series; + QAbstractBarSeries *m_series; // Not owned. QList m_bars; QList m_labels; }; diff --git a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp index e3d741d..a4e21ca 100644 --- a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp +++ b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp @@ -46,7 +46,14 @@ QVector HorizontalBarChartItem::calculateLayout() qreal rangeX = m_domainMaxX - m_domainMinX; qreal scaleY = (height / rangeY); qreal scaleX = (width / rangeX); - qreal barHeight = (scaleY / setCount) * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect. + qreal barHeight; + + // On horizontal chart barWidth of the barseries means height of the rect. + if (m_series->d_func()->m_grouping) { + barHeight = (scaleY / setCount) * m_series->d_func()->barWidth(); + } else { + barHeight = scaleY * m_series->d_func()->barWidth(); + } int itemIndex(0); for (int category = 0; category < categoryCount; category++) { @@ -55,8 +62,12 @@ QVector HorizontalBarChartItem::calculateLayout() QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY; - yPos += setCount*barHeight/2; - yPos -= set*barHeight; + if (m_series->d_func()->m_grouping) { + yPos += setCount*barHeight/2; + yPos -= set*barHeight; + } else { + yPos += barHeight/2; + } qreal barWidth = barSet->value(category) * scaleX; Bar* bar = m_bars.at(itemIndex); diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index eaf39ab..1f8e087 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -357,13 +357,22 @@ bool QAbstractBarSeries::isLabelsVisible() const return d->m_labelsVisible; } +void QAbstractBarSeries::setGrouping(bool grouping) +{ + Q_D(QAbstractBarSeries); + d->setGrouping(grouping); +} + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) : QAbstractSeriesPrivate(q), m_barWidth(0.5), // Default value is 50% of category width m_labelsVisible(false), - m_visible(true) + m_visible(true), + m_grouping(true) { } @@ -411,6 +420,14 @@ void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) emit labelsVisibleChanged(visible); } +void QAbstractBarSeriesPrivate::setGrouping(bool grouping) +{ + if (m_grouping != grouping) { + m_grouping = grouping; + emit updatedBars(); + } +} + qreal QAbstractBarSeriesPrivate::min() { if (m_barSets.count() <= 0) { diff --git a/src/barchart/qabstractbarseries.h b/src/barchart/qabstractbarseries.h index 54195af..764b075 100644 --- a/src/barchart/qabstractbarseries.h +++ b/src/barchart/qabstractbarseries.h @@ -58,6 +58,10 @@ public: void setLabelsVisible(bool visible = true); bool isLabelsVisible() const; + // TODO. Good name for this. This affects how bars are laid out on chart. (for example with pyramid chart) + // Also do we support same for stacked and percent bar charts or just normal bar charts? + void setGrouping(bool grouping = true); + protected: explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0); diff --git a/src/barchart/qabstractbarseries_p.h b/src/barchart/qabstractbarseries_p.h index 0ba8f1a..6212484 100644 --- a/src/barchart/qabstractbarseries_p.h +++ b/src/barchart/qabstractbarseries_p.h @@ -52,6 +52,7 @@ public: void setVisible(bool visible); void setLabelsVisible(bool visible); + void setGrouping(bool grouping); void scaleDomain(Domain& domain); ChartElement* createGraphics(ChartPresenter* presenter); @@ -91,9 +92,12 @@ protected: qreal m_barWidth; bool m_labelsVisible; bool m_visible; + bool m_grouping; private: Q_DECLARE_PUBLIC(QAbstractBarSeries) + friend class HorizontalBarChartItem; + friend class BarChartItem; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/vertical/bar/barchartitem.cpp b/src/barchart/vertical/bar/barchartitem.cpp index 9625c03..579fb44 100644 --- a/src/barchart/vertical/bar/barchartitem.cpp +++ b/src/barchart/vertical/bar/barchartitem.cpp @@ -48,7 +48,13 @@ QVector BarChartItem::calculateLayout() qreal rangeX = m_domainMaxX - m_domainMinX; qreal scaleY = (height / rangeY); qreal scaleX = (width / rangeX); - qreal barWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); + qreal barWidth; + + if (m_series->d_func()->m_grouping) { + barWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); + } else { + barWidth = scaleX * m_series->d_func()->barWidth(); + } int itemIndex(0); for (int category = 0; category < categoryCount; category++) { @@ -56,9 +62,16 @@ QVector BarChartItem::calculateLayout() for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left(); - xPos -= setCount*barWidth/2; - xPos += set*barWidth; + qreal xPos; + + if (m_series->d_func()->m_grouping) { + xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left(); + xPos -= setCount*barWidth/2; + xPos += set*barWidth; + } else { + xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + } + qreal barHeight = barSet->value(category) * scaleY; Bar* bar = m_bars.at(itemIndex);