@@ -0,0 +1,93 | |||||
|
1 | /**************************************************************************** | |||
|
2 | ** | |||
|
3 | ** Copyright (C) 2012 Digia Plc | |||
|
4 | ** All rights reserved. | |||
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |||
|
6 | ** | |||
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |||
|
8 | ** | |||
|
9 | ** $QT_BEGIN_LICENSE$ | |||
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |||
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |||
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |||
|
13 | ** a written agreement between you and Digia. | |||
|
14 | ** | |||
|
15 | ** If you have questions regarding the use of this file, please use | |||
|
16 | ** contact form at http://qt.digia.com | |||
|
17 | ** $QT_END_LICENSE$ | |||
|
18 | ** | |||
|
19 | ****************************************************************************/ | |||
|
20 | ||||
|
21 | #include <QApplication> | |||
|
22 | #include <QMainWindow> | |||
|
23 | #include <QChartView> | |||
|
24 | #include <QBarSeries> | |||
|
25 | #include <QBarSet> | |||
|
26 | #include <QLegend> | |||
|
27 | #include <QBarCategoriesAxis> | |||
|
28 | #include <QHorizontalBarSeries> | |||
|
29 | ||||
|
30 | QTCOMMERCIALCHART_USE_NAMESPACE | |||
|
31 | ||||
|
32 | int main(int argc, char *argv[]) | |||
|
33 | { | |||
|
34 | QApplication a(argc, argv); | |||
|
35 | ||||
|
36 | //![1] | |||
|
37 | QBarSet *male = new QBarSet("Male"); | |||
|
38 | QBarSet *female = new QBarSet("Female"); | |||
|
39 | ||||
|
40 | // From wikipedia | |||
|
41 | *male << -145596 << -149894 << -167327 << -164118 << -170710 << -169408 << -158395 << -176975 << -191803 << -191198 << -196815 | |||
|
42 | << -207826 << -145517 << -113204 << -90986 << -70909 << -40013 << -15887 << -5769; | |||
|
43 | ||||
|
44 | *female << 139353 << 143522 << 161212 << 156824 << 163226 << 161766 << 150595 << 170779 << 185979 << 187897 << 196158 | |||
|
45 | << 209062 << 152460 << 129230 << 114778 << 109611 << 83845 << 44605 << 22328; | |||
|
46 | ||||
|
47 | //![1] | |||
|
48 | ||||
|
49 | //![2] | |||
|
50 | QHorizontalBarSeries *series = new QHorizontalBarSeries(); | |||
|
51 | series->append(male); | |||
|
52 | series->append(female); | |||
|
53 | series->setBarWidth(0.5); | |||
|
54 | series->setGrouping(false); | |||
|
55 | //![2] | |||
|
56 | ||||
|
57 | //![3] | |||
|
58 | QChart* chart = new QChart(); | |||
|
59 | chart->addSeries(series); | |||
|
60 | chart->setTitle("Population of Finland in 2005 by age group"); | |||
|
61 | chart->createDefaultAxes(); | |||
|
62 | chart->setAnimationOptions(QChart::SeriesAnimations); | |||
|
63 | //![3] | |||
|
64 | ||||
|
65 | //![4] | |||
|
66 | QStringList categories; | |||
|
67 | categories << "0-4" << "5-9" << "10-14" << "15-19" << "20-24" << "25-29" << "30-34" << "35-39" << "40-44" << "45-49" | |||
|
68 | << "50-54" << "55-59" << "60-64" << "65-69" << "70-74" << "75-79" << "80-84" << "85-89" << "90-"; | |||
|
69 | ||||
|
70 | QBarCategoriesAxis* axis = new QBarCategoriesAxis(); | |||
|
71 | axis->append(categories); | |||
|
72 | chart->setAxisY(axis,series); | |||
|
73 | //![4] | |||
|
74 | ||||
|
75 | //![5] | |||
|
76 | chart->legend()->setVisible(true); | |||
|
77 | chart->legend()->setAlignment(Qt::AlignBottom); | |||
|
78 | //![5] | |||
|
79 | ||||
|
80 | //![6] | |||
|
81 | QChartView* chartView = new QChartView(chart); | |||
|
82 | chartView->setRenderHint(QPainter::Antialiasing); | |||
|
83 | //![6] | |||
|
84 | ||||
|
85 | //![7] | |||
|
86 | QMainWindow window; | |||
|
87 | window.setCentralWidget(chartView); | |||
|
88 | window.resize(400, 800); | |||
|
89 | window.show(); | |||
|
90 | //![7] | |||
|
91 | ||||
|
92 | return a.exec(); | |||
|
93 | } |
@@ -0,0 +1,6 | |||||
|
1 | !include( ../examples.pri ) { | |||
|
2 | error( "Couldn't find the examples.pri file!" ) | |||
|
3 | } | |||
|
4 | ||||
|
5 | TARGET = populationpyramid | |||
|
6 | SOURCES += main.cpp |
@@ -30,4 +30,5 SUBDIRS += \ | |||||
30 | donut \ |
|
30 | donut \ | |
31 | donutbreakdown \ |
|
31 | donutbreakdown \ | |
32 | scrollchart \ |
|
32 | scrollchart \ | |
33 | datetimeaxis |
|
33 | datetimeaxis \ | |
|
34 | populationpyramid |
@@ -81,8 +81,7 protected: | |||||
81 |
|
81 | |||
82 | AbstractBarAnimation *m_animation; |
|
82 | AbstractBarAnimation *m_animation; | |
83 |
|
83 | |||
84 | // Not owned. |
|
84 | QAbstractBarSeries *m_series; // Not owned. | |
85 | QAbstractBarSeries *m_series; |
|
|||
86 | QList<Bar *> m_bars; |
|
85 | QList<Bar *> m_bars; | |
87 | QList<QGraphicsSimpleTextItem *> m_labels; |
|
86 | QList<QGraphicsSimpleTextItem *> m_labels; | |
88 | }; |
|
87 | }; |
@@ -46,7 +46,14 QVector<QRectF> HorizontalBarChartItem::calculateLayout() | |||||
46 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
46 | qreal rangeX = m_domainMaxX - m_domainMinX; | |
47 | qreal scaleY = (height / rangeY); |
|
47 | qreal scaleY = (height / rangeY); | |
48 | qreal scaleX = (width / rangeX); |
|
48 | qreal scaleX = (width / rangeX); | |
49 | qreal barHeight = (scaleY / setCount) * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect. |
|
49 | qreal barHeight; | |
|
50 | ||||
|
51 | // On horizontal chart barWidth of the barseries means height of the rect. | |||
|
52 | if (m_series->d_func()->m_grouping) { | |||
|
53 | barHeight = (scaleY / setCount) * m_series->d_func()->barWidth(); | |||
|
54 | } else { | |||
|
55 | barHeight = scaleY * m_series->d_func()->barWidth(); | |||
|
56 | } | |||
50 |
|
57 | |||
51 | int itemIndex(0); |
|
58 | int itemIndex(0); | |
52 | for (int category = 0; category < categoryCount; category++) { |
|
59 | for (int category = 0; category < categoryCount; category++) { | |
@@ -55,8 +62,12 QVector<QRectF> HorizontalBarChartItem::calculateLayout() | |||||
55 | QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); |
|
62 | QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); | |
56 |
|
63 | |||
57 | qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY; |
|
64 | qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY; | |
|
65 | if (m_series->d_func()->m_grouping) { | |||
58 | yPos += setCount*barHeight/2; |
|
66 | yPos += setCount*barHeight/2; | |
59 | yPos -= set*barHeight; |
|
67 | yPos -= set*barHeight; | |
|
68 | } else { | |||
|
69 | yPos += barHeight/2; | |||
|
70 | } | |||
60 |
|
71 | |||
61 | qreal barWidth = barSet->value(category) * scaleX; |
|
72 | qreal barWidth = barSet->value(category) * scaleX; | |
62 | Bar* bar = m_bars.at(itemIndex); |
|
73 | Bar* bar = m_bars.at(itemIndex); |
@@ -357,13 +357,22 bool QAbstractBarSeries::isLabelsVisible() const | |||||
357 | return d->m_labelsVisible; |
|
357 | return d->m_labelsVisible; | |
358 | } |
|
358 | } | |
359 |
|
359 | |||
|
360 | void QAbstractBarSeries::setGrouping(bool grouping) | |||
|
361 | { | |||
|
362 | Q_D(QAbstractBarSeries); | |||
|
363 | d->setGrouping(grouping); | |||
|
364 | } | |||
|
365 | ||||
|
366 | ||||
|
367 | ||||
360 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
368 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
361 |
|
369 | |||
362 | QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) : |
|
370 | QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) : | |
363 | QAbstractSeriesPrivate(q), |
|
371 | QAbstractSeriesPrivate(q), | |
364 | m_barWidth(0.5), // Default value is 50% of category width |
|
372 | m_barWidth(0.5), // Default value is 50% of category width | |
365 | m_labelsVisible(false), |
|
373 | m_labelsVisible(false), | |
366 | m_visible(true) |
|
374 | m_visible(true), | |
|
375 | m_grouping(true) | |||
367 | { |
|
376 | { | |
368 | } |
|
377 | } | |
369 |
|
378 | |||
@@ -411,6 +420,14 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) | |||||
411 | emit labelsVisibleChanged(visible); |
|
420 | emit labelsVisibleChanged(visible); | |
412 | } |
|
421 | } | |
413 |
|
422 | |||
|
423 | void QAbstractBarSeriesPrivate::setGrouping(bool grouping) | |||
|
424 | { | |||
|
425 | if (m_grouping != grouping) { | |||
|
426 | m_grouping = grouping; | |||
|
427 | emit updatedBars(); | |||
|
428 | } | |||
|
429 | } | |||
|
430 | ||||
414 | qreal QAbstractBarSeriesPrivate::min() |
|
431 | qreal QAbstractBarSeriesPrivate::min() | |
415 | { |
|
432 | { | |
416 | if (m_barSets.count() <= 0) { |
|
433 | if (m_barSets.count() <= 0) { |
@@ -58,6 +58,10 public: | |||||
58 | void setLabelsVisible(bool visible = true); |
|
58 | void setLabelsVisible(bool visible = true); | |
59 | bool isLabelsVisible() const; |
|
59 | bool isLabelsVisible() const; | |
60 |
|
60 | |||
|
61 | // TODO. Good name for this. This affects how bars are laid out on chart. (for example with pyramid chart) | |||
|
62 | // Also do we support same for stacked and percent bar charts or just normal bar charts? | |||
|
63 | void setGrouping(bool grouping = true); | |||
|
64 | ||||
61 | protected: |
|
65 | protected: | |
62 | explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0); |
|
66 | explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0); | |
63 |
|
67 |
@@ -52,6 +52,7 public: | |||||
52 |
|
52 | |||
53 | void setVisible(bool visible); |
|
53 | void setVisible(bool visible); | |
54 | void setLabelsVisible(bool visible); |
|
54 | void setLabelsVisible(bool visible); | |
|
55 | void setGrouping(bool grouping); | |||
55 |
|
56 | |||
56 | void scaleDomain(Domain& domain); |
|
57 | void scaleDomain(Domain& domain); | |
57 | ChartElement* createGraphics(ChartPresenter* presenter); |
|
58 | ChartElement* createGraphics(ChartPresenter* presenter); | |
@@ -91,9 +92,12 protected: | |||||
91 | qreal m_barWidth; |
|
92 | qreal m_barWidth; | |
92 | bool m_labelsVisible; |
|
93 | bool m_labelsVisible; | |
93 | bool m_visible; |
|
94 | bool m_visible; | |
|
95 | bool m_grouping; | |||
94 |
|
96 | |||
95 | private: |
|
97 | private: | |
96 | Q_DECLARE_PUBLIC(QAbstractBarSeries) |
|
98 | Q_DECLARE_PUBLIC(QAbstractBarSeries) | |
|
99 | friend class HorizontalBarChartItem; | |||
|
100 | friend class BarChartItem; | |||
97 | }; |
|
101 | }; | |
98 |
|
102 | |||
99 | QTCOMMERCIALCHART_END_NAMESPACE |
|
103 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -48,7 +48,13 QVector<QRectF> BarChartItem::calculateLayout() | |||||
48 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
48 | qreal rangeX = m_domainMaxX - m_domainMinX; | |
49 | qreal scaleY = (height / rangeY); |
|
49 | qreal scaleY = (height / rangeY); | |
50 | qreal scaleX = (width / rangeX); |
|
50 | qreal scaleX = (width / rangeX); | |
51 | qreal barWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); |
|
51 | qreal barWidth; | |
|
52 | ||||
|
53 | if (m_series->d_func()->m_grouping) { | |||
|
54 | barWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); | |||
|
55 | } else { | |||
|
56 | barWidth = scaleX * m_series->d_func()->barWidth(); | |||
|
57 | } | |||
52 |
|
58 | |||
53 | int itemIndex(0); |
|
59 | int itemIndex(0); | |
54 | for (int category = 0; category < categoryCount; category++) { |
|
60 | for (int category = 0; category < categoryCount; category++) { | |
@@ -56,9 +62,16 QVector<QRectF> BarChartItem::calculateLayout() | |||||
56 | for (int set = 0; set < setCount; set++) { |
|
62 | for (int set = 0; set < setCount; set++) { | |
57 | QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); |
|
63 | QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); | |
58 |
|
64 | |||
59 | qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left(); |
|
65 | qreal xPos; | |
|
66 | ||||
|
67 | if (m_series->d_func()->m_grouping) { | |||
|
68 | xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left(); | |||
60 | xPos -= setCount*barWidth/2; |
|
69 | xPos -= setCount*barWidth/2; | |
61 | xPos += set*barWidth; |
|
70 | xPos += set*barWidth; | |
|
71 | } else { | |||
|
72 | xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; | |||
|
73 | } | |||
|
74 | ||||
62 | qreal barHeight = barSet->value(category) * scaleY; |
|
75 | qreal barHeight = barSet->value(category) * scaleY; | |
63 | Bar* bar = m_bars.at(itemIndex); |
|
76 | Bar* bar = m_bars.at(itemIndex); | |
64 |
|
77 |
General Comments 0
You need to be logged in to leave comments.
Login now