##// END OF EJS Templates
Fixed colors in spline
Marek Rosa -
r577:d1f00b1c0524
parent child
Show More
@@ -1,102 +1,114
1 #include "splinewidget.h"
1 #include "splinewidget.h"
2 #include "qchartview.h"
2 #include "qchartview.h"
3 #include "qlineseries.h"
3 #include "qlineseries.h"
4 #include <QGridLayout>
4 #include <QGridLayout>
5 #include <QPushButton>
5 #include <QPushButton>
6 #include "qchartaxis.h"
6 #include "qchartaxis.h"
7 #include <qmath.h>
7 #include <qmath.h>
8
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
10
11 SplineWidget::SplineWidget(QWidget *parent)
11 SplineWidget::SplineWidget(QWidget *parent)
12 : QWidget(parent)
12 : QWidget(parent)
13 {
13 {
14 // qsrand(time(NULL));
14 // qsrand(time(NULL));
15 //! [1]
15 //! [1]
16 //create QSplineSeries
16 //create QSplineSeries
17 series = new QSplineSeries;
17 series = new QSplineSeries(this);
18 //! [1]
18 //! [1]
19
19
20 //! [2]
20 //! [2]
21 // customize the series presentation settings
21 // customize the series presentation settings
22 QPen seriesPen(Qt::blue);
22 QPen seriesPen(Qt::blue);
23 seriesPen.setWidth(3);
23 seriesPen.setWidth(3);
24 series->setPen(seriesPen);
24 // series->setPen(seriesPen);
25 //! [2]
25 //! [2]
26
26
27 //! [add points to series]
27 //! [add points to series]
28 //add data points to the series
28 //add data points to the series
29 series->add(QPointF(150, 100));
29 series->add(QPointF(150, 100));
30 series->add(QPointF(200, 130));
30 series->add(QPointF(200, 130));
31 series->add(QPointF(250, 120));
31 series->add(QPointF(250, 120));
32 series->add(QPointF(300, 140));
32 series->add(QPointF(300, 140));
33 series->add(QPointF(350, 160));
33 series->add(QPointF(350, 160));
34 //! [add points to series]
34 //! [add points to series]
35 // series->add(QPointF(400, 120));
35
36 // series->add(QPointF(450, 150));
36 QSplineSeries* series2 = new QSplineSeries;
37 // series->add(QPointF(500, 145));
37
38 // series->add(QPointF(550, 170));
38 series2->add(QPointF(400, 120));
39 series2->add(QPointF(450, 150));
40 series2->add(QPointF(500, 145));
41 series2->add(QPointF(550, 170));
42
39 // series->add(QPointF(600, 190));
43 // series->add(QPointF(600, 190));
40 // series->add(QPointF(650, 210));
44 // series->add(QPointF(650, 210));
41 // series->add(QPointF(700, 190));
45 // series->add(QPointF(700, 190));
42 // series->add(QPointF(750, 180));
46 // series->add(QPointF(750, 180));
43 // series->add(QPointF(800, 170));
47 // series->add(QPointF(800, 170));
48 QSplineSeries* series3 = new QSplineSeries;
49 series3->add(QPointF(600, 190));
50 series3->add(QPointF(650, 210));
51 series3->add(QPointF(700, 190));
52 series3->add(QPointF(750, 180));
53 series3->add(QPointF(800, 170));
44
54
45 //! [3]
55 //! [3]
46 // create chart view
56 // create chart view
47 QChartView* chart = new QChartView;
57 QChartView* chart = new QChartView;
48 chart->addSeries(series);
58 chart->addSeries(series);
59 chart->addSeries(series2);
60 chart->addSeries(series3);
49
61
50 chart->setChartTitle("Spline chart example");
62 chart->setChartTitle("Spline chart example");
51 chart->axisX()->setMax(1500);
63 chart->axisX()->setMax(1500);
52 chart->axisY()->setMax(500);
64 chart->axisY()->setMax(500);
53
65
54 chart->setMinimumSize(800,600);
66 chart->setMinimumSize(800,600);
55 //! [3]
67 //! [3]
56
68
57 //! [4]
69 //! [4]
58 //add new data point button
70 //add new data point button
59 QPushButton* addButton = new QPushButton("Add new point");
71 QPushButton* addButton = new QPushButton("Add new point");
60 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
72 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
61
73
62 // remove the last data point in the series
74 // remove the last data point in the series
63 QPushButton* removeButton = new QPushButton("Remove point");
75 QPushButton* removeButton = new QPushButton("Remove point");
64 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
76 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
65 //! [4]
77 //! [4]
66
78
67 //! [5]
79 //! [5]
68 //butttons layout
80 //butttons layout
69 QVBoxLayout* buttonsLayout = new QVBoxLayout;
81 QVBoxLayout* buttonsLayout = new QVBoxLayout;
70 buttonsLayout->addWidget(addButton);
82 buttonsLayout->addWidget(addButton);
71 buttonsLayout->addWidget(removeButton);
83 buttonsLayout->addWidget(removeButton);
72 buttonsLayout->addStretch();
84 buttonsLayout->addStretch();
73
85
74 QGridLayout* mainLayout = new QGridLayout;
86 QGridLayout* mainLayout = new QGridLayout;
75 mainLayout->addWidget(chart, 1, 0);
87 mainLayout->addWidget(chart, 1, 0);
76 mainLayout->addLayout(buttonsLayout, 1, 1);
88 mainLayout->addLayout(buttonsLayout, 1, 1);
77 setLayout(mainLayout);
89 setLayout(mainLayout);
78 //! [5]
90 //! [5]
79 }
91 }
80
92
81 //! [add point]
93 //! [add point]
82 void SplineWidget::addNewPoint()
94 void SplineWidget::addNewPoint()
83 {
95 {
84 if (series->count() > 0)
96 if (series->count() > 0)
85 series->add(QPointF(series->x(series->count() - 1) + 40 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100)));
97 series->add(QPointF(series->x(series->count() - 1) + 40 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100)));
86 else
98 else
87 series->add(QPointF(50, 50 + qrand()%50));
99 series->add(QPointF(50, 50 + qrand()%50));
88 }
100 }
89 //! [add point]
101 //! [add point]
90
102
91 //! [remove point]
103 //! [remove point]
92 void SplineWidget::removePoint()
104 void SplineWidget::removePoint()
93 {
105 {
94 if (series->count() > 0)
106 if (series->count() > 0)
95 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
107 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
96 }
108 }
97 //! [remove point]
109 //! [remove point]
98
110
99 SplineWidget::~SplineWidget()
111 SplineWidget::~SplineWidget()
100 {
112 {
101
113
102 }
114 }
@@ -1,305 +1,304
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qlegend.h"
3 #include "qlegend.h"
4 #include "qchartaxis.h"
4 #include "qchartaxis.h"
5 #include <QTime>
5 #include <QTime>
6
6
7 //series
7 //series
8 #include "qbarset.h"
8 #include "qbarset.h"
9 #include "qbarseries.h"
9 #include "qbarseries.h"
10 #include "qstackedbarseries.h"
10 #include "qstackedbarseries.h"
11 #include "qpercentbarseries.h"
11 #include "qpercentbarseries.h"
12 #include "qlineseries.h"
12 #include "qlineseries.h"
13 #include "qareaseries.h"
13 #include "qareaseries.h"
14 #include "qscatterseries.h"
14 #include "qscatterseries.h"
15 #include "qpieseries.h"
15 #include "qpieseries.h"
16 #include "qpieslice.h"
16 #include "qpieslice.h"
17 #include "qsplineseries.h"
17 #include "qsplineseries.h"
18
18
19 //items
19 //items
20 #include "axisitem_p.h"
20 #include "axisitem_p.h"
21 #include "barpresenter_p.h"
21 #include "barpresenter_p.h"
22 #include "stackedbarpresenter_p.h"
22 #include "stackedbarpresenter_p.h"
23 #include "percentbarpresenter_p.h"
23 #include "percentbarpresenter_p.h"
24 #include "linechartitem_p.h"
24 #include "linechartitem_p.h"
25 #include "areachartitem_p.h"
25 #include "areachartitem_p.h"
26 #include "scatterchartitem_p.h"
26 #include "scatterchartitem_p.h"
27 #include "piechartitem_p.h"
27 #include "piechartitem_p.h"
28 #include "splinechartitem_p.h"
28 #include "splinechartitem_p.h"
29
29
30 //themes
30 //themes
31 #include "chartthemedefault_p.h"
31 #include "chartthemedefault_p.h"
32 #include "chartthemevanilla_p.h"
32 #include "chartthemevanilla_p.h"
33 #include "chartthemeicy_p.h"
33 #include "chartthemeicy_p.h"
34 #include "chartthemegrayscale_p.h"
34 #include "chartthemegrayscale_p.h"
35 #include "chartthemescientific_p.h"
35 #include "chartthemescientific_p.h"
36
36
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 ChartTheme::ChartTheme(QChart::ChartTheme id)
40 ChartTheme::ChartTheme(QChart::ChartTheme id)
41 {
41 {
42 m_id = id;
42 m_id = id;
43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 }
44 }
45
45
46
46
47 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
47 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
48 {
48 {
49 switch(theme) {
49 switch(theme) {
50 case QChart::ChartThemeVanilla:
50 case QChart::ChartThemeVanilla:
51 return new ChartThemeVanilla();
51 return new ChartThemeVanilla();
52 case QChart::ChartThemeIcy:
52 case QChart::ChartThemeIcy:
53 return new ChartThemeIcy();
53 return new ChartThemeIcy();
54 case QChart::ChartThemeGrayscale:
54 case QChart::ChartThemeGrayscale:
55 return new ChartThemeGrayscale();
55 return new ChartThemeGrayscale();
56 case QChart::ChartThemeScientific:
56 case QChart::ChartThemeScientific:
57 return new ChartThemeScientific();
57 return new ChartThemeScientific();
58 default:
58 default:
59 return new ChartThemeDefault();
59 return new ChartThemeDefault();
60 }
60 }
61 }
61 }
62
62
63 void ChartTheme::decorate(QChart* chart)
63 void ChartTheme::decorate(QChart* chart)
64 {
64 {
65 chart->setChartBackgroundBrush(m_backgroundGradient);
65 chart->setChartBackgroundBrush(m_backgroundGradient);
66 chart->setChartTitleFont(m_masterFont);
66 chart->setChartTitleFont(m_masterFont);
67 }
67 }
68
68
69 void ChartTheme::decorate(QLegend* legend)
69 void ChartTheme::decorate(QLegend* legend)
70 {
70 {
71 legend->setBackgroundBrush(m_backgroundGradient);
71 legend->setBackgroundBrush(m_backgroundGradient);
72 }
72 }
73
73
74 void ChartTheme::decorate(QAreaSeries* series, int index)
74 void ChartTheme::decorate(QAreaSeries* series, int index)
75 {
75 {
76 QPen pen;
76 QPen pen;
77 QBrush brush;
77 QBrush brush;
78
78
79 if (pen == series->pen()){
79 if (pen == series->pen()){
80 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
80 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
81 pen.setWidthF(2);
81 pen.setWidthF(2);
82 series->setPen(pen);
82 series->setPen(pen);
83 }
83 }
84
84
85 if (brush == series->brush()) {
85 if (brush == series->brush()) {
86 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
86 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
87 series->setBrush(brush);
87 series->setBrush(brush);
88 }
88 }
89 }
89 }
90
90
91
91
92 void ChartTheme::decorate(QLineSeries* series,int index)
92 void ChartTheme::decorate(QLineSeries* series,int index)
93 {
93 {
94 QPen pen;
94 QPen pen;
95 if(pen == series->pen()){
95 if(pen == series->pen()){
96 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
96 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
97 pen.setWidthF(2);
97 pen.setWidthF(2);
98 series->setPen(pen);
98 series->setPen(pen);
99 }
99 }
100 }
100 }
101
101
102 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
102 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
103 {
103 {
104 QList<QBarSet*> sets = series->barSets();
104 QList<QBarSet*> sets = series->barSets();
105 for (int i=0; i<sets.count(); i++) {
105 for (int i=0; i<sets.count(); i++) {
106 qreal pos = 0.5;
106 qreal pos = 0.5;
107 if (sets.count() > 1)
107 if (sets.count() > 1)
108 pos = (qreal) i / (qreal) (sets.count() - 1);
108 pos = (qreal) i / (qreal) (sets.count() - 1);
109 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
109 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
110 sets.at(i)->setBrush(QBrush(c));
110 sets.at(i)->setBrush(QBrush(c));
111
111
112 // Pick label color as far as possible from bar color (within gradient).
112 // Pick label color as far as possible from bar color (within gradient).
113 // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :)
113 // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :)
114 // TODO: better picking of label color?
114 // TODO: better picking of label color?
115 if (pos < 0.3) {
115 if (pos < 0.3) {
116 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
116 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
117 } else {
117 } else {
118 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
118 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
119 }
119 }
120 sets.at(i)->setFloatingValuePen(QPen(c));
120 sets.at(i)->setFloatingValuePen(QPen(c));
121 }
121 }
122 }
122 }
123
123
124 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index)
124 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index)
125 {
125 {
126 QList<QBarSet*> sets = series->barSets();
126 QList<QBarSet*> sets = series->barSets();
127 for (int i=0; i<sets.count(); i++) {
127 for (int i=0; i<sets.count(); i++) {
128 qreal pos = 0.5;
128 qreal pos = 0.5;
129 if (sets.count() > 1)
129 if (sets.count() > 1)
130 pos = (qreal) i / (qreal) (sets.count() - 1);
130 pos = (qreal) i / (qreal) (sets.count() - 1);
131 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
131 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
132 sets.at(i)->setBrush(QBrush(c));
132 sets.at(i)->setBrush(QBrush(c));
133
133
134 if (pos < 0.3) {
134 if (pos < 0.3) {
135 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
135 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
136 } else {
136 } else {
137 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
137 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
138 }
138 }
139 sets.at(i)->setFloatingValuePen(QPen(c));
139 sets.at(i)->setFloatingValuePen(QPen(c));
140 }
140 }
141 }
141 }
142
142
143 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index)
143 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index)
144 {
144 {
145 QList<QBarSet*> sets = series->barSets();
145 QList<QBarSet*> sets = series->barSets();
146 for (int i=0; i<sets.count(); i++) {
146 for (int i=0; i<sets.count(); i++) {
147 qreal pos = 0.5;
147 qreal pos = 0.5;
148 if (sets.count() > 1)
148 if (sets.count() > 1)
149 pos = (qreal) i / (qreal) (sets.count() - 1);
149 pos = (qreal) i / (qreal) (sets.count() - 1);
150 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
150 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
151 sets.at(i)->setBrush(QBrush(c));
151 sets.at(i)->setBrush(QBrush(c));
152
152
153 if (pos < 0.3) {
153 if (pos < 0.3) {
154 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
154 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
155 } else {
155 } else {
156 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
156 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
157 }
157 }
158 sets.at(i)->setFloatingValuePen(QPen(c));
158 sets.at(i)->setFloatingValuePen(QPen(c));
159 }
159 }
160 }
160 }
161
161
162 void ChartTheme::decorate(QScatterSeries* series, int index)
162 void ChartTheme::decorate(QScatterSeries* series, int index)
163 {
163 {
164
164
165 QPen pen;
165 QPen pen;
166 QBrush brush;
166 QBrush brush;
167
167
168 if (pen == series->pen()) {
168 if (pen == series->pen()) {
169 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
169 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
170 pen.setWidthF(2);
170 pen.setWidthF(2);
171 series->setPen(pen);
171 series->setPen(pen);
172 }
172 }
173
173
174 if (brush == series->brush()) {
174 if (brush == series->brush()) {
175 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
175 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
176 series->setBrush(brush);
176 series->setBrush(brush);
177 }
177 }
178 }
178 }
179
179
180 void ChartTheme::decorate(PieChartItem* item, QPieSeries* series, int index)
180 void ChartTheme::decorate(PieChartItem* item, QPieSeries* series, int index)
181 {
181 {
182 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
182 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
183 for (int i(0); i < series->slices().count(); i++) {
183 for (int i(0); i < series->slices().count(); i++) {
184 qreal pos = (qreal) i / (qreal) series->count();
184 qreal pos = (qreal) i / (qreal) series->count();
185 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
185 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
186 series->slices().at(i)->setSlicePen(penColor);
186 series->slices().at(i)->setSlicePen(penColor);
187 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
187 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
188 series->slices().at(i)->setSliceBrush(brushColor);
188 series->slices().at(i)->setSliceBrush(brushColor);
189 }
189 }
190 }
190 }
191
191
192 void ChartTheme::decorate(QSplineSeries* series, int index)
192 void ChartTheme::decorate(QSplineSeries* series, int index)
193 {
193 {
194 QPen pen;
194 QPen pen;
195
196 if(pen == series->pen()){
195 if(pen == series->pen()){
197 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
196 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
198 pen.setWidthF(2);
197 pen.setWidthF(2);
199 series->setPen(pen);
198 series->setPen(pen);
200 }
199 }
201 }
200 }
202
201
203 void ChartTheme::decorate(QChartAxis* axis,bool axisX)
202 void ChartTheme::decorate(QChartAxis* axis,bool axisX)
204 {
203 {
205 if (axis->isAxisVisible()) {
204 if (axis->isAxisVisible()) {
206 axis->setLabelsBrush(m_axisLabelBrush);
205 axis->setLabelsBrush(m_axisLabelBrush);
207 axis->setLabelsPen(m_axisLabelPen);
206 axis->setLabelsPen(m_axisLabelPen);
208 // TODO: check the axis type (x or y) should define whether to show the shades or not
207 // TODO: check the axis type (x or y) should define whether to show the shades or not
209 if (m_backgroundShades == BackgroundShadesBoth
208 if (m_backgroundShades == BackgroundShadesBoth
210 || (m_backgroundShades == BackgroundShadesVertical && axisX)
209 || (m_backgroundShades == BackgroundShadesVertical && axisX)
211 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX)) {
210 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX)) {
212 axis->setShadesPen(m_backgroundShadesPen);
211 axis->setShadesPen(m_backgroundShadesPen);
213 axis->setShadesBrush(m_backgroundShadesBrush);
212 axis->setShadesBrush(m_backgroundShadesBrush);
214 } else {
213 } else {
215 // The shades not supposed to be shown for this axis, clear possible brush and pen
214 // The shades not supposed to be shown for this axis, clear possible brush and pen
216 axis->setShadesPen(Qt::NoPen);
215 axis->setShadesPen(Qt::NoPen);
217 axis->setShadesBrush(Qt::NoBrush);
216 axis->setShadesBrush(Qt::NoBrush);
218 }
217 }
219 axis->setAxisPen(m_axisLinePen);
218 axis->setAxisPen(m_axisLinePen);
220 axis->setGridLinePen(m_gridLinePen);
219 axis->setGridLinePen(m_gridLinePen);
221 axis->setLabelsFont(m_masterFont);
220 axis->setLabelsFont(m_masterFont);
222 }
221 }
223 }
222 }
224
223
225 void ChartTheme::generateSeriesGradients()
224 void ChartTheme::generateSeriesGradients()
226 {
225 {
227 // Generate gradients in HSV color space
226 // Generate gradients in HSV color space
228 foreach (QColor color, m_seriesColors) {
227 foreach (QColor color, m_seriesColors) {
229 QLinearGradient g;
228 QLinearGradient g;
230 qreal h = color.hsvHueF();
229 qreal h = color.hsvHueF();
231 qreal s = color.hsvSaturationF();
230 qreal s = color.hsvSaturationF();
232
231
233 // TODO: tune the algorithm to give nice results with most base colors defined in
232 // TODO: tune the algorithm to give nice results with most base colors defined in
234 // most themes. The rest of the gradients we can define manually in theme specific
233 // most themes. The rest of the gradients we can define manually in theme specific
235 // implementation.
234 // implementation.
236 QColor start = color;
235 QColor start = color;
237 start.setHsvF(h, 0.05, 0.95);
236 start.setHsvF(h, 0.05, 0.95);
238 g.setColorAt(0.0, start);
237 g.setColorAt(0.0, start);
239
238
240 g.setColorAt(0.5, color);
239 g.setColorAt(0.5, color);
241
240
242 QColor end = color;
241 QColor end = color;
243 end.setHsvF(h, s, 0.25);
242 end.setHsvF(h, s, 0.25);
244 g.setColorAt(1.0, end);
243 g.setColorAt(1.0, end);
245
244
246 m_seriesGradients << g;
245 m_seriesGradients << g;
247 }
246 }
248 }
247 }
249
248
250
249
251 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
250 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
252 {
251 {
253 Q_ASSERT(pos >=0.0 && pos <= 1.0);
252 Q_ASSERT(pos >=0.0 && pos <= 1.0);
254 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
253 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
255 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
254 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
256 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
255 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
257 QColor c;
256 QColor c;
258 c.setRgbF(r, g, b);
257 c.setRgbF(r, g, b);
259 return c;
258 return c;
260 }
259 }
261
260
262 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
261 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
263 {
262 {
264 Q_ASSERT(pos >=0 && pos <= 1.0);
263 Q_ASSERT(pos >=0 && pos <= 1.0);
265
264
266 // another possibility:
265 // another possibility:
267 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
266 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
268
267
269 QGradientStops stops = gradient.stops();
268 QGradientStops stops = gradient.stops();
270 int count = stops.count();
269 int count = stops.count();
271
270
272 // find previous stop relative to position
271 // find previous stop relative to position
273 QGradientStop prev = stops.first();
272 QGradientStop prev = stops.first();
274 for (int i=0; i<count; i++) {
273 for (int i=0; i<count; i++) {
275 QGradientStop stop = stops.at(i);
274 QGradientStop stop = stops.at(i);
276 if (pos > stop.first)
275 if (pos > stop.first)
277 prev = stop;
276 prev = stop;
278
277
279 // given position is actually a stop position?
278 // given position is actually a stop position?
280 if (pos == stop.first) {
279 if (pos == stop.first) {
281 //qDebug() << "stop color" << pos;
280 //qDebug() << "stop color" << pos;
282 return stop.second;
281 return stop.second;
283 }
282 }
284 }
283 }
285
284
286 // find next stop relative to position
285 // find next stop relative to position
287 QGradientStop next = stops.last();
286 QGradientStop next = stops.last();
288 for (int i=count-1; i>=0; i--) {
287 for (int i=count-1; i>=0; i--) {
289 QGradientStop stop = stops.at(i);
288 QGradientStop stop = stops.at(i);
290 if (pos < stop.first)
289 if (pos < stop.first)
291 next = stop;
290 next = stop;
292 }
291 }
293
292
294 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
293 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
295
294
296 qreal range = next.first - prev.first;
295 qreal range = next.first - prev.first;
297 qreal posDelta = pos - prev.first;
296 qreal posDelta = pos - prev.first;
298 qreal relativePos = posDelta / range;
297 qreal relativePos = posDelta / range;
299
298
300 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
299 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
301
300
302 return colorAt(prev.second, next.second, relativePos);
301 return colorAt(prev.second, next.second, relativePos);
303 }
302 }
304
303
305 QTCOMMERCIALCHART_END_NAMESPACE
304 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,101
1 #include "splinechartitem_p.h"
1 #include "splinechartitem_p.h"
2 #include "chartpresenter_p.h"
2 #include "chartpresenter_p.h"
3 #include <QPainter>
3 #include <QPainter>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
8 XYChartItem(series, parent),
8 XYChartItem(series, parent),
9 m_series(series)
9 m_series(series)
10 {
10 {
11 setZValue(ChartPresenter::LineChartZValue);
11 setZValue(ChartPresenter::LineChartZValue);
12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
13 handleUpdated();
13 handleUpdated();
14 }
14 }
15
15
16 QRectF SplineChartItem::boundingRect() const
16 QRectF SplineChartItem::boundingRect() const
17 {
17 {
18 return m_rect;
18 return m_rect;
19 }
19 }
20
20
21 QPainterPath SplineChartItem::shape() const
21 QPainterPath SplineChartItem::shape() const
22 {
22 {
23 return m_path;
23 return m_path;
24 }
24 }
25
25
26 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
26 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
27 {
27 {
28 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
28 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
29 }
29 }
30
30
31 void SplineChartItem::setLayout(QVector<QPointF>& points)
31 void SplineChartItem::setLayout(QVector<QPointF>& points)
32 {
32 {
33
33
34 if(points.size()==0)
34 if(points.size()==0)
35 {
35 {
36 XYChartItem::setLayout(points);
36 XYChartItem::setLayout(points);
37 return;
37 return;
38 }
38 }
39
39
40 QPainterPath splinePath;
40 QPainterPath splinePath;
41 const QPointF& point = points.at(0);
41 const QPointF& point = points.at(0);
42 splinePath.moveTo(point);
42 splinePath.moveTo(point);
43
43
44 for (int i = 0; i < points.size() - 1; i++)
44 for (int i = 0; i < points.size() - 1; i++)
45 {
45 {
46 const QPointF& point = points.at(i + 1);
46 const QPointF& point = points.at(i + 1);
47 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
47 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
48 }
48 }
49
49
50 prepareGeometryChange();
50 prepareGeometryChange();
51 m_path = splinePath;
51 m_path = splinePath;
52 m_rect = splinePath.boundingRect();
52 m_rect = splinePath.boundingRect();
53 XYChartItem::setLayout(points);
53 XYChartItem::setLayout(points);
54 }
54 }
55
55
56 //handlers
56 //handlers
57
57
58 void SplineChartItem::handleUpdated()
58 void SplineChartItem::handleUpdated()
59 {
59 {
60 //m_items.setVisible(m_series->pointsVisible());
61 m_pen = m_series->pen();
60 m_pen = m_series->pen();
62 update();
61 update();
63 }
62 }
64
63
65 //painter
64 //painter
66
65
67 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
68 {
67 {
69 Q_UNUSED(widget);
68 Q_UNUSED(widget);
70 Q_UNUSED(option);
69 Q_UNUSED(option);
71 painter->save();
70 painter->save();
72 painter->setClipRect(clipRect());
71 painter->setClipRect(clipRect());
73 painter->setPen(m_pen);
72 // painter->setPen(m_pen);
73 painter->setPen(m_series->pen());
74 painter->drawPath(m_path);
74 painter->drawPath(m_path);
75
75
76 const QVector<QPointF> points = XYChartItem::points();
76 // const QVector<QPointF> points = XYChartItem::points();
77
77
78 for (int i = 0; i < points.size() - 1; i++)
78 // for (int i = 0; i < points.size() - 1; i++)
79 {
79 // {
80 painter->setPen(Qt::red);
80 // painter->setPen(Qt::red);
81 painter->drawEllipse(points[i], 2, 2);
81 // painter->drawEllipse(points[i], 2, 2);
82
82
83 painter->setPen(Qt::blue);
83 // painter->setPen(Qt::blue);
84 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
84 // // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
85 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
85 // // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
86 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
86 // // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
87 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
87 // // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
88 }
88 // }
89 if (points.count() > 0)
89 // if (points.count() > 0)
90 {
90 // {
91 painter->setPen(Qt::red);
91 // painter->setPen(Qt::red);
92 painter->drawEllipse(points[points.count() - 1], 2, 2);
92 // painter->drawEllipse(points[points.count() - 1], 2, 2);
93 }
93 // }
94 painter->restore();
94 painter->restore();
95 }
95 }
96
96
97
97
98
98
99 #include "moc_splinechartitem_p.cpp"
99 #include "moc_splinechartitem_p.cpp"
100
100
101 QTCOMMERCIALCHART_END_NAMESPACE
101 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now