@@ -1,62 +1,62 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qlinechartseries.h> |
|
5 | 5 | #include <qchart.h> |
|
6 | 6 | #include <qchartaxis.h> |
|
7 | 7 | #include <cmath> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | #define PI 3.14159265358979 |
|
12 | 12 | |
|
13 | 13 | int main(int argc, char *argv[]) |
|
14 | 14 | { |
|
15 | 15 | QApplication a(argc, argv); |
|
16 | 16 | |
|
17 | 17 | QMainWindow window; |
|
18 | 18 | |
|
19 | 19 | QLineChartSeries* series0 = new QLineChartSeries(); |
|
20 | 20 | QPen blue(Qt::blue); |
|
21 | 21 | blue.setWidth(3); |
|
22 | 22 | series0->setPen(blue); |
|
23 | 23 | QLineChartSeries* series1 = new QLineChartSeries(); |
|
24 | 24 | QPen red(Qt::red); |
|
25 | 25 | red.setWidth(3); |
|
26 | 26 | series1->setPen(red); |
|
27 | 27 | |
|
28 | 28 | int numPoints = 100; |
|
29 | 29 | |
|
30 | 30 | for (int x = 0; x <= numPoints; ++x) { |
|
31 | 31 | series0->add(x, fabs(sin(PI/50*x)*100)); |
|
32 | 32 | series1->add(x, fabs(cos(PI/50*x)*100)); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | QChartView* chartView = new QChartView(&window); |
|
36 | 36 | |
|
37 | 37 | chartView->setRenderHint(QPainter::Antialiasing); |
|
38 |
chartView->setTitle(" |
|
|
38 | chartView->setChartTitle("This is custom axis chart example"); | |
|
39 | 39 | chartView->addSeries(series0); |
|
40 | 40 | chartView->addSeries(series1); |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | QLinearGradient backgroundGradient; |
|
44 | 44 | backgroundGradient.setColorAt(0.0, Qt::white); |
|
45 | 45 | backgroundGradient.setColorAt(1.0, QRgb(0xffff80)); |
|
46 | 46 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
47 | 47 | chartView->setChartBackgroundBrush(backgroundGradient); |
|
48 | 48 | |
|
49 | 49 | QChartAxis axis = chartView->defaultAxisX(); |
|
50 | 50 | axis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); |
|
51 | 51 | axis.setGridPen(Qt::DashLine); |
|
52 | 52 | |
|
53 | 53 | chartView->setDefaultAxisX(axis); |
|
54 | 54 | axis.setShadesBrush(Qt::yellow); |
|
55 | 55 | chartView->setDefaultAxisY(axis); |
|
56 | 56 | |
|
57 | 57 | window.setCentralWidget(chartView); |
|
58 | 58 | window.resize(400, 300); |
|
59 | 59 | window.show(); |
|
60 | 60 | |
|
61 | 61 | return a.exec(); |
|
62 | 62 | } |
@@ -1,51 +1,55 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qlinechartseries.h> |
|
5 | 5 | #include <qchart.h> |
|
6 | 6 | #include <cmath> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | #define PI 3.14159265358979 |
|
11 | 11 | |
|
12 | 12 | int main(int argc, char *argv[]) |
|
13 | 13 | { |
|
14 | 14 | QApplication a(argc, argv); |
|
15 | 15 | |
|
16 | 16 | QMainWindow window; |
|
17 | 17 | |
|
18 | 18 | QLineChartSeries* series0 = new QLineChartSeries(); |
|
19 | 19 | QPen blue(Qt::blue); |
|
20 | 20 | blue.setWidth(3); |
|
21 | 21 | series0->setPen(blue); |
|
22 | 22 | QLineChartSeries* series1 = new QLineChartSeries(); |
|
23 | 23 | QPen red(Qt::red); |
|
24 | 24 | red.setWidth(3); |
|
25 | 25 | series1->setPen(red); |
|
26 | 26 | |
|
27 | 27 | int numPoints = 100; |
|
28 | 28 | |
|
29 | 29 | for (int x = 0; x <= numPoints; ++x) { |
|
30 | 30 | series0->add(x, fabs(sin(PI/50*x)*100)); |
|
31 | 31 | series1->add(x, fabs(cos(PI/50*x)*100)); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | QChartView* chartView = new QChartView(&window); |
|
35 | 35 | chartView->setRenderHint(QPainter::Antialiasing); |
|
36 | chartView->setTitle("Custom color line chart example"); | |
|
36 | ||
|
37 | QFont font; | |
|
38 | font.setPixelSize(18); | |
|
39 | chartView->setChartTitleFont(font); | |
|
40 | chartView->setChartTitle("Custom color line chart example"); | |
|
37 | 41 | chartView->addSeries(series0); |
|
38 | 42 | chartView->addSeries(series1); |
|
39 | 43 | |
|
40 | 44 | QLinearGradient backgroundGradient; |
|
41 | 45 | backgroundGradient.setColorAt(0.0, Qt::blue); |
|
42 | 46 | backgroundGradient.setColorAt(1.0, Qt::yellow); |
|
43 | 47 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
44 | 48 | chartView->setChartBackgroundBrush(backgroundGradient); |
|
45 | 49 | |
|
46 | 50 | window.setCentralWidget(chartView); |
|
47 | 51 | window.resize(400, 300); |
|
48 | 52 | window.show(); |
|
49 | 53 | |
|
50 | 54 | return a.exec(); |
|
51 | 55 | } |
@@ -1,40 +1,40 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qlinechartseries.h> |
|
5 | 5 | #include <qchart.h> |
|
6 | 6 | #include <cmath> |
|
7 | 7 | #include "wavegenerator.h" |
|
8 | 8 | #include <QGLWidget> |
|
9 | 9 | |
|
10 | 10 | int main(int argc, char *argv[]) |
|
11 | 11 | { |
|
12 | 12 | QApplication a(argc, argv); |
|
13 | 13 | |
|
14 | 14 | QMainWindow window; |
|
15 | 15 | |
|
16 | 16 | QLineChartSeries* series0 = new QLineChartSeries(); |
|
17 | 17 | QPen blue(Qt::blue); |
|
18 | 18 | blue.setWidth(3); |
|
19 | 19 | series0->setPen(blue); |
|
20 | 20 | QLineChartSeries* series1 = new QLineChartSeries(); |
|
21 | 21 | QPen red(Qt::red); |
|
22 | 22 | red.setWidth(3); |
|
23 | 23 | series1->setPen(red); |
|
24 | 24 | |
|
25 | 25 | WaveGenerator generator(series0,series1); |
|
26 | 26 | |
|
27 | 27 | QChartView* chartView = new QChartView(&window); |
|
28 | 28 | |
|
29 | 29 | chartView->setViewport( new QGLWidget() ); |
|
30 | 30 | chartView->setRenderHint(QPainter::Antialiasing); |
|
31 | chartView->setTitle("This is wave generator buahha."); | |
|
31 | chartView->setChartTitle("This is wave generator buahha."); | |
|
32 | 32 | chartView->addSeries(series0); |
|
33 | 33 | chartView->addSeries(series1); |
|
34 | 34 | |
|
35 | 35 | window.setCentralWidget(chartView); |
|
36 | 36 | window.resize(400, 300); |
|
37 | 37 | window.show(); |
|
38 | 38 | |
|
39 | 39 | return a.exec(); |
|
40 | 40 | } |
@@ -1,46 +1,46 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qlinechartseries.h> |
|
5 | 5 | #include <qchart.h> |
|
6 | 6 | #include <cmath> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | #define PI 3.14159265358979 |
|
11 | 11 | |
|
12 | 12 | int main(int argc, char *argv[]) |
|
13 | 13 | { |
|
14 | 14 | QApplication a(argc, argv); |
|
15 | 15 | |
|
16 | 16 | QMainWindow window; |
|
17 | 17 | |
|
18 | 18 | QLineChartSeries* series0 = new QLineChartSeries(); |
|
19 | 19 | QPen blue(Qt::blue); |
|
20 | 20 | blue.setWidth(3); |
|
21 | 21 | series0->setPen(blue); |
|
22 | 22 | QLineChartSeries* series1 = new QLineChartSeries(); |
|
23 | 23 | QPen red(Qt::red); |
|
24 | 24 | red.setWidth(3); |
|
25 | 25 | series1->setPen(red); |
|
26 | 26 | |
|
27 | 27 | int numPoints = 100; |
|
28 | 28 | |
|
29 | 29 | for (int x = 0; x <= numPoints; ++x) { |
|
30 | 30 | series0->add(x, fabs(sin(PI/50*x)*100)); |
|
31 | 31 | series1->add(x, fabs(cos(PI/50*x)*100)); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | QChartView* chartView = new QChartView(&window); |
|
35 | 35 | |
|
36 | 36 | chartView->setRenderHint(QPainter::Antialiasing); |
|
37 | chartView->setTitle("Basic line chart example"); | |
|
37 | chartView->setChartTitle("Basic line chart example"); | |
|
38 | 38 | chartView->addSeries(series0); |
|
39 | 39 | chartView->addSeries(series1); |
|
40 | 40 | |
|
41 | 41 | window.setCentralWidget(chartView); |
|
42 | 42 | window.resize(400, 300); |
|
43 | 43 | window.show(); |
|
44 | 44 | |
|
45 | 45 | return a.exec(); |
|
46 | 46 | } |
@@ -1,44 +1,44 | |||
|
1 | 1 | #include "chartwidget.h" |
|
2 | 2 | #include <QApplication> |
|
3 | 3 | #include <QMainWindow> |
|
4 | 4 | #include <qlinechartseries.h> |
|
5 | 5 | #include <cmath> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | #define PI 3.14159265358979 |
|
10 | 10 | |
|
11 | 11 | int main(int argc, char *argv[]) |
|
12 | 12 | { |
|
13 | 13 | QApplication a(argc, argv); |
|
14 | 14 | |
|
15 | 15 | QMainWindow window; |
|
16 | 16 | |
|
17 | 17 | QLineChartSeries* series0 = new QLineChartSeries(); |
|
18 | 18 | QPen blue(Qt::blue); |
|
19 | 19 | blue.setWidth(3); |
|
20 | 20 | series0->setPen(blue); |
|
21 | 21 | QLineChartSeries* series1 = new QLineChartSeries(); |
|
22 | 22 | QPen red(Qt::red); |
|
23 | 23 | red.setWidth(3); |
|
24 | 24 | series1->setPen(red); |
|
25 | 25 | |
|
26 | 26 | int numPoints = 100; |
|
27 | 27 | |
|
28 | 28 | for (int x = 0; x <= numPoints; ++x) { |
|
29 | 29 | series0->add(x, fabs(sin(PI/50*x)*100)); |
|
30 | 30 | series1->add(x, fabs(cos(PI/50*x)*100)); |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | ChartWidget* chartWidget = new ChartWidget(&window); |
|
34 | 34 | chartWidget->setRenderHint(QPainter::Antialiasing); |
|
35 | chartWidget->setTitle("Zoom in/out line chart example"); | |
|
35 | chartWidget->setChartTitle("Zoom in/out line chart example"); | |
|
36 | 36 | chartWidget->addSeries(series0); |
|
37 | 37 | chartWidget->addSeries(series1); |
|
38 | 38 | |
|
39 | 39 | window.setCentralWidget(chartWidget); |
|
40 | 40 | window.resize(400, 300); |
|
41 | 41 | window.show(); |
|
42 | 42 | |
|
43 | 43 | return a.exec(); |
|
44 | 44 | } |
@@ -1,204 +1,209 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qscatterseries.h" |
|
3 | 3 | #include "qscatterseries_p.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include "qchartaxis.h" |
|
6 | 6 | #include "chartpresenter_p.h" |
|
7 | 7 | #include "chartdataset_p.h" |
|
8 | 8 | |
|
9 | 9 | //series |
|
10 | 10 | #include "barchartseries.h" |
|
11 | 11 | #include "stackedbarchartseries.h" |
|
12 | 12 | #include "percentbarchartseries.h" |
|
13 | 13 | #include "qlinechartseries.h" |
|
14 | 14 | |
|
15 | 15 | #include <QGraphicsScene> |
|
16 | 16 | #include <QGraphicsSceneResizeEvent> |
|
17 | 17 | #include <QDebug> |
|
18 | 18 | |
|
19 | 19 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
20 | 20 | |
|
21 | 21 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
22 | 22 | m_backgroundItem(0), |
|
23 | 23 | m_titleItem(0), |
|
24 | 24 | m_dataset(new ChartDataSet(this)), |
|
25 | 25 | m_presenter(new ChartPresenter(this,m_dataset)) |
|
26 | 26 | { |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | QChart::~QChart() {} |
|
30 | 30 | |
|
31 | 31 | void QChart::addSeries(QChartSeries* series) |
|
32 | 32 | { |
|
33 | 33 | m_dataset->addSeries(series); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | //TODO on review, is it really needed ?? |
|
37 | 37 | QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) |
|
38 | 38 | { |
|
39 | 39 | QChartSeries *series(0); |
|
40 | 40 | |
|
41 | 41 | switch (type) { |
|
42 | 42 | case QChartSeries::SeriesTypeLine: { |
|
43 | 43 | series = new QLineChartSeries(this); |
|
44 | 44 | break; |
|
45 | 45 | } |
|
46 | 46 | case QChartSeries::SeriesTypeBar: { |
|
47 | 47 | //series = new BarChartSeries(this); |
|
48 | 48 | break; |
|
49 | 49 | } |
|
50 | 50 | case QChartSeries::SeriesTypeStackedBar: { |
|
51 | 51 | //series = new StackedBarChartSeries(this); |
|
52 | 52 | break; |
|
53 | 53 | } |
|
54 | 54 | case QChartSeries::SeriesTypePercentBar: { |
|
55 | 55 | //series = new PercentBarChartSeries(this); |
|
56 | 56 | break; |
|
57 | 57 | } |
|
58 | 58 | case QChartSeries::SeriesTypeScatter: { |
|
59 | 59 | series = new QScatterSeries(this); |
|
60 | 60 | break; |
|
61 | 61 | } |
|
62 | 62 | case QChartSeries::SeriesTypePie: { |
|
63 | 63 | series = new QPieSeries(this); |
|
64 | 64 | break; |
|
65 | 65 | } |
|
66 | 66 | default: |
|
67 | 67 | Q_ASSERT(false); |
|
68 | 68 | break; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | addSeries(series); |
|
72 | 72 | return series; |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
76 | 76 | { |
|
77 | 77 | |
|
78 | 78 | if(!m_backgroundItem) { |
|
79 | 79 | m_backgroundItem = new QGraphicsRectItem(this); |
|
80 | 80 | m_backgroundItem->setZValue(-1); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | m_backgroundItem->setBrush(brush); |
|
84 | 84 | m_backgroundItem->update(); |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
88 | 88 | { |
|
89 | 89 | |
|
90 | 90 | if(!m_backgroundItem) { |
|
91 | 91 | m_backgroundItem = new QGraphicsRectItem(this); |
|
92 | 92 | m_backgroundItem->setZValue(-1); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | m_backgroundItem->setPen(pen); |
|
96 | 96 | m_backgroundItem->update(); |
|
97 | 97 | } |
|
98 | 98 | |
|
99 |
void QChart::setTitle(const QString& title |
|
|
99 | void QChart::setChartTitle(const QString& title) | |
|
100 | 100 | { |
|
101 | 101 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); |
|
102 | 102 | m_titleItem->setPlainText(title); |
|
103 | } | |
|
104 | ||
|
105 | void QChart::setChartTitleFont(const QFont& font) | |
|
106 | { | |
|
107 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); | |
|
103 | 108 | m_titleItem->setFont(font); |
|
104 | 109 | } |
|
105 | 110 | |
|
106 | 111 | int QChart::margin() const |
|
107 | 112 | { |
|
108 | 113 | return m_presenter->margin(); |
|
109 | 114 | } |
|
110 | 115 | |
|
111 | 116 | void QChart::setMargin(int margin) |
|
112 | 117 | { |
|
113 | 118 | m_presenter->setMargin(margin); |
|
114 | 119 | } |
|
115 | 120 | |
|
116 | 121 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
117 | 122 | { |
|
118 | 123 | m_presenter->setChartTheme(theme); |
|
119 | 124 | } |
|
120 | 125 | |
|
121 | 126 | QChart::ChartTheme QChart::chartTheme() const |
|
122 | 127 | { |
|
123 | 128 | return m_presenter->chartTheme(); |
|
124 | 129 | } |
|
125 | 130 | |
|
126 | 131 | void QChart::zoomInToRect(const QRectF& rectangle) |
|
127 | 132 | { |
|
128 | 133 | m_presenter->zoomInToRect(rectangle); |
|
129 | 134 | } |
|
130 | 135 | |
|
131 | 136 | void QChart::zoomIn() |
|
132 | 137 | { |
|
133 | 138 | m_presenter->zoomIn(); |
|
134 | 139 | } |
|
135 | 140 | |
|
136 | 141 | void QChart::zoomOut() |
|
137 | 142 | { |
|
138 | 143 | m_presenter->zoomOut(); |
|
139 | 144 | } |
|
140 | 145 | |
|
141 | 146 | void QChart::zoomReset() |
|
142 | 147 | { |
|
143 | 148 | m_presenter->zoomReset(); |
|
144 | 149 | } |
|
145 | 150 | |
|
146 | 151 | void QChart::setDefaultAxisX(const QChartAxis& axis) |
|
147 | 152 | { |
|
148 | 153 | m_presenter->setDefaultAxisX(axis); |
|
149 | 154 | } |
|
150 | 155 | |
|
151 | 156 | void QChart::setDefaultAxisY(const QChartAxis& axis) |
|
152 | 157 | { |
|
153 | 158 | m_presenter->setDefaultAxisY(axis); |
|
154 | 159 | } |
|
155 | 160 | |
|
156 | 161 | QChartAxis QChart::defaultAxisX() const |
|
157 | 162 | { |
|
158 | 163 | return m_presenter->defaultAxisX(); |
|
159 | 164 | } |
|
160 | 165 | |
|
161 | 166 | QChartAxis QChart::defaultAxisY() const |
|
162 | 167 | { |
|
163 | 168 | return m_presenter->defaultAxisY(); |
|
164 | 169 | } |
|
165 | 170 | |
|
166 | 171 | int QChart::addAxisY(const QChartAxis& axis) |
|
167 | 172 | { |
|
168 | 173 | return m_presenter->addAxisY(axis); |
|
169 | 174 | } |
|
170 | 175 | |
|
171 | 176 | QChartAxis QChart::axisY(int id) const |
|
172 | 177 | { |
|
173 | 178 | return m_presenter->axisY(id); |
|
174 | 179 | } |
|
175 | 180 | |
|
176 | 181 | void QChart::removeAxisY(int id) |
|
177 | 182 | { |
|
178 | 183 | m_presenter->removeAxisY(id); |
|
179 | 184 | } |
|
180 | 185 | |
|
181 | 186 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
182 | 187 | { |
|
183 | 188 | |
|
184 | 189 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
185 | 190 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
186 | 191 | |
|
187 | 192 | // recalculate title position |
|
188 | 193 | if (m_titleItem) { |
|
189 | 194 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
190 | 195 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
191 | 196 | } |
|
192 | 197 | |
|
193 | 198 | //recalculate background gradient |
|
194 | 199 | if (m_backgroundItem) { |
|
195 | 200 | m_backgroundItem->setRect(rect); |
|
196 | 201 | } |
|
197 | 202 | |
|
198 | 203 | QGraphicsWidget::resizeEvent(event); |
|
199 | 204 | update(); |
|
200 | 205 | } |
|
201 | 206 | |
|
202 | 207 | #include "moc_qchart.cpp" |
|
203 | 208 | |
|
204 | 209 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,92 +1,93 | |||
|
1 | 1 | #ifndef CHART_H |
|
2 | 2 | #define CHART_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <qchartseries.h> |
|
6 | 6 | #include <QGraphicsWidget> |
|
7 | 7 | #include <QLinearGradient> |
|
8 | 8 | #include <QFont> |
|
9 | 9 | |
|
10 | 10 | class QGraphicsSceneResizeEvent; |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | class AxisItem; |
|
15 | 15 | class QChartSeries; |
|
16 | 16 | class PlotDomain; |
|
17 | 17 | class BarGroup; |
|
18 | 18 | class QChartAxis; |
|
19 | 19 | class ChartTheme; |
|
20 | 20 | class ChartItem; |
|
21 | 21 | class ChartDataSet; |
|
22 | 22 | class ChartPresenter; |
|
23 | 23 | |
|
24 | 24 | // TODO: We don't need to have QChart tied to QGraphicsItem: |
|
25 | 25 | //class QTCOMMERCIALCHART_EXPORT QChart |
|
26 | 26 | //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem { |
|
27 | 27 | // public: QChartGraphicsItem(QChart &chart); |
|
28 | 28 | |
|
29 | 29 | /*! |
|
30 | 30 | * TODO: define the responsibilities |
|
31 | 31 | */ |
|
32 | 32 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget |
|
33 | 33 | { |
|
34 | 34 | Q_OBJECT |
|
35 | 35 | public: |
|
36 | 36 | enum ChartTheme { |
|
37 | 37 | /*! The default theme follows the GUI style of the Operating System */ |
|
38 | 38 | ChartThemeDefault, |
|
39 | 39 | ChartThemeVanilla, |
|
40 | 40 | ChartThemeIcy, |
|
41 | 41 | ChartThemeGrayscale, |
|
42 | 42 | ChartThemeScientific, |
|
43 | 43 | //ChartThemeUnnamed1 |
|
44 | 44 | }; |
|
45 | 45 | |
|
46 | 46 | public: |
|
47 | 47 | QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); |
|
48 | 48 | ~QChart(); |
|
49 | 49 | |
|
50 | 50 | void addSeries(QChartSeries* series); |
|
51 | 51 | |
|
52 | 52 | //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type); |
|
53 | 53 | // TODO: who owns the series now? maybe owned by chart and returned a reference instead... |
|
54 | 54 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
55 | 55 | |
|
56 | 56 | void setMargin(int margin); |
|
57 | 57 | int margin() const; |
|
58 | 58 | void setChartTheme(QChart::ChartTheme theme); |
|
59 | 59 | QChart::ChartTheme chartTheme() const; |
|
60 | 60 | |
|
61 |
void setTitle(const QString& title |
|
|
61 | void setChartTitle(const QString& title); | |
|
62 | void setChartTitleFont(const QFont& font); | |
|
62 | 63 | void setChartBackgroundBrush(const QBrush& brush); |
|
63 | 64 | void setChartBackgroundPen(const QPen& pen); |
|
64 | 65 | |
|
65 | 66 | void zoomInToRect(const QRectF& rectangle); |
|
66 | 67 | void zoomIn(); |
|
67 | 68 | void zoomOut(); |
|
68 | 69 | void zoomReset(); |
|
69 | 70 | |
|
70 | 71 | void setDefaultAxisX(const QChartAxis& axis); |
|
71 | 72 | void setDefaultAxisY(const QChartAxis& axis); |
|
72 | 73 | QChartAxis defaultAxisX() const; |
|
73 | 74 | QChartAxis defaultAxisY() const; |
|
74 | 75 | QChartAxis axisY(int id) const; |
|
75 | 76 | int addAxisY(const QChartAxis& axis); |
|
76 | 77 | void removeAxisY(int id); |
|
77 | 78 | |
|
78 | 79 | protected: |
|
79 | 80 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
80 | 81 | |
|
81 | 82 | private: |
|
82 | 83 | Q_DISABLE_COPY(QChart) |
|
83 | 84 | QGraphicsRectItem* m_backgroundItem; |
|
84 | 85 | QGraphicsTextItem* m_titleItem; |
|
85 | 86 | QRectF m_rect; |
|
86 | 87 | ChartDataSet *m_dataset; |
|
87 | 88 | ChartPresenter *m_presenter; |
|
88 | 89 | }; |
|
89 | 90 | |
|
90 | 91 | QTCOMMERCIALCHART_END_NAMESPACE |
|
91 | 92 | |
|
92 | 93 | #endif |
@@ -1,236 +1,241 | |||
|
1 | 1 | #include "qchartview.h" |
|
2 | 2 | #include "qchart.h" |
|
3 | 3 | #include "qchartaxis.h" |
|
4 | 4 | #include <QGraphicsView> |
|
5 | 5 | #include <QGraphicsScene> |
|
6 | 6 | #include <QRubberBand> |
|
7 | 7 | #include <QResizeEvent> |
|
8 | 8 | #include <QDebug> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | QChartView::QChartView(QWidget *parent) : |
|
13 | 13 | QGraphicsView(parent), |
|
14 | 14 | m_scene(new QGraphicsScene()), |
|
15 | 15 | m_chart(new QChart()), |
|
16 | 16 | m_rubberBand(0), |
|
17 | 17 | m_verticalRubberBand(false), |
|
18 | 18 | m_horizonalRubberBand(false) |
|
19 | 19 | { |
|
20 | 20 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
21 | 21 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
22 | 22 | setScene(m_scene); |
|
23 | 23 | m_chart->setMargin(50); |
|
24 | 24 | m_scene->addItem(m_chart); |
|
25 | 25 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | QChartView::~QChartView() |
|
29 | 29 | { |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | void QChartView::resizeEvent(QResizeEvent *event) |
|
33 | 33 | { |
|
34 | 34 | m_scene->setSceneRect(0,0,size().width(),size().height()); |
|
35 | 35 | m_chart->resize(size()); |
|
36 | 36 | QWidget::resizeEvent(event); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | void QChartView::addSeries(QChartSeries* series) |
|
40 | 40 | { |
|
41 | 41 | m_chart->addSeries(series); |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type) |
|
45 | 45 | { |
|
46 | 46 | |
|
47 | 47 | return m_chart->createSeries(type); |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | void QChartView::zoomInToRect(const QRect& rectangle) |
|
51 | 51 | { |
|
52 | 52 | m_chart->zoomInToRect(rectangle); |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | void QChartView::zoomIn() |
|
56 | 56 | { |
|
57 | 57 | m_chart->zoomIn(); |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | void QChartView::zoomOut() |
|
61 | 61 | { |
|
62 | 62 | m_chart->zoomOut(); |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | int QChartView::margin() const |
|
66 | 66 | { |
|
67 | 67 | return m_chart->margin(); |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | void QChartView::setTitle(const QString& title) | |
|
70 | void QChartView::setChartTitle(const QString& title) | |
|
71 | 71 | { |
|
72 | m_chart->setTitle(title); | |
|
72 | m_chart->setChartTitle(title); | |
|
73 | } | |
|
74 | ||
|
75 | void QChartView::setChartTitleFont(const QFont& font) | |
|
76 | { | |
|
77 | m_chart->setChartTitleFont(font); | |
|
73 | 78 | } |
|
74 | 79 | |
|
75 | 80 | void QChartView::setChartBackgroundBrush(const QBrush& brush) |
|
76 | 81 | { |
|
77 | 82 | m_chart->setChartBackgroundBrush(brush); |
|
78 | 83 | } |
|
79 | 84 | void QChartView::setChartBackgroundPen(const QPen& pen) |
|
80 | 85 | { |
|
81 | 86 | m_chart->setChartBackgroundPen(pen); |
|
82 | 87 | } |
|
83 | 88 | |
|
84 | 89 | void QChartView::setRubberBandPolicy(const RubberBandPolicy policy) |
|
85 | 90 | { |
|
86 | 91 | switch(policy) { |
|
87 | 92 | case VerticalRubberBand: |
|
88 | 93 | m_verticalRubberBand = true; |
|
89 | 94 | m_horizonalRubberBand = false; |
|
90 | 95 | break; |
|
91 | 96 | case HorizonalRubberBand: |
|
92 | 97 | m_verticalRubberBand = false; |
|
93 | 98 | m_horizonalRubberBand = true; |
|
94 | 99 | break; |
|
95 | 100 | case RectangleRubberBand: |
|
96 | 101 | m_verticalRubberBand = true; |
|
97 | 102 | m_horizonalRubberBand = true; |
|
98 | 103 | break; |
|
99 | 104 | case NoRubberBand: |
|
100 | 105 | default: |
|
101 | 106 | delete m_rubberBand; |
|
102 | 107 | m_rubberBand=0; |
|
103 | 108 | m_horizonalRubberBand = false; |
|
104 | 109 | m_verticalRubberBand = false; |
|
105 | 110 | return; |
|
106 | 111 | } |
|
107 | 112 | if(!m_rubberBand) { |
|
108 | 113 | m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); |
|
109 | 114 | m_rubberBand->setEnabled(true); |
|
110 | 115 | } |
|
111 | 116 | } |
|
112 | 117 | |
|
113 | 118 | QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const |
|
114 | 119 | { |
|
115 | 120 | if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand; |
|
116 | 121 | if(m_horizonalRubberBand) return HorizonalRubberBand; |
|
117 | 122 | if(m_verticalRubberBand) return VerticalRubberBand; |
|
118 | 123 | return NoRubberBand; |
|
119 | 124 | } |
|
120 | 125 | |
|
121 | 126 | void QChartView::mousePressEvent(QMouseEvent *event) |
|
122 | 127 | { |
|
123 | 128 | if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { |
|
124 | 129 | |
|
125 | 130 | int margin = m_chart->margin(); |
|
126 | 131 | QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); |
|
127 | 132 | |
|
128 | 133 | if (rect.contains(event->pos())) { |
|
129 | 134 | m_rubberBandOrigin = event->pos(); |
|
130 | 135 | m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize())); |
|
131 | 136 | m_rubberBand->show(); |
|
132 | 137 | event->accept(); |
|
133 | 138 | } |
|
134 | 139 | } |
|
135 | 140 | } |
|
136 | 141 | |
|
137 | 142 | void QChartView::mouseMoveEvent(QMouseEvent *event) |
|
138 | 143 | { |
|
139 | 144 | if(m_rubberBand && m_rubberBand->isVisible()) { |
|
140 | 145 | int margin = m_chart->margin(); |
|
141 | 146 | QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); |
|
142 | 147 | int width = event->pos().x() - m_rubberBandOrigin.x(); |
|
143 | 148 | int height = event->pos().y() - m_rubberBandOrigin.y(); |
|
144 | 149 | if(!m_verticalRubberBand) { |
|
145 | 150 | m_rubberBandOrigin.setY(rect.top()); |
|
146 | 151 | height = rect.height(); |
|
147 | 152 | } |
|
148 | 153 | if(!m_horizonalRubberBand) { |
|
149 | 154 | m_rubberBandOrigin.setX(rect.left()); |
|
150 | 155 | width= rect.width(); |
|
151 | 156 | } |
|
152 | 157 | m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized()); |
|
153 | 158 | } |
|
154 | 159 | else { |
|
155 | 160 | QGraphicsView::mouseMoveEvent(event); |
|
156 | 161 | } |
|
157 | 162 | } |
|
158 | 163 | |
|
159 | 164 | void QChartView::mouseReleaseEvent(QMouseEvent *event) |
|
160 | 165 | { |
|
161 | 166 | if(m_rubberBand) { |
|
162 | 167 | if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { |
|
163 | 168 | m_rubberBand->hide(); |
|
164 | 169 | QRect rect = m_rubberBand->geometry(); |
|
165 | 170 | m_chart->zoomInToRect(rect); |
|
166 | 171 | event->accept(); |
|
167 | 172 | } |
|
168 | 173 | |
|
169 | 174 | if(event->button()==Qt::RightButton) |
|
170 | 175 | m_chart->zoomReset(); |
|
171 | 176 | } |
|
172 | 177 | else { |
|
173 | 178 | QGraphicsView::mouseReleaseEvent(event); |
|
174 | 179 | } |
|
175 | 180 | } |
|
176 | 181 | |
|
177 | 182 | void QChartView::keyPressEvent(QKeyEvent *event) |
|
178 | 183 | { |
|
179 | 184 | switch (event->key()) { |
|
180 | 185 | case Qt::Key_Plus: |
|
181 | 186 | zoomIn(); |
|
182 | 187 | break; |
|
183 | 188 | case Qt::Key_Minus: |
|
184 | 189 | zoomOut(); |
|
185 | 190 | break; |
|
186 | 191 | default: |
|
187 | 192 | QGraphicsView::keyPressEvent(event); |
|
188 | 193 | break; |
|
189 | 194 | } |
|
190 | 195 | } |
|
191 | 196 | |
|
192 | 197 | void QChartView::setChartTheme(QChart::ChartTheme theme) |
|
193 | 198 | { |
|
194 | 199 | m_chart->setChartTheme(theme); |
|
195 | 200 | } |
|
196 | 201 | |
|
197 | 202 | QChart::ChartTheme QChartView::chartTheme() const |
|
198 | 203 | { |
|
199 | 204 | return m_chart->chartTheme(); |
|
200 | 205 | } |
|
201 | 206 | |
|
202 | 207 | void QChartView::setDefaultAxisX(const QChartAxis& axis) |
|
203 | 208 | { |
|
204 | 209 | m_chart->setDefaultAxisX(axis); |
|
205 | 210 | } |
|
206 | 211 | |
|
207 | 212 | void QChartView::setDefaultAxisY(const QChartAxis& axis) |
|
208 | 213 | { |
|
209 | 214 | m_chart->setDefaultAxisY(axis); |
|
210 | 215 | } |
|
211 | 216 | |
|
212 | 217 | QChartAxis QChartView::defaultAxisX() const |
|
213 | 218 | { |
|
214 | 219 | return m_chart->defaultAxisX(); |
|
215 | 220 | } |
|
216 | 221 | |
|
217 | 222 | QChartAxis QChartView::defaultAxisY() const |
|
218 | 223 | { |
|
219 | 224 | return m_chart->defaultAxisY(); |
|
220 | 225 | } |
|
221 | 226 | |
|
222 | 227 | int QChartView::addAxisY(const QChartAxis& axis) |
|
223 | 228 | { |
|
224 | 229 | return m_chart->addAxisY(axis); |
|
225 | 230 | } |
|
226 | 231 | |
|
227 | 232 | QChartAxis QChartView::axisY(int id) const |
|
228 | 233 | { |
|
229 | 234 | return m_chart->axisY(id); |
|
230 | 235 | } |
|
231 | 236 | |
|
232 | 237 | void QChartView::removeAxisY(int id) |
|
233 | 238 | { |
|
234 | 239 | m_chart->removeAxisY(id); |
|
235 | 240 | } |
|
236 | 241 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,77 +1,78 | |||
|
1 | 1 | #ifndef QCHARTWIDGET_H |
|
2 | 2 | #define QCHARTWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qchartseries.h" |
|
6 | 6 | #include "qchart.h" |
|
7 | 7 | #include <QGraphicsView> |
|
8 | 8 | |
|
9 | 9 | class QGraphicsScene; |
|
10 | 10 | class QRubberBand; |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | class QChart; |
|
15 | 15 | |
|
16 | 16 | class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView |
|
17 | 17 | { |
|
18 | 18 | public: |
|
19 | 19 | enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand }; |
|
20 | 20 | |
|
21 | 21 | explicit QChartView(QWidget *parent = 0); |
|
22 | 22 | ~QChartView(); |
|
23 | 23 | |
|
24 | 24 | //implement from QWidget |
|
25 | 25 | void resizeEvent(QResizeEvent *event); |
|
26 | 26 | |
|
27 | 27 | void addSeries(QChartSeries* series); |
|
28 | 28 | |
|
29 | 29 | // Convenience function |
|
30 | 30 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
31 | 31 | |
|
32 | 32 | int margin() const; |
|
33 | 33 | |
|
34 | void setTitle(const QString& title); | |
|
34 | void setChartTitle(const QString& title); | |
|
35 | void setChartTitleFont(const QFont& font); | |
|
35 | 36 | void setChartBackgroundBrush(const QBrush& brush); |
|
36 | 37 | void setChartBackgroundPen(const QPen& pen); |
|
37 | 38 | |
|
38 | 39 | void zoomInToRect(const QRect& rectangle); |
|
39 | 40 | void zoomIn(); |
|
40 | 41 | void zoomOut(); |
|
41 | 42 | |
|
42 | 43 | void setRubberBandPolicy(const RubberBandPolicy ); |
|
43 | 44 | RubberBandPolicy rubberBandPolicy() const; |
|
44 | 45 | |
|
45 | 46 | void setChartTheme(QChart::ChartTheme theme); |
|
46 | 47 | QChart::ChartTheme chartTheme() const; |
|
47 | 48 | |
|
48 | 49 | void setDefaultAxisX(const QChartAxis& axis); |
|
49 | 50 | void setDefaultAxisY(const QChartAxis& axis); |
|
50 | 51 | QChartAxis defaultAxisX() const; |
|
51 | 52 | QChartAxis defaultAxisY() const; |
|
52 | 53 | QChartAxis axisY(int id) const; |
|
53 | 54 | int addAxisY(const QChartAxis& axis); |
|
54 | 55 | void removeAxisY(int id); |
|
55 | 56 | |
|
56 | 57 | protected: |
|
57 | 58 | void mousePressEvent(QMouseEvent *event); |
|
58 | 59 | void mouseMoveEvent(QMouseEvent *event); |
|
59 | 60 | void mouseReleaseEvent(QMouseEvent *event); |
|
60 | 61 | void keyPressEvent(QKeyEvent *event); |
|
61 | 62 | |
|
62 | 63 | |
|
63 | 64 | private: |
|
64 | 65 | QGraphicsScene *m_scene; |
|
65 | 66 | QChart* m_chart; |
|
66 | 67 | QPoint m_rubberBandOrigin; |
|
67 | 68 | QRubberBand* m_rubberBand; |
|
68 | 69 | bool m_verticalRubberBand; |
|
69 | 70 | bool m_horizonalRubberBand; |
|
70 | 71 | Q_DISABLE_COPY(QChartView) |
|
71 | 72 | |
|
72 | 73 | |
|
73 | 74 | }; |
|
74 | 75 | |
|
75 | 76 | QTCOMMERCIALCHART_END_NAMESPACE |
|
76 | 77 | |
|
77 | 78 | #endif // QCHARTWIDGET_H |
General Comments 0
You need to be logged in to leave comments.
Login now