##// END OF EJS Templates
ChartViewer: added logaxes charts
Marek Rosa -
r2279:344c3d3c2742
parent child
Show More
@@ -0,0 +1,81
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 "charts.h"
22 #include "qchart.h"
23 #include "qbarseries.h"
24 #include "qbarset.h"
25 #include "qlogvalueaxis.h"
26 #include "qbarcategoryaxis.h"
27
28 class BarCategoryAxisXLogY: public Chart
29 {
30 public:
31 QString name() { return "AxisX"; }
32 QString category() { return QObject::tr("Axis"); }
33 QString subCategory() { return "BarCategoryAxis"; }
34
35 QChart *createChart(const DataTable &table)
36 {
37 QChart *chart = new QChart();
38 chart->setTitle(" BarCateogry X , LogValue Y");
39
40 QString name("Series ");
41 QBarSeries *series = new QBarSeries(chart);
42 QLogValueAxis *logvalueaxis = new QLogValueAxis();
43 logvalueaxis->setBase(2);
44 QBarCategoryAxis *barcategory = new QBarCategoryAxis();
45 for (int i(0); i < table.count(); i++) {
46 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
47 foreach (Data data, table[i])
48 *set << data.first.y();
49 series->append(set);
50 }
51 chart->addSeries(series);
52
53 int count = series->barSets().first()->count();
54
55
56 for (int i = 0; i < count; i++) {
57 barcategory->append("BarSet " + QString::number(i));
58 }
59
60 chart->setAxisY(logvalueaxis, series);
61 chart->setAxisX(barcategory, series);
62
63 return chart;
64 }
65 };
66
67 class BarCategoryAxisXLogYTitle: public BarCategoryAxisXLogY
68 {
69 QString name() { return "AxisX Title"; }
70 QChart *createChart(const DataTable &table)
71 {
72 QChart *chart = BarCategoryAxisXLogY::createChart(table);
73 chart->axisX()->setTitleText("Axis X");
74 chart->axisY()->setTitleText("Axis Y");
75 chart->setTitle(" BarCateogry X , LogValue Y, title");
76 return chart;
77 }
78 };
79
80 DECLARE_CHART(BarCategoryAxisXLogY);
81 DECLARE_CHART(BarCategoryAxisXLogYTitle);
@@ -0,0 +1,81
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 "charts.h"
22 #include "qchart.h"
23 #include "qhorizontalbarseries.h"
24 #include "qbarset.h"
25 #include "qlogvalueaxis.h"
26 #include "qbarcategoryaxis.h"
27
28 class BarCategoryAxisYLogX: public Chart
29 {
30 public:
31 QString name() { return "AxisY"; }
32 QString category() { return QObject::tr("Axis"); }
33 QString subCategory() { return "BarCategoryAxis"; }
34
35 QChart *createChart(const DataTable &table)
36 {
37 QChart *chart = new QChart();
38 chart->setTitle(" LogValue X , BarCateogry Y");
39
40 QString name("Series ");
41 QHorizontalBarSeries *series = new QHorizontalBarSeries(chart);
42 QLogValueAxis *logvalueaxis = new QLogValueAxis();
43 logvalueaxis->setBase(2);
44 QBarCategoryAxis *barcategory = new QBarCategoryAxis();
45 for (int i(0); i < table.count(); i++) {
46 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
47 foreach (Data data, table[i])
48 *set << data.first.y();
49 series->append(set);
50 }
51 chart->addSeries(series);
52
53 int count = series->barSets().first()->count();
54
55
56 for (int i = 0; i < count; i++) {
57 barcategory->append("BarSet " + QString::number(i));
58 }
59
60 chart->setAxisX(logvalueaxis, series);
61 chart->setAxisY(barcategory, series);
62
63 return chart;
64 }
65 };
66
67 class BarCategoryAxisYLogXTitle: public BarCategoryAxisYLogX
68 {
69 QString name() { return "AxisX Title"; }
70 QChart *createChart(const DataTable &table)
71 {
72 QChart *chart = BarCategoryAxisYLogX::createChart(table);
73 chart->axisX()->setTitleText("Axis X");
74 chart->axisY()->setTitleText("Axis Y");
75 chart->setTitle(" LogValue X , BarCateogry Y, title");
76 return chart;
77 }
78 };
79
80 DECLARE_CHART(BarCategoryAxisYLogX);
81 DECLARE_CHART(BarCategoryAxisYLogXTitle);
@@ -0,0 +1,76
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 "charts.h"
22 #include "qchart.h"
23 #include "qlineseries.h"
24 #include "qlogvalueaxis.h"
25 #include "qcategoryaxis.h"
26
27 class LogValueAxisX: public Chart
28 {
29 public:
30 QString name() { return "LogValueAxisX"; }
31 QString category() { return QObject::tr("Axis"); }
32 QString subCategory() { return QObject::tr("Log"); }
33
34 QChart *createChart(const DataTable &table)
35 {
36 QChart *chart = new QChart();
37 chart->setTitle("LogValue X , Value Y");
38
39 QString name("Series ");
40 int nameIndex = 0;
41 foreach (DataList list, table) {
42 QLineSeries *series = new QLineSeries(chart);
43 foreach (Data data, list)
44 series->append(data.first);
45 series->setName(name + QString::number(nameIndex));
46 nameIndex++;
47 chart->addSeries(series);
48 }
49
50 chart->createDefaultAxes();
51 QLogValueAxis *axis = new QLogValueAxis();
52 axis->setBase(1.2);
53 foreach (QAbstractSeries *series, chart->series())
54 chart->setAxisX(axis, series);
55
56 return chart;
57 }
58 };
59
60 class LogValueAxisTitleX: public LogValueAxisX
61 {
62 public:
63 QString name() { return "LogValueAxisXTitle"; }
64
65 QChart *createChart(const DataTable &table)
66 {
67 QChart *chart = LogValueAxisX::createChart(table);
68 chart->axisX()->setTitleText("Axis X");
69 chart->axisY()->setTitleText("Axis Y");
70 chart->setTitle("Log Value X , Value Y, title");
71 return chart;
72 }
73 };
74
75 DECLARE_CHART(LogValueAxisX);
76 DECLARE_CHART(LogValueAxisTitleX);
@@ -0,0 +1,76
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 "charts.h"
22 #include "qchart.h"
23 #include "qlineseries.h"
24 #include "qlogvalueaxis.h"
25 #include "qcategoryaxis.h"
26
27 class LogValueAxisY: public Chart
28 {
29 public:
30 QString name() { return "LogValueAxisY"; }
31 QString category() { return QObject::tr("Axis"); }
32 QString subCategory() { return QObject::tr("Log"); }
33
34 QChart *createChart(const DataTable &table)
35 {
36 QChart *chart = new QChart();
37 chart->setTitle("Value X , LogValue Y");
38
39 QString name("Series ");
40 int nameIndex = 0;
41 foreach (DataList list, table) {
42 QLineSeries *series = new QLineSeries(chart);
43 foreach (Data data, list)
44 series->append(data.first);
45 series->setName(name + QString::number(nameIndex));
46 nameIndex++;
47 chart->addSeries(series);
48 }
49
50 chart->createDefaultAxes();
51 QLogValueAxis *axis = new QLogValueAxis();
52 axis->setBase(2);
53 foreach (QAbstractSeries *series, chart->series())
54 chart->setAxisY(axis, series);
55
56 return chart;
57 }
58 };
59
60 class LogValueAxisTitleY: public LogValueAxisY
61 {
62 public:
63 QString name() { return "LogValueAxisYTitle"; }
64
65 QChart *createChart(const DataTable &table)
66 {
67 QChart *chart = LogValueAxisY::createChart(table);
68 chart->axisX()->setTitleText("Axis X");
69 chart->axisY()->setTitleText("Axis Y");
70 chart->setTitle("Value X , Log Value Y, title");
71 return chart;
72 }
73 };
74
75 DECLARE_CHART(LogValueAxisY);
76 DECLARE_CHART(LogValueAxisTitleY);
@@ -1,32 +1,36
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3 SOURCES += \
3 SOURCES += \
4 $$PWD/font/font.cpp \
4 $$PWD/font/font.cpp \
5 $$PWD/xyseries/linechart.cpp \
5 $$PWD/xyseries/linechart.cpp \
6 $$PWD/xyseries/scatterchart.cpp \
6 $$PWD/xyseries/scatterchart.cpp \
7 $$PWD/xyseries/splinechart.cpp \
7 $$PWD/xyseries/splinechart.cpp \
8 $$PWD/xyseries/areachart.cpp \
8 $$PWD/xyseries/areachart.cpp \
9 $$PWD/barseries/verticalstackedbarchart.cpp \
9 $$PWD/barseries/verticalstackedbarchart.cpp \
10 $$PWD/barseries/horizontalstackedbarchart.cpp \
10 $$PWD/barseries/horizontalstackedbarchart.cpp \
11 $$PWD/barseries/verticalbarchart.cpp \
11 $$PWD/barseries/verticalbarchart.cpp \
12 $$PWD/barseries/horizontalbarchart.cpp \
12 $$PWD/barseries/horizontalbarchart.cpp \
13 $$PWD/barseries/horizontalpercentbarchart.cpp \
13 $$PWD/barseries/horizontalpercentbarchart.cpp \
14 $$PWD/barseries/verticalpercentbarchart.cpp \
14 $$PWD/barseries/verticalpercentbarchart.cpp \
15 $$PWD/pieseries/piechart.cpp \
15 $$PWD/pieseries/piechart.cpp \
16 $$PWD/pieseries/donutchart.cpp \
16 $$PWD/pieseries/donutchart.cpp \
17 $$PWD/axis/valueaxis.cpp \
17 $$PWD/axis/valueaxis.cpp \
18 $$PWD/axis/logvalueaxisx.cpp \
19 $$PWD/axis/logvalueaxisy.cpp \
18 $$PWD/axis/categoryaxis.cpp \
20 $$PWD/axis/categoryaxis.cpp \
19 $$PWD/axis/barcategoryaxisx.cpp \
21 $$PWD/axis/barcategoryaxisx.cpp \
20 $$PWD/axis/barcategoryaxisy.cpp \
22 $$PWD/axis/barcategoryaxisy.cpp \
23 $$PWD/axis/barcategoryaxisxlogy.cpp \
24 $$PWD/axis/barcategoryaxisylogx.cpp \
21 $$PWD/multiaxis/multivalueaxis.cpp \
25 $$PWD/multiaxis/multivalueaxis.cpp \
22 $$PWD/multiaxis/multivalueaxis2.cpp \
26 $$PWD/multiaxis/multivalueaxis2.cpp \
23 $$PWD/multiaxis/multivalueaxis3.cpp \
27 $$PWD/multiaxis/multivalueaxis3.cpp \
24 $$PWD/multiaxis/multivalueaxis4.cpp \
28 $$PWD/multiaxis/multivalueaxis4.cpp \
25 $$PWD/multiaxis/multivaluebaraxis.cpp \
29 $$PWD/multiaxis/multivaluebaraxis.cpp \
26 $$PWD/size/sizecharts.cpp
30 $$PWD/size/sizecharts.cpp
27
31
28 !linux-arm*: {
32 !linux-arm*: {
29 SOURCES += \
33 SOURCES += \
30 $$PWD/axis/datetimeaxisx.cpp \
34 $$PWD/axis/datetimeaxisx.cpp \
31 $$PWD/axis/datetimeaxisy.cpp
35 $$PWD/axis/datetimeaxisy.cpp
32 }
36 }
@@ -1,94 +1,94
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qabstractbarseries_p.h"
23 #include "qabstractbarseries_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qbarset_p.h"
25 #include "qbarset_p.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
29 BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
30 AbstractBarChartItem(series, item)
30 AbstractBarChartItem(series, item)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> BarChartItem::calculateLayout()
34 QVector<QRectF> BarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->count();
40 qreal setCount = m_series->count();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal rectWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
50 qreal rectWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
54 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
55 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
57
57
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left();
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left();
59 xPos -= setCount * rectWidth / 2;
59 xPos -= setCount * rectWidth / 2;
60 xPos += set * rectWidth;
60 xPos += set * rectWidth;
61
61
62 qreal rectHeight = barSet->value(category) * scaleY;
62 qreal rectHeight = barSet->value(category) * scaleY;
63 Bar *bar = m_bars.at(itemIndex);
63 Bar *bar = m_bars.at(itemIndex);
64
64
65 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
65 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
66 layout.append(rect);
66 layout.append(rect);
67 bar->setPen(barSet->m_pen);
67 bar->setPen(barSet->m_pen);
68 bar->setBrush(barSet->m_brush);
68 bar->setBrush(barSet->m_brush);
69 if (qFuzzyCompare(rectHeight, 0))
69 if (qFuzzyIsNull(rectHeight))
70 bar->setVisible(false);
70 bar->setVisible(false);
71 else
71 else
72 bar->setVisible(barsVisible);
72 bar->setVisible(barsVisible);
73
73
74 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
74 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
75
75
76 if (!qFuzzyCompare(barSet->value(category), 0))
76 if (!qFuzzyIsNull(barSet->value(category)))
77 label->setText(QString::number(barSet->value(category)));
77 label->setText(QString::number(barSet->value(category)));
78 else
78 else
79 label->setText(QString(""));
79 label->setText(QString(""));
80
80
81 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
81 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
82 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
82 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
83 label->setFont(barSet->m_labelFont);
83 label->setFont(barSet->m_labelFont);
84 label->setBrush(barSet->m_labelBrush);
84 label->setBrush(barSet->m_labelBrush);
85
85
86 itemIndex++;
86 itemIndex++;
87 }
87 }
88 }
88 }
89 return layout;
89 return layout;
90 }
90 }
91
91
92 #include "moc_barchartitem_p.cpp"
92 #include "moc_barchartitem_p.cpp"
93
93
94 QTCOMMERCIALCHART_END_NAMESPACE
94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,100 +1,100
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "percentbarchartitem_p.h"
21 #include "percentbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qabstractbarseries_p.h"
23 #include "qabstractbarseries_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qbarset_p.h"
25 #include "qbarset_p.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
29 PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
30 AbstractBarChartItem(series, item)
30 AbstractBarChartItem(series, item)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->count();
40 qreal setCount = m_series->count();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal rectWidth = scaleX * m_series->d_func()->barWidth();
50 qreal rectWidth = scaleX * m_series->d_func()->barWidth();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal percentage = (100 / colSum);
55 qreal percentage = (100 / colSum);
56 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
56 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
57 for (int set = 0; set < setCount; set++) {
57 for (int set = 0; set < setCount; set++) {
58 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
58 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
59
59
60 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
60 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
61
61
62 qreal rectHeight = barSet->value(category) * percentage * scaleY;
62 qreal rectHeight = barSet->value(category) * percentage * scaleY;
63 Bar *bar = m_bars.at(itemIndex);
63 Bar *bar = m_bars.at(itemIndex);
64 bar->setPen(barSet->m_pen);
64 bar->setPen(barSet->m_pen);
65 bar->setBrush(barSet->m_brush);
65 bar->setBrush(barSet->m_brush);
66 if (qFuzzyCompare(rectHeight, 0))
66 if (qFuzzyIsNull(rectHeight))
67 bar->setVisible(false);
67 bar->setVisible(false);
68 else
68 else
69 bar->setVisible(barsVisible);
69 bar->setVisible(barsVisible);
70
70
71 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
71 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
72 layout.append(rect);
72 layout.append(rect);
73
73
74 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
74 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
75
75
76 if (!qFuzzyCompare(m_series->d_func()->valueAt(set, category), 0)) {
76 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) {
77 int p = m_series->d_func()->percentageAt(set, category) * 100;
77 int p = m_series->d_func()->percentageAt(set, category) * 100;
78 QString vString(QString::number(p));
78 QString vString(QString::number(p));
79 vString.truncate(3);
79 vString.truncate(3);
80 vString.append("%");
80 vString.append("%");
81 label->setText(vString);
81 label->setText(vString);
82 } else {
82 } else {
83 label->setText(QString(""));
83 label->setText(QString(""));
84 }
84 }
85
85
86 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
86 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
87 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
87 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
88 label->setFont(barSet->m_labelFont);
88 label->setFont(barSet->m_labelFont);
89 label->setBrush(barSet->m_labelBrush);
89 label->setBrush(barSet->m_labelBrush);
90
90
91 itemIndex++;
91 itemIndex++;
92 yPos -= rectHeight;
92 yPos -= rectHeight;
93 }
93 }
94 }
94 }
95 return layout;
95 return layout;
96 }
96 }
97
97
98 #include "moc_percentbarchartitem_p.cpp"
98 #include "moc_percentbarchartitem_p.cpp"
99
99
100 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,102 +1,102
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "stackedbarchartitem_p.h"
21 #include "stackedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qabstractbarseries_p.h"
24 #include "qabstractbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
29 StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
30 AbstractBarChartItem(series, item)
30 AbstractBarChartItem(series, item)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> StackedBarChartItem::calculateLayout()
34 QVector<QRectF> StackedBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37 // Use temporary qreals for accuracy
37 // Use temporary qreals for accuracy
38 qreal categoryCount = m_series->d_func()->categoryCount();
38 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal setCount = m_series->count();
39 qreal setCount = m_series->count();
40 bool barsVisible = m_series->isVisible();
40 bool barsVisible = m_series->isVisible();
41
41
42 // Domain:
42 // Domain:
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal height = geometry().height();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
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 rectWidth = scaleX * m_series->d_func()->barWidth();
49 qreal rectWidth = scaleX * m_series->d_func()->barWidth();
50
50
51 int itemIndex(0);
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
52 for (int category = 0; category < categoryCount; category++) {
53 qreal yMax = height + scaleY * m_domainMinY + geometry().top();
53 qreal yMax = height + scaleY * m_domainMinY + geometry().top();
54 qreal yMin = height + scaleY * m_domainMinY + geometry().top();
54 qreal yMin = height + scaleY * m_domainMinY + geometry().top();
55 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
57
57
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
59
59
60 qreal rectHeight = barSet->value(category) * scaleY;
60 qreal rectHeight = barSet->value(category) * scaleY;
61 Bar *bar = m_bars.at(itemIndex);
61 Bar *bar = m_bars.at(itemIndex);
62 bar->setPen(barSet->m_pen);
62 bar->setPen(barSet->m_pen);
63 bar->setBrush(barSet->m_brush);
63 bar->setBrush(barSet->m_brush);
64 if (qFuzzyCompare(rectHeight, 0))
64 if (qFuzzyIsNull(rectHeight))
65 bar->setVisible(false);
65 bar->setVisible(false);
66 else
66 else
67 bar->setVisible(barsVisible);
67 bar->setVisible(barsVisible);
68
68
69 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
69 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
70
70
71 if (!qFuzzyCompare(barSet->value(category), 0))
71 if (!qFuzzyIsNull(barSet->value(category)))
72 label->setText(QString::number(barSet->value(category)));
72 label->setText(QString::number(barSet->value(category)));
73 else
73 else
74 label->setText(QString(""));
74 label->setText(QString(""));
75
75
76 label->setFont(barSet->m_labelFont);
76 label->setFont(barSet->m_labelFont);
77 label->setBrush(barSet->m_labelBrush);
77 label->setBrush(barSet->m_labelBrush);
78
78
79 if (rectHeight < 0) {
79 if (rectHeight < 0) {
80 QRectF rect(xPos, yMax - rectHeight, rectWidth, rectHeight);
80 QRectF rect(xPos, yMax - rectHeight, rectWidth, rectHeight);
81 layout.append(rect);
81 layout.append(rect);
82 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
82 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
83 yMax - rectHeight / 2 - label->boundingRect().height() / 2);
83 yMax - rectHeight / 2 - label->boundingRect().height() / 2);
84 yMax -= rectHeight;
84 yMax -= rectHeight;
85 } else {
85 } else {
86 QRectF rect(xPos, yMin - rectHeight, rectWidth, rectHeight);
86 QRectF rect(xPos, yMin - rectHeight, rectWidth, rectHeight);
87 layout.append(rect);
87 layout.append(rect);
88 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
88 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
89 yMin - rectHeight / 2 - label->boundingRect().height() / 2);
89 yMin - rectHeight / 2 - label->boundingRect().height() / 2);
90 yMin -= rectHeight;
90 yMin -= rectHeight;
91 }
91 }
92
92
93 itemIndex++;
93 itemIndex++;
94 }
94 }
95 }
95 }
96
96
97 return layout;
97 return layout;
98 }
98 }
99
99
100 #include "moc_stackedbarchartitem_p.cpp"
100 #include "moc_stackedbarchartitem_p.cpp"
101
101
102 QTCOMMERCIALCHART_END_NAMESPACE
102 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now