@@ -1,108 +1,102 | |||
|
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 | 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 | 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 | 51 |
|
|
58 | // chart-> | |
|
52 | chart->axisY()->setMax(500); | |
|
59 | 53 | |
|
60 | 54 | chart->setMinimumSize(800,600); |
|
61 | 55 | //! [3] |
|
62 | 56 | |
|
63 | 57 | //! [4] |
|
64 | 58 | //add new data point button |
|
65 | 59 | QPushButton* addButton = new QPushButton("Add new point"); |
|
66 | 60 | connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); |
|
67 | 61 | |
|
68 | 62 | // remove the last data point in the series |
|
69 | 63 | QPushButton* removeButton = new QPushButton("Remove point"); |
|
70 | 64 | connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); |
|
71 | 65 | //! [4] |
|
72 | 66 | |
|
73 | 67 | //! [5] |
|
74 | 68 | //butttons layout |
|
75 | 69 | QVBoxLayout* buttonsLayout = new QVBoxLayout; |
|
76 | 70 | buttonsLayout->addWidget(addButton); |
|
77 | 71 | buttonsLayout->addWidget(removeButton); |
|
78 | 72 | buttonsLayout->addStretch(); |
|
79 | 73 | |
|
80 | 74 | QGridLayout* mainLayout = new QGridLayout; |
|
81 | 75 | mainLayout->addWidget(chart, 1, 0); |
|
82 | 76 | mainLayout->addLayout(buttonsLayout, 1, 1); |
|
83 | 77 | setLayout(mainLayout); |
|
84 | 78 | //! [5] |
|
85 | 79 | } |
|
86 | 80 | |
|
87 | 81 | //! [add point] |
|
88 | 82 | void SplineWidget::addNewPoint() |
|
89 | 83 | { |
|
90 | 84 | if (series->count() > 0) |
|
91 |
series->add(QPointF(series->x(series->count() - 1) + |
|
|
85 | series->add(QPointF(series->x(series->count() - 1) + 40 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100))); | |
|
92 | 86 | else |
|
93 | 87 | series->add(QPointF(50, 50 + qrand()%50)); |
|
94 | 88 | } |
|
95 | 89 | //! [add point] |
|
96 | 90 | |
|
97 | 91 | //! [remove point] |
|
98 | 92 | void SplineWidget::removePoint() |
|
99 | 93 | { |
|
100 | 94 | if (series->count() > 0) |
|
101 | 95 | series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); |
|
102 | 96 | } |
|
103 | 97 | //! [remove point] |
|
104 | 98 | |
|
105 | 99 | SplineWidget::~SplineWidget() |
|
106 | 100 | { |
|
107 | 101 | |
|
108 | 102 | } |
@@ -1,373 +1,373 | |||
|
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 |
#include "spline |
|
|
26 | #include "splinechartitem_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 | item->handleTicksCountChanged(4); |
|
114 | 114 | } |
|
115 | 115 | else{ |
|
116 | 116 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); |
|
117 | 117 | //initialize |
|
118 | 118 | item->handleRangeChanged(domain->minY(),domain->maxY()); |
|
119 | 119 | item->handleTicksCountChanged(4); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
123 | 123 | //initialize |
|
124 | 124 | item->handleGeometryChanged(m_rect); |
|
125 | 125 | m_chartTheme->decorate(axis,item); |
|
126 | 126 | m_axisItems.insert(axis,item); |
|
127 | 127 | } |
|
128 | 128 | |
|
129 | 129 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
130 | 130 | { |
|
131 | 131 | AxisItem* item = m_axisItems.take(axis); |
|
132 | 132 | Q_ASSERT(item); |
|
133 | 133 | delete item; |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) |
|
138 | 138 | { |
|
139 | 139 | switch(series->type()) |
|
140 | 140 | { |
|
141 | 141 | case QSeries::SeriesTypeLine: { |
|
142 | 142 | |
|
143 | 143 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
144 | 144 | LineChartItem* item; |
|
145 | 145 | if(m_options.testFlag(QChart::SeriesAnimations)){ |
|
146 | 146 | item = new LineChartAnimationItem(lineSeries,m_chart); |
|
147 | 147 | }else{ |
|
148 | 148 | item = new LineChartItem(lineSeries,m_chart); |
|
149 | 149 | } |
|
150 | 150 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
151 | 151 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
152 | 152 | //initialize |
|
153 | 153 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
154 | 154 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
155 | 155 | //decorate |
|
156 | 156 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); |
|
157 | 157 | m_chartItems.insert(series,item); |
|
158 | 158 | break; |
|
159 | 159 | } |
|
160 | 160 | |
|
161 | 161 | case QSeries::SeriesTypeArea: { |
|
162 | 162 | |
|
163 | 163 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
164 | 164 | AreaChartItem* item; |
|
165 | 165 | if(m_options.testFlag(QChart::SeriesAnimations)) { |
|
166 | 166 | item = new AreaChartItem(areaSeries,m_chart); |
|
167 | 167 | } |
|
168 | 168 | else { |
|
169 | 169 | item = new AreaChartItem(areaSeries,m_chart); |
|
170 | 170 | } |
|
171 | 171 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
172 | 172 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
173 | 173 | //initialize |
|
174 | 174 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
175 | 175 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
176 | 176 | //decorate |
|
177 | 177 | m_chartTheme->decorate(item,areaSeries,m_chartItems.count()); |
|
178 | 178 | m_chartItems.insert(series,item); |
|
179 | 179 | break; |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | 182 | case QSeries::SeriesTypeBar: { |
|
183 | 183 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
184 | 184 | BarPresenter* item = new BarPresenter(barSeries,m_chart); |
|
185 | 185 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); |
|
186 | 186 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
187 | 187 | // QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
188 | 188 | m_chartItems.insert(series,item); |
|
189 | 189 | // m_axisXItem->setVisible(false); |
|
190 | 190 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
191 | 191 | break; |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | case QSeries::SeriesTypeStackedBar: { |
|
195 | 195 | |
|
196 | 196 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
197 | 197 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); |
|
198 | 198 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); |
|
199 | 199 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
200 | 200 | // QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
201 | 201 | m_chartItems.insert(series,item); |
|
202 | 202 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
203 | 203 | break; |
|
204 | 204 | } |
|
205 | 205 | |
|
206 | 206 | case QSeries::SeriesTypePercentBar: { |
|
207 | 207 | |
|
208 | 208 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
209 | 209 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); |
|
210 | 210 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); |
|
211 | 211 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
212 | 212 | // QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
213 | 213 | m_chartItems.insert(series,item); |
|
214 | 214 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
215 | 215 | break; |
|
216 | 216 | } |
|
217 | 217 | case QSeries::SeriesTypeScatter: { |
|
218 | 218 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
219 | 219 | ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); |
|
220 | 220 | QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)), |
|
221 | 221 | scatterSeries, SIGNAL(clicked(QPointF))); |
|
222 | 222 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), |
|
223 | 223 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
224 | 224 | QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)), |
|
225 | 225 | scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
226 | 226 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); |
|
227 | 227 | m_chartItems.insert(scatterSeries, scatterPresenter); |
|
228 | 228 | if (m_rect.isValid()) |
|
229 | 229 | scatterPresenter->handleGeometryChanged(m_rect); |
|
230 | 230 | scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY()); |
|
231 | 231 | break; |
|
232 | 232 | } |
|
233 | 233 | case QSeries::SeriesTypePie: { |
|
234 | 234 | QPieSeries *s = qobject_cast<QPieSeries *>(series); |
|
235 | 235 | PiePresenter* pie = new PiePresenter(m_chart, s); |
|
236 | 236 | m_chartTheme->decorate(pie, s, m_chartItems.count()); |
|
237 | 237 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); |
|
238 | 238 | |
|
239 | 239 | // Hide all from background when there is only piechart |
|
240 | 240 | // TODO: refactor this ugly code... should be one setting for this |
|
241 | 241 | if (m_chartItems.count() == 0) { |
|
242 | 242 | m_chart->axisX()->setAxisVisible(false); |
|
243 | 243 | m_chart->axisY()->setAxisVisible(false); |
|
244 | 244 | m_chart->axisX()->setGridVisible(false); |
|
245 | 245 | m_chart->axisY()->setGridVisible(false); |
|
246 | 246 | m_chart->axisX()->setLabelsVisible(false); |
|
247 | 247 | m_chart->axisY()->setLabelsVisible(false); |
|
248 | 248 | m_chart->axisX()->setShadesVisible(false); |
|
249 | 249 | m_chart->axisY()->setShadesVisible(false); |
|
250 | 250 | m_chart->setChartBackgroundBrush(Qt::transparent); |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | m_chartItems.insert(series, pie); |
|
254 | 254 | pie->handleGeometryChanged(m_rect); |
|
255 | 255 | break; |
|
256 | 256 | } |
|
257 | 257 | |
|
258 | 258 | case QSeries::SeriesTypeSpline: { |
|
259 | 259 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series); |
|
260 |
Spline |
|
|
260 | SplineChartItem* splinePresenter = new SplineChartItem(splineSeries, m_chart); | |
|
261 | 261 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
262 | 262 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),splinePresenter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
263 | 263 | //initialize |
|
264 | 264 | splinePresenter->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
265 | 265 | m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count()); |
|
266 | 266 | m_chartItems.insert(splineSeries, splinePresenter); |
|
267 | 267 | break; |
|
268 | 268 | } |
|
269 | 269 | default: { |
|
270 | 270 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
271 | 271 | break; |
|
272 | 272 | } |
|
273 | 273 | } |
|
274 | 274 | |
|
275 | 275 | zoomReset(); |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | void ChartPresenter::handleSeriesRemoved(QSeries* series) |
|
279 | 279 | { |
|
280 | 280 | ChartItem* item = m_chartItems.take(series); |
|
281 | 281 | delete item; |
|
282 | 282 | } |
|
283 | 283 | |
|
284 | 284 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) |
|
285 | 285 | { |
|
286 | 286 | delete m_chartTheme; |
|
287 | 287 | |
|
288 | 288 | m_chartTheme = ChartTheme::createTheme(theme); |
|
289 | 289 | |
|
290 | 290 | m_chartTheme->decorate(m_chart); |
|
291 | 291 | QMapIterator<QSeries*,ChartItem*> i(m_chartItems); |
|
292 | 292 | |
|
293 | 293 | int index=0; |
|
294 | 294 | while (i.hasNext()) { |
|
295 | 295 | i.next(); |
|
296 | 296 | m_chartTheme->decorate(i.value(),i.key(),index); |
|
297 | 297 | index++; |
|
298 | 298 | } |
|
299 | 299 | |
|
300 | 300 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); |
|
301 | 301 | while (j.hasNext()) { |
|
302 | 302 | j.next(); |
|
303 | 303 | m_chartTheme->decorate(j.key(),j.value()); |
|
304 | 304 | } |
|
305 | 305 | } |
|
306 | 306 | |
|
307 | 307 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
308 | 308 | { |
|
309 | 309 | return m_chartTheme->id(); |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
313 | 313 | { |
|
314 | 314 | if(m_options!=options) { |
|
315 | 315 | |
|
316 | 316 | m_options=options; |
|
317 | 317 | |
|
318 | 318 | //recreate elements |
|
319 | 319 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); |
|
320 | 320 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); |
|
321 | 321 | |
|
322 | 322 | foreach(QChartAxis* axis, axisList) { |
|
323 | 323 | handleAxisRemoved(axis); |
|
324 | 324 | handleAxisAdded(axis,m_dataset->domain(axis)); |
|
325 | 325 | } |
|
326 | 326 | foreach(QSeries* series, seriesList) { |
|
327 | 327 | handleSeriesRemoved(series); |
|
328 | 328 | handleSeriesAdded(series,m_dataset->domain(series)); |
|
329 | 329 | } |
|
330 | 330 | } |
|
331 | 331 | } |
|
332 | 332 | |
|
333 | 333 | void ChartPresenter::zoomIn() |
|
334 | 334 | { |
|
335 | 335 | QRectF rect = geometry(); |
|
336 | 336 | rect.setWidth(rect.width()/2); |
|
337 | 337 | rect.setHeight(rect.height()/2); |
|
338 | 338 | rect.moveCenter(geometry().center()); |
|
339 | 339 | zoomIn(rect); |
|
340 | 340 | } |
|
341 | 341 | |
|
342 | 342 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
343 | 343 | { |
|
344 | 344 | QRectF r = rect.normalized(); |
|
345 | 345 | r.translate(-m_marginSize, -m_marginSize); |
|
346 | 346 | m_dataset->zoomInDomain(r,geometry().size()); |
|
347 | 347 | m_zoomStack<<r; |
|
348 | 348 | m_zoomIndex++; |
|
349 | 349 | } |
|
350 | 350 | |
|
351 | 351 | void ChartPresenter::zoomOut() |
|
352 | 352 | { |
|
353 | 353 | if(m_zoomIndex==0) return; |
|
354 | 354 | m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); |
|
355 | 355 | m_zoomIndex--; |
|
356 | 356 | m_zoomStack.resize(m_zoomIndex); |
|
357 | 357 | } |
|
358 | 358 | |
|
359 | 359 | void ChartPresenter::zoomReset() |
|
360 | 360 | { |
|
361 | 361 | m_zoomIndex=0; |
|
362 | 362 | m_zoomStack.resize(m_zoomIndex); |
|
363 | 363 | } |
|
364 | 364 | |
|
365 | 365 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
366 | 366 | { |
|
367 | 367 | return m_options; |
|
368 | 368 | } |
|
369 | 369 | |
|
370 | 370 | |
|
371 | 371 | #include "moc_chartpresenter_p.cpp" |
|
372 | 372 | |
|
373 | 373 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,328 +1,328 | |||
|
1 | 1 | #include "charttheme_p.h" |
|
2 | 2 | #include "qchart.h" |
|
3 | 3 | #include "qchartaxis.h" |
|
4 | 4 | #include <QTime> |
|
5 | 5 | |
|
6 | 6 | //series |
|
7 | 7 | #include "qbarset.h" |
|
8 | 8 | #include "qbarseries.h" |
|
9 | 9 | #include "qstackedbarseries.h" |
|
10 | 10 | #include "qpercentbarseries.h" |
|
11 | 11 | #include "qlineseries.h" |
|
12 | 12 | #include "qareaseries.h" |
|
13 | 13 | #include "qscatterseries.h" |
|
14 | 14 | #include "qpieseries.h" |
|
15 | 15 | #include "qpieslice.h" |
|
16 | 16 | #include "qsplineseries.h" |
|
17 | 17 | |
|
18 | 18 | //items |
|
19 | 19 | #include "axisitem_p.h" |
|
20 | 20 | #include "barpresenter_p.h" |
|
21 | 21 | #include "stackedbarpresenter_p.h" |
|
22 | 22 | #include "percentbarpresenter_p.h" |
|
23 | 23 | #include "linechartitem_p.h" |
|
24 | 24 | #include "areachartitem_p.h" |
|
25 | 25 | #include "scatterpresenter_p.h" |
|
26 | 26 | #include "piepresenter_p.h" |
|
27 |
#include "spline |
|
|
27 | #include "splinechartitem_p.h" | |
|
28 | 28 | |
|
29 | 29 | //themes |
|
30 | 30 | #include "chartthemevanilla_p.h" |
|
31 | 31 | #include "chartthemeicy_p.h" |
|
32 | 32 | #include "chartthemegrayscale_p.h" |
|
33 | 33 | #include "chartthemescientific_p.h" |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
37 | 37 | |
|
38 | 38 | /* TODO |
|
39 | 39 | case QChart::ChartThemeUnnamed1: |
|
40 | 40 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); |
|
41 | 41 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); |
|
42 | 42 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); |
|
43 | 43 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); |
|
44 | 44 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); |
|
45 | 45 | |
|
46 | 46 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); |
|
47 | 47 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
48 | 48 | */ |
|
49 | 49 | |
|
50 | 50 | ChartTheme::ChartTheme(QChart::ChartTheme id) |
|
51 | 51 | { |
|
52 | 52 | m_id = id; |
|
53 | 53 | m_seriesColor.append(QRgb(0xff000000)); |
|
54 | 54 | m_seriesColor.append(QRgb(0xff707070)); |
|
55 | 55 | m_gradientStartColor = QColor(QRgb(0xffffffff)); |
|
56 | 56 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
57 | 57 | |
|
58 | 58 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | |
|
62 | 62 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
63 | 63 | { |
|
64 | 64 | switch(theme) { |
|
65 | 65 | case QChart::ChartThemeDefault: |
|
66 | 66 | return new ChartTheme(); |
|
67 | 67 | case QChart::ChartThemeVanilla: |
|
68 | 68 | return new ChartThemeVanilla(); |
|
69 | 69 | case QChart::ChartThemeIcy: |
|
70 | 70 | return new ChartThemeIcy(); |
|
71 | 71 | case QChart::ChartThemeGrayscale: |
|
72 | 72 | return new ChartThemeGrayscale(); |
|
73 | 73 | case QChart::ChartThemeScientific: |
|
74 | 74 | return new ChartThemeScientific(); |
|
75 | 75 | } |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | void ChartTheme::decorate(QChart* chart) |
|
79 | 79 | { |
|
80 | 80 | QLinearGradient backgroundGradient; |
|
81 | 81 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); |
|
82 | 82 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); |
|
83 | 83 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
84 | 84 | chart->setChartBackgroundBrush(backgroundGradient); |
|
85 | 85 | } |
|
86 | 86 | //TODO helper to by removed later |
|
87 | 87 | void ChartTheme::decorate(ChartItem* item, QSeries* series,int count) |
|
88 | 88 | { |
|
89 | 89 | switch(series->type()) |
|
90 | 90 | { |
|
91 | 91 | case QSeries::SeriesTypeLine: { |
|
92 | 92 | QLineSeries* s = static_cast<QLineSeries*>(series); |
|
93 | 93 | LineChartItem* i = static_cast<LineChartItem*>(item); |
|
94 | 94 | decorate(i,s,count); |
|
95 | 95 | break; |
|
96 | 96 | } |
|
97 | 97 | case QSeries::SeriesTypeArea: { |
|
98 | 98 | QAreaSeries* s = static_cast<QAreaSeries*>(series); |
|
99 | 99 | AreaChartItem* i = static_cast<AreaChartItem*>(item); |
|
100 | 100 | decorate(i,s,count); |
|
101 | 101 | break; |
|
102 | 102 | } |
|
103 | 103 | case QSeries::SeriesTypeBar: { |
|
104 | 104 | QBarSeries* b = static_cast<QBarSeries*>(series); |
|
105 | 105 | BarPresenter* i = static_cast<BarPresenter*>(item); |
|
106 | 106 | decorate(i,b,count); |
|
107 | 107 | break; |
|
108 | 108 | } |
|
109 | 109 | case QSeries::SeriesTypeStackedBar: { |
|
110 | 110 | QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series); |
|
111 | 111 | StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item); |
|
112 | 112 | decorate(i,s,count); |
|
113 | 113 | break; |
|
114 | 114 | } |
|
115 | 115 | case QSeries::SeriesTypePercentBar: { |
|
116 | 116 | QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series); |
|
117 | 117 | PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item); |
|
118 | 118 | decorate(i,s,count); |
|
119 | 119 | break; |
|
120 | 120 | } |
|
121 | 121 | case QSeries::SeriesTypeScatter: { |
|
122 | 122 | QScatterSeries* s = qobject_cast<QScatterSeries*>(series); |
|
123 | 123 | Q_ASSERT(s); |
|
124 | 124 | ScatterPresenter* i = static_cast<ScatterPresenter*>(item); |
|
125 | 125 | Q_ASSERT(i); |
|
126 | 126 | decorate(i, s, count); |
|
127 | 127 | break; |
|
128 | 128 | } |
|
129 | 129 | case QSeries::SeriesTypePie: { |
|
130 | 130 | QPieSeries* s = static_cast<QPieSeries*>(series); |
|
131 | 131 | PiePresenter* i = static_cast<PiePresenter*>(item); |
|
132 | 132 | decorate(i,s,count); |
|
133 | 133 | break; |
|
134 | 134 | } |
|
135 | 135 | default: |
|
136 | 136 | qDebug()<<"Wrong item to be decorated by theme"; |
|
137 | 137 | break; |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count) |
|
143 | 143 | { |
|
144 | 144 | QPen pen; |
|
145 | 145 | QBrush brush; |
|
146 | 146 | |
|
147 | 147 | if(pen != series->pen()){ |
|
148 | 148 | item->setPen(series->pen()); |
|
149 | 149 | }else{ |
|
150 | 150 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
151 | 151 | pen.setWidthF(2); |
|
152 | 152 | item->setPen(pen); |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | if(brush != series->brush()){ |
|
156 | 156 | item->setBrush(series->brush()); |
|
157 | 157 | }else{ |
|
158 | 158 | QBrush brush(m_seriesColor.at(count%m_seriesColor.size())); |
|
159 | 159 | item->setBrush(brush); |
|
160 | 160 | } |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | |
|
164 | 164 | void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count) |
|
165 | 165 | { |
|
166 | 166 | QPen pen; |
|
167 | 167 | if(pen != series->pen()){ |
|
168 | 168 | item->setPen(series->pen()); |
|
169 | 169 | return; |
|
170 | 170 | } |
|
171 | 171 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
172 | 172 | pen.setWidthF(2); |
|
173 | 173 | item->setPen(pen); |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count) |
|
177 | 177 | { |
|
178 | 178 | QList<QBarSet*> sets = series->barSets(); |
|
179 | 179 | for (int i=0; i<series->barsetCount(); i++) { |
|
180 | 180 | sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); |
|
181 | 181 | } |
|
182 | 182 | } |
|
183 | 183 | |
|
184 | 184 | void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count) |
|
185 | 185 | { |
|
186 | 186 | QList<QBarSet*> sets = series->barSets(); |
|
187 | 187 | for (int i=0; i<series->barsetCount(); i++) { |
|
188 | 188 | sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); |
|
189 | 189 | } |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count) |
|
193 | 193 | { |
|
194 | 194 | QList<QBarSet*> sets = series->barSets(); |
|
195 | 195 | for (int i=0; i<series->barsetCount(); i++) { |
|
196 | 196 | sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); |
|
197 | 197 | } |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) |
|
201 | 201 | { |
|
202 | 202 | Q_ASSERT(presenter); |
|
203 | 203 | Q_ASSERT(series); |
|
204 | 204 | |
|
205 | 205 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); |
|
206 | 206 | // TODO: define alpha in the theme? or in the series? |
|
207 | 207 | //color.setAlpha(120); |
|
208 | 208 | |
|
209 | 209 | QBrush brush(color, Qt::SolidPattern); |
|
210 | 210 | presenter->m_markerBrush = brush; |
|
211 | 211 | |
|
212 | 212 | QPen pen(brush, 3); |
|
213 | 213 | pen.setColor(color); |
|
214 | 214 | presenter->m_markerPen = pen; |
|
215 | 215 | } |
|
216 | 216 | |
|
217 | 217 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
|
218 | 218 | { |
|
219 | 219 | // create a list of slice colors based on current theme |
|
220 | 220 | int i = 0; |
|
221 | 221 | QList<QColor> colors; |
|
222 | 222 | while (colors.count() < series->count()) { |
|
223 | 223 | |
|
224 | 224 | // get base color |
|
225 | 225 | QColor c = m_seriesColor[i++]; |
|
226 | 226 | i = i % m_seriesColor.count(); |
|
227 | 227 | |
|
228 | 228 | // dont use black colors... looks bad |
|
229 | 229 | if (c == Qt::black) |
|
230 | 230 | continue; |
|
231 | 231 | |
|
232 | 232 | // by default use the "raw" theme color |
|
233 | 233 | if (!colors.contains(c)) { |
|
234 | 234 | colors << c; |
|
235 | 235 | continue; |
|
236 | 236 | } |
|
237 | 237 | // ...ok we need to generate something that looks like the same color |
|
238 | 238 | // but different lightness |
|
239 | 239 | |
|
240 | 240 | int tryCount = 0; |
|
241 | 241 | while (tryCount++ < 100) { |
|
242 | 242 | |
|
243 | 243 | // find maximum value we can raise the lightness |
|
244 | 244 | int lMax = 255; |
|
245 | 245 | if (lMax > 255 - c.red()) |
|
246 | 246 | lMax = 255 - c.red(); |
|
247 | 247 | if (lMax > 255 - c.green()) |
|
248 | 248 | lMax = 255 - c.green(); |
|
249 | 249 | if (lMax > 255 - c.blue()) |
|
250 | 250 | lMax = 255 - c.blue(); |
|
251 | 251 | |
|
252 | 252 | // find maximum value we can make it darker |
|
253 | 253 | int dMax = 255; |
|
254 | 254 | if (dMax > c.red()) |
|
255 | 255 | dMax = c.red(); |
|
256 | 256 | if (dMax > c.green()) |
|
257 | 257 | dMax = c.green(); |
|
258 | 258 | if (dMax > c.blue()) |
|
259 | 259 | dMax = c.blue(); |
|
260 | 260 | |
|
261 | 261 | int max = dMax + lMax; |
|
262 | 262 | if (max == 0) { |
|
263 | 263 | // no room to make color lighter or darker... |
|
264 | 264 | qDebug() << "cannot generate a color for pie!"; |
|
265 | 265 | break; |
|
266 | 266 | } |
|
267 | 267 | |
|
268 | 268 | // generate random color |
|
269 | 269 | int r = c.red() - dMax; |
|
270 | 270 | int g = c.green() - dMax; |
|
271 | 271 | int b = c.blue() - dMax; |
|
272 | 272 | int d = qrand() % max; |
|
273 | 273 | c.setRgb(r+d, g+d, b+d); |
|
274 | 274 | |
|
275 | 275 | // found a unique color? |
|
276 | 276 | if (!colors.contains(c)) |
|
277 | 277 | break; |
|
278 | 278 | } |
|
279 | 279 | |
|
280 | 280 | qDebug() << "generated a color for pie" << c; |
|
281 | 281 | colors << c; |
|
282 | 282 | } |
|
283 | 283 | |
|
284 | 284 | // finally update colors |
|
285 | 285 | foreach (QPieSlice* s, series->slices()) { |
|
286 | 286 | QColor c = colors.takeFirst(); |
|
287 | 287 | s->setPen(c); |
|
288 | 288 | s->setBrush(c); |
|
289 | 289 | } |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | |
|
293 | 293 | void ChartTheme::decorate(QChartAxis* axis,AxisItem* item) |
|
294 | 294 | { |
|
295 | 295 | //TODO: dummy defults for now |
|
296 | 296 | axis->setLabelsBrush(Qt::black); |
|
297 | 297 | axis->setLabelsPen(Qt::NoPen); |
|
298 | 298 | axis->setShadesPen(Qt::NoPen); |
|
299 | 299 | axis->setShadesOpacity(0.5); |
|
300 | 300 | } |
|
301 | 301 | |
|
302 |
void ChartTheme::decorate(Spline |
|
|
302 | void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int count) | |
|
303 | 303 | { |
|
304 |
Q_ASSERT( |
|
|
304 | Q_ASSERT(item); | |
|
305 | 305 | Q_ASSERT(series); |
|
306 | 306 | |
|
307 | 307 | QPen pen; |
|
308 | 308 | if(pen != series->pen()){ |
|
309 |
|
|
|
309 | item->setPen(series->pen()); | |
|
310 | 310 | return; |
|
311 | 311 | } |
|
312 | 312 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
313 | 313 | pen.setWidthF(series->pen().widthF()); |
|
314 |
|
|
|
314 | item->setPen(pen); | |
|
315 | 315 | |
|
316 | 316 | // QColor color = m_seriesColor.at(count % m_seriesColor.size()); |
|
317 | 317 | // TODO: define alpha in the theme? or in the series? |
|
318 | 318 | //color.setAlpha(120); |
|
319 | 319 | |
|
320 | 320 | // QBrush brush(color, Qt::SolidPattern); |
|
321 | 321 | // presenter->m_markerBrush = brush; |
|
322 | 322 | |
|
323 | 323 | // QPen pen(brush, 3); |
|
324 | 324 | // pen.setColor(color); |
|
325 | 325 | // presenter->m_markerPen = pen; |
|
326 | 326 | } |
|
327 | 327 | |
|
328 | 328 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,58 +1,58 | |||
|
1 | 1 | #ifndef CHARTTHEME_H |
|
2 | 2 | #define CHARTTHEME_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qchart.h" |
|
6 | 6 | #include <QColor> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class ChartItem; |
|
11 | 11 | class QSeries; |
|
12 | 12 | class LineChartItem; |
|
13 | 13 | class QLineSeries; |
|
14 | 14 | class BarPresenter; |
|
15 | 15 | class QBarSeries; |
|
16 | 16 | class StackedBarPresenter; |
|
17 | 17 | class QStackedBarSeries; |
|
18 | 18 | class QPercentBarSeries; |
|
19 | 19 | class PercentBarPresenter; |
|
20 | 20 | class QScatterSeries; |
|
21 | 21 | class ScatterPresenter; |
|
22 | 22 | class PiePresenter; |
|
23 | 23 | class QPieSeries; |
|
24 |
class Spline |
|
|
24 | class SplineChartItem; | |
|
25 | 25 | class QSplineSeries; |
|
26 | 26 | class AreaChartItem; |
|
27 | 27 | class QAreaSeries; |
|
28 | 28 | |
|
29 | 29 | class ChartTheme |
|
30 | 30 | { |
|
31 | 31 | protected: |
|
32 | 32 | explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault); |
|
33 | 33 | public: |
|
34 | 34 | static ChartTheme* createTheme(QChart::ChartTheme theme); |
|
35 | 35 | QChart::ChartTheme id() const {return m_id;} |
|
36 | 36 | void decorate(QChart* chart); |
|
37 | 37 | void decorate(ChartItem* item, QSeries* series,int count); |
|
38 | 38 | void decorate(BarPresenter* item, QBarSeries* series,int count); |
|
39 | 39 | void decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count); |
|
40 | 40 | void decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count); |
|
41 | 41 | void decorate(LineChartItem* item, QLineSeries* series,int count); |
|
42 | 42 | void decorate(AreaChartItem* item, QAreaSeries* series,int count); |
|
43 | 43 | void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count); |
|
44 | 44 | void decorate(PiePresenter* item, QPieSeries* series, int count); |
|
45 | 45 | void decorate(QChartAxis* axis,AxisItem* item); |
|
46 |
void decorate(Spline |
|
|
46 | void decorate(SplineChartItem* presenter, QSplineSeries* series, int count); | |
|
47 | 47 | |
|
48 | 48 | protected: |
|
49 | 49 | QChart::ChartTheme m_id; |
|
50 | 50 | QColor m_gradientStartColor; |
|
51 | 51 | QColor m_gradientEndColor; |
|
52 | 52 | QList<QColor> m_seriesColor; |
|
53 | 53 | |
|
54 | 54 | }; |
|
55 | 55 | |
|
56 | 56 | QTCOMMERCIALCHART_END_NAMESPACE |
|
57 | 57 | |
|
58 | 58 | #endif // CHARTTHEME_H |
@@ -1,12 +1,12 | |||
|
1 | 1 | INCLUDEPATH += $$PWD |
|
2 | 2 | DEPENDPATH += $$PWD |
|
3 | 3 | |
|
4 | 4 | SOURCES += \ |
|
5 | 5 | $$PWD/qsplineseries.cpp \ |
|
6 |
$$PWD/spline |
|
|
6 | $$PWD/splinechartitem.cpp | |
|
7 | 7 | |
|
8 | 8 | PRIVATE_HEADERS += \ |
|
9 |
$$PWD/spline |
|
|
9 | $$PWD/splinechartitem_p.h | |
|
10 | 10 | |
|
11 | 11 | PUBLIC_HEADERS += \ |
|
12 | 12 | $$PWD/qsplineseries.h |
@@ -1,75 +1,75 | |||
|
1 |
#include "spline |
|
|
1 | #include "splinechartitem_p.h" | |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | |
|
4 | 4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | 5 | |
|
6 |
Spline |
|
|
6 | SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsObject *parent) : | |
|
7 | 7 | LineChartItem(series, parent)//,m_boundingRect() |
|
8 | 8 | { |
|
9 | 9 | // |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | |
|
14 |
QPointF Spline |
|
|
14 | QPointF SplineChartItem::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 |
void Spline |
|
|
24 | void SplineChartItem::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 | prepareGeometryChange(); |
|
40 | 40 | m_path = splinePath; |
|
41 | 41 | m_rect = splinePath.boundingRect(); |
|
42 | 42 | } |
|
43 | 43 | |
|
44 |
void Spline |
|
|
44 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
|
45 | 45 | { |
|
46 | 46 | Q_UNUSED(widget); |
|
47 | 47 | Q_UNUSED(option); |
|
48 | 48 | painter->save(); |
|
49 | 49 | painter->setPen(m_pen); |
|
50 | 50 | painter->setClipRect(m_clipRect); |
|
51 | 51 | painter->drawPath(m_path); |
|
52 | 52 | |
|
53 | 53 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
54 | 54 | for (int i = 0; i < m_points.size() - 1; i++) |
|
55 | 55 | { |
|
56 | 56 | painter->setPen(Qt::red); |
|
57 | 57 | painter->drawEllipse(m_points[i], 2, 2); |
|
58 | 58 | |
|
59 | 59 | painter->setPen(Qt::blue); |
|
60 | 60 | // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); |
|
61 | 61 | // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); |
|
62 | 62 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); |
|
63 | 63 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); |
|
64 | 64 | } |
|
65 | 65 | if (m_points.count() > 0) |
|
66 | 66 | { |
|
67 | 67 | painter->setPen(Qt::red); |
|
68 | 68 | painter->drawEllipse(m_points[m_points.count() - 1], 2, 2); |
|
69 | 69 | } |
|
70 | 70 | painter->restore(); |
|
71 | 71 | } |
|
72 | 72 | |
|
73 |
#include "moc_spline |
|
|
73 | #include "moc_splinechartitem_p.cpp" | |
|
74 | 74 | |
|
75 | 75 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,28 +1,28 | |||
|
1 |
#ifndef SPLINE |
|
|
2 |
#define SPLINE |
|
|
1 | #ifndef SPLINECHARTITEM_P_H | |
|
2 | #define SPLINECHARTITEM_P_H | |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include "qsplineseries.h" |
|
7 | 7 | #include "linechartitem_p.h" |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 |
class Spline |
|
|
11 | class SplineChartItem : public LineChartItem | |
|
12 | 12 | { |
|
13 | 13 | Q_OBJECT |
|
14 | 14 | public: |
|
15 |
Spline |
|
|
15 | SplineChartItem(QSplineSeries* series, QGraphicsObject *parent = 0); | |
|
16 | 16 | |
|
17 | 17 | void updateGeometry(); |
|
18 | 18 | |
|
19 | 19 | void applyGeometry(QVector<QPointF>& points); |
|
20 | 20 | |
|
21 | 21 | QPointF calculateGeometryControlPoint(int index) const; |
|
22 | 22 | |
|
23 | 23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
24 | 24 | }; |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_END_NAMESPACE |
|
27 | 27 | |
|
28 |
#endif // SPLINE |
|
|
28 | #endif // SPLINECHARTITEM_P_H |
General Comments 0
You need to be logged in to leave comments.
Login now