##// END OF EJS Templates
QCategoryAxis scrolling support added
Marek Rosa -
r1950:d136a6135e13
parent child
Show More
@@ -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 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3 SOURCES += \
4 4 font/font.cpp \
5 5 xyseries/linechart.cpp \
6 6 xyseries/scatterchart.cpp \
7 7 xyseries/splinechart.cpp \
8 8 xyseries/areachart.cpp \
9 xyseries/categorylinechart.cpp \
9 10 barseries/verticalstackedbarchart.cpp \
10 11 barseries/horizontalstackedbarchart.cpp \
11 12 barseries/verticalbarchart.cpp \
12 13 barseries/horizontalbarchart.cpp \
13 14 barseries/horizontalpercentbarchart.cpp \
14 15 barseries/verticalpercentbarchart.cpp \
15 16 pieseries/piechart.cpp \
16 17 pieseries/donutchart.cpp
@@ -1,134 +1,149
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartcategoryaxisx_p.h"
22 22 #include "qcategoryaxis.h"
23 23 #include "qabstractaxis.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QFontMetrics>
27 27 #include <qmath.h>
28 28
29 29 static int label_padding = 5;
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
34 34 {
35 35 }
36 36
37 37 ChartCategoryAxisX::~ChartCategoryAxisX()
38 38 {
39 39 }
40 40
41 41 QVector<qreal> ChartCategoryAxisX::calculateLayout() const
42 42 {
43 43 QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis);
44 44 int tickCount = axis->categoriesLabels().count() + 1;
45 45 QVector<qreal> points;
46 46
47 47 if (tickCount < 2)
48 48 return points;
49 49
50 50 qreal range = axis->max() - axis->min();
51 51 if (range > 0) {
52 52 points.resize(tickCount);
53 53 qreal scale = m_rect.width() / range;
54 54 for (int i = 0; i < tickCount; ++i)
55 55 if (i < tickCount - 1) {
56 56 int x = (axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.left();
57 57 points[i] = x;
58 58 } else {
59 59 int x = (axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.left();
60 60 points[i] = x;
61 61 }
62 62 }
63 63 return points;
64 64 }
65 65
66 66 void ChartCategoryAxisX::updateGeometry()
67 67 {
68 68 const QVector<qreal>& layout = ChartAxis::layout();
69 69
70 70 m_minWidth = 0;
71 71 m_minHeight = 0;
72 72
73 73 if(layout.isEmpty()) return;
74 74
75 75 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
76 76 QStringList ticksList = categoryAxis->categoriesLabels();
77 77
78 78 QList<QGraphicsItem *> lines = m_grid->childItems();
79 79 QList<QGraphicsItem *> labels = m_labels->childItems();
80 80 QList<QGraphicsItem *> shades = m_shades->childItems();
81 81 QList<QGraphicsItem *> axis = m_arrow->childItems();
82 82
83 // Q_ASSERT(labels.size() == ticksList.size());
84 // Q_ASSERT(layout.size() == ticksList.size());
83
84 for (int i = 0; i < labels.count(); i++) {
85 labels.at(i)->setVisible(false);
86 }
85 87
86 88 // axis base line
87 89 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
88 90 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
89 91
90 92 for (int i = 0; i < layout.size(); ++i) {
91 93
92 94 // label items
93 95 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
94 96 if (i < ticksList.count()) {
95 97 labelItem->setText(ticksList.at(i));
96 98 }
97 99 const QRectF& rect = labelItem->boundingRect();
98 100 QPointF center = rect.center();
99 101 labelItem->setTransformOriginPoint(center.x(), center.y());
100 if (i < ticksList.count())
102 if (i < layout.size() - 1) {
101 103 labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), m_rect.bottom() + label_padding);
102 else
104 } else {
103 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 114 m_minWidth += rect.width();
106 115 m_minHeight = qMax(rect.height()+ label_padding, m_minHeight);
107 116
108 117 if ((i + 1) % 2 && i > 1) {
109 118 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
110 119 rectItem->setRect(layout[i - 1],m_rect.top(),layout[i]-layout[i - 1],m_rect.height());
111 120 }
112 121
113 122 // grid lines and axis line ticks
114 if (layout[i] < m_rect.left() || layout[i] > m_rect.right())
115 continue;
116
117 123 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
118 124 lineItem->setPos(layout[i], m_rect.top());
119 125 lineItem->setLine(0, 0, 0, m_rect.height());
120 126
121 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
122 lineItem->setPos(layout[i], m_rect.bottom());
123 lineItem->setLine(0, 0, 0, 5);
127 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
128 tickLineItem->setPos(layout[i], m_rect.bottom());
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 143 void ChartCategoryAxisX::handleAxisUpdated()
129 144 {
130 145 updateGeometry();
131 146 ChartAxis::handleAxisUpdated();
132 147 }
133 148
134 149 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,149 +1,161
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartcategoryaxisy_p.h"
22 22 #include "qcategoryaxis.h"
23 23 #include "qabstractaxis.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QFontMetrics>
27 27 #include <qmath.h>
28 28
29 29 static int label_padding = 5;
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
34 34 {
35 35 }
36 36
37 37 ChartCategoryAxisY::~ChartCategoryAxisY()
38 38 {
39 39 }
40 40
41 41 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
42 42 {
43 43 QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis);
44 44 int tickCount = axis->categoriesLabels().count() + 1;
45 45 QVector<qreal> points;
46 46
47 47 if (tickCount < 2)
48 48 return points;
49 49
50 50 qreal range = axis->max() - axis->min();
51 51 if (range > 0) {
52 52 points.resize(tickCount);
53 53 qreal scale = m_rect.height() / range;
54 54 for (int i = 0; i < tickCount; ++i)
55 55 if (i < tickCount - 1) {
56 56 int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.bottom();
57 57 points[i] = y;
58 58 } else {
59 59 int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.bottom();
60 60 points[i] = y;
61 61 }
62 62 }
63 63
64 64 return points;
65 65 }
66 66
67 67 void ChartCategoryAxisY::updateGeometry()
68 68 {
69 69 const QVector<qreal> &layout = ChartAxis::layout();
70 70 m_minWidth = 0;
71 71 m_minHeight = 0;
72 72
73 73 if(layout.isEmpty()) {
74 74 return;
75 75 }
76 76
77 77 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
78 78 QStringList ticksList = categoryAxis->categoriesLabels();
79 79
80 80 QList<QGraphicsItem *> lines = m_grid->childItems();
81 81 QList<QGraphicsItem *> labels = m_labels->childItems();
82 82 QList<QGraphicsItem *> shades = m_shades->childItems();
83 83 QList<QGraphicsItem *> axis = m_arrow->childItems();
84 84
85 // Q_ASSERT(labels.size() == ticksList.size());
86 // Q_ASSERT(layout.size() == ticksList.size());
85 // qreal height = 2*m_rect.bottom();
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 91 // axis base line
91 92 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
92 93 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
93 94
94 95 for (int i = 0; i < layout.size(); ++i) {
95 96
96 97 // label items
97 98 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
98
99 99 if (i < ticksList.count()) {
100 100 labelItem->setText(ticksList.at(i));
101 101 }
102 102 const QRectF& rect = labelItem->boundingRect();
103 103
104 104 QPointF center = rect.center();
105 105 labelItem->setTransformOriginPoint(center.x(), center.y());
106 106
107 if (i < ticksList.count())
107 if (i < layout.size() - 1)
108 108 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y());
109 109 else
110 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 114 labelItem->setVisible(false);
114 }
115 else {
115 else
116 116 labelItem->setVisible(true);
117 height=labelItem->pos().y();
118 }
117
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 126 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
121 127 m_minHeight+=rect.height();
122 128
123 129 if ((i+1)%2 && i>1) {
124 130 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
125 131 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
126 132 }
127 133
128 134 // grid lines and axis line ticks
129 if (layout[i] < m_rect.left() || layout[i] > m_rect.right())
130 continue;
131
132 135 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
133 136 lineItem->setPos(m_rect.left(), layout[i]);
134 137 lineItem->setLine(0, 0, m_rect.width(), 0);
135 138
136 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
137 lineItem->setPos(m_rect.left(), layout[i]);
138 lineItem->setLine(-5, 0, 0, 0);
139 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
140 tickLineItem->setPos(m_rect.left(), layout[i]);
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 155 void ChartCategoryAxisY::handleAxisUpdated()
144 156 {
145 157 updateGeometry();
146 158 ChartAxis::handleAxisUpdated();
147 159 }
148 160
149 161 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now