@@ -1,122 +1,122 | |||||
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 <QApplication> |
|
21 | #include <QApplication> | |
22 | #include <QMainWindow> |
|
22 | #include <QMainWindow> | |
23 | #include <QChartView> |
|
23 | #include <QChartView> | |
24 | #include <QLineSeries> |
|
24 | #include <QLineSeries> | |
25 | #include <QValuesAxis> |
|
25 | #include <QValuesAxis> | |
26 | #include <QBarCategoriesAxis> |
|
26 | #include <QBarCategoriesAxis> | |
27 |
|
27 | |||
28 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | QTCOMMERCIALCHART_USE_NAMESPACE | |
29 |
|
29 | |||
30 | int main(int argc, char *argv[]) |
|
30 | int main(int argc, char *argv[]) | |
31 | { |
|
31 | { | |
32 | QApplication a(argc, argv); |
|
32 | QApplication a(argc, argv); | |
33 |
|
33 | |||
34 | //![1] |
|
34 | //![1] | |
35 | QLineSeries* series = new QLineSeries(); |
|
35 | QLineSeries* series = new QLineSeries(); | |
36 | *series << QPointF(0, 0.6) << QPointF(0.5, 0.4) << QPointF(1, 2) << QPointF(1.5, 1.2) << QPointF(2, 1.0); |
|
36 | *series << QPointF(0, 0.6) << QPointF(0.5, 0.4) << QPointF(1, 2) << QPointF(1.5, 1.2) << QPointF(2, 1.0); | |
37 | QChart* chart = new QChart(); |
|
37 | QChart* chart = new QChart(); | |
38 | chart->legend()->hide(); |
|
38 | chart->legend()->hide(); | |
39 | chart->addSeries(series); |
|
39 | chart->addSeries(series); | |
40 | //![1] |
|
40 | //![1] | |
41 |
|
41 | |||
42 | //![2] |
|
42 | //![2] | |
43 | // Customize series |
|
43 | // Customize series | |
44 | QPen pen(QRgb(0xfdb157)); |
|
44 | QPen pen(QRgb(0xfdb157)); | |
45 | pen.setWidth(5); |
|
45 | pen.setWidth(5); | |
46 | series->setPen(pen); |
|
46 | series->setPen(pen); | |
47 |
|
47 | |||
48 | // Customize chart title |
|
48 | // Customize chart title | |
49 | QFont font; |
|
49 | QFont font; | |
50 | font.setPixelSize(18); |
|
50 | font.setPixelSize(18); | |
51 | chart->setTitleFont(font); |
|
51 | chart->setTitleFont(font); | |
52 | chart->setTitleBrush(QBrush(Qt::white)); |
|
52 | chart->setTitleBrush(QBrush(Qt::white)); | |
53 | chart->setTitle("Customchart example"); |
|
53 | chart->setTitle("Customchart example"); | |
54 |
|
54 | |||
55 | // Customize chart background |
|
55 | // Customize chart background | |
56 | QLinearGradient backgroundGradient; |
|
56 | QLinearGradient backgroundGradient; | |
57 | backgroundGradient.setStart(QPointF(0,0)); |
|
57 | backgroundGradient.setStart(QPointF(0,0)); | |
58 | backgroundGradient.setFinalStop(QPointF(0,1)); |
|
58 | backgroundGradient.setFinalStop(QPointF(0,1)); | |
59 | backgroundGradient.setColorAt(0.0, QRgb(0xd2d0d1)); |
|
59 | backgroundGradient.setColorAt(0.0, QRgb(0xd2d0d1)); | |
60 | backgroundGradient.setColorAt(1.0, QRgb(0x4c4547)); |
|
60 | backgroundGradient.setColorAt(1.0, QRgb(0x4c4547)); | |
61 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
61 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
62 | chart->setBackgroundBrush(backgroundGradient); |
|
62 | chart->setBackgroundBrush(backgroundGradient); | |
63 | //![2] |
|
63 | //![2] | |
64 |
|
64 | |||
65 | //![3] |
|
65 | //![3] | |
66 | QBarCategoriesAxis* axisX = new QBarCategoriesAxis; |
|
66 | QBarCategoriesAxis* axisX = new QBarCategoriesAxis(); | |
67 | QBarCategoriesAxis* axisY = new QBarCategoriesAxis; |
|
67 | QBarCategoriesAxis* axisY = new QBarCategoriesAxis(); | |
68 |
|
68 | |||
69 | // Customize axis label font |
|
69 | // Customize axis label font | |
70 | QFont labelsFont; |
|
70 | QFont labelsFont; | |
71 | labelsFont.setPixelSize(12); |
|
71 | labelsFont.setPixelSize(12); | |
72 | axisX->setLabelsFont(labelsFont); |
|
72 | axisX->setLabelsFont(labelsFont); | |
73 | axisY->setLabelsFont(labelsFont); |
|
73 | axisY->setLabelsFont(labelsFont); | |
74 |
|
74 | |||
75 | // Customize axis colors |
|
75 | // Customize axis colors | |
76 | QPen axisPen(QRgb(0xd18952)); |
|
76 | QPen axisPen(QRgb(0xd18952)); | |
77 | axisPen.setWidth(2); |
|
77 | axisPen.setWidth(2); | |
78 | axisX->setAxisPen(axisPen); |
|
78 | axisX->setAxisPen(axisPen); | |
79 | axisY->setAxisPen(axisPen); |
|
79 | axisY->setAxisPen(axisPen); | |
80 |
|
80 | |||
81 | // Customize axis label colors |
|
81 | // Customize axis label colors | |
82 | QBrush axisBrush(Qt::white); |
|
82 | QBrush axisBrush(Qt::white); | |
83 | axisX->setLabelsBrush(axisBrush); |
|
83 | axisX->setLabelsBrush(axisBrush); | |
84 | axisY->setLabelsBrush(axisBrush); |
|
84 | axisY->setLabelsBrush(axisBrush); | |
85 |
|
85 | |||
86 | // Customize grid lines and shades |
|
86 | // Customize grid lines and shades | |
87 | axisX->setGridLineVisible(false); |
|
87 | axisX->setGridLineVisible(false); | |
88 | axisY->setGridLineVisible(false); |
|
88 | axisY->setGridLineVisible(false); | |
89 | axisY->setShadesPen(Qt::NoPen); |
|
89 | axisY->setShadesPen(Qt::NoPen); | |
90 | axisY->setShadesBrush(QBrush(QRgb(0xa5a2a3))); |
|
90 | axisY->setShadesBrush(QBrush(QRgb(0xa5a2a3))); | |
91 | axisY->setShadesVisible(true); |
|
91 | axisY->setShadesVisible(true); | |
92 | //![3] |
|
92 | //![3] | |
93 |
|
93 | |||
94 | //![4] |
|
94 | //![4] | |
95 | axisX->append("low"); |
|
95 | axisX->append("low"); | |
96 | axisX->append("optimal"); |
|
96 | axisX->append("optimal"); | |
97 |
axisX->append("high"); |
|
97 | axisX->append("high"); | |
98 |
|
||||
99 | axisX->setRange("low","high"); |
|
98 | axisX->setRange("low","high"); | |
|
99 | ||||
100 | axisY->append("slow"); |
|
100 | axisY->append("slow"); | |
101 |
axisY->append("med |
|
101 | axisY->append("med"); | |
102 | axisY->append("fast"); |
|
102 | axisY->append("fast"); | |
103 | axisY->setRange("slow","fast"); |
|
103 | axisY->setRange("slow","fast"); | |
104 | // axisY->setTicksCount(4); |
|
104 | ||
105 | chart->setAxisX(axisX, series); |
|
105 | chart->setAxisX(axisX, series); | |
106 | chart->setAxisY(axisY, series); |
|
106 | chart->setAxisY(axisY, series); | |
107 | //![4] |
|
107 | //![4] | |
108 |
|
108 | |||
109 | //![5] |
|
109 | //![5] | |
110 | QChartView* chartView = new QChartView(chart); |
|
110 | QChartView* chartView = new QChartView(chart); | |
111 | chartView->setRenderHint(QPainter::Antialiasing); |
|
111 | chartView->setRenderHint(QPainter::Antialiasing); | |
112 | //![5] |
|
112 | //![5] | |
113 |
|
113 | |||
114 | //![6] |
|
114 | //![6] | |
115 | QMainWindow window; |
|
115 | QMainWindow window; | |
116 | window.setCentralWidget(chartView); |
|
116 | window.setCentralWidget(chartView); | |
117 | window.resize(400, 300); |
|
117 | window.resize(400, 300); | |
118 | window.show(); |
|
118 | window.show(); | |
119 | //![6] |
|
119 | //![6] | |
120 |
|
120 | |||
121 | return a.exec(); |
|
121 | return a.exec(); | |
122 | } |
|
122 | } |
@@ -1,119 +1,117 | |||||
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 "chartcategoriesaxisx_p.h" |
|
21 | #include "chartcategoriesaxisx_p.h" | |
22 | #include "qabstractaxis.h" |
|
|||
23 | #include "chartpresenter_p.h" |
|
22 | #include "chartpresenter_p.h" | |
24 | #include "chartanimator_p.h" |
|
23 | #include "chartanimator_p.h" | |
|
24 | #include "qbarcategoriesaxis.h" | |||
25 | #include <QGraphicsLayout> |
|
25 | #include <QGraphicsLayout> | |
26 | #include <QDebug> |
|
|||
27 | #include <QFontMetrics> |
|
26 | #include <QFontMetrics> | |
28 | #include <QBarCategoriesAxis> |
|
|||
29 |
|
27 | |||
30 | static int label_padding = 5; |
|
28 | static int label_padding = 5; | |
31 |
|
29 | |||
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
33 |
|
31 | |||
34 | ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), |
|
32 | ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), | |
35 | m_categoriesAxis(axis) |
|
33 | m_categoriesAxis(axis) | |
36 | { |
|
34 | { | |
37 |
|
35 | |||
38 | } |
|
36 | } | |
39 |
|
37 | |||
40 | ChartCategoriesAxisX::~ChartCategoriesAxisX() |
|
38 | ChartCategoriesAxisX::~ChartCategoriesAxisX() | |
41 | { |
|
39 | { | |
42 | } |
|
40 | } | |
43 |
|
41 | |||
44 | QVector<qreal> ChartCategoriesAxisX::calculateLayout() const |
|
42 | QVector<qreal> ChartCategoriesAxisX::calculateLayout() const | |
45 | { |
|
43 | { | |
46 | Q_ASSERT(m_ticksCount>=2); |
|
44 | Q_ASSERT(m_ticksCount>=2); | |
47 |
|
45 | |||
48 | QVector<qreal> points; |
|
46 | QVector<qreal> points; | |
49 | points.resize(m_ticksCount); |
|
47 | points.resize(m_ticksCount); | |
50 |
|
48 | |||
51 | // TODO: shift logic |
|
49 | // TODO: shift logic | |
52 | const qreal deltaX = m_rect.width()/(m_ticksCount-1); |
|
50 | const qreal deltaX = m_rect.width()/(m_ticksCount-1); | |
53 | for (int i = 0; i < m_ticksCount; ++i) { |
|
51 | for (int i = 0; i < m_ticksCount; ++i) { | |
54 | int x = i * deltaX + m_rect.left(); |
|
52 | int x = i * deltaX + m_rect.left(); | |
55 | points[i] = x; |
|
53 | points[i] = x; | |
56 | } |
|
54 | } | |
57 | return points; |
|
55 | return points; | |
58 | } |
|
56 | } | |
59 |
|
57 | |||
60 | void ChartCategoriesAxisX::updateGeometry() |
|
58 | void ChartCategoriesAxisX::updateGeometry() | |
61 | { |
|
59 | { | |
62 | const QVector<qreal>& layout = ChartAxis::layout(); |
|
60 | const QVector<qreal>& layout = ChartAxis::layout(); | |
63 |
|
61 | |||
64 | m_minWidth = 0; |
|
62 | m_minWidth = 0; | |
65 | m_minHeight = 0; |
|
63 | m_minHeight = 0; | |
66 |
|
64 | |||
67 | if(layout.isEmpty()) return; |
|
65 | if(layout.isEmpty()) return; | |
68 |
|
66 | |||
69 | QStringList ticksList; |
|
67 | QStringList ticksList; | |
70 |
|
68 | |||
71 | createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories()); |
|
69 | createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories()); | |
72 |
|
70 | |||
73 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
71 | QList<QGraphicsItem *> lines = m_grid->childItems(); | |
74 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
72 | QList<QGraphicsItem *> labels = m_labels->childItems(); | |
75 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
73 | QList<QGraphicsItem *> shades = m_shades->childItems(); | |
76 | QList<QGraphicsItem *> axis = m_axis->childItems(); |
|
74 | QList<QGraphicsItem *> axis = m_axis->childItems(); | |
77 |
|
75 | |||
78 | Q_ASSERT(labels.size() == ticksList.size()); |
|
76 | Q_ASSERT(labels.size() == ticksList.size()); | |
79 | Q_ASSERT(layout.size() == ticksList.size()); |
|
77 | Q_ASSERT(layout.size() == ticksList.size()); | |
80 |
|
78 | |||
81 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
79 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
82 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
80 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | |
83 |
|
81 | |||
84 | for (int i = 0; i < layout.size(); ++i) { |
|
82 | for (int i = 0; i < layout.size(); ++i) { | |
85 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
83 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
86 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
84 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); | |
87 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
85 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
88 | if (i>=1) { |
|
86 | if (i>=1) { | |
89 | labelItem->setText(ticksList.at(i-1)); |
|
87 | labelItem->setText(ticksList.at(i-1)); | |
90 | const QRectF& rect = labelItem->boundingRect(); |
|
88 | const QRectF& rect = labelItem->boundingRect(); | |
91 | QPointF center = rect.center(); |
|
89 | QPointF center = rect.center(); | |
92 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
90 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
93 | labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding); |
|
91 | labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding); | |
94 | m_minWidth+=rect.width(); |
|
92 | m_minWidth+=rect.width(); | |
95 | m_minHeight=qMax(rect.height()+label_padding,m_minHeight); |
|
93 | m_minHeight=qMax(rect.height()+label_padding,m_minHeight); | |
96 | }else{ |
|
94 | }else{ | |
97 | labelItem->setVisible(false); |
|
95 | labelItem->setVisible(false); | |
98 | } |
|
96 | } | |
99 |
|
97 | |||
100 | if ((i+1)%2 && i>1) { |
|
98 | if ((i+1)%2 && i>1) { | |
101 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
99 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | |
102 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); |
|
100 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); | |
103 | } |
|
101 | } | |
104 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
102 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
105 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); |
|
103 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); | |
106 | } |
|
104 | } | |
107 | } |
|
105 | } | |
108 |
|
106 | |||
109 | void ChartCategoriesAxisX::handleAxisUpdated() |
|
107 | void ChartCategoriesAxisX::handleAxisUpdated() | |
110 | { |
|
108 | { | |
111 | if(m_categoriesAxis->categories()!=m_categories) |
|
109 | if(m_categoriesAxis->categories()!=m_categories) | |
112 | { |
|
110 | { | |
113 | m_categories=m_categoriesAxis->categories(); |
|
111 | m_categories=m_categoriesAxis->categories(); | |
114 | if(ChartAxis::layout().count()==m_categories.size()+1) updateGeometry(); |
|
112 | if(ChartAxis::layout().count()==m_categories.size()+1) updateGeometry(); | |
115 | } |
|
113 | } | |
116 | ChartAxis::handleAxisUpdated(); |
|
114 | ChartAxis::handleAxisUpdated(); | |
117 | } |
|
115 | } | |
118 |
|
116 | |||
119 | QTCOMMERCIALCHART_END_NAMESPACE |
|
117 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,120 +1,120 | |||||
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 "chartcategoriesaxisy_p.h" |
|
21 | #include "chartcategoriesaxisy_p.h" | |
22 | #include "qabstractaxis.h" |
|
|||
23 | #include "chartpresenter_p.h" |
|
22 | #include "chartpresenter_p.h" | |
24 | #include "chartanimator_p.h" |
|
23 | #include "chartanimator_p.h" | |
|
24 | #include "qbarcategoriesaxis.h" | |||
25 | #include <QGraphicsLayout> |
|
25 | #include <QGraphicsLayout> | |
26 | #include <QDebug> |
|
|||
27 | #include <QFontMetrics> |
|
26 | #include <QFontMetrics> | |
28 | #include <QBarCategoriesAxis> |
|
27 | #include <QBarCategoriesAxis> | |
29 |
|
28 | |||
30 | static int label_padding = 5; |
|
29 | static int label_padding = 5; | |
31 |
|
30 | |||
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
33 |
|
32 | |||
34 |
ChartCategoriesAxisY::ChartCategoriesAxisY(Q |
|
33 | ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), | |
|
34 | m_categoriesAxis(axis) | |||
35 | { |
|
35 | { | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | ChartCategoriesAxisY::~ChartCategoriesAxisY() |
|
38 | ChartCategoriesAxisY::~ChartCategoriesAxisY() | |
39 | { |
|
39 | { | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | QVector<qreal> ChartCategoriesAxisY::calculateLayout() const |
|
42 | QVector<qreal> ChartCategoriesAxisY::calculateLayout() const | |
43 | { |
|
43 | { | |
44 | Q_ASSERT(m_ticksCount>=2); |
|
44 | Q_ASSERT(m_ticksCount>=2); | |
45 |
|
45 | |||
46 | QVector<qreal> points; |
|
46 | QVector<qreal> points; | |
47 | points.resize(m_ticksCount); |
|
47 | points.resize(m_ticksCount); | |
48 |
|
48 | |||
49 | const qreal deltaY = m_rect.height()/(m_ticksCount-1); |
|
49 | const qreal deltaY = m_rect.height()/(m_ticksCount-1); | |
50 | for (int i = 0; i < m_ticksCount; ++i) { |
|
50 | for (int i = 0; i < m_ticksCount; ++i) { | |
51 | int y = i * -deltaY + m_rect.bottom(); |
|
51 | int y = i * -deltaY + m_rect.bottom(); | |
52 | points[i] = y; |
|
52 | points[i] = y; | |
53 | } |
|
53 | } | |
54 |
|
54 | |||
55 | return points; |
|
55 | return points; | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void ChartCategoriesAxisY::updateGeometry() |
|
58 | void ChartCategoriesAxisY::updateGeometry() | |
59 | { |
|
59 | { | |
60 | const QVector<qreal>& layout = ChartAxis::layout(); |
|
60 | const QVector<qreal>& layout = ChartAxis::layout(); | |
61 |
|
61 | |||
62 | m_minWidth = 0; |
|
62 | m_minWidth = 0; | |
63 | m_minHeight = 0; |
|
63 | m_minHeight = 0; | |
64 |
|
64 | |||
65 | if(layout.isEmpty()) return; |
|
65 | if(layout.isEmpty()) return; | |
66 |
|
66 | |||
67 | QStringList ticksList; |
|
67 | QStringList ticksList; | |
68 |
|
68 | |||
69 | createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories()); |
|
69 | createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories()); | |
70 |
|
70 | |||
71 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
71 | QList<QGraphicsItem *> lines = m_grid->childItems(); | |
72 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
72 | QList<QGraphicsItem *> labels = m_labels->childItems(); | |
73 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
73 | QList<QGraphicsItem *> shades = m_shades->childItems(); | |
74 | QList<QGraphicsItem *> axis = m_axis->childItems(); |
|
74 | QList<QGraphicsItem *> axis = m_axis->childItems(); | |
75 |
|
75 | |||
76 | Q_ASSERT(labels.size() == ticksList.size()); |
|
76 | Q_ASSERT(labels.size() == ticksList.size()); | |
77 | Q_ASSERT(layout.size() == ticksList.size()); |
|
77 | Q_ASSERT(layout.size() == ticksList.size()); | |
78 |
|
78 | |||
79 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
79 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
80 |
lineItem->setLine(m_rect.left(), m_rect. |
|
80 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | |
81 |
|
81 | |||
82 | for (int i = 0; i < layout.size(); ++i) { |
|
82 | for (int i = 0; i < layout.size(); ++i) { | |
83 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
83 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
84 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
84 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); | |
85 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
85 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
86 | if(i>=1){ |
|
86 | if(i>=1){ | |
87 | labelItem->setText(ticksList.at(i-1)); |
|
87 | labelItem->setText(ticksList.at(i-1)); | |
88 | const QRectF& rect = labelItem->boundingRect(); |
|
88 | const QRectF& rect = labelItem->boundingRect(); | |
89 | QPointF center = rect.center(); |
|
89 | QPointF center = rect.center(); | |
90 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
90 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
91 |
labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - |
|
91 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y()); | |
92 | m_minWidth+=rect.width(); |
|
92 | m_minWidth+=rect.width(); | |
93 | m_minHeight=qMax(rect.height()+label_padding,m_minHeight); |
|
93 | m_minHeight=qMax(rect.height()+label_padding,m_minHeight); | |
94 | }else{ |
|
94 | }else{ | |
95 | labelItem->setVisible(false); |
|
95 | labelItem->setVisible(false); | |
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | if ((i+1)%2 && i>1) { |
|
99 | if ((i+1)%2 && i>1) { | |
100 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
100 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | |
101 |
rectItem->setRect( |
|
101 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); | |
102 | } |
|
102 | } | |
103 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
103 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
104 |
lineItem->setLine( |
|
104 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); | |
105 | } |
|
105 | } | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | void ChartCategoriesAxisY::handleAxisUpdated() |
|
109 | void ChartCategoriesAxisY::handleAxisUpdated() | |
110 | { |
|
110 | { | |
111 |
|
111 | |||
112 | if(m_categoriesAxis->categories()!=m_categories) |
|
112 | if(m_categoriesAxis->categories()!=m_categories) | |
113 | { |
|
113 | { | |
114 | m_categories=m_categoriesAxis->categories(); |
|
114 | m_categories=m_categoriesAxis->categories(); | |
115 | if(ChartAxis::layout().count()==m_categories.size()+1) updateGeometry(); |
|
115 | if(ChartAxis::layout().count()==m_categories.size()+1) updateGeometry(); | |
116 | } |
|
116 | } | |
117 | ChartAxis::handleAxisUpdated(); |
|
117 | ChartAxis::handleAxisUpdated(); | |
118 | } |
|
118 | } | |
119 |
|
119 | |||
120 | QTCOMMERCIALCHART_END_NAMESPACE |
|
120 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,61 +1,61 | |||||
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 | // W A R N I N G |
|
21 | // W A R N I N G | |
22 | // ------------- |
|
22 | // ------------- | |
23 | // |
|
23 | // | |
24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
24 | // This file is not part of the QtCommercial Chart API. It exists purely as an | |
25 | // implementation detail. This header file may change from version to |
|
25 | // implementation detail. This header file may change from version to | |
26 | // version without notice, or even be removed. |
|
26 | // version without notice, or even be removed. | |
27 | // |
|
27 | // | |
28 | // We mean it. |
|
28 | // We mean it. | |
29 |
|
29 | |||
30 | #ifndef CHARTCATEGORIESAXISY_H |
|
30 | #ifndef CHARTCATEGORIESAXISY_H | |
31 | #define CHARTCATEGORIESAXISY_H |
|
31 | #define CHARTCATEGORIESAXISY_H | |
32 |
|
32 | |||
33 | #include "chartaxis_p.h" |
|
33 | #include "chartaxis_p.h" | |
34 |
|
34 | |||
35 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
35 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
36 |
|
36 | |||
37 | class QAbstractAxis; |
|
37 | class QAbstractAxis; | |
38 | class QBarCategoriesAxis; |
|
38 | class QBarCategoriesAxis; | |
39 | class ChartPresenter; |
|
39 | class ChartPresenter; | |
40 |
|
40 | |||
41 | class ChartCategoriesAxisY : public ChartAxis |
|
41 | class ChartCategoriesAxisY : public ChartAxis | |
42 | { |
|
42 | { | |
43 | public: |
|
43 | public: | |
44 |
ChartCategoriesAxisY(Q |
|
44 | ChartCategoriesAxisY(QBarCategoriesAxis *axis, ChartPresenter *presenter); | |
45 | ~ChartCategoriesAxisY(); |
|
45 | ~ChartCategoriesAxisY(); | |
46 |
|
46 | |||
47 | AxisType axisType() const { return Y_AXIS;} |
|
47 | AxisType axisType() const { return Y_AXIS;} | |
48 |
|
48 | |||
49 | protected: |
|
49 | protected: | |
50 | QVector<qreal> calculateLayout() const; |
|
50 | QVector<qreal> calculateLayout() const; | |
51 | void updateGeometry(); |
|
51 | void updateGeometry(); | |
52 | Q_SLOTS |
|
52 | Q_SLOTS | |
53 | void handleAxisUpdated(); |
|
53 | void handleAxisUpdated(); | |
54 | private: |
|
54 | private: | |
55 | QStringList m_categories; |
|
55 | QStringList m_categories; | |
56 | QBarCategoriesAxis *m_categoriesAxis; |
|
56 | QBarCategoriesAxis *m_categoriesAxis; | |
57 | }; |
|
57 | }; | |
58 |
|
58 | |||
59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
59 | QTCOMMERCIALCHART_END_NAMESPACE | |
60 |
|
60 | |||
61 | #endif /* CHARTCATEGORIESAXISY_H */ |
|
61 | #endif /* CHARTCATEGORIESAXISY_H */ |
General Comments 0
You need to be logged in to leave comments.
Login now