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