@@ -1,78 +1,81 | |||
|
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 |
backgroundGradient.setColorAt(1.0, Q |
|
|
45 | backgroundGradient.setColorAt(1.0, Qt::white); | |
|
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 |
axisX->setGridLinePen( |
|
|
51 | axisX->setGridLinePen(red); | |
|
52 | axisX->setGridLineVisible(false); | |
|
53 | axisX->setShadesVisible(true); | |
|
52 | 54 | |
|
53 | 55 | QChartAxisCategories* categoriesX = axisX->categories(); |
|
54 | 56 | categoriesX->insert(0,"low"); |
|
55 | 57 | categoriesX->insert(50,"medium"); |
|
56 | 58 | categoriesX->insert(100,"High"); |
|
57 | 59 | |
|
58 | axisX->setMin(-10); | |
|
59 | axisX->setMax(2200); | |
|
60 | // axisX->setMin(-10); | |
|
61 | // axisX->setMax(2200); | |
|
60 | 62 | |
|
61 | 63 | QChartAxis* axisY = chartView->axisY(); |
|
62 | 64 | axisY->setLabelsAngle(45); |
|
63 | 65 | axisY->setShadesBrush(Qt::yellow); |
|
66 | axisY->setGridLineVisible(false); | |
|
64 | 67 | |
|
65 | 68 | QChartAxisCategories* categoriesY = axisY->categories(); |
|
66 | 69 | categoriesY->insert(0,"low"); |
|
67 | 70 | categoriesY->insert(50,"medium"); |
|
68 | 71 | categoriesY->insert(100,"High"); |
|
69 | 72 | |
|
70 | axisY->setMin(-10); | |
|
71 | axisY->setMax(200); | |
|
73 | // axisY->setMin(-10); | |
|
74 | // axisY->setMax(200); | |
|
72 | 75 | |
|
73 | 76 | window.setCentralWidget(chartView); |
|
74 | 77 | window.resize(400, 300); |
|
75 | 78 | window.show(); |
|
76 | 79 | |
|
77 | 80 | return a.exec(); |
|
78 | 81 | } |
@@ -1,203 +1,202 | |||
|
1 | 1 | #include "qchartglobal.h" |
|
2 | 2 | #include "qlegend.h" |
|
3 | 3 | #include "qseries.h" |
|
4 | 4 | #include "legendmarker_p.h" |
|
5 | 5 | #include "qxyseries.h" |
|
6 | 6 | #include "qlineseries.h" |
|
7 | 7 | #include "qareaseries.h" |
|
8 | 8 | #include "qscatterseries.h" |
|
9 | 9 | #include "qsplineseries.h" |
|
10 | 10 | #include "qbarseries.h" |
|
11 | 11 | #include "qstackedbarseries.h" |
|
12 | 12 | #include "qpercentbarseries.h" |
|
13 | 13 | #include "qbarset.h" |
|
14 | 14 | #include "qpieseries.h" |
|
15 | 15 | #include "qpieslice.h" |
|
16 | 16 | #include <QPainter> |
|
17 | 17 | #include <QPen> |
|
18 | 18 | |
|
19 | 19 | #include <QGraphicsSceneEvent> |
|
20 | 20 | |
|
21 | 21 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
22 | 22 | |
|
23 | 23 | QLegend::QLegend(QGraphicsItem *parent) |
|
24 | 24 | : QGraphicsObject(parent) |
|
25 | 25 | ,mBoundingRect(0,0,1,1) |
|
26 | 26 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? |
|
27 | 27 | { |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
31 | 31 | { |
|
32 | 32 | painter->setBrush(mBackgroundBrush); |
|
33 | 33 | painter->drawRect(mBoundingRect); |
|
34 | 34 | |
|
35 | 35 | foreach(LegendMarker* m, mMarkers) { |
|
36 | 36 | QRectF r = m->boundingRect(); |
|
37 | 37 | painter->setBrush(m->brush()); |
|
38 | 38 | painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name()); |
|
39 | 39 | } |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | QRectF QLegend::boundingRect() const |
|
43 | 43 | { |
|
44 | 44 | return mBoundingRect; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void QLegend::setBackgroundBrush(const QBrush& brush) |
|
48 | 48 | { |
|
49 | 49 | mBackgroundBrush = brush; |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | QBrush QLegend::backgroundBrush() const |
|
53 | 53 | { |
|
54 | 54 | return mBackgroundBrush; |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) |
|
58 | 58 | { |
|
59 | 59 | mSeriesList.append(series); |
|
60 | 60 | |
|
61 | 61 | switch (series->type()) |
|
62 | 62 | { |
|
63 | 63 | case QSeries::SeriesTypeLine: { |
|
64 | 64 | |
|
65 | 65 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
66 | 66 | createMarker(lineSeries); |
|
67 | 67 | break; |
|
68 | 68 | } |
|
69 | 69 | case QSeries::SeriesTypeArea: { |
|
70 | 70 | |
|
71 | 71 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
72 | 72 | createMarker(areaSeries->upperSeries()); |
|
73 | if(areaSeries->lowerSeries()) | |
|
73 | 74 | createMarker(areaSeries->lowerSeries()); |
|
74 | 75 | break; |
|
75 | 76 | } |
|
76 | 77 | |
|
77 | 78 | case QSeries::SeriesTypeBar: { |
|
78 | 79 | |
|
79 | 80 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
80 | 81 | createMarkers(barSeries); |
|
81 | 82 | break; |
|
82 | 83 | } |
|
83 | 84 | |
|
84 | 85 | case QSeries::SeriesTypeStackedBar: { |
|
85 | 86 | |
|
86 | 87 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
87 | 88 | createMarkers(stackedBarSeries); |
|
88 | 89 | break; |
|
89 | 90 | } |
|
90 | 91 | |
|
91 | 92 | case QSeries::SeriesTypePercentBar: { |
|
92 | 93 | |
|
93 | 94 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
94 | 95 | createMarkers(percentBarSeries); |
|
95 | 96 | break; |
|
96 | 97 | } |
|
97 | 98 | |
|
98 | 99 | case QSeries::SeriesTypeScatter: { |
|
99 | 100 | |
|
100 | 101 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
101 | 102 | createMarker(scatterSeries); |
|
102 | 103 | break; |
|
103 | 104 | } |
|
104 | 105 | |
|
105 | 106 | case QSeries::SeriesTypePie: { |
|
106 | 107 | |
|
107 | 108 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
108 | 109 | createMarkers(pieSeries); |
|
109 | 110 | break; |
|
110 | 111 | } |
|
111 | 112 | |
|
112 | 113 | case QSeries::SeriesTypeSpline: { |
|
113 | 114 | |
|
114 | 115 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
115 | 116 | break; |
|
116 | 117 | } |
|
117 | 118 | default: { |
|
118 | 119 | qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented."; |
|
119 | 120 | break; |
|
120 | 121 | } |
|
121 | 122 | } |
|
122 | 123 | |
|
123 | 124 | layoutChanged(); |
|
124 | 125 | } |
|
125 | 126 | |
|
126 | 127 | void QLegend::handleSeriesRemoved(QSeries* series) |
|
127 | 128 | { |
|
128 | 129 | // TODO: delete markers, disconnect. |
|
129 | 130 | mSeriesList.removeOne(series); |
|
130 | 131 | layoutChanged(); |
|
131 | 132 | } |
|
132 | 133 | |
|
133 | 134 | void QLegend::handleGeometryChanged(const QRectF& size) |
|
134 | 135 | { |
|
135 | 136 | mBoundingRect = size; |
|
136 | 137 | layoutChanged(); |
|
137 | 138 | } |
|
138 | 139 | |
|
139 | 140 | void QLegend::createMarker(QXYSeries* series) |
|
140 | 141 | { |
|
141 | 142 | LegendMarker* marker = new LegendMarker(series,this); |
|
142 | 143 | marker->setName(series->name()); |
|
143 | 144 | marker->setBrush(series->brush()); |
|
144 | 145 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); |
|
145 | 146 | mMarkers.append(marker); |
|
146 | 147 | childItems().append(marker); |
|
147 | 148 | } |
|
148 | 149 | |
|
149 | 150 | void QLegend::createMarkers(QBarSeries *series) |
|
150 | 151 | { |
|
151 | 152 | foreach(QBarSet* s, series->barSets()) { |
|
152 | 153 | LegendMarker* marker = new LegendMarker(s,this); |
|
153 | 154 | marker->setName(s->name()); |
|
154 | 155 | marker->setBrush(s->brush()); |
|
155 | 156 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); |
|
156 | 157 | mMarkers.append(marker); |
|
157 | 158 | childItems().append(marker); |
|
158 | 159 | } |
|
159 | 160 | } |
|
160 | 161 | |
|
161 | 162 | void QLegend::createMarkers(QPieSeries *series) |
|
162 | 163 | { |
|
163 | 164 | foreach(QPieSlice* s, series->slices()) { |
|
164 | 165 | LegendMarker* marker = new LegendMarker(s,this); |
|
165 | 166 | marker->setName(s->label()); |
|
166 | 167 | marker->setBrush(s->sliceBrush()); |
|
167 | 168 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); |
|
168 | 169 | mMarkers.append(marker); |
|
169 | 170 | childItems().append(marker); |
|
170 | 171 | } |
|
171 | 172 | } |
|
172 | 173 | |
|
173 | 174 | void QLegend::layoutChanged() |
|
174 | 175 | { |
|
175 | 176 | // Calculate layout for markers and text |
|
176 | 177 | if (mMarkers.count() <= 0) { |
|
177 | 178 | // Nothing to do |
|
178 | 179 | return; |
|
179 | 180 | } |
|
180 | 181 | |
|
181 | 182 | // TODO: marker defined by series. |
|
182 | 183 | QSizeF markerSize(10,10); |
|
183 | 184 | |
|
184 | 185 | // TODO: better layout, this is just concept. |
|
185 | 186 | // Leave some space around markers like this: | x x x x | |
|
186 | 187 | qreal steps = mMarkers.count(); |
|
187 | 188 | |
|
188 | 189 | qreal xStep = mBoundingRect.width() / steps; |
|
189 | 190 | qreal yStep = mBoundingRect.height() / steps; |
|
190 | 191 | qreal x = mBoundingRect.x() + 5; |
|
191 | 192 | qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2; |
|
192 | 193 | foreach (LegendMarker* m, mMarkers) { |
|
193 | qDebug() << "marker x:" << x; | |
|
194 | qDebug() << "marker y:" << y; | |
|
195 | 194 | m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height())); |
|
196 | 195 | x += xStep; |
|
197 | 196 | } |
|
198 | 197 | } |
|
199 | 198 | |
|
200 | 199 | |
|
201 | 200 | |
|
202 | 201 | #include "moc_qlegend.cpp" |
|
203 | 202 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now