@@ -1,78 +1,78 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qlineseries.h> |
|
5 | 5 | #include <qchart.h> |
|
6 | 6 | #include <qchartaxis.h> |
|
7 | 7 | #include <qchartaxiscategories.h> |
|
8 | 8 | #include <cmath> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | #define PI 3.14159265358979 |
|
13 | 13 | |
|
14 | 14 | int main(int argc, char *argv[]) |
|
15 | 15 | { |
|
16 | 16 | QApplication a(argc, argv); |
|
17 | 17 | |
|
18 | 18 | QMainWindow window; |
|
19 | 19 | |
|
20 | 20 | QLineSeries* series0 = new QLineSeries(); |
|
21 | 21 | QPen blue(Qt::blue); |
|
22 | 22 | blue.setWidth(3); |
|
23 | 23 | series0->setPen(blue); |
|
24 | 24 | QLineSeries* series1 = new QLineSeries(); |
|
25 | 25 | QPen red(Qt::red); |
|
26 | 26 | red.setWidth(3); |
|
27 | 27 | series1->setPen(red); |
|
28 | 28 | |
|
29 | 29 | int numPoints = 100; |
|
30 | 30 | |
|
31 | 31 | for (int x = 0; x <= numPoints; ++x) { |
|
32 | 32 | series0->add(x, fabs(sin(PI/50*x)*100)); |
|
33 | 33 | series1->add(x, fabs(cos(PI/50*x)*100)); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | QChartView* chartView = new QChartView(&window); |
|
37 | 37 | |
|
38 | 38 | chartView->setRenderHint(QPainter::Antialiasing); |
|
39 | 39 | chartView->setChartTitle("This is custom axis chart example"); |
|
40 | 40 | chartView->addSeries(series0); |
|
41 | 41 | chartView->addSeries(series1); |
|
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* axisX = chartView->axisX(); |
|
50 | 50 | axisX->setLabelsAngle(45); |
|
51 | 51 | axisX->setGridPen(Qt::DashLine); |
|
52 | 52 | |
|
53 | 53 | QChartAxisCategories& categoriesX = axisX->categories(); |
|
54 | 54 | categoriesX.insert(0,"low"); |
|
55 | 55 | categoriesX.insert(50,"medium"); |
|
56 | 56 | categoriesX.insert(100,"High"); |
|
57 | 57 | |
|
58 | 58 | axisX->setMin(-10); |
|
59 | axisX->setMax(200); | |
|
59 | axisX->setMax(2200); | |
|
60 | 60 | |
|
61 | 61 | QChartAxis* axisY = chartView->axisY(); |
|
62 | 62 | axisY->setLabelsAngle(45); |
|
63 | 63 | axisY->setShadesBrush(Qt::yellow); |
|
64 | 64 | |
|
65 | 65 | QChartAxisCategories& categoriesY = axisY->categories(); |
|
66 | 66 | categoriesY.insert(0,"low"); |
|
67 | 67 | categoriesY.insert(50,"medium"); |
|
68 | 68 | categoriesY.insert(100,"High"); |
|
69 | 69 | |
|
70 | 70 | axisY->setMin(-10); |
|
71 | 71 | axisY->setMax(200); |
|
72 | 72 | |
|
73 | 73 | window.setCentralWidget(chartView); |
|
74 | 74 | window.resize(400, 300); |
|
75 | 75 | window.show(); |
|
76 | 76 | |
|
77 | 77 | return a.exec(); |
|
78 | 78 | } |
@@ -1,21 +1,21 | |||
|
1 | 1 | TEMPLATE = subdirs |
|
2 | 2 | SUBDIRS += linechart \ |
|
3 | 3 | zoomlinechart \ |
|
4 | 4 | colorlinechart \ |
|
5 | 5 | barchart \ |
|
6 | 6 | stackedbarchart \ |
|
7 | 7 | percentbarchart \ |
|
8 | 8 | scatter \ |
|
9 | 9 | piechart \ |
|
10 | 10 | piechartcustomization \ |
|
11 | 11 | piechartdrilldown \ |
|
12 | 12 | dynamiclinechart \ |
|
13 | 13 | axischart \ |
|
14 | 14 | multichart \ |
|
15 | 15 | gdpbarchart \ |
|
16 | 16 | presenterchart \ |
|
17 | 17 | chartview \ |
|
18 | 18 | scatterinteractions \ |
|
19 |
|
|
|
19 | splinechart \ | |
|
20 | 20 | areachart \ |
|
21 | 21 | stackedbarchartdrilldown |
@@ -1,101 +1,108 | |||
|
1 | 1 | #include "splinewidget.h" |
|
2 | 2 | #include "qchartview.h" |
|
3 | 3 | #include "qlineseries.h" |
|
4 | 4 | #include <QGridLayout> |
|
5 | 5 | #include <QPushButton> |
|
6 | 6 | #include "qchartaxis.h" |
|
7 | 7 | #include <qmath.h> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | SplineWidget::SplineWidget(QWidget *parent) |
|
12 | 12 | : QWidget(parent) |
|
13 | 13 | { |
|
14 | 14 | qsrand(time(NULL)); |
|
15 | 15 | //! [1] |
|
16 | 16 | //create QSplineSeries |
|
17 |
series = new QSplineSeries |
|
|
17 | series = new QSplineSeries; | |
|
18 | 18 | //! [1] |
|
19 | 19 | |
|
20 | 20 | //! [2] |
|
21 | 21 | // customize the series presentation settings |
|
22 | 22 | QPen seriesPen(Qt::blue); |
|
23 | 23 | seriesPen.setWidth(3); |
|
24 | 24 | series->setPen(seriesPen); |
|
25 | 25 | //! [2] |
|
26 | 26 | |
|
27 | 27 | //! [add points to series] |
|
28 | 28 | //add data points to the series |
|
29 | 29 | series->add(QPointF(150, 100)); |
|
30 | 30 | series->add(QPointF(200, 130)); |
|
31 | 31 | series->add(QPointF(250, 120)); |
|
32 | 32 | series->add(QPointF(300, 140)); |
|
33 | 33 | series->add(QPointF(350, 160)); |
|
34 | 34 | //! [add points to series] |
|
35 | 35 | // series->add(QPointF(400, 120)); |
|
36 | 36 | // series->add(QPointF(450, 150)); |
|
37 | 37 | // series->add(QPointF(500, 145)); |
|
38 | 38 | // series->add(QPointF(550, 170)); |
|
39 | 39 | // series->add(QPointF(600, 190)); |
|
40 | 40 | // series->add(QPointF(650, 210)); |
|
41 | 41 | // series->add(QPointF(700, 190)); |
|
42 | 42 | // series->add(QPointF(750, 180)); |
|
43 | 43 | // series->add(QPointF(800, 170)); |
|
44 | 44 | |
|
45 | 45 | //! [3] |
|
46 | 46 | // create chart view |
|
47 | 47 | QChartView* chart = new QChartView; |
|
48 | 48 | chart->addSeries(series); |
|
49 | 49 | |
|
50 | chart->axisX()->setRange(0, 1500); | |
|
51 | chart->axisY()->setRange(0, 400); | |
|
50 | chart->setChartTitle("Spline chart example"); | |
|
51 | ||
|
52 | // chart->setMinimumSize(800,600); | |
|
53 | // chart->axisX()->setRange(0, 1500); | |
|
54 | ||
|
55 | // chart->axisX()->setMax(1500); | |
|
56 | // chart->axisY()->setRange(0, 400); | |
|
57 | chart->axisX()->setMax(1500); | |
|
58 | // chart-> | |
|
52 | 59 | |
|
53 | 60 | chart->setMinimumSize(800,600); |
|
54 | 61 | //! [3] |
|
55 | 62 | |
|
56 | 63 | //! [4] |
|
57 | 64 | //add new data point button |
|
58 | 65 | QPushButton* addButton = new QPushButton("Add new point"); |
|
59 | 66 | connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); |
|
60 | 67 | |
|
61 | 68 | // remove the last data point in the series |
|
62 | 69 | QPushButton* removeButton = new QPushButton("Remove point"); |
|
63 | 70 | connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); |
|
64 | 71 | //! [4] |
|
65 | 72 | |
|
66 | 73 | //! [5] |
|
67 | 74 | //butttons layout |
|
68 | 75 | QVBoxLayout* buttonsLayout = new QVBoxLayout; |
|
69 | 76 | buttonsLayout->addWidget(addButton); |
|
70 | 77 | buttonsLayout->addWidget(removeButton); |
|
71 | 78 | buttonsLayout->addStretch(); |
|
72 | 79 | |
|
73 | 80 | QGridLayout* mainLayout = new QGridLayout; |
|
74 | 81 | mainLayout->addWidget(chart, 1, 0); |
|
75 | 82 | mainLayout->addLayout(buttonsLayout, 1, 1); |
|
76 | 83 | setLayout(mainLayout); |
|
77 | 84 | //! [5] |
|
78 | 85 | } |
|
79 | 86 | |
|
80 | 87 | //! [add point] |
|
81 | 88 | void SplineWidget::addNewPoint() |
|
82 | 89 | { |
|
83 | 90 | if (series->count() > 0) |
|
84 | 91 | series->add(QPointF(series->x(series->count() - 1) + 20 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100))); |
|
85 | 92 | else |
|
86 | 93 | series->add(QPointF(50, 50 + qrand()%50)); |
|
87 | 94 | } |
|
88 | 95 | //! [add point] |
|
89 | 96 | |
|
90 | 97 | //! [remove point] |
|
91 | 98 | void SplineWidget::removePoint() |
|
92 | 99 | { |
|
93 | 100 | if (series->count() > 0) |
|
94 | 101 | series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); |
|
95 | 102 | } |
|
96 | 103 | //! [remove point] |
|
97 | 104 | |
|
98 | 105 | SplineWidget::~SplineWidget() |
|
99 | 106 | { |
|
100 | 107 | |
|
101 | 108 | } |
@@ -1,364 +1,366 | |||
|
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 "qbarseries.h" |
|
8 | 8 | #include "qstackedbarseries.h" |
|
9 | 9 | #include "qpercentbarseries.h" |
|
10 | 10 | #include "qlineseries.h" |
|
11 | 11 | #include "qareaseries.h" |
|
12 | 12 | #include "qpieseries.h" |
|
13 | 13 | #include "qscatterseries.h" |
|
14 | 14 | #include "qsplineseries.h" |
|
15 | 15 | //items |
|
16 | 16 | #include "axisitem_p.h" |
|
17 | 17 | #include "axisanimationitem_p.h" |
|
18 | 18 | #include "areachartitem_p.h" |
|
19 | 19 | #include "barpresenter_p.h" |
|
20 | 20 | #include "stackedbarpresenter_p.h" |
|
21 | 21 | #include "percentbarpresenter_p.h" |
|
22 | 22 | #include "linechartitem_p.h" |
|
23 | 23 | #include "linechartanimationitem_p.h" |
|
24 | 24 | #include "piepresenter_p.h" |
|
25 | 25 | #include "scatterpresenter_p.h" |
|
26 | 26 | #include "splinepresenter_p.h" |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
31 | 31 | m_chart(chart), |
|
32 | 32 | m_dataset(dataset), |
|
33 | 33 | m_chartTheme(0), |
|
34 | 34 | m_zoomIndex(0), |
|
35 | 35 | m_marginSize(0), |
|
36 | 36 | m_rect(QRectF(QPoint(0,0),m_chart->size())), |
|
37 | 37 | m_options(QChart::NoAnimation) |
|
38 | 38 | { |
|
39 | 39 | createConnections(); |
|
40 | 40 | setChartTheme(QChart::ChartThemeDefault); |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | ChartPresenter::~ChartPresenter() |
|
44 | 44 | { |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void ChartPresenter::createConnections() |
|
48 | 48 | { |
|
49 | 49 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); |
|
50 | 50 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
51 | 51 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); |
|
52 | 52 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*))); |
|
53 | 53 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | QRectF ChartPresenter::geometry() const |
|
58 | 58 | { |
|
59 | 59 | return m_rect; |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | void ChartPresenter::handleGeometryChanged() |
|
63 | 63 | { |
|
64 | 64 | QRectF rect(QPoint(0,0),m_chart->size()); |
|
65 | 65 | rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); |
|
66 | 66 | |
|
67 | 67 | //rewrite zoom stack |
|
68 | 68 | for(int i=0;i<m_zoomStack.count();i++){ |
|
69 | 69 | QRectF r = m_zoomStack[i]; |
|
70 | 70 | qreal w = rect.width()/m_rect.width(); |
|
71 | 71 | qreal h = rect.height()/m_rect.height(); |
|
72 | 72 | QPointF tl = r.topLeft(); |
|
73 | 73 | tl.setX(tl.x()*w); |
|
74 | 74 | tl.setY(tl.y()*h); |
|
75 | 75 | QPointF br = r.bottomRight(); |
|
76 | 76 | br.setX(br.x()*w); |
|
77 | 77 | br.setY(br.y()*h); |
|
78 | 78 | r.setTopLeft(tl); |
|
79 | 79 | r.setBottomRight(br); |
|
80 | 80 | m_zoomStack[i]=r; |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | m_rect = rect; |
|
84 | 84 | Q_ASSERT(m_rect.isValid()); |
|
85 | 85 | emit geometryChanged(m_rect); |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | int ChartPresenter::margin() const |
|
89 | 89 | { |
|
90 | 90 | return m_marginSize; |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | void ChartPresenter::setMargin(int margin) |
|
94 | 94 | { |
|
95 | 95 | m_marginSize = margin; |
|
96 | 96 | } |
|
97 | 97 | |
|
98 | 98 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) |
|
99 | 99 | { |
|
100 | 100 | |
|
101 | 101 | AxisItem* item ; |
|
102 | 102 | |
|
103 | 103 | if(!m_options.testFlag(QChart::GridAxisAnimations)) |
|
104 | 104 | { |
|
105 | 105 | item = new AxisItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); |
|
106 | 106 | }else{ |
|
107 | 107 | item = new AxisAnimationItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); |
|
108 | 108 | } |
|
109 | 109 | if(axis==m_dataset->axisX()){ |
|
110 | 110 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); |
|
111 | 111 | //initialize |
|
112 | 112 | item->handleRangeChanged(domain->minX(),domain->maxX()); |
|
113 | 113 | } |
|
114 | 114 | else{ |
|
115 | 115 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); |
|
116 | 116 | //initialize |
|
117 | 117 | item->handleRangeChanged(domain->minY(),domain->maxY()); |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
121 | 121 | //initialize |
|
122 | 122 | item->handleGeometryChanged(m_rect); |
|
123 | 123 | m_chartTheme->decorate(axis,item); |
|
124 | 124 | m_axisItems.insert(axis,item); |
|
125 | 125 | } |
|
126 | 126 | |
|
127 | 127 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
128 | 128 | { |
|
129 | 129 | AxisItem* item = m_axisItems.take(axis); |
|
130 | 130 | Q_ASSERT(item); |
|
131 | 131 | delete item; |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) |
|
136 | 136 | { |
|
137 | 137 | switch(series->type()) |
|
138 | 138 | { |
|
139 | 139 | case QSeries::SeriesTypeLine: { |
|
140 | 140 | |
|
141 | 141 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
142 | 142 | LineChartItem* item; |
|
143 | 143 | if(m_options.testFlag(QChart::SeriesAnimations)){ |
|
144 | 144 | item = new LineChartAnimationItem(lineSeries,m_chart); |
|
145 | 145 | }else{ |
|
146 | 146 | item = new LineChartItem(lineSeries,m_chart); |
|
147 | 147 | } |
|
148 | 148 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
149 | 149 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
150 | 150 | //initialize |
|
151 | 151 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
152 | 152 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
153 | 153 | //decorate |
|
154 | 154 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); |
|
155 | 155 | m_chartItems.insert(series,item); |
|
156 | 156 | break; |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | case QSeries::SeriesTypeArea: { |
|
160 | 160 | |
|
161 | 161 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
162 | 162 | AreaChartItem* item; |
|
163 | 163 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
164 | 164 | item = new AreaChartItem(areaSeries,m_chart); |
|
165 | 165 | } |
|
166 | 166 | else { |
|
167 | 167 | item = new AreaChartItem(areaSeries,m_chart); |
|
168 | 168 | } |
|
169 | 169 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
170 | 170 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
171 | 171 | //initialize |
|
172 | 172 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
173 | 173 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
174 | 174 | //decorate |
|
175 | 175 | m_chartTheme->decorate(item,areaSeries,m_chartItems.count()); |
|
176 | 176 | m_chartItems.insert(series,item); |
|
177 | 177 | break; |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | case QSeries::SeriesTypeBar: { |
|
181 | 181 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
182 | 182 | BarPresenter* item = new BarPresenter(barSeries,m_chart); |
|
183 | 183 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); |
|
184 | 184 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
185 | 185 | // QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
186 | 186 | m_chartItems.insert(series,item); |
|
187 | 187 | // m_axisXItem->setVisible(false); |
|
188 | 188 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
189 | 189 | break; |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | case QSeries::SeriesTypeStackedBar: { |
|
193 | 193 | |
|
194 | 194 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
195 | 195 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); |
|
196 | 196 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); |
|
197 | 197 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
198 | 198 | // QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
199 | 199 | m_chartItems.insert(series,item); |
|
200 | 200 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
201 | 201 | break; |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | case QSeries::SeriesTypePercentBar: { |
|
205 | 205 | |
|
206 | 206 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
207 | 207 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); |
|
208 | 208 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); |
|
209 | 209 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
210 | 210 | // QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
211 | 211 | m_chartItems.insert(series,item); |
|
212 | 212 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
213 | 213 | break; |
|
214 | 214 | } |
|
215 | 215 | case QSeries::SeriesTypeScatter: { |
|
216 | 216 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
217 | 217 | ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); |
|
218 | 218 | QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)), |
|
219 | 219 | scatterSeries, SIGNAL(clicked(QPointF))); |
|
220 | 220 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), |
|
221 | 221 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
222 | 222 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); |
|
223 | 223 | m_chartItems.insert(scatterSeries, scatterPresenter); |
|
224 | 224 | if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect); |
|
225 | 225 | break; |
|
226 | 226 | } |
|
227 | 227 | case QSeries::SeriesTypePie: { |
|
228 | 228 | QPieSeries *s = qobject_cast<QPieSeries *>(series); |
|
229 | 229 | PiePresenter* pie = new PiePresenter(m_chart, s); |
|
230 | 230 | m_chartTheme->decorate(pie, s, m_chartItems.count()); |
|
231 | 231 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); |
|
232 | 232 | |
|
233 | 233 | // Hide all from background when there is only piechart |
|
234 | 234 | // TODO: refactor this ugly code... should be one setting for this |
|
235 | 235 | if (m_chartItems.count() == 0) { |
|
236 | 236 | m_chart->axisX()->setAxisVisible(false); |
|
237 | 237 | m_chart->axisY()->setAxisVisible(false); |
|
238 | 238 | m_chart->axisX()->setGridVisible(false); |
|
239 | 239 | m_chart->axisY()->setGridVisible(false); |
|
240 | 240 | m_chart->axisX()->setLabelsVisible(false); |
|
241 | 241 | m_chart->axisY()->setLabelsVisible(false); |
|
242 | 242 | m_chart->axisX()->setShadesVisible(false); |
|
243 | 243 | m_chart->axisY()->setShadesVisible(false); |
|
244 | 244 | m_chart->setChartBackgroundBrush(Qt::transparent); |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | m_chartItems.insert(series, pie); |
|
248 | 248 | pie->handleGeometryChanged(m_rect); |
|
249 | 249 | break; |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | case QSeries::SeriesTypeSpline: { |
|
253 | 253 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series); |
|
254 | 254 | SplinePresenter* splinePresenter = new SplinePresenter(splineSeries, m_chart); |
|
255 | 255 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
256 | //initialize | |
|
257 | splinePresenter->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | |
|
256 | 258 | m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count()); |
|
257 | 259 | m_chartItems.insert(splineSeries, splinePresenter); |
|
258 | 260 | break; |
|
259 | 261 | } |
|
260 | 262 | default: { |
|
261 | 263 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
262 | 264 | break; |
|
263 | 265 | } |
|
264 | 266 | } |
|
265 | 267 | |
|
266 | 268 | zoomReset(); |
|
267 | 269 | } |
|
268 | 270 | |
|
269 | 271 | void ChartPresenter::handleSeriesRemoved(QSeries* series) |
|
270 | 272 | { |
|
271 | 273 | ChartItem* item = m_chartItems.take(series); |
|
272 | 274 | delete item; |
|
273 | 275 | } |
|
274 | 276 | |
|
275 | 277 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) |
|
276 | 278 | { |
|
277 | 279 | delete m_chartTheme; |
|
278 | 280 | |
|
279 | 281 | m_chartTheme = ChartTheme::createTheme(theme); |
|
280 | 282 | |
|
281 | 283 | m_chartTheme->decorate(m_chart); |
|
282 | 284 | QMapIterator<QSeries*,ChartItem*> i(m_chartItems); |
|
283 | 285 | |
|
284 | 286 | int index=0; |
|
285 | 287 | while (i.hasNext()) { |
|
286 | 288 | i.next(); |
|
287 | 289 | m_chartTheme->decorate(i.value(),i.key(),index); |
|
288 | 290 | index++; |
|
289 | 291 | } |
|
290 | 292 | |
|
291 | 293 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); |
|
292 | 294 | while (j.hasNext()) { |
|
293 | 295 | j.next(); |
|
294 | 296 | m_chartTheme->decorate(j.key(),j.value()); |
|
295 | 297 | } |
|
296 | 298 | } |
|
297 | 299 | |
|
298 | 300 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
299 | 301 | { |
|
300 | 302 | return m_chartTheme->id(); |
|
301 | 303 | } |
|
302 | 304 | |
|
303 | 305 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
304 | 306 | { |
|
305 | 307 | if(m_options!=options) { |
|
306 | 308 | |
|
307 | 309 | m_options=options; |
|
308 | 310 | |
|
309 | 311 | //recreate elements |
|
310 | 312 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); |
|
311 | 313 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); |
|
312 | 314 | |
|
313 | 315 | foreach(QChartAxis* axis, axisList) { |
|
314 | 316 | handleAxisRemoved(axis); |
|
315 | 317 | handleAxisAdded(axis,m_dataset->domain(axis)); |
|
316 | 318 | } |
|
317 | 319 | foreach(QSeries* series, seriesList) { |
|
318 | 320 | handleSeriesRemoved(series); |
|
319 | 321 | handleSeriesAdded(series,m_dataset->domain(series)); |
|
320 | 322 | } |
|
321 | 323 | } |
|
322 | 324 | } |
|
323 | 325 | |
|
324 | 326 | void ChartPresenter::zoomIn() |
|
325 | 327 | { |
|
326 | 328 | QRectF rect = geometry(); |
|
327 | 329 | rect.setWidth(rect.width()/2); |
|
328 | 330 | rect.setHeight(rect.height()/2); |
|
329 | 331 | rect.moveCenter(geometry().center()); |
|
330 | 332 | zoomIn(rect); |
|
331 | 333 | } |
|
332 | 334 | |
|
333 | 335 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
334 | 336 | { |
|
335 | 337 | QRectF r = rect.normalized(); |
|
336 | 338 | r.translate(-m_marginSize, -m_marginSize); |
|
337 | 339 | m_dataset->zoomInDomain(r,geometry().size()); |
|
338 | 340 | m_zoomStack<<r; |
|
339 | 341 | m_zoomIndex++; |
|
340 | 342 | } |
|
341 | 343 | |
|
342 | 344 | void ChartPresenter::zoomOut() |
|
343 | 345 | { |
|
344 | 346 | if(m_zoomIndex==0) return; |
|
345 | 347 | m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); |
|
346 | 348 | m_zoomIndex--; |
|
347 | 349 | m_zoomStack.resize(m_zoomIndex); |
|
348 | 350 | } |
|
349 | 351 | |
|
350 | 352 | void ChartPresenter::zoomReset() |
|
351 | 353 | { |
|
352 | 354 | m_zoomIndex=0; |
|
353 | 355 | m_zoomStack.resize(m_zoomIndex); |
|
354 | 356 | } |
|
355 | 357 | |
|
356 | 358 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
357 | 359 | { |
|
358 | 360 | return m_options; |
|
359 | 361 | } |
|
360 | 362 | |
|
361 | 363 | |
|
362 | 364 | #include "moc_chartpresenter_p.cpp" |
|
363 | 365 | |
|
364 | 366 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,77 +1,77 | |||
|
1 | 1 | #include "splinepresenter_p.h" |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | |
|
4 | 4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | 5 | |
|
6 | 6 | SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) : |
|
7 | 7 | LineChartItem(series, parent)//,m_boundingRect() |
|
8 | 8 | { |
|
9 | 9 | // |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | QPointF SplinePresenter::calculateGeometryControlPoint(int index) const |
|
15 | 15 | { |
|
16 | 16 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
17 | 17 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
18 | 18 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
19 | 19 | qreal x = (splineSeries->controlPoint(index).x() - m_minX)* deltaX; |
|
20 | 20 | qreal y = (splineSeries->controlPoint(index).y() - m_minY)*-deltaY + m_size.height(); |
|
21 | 21 | return QPointF(x,y); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | void SplinePresenter::applyGeometry(QVector<QPointF>& points) |
|
25 | 25 | { |
|
26 | 26 | if(points.size()==0) return; |
|
27 | 27 | |
|
28 | 28 | QPainterPath splinePath; |
|
29 | 29 | const QPointF& point = points.at(0); |
|
30 | 30 | splinePath.moveTo(point); |
|
31 | 31 | |
|
32 | 32 | // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
33 | 33 | for (int i = 0; i < points.size() - 1; i++) |
|
34 | 34 | { |
|
35 | 35 | const QPointF& point = points.at(i + 1); |
|
36 | 36 | splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | prepareGeometryChange(); |
|
42 | 42 | m_path = splinePath; |
|
43 | 43 | m_rect = splinePath.boundingRect(); |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
47 | 47 | { |
|
48 | 48 | Q_UNUSED(widget); |
|
49 | 49 | Q_UNUSED(option); |
|
50 | 50 | painter->save(); |
|
51 | 51 | painter->setPen(m_pen); |
|
52 | 52 | painter->setClipRect(m_clipRect); |
|
53 | 53 | painter->drawPath(m_path); |
|
54 | 54 | |
|
55 | 55 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
56 |
for (int i = 0; i < |
|
|
56 | for (int i = 0; i < m_points.size() - 1; i++) | |
|
57 | 57 | { |
|
58 | 58 | painter->setPen(Qt::red); |
|
59 | 59 | painter->drawEllipse(m_points[i], 2, 2); |
|
60 | 60 | |
|
61 | 61 | painter->setPen(Qt::blue); |
|
62 | 62 | // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); |
|
63 | 63 | // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); |
|
64 | 64 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); |
|
65 | 65 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); |
|
66 | 66 | } |
|
67 | 67 | if (m_points.count() > 0) |
|
68 | 68 | { |
|
69 | 69 | painter->setPen(Qt::red); |
|
70 | 70 | painter->drawEllipse(m_points[m_points.count() - 1], 2, 2); |
|
71 | 71 | } |
|
72 | 72 | painter->restore(); |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | #include "moc_splinepresenter_p.cpp" |
|
76 | 76 | |
|
77 | 77 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now