@@ -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 |
@@ -6,6 +6,7 SOURCES += \ | |||||
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 \ |
@@ -80,8 +80,10 void ChartCategoryAxisX::updateGeometry() | |||||
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)); | |
@@ -97,10 +99,17 void ChartCategoryAxisX::updateGeometry() | |||||
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); | |
@@ -111,16 +120,22 void ChartCategoryAxisX::updateGeometry() | |||||
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 | } |
@@ -82,10 +82,11 void ChartCategoryAxisY::updateGeometry() | |||||
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)); | |
@@ -95,7 +96,6 void ChartCategoryAxisY::updateGeometry() | |||||
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 | } | |
@@ -104,18 +104,24 void ChartCategoryAxisY::updateGeometry() | |||||
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(); | |
@@ -126,16 +132,22 void ChartCategoryAxisY::updateGeometry() | |||||
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 | } |
General Comments 0
You need to be logged in to leave comments.
Login now