@@ -1,40 +1,40 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartglobal.h> |
|
4 | 4 | #include <qchartview.h> |
|
5 | 5 | #include <qpieseries.h> |
|
6 | 6 | #include <qpieslice.h> |
|
7 | 7 | #include "customslice.h" |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | int main(int argc, char *argv[]) |
|
12 | 12 | { |
|
13 | 13 | QApplication a(argc, argv); |
|
14 | 14 | |
|
15 | 15 | QMainWindow window; |
|
16 | 16 | |
|
17 | 17 | QPieSeries *series = new QPieSeries(); |
|
18 | 18 | series->add(5, "Slice 1"); |
|
19 | 19 | series->add(2, "Slice 2"); |
|
20 | 20 | series->add(3, "Slice 3"); |
|
21 | 21 | series->add(4, "Slice 4"); |
|
22 | 22 | series->add(5, "Slice 5"); |
|
23 | 23 | series->add(6, "Slice 6"); |
|
24 | 24 | series->add(7, "Slice 7"); |
|
25 | 25 | series->add(new CustomSlice(8)); |
|
26 | 26 | series->enableClickExplodes(true); |
|
27 | 27 | series->enableHoverHighlight(true); |
|
28 | 28 | |
|
29 | 29 | QChartView* chartView = new QChartView(&window); |
|
30 | 30 | chartView->setRenderHint(QPainter::Antialiasing); |
|
31 | chartView->addSeries(series); | |
|
32 | chartView->setChartTitle("simple piechart"); | |
|
33 | 31 | chartView->setChartTheme(QChart::ChartThemeIcy); |
|
32 | chartView->setChartTitle("Simple piechart"); | |
|
33 | chartView->addSeries(series); | |
|
34 | 34 | |
|
35 | 35 | window.setCentralWidget(chartView); |
|
36 | 36 | window.resize(600, 600); |
|
37 | 37 | window.show(); |
|
38 | 38 | |
|
39 | 39 | return a.exec(); |
|
40 | 40 | } |
@@ -1,226 +1,241 | |||
|
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 "qbarchartseries.h" |
|
8 | 8 | #include "qstackedbarchartseries.h" |
|
9 | 9 | #include "qpercentbarchartseries.h" |
|
10 | 10 | #include "qlinechartseries.h" |
|
11 | 11 | #include "qpieseries.h" |
|
12 | 12 | #include "qscatterseries.h" |
|
13 | 13 | //items |
|
14 | 14 | #include "axisitem_p.h" |
|
15 | 15 | #include "barpresenter.h" |
|
16 | 16 | #include "stackedbarpresenter.h" |
|
17 | 17 | #include "linechartitem_p.h" |
|
18 | 18 | #include "percentbarpresenter.h" |
|
19 | 19 | #include "linechartanimationitem_p.h" |
|
20 | 20 | #include "piepresenter.h" |
|
21 | 21 | #include "scatterpresenter_p.h" |
|
22 | 22 | |
|
23 | 23 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
24 | 24 | |
|
25 | 25 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
26 | 26 | m_chart(chart), |
|
27 | 27 | m_dataset(dataset), |
|
28 | 28 | m_chartTheme(0), |
|
29 | 29 | m_marginSize(0), |
|
30 | 30 | m_rect(QRectF(QPoint(0,0),m_chart->size())) |
|
31 | 31 | { |
|
32 | 32 | createConnections(); |
|
33 | 33 | setChartTheme(QChart::ChartThemeDefault); |
|
34 | 34 | |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | ChartPresenter::~ChartPresenter() |
|
38 | 38 | { |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | void ChartPresenter::createConnections() |
|
42 | 42 | { |
|
43 | 43 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); |
|
44 | 44 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); |
|
45 | 45 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*))); |
|
46 | 46 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); |
|
47 | 47 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); |
|
48 | 48 | QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&))); |
|
49 | 49 | QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&))); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | QRectF ChartPresenter::geometry() const |
|
54 | 54 | { |
|
55 | 55 | return m_rect; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void ChartPresenter::handleGeometryChanged() |
|
59 | 59 | { |
|
60 | 60 | m_rect = QRectF(QPoint(0,0),m_chart->size()); |
|
61 | 61 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); |
|
62 | 62 | Q_ASSERT(m_rect.isValid()); |
|
63 | 63 | emit geometryChanged(m_rect); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | int ChartPresenter::margin() const |
|
67 | 67 | { |
|
68 | 68 | return m_marginSize; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void ChartPresenter::setMargin(int margin) |
|
72 | 72 | { |
|
73 | 73 | m_marginSize = margin; |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | void ChartPresenter::handleAxisAdded(QChartAxis* axis) |
|
77 | 77 | { |
|
78 | 78 | AxisItem* item ; |
|
79 | 79 | |
|
80 | 80 | if(axis==m_dataset->axisX()){ |
|
81 | 81 | item = new AxisItem(AxisItem::X_AXIS,m_chart); |
|
82 | 82 | }else{ |
|
83 | 83 | item = new AxisItem(AxisItem::Y_AXIS,m_chart); |
|
84 | 84 | } |
|
85 | 85 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
86 | 86 | QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); |
|
87 | 87 | |
|
88 | 88 | item->handleAxisUpdate(axis); |
|
89 | 89 | item->handleGeometryChanged(m_rect); |
|
90 | 90 | m_chartTheme->decorate(axis,item); |
|
91 | 91 | m_axisItems.insert(axis,item); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
95 | 95 | { |
|
96 | 96 | AxisItem* item = m_axisItems.take(axis); |
|
97 | 97 | Q_ASSERT(item); |
|
98 | 98 | delete item; |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | void ChartPresenter::handleSeriesAdded(QChartSeries* series) |
|
103 | 103 | { |
|
104 | 104 | switch(series->type()) |
|
105 | 105 | { |
|
106 | 106 | case QChartSeries::SeriesTypeLine: { |
|
107 | 107 | QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series); |
|
108 | 108 | LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart); |
|
109 | 109 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); |
|
110 | 110 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
111 | 111 | QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
112 | 112 | m_chartItems.insert(series,item); |
|
113 | 113 | break; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | case QChartSeries::SeriesTypeBar: { |
|
117 | 117 | QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series); |
|
118 | 118 | BarPresenter* item = new BarPresenter(barSeries,m_chart); |
|
119 | 119 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); |
|
120 | 120 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
121 | 121 | QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
122 | 122 | m_chartItems.insert(series,item); |
|
123 | 123 | // m_axisXItem->setVisible(false); |
|
124 | 124 | break; |
|
125 | 125 | } |
|
126 | 126 | |
|
127 | 127 | case QChartSeries::SeriesTypeStackedBar: { |
|
128 | 128 | |
|
129 | 129 | QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series); |
|
130 | 130 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); |
|
131 | 131 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); |
|
132 | 132 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
133 | 133 | QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
134 | 134 | m_chartItems.insert(series,item); |
|
135 | 135 | break; |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | case QChartSeries::SeriesTypePercentBar: { |
|
139 | 139 | |
|
140 | 140 | QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series); |
|
141 | 141 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); |
|
142 | 142 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); |
|
143 | 143 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
144 | 144 | QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
145 | 145 | m_chartItems.insert(series,item); |
|
146 | 146 | break; |
|
147 | 147 | } |
|
148 | 148 | case QChartSeries::SeriesTypeScatter: { |
|
149 | 149 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
150 | 150 | ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); |
|
151 | 151 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), |
|
152 | 152 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
153 | 153 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); |
|
154 | 154 | m_chartItems.insert(scatterSeries, scatterPresenter); |
|
155 | 155 | break; |
|
156 | 156 | } |
|
157 | 157 | case QChartSeries::SeriesTypePie: { |
|
158 | 158 | QPieSeries *s = qobject_cast<QPieSeries *>(series); |
|
159 | 159 | PiePresenter* pie = new PiePresenter(m_chart, s); |
|
160 | 160 | m_chartTheme->decorate(pie, s, m_chartItems.count()); |
|
161 | 161 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); |
|
162 | ||
|
163 | // Hide all from background when there is only piechart | |
|
164 | // TODO: refactor this ugly code... should be one setting for this | |
|
165 | if (m_chartItems.count() == 0) { | |
|
166 | m_chart->axisX()->setAxisVisible(false); | |
|
167 | m_chart->axisY()->setAxisVisible(false); | |
|
168 | m_chart->axisX()->setGridVisible(false); | |
|
169 | m_chart->axisY()->setGridVisible(false); | |
|
170 | m_chart->axisX()->setLabelsVisible(false); | |
|
171 | m_chart->axisY()->setLabelsVisible(false); | |
|
172 | m_chart->axisX()->setShadesVisible(false); | |
|
173 | m_chart->axisY()->setShadesVisible(false); | |
|
174 | m_chart->setChartBackgroundBrush(Qt::transparent); | |
|
175 | } | |
|
176 | ||
|
162 | 177 | m_chartItems.insert(series, pie); |
|
163 | 178 | break; |
|
164 | 179 | } |
|
165 | 180 | default: { |
|
166 | 181 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
167 | 182 | break; |
|
168 | 183 | } |
|
169 | 184 | } |
|
170 | 185 | |
|
171 | 186 | if(m_rect.isValid()) emit geometryChanged(m_rect); |
|
172 | 187 | } |
|
173 | 188 | |
|
174 | 189 | void ChartPresenter::handleSeriesRemoved(QChartSeries* series) |
|
175 | 190 | { |
|
176 | 191 | ChartItem* item = m_chartItems.take(series); |
|
177 | 192 | delete item; |
|
178 | 193 | } |
|
179 | 194 | |
|
180 | 195 | void ChartPresenter::handleSeriesChanged(QChartSeries* series) |
|
181 | 196 | { |
|
182 | 197 | //TODO: |
|
183 | 198 | } |
|
184 | 199 | |
|
185 | 200 | void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain) |
|
186 | 201 | { |
|
187 | 202 | m_chartItems.value(series)->handleDomainChanged(domain); |
|
188 | 203 | } |
|
189 | 204 | |
|
190 | 205 | void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels) |
|
191 | 206 | { |
|
192 | 207 | m_axisItems.value(axis)->handleLabelsChanged(axis,labels); |
|
193 | 208 | } |
|
194 | 209 | |
|
195 | 210 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) |
|
196 | 211 | { |
|
197 | 212 | delete m_chartTheme; |
|
198 | 213 | |
|
199 | 214 | m_chartTheme = ChartTheme::createTheme(theme); |
|
200 | 215 | |
|
201 | 216 | m_chartTheme->decorate(m_chart); |
|
202 | 217 | QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems); |
|
203 | 218 | |
|
204 | 219 | int index=0; |
|
205 | 220 | while (i.hasNext()) { |
|
206 | 221 | i.next(); |
|
207 | 222 | index++; |
|
208 | 223 | m_chartTheme->decorate(i.value(),i.key(),index); |
|
209 | 224 | } |
|
210 | 225 | |
|
211 | 226 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); |
|
212 | 227 | while (j.hasNext()) { |
|
213 | 228 | j.next(); |
|
214 | 229 | m_chartTheme->decorate(j.key(),j.value()); |
|
215 | 230 | } |
|
216 | 231 | } |
|
217 | 232 | |
|
218 | 233 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
219 | 234 | { |
|
220 | 235 | return m_chartTheme->id(); |
|
221 | 236 | } |
|
222 | 237 | |
|
223 | 238 | |
|
224 | 239 | #include "moc_chartpresenter_p.cpp" |
|
225 | 240 | |
|
226 | 241 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now