##// END OF EJS Templates
population pyramid documentation. updated example.
sauimone -
r1866:b747c15751dd
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -0,0 +1,45
1 /*!
2 \example examples/populationpyramid
3 \title Population pyramid example
4 \subtitle
5
6 The example shows how to create a bar chart with bars extending in opposite directions. For example purposes we use population data found from wikipedia.
7
8 \image examples_populationpyramid.png
9
10 First we create two barsets and append the data to them. To make another barset grow to left, the values are negated.
11
12 \snippet ../examples/populationpyramid/main.cpp 1
13
14 We create the series and append the barsets to it. The series takes ownership of the barsets. We want the bars to be at same position on the y-axis, so we turn the
15 overlap drawing on. We also set the bar width to be 50% of the category width.
16
17 \snippet ../examples/populationpyramid/main.cpp 2
18
19 Here we create the chart object and add the series to it. We set the title for chart with setTitle and then turn on animations of the series by calling
20 setAnimationOptions(QChart::SeriesAnimations)
21
22 \snippet ../examples/populationpyramid/main.cpp 3
23
24 To have categories displayed on axis, we need to create a QBarCategoryAxis for that. Here we create a category axis with list of categories and
25 set it to be the y-axis of the chart. The chart takes ownership of axis. For x-axis we use default axis, which is created and scaled to series data
26 by calling createDefaultAxes of the chart. Note that the call for createDefaultAxes must be before we set the category axis. Otherwise the default axis will
27 override the category axis.
28 We also set the range for x-axis, since in this case it gives nicer result than autoscaling.
29
30 \snippet ../examples/populationpyramid/main.cpp 4
31
32 We also want to show the legend. To do that, we get the legend pointer from chart and set it to visible. We also place the legend to bottom of the chart by setting it's alignment
33 to Qt::AlignBottom.
34
35 \snippet ../examples/populationpyramid/main.cpp 5
36
37 Finally we add the chart onto a view. We also turn on the antialiasing for the chartView.
38
39 \snippet ../examples/populationpyramid/main.cpp 6
40
41 Chart is ready to be shown. We set the chart to be central widget of the window.
42 We also set the size for the chart window and show it.
43
44 \snippet ../examples/populationpyramid/main.cpp 7
45 */ No newline at end of file
@@ -25,6 +25,7
25 25 <li><a href="examples-modeldata.html">Model data</a></li>
26 26 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
27 27 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
28 <li><a href="examples-populationpyramid.html">Population pyramid</a></li>
28 29 <li><a href="examples-piechart.html">Pie chart</a></li>
29 30 <li><a href="examples-donutchart.html">Donut chart</a></li>
30 31 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
@@ -37,7 +37,7 int main(int argc, char *argv[])
37 37 QBarSet *male = new QBarSet("Male");
38 38 QBarSet *female = new QBarSet("Female");
39 39
40 // From wikipedia
40 // Not really negative population. Negated values are used to grow bar to left.
41 41 *male << -145596 << -149894 << -167327 << -164118 << -170710 << -169408 << -158395 << -176975 << -191803 << -191198 << -196815
42 42 << -207826 << -145517 << -113204 << -90986 << -70909 << -40013 << -15887 << -5769;
43 43
@@ -51,14 +51,13 int main(int argc, char *argv[])
51 51 series->append(male);
52 52 series->append(female);
53 53 series->setBarWidth(0.5);
54 series->setGrouping(false);
54 series->setOverlap(true);
55 55 //![2]
56 56
57 57 //![3]
58 58 QChart* chart = new QChart();
59 59 chart->addSeries(series);
60 60 chart->setTitle("Population of Finland in 2005 by age group");
61 chart->createDefaultAxes();
62 61 chart->setAnimationOptions(QChart::SeriesAnimations);
63 62 //![3]
64 63
@@ -69,6 +68,7 int main(int argc, char *argv[])
69 68
70 69 QBarCategoryAxis* axis = new QBarCategoryAxis();
71 70 axis->append(categories);
71 chart->createDefaultAxes();
72 72 chart->setAxisY(axis,series);
73 73 chart->axisX(series)->setRange(-210000,210000);
74 74 //![4]
@@ -86,7 +86,7 int main(int argc, char *argv[])
86 86 //![7]
87 87 QMainWindow window;
88 88 window.setCentralWidget(chartView);
89 window.resize(400, 800);
89 window.resize(400, 600);
90 90 window.show();
91 91 //![7]
92 92
@@ -49,10 +49,10 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
49 49 qreal barHeight;
50 50
51 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 {
52 if (m_series->d_func()->m_overlap) {
55 53 barHeight = scaleY * m_series->d_func()->barWidth();
54 } else {
55 barHeight = (scaleY / setCount) * m_series->d_func()->barWidth();
56 56 }
57 57
58 58 int itemIndex(0);
@@ -62,11 +62,11 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
62 62 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
63 63
64 64 qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
65 if (m_series->d_func()->m_grouping) {
65 if (m_series->d_func()->m_overlap) {
66 yPos += barHeight/2;
67 } else {
66 68 yPos += setCount*barHeight/2;
67 69 yPos -= set*barHeight;
68 } else {
69 yPos += barHeight/2;
70 70 }
71 71
72 72 qreal barWidth = barSet->value(category) * scaleX;
@@ -386,14 +386,15 bool QAbstractBarSeries::isLabelsVisible() const
386 386 }
387 387
388 388 /*!
389 Sets the grouped drawing mode for bars. If \a grouping is true, then the bars
389 Sets the overlap drawing mode for bars. If \a overlap is true, then the bars
390 390 are drawn at same position inside group. Can be used for example to draw negative bars
391 at same position on axis than positive bars.
391 at same position on axis than positive bars. By default overlap is false and bars are drawn
392 next to each other. Note that this setting doesn't affect stacked and percent bar series.
392 393 */
393 void QAbstractBarSeries::setGrouping(bool grouping)
394 void QAbstractBarSeries::setOverlap(bool overlap)
394 395 {
395 396 Q_D(QAbstractBarSeries);
396 d->setGrouping(grouping);
397 d->setOverlap(overlap);
397 398 }
398 399
399 400
@@ -405,7 +406,7 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
405 406 m_barWidth(0.5), // Default value is 50% of category width
406 407 m_labelsVisible(false),
407 408 m_visible(true),
408 m_grouping(true)
409 m_overlap(false)
409 410 {
410 411 }
411 412
@@ -453,10 +454,10 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
453 454 emit labelsVisibleChanged(visible);
454 455 }
455 456
456 void QAbstractBarSeriesPrivate::setGrouping(bool grouping)
457 void QAbstractBarSeriesPrivate::setOverlap(bool overlap)
457 458 {
458 if (m_grouping != grouping) {
459 m_grouping = grouping;
459 if (m_overlap != overlap) {
460 m_overlap = overlap;
460 461 emit updatedBars();
461 462 }
462 463 }
@@ -59,7 +59,7 public:
59 59 void setLabelsVisible(bool visible = true);
60 60 bool isLabelsVisible() const;
61 61
62 void setGrouping(bool grouping = true);
62 void setOverlap(bool overlap = true);
63 63
64 64 protected:
65 65 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0);
@@ -52,7 +52,7 public:
52 52
53 53 void setVisible(bool visible);
54 54 void setLabelsVisible(bool visible);
55 void setGrouping(bool grouping);
55 void setOverlap(bool overlap);
56 56
57 57 void scaleDomain(Domain& domain);
58 58 ChartElement* createGraphics(ChartPresenter* presenter);
@@ -92,7 +92,7 protected:
92 92 qreal m_barWidth;
93 93 bool m_labelsVisible;
94 94 bool m_visible;
95 bool m_grouping;
95 bool m_overlap;
96 96
97 97 private:
98 98 Q_DECLARE_PUBLIC(QAbstractBarSeries)
@@ -50,10 +50,10 QVector<QRectF> BarChartItem::calculateLayout()
50 50 qreal scaleX = (width / rangeX);
51 51 qreal barWidth;
52 52
53 if (m_series->d_func()->m_grouping) {
54 barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
55 } else {
53 if (m_series->d_func()->m_overlap) {
56 54 barWidth = scaleX * m_series->d_func()->barWidth();
55 } else {
56 barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
57 57 }
58 58
59 59 int itemIndex(0);
@@ -64,12 +64,12 QVector<QRectF> BarChartItem::calculateLayout()
64 64
65 65 qreal xPos;
66 66
67 if (m_series->d_func()->m_grouping) {
67 if (m_series->d_func()->m_overlap) {
68 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
69 } else {
68 70 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left();
69 71 xPos -= setCount*barWidth/2;
70 72 xPos += set*barWidth;
71 } else {
72 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
73 73 }
74 74
75 75 qreal barHeight = barSet->value(category) * scaleY;
General Comments 0
You need to be logged in to leave comments. Login now