@@ -0,0 +1,70 | |||||
|
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 <QCategoryAxis> | |||
|
25 | ||||
|
26 | class CategoryLineChart: public Chart | |||
|
27 | { | |||
|
28 | public: | |||
|
29 | QString name() { return QObject::tr("CategoryLineChart"); } | |||
|
30 | QString category() { return QObject::tr("XYSeries"); } | |||
|
31 | QString subCategory() { return QString::null; } | |||
|
32 | ||||
|
33 | QChart* createChart(const DataTable& table) { | |||
|
34 | ||||
|
35 | QChart* chart = new QChart(); | |||
|
36 | chart->setTitle("Category Line chart"); | |||
|
37 | ||||
|
38 | QString name("Series "); | |||
|
39 | int nameIndex = 0; | |||
|
40 | foreach (DataList list, table) { | |||
|
41 | QLineSeries *series = new QLineSeries(chart); | |||
|
42 | foreach (Data data, list) | |||
|
43 | series->append(data.first); | |||
|
44 | series->setName(name + QString::number(nameIndex)); | |||
|
45 | nameIndex++; | |||
|
46 | chart->addSeries(series); | |||
|
47 | } | |||
|
48 | ||||
|
49 | // chart->createDefaultAxes(); | |||
|
50 | QCategoryAxis *axisX = new QCategoryAxis; | |||
|
51 | axisX->append("low", 5); | |||
|
52 | axisX->append("optimal", 12); | |||
|
53 | axisX->append("high", 19); | |||
|
54 | axisX->setRange(-1, 20); | |||
|
55 | chart->setAxisX(axisX, chart->series().at(0)); | |||
|
56 | ||||
|
57 | QCategoryAxis *axisY = new QCategoryAxis; | |||
|
58 | axisY->append("low", 5); | |||
|
59 | axisY->append("optimal", 8); | |||
|
60 | axisY->append("high", 12); | |||
|
61 | axisY->setRange(-5, 20); | |||
|
62 | chart->setAxisY(axisY, chart->series().at(0)); | |||
|
63 | ||||
|
64 | return chart; | |||
|
65 | } | |||
|
66 | ||||
|
67 | }; | |||
|
68 | ||||
|
69 | DECLARE_CHART(CategoryLineChart) | |||
|
70 |
@@ -1,16 +1,17 | |||||
1 | INCLUDEPATH += $$PWD |
|
1 | INCLUDEPATH += $$PWD | |
2 | DEPENDPATH += $$PWD |
|
2 | DEPENDPATH += $$PWD | |
3 | SOURCES += \ |
|
3 | SOURCES += \ | |
4 | font/font.cpp \ |
|
4 | font/font.cpp \ | |
5 | xyseries/linechart.cpp \ |
|
5 | xyseries/linechart.cpp \ | |
6 | xyseries/scatterchart.cpp \ |
|
6 | xyseries/scatterchart.cpp \ | |
7 | xyseries/splinechart.cpp \ |
|
7 | xyseries/splinechart.cpp \ | |
8 | xyseries/areachart.cpp \ |
|
8 | xyseries/areachart.cpp \ | |
|
9 | xyseries/categorylinechart.cpp \ | |||
9 | barseries/verticalstackedbarchart.cpp \ |
|
10 | barseries/verticalstackedbarchart.cpp \ | |
10 | barseries/horizontalstackedbarchart.cpp \ |
|
11 | barseries/horizontalstackedbarchart.cpp \ | |
11 | barseries/verticalbarchart.cpp \ |
|
12 | barseries/verticalbarchart.cpp \ | |
12 | barseries/horizontalbarchart.cpp \ |
|
13 | barseries/horizontalbarchart.cpp \ | |
13 | barseries/horizontalpercentbarchart.cpp \ |
|
14 | barseries/horizontalpercentbarchart.cpp \ | |
14 | barseries/verticalpercentbarchart.cpp \ |
|
15 | barseries/verticalpercentbarchart.cpp \ | |
15 | pieseries/piechart.cpp \ |
|
16 | pieseries/piechart.cpp \ | |
16 | pieseries/donutchart.cpp |
|
17 | pieseries/donutchart.cpp |
@@ -1,134 +1,149 | |||||
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 "chartcategoryaxisx_p.h" |
|
21 | #include "chartcategoryaxisx_p.h" | |
22 | #include "qcategoryaxis.h" |
|
22 | #include "qcategoryaxis.h" | |
23 | #include "qabstractaxis.h" |
|
23 | #include "qabstractaxis.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_p.h" | |
25 | #include <QGraphicsLayout> |
|
25 | #include <QGraphicsLayout> | |
26 | #include <QFontMetrics> |
|
26 | #include <QFontMetrics> | |
27 | #include <qmath.h> |
|
27 | #include <qmath.h> | |
28 |
|
28 | |||
29 | static int label_padding = 5; |
|
29 | static int label_padding = 5; | |
30 |
|
30 | |||
31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
32 |
|
32 | |||
33 | ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) |
|
33 | ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) | |
34 | { |
|
34 | { | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | ChartCategoryAxisX::~ChartCategoryAxisX() |
|
37 | ChartCategoryAxisX::~ChartCategoryAxisX() | |
38 | { |
|
38 | { | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | QVector<qreal> ChartCategoryAxisX::calculateLayout() const |
|
41 | QVector<qreal> ChartCategoryAxisX::calculateLayout() const | |
42 | { |
|
42 | { | |
43 | QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
43 | QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis); | |
44 | int tickCount = axis->categoriesLabels().count() + 1; |
|
44 | int tickCount = axis->categoriesLabels().count() + 1; | |
45 | QVector<qreal> points; |
|
45 | QVector<qreal> points; | |
46 |
|
46 | |||
47 | if (tickCount < 2) |
|
47 | if (tickCount < 2) | |
48 | return points; |
|
48 | return points; | |
49 |
|
49 | |||
50 | qreal range = axis->max() - axis->min(); |
|
50 | qreal range = axis->max() - axis->min(); | |
51 | if (range > 0) { |
|
51 | if (range > 0) { | |
52 | points.resize(tickCount); |
|
52 | points.resize(tickCount); | |
53 | qreal scale = m_rect.width() / range; |
|
53 | qreal scale = m_rect.width() / range; | |
54 | for (int i = 0; i < tickCount; ++i) |
|
54 | for (int i = 0; i < tickCount; ++i) | |
55 | if (i < tickCount - 1) { |
|
55 | if (i < tickCount - 1) { | |
56 | int x = (axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.left(); |
|
56 | int x = (axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.left(); | |
57 | points[i] = x; |
|
57 | points[i] = x; | |
58 | } else { |
|
58 | } else { | |
59 | int x = (axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.left(); |
|
59 | int x = (axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.left(); | |
60 | points[i] = x; |
|
60 | points[i] = x; | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 | return points; |
|
63 | return points; | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | void ChartCategoryAxisX::updateGeometry() |
|
66 | void ChartCategoryAxisX::updateGeometry() | |
67 | { |
|
67 | { | |
68 | const QVector<qreal>& layout = ChartAxis::layout(); |
|
68 | const QVector<qreal>& layout = ChartAxis::layout(); | |
69 |
|
69 | |||
70 | m_minWidth = 0; |
|
70 | m_minWidth = 0; | |
71 | m_minHeight = 0; |
|
71 | m_minHeight = 0; | |
72 |
|
72 | |||
73 | if(layout.isEmpty()) return; |
|
73 | if(layout.isEmpty()) return; | |
74 |
|
74 | |||
75 | QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
75 | QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis); | |
76 | QStringList ticksList = categoryAxis->categoriesLabels(); |
|
76 | QStringList ticksList = categoryAxis->categoriesLabels(); | |
77 |
|
77 | |||
78 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
78 | QList<QGraphicsItem *> lines = m_grid->childItems(); | |
79 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
79 | QList<QGraphicsItem *> labels = m_labels->childItems(); | |
80 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
80 | QList<QGraphicsItem *> shades = m_shades->childItems(); | |
81 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
81 | QList<QGraphicsItem *> axis = m_arrow->childItems(); | |
82 |
|
82 | |||
83 | // Q_ASSERT(labels.size() == ticksList.size()); |
|
83 | ||
84 | // Q_ASSERT(layout.size() == ticksList.size()); |
|
84 | for (int i = 0; i < labels.count(); i++) { | |
|
85 | labels.at(i)->setVisible(false); | |||
|
86 | } | |||
85 |
|
87 | |||
86 | // axis base line |
|
88 | // axis base line | |
87 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
89 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
88 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
90 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | |
89 |
|
91 | |||
90 | for (int i = 0; i < layout.size(); ++i) { |
|
92 | for (int i = 0; i < layout.size(); ++i) { | |
91 |
|
93 | |||
92 | // label items |
|
94 | // label items | |
93 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
95 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
94 | if (i < ticksList.count()) { |
|
96 | if (i < ticksList.count()) { | |
95 | labelItem->setText(ticksList.at(i)); |
|
97 | labelItem->setText(ticksList.at(i)); | |
96 | } |
|
98 | } | |
97 | const QRectF& rect = labelItem->boundingRect(); |
|
99 | const QRectF& rect = labelItem->boundingRect(); | |
98 | QPointF center = rect.center(); |
|
100 | QPointF center = rect.center(); | |
99 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
101 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
100 |
if (i < |
|
102 | if (i < layout.size() - 1) { | |
101 | labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), m_rect.bottom() + label_padding); |
|
103 | labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), m_rect.bottom() + label_padding); | |
102 | else |
|
104 | } else { | |
103 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); |
|
105 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); | |
|
106 | } | |||
|
107 | ||||
|
108 | // check if the label should be shown | |||
|
109 | if (labelItem->pos().x() + center.x() < m_rect.left() || labelItem->pos().x() + center.x() > m_rect.right()) | |||
|
110 | labelItem->setVisible(false); | |||
|
111 | else | |||
|
112 | labelItem->setVisible(true); | |||
104 |
|
113 | |||
105 | m_minWidth += rect.width(); |
|
114 | m_minWidth += rect.width(); | |
106 | m_minHeight = qMax(rect.height()+ label_padding, m_minHeight); |
|
115 | m_minHeight = qMax(rect.height()+ label_padding, m_minHeight); | |
107 |
|
116 | |||
108 | if ((i + 1) % 2 && i > 1) { |
|
117 | if ((i + 1) % 2 && i > 1) { | |
109 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1)); |
|
118 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1)); | |
110 | rectItem->setRect(layout[i - 1],m_rect.top(),layout[i]-layout[i - 1],m_rect.height()); |
|
119 | rectItem->setRect(layout[i - 1],m_rect.top(),layout[i]-layout[i - 1],m_rect.height()); | |
111 | } |
|
120 | } | |
112 |
|
121 | |||
113 | // grid lines and axis line ticks |
|
122 | // grid lines and axis line ticks | |
114 | if (layout[i] < m_rect.left() || layout[i] > m_rect.right()) |
|
|||
115 | continue; |
|
|||
116 |
|
||||
117 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
123 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
118 | lineItem->setPos(layout[i], m_rect.top()); |
|
124 | lineItem->setPos(layout[i], m_rect.top()); | |
119 | lineItem->setLine(0, 0, 0, m_rect.height()); |
|
125 | lineItem->setLine(0, 0, 0, m_rect.height()); | |
120 |
|
126 | |||
121 |
|
|
127 | QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
122 |
|
|
128 | tickLineItem->setPos(layout[i], m_rect.bottom()); | |
123 |
|
|
129 | tickLineItem->setLine(0, 0, 0, 5); | |
|
130 | ||||
|
131 | // check if the grid line and the axis tick should be shown | |||
|
132 | if (lineItem->pos().x() < m_rect.left() || lineItem->pos().x() > m_rect.right()) { | |||
|
133 | lineItem->setVisible(false); | |||
|
134 | tickLineItem->setVisible(false); | |||
|
135 | } else { | |||
|
136 | lineItem->setVisible(true); | |||
|
137 | tickLineItem->setVisible(true); | |||
|
138 | } | |||
124 | } |
|
139 | } | |
125 |
|
140 | |||
126 | } |
|
141 | } | |
127 |
|
142 | |||
128 | void ChartCategoryAxisX::handleAxisUpdated() |
|
143 | void ChartCategoryAxisX::handleAxisUpdated() | |
129 | { |
|
144 | { | |
130 | updateGeometry(); |
|
145 | updateGeometry(); | |
131 | ChartAxis::handleAxisUpdated(); |
|
146 | ChartAxis::handleAxisUpdated(); | |
132 | } |
|
147 | } | |
133 |
|
148 | |||
134 | QTCOMMERCIALCHART_END_NAMESPACE |
|
149 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,149 +1,161 | |||||
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 "chartcategoryaxisy_p.h" |
|
21 | #include "chartcategoryaxisy_p.h" | |
22 | #include "qcategoryaxis.h" |
|
22 | #include "qcategoryaxis.h" | |
23 | #include "qabstractaxis.h" |
|
23 | #include "qabstractaxis.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_p.h" | |
25 | #include <QGraphicsLayout> |
|
25 | #include <QGraphicsLayout> | |
26 | #include <QFontMetrics> |
|
26 | #include <QFontMetrics> | |
27 | #include <qmath.h> |
|
27 | #include <qmath.h> | |
28 |
|
28 | |||
29 | static int label_padding = 5; |
|
29 | static int label_padding = 5; | |
30 |
|
30 | |||
31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
32 |
|
32 | |||
33 | ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) |
|
33 | ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) | |
34 | { |
|
34 | { | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | ChartCategoryAxisY::~ChartCategoryAxisY() |
|
37 | ChartCategoryAxisY::~ChartCategoryAxisY() | |
38 | { |
|
38 | { | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | QVector<qreal> ChartCategoryAxisY::calculateLayout() const |
|
41 | QVector<qreal> ChartCategoryAxisY::calculateLayout() const | |
42 | { |
|
42 | { | |
43 | QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
43 | QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis); | |
44 | int tickCount = axis->categoriesLabels().count() + 1; |
|
44 | int tickCount = axis->categoriesLabels().count() + 1; | |
45 | QVector<qreal> points; |
|
45 | QVector<qreal> points; | |
46 |
|
46 | |||
47 | if (tickCount < 2) |
|
47 | if (tickCount < 2) | |
48 | return points; |
|
48 | return points; | |
49 |
|
49 | |||
50 | qreal range = axis->max() - axis->min(); |
|
50 | qreal range = axis->max() - axis->min(); | |
51 | if (range > 0) { |
|
51 | if (range > 0) { | |
52 | points.resize(tickCount); |
|
52 | points.resize(tickCount); | |
53 | qreal scale = m_rect.height() / range; |
|
53 | qreal scale = m_rect.height() / range; | |
54 | for (int i = 0; i < tickCount; ++i) |
|
54 | for (int i = 0; i < tickCount; ++i) | |
55 | if (i < tickCount - 1) { |
|
55 | if (i < tickCount - 1) { | |
56 | int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.bottom(); |
|
56 | int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.bottom(); | |
57 | points[i] = y; |
|
57 | points[i] = y; | |
58 | } else { |
|
58 | } else { | |
59 | int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.bottom(); |
|
59 | int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.bottom(); | |
60 | points[i] = y; |
|
60 | points[i] = y; | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | return points; |
|
64 | return points; | |
65 | } |
|
65 | } | |
66 |
|
66 | |||
67 | void ChartCategoryAxisY::updateGeometry() |
|
67 | void ChartCategoryAxisY::updateGeometry() | |
68 | { |
|
68 | { | |
69 | const QVector<qreal> &layout = ChartAxis::layout(); |
|
69 | const QVector<qreal> &layout = ChartAxis::layout(); | |
70 | m_minWidth = 0; |
|
70 | m_minWidth = 0; | |
71 | m_minHeight = 0; |
|
71 | m_minHeight = 0; | |
72 |
|
72 | |||
73 | if(layout.isEmpty()) { |
|
73 | if(layout.isEmpty()) { | |
74 | return; |
|
74 | return; | |
75 | } |
|
75 | } | |
76 |
|
76 | |||
77 | QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
77 | QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis); | |
78 | QStringList ticksList = categoryAxis->categoriesLabels(); |
|
78 | QStringList ticksList = categoryAxis->categoriesLabels(); | |
79 |
|
79 | |||
80 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
80 | QList<QGraphicsItem *> lines = m_grid->childItems(); | |
81 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
81 | QList<QGraphicsItem *> labels = m_labels->childItems(); | |
82 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
82 | QList<QGraphicsItem *> shades = m_shades->childItems(); | |
83 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
83 | QList<QGraphicsItem *> axis = m_arrow->childItems(); | |
84 |
|
84 | |||
85 | // Q_ASSERT(labels.size() == ticksList.size()); |
|
85 | // qreal height = 2*m_rect.bottom(); | |
86 | // Q_ASSERT(layout.size() == ticksList.size()); |
|
|||
87 |
|
86 | |||
88 | qreal height = 2*m_rect.bottom(); |
|
87 | for (int i = 0; i < labels.count(); i++) { | |
|
88 | labels.at(i)->setVisible(false); | |||
|
89 | } | |||
89 |
|
90 | |||
90 | // axis base line |
|
91 | // axis base line | |
91 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
92 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
92 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
93 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | |
93 |
|
94 | |||
94 | for (int i = 0; i < layout.size(); ++i) { |
|
95 | for (int i = 0; i < layout.size(); ++i) { | |
95 |
|
96 | |||
96 | // label items |
|
97 | // label items | |
97 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
98 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
98 |
|
||||
99 | if (i < ticksList.count()) { |
|
99 | if (i < ticksList.count()) { | |
100 | labelItem->setText(ticksList.at(i)); |
|
100 | labelItem->setText(ticksList.at(i)); | |
101 | } |
|
101 | } | |
102 | const QRectF& rect = labelItem->boundingRect(); |
|
102 | const QRectF& rect = labelItem->boundingRect(); | |
103 |
|
103 | |||
104 | QPointF center = rect.center(); |
|
104 | QPointF center = rect.center(); | |
105 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
105 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
106 |
|
106 | |||
107 |
if (i < |
|
107 | if (i < layout.size() - 1) | |
108 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y()); |
|
108 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y()); | |
109 | else |
|
109 | else | |
110 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y()); |
|
110 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y()); | |
111 |
|
111 | |||
112 | if(labelItem->pos().y()+rect.height()>height) { |
|
112 | // check if the label should be shown | |
|
113 | if (labelItem->pos().y() + center.y() < m_rect.top() || labelItem->pos().y() + center.y() > m_rect.bottom()) | |||
113 | labelItem->setVisible(false); |
|
114 | labelItem->setVisible(false); | |
114 |
|
|
115 | else | |
115 | else { |
|
|||
116 | labelItem->setVisible(true); |
|
116 | labelItem->setVisible(true); | |
117 | height=labelItem->pos().y(); |
|
117 | ||
118 | } |
|
118 | // if(labelItem->pos().y()+rect.height()>height) { | |
|
119 | // labelItem->setVisible(false); | |||
|
120 | // } | |||
|
121 | // else { | |||
|
122 | // labelItem->setVisible(true); | |||
|
123 | // height=labelItem->pos().y(); | |||
|
124 | // } | |||
119 |
|
125 | |||
120 | m_minWidth=qMax(rect.width()+label_padding,m_minWidth); |
|
126 | m_minWidth=qMax(rect.width()+label_padding,m_minWidth); | |
121 | m_minHeight+=rect.height(); |
|
127 | m_minHeight+=rect.height(); | |
122 |
|
128 | |||
123 | if ((i+1)%2 && i>1) { |
|
129 | if ((i+1)%2 && i>1) { | |
124 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
130 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | |
125 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); |
|
131 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); | |
126 | } |
|
132 | } | |
127 |
|
133 | |||
128 | // grid lines and axis line ticks |
|
134 | // grid lines and axis line ticks | |
129 | if (layout[i] < m_rect.left() || layout[i] > m_rect.right()) |
|
|||
130 | continue; |
|
|||
131 |
|
||||
132 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
135 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
133 | lineItem->setPos(m_rect.left(), layout[i]); |
|
136 | lineItem->setPos(m_rect.left(), layout[i]); | |
134 | lineItem->setLine(0, 0, m_rect.width(), 0); |
|
137 | lineItem->setLine(0, 0, m_rect.width(), 0); | |
135 |
|
138 | |||
136 |
|
|
139 | QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
137 |
|
|
140 | tickLineItem->setPos(m_rect.left(), layout[i]); | |
138 |
|
|
141 | tickLineItem->setLine(-5, 0, 0, 0); | |
|
142 | ||||
|
143 | // check if the grid line and the axis tick should be shown | |||
|
144 | if (lineItem->pos().y() < m_rect.top() || lineItem->pos().y() > m_rect.bottom()) { | |||
|
145 | lineItem->setVisible(false); | |||
|
146 | tickLineItem->setVisible(false); | |||
|
147 | } else { | |||
|
148 | lineItem->setVisible(true); | |||
|
149 | tickLineItem->setVisible(true); | |||
|
150 | } | |||
139 | } |
|
151 | } | |
140 |
|
152 | |||
141 | } |
|
153 | } | |
142 |
|
154 | |||
143 | void ChartCategoryAxisY::handleAxisUpdated() |
|
155 | void ChartCategoryAxisY::handleAxisUpdated() | |
144 | { |
|
156 | { | |
145 | updateGeometry(); |
|
157 | updateGeometry(); | |
146 | ChartAxis::handleAxisUpdated(); |
|
158 | ChartAxis::handleAxisUpdated(); | |
147 | } |
|
159 | } | |
148 |
|
160 | |||
149 | QTCOMMERCIALCHART_END_NAMESPACE |
|
161 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now