@@ -1,51 +1,51 | |||
|
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 |
QLineChartSeries* series0 = QLineChartSeries |
|
|
18 | QLineChartSeries* series0 = new QLineChartSeries(); | |
|
19 | 19 | QPen blue(Qt::blue); |
|
20 | 20 | blue.setWidth(3); |
|
21 | 21 | series0->setPen(blue); |
|
22 |
QLineChartSeries* series1 = QLineChartSeries |
|
|
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 | 36 | chartView->setTitle("Custom color line chart example"); |
|
37 | 37 | chartView->addSeries(series0); |
|
38 | 38 | chartView->addSeries(series1); |
|
39 | 39 | |
|
40 | 40 | QLinearGradient backgroundGradient; |
|
41 | 41 | backgroundGradient.setColorAt(0.0, Qt::blue); |
|
42 | 42 | backgroundGradient.setColorAt(1.0, Qt::yellow); |
|
43 | 43 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
44 | 44 | chartView->setChartBackgroundBrush(backgroundGradient); |
|
45 | 45 | |
|
46 | 46 | window.setCentralWidget(chartView); |
|
47 | 47 | window.resize(400, 300); |
|
48 | 48 | window.show(); |
|
49 | 49 | |
|
50 | 50 | return a.exec(); |
|
51 | 51 | } |
@@ -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 |
QLineChartSeries* series0 = QLineChartSeries |
|
|
16 | QLineChartSeries* series0 = new QLineChartSeries(); | |
|
17 | 17 | QPen blue(Qt::blue); |
|
18 | 18 | blue.setWidth(3); |
|
19 | 19 | series0->setPen(blue); |
|
20 |
QLineChartSeries* series1 = QLineChartSeries |
|
|
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 | 31 | chartView->setTitle("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 |
QLineChartSeries* series0 = QLineChartSeries |
|
|
18 | QLineChartSeries* series0 = new QLineChartSeries(); | |
|
19 | 19 | QPen blue(Qt::blue); |
|
20 | 20 | blue.setWidth(3); |
|
21 | 21 | series0->setPen(blue); |
|
22 |
QLineChartSeries* series1 = QLineChartSeries |
|
|
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 | 37 | chartView->setTitle("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 |
QLineChartSeries* series0 = QLineChartSeries |
|
|
17 | QLineChartSeries* series0 = new QLineChartSeries(); | |
|
18 | 18 | QPen blue(Qt::blue); |
|
19 | 19 | blue.setWidth(3); |
|
20 | 20 | series0->setPen(blue); |
|
21 |
QLineChartSeries* series1 = QLineChartSeries |
|
|
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 | 35 | chartWidget->setTitle("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,292 +1,272 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include "chartdataset_p.h" |
|
5 | 5 | #include "charttheme_p.h" |
|
6 | 6 | //series |
|
7 | 7 | #include "barchartseries.h" |
|
8 | 8 | #include "stackedbarchartseries.h" |
|
9 | 9 | #include "percentbarchartseries.h" |
|
10 | 10 | #include "qlinechartseries.h" |
|
11 | 11 | #include "qpieseries.h" |
|
12 | 12 | //items |
|
13 | 13 | #include "axisitem_p.h" |
|
14 | 14 | #include "bargroup.h" |
|
15 | 15 | #include "stackedbargroup.h" |
|
16 | 16 | #include "linechartitem_p.h" |
|
17 | 17 | #include "percentbargroup.h" |
|
18 | 18 | #include "linechartanimationitem_p.h" |
|
19 | 19 | #include "piepresenter.h" |
|
20 | 20 | |
|
21 | 21 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
22 | 22 | |
|
23 | 23 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
24 | 24 | m_chart(chart), |
|
25 | 25 | m_dataset(dataset), |
|
26 | 26 | m_chartTheme(0), |
|
27 | 27 | m_domainIndex(0), |
|
28 | 28 | m_marginSize(0), |
|
29 | 29 | m_axisX(new QChartAxis(this)), |
|
30 | 30 | m_axisY(new QChartAxis(this)), |
|
31 | 31 | m_rect(QRectF(QPoint(0,0),m_chart->size())) |
|
32 | 32 | { |
|
33 | 33 | setChartTheme(QChart::ChartThemeDefault); |
|
34 | 34 | m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart); |
|
35 | 35 | m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart); |
|
36 | 36 | createConnections(); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | ChartPresenter::~ChartPresenter() |
|
40 | 40 | { |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | void ChartPresenter::createConnections() |
|
44 | 44 | { |
|
45 | 45 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); |
|
46 | 46 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); |
|
47 | 47 | |
|
48 | 48 | QMapIterator<QChartAxis*,AxisItem*> i(m_axisItems); |
|
49 | 49 | |
|
50 | 50 | while (i.hasNext()) { |
|
51 | 51 | i.next(); |
|
52 | 52 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&))); |
|
53 | 53 | QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&))); |
|
54 | 54 | } |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | void ChartPresenter::handleGeometryChanged() |
|
58 | 58 | { |
|
59 | 59 | m_rect = QRectF(QPoint(0,0),m_chart->size()); |
|
60 | 60 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); |
|
61 | 61 | Q_ASSERT(m_rect.isValid()); |
|
62 | 62 | emit geometryChanged(m_rect); |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | int ChartPresenter::margin() const |
|
66 | 66 | { |
|
67 | 67 | return m_marginSize; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | void ChartPresenter::setMargin(int margin) |
|
71 | 71 | { |
|
72 | 72 | m_marginSize = margin; |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | void ChartPresenter::handleSeriesAdded(QChartSeries* series) |
|
76 | 76 | { |
|
77 | 77 | switch(series->type()) |
|
78 | 78 | { |
|
79 | 79 | case QChartSeries::SeriesTypeLine: { |
|
80 | 80 | QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series); |
|
81 | 81 | LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart); |
|
82 | 82 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); |
|
83 | 83 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
84 | 84 | QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); |
|
85 | 85 | QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
86 | 86 | m_chartItems.insert(series,item); |
|
87 | 87 | break; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | case QChartSeries::SeriesTypeBar: { |
|
91 | 91 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); |
|
92 | 92 | BarGroup* item = new BarGroup(*barSeries,m_chart); |
|
93 | 93 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); |
|
94 | 94 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
95 | 95 | QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); |
|
96 | 96 | QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
97 | 97 | m_chartItems.insert(series,item); |
|
98 | 98 | // m_axisXItem->setVisible(false); |
|
99 | 99 | break; |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | case QChartSeries::SeriesTypeStackedBar: { |
|
103 | 103 | |
|
104 | 104 | StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); |
|
105 | 105 | StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart); |
|
106 | 106 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); |
|
107 | 107 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
108 | 108 | QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); |
|
109 | 109 | QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
110 | 110 | m_chartItems.insert(series,item); |
|
111 | 111 | break; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | case QChartSeries::SeriesTypePercentBar: { |
|
115 | 115 | |
|
116 | 116 | PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); |
|
117 | 117 | PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart); |
|
118 | 118 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); |
|
119 | 119 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
120 | 120 | QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); |
|
121 | 121 | QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
122 | 122 | m_chartItems.insert(series,item); |
|
123 | 123 | break; |
|
124 | 124 | } |
|
125 | 125 | /* |
|
126 | 126 | case QChartSeries::SeriesTypeScatter: { |
|
127 | 127 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
128 | 128 | scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); |
|
129 | 129 | scatterSeries->d->setParentItem(this); |
|
130 | 130 | scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
131 | 131 | m_chartItems << scatterSeries->d; |
|
132 | 132 | m_chartTheme->addObserver(scatterSeries->d); |
|
133 | 133 | |
|
134 | 134 | foreach (qreal x, scatterSeries->d->m_x) { |
|
135 | 135 | domain.m_minX = qMin(domain.m_minX, x); |
|
136 | 136 | domain.m_maxX = qMax(domain.m_maxX, x); |
|
137 | 137 | } |
|
138 | 138 | foreach (qreal y, scatterSeries->d->m_y) { |
|
139 | 139 | domain.m_minY = qMin(domain.m_minY, y); |
|
140 | 140 | domain.m_maxY = qMax(domain.m_maxY, y); |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | break; |
|
144 | 144 | } |
|
145 | 145 | */ |
|
146 | 146 | |
|
147 | 147 | case QChartSeries::SeriesTypePie: { |
|
148 | 148 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
149 | 149 | PiePresenter* pie = new PiePresenter(m_chart, pieSeries); |
|
150 | 150 | pieSeries->m_piePresenter = pie; // TODO: remove this pointer passing use signals&slots |
|
151 | 151 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); |
|
152 | 152 | QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&))); |
|
153 | 153 | m_chartItems.insert(series, pie); |
|
154 | 154 | break; |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | default: { |
|
158 | 158 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
159 | 159 | break; |
|
160 | 160 | } |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | if(m_rect.isValid()) emit geometryChanged(m_rect); |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | void ChartPresenter::handleSeriesChanged(QChartSeries* series) |
|
167 | 167 | { |
|
168 | 168 | //TODO: |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | void ChartPresenter::zoomInToRect(const QRectF& rect) |
|
172 | 172 | { |
|
173 | 173 | if(!rect.isValid()) return; |
|
174 | 174 | QRectF r = rect.normalized(); |
|
175 | 175 | r.translate(-m_marginSize, -m_marginSize); |
|
176 | 176 | Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height())); |
|
177 | 177 | m_dataset->addDomain(domain); |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | void ChartPresenter::zoomIn() |
|
181 | 181 | { |
|
182 | 182 | if (!m_dataset->nextDomain()) { |
|
183 | 183 | QRectF rect = m_rect; |
|
184 | 184 | rect.setWidth(rect.width()/2); |
|
185 | 185 | rect.setHeight(rect.height()/2); |
|
186 | 186 | rect.moveCenter(m_rect.center()); |
|
187 | 187 | Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height())); |
|
188 | 188 | m_dataset->addDomain(domain); |
|
189 | 189 | } |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | void ChartPresenter::zoomOut() |
|
193 | 193 | { |
|
194 | 194 | m_dataset->previousDomain(); |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | void ChartPresenter::zoomReset() |
|
198 | 198 | { |
|
199 | 199 | m_dataset->clearDomains(); |
|
200 | 200 | } |
|
201 | 201 | |
|
202 | 202 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) |
|
203 | 203 | { |
|
204 | 204 | delete m_chartTheme; |
|
205 | 205 | |
|
206 | 206 | m_chartTheme = ChartTheme::createTheme(theme); |
|
207 | 207 | |
|
208 | 208 | m_chartTheme->decorate(m_chart); |
|
209 | 209 | QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems); |
|
210 | 210 | |
|
211 | 211 | int index=0; |
|
212 | 212 | while (i.hasNext()) { |
|
213 | 213 | i.next(); |
|
214 | 214 | index++; |
|
215 | 215 | m_chartTheme->decorate(i.value(),i.key(),index); |
|
216 | 216 | } |
|
217 | 217 | } |
|
218 | 218 | |
|
219 | 219 | |
|
220 | 220 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
221 | 221 | { |
|
222 | 222 | return m_chartTheme->id(); |
|
223 | 223 | } |
|
224 | 224 | |
|
225 | 225 | QChartAxis* ChartPresenter::axisX() |
|
226 | 226 | { |
|
227 | 227 | return m_axisX; |
|
228 | 228 | } |
|
229 | 229 | |
|
230 | 230 | QChartAxis* ChartPresenter::axisY() |
|
231 | 231 | { |
|
232 | 232 | return m_axisY; |
|
233 | 233 | } |
|
234 | 234 | |
|
235 | 235 | QChartAxis* ChartPresenter::addAxisX() |
|
236 | 236 | { |
|
237 | 237 | //only one axis |
|
238 | 238 | if(m_axisX==0){ |
|
239 | 239 | m_axisX = new QChartAxis(this); |
|
240 | 240 | m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart); |
|
241 | 241 | } |
|
242 | 242 | return m_axisX; |
|
243 | 243 | } |
|
244 | 244 | |
|
245 | 245 | QChartAxis* ChartPresenter::addAxisY() |
|
246 | 246 | { |
|
247 | 247 | if(m_axisY==0){ |
|
248 | 248 | m_axisY = new QChartAxis(this); |
|
249 | 249 | m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart); |
|
250 | 250 | return m_axisY; |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | QChartAxis* axis = new QChartAxis(this); |
|
254 | 254 | m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart); |
|
255 | 255 | return axis; |
|
256 | 256 | } |
|
257 | 257 | |
|
258 | 258 | void ChartPresenter::removeAxis(QChartAxis* axis) |
|
259 | 259 | { |
|
260 | 260 | AxisItem* item = m_axisItems.take(axis); |
|
261 | 261 | if(item){ |
|
262 | 262 | delete item; |
|
263 | 263 | delete axis; |
|
264 | 264 | } |
|
265 | 265 | //reset pointers to default ones |
|
266 | 266 | if(axis == m_axisX) m_axisX=0; |
|
267 | 267 | else if(axis == m_axisY) m_axisY=0; |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | /* | |
|
271 | void ChartPresenter::setAxisX(const QChartAxis& axis) | |
|
272 | { | |
|
273 | setAxis(m_axisXItem,axis); | |
|
274 | } | |
|
275 | void ChartPresenter::setAxisY(const QChartAxis& axis) | |
|
276 | { | |
|
277 | setAxis(m_axisYItem.at(0),axis); | |
|
278 | } | |
|
279 | ||
|
280 | void ChartPresenter::setAxisY(const QList<QChartAxis>& axis) | |
|
281 | { | |
|
282 | //TODO not implemented | |
|
283 | } | |
|
284 | ||
|
285 | void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis) | |
|
286 | { | |
|
287 | item->setVisible(axis.isAxisVisible()); | |
|
288 | } | |
|
289 | */ | |
|
290 | 270 | #include "moc_chartpresenter_p.cpp" |
|
291 | 271 | |
|
292 | 272 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,77 +1,70 | |||
|
1 | 1 | #include "qlinechartseries.h" |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | QLineChartSeries::QLineChartSeries(QObject* parent):QChartSeries(parent) |
|
6 | 6 | { |
|
7 | 7 | } |
|
8 | 8 | |
|
9 | 9 | QLineChartSeries::~QLineChartSeries() |
|
10 | 10 | { |
|
11 | 11 | } |
|
12 | 12 | |
|
13 | QLineChartSeries* QLineChartSeries::create(QObject* parent) | |
|
14 | { | |
|
15 | //TODO: here we take QChartData when it is ready | |
|
16 | // return null if malformed; | |
|
17 | return new QLineChartSeries(parent); | |
|
18 | } | |
|
19 | ||
|
20 | 13 | int QLineChartSeries::add(qreal x,qreal y) |
|
21 | 14 | { |
|
22 | 15 | m_x<<x; |
|
23 | 16 | m_y<<y; |
|
24 | 17 | return m_x.size()-1; |
|
25 | 18 | } |
|
26 | 19 | |
|
27 | 20 | void QLineChartSeries::set(int index,qreal x,qreal y) |
|
28 | 21 | { |
|
29 | 22 | m_x[index]=x; |
|
30 | 23 | m_y[index]=y; |
|
31 | 24 | emit changed(index); |
|
32 | 25 | } |
|
33 | 26 | |
|
34 | 27 | void QLineChartSeries::clear() |
|
35 | 28 | { |
|
36 | 29 | m_x.clear(); |
|
37 | 30 | m_y.clear(); |
|
38 | 31 | } |
|
39 | 32 | |
|
40 | 33 | qreal QLineChartSeries::x(int pos) const |
|
41 | 34 | { |
|
42 | 35 | return m_x.at(pos); |
|
43 | 36 | } |
|
44 | 37 | |
|
45 | 38 | qreal QLineChartSeries::y(int pos) const |
|
46 | 39 | { |
|
47 | 40 | return m_y.at(pos); |
|
48 | 41 | } |
|
49 | 42 | |
|
50 | 43 | int QLineChartSeries::count() const |
|
51 | 44 | { |
|
52 | 45 | Q_ASSERT(m_x.size() == m_y.size()); |
|
53 | 46 | |
|
54 | 47 | return m_x.size(); |
|
55 | 48 | |
|
56 | 49 | } |
|
57 | 50 | |
|
58 | 51 | void QLineChartSeries::setPen(const QPen& pen) |
|
59 | 52 | { |
|
60 | 53 | m_pen=pen; |
|
61 | 54 | } |
|
62 | 55 | |
|
63 | 56 | QDebug operator<< (QDebug debug, const QLineChartSeries series) |
|
64 | 57 | { |
|
65 | 58 | Q_ASSERT(series.m_x.size() == series.m_y.size()); |
|
66 | 59 | |
|
67 | 60 | int size = series.m_x.size(); |
|
68 | 61 | |
|
69 | 62 | for (int i=0;i<size;i++) { |
|
70 | 63 | debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") "; |
|
71 | 64 | } |
|
72 | 65 | return debug.space(); |
|
73 | 66 | } |
|
74 | 67 | |
|
75 | 68 | #include "moc_qlinechartseries.cpp" |
|
76 | 69 | |
|
77 | 70 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,48 +1,46 | |||
|
1 | 1 | #ifndef QLINECHARTSERIES_H_ |
|
2 | 2 | #define QLINECHARTSERIES_H_ |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qchartseries.h" |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | #include <QPen> |
|
8 | 8 | #include <QBrush> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class QTCOMMERCIALCHART_EXPORT QLineChartSeries : public QChartSeries |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | private: | |
|
16 | QLineChartSeries(QObject* parent=0); | |
|
17 | 15 | public: |
|
16 | QLineChartSeries(QObject* parent=0); | |
|
18 | 17 | virtual ~QLineChartSeries(); |
|
19 | 18 | |
|
20 | 19 | public: // from QChartSeries |
|
21 | static QLineChartSeries* create(QObject* parent=0); | |
|
22 | 20 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;} |
|
23 | 21 | |
|
24 | 22 | public: |
|
25 | 23 | int add(qreal x, qreal y); |
|
26 | 24 | void set(int index,qreal x,qreal y); |
|
27 | 25 | void clear(); |
|
28 | 26 | |
|
29 | 27 | void setPen(const QPen& pen); |
|
30 | 28 | const QPen& pen() const { return m_pen;} |
|
31 | 29 | |
|
32 | 30 | int count() const; |
|
33 | 31 | qreal x(int pos) const; |
|
34 | 32 | qreal y(int pos) const; |
|
35 | 33 | friend QDebug operator<< (QDebug d, const QLineChartSeries series); |
|
36 | 34 | |
|
37 | 35 | signals: |
|
38 | 36 | void changed(int index); |
|
39 | 37 | |
|
40 | 38 | private: |
|
41 | 39 | QVector<qreal> m_x; |
|
42 | 40 | QVector<qreal> m_y; |
|
43 | 41 | QPen m_pen; |
|
44 | 42 | }; |
|
45 | 43 | |
|
46 | 44 | QTCOMMERCIALCHART_END_NAMESPACE |
|
47 | 45 | |
|
48 | 46 | #endif |
@@ -1,196 +1,196 | |||
|
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 | // TODO: support also other types; not only scatter and pie |
|
40 | 40 | |
|
41 | 41 | QChartSeries *series(0); |
|
42 | 42 | |
|
43 | 43 | switch (type) { |
|
44 | 44 | case QChartSeries::SeriesTypeLine: { |
|
45 |
series = QLineChartSeries |
|
|
45 | series = new QLineChartSeries(this); | |
|
46 | 46 | break; |
|
47 | 47 | } |
|
48 | 48 | case QChartSeries::SeriesTypeBar: { |
|
49 | 49 | series = new BarChartSeries(this); |
|
50 | 50 | break; |
|
51 | 51 | } |
|
52 | 52 | case QChartSeries::SeriesTypeStackedBar: { |
|
53 | 53 | series = new StackedBarChartSeries(this); |
|
54 | 54 | break; |
|
55 | 55 | } |
|
56 | 56 | case QChartSeries::SeriesTypePercentBar: { |
|
57 | 57 | series = new PercentBarChartSeries(this); |
|
58 | 58 | break; |
|
59 | 59 | } |
|
60 | 60 | case QChartSeries::SeriesTypeScatter: { |
|
61 | 61 | series = new QScatterSeries(this); |
|
62 | 62 | break; |
|
63 | 63 | } |
|
64 | 64 | case QChartSeries::SeriesTypePie: { |
|
65 | 65 | series = new QPieSeries(this); |
|
66 | 66 | break; |
|
67 | 67 | } |
|
68 | 68 | default: |
|
69 | 69 | Q_ASSERT(false); |
|
70 | 70 | break; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | addSeries(series); |
|
74 | 74 | return series; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
78 | 78 | { |
|
79 | 79 | |
|
80 | 80 | if(!m_backgroundItem) { |
|
81 | 81 | m_backgroundItem = new QGraphicsRectItem(this); |
|
82 | 82 | m_backgroundItem->setZValue(-1); |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | m_backgroundItem->setBrush(brush); |
|
86 | 86 | m_backgroundItem->update(); |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
90 | 90 | { |
|
91 | 91 | |
|
92 | 92 | if(!m_backgroundItem) { |
|
93 | 93 | m_backgroundItem = new QGraphicsRectItem(this); |
|
94 | 94 | m_backgroundItem->setZValue(-1); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | m_backgroundItem->setPen(pen); |
|
98 | 98 | m_backgroundItem->update(); |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | void QChart::setTitle(const QString& title,const QFont& font) |
|
102 | 102 | { |
|
103 | 103 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); |
|
104 | 104 | m_titleItem->setPlainText(title); |
|
105 | 105 | m_titleItem->setFont(font); |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | int QChart::margin() const |
|
109 | 109 | { |
|
110 | 110 | return m_presenter->margin(); |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | void QChart::setMargin(int margin) |
|
114 | 114 | { |
|
115 | 115 | m_presenter->setMargin(margin); |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
119 | 119 | { |
|
120 | 120 | m_presenter->setChartTheme(theme); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | QChart::ChartTheme QChart::chartTheme() const |
|
124 | 124 | { |
|
125 | 125 | return m_presenter->chartTheme(); |
|
126 | 126 | } |
|
127 | 127 | |
|
128 | 128 | void QChart::zoomInToRect(const QRectF& rectangle) |
|
129 | 129 | { |
|
130 | 130 | m_presenter->zoomInToRect(rectangle); |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | void QChart::zoomIn() |
|
134 | 134 | { |
|
135 | 135 | m_presenter->zoomIn(); |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | void QChart::zoomOut() |
|
139 | 139 | { |
|
140 | 140 | m_presenter->zoomOut(); |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | void QChart::zoomReset() |
|
144 | 144 | { |
|
145 | 145 | m_presenter->zoomReset(); |
|
146 | 146 | } |
|
147 | 147 | |
|
148 | 148 | QChartAxis* QChart::axisX() |
|
149 | 149 | { |
|
150 | 150 | return m_presenter->axisX(); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | QChartAxis* QChart::axisY() |
|
154 | 154 | { |
|
155 | 155 | return m_presenter->axisY(); |
|
156 | 156 | } |
|
157 | 157 | |
|
158 | 158 | QChartAxis* QChart::addAxisX() |
|
159 | 159 | { |
|
160 | 160 | return m_presenter->addAxisX(); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | QChartAxis* QChart::addAxisY() |
|
164 | 164 | { |
|
165 | 165 | return m_presenter->addAxisY(); |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | void QChart::removeAxis(QChartAxis* axis) |
|
169 | 169 | { |
|
170 | 170 | m_presenter->removeAxis(axis); |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
174 | 174 | { |
|
175 | 175 | |
|
176 | 176 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
177 | 177 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
178 | 178 | |
|
179 | 179 | // recalculate title position |
|
180 | 180 | if (m_titleItem) { |
|
181 | 181 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
182 | 182 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | //recalculate background gradient |
|
186 | 186 | if (m_backgroundItem) { |
|
187 | 187 | m_backgroundItem->setRect(rect); |
|
188 | 188 | } |
|
189 | 189 | |
|
190 | 190 | QGraphicsWidget::resizeEvent(event); |
|
191 | 191 | update(); |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | #include "moc_qchart.cpp" |
|
195 | 195 | |
|
196 | 196 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,34 +1,5 | |||
|
1 | #include "qchartglobal.h" | |
|
2 | 1 |
|
|
3 | 2 | |
|
4 | #include "barchartseries.h" | |
|
5 | #include "qlinechartseries.h" | |
|
6 | ||
|
7 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | ||
|
9 | QChartSeries* QChartSeries::create(QChartSeriesType type, QObject* parent) | |
|
10 | { | |
|
11 | qDebug() << "QChartSeries::create"; | |
|
12 | // TODO: Other types | |
|
13 | switch (type) { | |
|
14 | case QChartSeries::SeriesTypeLine: { | |
|
15 | QLineChartSeries* s = QLineChartSeries::create(parent); // TODO: do we need create method for derived implementations? | |
|
16 | return s; | |
|
17 | } | |
|
18 | case QChartSeries::SeriesTypePie: { | |
|
19 | return 0; | |
|
20 | } | |
|
21 | case QChartSeries::SeriesTypeScatter: { | |
|
22 | return 0; | |
|
23 | } | |
|
24 | case QChartSeries::SeriesTypeBar: { | |
|
25 | BarChartSeries* s = new BarChartSeries(parent); | |
|
26 | return s; | |
|
27 | } | |
|
28 | default: | |
|
29 | return 0; | |
|
30 | } | |
|
31 | } | |
|
32 | ||
|
33 | 4 | #include "moc_qchartseries.cpp" |
|
34 | 5 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,50 +1,47 | |||
|
1 | 1 | #ifndef QCHARTSERIES_H |
|
2 | 2 | #define QCHARTSERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include <QAbstractItemModel> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QTCOMMERCIALCHART_EXPORT QChartSeries : public QObject |
|
11 | 11 | { |
|
12 | 12 | Q_OBJECT |
|
13 | 13 | public: |
|
14 | 14 | enum QChartSeriesType { |
|
15 | 15 | SeriesTypeInvalid = -1, |
|
16 | 16 | SeriesTypeLine, |
|
17 | 17 | // SeriesTypeArea, |
|
18 | 18 | SeriesTypeBar, |
|
19 | 19 | SeriesTypeStackedBar, |
|
20 | 20 | SeriesTypePercentBar, |
|
21 | 21 | SeriesTypePie, |
|
22 | 22 | SeriesTypeScatter |
|
23 | 23 | // SeriesTypeSpline |
|
24 | 24 | }; |
|
25 | 25 | |
|
26 | 26 | protected: |
|
27 | 27 | QChartSeries(QObject *parent = 0):QObject(parent){}; |
|
28 | 28 | |
|
29 | 29 | public: |
|
30 | 30 | virtual ~QChartSeries(){}; |
|
31 | 31 | |
|
32 | // Factory method | |
|
33 | static QChartSeries* create(QChartSeriesType type, QObject* parent = 0 ); | |
|
34 | ||
|
35 | 32 | // Pure virtual |
|
36 | 33 | virtual QChartSeriesType type() const = 0; |
|
37 | 34 | |
|
38 | 35 | virtual bool setData(QList<int> /*data*/) { return false; } |
|
39 | 36 | virtual bool setData(QList<qreal> /*data*/) { return false; } |
|
40 | 37 | virtual bool setData(QList<qreal> /*x*/, QList<qreal> /*y*/){ return false; } |
|
41 | 38 | |
|
42 | 39 | // Prototype for data model. TODO: remove the other setData methods and use something like this for now? |
|
43 | 40 | virtual bool setData(QAbstractItemModel* /*model*/) { return false; } |
|
44 | 41 | |
|
45 | 42 | }; |
|
46 | 43 | |
|
47 | 44 | QTCOMMERCIALCHART_END_NAMESPACE |
|
48 | 45 | |
|
49 | 46 | #endif |
|
50 | 47 |
@@ -1,381 +1,381 | |||
|
1 | 1 | #include "mainwidget.h" |
|
2 | 2 | #include "dataseriedialog.h" |
|
3 | 3 | #include "qchartseries.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include <qlinechartseries.h> |
|
6 | 6 | #include <barchartseries.h> |
|
7 | 7 | #include <QPushButton> |
|
8 | 8 | #include <QComboBox> |
|
9 | 9 | #include <QSpinBox> |
|
10 | 10 | #include <QCheckBox> |
|
11 | 11 | #include <QGridLayout> |
|
12 | 12 | #include <QHBoxLayout> |
|
13 | 13 | #include <QLabel> |
|
14 | 14 | #include <QSpacerItem> |
|
15 | 15 | #include <QMessageBox> |
|
16 | 16 | #include <cmath> |
|
17 | 17 | #include <QDebug> |
|
18 | 18 | #include <QStandardItemModel> |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
22 | 22 | |
|
23 | 23 | MainWidget::MainWidget(QWidget *parent) : |
|
24 | 24 | QWidget(parent) |
|
25 | 25 | { |
|
26 | 26 | m_chartWidget = new QChartView(this); |
|
27 | 27 | m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand); |
|
28 | 28 | |
|
29 | 29 | // Grid layout for the controls for configuring the chart widget |
|
30 | 30 | QGridLayout *grid = new QGridLayout(); |
|
31 | 31 | QPushButton *addSeriesButton = new QPushButton("Add series"); |
|
32 | 32 | connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); |
|
33 | 33 | grid->addWidget(addSeriesButton, 0, 1); |
|
34 | 34 | initBackroundCombo(grid); |
|
35 | 35 | initScaleControls(grid); |
|
36 | 36 | initThemeCombo(grid); |
|
37 | 37 | QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom"); |
|
38 | 38 | connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool))); |
|
39 | 39 | zoomCheckBox->setChecked(true); |
|
40 | 40 | grid->addWidget(zoomCheckBox, grid->rowCount(), 0); |
|
41 | 41 | // add row with empty label to make all the other rows static |
|
42 | 42 | grid->addWidget(new QLabel(""), grid->rowCount(), 0); |
|
43 | 43 | grid->setRowStretch(grid->rowCount() - 1, 1); |
|
44 | 44 | |
|
45 | 45 | // Another grid layout as a main layout |
|
46 | 46 | QGridLayout *mainLayout = new QGridLayout(); |
|
47 | 47 | mainLayout->addLayout(grid, 0, 0); |
|
48 | 48 | |
|
49 | 49 | // Init series type specific controls |
|
50 | 50 | initPieControls(); |
|
51 | 51 | mainLayout->addLayout(m_pieLayout, 2, 0); |
|
52 | 52 | // Scatter series specific settings |
|
53 | 53 | // m_scatterLayout = new QGridLayout(); |
|
54 | 54 | // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0); |
|
55 | 55 | // m_scatterLayout->setEnabled(false); |
|
56 | 56 | // mainLayout->addLayout(m_scatterLayout, 1, 0); |
|
57 | 57 | |
|
58 | 58 | // Add layouts and the chart widget to the main layout |
|
59 | 59 | mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1); |
|
60 | 60 | setLayout(mainLayout); |
|
61 | 61 | |
|
62 | 62 | // force an update to test data |
|
63 | 63 | testDataChanged(0); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | // Combo box for selecting the chart's background |
|
67 | 67 | void MainWidget::initBackroundCombo(QGridLayout *grid) |
|
68 | 68 | { |
|
69 | 69 | QComboBox *backgroundCombo = new QComboBox(this); |
|
70 | 70 | backgroundCombo->addItem("Color"); |
|
71 | 71 | backgroundCombo->addItem("Gradient"); |
|
72 | 72 | backgroundCombo->addItem("Image"); |
|
73 | 73 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), |
|
74 | 74 | this, SLOT(backgroundChanged(int))); |
|
75 | 75 | |
|
76 | 76 | grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0); |
|
77 | 77 | grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | // Scale related controls (auto-scale vs. manual min-max values) |
|
81 | 81 | void MainWidget::initScaleControls(QGridLayout *grid) |
|
82 | 82 | { |
|
83 | 83 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); |
|
84 | 84 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
85 | 85 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
86 | 86 | m_xMinSpin = new QSpinBox(); |
|
87 | 87 | m_xMinSpin->setMinimum(INT_MIN); |
|
88 | 88 | m_xMinSpin->setMaximum(INT_MAX); |
|
89 | 89 | m_xMinSpin->setValue(0); |
|
90 | 90 | connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); |
|
91 | 91 | m_xMaxSpin = new QSpinBox(); |
|
92 | 92 | m_xMaxSpin->setMinimum(INT_MIN); |
|
93 | 93 | m_xMaxSpin->setMaximum(INT_MAX); |
|
94 | 94 | m_xMaxSpin->setValue(10); |
|
95 | 95 | connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); |
|
96 | 96 | m_yMinSpin = new QSpinBox(); |
|
97 | 97 | m_yMinSpin->setMinimum(INT_MIN); |
|
98 | 98 | m_yMinSpin->setMaximum(INT_MAX); |
|
99 | 99 | m_yMinSpin->setValue(0); |
|
100 | 100 | connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); |
|
101 | 101 | m_yMaxSpin = new QSpinBox(); |
|
102 | 102 | m_yMaxSpin->setMinimum(INT_MIN); |
|
103 | 103 | m_yMaxSpin->setMaximum(INT_MAX); |
|
104 | 104 | m_yMaxSpin->setValue(10); |
|
105 | 105 | connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); |
|
106 | 106 | |
|
107 | 107 | grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0); |
|
108 | 108 | grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0); |
|
109 | 109 | grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1); |
|
110 | 110 | grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0); |
|
111 | 111 | grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1); |
|
112 | 112 | grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0); |
|
113 | 113 | grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1); |
|
114 | 114 | grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0); |
|
115 | 115 | grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1); |
|
116 | 116 | |
|
117 | 117 | m_autoScaleCheck->setChecked(true); |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | // Combo box for selecting theme |
|
121 | 121 | void MainWidget::initThemeCombo(QGridLayout *grid) |
|
122 | 122 | { |
|
123 | 123 | QComboBox *chartTheme = new QComboBox(); |
|
124 | 124 | chartTheme->addItem("Default"); |
|
125 | 125 | chartTheme->addItem("Vanilla"); |
|
126 | 126 | chartTheme->addItem("Icy"); |
|
127 | 127 | chartTheme->addItem("Grayscale"); |
|
128 | 128 | chartTheme->addItem("Scientific"); |
|
129 | 129 | chartTheme->addItem("Unnamed1"); |
|
130 | 130 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), |
|
131 | 131 | this, SLOT(changeChartTheme(int))); |
|
132 | 132 | grid->addWidget(new QLabel("Chart theme:"), 8, 0); |
|
133 | 133 | grid->addWidget(chartTheme, 8, 1); |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | void MainWidget::initPieControls() |
|
137 | 137 | { |
|
138 | 138 | // Pie series specific settings |
|
139 | 139 | // Pie size factory |
|
140 | 140 | QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox(); |
|
141 | 141 | pieSizeSpin->setMinimum(LONG_MIN); |
|
142 | 142 | pieSizeSpin->setMaximum(LONG_MAX); |
|
143 | 143 | pieSizeSpin->setValue(1.0); |
|
144 | 144 | pieSizeSpin->setSingleStep(0.1); |
|
145 | 145 | connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double))); |
|
146 | 146 | // Pie position |
|
147 | 147 | QComboBox *piePosCombo = new QComboBox(this); |
|
148 | 148 | piePosCombo->addItem("Maximized"); |
|
149 | 149 | piePosCombo->addItem("Top left"); |
|
150 | 150 | piePosCombo->addItem("Top right"); |
|
151 | 151 | piePosCombo->addItem("Bottom left"); |
|
152 | 152 | piePosCombo->addItem("Bottom right"); |
|
153 | 153 | connect(piePosCombo, SIGNAL(currentIndexChanged(int)), |
|
154 | 154 | this, SLOT(setPiePosition(int))); |
|
155 | 155 | m_pieLayout = new QGridLayout(); |
|
156 | 156 | m_pieLayout->setEnabled(false); |
|
157 | 157 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); |
|
158 | 158 | m_pieLayout->addWidget(pieSizeSpin, 0, 1); |
|
159 | 159 | m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0); |
|
160 | 160 | m_pieLayout->addWidget(piePosCombo, 1, 1); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | void MainWidget::addSeries() |
|
164 | 164 | { |
|
165 | 165 | DataSerieDialog dialog(m_defaultSeriesName, this); |
|
166 | 166 | connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString))); |
|
167 | 167 | dialog.exec(); |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | void MainWidget::addSeries(QString series, QString data) |
|
171 | 171 | { |
|
172 | 172 | qDebug() << "addSeries: " << series << " data: " << data; |
|
173 | 173 | m_defaultSeriesName = series; |
|
174 | 174 | |
|
175 | 175 | // TODO: a dedicated data class for storing x and y values |
|
176 | 176 | QList<qreal> x; |
|
177 | 177 | QList<qreal> y; |
|
178 | 178 | |
|
179 | 179 | if (data == "linear") { |
|
180 | 180 | for (int i = 0; i < 20; i++) { |
|
181 | 181 | x.append(i); |
|
182 | 182 | y.append(i); |
|
183 | 183 | } |
|
184 | 184 | } else if (data == "linear, 1M") { |
|
185 | 185 | // 1 million data points from 0.0001 to 100 |
|
186 | 186 | // TODO: What is the requirement? Should we be able to show this kind of data with |
|
187 | 187 | // reasonable performance, or can we expect the application developer to do "data mining" |
|
188 | 188 | // for us, so that the count of data points given to QtCommercial Chart is always |
|
189 | 189 | // reasonable? |
|
190 | 190 | for (qreal i = 0; i < 100; i += 0.0001) { |
|
191 | 191 | x.append(i); |
|
192 | 192 | y.append(20); |
|
193 | 193 | } |
|
194 | 194 | } else if (data == "SIN") { |
|
195 | 195 | for (int i = 0; i < 100; i++) { |
|
196 | 196 | x.append(i); |
|
197 | 197 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100)); |
|
198 | 198 | } |
|
199 | 199 | } else if (data == "SIN + random") { |
|
200 | 200 | for (qreal i = 0; i < 100; i += 0.1) { |
|
201 | 201 | x.append(i + (rand() % 5)); |
|
202 | 202 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); |
|
203 | 203 | } |
|
204 | 204 | } else { |
|
205 | 205 | // TODO: check if data has a valid file name |
|
206 | 206 | Q_ASSERT(false); |
|
207 | 207 | } |
|
208 | 208 | |
|
209 | 209 | // TODO: color of the series |
|
210 | 210 | QChartSeries *newSeries = 0; |
|
211 | 211 | if (series == "Scatter") { |
|
212 | 212 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter); |
|
213 | 213 | Q_ASSERT(newSeries->setData(x, y)); |
|
214 | 214 | } else if (series == "Pie") { |
|
215 | 215 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); |
|
216 | 216 | Q_ASSERT(newSeries->setData(y)); |
|
217 | 217 | } else if (series == "Line") { |
|
218 | 218 | // TODO: adding data to an existing line series does not give any visuals for some reason |
|
219 | 219 | // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine); |
|
220 | 220 | // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries); |
|
221 | 221 | // lineSeries->setColor(Qt::blue); |
|
222 | 222 | // for (int i(0); i < x.count() && i < y.count(); i++) { |
|
223 | 223 | // lineSeries->add(x.at(i), y.at(i)); |
|
224 | 224 | // } |
|
225 | 225 | //Q_ASSERT(newSeries->setData(x, y)); |
|
226 |
QLineChartSeries* series0 = QLineChartSeries |
|
|
226 | QLineChartSeries* series0 = new QLineChartSeries(); | |
|
227 | 227 | for (int i(0); i < x.count() && i < y.count(); i++) |
|
228 | 228 | series0->add(x.at(i), y.at(i)); |
|
229 | 229 | m_chartWidget->addSeries(series0); |
|
230 | 230 | newSeries = series0; |
|
231 | 231 | } else { |
|
232 | 232 | // TODO |
|
233 | 233 | } |
|
234 | 234 | |
|
235 | 235 | // BarChart |
|
236 | 236 | if (series == "Bar") { |
|
237 | 237 | // This is the another way of creating series. Should we create test cases for both ways, if we support them? |
|
238 | 238 | qDebug() << "Bar chart series"; |
|
239 |
newSeries = |
|
|
239 | newSeries = new BarChartSeries(this); | |
|
240 | 240 | |
|
241 | 241 | // Create some test data to chart |
|
242 | 242 | QStandardItemModel dataModel(2,10,this); |
|
243 | 243 | QModelIndex index; |
|
244 | 244 | index = dataModel.index(0,0); |
|
245 | 245 | // Series 1, items 6 to 9 missing. |
|
246 | 246 | dataModel.setData(dataModel.index(0,0),1); |
|
247 | 247 | dataModel.setData(dataModel.index(0,1),12); |
|
248 | 248 | dataModel.setData(dataModel.index(0,2),5); |
|
249 | 249 | dataModel.setData(dataModel.index(0,3),8); |
|
250 | 250 | dataModel.setData(dataModel.index(0,4),17); |
|
251 | 251 | dataModel.setData(dataModel.index(0,5),9); |
|
252 | 252 | |
|
253 | 253 | // Series 2, some other items missing |
|
254 | 254 | dataModel.setData(dataModel.index(1,0),5); |
|
255 | 255 | dataModel.setData(dataModel.index(1,3),4); |
|
256 | 256 | dataModel.setData(dataModel.index(1,5),7); |
|
257 | 257 | dataModel.setData(dataModel.index(1,6),8); |
|
258 | 258 | dataModel.setData(dataModel.index(1,8),9); |
|
259 | 259 | dataModel.setData(dataModel.index(1,9),9); |
|
260 | 260 | |
|
261 | 261 | newSeries->setData(&dataModel); |
|
262 | 262 | |
|
263 | 263 | m_chartWidget->addSeries(newSeries); |
|
264 | 264 | } |
|
265 | 265 | |
|
266 | 266 | setCurrentSeries(newSeries); |
|
267 | 267 | } |
|
268 | 268 | |
|
269 | 269 | void MainWidget::setCurrentSeries(QChartSeries *series) |
|
270 | 270 | { |
|
271 | 271 | m_currentSeries = series; |
|
272 | 272 | switch (m_currentSeries->type()) { |
|
273 | 273 | case QChartSeries::SeriesTypeLine: |
|
274 | 274 | break; |
|
275 | 275 | case QChartSeries::SeriesTypeScatter: |
|
276 | 276 | break; |
|
277 | 277 | case QChartSeries::SeriesTypePie: |
|
278 | 278 | break; |
|
279 | 279 | case QChartSeries::SeriesTypeBar: |
|
280 | 280 | qDebug() << "setCurrentSeries (bar)"; |
|
281 | 281 | break; |
|
282 | 282 | default: |
|
283 | 283 | Q_ASSERT(false); |
|
284 | 284 | break; |
|
285 | 285 | } |
|
286 | 286 | } |
|
287 | 287 | |
|
288 | 288 | void MainWidget::testDataChanged(int itemIndex) |
|
289 | 289 | { |
|
290 | 290 | qDebug() << "testDataChanged: " << itemIndex; |
|
291 | 291 | |
|
292 | 292 | // switch (itemIndex) { |
|
293 | 293 | // case 0: { |
|
294 | 294 | // QList<QChartDataPoint> data; |
|
295 | 295 | // for (int x = 0; x < 20; x++) { |
|
296 | 296 | // data.append(QChartDataPoint() << x << x / 2); |
|
297 | 297 | // } |
|
298 | 298 | // m_chartWidget->setData(data); |
|
299 | 299 | // break; |
|
300 | 300 | // } |
|
301 | 301 | // case 1: { |
|
302 | 302 | // QList<QChartDataPoint> data; |
|
303 | 303 | // for (int x = 0; x < 100; x++) { |
|
304 | 304 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); |
|
305 | 305 | // } |
|
306 | 306 | // m_chartWidget->setData(data); |
|
307 | 307 | // break; |
|
308 | 308 | // } |
|
309 | 309 | // case 2: { |
|
310 | 310 | // QList<QChartDataPoint> data; |
|
311 | 311 | // for (int x = 0; x < 1000; x++) { |
|
312 | 312 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
313 | 313 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
314 | 314 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
315 | 315 | // } |
|
316 | 316 | // m_chartWidget->setData(data); |
|
317 | 317 | // break; |
|
318 | 318 | // } |
|
319 | 319 | // default: |
|
320 | 320 | // break; |
|
321 | 321 | // } |
|
322 | 322 | } |
|
323 | 323 | |
|
324 | 324 | void MainWidget::backgroundChanged(int itemIndex) |
|
325 | 325 | { |
|
326 | 326 | qDebug() << "backgroundChanged: " << itemIndex; |
|
327 | 327 | } |
|
328 | 328 | |
|
329 | 329 | void MainWidget::autoScaleChanged(int value) |
|
330 | 330 | { |
|
331 | 331 | if (value) { |
|
332 | 332 | // TODO: enable auto scaling |
|
333 | 333 | } else { |
|
334 | 334 | // TODO: set scaling manually (and disable auto scaling) |
|
335 | 335 | } |
|
336 | 336 | |
|
337 | 337 | m_xMinSpin->setEnabled(!value); |
|
338 | 338 | m_xMaxSpin->setEnabled(!value); |
|
339 | 339 | m_yMinSpin->setEnabled(!value); |
|
340 | 340 | m_yMaxSpin->setEnabled(!value); |
|
341 | 341 | } |
|
342 | 342 | |
|
343 | 343 | void MainWidget::xMinChanged(int value) |
|
344 | 344 | { |
|
345 | 345 | qDebug() << "xMinChanged: " << value; |
|
346 | 346 | } |
|
347 | 347 | |
|
348 | 348 | void MainWidget::xMaxChanged(int value) |
|
349 | 349 | { |
|
350 | 350 | qDebug() << "xMaxChanged: " << value; |
|
351 | 351 | } |
|
352 | 352 | |
|
353 | 353 | void MainWidget::yMinChanged(int value) |
|
354 | 354 | { |
|
355 | 355 | qDebug() << "yMinChanged: " << value; |
|
356 | 356 | } |
|
357 | 357 | |
|
358 | 358 | void MainWidget::yMaxChanged(int value) |
|
359 | 359 | { |
|
360 | 360 | qDebug() << "yMaxChanged: " << value; |
|
361 | 361 | } |
|
362 | 362 | |
|
363 | 363 | void MainWidget::changeChartTheme(int themeIndex) |
|
364 | 364 | { |
|
365 | 365 | qDebug() << "changeChartTheme: " << themeIndex; |
|
366 | 366 | m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex); |
|
367 | 367 | } |
|
368 | 368 | |
|
369 | 369 | void MainWidget::setPieSizeFactor(double size) |
|
370 | 370 | { |
|
371 | 371 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
372 | 372 | if (pie) |
|
373 | 373 | pie->setSizeFactor(qreal(size)); |
|
374 | 374 | } |
|
375 | 375 | |
|
376 | 376 | void MainWidget::setPiePosition(int position) |
|
377 | 377 | { |
|
378 | 378 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
379 | 379 | if (pie) |
|
380 | 380 | pie->setPosition((QPieSeries::PiePosition) position); |
|
381 | 381 | } |
General Comments 0
You need to be logged in to leave comments.
Login now