@@ -1,96 +1,99 | |||
|
1 | 1 | #include "bar_p.h" |
|
2 | 2 | #include <QDebug> |
|
3 | 3 | #include <QPainter> |
|
4 | 4 | #include <QGraphicsSceneEvent> |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | Bar::Bar(QString category, QGraphicsItem *parent) |
|
9 | 9 | : QGraphicsObject(parent), |
|
10 | 10 | mXpos(0), |
|
11 | 11 | mYpos(0), |
|
12 | 12 | mWidth(0), |
|
13 | 13 | mHeight(0), |
|
14 | mBrush(QBrush()), | |
|
15 | mPen(QPen()), | |
|
14 | 16 | mCategory(category) |
|
15 | 17 | { |
|
16 | 18 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
17 | 19 | setAcceptHoverEvents(true); |
|
18 | 20 | } |
|
19 | 21 | |
|
20 | 22 | void Bar::setSize(const QSizeF& size) |
|
21 | 23 | { |
|
22 | 24 | mWidth = size.width(); |
|
23 | 25 | mHeight = size.height(); |
|
24 | 26 | } |
|
25 | 27 | |
|
26 | 28 | |
|
27 | 29 | void Bar::resize( qreal w, qreal h ) |
|
28 | 30 | { |
|
29 | 31 | mWidth = w; |
|
30 | 32 | mHeight = h; |
|
31 | 33 | } |
|
32 | 34 | |
|
33 | 35 | void Bar::setPos(qreal x, qreal y) |
|
34 | 36 | { |
|
35 | 37 | mXpos = x; |
|
36 | 38 | mYpos = y; |
|
37 | 39 | } |
|
38 | 40 | |
|
39 | 41 | void Bar::setPen(QPen pen) |
|
40 | 42 | { |
|
41 | 43 | mPen = pen; |
|
42 | 44 | } |
|
43 | 45 | |
|
44 | 46 | void Bar::setBrush(QBrush brush) |
|
45 | 47 | { |
|
46 | 48 | mBrush = brush; |
|
47 | 49 | } |
|
48 | 50 | |
|
49 | 51 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
50 | 52 | { |
|
51 | 53 | Q_UNUSED(option) |
|
52 | 54 | Q_UNUSED(widget) |
|
53 | 55 | |
|
54 | 56 | if (0 == mHeight) { |
|
55 | 57 | return; |
|
56 | 58 | } |
|
59 | painter->setPen(mPen); | |
|
57 | 60 | painter->setBrush(mBrush); |
|
58 | 61 | |
|
59 | 62 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. |
|
60 | 63 | int x0 = mXpos; |
|
61 | 64 | int x1 = (mXpos + mWidth); |
|
62 | 65 | int w = x1-x0; |
|
63 | 66 | int y0 = mYpos; |
|
64 | 67 | int y1 = (mYpos + mHeight); |
|
65 | 68 | int h = y1-y0; |
|
66 | 69 | painter->drawRect(x0, y0 ,w ,h); |
|
67 | 70 | } |
|
68 | 71 | |
|
69 | 72 | QRectF Bar::boundingRect() const |
|
70 | 73 | { |
|
71 | 74 | QRectF r(mXpos, mYpos, mWidth, mHeight); |
|
72 | 75 | return r; |
|
73 | 76 | } |
|
74 | 77 | |
|
75 | 78 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) |
|
76 | 79 | { |
|
77 | 80 | if (event->button() == Qt::LeftButton) { |
|
78 | 81 | emit clicked(mCategory); |
|
79 | 82 | } else if (event->button() == Qt::RightButton) { |
|
80 | 83 | emit rightClicked(mCategory); |
|
81 | 84 | } |
|
82 | 85 | } |
|
83 | 86 | |
|
84 | 87 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) |
|
85 | 88 | { |
|
86 | 89 | emit hoverEntered(event->lastScreenPos()); |
|
87 | 90 | } |
|
88 | 91 | |
|
89 | 92 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
90 | 93 | { |
|
91 | 94 | emit hoverLeaved(); |
|
92 | 95 | } |
|
93 | 96 | |
|
94 | 97 | #include "moc_bar_p.cpp" |
|
95 | 98 | |
|
96 | 99 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,103 +1,104 | |||
|
1 | 1 | #include "percentbarpresenter_p.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barvalue_p.h" |
|
4 | 4 | #include "separator_p.h" |
|
5 | 5 | #include "qbarset.h" |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | |
|
11 | 11 | PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) : |
|
12 | 12 | BarPresenterBase(series, parent) |
|
13 | 13 | { |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | void PercentBarPresenter::layoutChanged() |
|
17 | 17 | { |
|
18 | 18 | // Scale bars to new layout |
|
19 | 19 | // Layout for bars: |
|
20 | 20 | if (mSeries->barsetCount() <= 0) { |
|
21 | 21 | qDebug() << "No sets in model!"; |
|
22 | 22 | // Nothing to do. |
|
23 | 23 | return; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | if (childItems().count() == 0) { |
|
27 | 27 | qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!"; |
|
28 | 28 | return; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
32 | 32 | qreal tW = mWidth; |
|
33 | 33 | qreal tC = mSeries->categoryCount() + 1; |
|
34 | 34 | qreal cC = mSeries->categoryCount() * 2 + 1; |
|
35 | 35 | mBarWidth = tW / cC; |
|
36 | 36 | qreal xStep = (tW/tC); |
|
37 | 37 | qreal xPos = ((tW/tC) - mBarWidth / 2); |
|
38 | 38 | qreal h = mHeight; |
|
39 | 39 | |
|
40 | 40 | int itemIndex(0); |
|
41 | 41 | for (int category = 0; category < mSeries->categoryCount(); category++) { |
|
42 | 42 | qreal colSum = mSeries->categorySum(category); |
|
43 | 43 | qreal scale = (h / colSum); |
|
44 | 44 | qreal yPos = h; |
|
45 | 45 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
46 | 46 | qreal barHeight = mSeries->valueAt(set, category) * scale; |
|
47 | 47 | Bar* bar = mBars.at(itemIndex); |
|
48 | 48 | |
|
49 | 49 | // TODO: width settable per bar? |
|
50 | 50 | bar->resize(mBarWidth, barHeight); |
|
51 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
|
51 | 52 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
52 | 53 | bar->setPos(xPos, yPos-barHeight); |
|
53 | 54 | itemIndex++; |
|
54 | 55 | yPos -= barHeight; |
|
55 | 56 | } |
|
56 | 57 | xPos += xStep; |
|
57 | 58 | } |
|
58 | 59 | /* |
|
59 | 60 | // Position separators |
|
60 | 61 | xPos = xStep + xStep/2; |
|
61 | 62 | for (int s=0; s < mSeries->categoryCount() - 1; s++) { |
|
62 | 63 | Separator* sep = mSeparators.at(s); |
|
63 | 64 | sep->setPos(xPos,0); |
|
64 | 65 | sep->setSize(QSizeF(1,mHeight)); |
|
65 | 66 | xPos += xStep; |
|
66 | 67 | } |
|
67 | 68 | */ |
|
68 | 69 | // Position floating values |
|
69 | 70 | itemIndex = 0; |
|
70 | 71 | xPos = (tW/tC); |
|
71 | 72 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
72 | 73 | qreal yPos = h; |
|
73 | 74 | qreal colSum = mSeries->categorySum(category); |
|
74 | 75 | qreal scale = (h / colSum); |
|
75 | 76 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
76 | 77 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
77 | 78 | BarValue* value = mFloatingValues.at(itemIndex); |
|
78 | 79 | |
|
79 | 80 | QBarSet* barSet = mSeries->barsetAt(set); |
|
80 | 81 | value->resize(100,50); // TODO: proper layout for this. |
|
81 | 82 | value->setPos(xPos, yPos-barHeight/2); |
|
82 | 83 | value->setPen(barSet->floatingValuePen()); |
|
83 | 84 | |
|
84 | 85 | if (mSeries->valueAt(set,category) != 0) { |
|
85 | 86 | int p = mSeries->percentageAt(set,category) * 100; |
|
86 | 87 | QString vString(QString::number(p)); |
|
87 | 88 | vString.truncate(3); |
|
88 | 89 | vString.append("%"); |
|
89 | 90 | value->setValueString(vString); |
|
90 | 91 | } else { |
|
91 | 92 | value->setValueString(QString("")); |
|
92 | 93 | } |
|
93 | 94 | |
|
94 | 95 | itemIndex++; |
|
95 | 96 | yPos -= barHeight; |
|
96 | 97 | } |
|
97 | 98 | xPos += xStep; |
|
98 | 99 | } |
|
99 | 100 | } |
|
100 | 101 | |
|
101 | 102 | #include "moc_percentbarpresenter_p.cpp" |
|
102 | 103 | |
|
103 | 104 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,399 +1,352 | |||
|
1 | 1 | #include "charttheme_p.h" |
|
2 | 2 | #include "qchart.h" |
|
3 | 3 | #include "qchartview.h" |
|
4 | 4 | #include "qlegend.h" |
|
5 | 5 | #include "qchartaxis.h" |
|
6 | 6 | #include <QTime> |
|
7 | 7 | |
|
8 | 8 | //series |
|
9 | 9 | #include "qbarset.h" |
|
10 | 10 | #include "qbarseries.h" |
|
11 | 11 | #include "qstackedbarseries.h" |
|
12 | 12 | #include "qpercentbarseries.h" |
|
13 | 13 | #include "qlineseries.h" |
|
14 | 14 | #include "qareaseries.h" |
|
15 | 15 | #include "qscatterseries.h" |
|
16 | 16 | #include "qpieseries.h" |
|
17 | 17 | #include "qpieslice.h" |
|
18 | 18 | #include "qsplineseries.h" |
|
19 | 19 | |
|
20 | 20 | //items |
|
21 | 21 | #include "axisitem_p.h" |
|
22 | 22 | #include "barpresenter_p.h" |
|
23 | 23 | #include "stackedbarpresenter_p.h" |
|
24 | 24 | #include "percentbarpresenter_p.h" |
|
25 | 25 | #include "linechartitem_p.h" |
|
26 | 26 | #include "areachartitem_p.h" |
|
27 | 27 | #include "scatterchartitem_p.h" |
|
28 | 28 | #include "piechartitem_p.h" |
|
29 | 29 | #include "splinechartitem_p.h" |
|
30 | 30 | |
|
31 | 31 | //themes |
|
32 | 32 | #include "chartthemedefault_p.h" |
|
33 | 33 | #include "chartthemelight_p.h" |
|
34 | 34 | #include "chartthemebluecerulean_p.h" |
|
35 | 35 | #include "chartthemedark_p.h" |
|
36 | 36 | #include "chartthemebrownsand_p.h" |
|
37 | 37 | #include "chartthemebluencs_p.h" |
|
38 | 38 | #include "chartthemevanilla_p.h" |
|
39 | 39 | #include "chartthemeicy_p.h" |
|
40 | 40 | #include "chartthemegrayscale_p.h" |
|
41 | 41 | #include "chartthemescientific_p.h" |
|
42 | 42 | |
|
43 | 43 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
44 | 44 | |
|
45 | 45 | ChartTheme::ChartTheme(QChart::ChartTheme id) : |
|
46 | 46 | m_masterFont(QFont()), |
|
47 | 47 | m_titleBrush(QColor(QRgb(0x000000))), |
|
48 | 48 | m_axisLinePen(QPen(QRgb(0x000000))), |
|
49 | 49 | m_axisLabelBrush(QColor(QRgb(0x000000))), |
|
50 | 50 | m_backgroundShadesPen(Qt::NoPen), |
|
51 | 51 | m_backgroundShadesBrush(Qt::NoBrush), |
|
52 | 52 | m_backgroundShades(BackgroundShadesNone), |
|
53 | 53 | m_gridLinePen(QPen(QRgb(0x000000))) |
|
54 | 54 | { |
|
55 | 55 | m_id = id; |
|
56 | 56 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
61 | 61 | { |
|
62 | 62 | switch(theme) { |
|
63 | 63 | case QChart::ChartThemeLight: |
|
64 | 64 | return new ChartThemeLight(); |
|
65 | 65 | case QChart::ChartThemeBlueCerulean: |
|
66 | 66 | return new ChartThemeBlueCerulean(); |
|
67 | 67 | case QChart::ChartThemeDark: |
|
68 | 68 | return new ChartThemeDark(); |
|
69 | 69 | case QChart::ChartThemeBrownSand: |
|
70 | 70 | return new ChartThemeBrownSand(); |
|
71 | 71 | case QChart::ChartThemeBlueNcs: |
|
72 | 72 | return new ChartThemeBlueNcs(); |
|
73 | 73 | case QChart::ChartThemeVanilla: |
|
74 | 74 | return new ChartThemeVanilla(); |
|
75 | 75 | case QChart::ChartThemeIcy: |
|
76 | 76 | return new ChartThemeIcy(); |
|
77 | 77 | case QChart::ChartThemeGrayscale: |
|
78 | 78 | return new ChartThemeGrayscale(); |
|
79 | 79 | case QChart::ChartThemeScientific: |
|
80 | 80 | return new ChartThemeScientific(); |
|
81 | 81 | default: |
|
82 | 82 | return new ChartThemeDefault(); |
|
83 | 83 | } |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | void ChartTheme::decorate(QChart* chart,bool force) |
|
87 | 87 | { |
|
88 | 88 | QPen pen; |
|
89 | 89 | QBrush brush; |
|
90 | 90 | |
|
91 | 91 | if(brush == chart->backgroundBrush() || force) |
|
92 | 92 | { |
|
93 | 93 | if (m_backgroundShades == BackgroundShadesNone) { |
|
94 | 94 | chart->setBackgroundBrush(m_chartBackgroundGradient); |
|
95 | 95 | } |
|
96 | 96 | else { |
|
97 | 97 | chart->setBackgroundBrush(Qt::NoBrush); |
|
98 | 98 | } |
|
99 | 99 | } |
|
100 | 100 | chart->setTitleFont(m_masterFont); |
|
101 | 101 | chart->setTitleBrush(m_titleBrush); |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | void ChartTheme::decorate(QLegend* legend,bool force) |
|
105 | 105 | { |
|
106 | 106 | QPen pen; |
|
107 | 107 | QBrush brush; |
|
108 | 108 | |
|
109 | 109 | if (pen == legend->pen() || force){ |
|
110 | 110 | //TODO:: legend->setPen(); |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | if (brush == legend->brush() || force) { |
|
115 | 115 | legend->setBrush(m_chartBackgroundGradient); |
|
116 | 116 | } |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | void ChartTheme::decorate(QAreaSeries* series, int index,bool force) |
|
120 | 120 | { |
|
121 | 121 | QPen pen; |
|
122 | 122 | QBrush brush; |
|
123 | 123 | |
|
124 | 124 | if (pen == series->pen() || force){ |
|
125 |
pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), |
|
|
125 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | |
|
126 | 126 | pen.setWidthF(2); |
|
127 | 127 | series->setPen(pen); |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | if (brush == series->brush() || force) { |
|
131 | 131 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); |
|
132 | 132 | series->setBrush(brush); |
|
133 | 133 | } |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | void ChartTheme::decorate(QLineSeries* series,int index,bool force) |
|
138 | 138 | { |
|
139 | 139 | QPen pen; |
|
140 | 140 | if(pen == series->pen() || force ){ |
|
141 | 141 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
142 | 142 | pen.setWidthF(2); |
|
143 | 143 | series->setPen(pen); |
|
144 | 144 | } |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | void ChartTheme::decorate(QBarSeries* series,int index,bool force) | |
|
147 | void ChartTheme::decorate(QBarSeries* series, int index, bool force) | |
|
148 | 148 | { |
|
149 | 149 | QBrush brush; |
|
150 | QPen pen; | |
|
150 | 151 | QList<QBarSet*> sets = series->barSets(); |
|
151 | 152 | |
|
152 |
for (int i |
|
|
153 | for (int i(0); i < sets.count(); i++) { | |
|
153 | 154 | qreal pos = 0.5; |
|
154 | 155 | if (sets.count() > 1) |
|
155 | 156 | pos = (qreal) i / (qreal) (sets.count() - 1); |
|
156 | 157 | |
|
157 | if(brush == sets.at(i)->brush() || force ){ | |
|
158 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
|
159 | sets.at(i)->setBrush(QBrush(c)); | |
|
160 | } | |
|
161 | // Pick label color as far as possible from bar color (within gradient). | |
|
162 | // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :) | |
|
163 | // TODO: better picking of label color? | |
|
164 | QColor c; | |
|
165 | ||
|
166 | if (pos < 0.3) { | |
|
167 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | |
|
168 | } else { | |
|
169 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | |
|
170 | } | |
|
171 | sets.at(i)->setFloatingValuePen(QPen(c)); | |
|
172 | ||
|
173 | } | |
|
174 | } | |
|
175 | ||
|
176 | void ChartTheme::decorate(QStackedBarSeries* series,int index,bool force) | |
|
177 | { | |
|
178 | QBrush brush; | |
|
179 | QList<QBarSet*> sets = series->barSets(); | |
|
180 | ||
|
181 | for (int i=0; i<sets.count(); i++) { | |
|
182 | qreal pos = 0.5; | |
|
183 | if (sets.count() > 1) | |
|
184 | pos = (qreal) i / (qreal) (sets.count() - 1); | |
|
185 | if(brush == sets.at(i)->brush() || force){ | |
|
186 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
|
187 | sets.at(i)->setBrush(QBrush(c)); | |
|
188 | } | |
|
189 | QColor c; | |
|
190 | if (pos < 0.3) { | |
|
191 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | |
|
192 | } else { | |
|
193 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | |
|
158 | if (brush == sets.at(i)->brush() || force ) { | |
|
159 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
|
160 | sets.at(i)->setBrush(QBrush(c)); | |
|
194 | 161 | } |
|
195 | sets.at(i)->setFloatingValuePen(QPen(c)); | |
|
196 | } | |
|
197 | } | |
|
198 | 162 | |
|
199 | void ChartTheme::decorate(QPercentBarSeries* series,int index,bool force) | |
|
200 | { | |
|
201 | QBrush brush; | |
|
202 | QList<QBarSet*> sets = series->barSets(); | |
|
163 | // Pick label color from the opposite end of the gradient. | |
|
164 | // 0.3 as a boundary seems to work well. | |
|
165 | if (pos < 0.3) | |
|
166 | sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); | |
|
167 | else | |
|
168 | sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); | |
|
203 | 169 | |
|
204 | for (int i=0; i<sets.count(); i++) { | |
|
205 | qreal pos = 0.5; | |
|
206 |
|
|
|
207 | pos = (qreal) i / (qreal) (sets.count() - 1); | |
|
208 | ||
|
209 | if(brush == sets.at(i)->brush() || force){ | |
|
210 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
|
211 | sets.at(i)->setBrush(QBrush(c)); | |
|
212 | } | |
|
213 | QColor c; | |
|
214 | if (pos < 0.3) { | |
|
215 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1); | |
|
216 | } else { | |
|
217 | c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0); | |
|
170 | if (pen == sets.at(i)->pen() || force) { | |
|
171 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | |
|
172 | sets.at(i)->setPen(c); | |
|
218 | 173 | } |
|
219 | sets.at(i)->setFloatingValuePen(QPen(c)); | |
|
220 | 174 | } |
|
221 | 175 | } |
|
222 | 176 | |
|
223 | 177 | void ChartTheme::decorate(QScatterSeries* series, int index,bool force) |
|
224 | 178 | { |
|
225 | ||
|
226 | 179 | QPen pen; |
|
227 | 180 | QBrush brush; |
|
228 | 181 | |
|
229 | 182 | if (pen == series->pen() || force) { |
|
230 |
pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), |
|
|
183 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | |
|
231 | 184 | pen.setWidthF(2); |
|
232 | 185 | series->setPen(pen); |
|
233 | 186 | } |
|
234 | 187 | |
|
235 | 188 | if (brush == series->brush() || force) { |
|
236 | 189 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); |
|
237 | 190 | series->setBrush(brush); |
|
238 | 191 | } |
|
239 | 192 | } |
|
240 | 193 | |
|
241 | 194 | void ChartTheme::decorate(QPieSeries* series, int index, bool force) |
|
242 | 195 | { |
|
243 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient | |
|
244 | ||
|
245 | 196 | QPen pen; |
|
246 | 197 | QBrush brush; |
|
247 | 198 | |
|
248 | 199 | for (int i(0); i < series->slices().count(); i++) { |
|
249 | qreal pos = (qreal) i / (qreal) series->count(); | |
|
250 | if( pen == series->slices().at(i)->slicePen() || force){ | |
|
251 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1); | |
|
252 | series->slices().at(i)->setSlicePen(penColor); | |
|
200 | if (pen == series->slices().at(i)->slicePen() || force) { | |
|
201 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | |
|
202 | series->slices().at(i)->setSlicePen(penColor); | |
|
253 | 203 | } |
|
254 | if( brush == series->slices().at(i)->sliceBrush() || force){ | |
|
255 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
|
256 | series->slices().at(i)->setSliceBrush(brushColor); | |
|
204 | ||
|
205 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient | |
|
206 | qreal pos = (qreal) i / (qreal) series->count(); | |
|
207 | if (brush == series->slices().at(i)->sliceBrush() || force) { | |
|
208 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
|
209 | series->slices().at(i)->setSliceBrush(brushColor); | |
|
257 | 210 | } |
|
258 | 211 | } |
|
259 | 212 | } |
|
260 | 213 | |
|
261 | 214 | void ChartTheme::decorate(QSplineSeries* series, int index, bool force) |
|
262 | 215 | { |
|
263 | 216 | QPen pen; |
|
264 | 217 | if(pen == series->pen() || force){ |
|
265 | 218 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
266 | 219 | pen.setWidthF(2); |
|
267 | 220 | series->setPen(pen); |
|
268 | 221 | } |
|
269 | 222 | } |
|
270 | 223 | |
|
271 | 224 | void ChartTheme::decorate(QChartAxis* axis,bool axisX, bool force) |
|
272 | 225 | { |
|
273 | 226 | QPen pen; |
|
274 | 227 | QBrush brush; |
|
275 | 228 | QFont font; |
|
276 | 229 | |
|
277 | 230 | if (axis->isAxisVisible()) { |
|
278 | 231 | |
|
279 | 232 | if(brush == axis->labelsBrush() || force){ |
|
280 | 233 | axis->setLabelsBrush(m_axisLabelBrush); |
|
281 | 234 | } |
|
282 | 235 | if(pen == axis->labelsPen() || force){ |
|
283 | 236 | axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons |
|
284 | 237 | } |
|
285 | 238 | |
|
286 | 239 | |
|
287 | 240 | if (axis->shadesVisible() || force) { |
|
288 | 241 | |
|
289 | 242 | if(brush == axis->shadesBrush() || force){ |
|
290 | 243 | axis->setShadesBrush(m_backgroundShadesBrush); |
|
291 | 244 | } |
|
292 | 245 | |
|
293 | 246 | if(pen == axis->shadesPen() || force){ |
|
294 | 247 | axis->setShadesPen(m_backgroundShadesPen); |
|
295 | 248 | } |
|
296 | 249 | |
|
297 | 250 | if(force && (m_backgroundShades == BackgroundShadesBoth |
|
298 | 251 | || (m_backgroundShades == BackgroundShadesVertical && axisX) |
|
299 | 252 | || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){ |
|
300 | 253 | axis->setShadesVisible(true); |
|
301 | 254 | |
|
302 | 255 | } |
|
303 | 256 | } |
|
304 | 257 | |
|
305 | 258 | if(pen == axis->axisPen() || force){ |
|
306 | 259 | axis->setAxisPen(m_axisLinePen); |
|
307 | 260 | } |
|
308 | 261 | |
|
309 | 262 | if(pen == axis->gridLinePen() || force){ |
|
310 | 263 | axis->setGridLinePen(m_gridLinePen); |
|
311 | 264 | } |
|
312 | 265 | |
|
313 | 266 | if(font == axis->labelsFont() || force){ |
|
314 | 267 | axis->setLabelsFont(m_masterFont); |
|
315 | 268 | } |
|
316 | 269 | } |
|
317 | 270 | } |
|
318 | 271 | |
|
319 | 272 | void ChartTheme::generateSeriesGradients() |
|
320 | 273 | { |
|
321 | 274 | // Generate gradients in HSV color space |
|
322 | 275 | foreach (QColor color, m_seriesColors) { |
|
323 | 276 | QLinearGradient g; |
|
324 | 277 | qreal h = color.hsvHueF(); |
|
325 | 278 | qreal s = color.hsvSaturationF(); |
|
326 | 279 | |
|
327 | 280 | // TODO: tune the algorithm to give nice results with most base colors defined in |
|
328 | 281 | // most themes. The rest of the gradients we can define manually in theme specific |
|
329 | 282 | // implementation. |
|
330 | 283 | QColor start = color; |
|
331 | 284 | start.setHsvF(h, 0.05, 0.95); |
|
332 | 285 | g.setColorAt(0.0, start); |
|
333 | 286 | |
|
334 | 287 | g.setColorAt(0.5, color); |
|
335 | 288 | |
|
336 | 289 | QColor end = color; |
|
337 | 290 | end.setHsvF(h, s, 0.25); |
|
338 | 291 | g.setColorAt(1.0, end); |
|
339 | 292 | |
|
340 | 293 | m_seriesGradients << g; |
|
341 | 294 | } |
|
342 | 295 | } |
|
343 | 296 | |
|
344 | 297 | |
|
345 | 298 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) |
|
346 | 299 | { |
|
347 | 300 | Q_ASSERT(pos >=0.0 && pos <= 1.0); |
|
348 | 301 | qreal r = start.redF() + ((end.redF() - start.redF()) * pos); |
|
349 | 302 | qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); |
|
350 | 303 | qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); |
|
351 | 304 | QColor c; |
|
352 | 305 | c.setRgbF(r, g, b); |
|
353 | 306 | return c; |
|
354 | 307 | } |
|
355 | 308 | |
|
356 | 309 | QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) |
|
357 | 310 | { |
|
358 | 311 | Q_ASSERT(pos >=0 && pos <= 1.0); |
|
359 | 312 | |
|
360 | 313 | // another possibility: |
|
361 | 314 | // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient |
|
362 | 315 | |
|
363 | 316 | QGradientStops stops = gradient.stops(); |
|
364 | 317 | int count = stops.count(); |
|
365 | 318 | |
|
366 | 319 | // find previous stop relative to position |
|
367 | 320 | QGradientStop prev = stops.first(); |
|
368 | 321 | for (int i=0; i<count; i++) { |
|
369 | 322 | QGradientStop stop = stops.at(i); |
|
370 | 323 | if (pos > stop.first) |
|
371 | 324 | prev = stop; |
|
372 | 325 | |
|
373 | 326 | // given position is actually a stop position? |
|
374 | 327 | if (pos == stop.first) { |
|
375 | 328 | //qDebug() << "stop color" << pos; |
|
376 | 329 | return stop.second; |
|
377 | 330 | } |
|
378 | 331 | } |
|
379 | 332 | |
|
380 | 333 | // find next stop relative to position |
|
381 | 334 | QGradientStop next = stops.last(); |
|
382 | 335 | for (int i=count-1; i>=0; i--) { |
|
383 | 336 | QGradientStop stop = stops.at(i); |
|
384 | 337 | if (pos < stop.first) |
|
385 | 338 | next = stop; |
|
386 | 339 | } |
|
387 | 340 | |
|
388 | 341 | //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; |
|
389 | 342 | |
|
390 | 343 | qreal range = next.first - prev.first; |
|
391 | 344 | qreal posDelta = pos - prev.first; |
|
392 | 345 | qreal relativePos = posDelta / range; |
|
393 | 346 | |
|
394 | 347 | //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; |
|
395 | 348 | |
|
396 | 349 | return colorAt(prev.second, next.second, relativePos); |
|
397 | 350 | } |
|
398 | 351 | |
|
399 | 352 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,81 +1,78 | |||
|
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 | #include <QGradientStops> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class ChartItem; |
|
12 | 12 | class QSeries; |
|
13 | 13 | class LineChartItem; |
|
14 | 14 | class QLineSeries; |
|
15 | 15 | class BarPresenter; |
|
16 | 16 | class QBarSeries; |
|
17 | 17 | class StackedBarPresenter; |
|
18 | 18 | class QStackedBarSeries; |
|
19 | 19 | class QPercentBarSeries; |
|
20 | 20 | class PercentBarPresenter; |
|
21 | 21 | class QScatterSeries; |
|
22 | 22 | class ScatterChartItem; |
|
23 | 23 | class PieChartItem; |
|
24 | 24 | class QPieSeries; |
|
25 | 25 | class SplineChartItem; |
|
26 | 26 | class QSplineSeries; |
|
27 | 27 | class AreaChartItem; |
|
28 | 28 | class QAreaSeries; |
|
29 | 29 | |
|
30 | 30 | class ChartTheme |
|
31 | 31 | { |
|
32 | 32 | public: |
|
33 | 33 | enum BackgroundShadesMode { |
|
34 | 34 | BackgroundShadesNone = 0, |
|
35 | 35 | BackgroundShadesVertical, |
|
36 | 36 | BackgroundShadesHorizontal, |
|
37 | 37 | BackgroundShadesBoth |
|
38 | 38 | }; |
|
39 | 39 | |
|
40 | 40 | protected: |
|
41 | 41 | explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault); |
|
42 | 42 | public: |
|
43 | 43 | static ChartTheme* createTheme(QChart::ChartTheme theme); |
|
44 | 44 | QChart::ChartTheme id() const {return m_id;} |
|
45 | 45 | void decorate(QChart* chart,bool force = true); |
|
46 | 46 | void decorate(QLegend* legend,bool force = true); |
|
47 | //void decorate(ChartItem* item, QSeries* series,int index); | |
|
48 | 47 | void decorate(QBarSeries* series, int index,bool force = true); |
|
49 | void decorate(QStackedBarSeries* series, int index,bool force = true); | |
|
50 | void decorate(QPercentBarSeries* series, int index,bool force = true); | |
|
51 | 48 | void decorate(QLineSeries* series, int index,bool force = true); |
|
52 | 49 | void decorate(QAreaSeries* series, int index,bool force = true); |
|
53 | 50 | void decorate(QScatterSeries* series, int index,bool force = true); |
|
54 | 51 | void decorate(QPieSeries* series, int index,bool force = true); |
|
55 | 52 | void decorate(QSplineSeries* series, int index,bool force = true); |
|
56 | 53 | void decorate(QChartAxis* axis, bool axisX,bool force = true); |
|
57 | 54 | |
|
58 | 55 | public: // utils |
|
59 | 56 | void generateSeriesGradients(); |
|
60 | 57 | static QColor colorAt(const QColor &start, const QColor &end, qreal pos); |
|
61 | 58 | static QColor colorAt(const QGradient &gradient, qreal pos); |
|
62 | 59 | |
|
63 | 60 | protected: |
|
64 | 61 | QChart::ChartTheme m_id; |
|
65 | 62 | QList<QColor> m_seriesColors; |
|
66 | 63 | QList<QGradient> m_seriesGradients; |
|
67 | 64 | QLinearGradient m_chartBackgroundGradient; |
|
68 | 65 | |
|
69 | 66 | QFont m_masterFont; |
|
70 | 67 | QBrush m_titleBrush; |
|
71 | 68 | QPen m_axisLinePen; |
|
72 | 69 | QBrush m_axisLabelBrush; |
|
73 | 70 | QPen m_backgroundShadesPen; |
|
74 | 71 | QBrush m_backgroundShadesBrush; |
|
75 | 72 | BackgroundShadesMode m_backgroundShades; |
|
76 | 73 | QPen m_gridLinePen; |
|
77 | 74 | }; |
|
78 | 75 | |
|
79 | 76 | QTCOMMERCIALCHART_END_NAMESPACE |
|
80 | 77 | |
|
81 | 78 | #endif // CHARTTHEME_H |
@@ -1,179 +1,173 | |||
|
1 | 1 | #include "scatterchartitem_p.h" |
|
2 | 2 | #include "qscatterseries.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include <QPainter> |
|
5 | 5 | #include <QGraphicsScene> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | ||
|
10 | ||
|
11 | ||
|
12 | ||
|
13 | ||
|
14 | ||
|
15 | 9 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) : |
|
16 | 10 | XYChartItem(series,parent), |
|
17 | 11 | m_series(series), |
|
18 | 12 | m_items(this), |
|
19 | 13 | m_shape(QScatterSeries::MarkerShapeRectangle), |
|
20 |
m_size(1 |
|
|
14 | m_size(15) | |
|
21 | 15 | |
|
22 | 16 | { |
|
23 | 17 | Q_ASSERT(parent); |
|
24 | 18 | Q_ASSERT(series); |
|
25 | 19 | |
|
26 | 20 | QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
27 | 21 | |
|
28 | 22 | setZValue(ChartPresenter::ScatterSeriesZValue); |
|
29 | 23 | setFlags(QGraphicsItem::ItemHasNoContents); |
|
30 | 24 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
31 | 25 | |
|
32 | 26 | handleUpdated(); |
|
33 | 27 | |
|
34 | 28 | m_items.setHandlesChildEvents(false); |
|
35 | 29 | |
|
36 | 30 | // TODO: how to draw a drop shadow? |
|
37 | 31 | // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); |
|
38 | 32 | // dropShadow->setOffset(2.0); |
|
39 | 33 | // dropShadow->setBlurRadius(2.0); |
|
40 | 34 | // setGraphicsEffect(dropShadow); |
|
41 | 35 | } |
|
42 | 36 | |
|
43 | 37 | |
|
44 | 38 | QRectF ScatterChartItem::boundingRect() const |
|
45 | 39 | { |
|
46 | 40 | return m_rect; |
|
47 | 41 | } |
|
48 | 42 | |
|
49 | 43 | void ScatterChartItem::createPoints(int count) |
|
50 | 44 | { |
|
51 | 45 | for (int i = 0; i < count; ++i) { |
|
52 | 46 | |
|
53 | 47 | QGraphicsItem *item; |
|
54 | 48 | |
|
55 | 49 | switch (m_shape) { |
|
56 | 50 | case QScatterSeries::MarkerShapeCircle:{ |
|
57 | 51 | QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size); |
|
58 | 52 | const QRectF& rect = i->boundingRect(); |
|
59 | 53 | i->setPos(-rect.width()/2,-rect.height()/2); |
|
60 | 54 | item = new Marker(i,this); |
|
61 | 55 | break; |
|
62 | 56 | } |
|
63 | 57 | case QScatterSeries::MarkerShapeRectangle:{ |
|
64 | 58 | QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size); |
|
65 | 59 | i->setPos(-m_size/2,-m_size/2); |
|
66 | 60 | item = new Marker(i,this); |
|
67 | 61 | break; |
|
68 | 62 | } |
|
69 | 63 | default: |
|
70 | 64 | qWarning()<<"Unsupported marker type"; |
|
71 | 65 | break; |
|
72 | 66 | |
|
73 | 67 | } |
|
74 | 68 | m_items.addToGroup(item); |
|
75 | 69 | } |
|
76 | 70 | } |
|
77 | 71 | |
|
78 | 72 | void ScatterChartItem::deletePoints(int count) |
|
79 | 73 | { |
|
80 | 74 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
81 | 75 | |
|
82 | 76 | for (int i = 0; i < count; ++i) { |
|
83 | 77 | delete(items.takeLast()); |
|
84 | 78 | } |
|
85 | 79 | } |
|
86 | 80 | |
|
87 | 81 | void ScatterChartItem::markerSelected(Marker* marker) |
|
88 | 82 | { |
|
89 | 83 | emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index()))); |
|
90 | 84 | } |
|
91 | 85 | |
|
92 | 86 | void ScatterChartItem::setLayout(QVector<QPointF>& points) |
|
93 | 87 | { |
|
94 | 88 | if(points.size()==0) |
|
95 | 89 | { |
|
96 | 90 | XYChartItem::setLayout(points); |
|
97 | 91 | return; |
|
98 | 92 | } |
|
99 | 93 | |
|
100 | 94 | int diff = XYChartItem::points().size() - points.size(); |
|
101 | 95 | |
|
102 | 96 | if(diff>0) { |
|
103 | 97 | deletePoints(diff); |
|
104 | 98 | } |
|
105 | 99 | else if(diff<0) { |
|
106 | 100 | createPoints(-diff); |
|
107 | 101 | } |
|
108 | 102 | |
|
109 | 103 | if(diff!=0) handleUpdated(); |
|
110 | 104 | |
|
111 | 105 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
112 | 106 | |
|
113 | 107 | for(int i=0; i< points.size();i++) { |
|
114 | 108 | Marker* item = static_cast<Marker*>(items.at(i)); |
|
115 | 109 | const QPointF& point = points.at(i); |
|
116 | 110 | const QRectF& rect = item->boundingRect(); |
|
117 | 111 | item->setIndex(i); |
|
118 | 112 | item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); |
|
119 | 113 | if(!clipRect().contains(point)) { |
|
120 | 114 | item->setVisible(false); |
|
121 | 115 | } |
|
122 | 116 | else { |
|
123 | 117 | item->setVisible(true); |
|
124 | 118 | } |
|
125 | 119 | } |
|
126 | 120 | |
|
127 | 121 | prepareGeometryChange(); |
|
128 | 122 | m_rect = clipRect(); |
|
129 | 123 | XYChartItem::setLayout(points); |
|
130 | 124 | } |
|
131 | 125 | |
|
132 | 126 | |
|
133 | 127 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
134 | 128 | { |
|
135 | 129 | Q_UNUSED(painter) |
|
136 | 130 | Q_UNUSED(option) |
|
137 | 131 | Q_UNUSED(widget) |
|
138 | 132 | } |
|
139 | 133 | |
|
140 | 134 | void ScatterChartItem::setPen(const QPen& pen) |
|
141 | 135 | { |
|
142 | 136 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
143 | 137 | static_cast<Marker*>(item)->setPen(pen); |
|
144 | 138 | } |
|
145 | 139 | } |
|
146 | 140 | |
|
147 | 141 | void ScatterChartItem::setBrush(const QBrush& brush) |
|
148 | 142 | { |
|
149 | 143 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
150 | 144 | static_cast<Marker*>(item)->setBrush(brush); |
|
151 | 145 | } |
|
152 | 146 | } |
|
153 | 147 | |
|
154 | 148 | void ScatterChartItem::handleUpdated() |
|
155 | 149 | { |
|
156 | 150 | |
|
157 | 151 | int count = m_items.childItems().count(); |
|
158 | 152 | |
|
159 | 153 | if(count==0) return; |
|
160 | 154 | |
|
161 | 155 | bool recreate = m_size != m_series->size() || m_shape != m_series->shape(); |
|
162 | 156 | |
|
163 | 157 | //TODO: only rewrite on size change |
|
164 | 158 | |
|
165 | 159 | m_size = m_series->size(); |
|
166 | 160 | m_shape = m_series->shape(); |
|
167 | 161 | |
|
168 | 162 | if(recreate){ |
|
169 | 163 | deletePoints(count); |
|
170 | 164 | createPoints(count); |
|
171 | 165 | } |
|
172 | 166 | |
|
173 | 167 | setPen(m_series->pen()); |
|
174 | 168 | setBrush(m_series->brush()); |
|
175 | 169 | } |
|
176 | 170 | |
|
177 | 171 | #include "moc_scatterchartitem_p.cpp" |
|
178 | 172 | |
|
179 | 173 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now