##// END OF EJS Templates
renamed barchart floating values with labels to be consistent with piechart
sauimone -
r820:2bd79611bb1d
parent child
Show More
@@ -360,7 +360,6 void ThemeWidget::updateUI()
360 360
361 361 QLegend::Alignments alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
362 362 foreach (QChartView *chartView, m_charts) {
363 qDebug() << alignment;
364 363 chartView->chart()->legend()->setAlignmnent(alignment);
365 364 }
366 365 }
@@ -11,7 +11,7 SOURCES += \
11 11 $$PWD/qpercentbarseries.cpp \
12 12 $$PWD/qstackedbarseries.cpp \
13 13 $$PWD/stackedbarchartitem.cpp \
14 $$PWD/barvalue.cpp
14 $$PWD/barlabel.cpp
15 15
16 16 PRIVATE_HEADERS += \
17 17 $$PWD/bar_p.h \
@@ -19,7 +19,7 PRIVATE_HEADERS += \
19 19 $$PWD/barchartitem_p.h \
20 20 $$PWD/percentbarchartitem_p.h \
21 21 $$PWD/stackedbarchartitem_p.h \
22 $$PWD/barvalue_p.h
22 $$PWD/barlabel_p.h
23 23
24 24 PUBLIC_HEADERS += \
25 25 $$PWD/qbarseries.h \
@@ -20,7 +20,7
20 20
21 21 #include "barchartitem_p.h"
22 22 #include "bar_p.h"
23 #include "barvalue_p.h"
23 #include "barlabel_p.h"
24 24 #include "qbarset.h"
25 25 #include "qbarseries.h"
26 26 #include "qchart.h"
@@ -75,7 +75,7 void BarChartItem::dataChanged()
75 75 delete item;
76 76
77 77 m_bars.clear();
78 m_values.clear();
78 m_labels.clear();
79 79 m_layout.clear();
80 80
81 81 // Create new graphic items for bars
@@ -93,14 +93,14 void BarChartItem::dataChanged()
93 93 }
94 94 }
95 95
96 // Create value items
96 // Create labels
97 97 for (int category = 0; category < m_series->categoryCount(); category++) {
98 98 for (int s = 0; s < m_series->barsetCount(); s++) {
99 99 QBarSet *set = m_series->barsetAt(s);
100 BarValue *value = new BarValue(*set, this);
100 BarLabel *value = new BarLabel(*set, this);
101 101 childItems().append(value);
102 m_values.append(value);
103 connect(set,SIGNAL(valuesVisibleChanged(bool)),value,SLOT(valuesVisibleChanged(bool)));
102 m_labels.append(value);
103 connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
104 104 }
105 105 }
106 106 }
@@ -142,17 +142,17 QVector<QRectF> BarChartItem::calculateLayout()
142 142 bar->setPen(barSet->pen());
143 143 bar->setBrush(barSet->brush());
144 144
145 BarValue* value = m_values.at(itemIndex);
145 BarLabel* label = m_labels.at(itemIndex);
146 146
147 147 if (!qFuzzyIsNull(barSet->valueAt(category))) {
148 value->setText(QString::number(barSet->valueAt(category)));
148 label->setText(QString::number(barSet->valueAt(category)));
149 149 } else {
150 value->setText(QString(""));
150 label->setText(QString(""));
151 151 }
152 152
153 value->setPos(xPos + (rect.width()/2 - value->boundingRect().width()/2)
154 ,yPos - barHeight/2 - value->boundingRect().height()/2);
155 value->setPen(barSet->valuePen());
153 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
154 ,yPos - barHeight/2 - label->boundingRect().height()/2);
155 // value->setFont(barSet->valueFont());
156 156
157 157 itemIndex++;
158 158 xPos += barWidth;
@@ -29,7 +29,7
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class Bar;
32 class BarValue;
32 class BarLabel;
33 33 class QChartAxisCategories;
34 34 class QChart;
35 35
@@ -81,7 +81,7 protected:
81 81 // Not owned.
82 82 QBarSeries *m_series;
83 83 QList<Bar *> m_bars;
84 QList<BarValue *> m_values;
84 QList<BarLabel *> m_labels;
85 85 };
86 86
87 87 QTCOMMERCIALCHART_END_NAMESPACE
@@ -18,73 +18,87
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "barvalue_p.h"
21 #include "barlabel_p.h"
22 #include "chartpresenter_p.h"
23 #include <QBarSet>
22 24 #include <QPainter>
23 25 #include <QPen>
24 26 #include <QGraphicsSimpleTextItem>
25 27
26 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 29
28 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent) : QGraphicsObject(parent),
29 m_barSet(set),
30 BarLabel::BarLabel(QBarSet &barSet, QGraphicsItem *parent) : QGraphicsObject(parent),
31 m_barSet(barSet),
30 32 m_textItem(new QGraphicsSimpleTextItem(this))
31 33 {
32 // connect(&set,SIGNAL(valuesVisibleChanged(bool)),value,SLOT(valuesVisibleChanged(bool)));
33 setVisible(false);
34 // connect(&set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
35 setZValue(ChartPresenter::BarSeriesZValue + 1);
36 setVisible(barSet.labelsVisible());
37 setPen(Qt::NoPen);
38 setBrush(Qt::SolidPattern);
34 39 }
35 40
36 void BarValue::setText(QString str)
41 void BarLabel::setText(QString str)
37 42 {
38 43 m_textItem->setText(str);
39 44 }
40 45
41 QString BarValue::text() const
46 QString BarLabel::text() const
42 47 {
43 48 return m_textItem->text();
44 49 }
45 50
46 void BarValue::setPen(const QPen &pen)
51 void BarLabel::setPen(const QPen &pen)
47 52 {
48 53 m_textItem->setPen(pen);
49 54 }
50 55
51 QPen BarValue::pen() const
56 QPen BarLabel::pen() const
52 57 {
53 58 return m_textItem->pen();
54 59 }
55 60
56 void BarValue::setFont(const QFont &font)
61 void BarLabel::setBrush(const QBrush &brush)
62 {
63 m_textItem->setBrush(brush);
64 }
65
66 QBrush BarLabel::brush() const
67 {
68 return m_textItem->brush();
69 }
70
71 void BarLabel::setFont(const QFont &font)
57 72 {
58 73 m_textItem->setFont(font);
59 74 }
60 75
61 QFont BarValue::font() const
76 QFont BarLabel::font() const
62 77 {
63 78 return m_textItem->font();
64 79 }
65 80
66 void BarValue::setPos(qreal x, qreal y)
81 void BarLabel::setPos(qreal x, qreal y)
67 82 {
68 83 m_textItem->setPos(x,y);
69 84 }
70 85
71 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
86 void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
72 87 {
73 88 Q_UNUSED(painter)
74 89 Q_UNUSED(option)
75 90 Q_UNUSED(widget)
76 91 }
77 92
78 QRectF BarValue::boundingRect() const
93 QRectF BarLabel::boundingRect() const
79 94 {
80 95 return m_textItem->boundingRect();
81 96 }
82 97
83 void BarValue::valuesVisibleChanged(bool visible)
98 void BarLabel::labelsVisibleChanged(bool visible)
84 99 {
85 qDebug() << "BarValue visiblle changed:" <<visible;
86 100 setVisible(visible);
87 101 }
88 102
89 #include "moc_barvalue_p.cpp"
103 #include "moc_barlabel_p.cpp"
90 104 QTCOMMERCIALCHART_END_NAMESPACE
@@ -18,8 +18,8
18 18 **
19 19 ****************************************************************************/
20 20
21 #ifndef BARVALUE_P_H
22 #define BARVALUE_P_H
21 #ifndef BARLABEL_P_H
22 #define BARLABEL_P_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include <QGraphicsObject>
@@ -32,11 +32,11 class QBarSet;
32 32
33 33 // Visual class for bar values
34 34 // By default these are not visible.
35 class BarValue : public QGraphicsObject
35 class BarLabel : public QGraphicsObject
36 36 {
37 37 Q_OBJECT
38 38 public:
39 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
39 BarLabel(QBarSet &barSet, QGraphicsItem *parent = 0);
40 40
41 41 void setText(QString str);
42 42 QString text() const;
@@ -44,6 +44,9 public:
44 44 void setPen(const QPen &pen);
45 45 QPen pen() const;
46 46
47 void setBrush(const QBrush &brush);
48 QBrush brush() const;
49
47 50 void setFont(const QFont &font);
48 51 QFont font() const;
49 52
@@ -54,7 +57,7 public:
54 57 QRectF boundingRect() const;
55 58
56 59 public Q_SLOTS:
57 void valuesVisibleChanged(bool visible);
60 void labelsVisibleChanged(bool visible);
58 61
59 62 private:
60 63
@@ -64,4 +67,4 private:
64 67
65 68 QTCOMMERCIALCHART_END_NAMESPACE
66 69
67 #endif // BARVALUE_P_H
70 #endif // BARLABEL_P_H
@@ -20,7 +20,7
20 20
21 21 #include "percentbarchartitem_p.h"
22 22 #include "bar_p.h"
23 #include "barvalue_p.h"
23 #include "barlabel_p.h"
24 24 #include "qbarset.h"
25 25 #include <QDebug>
26 26
@@ -58,21 +58,21 QVector<QRectF> PercentBarChartItem::calculateLayout()
58 58 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
59 59 layout.append(rect);
60 60
61 BarValue* value = m_values.at(itemIndex);
61 BarLabel* label = m_labels.at(itemIndex);
62 62
63 63 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
64 64 int p = m_series->percentageAt(set,category) * 100;
65 65 QString vString(QString::number(p));
66 66 vString.truncate(3);
67 67 vString.append("%");
68 value->setText(vString);
68 label->setText(vString);
69 69 } else {
70 value->setText(QString(""));
70 label->setText(QString(""));
71 71 }
72 72
73 value->setPos(xPos + (rect.width()/2 - value->boundingRect().width()/2)
74 ,yPos - barHeight/2 - value->boundingRect().height()/2);
75 value->setPen(barSet->valuePen());
73 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
74 ,yPos - barHeight/2 - label->boundingRect().height()/2);
75 // value->setFont(barSet->valueFont());
76 76 itemIndex++;
77 77 yPos -= barHeight;
78 78 }
@@ -391,10 +391,10 QBarCategories QBarSeries::categories() const
391 391 return categories;
392 392 }
393 393
394 void QBarSeries::setValuesVisible(bool visible)
394 void QBarSeries::setLabelsVisible(bool visible)
395 395 {
396 396 foreach (QBarSet* s, barSets()) {
397 s->setValuesVisible(visible);
397 s->setLabelsVisible(visible);
398 398 }
399 399 }
400 400
@@ -51,7 +51,7 public:
51 51 QList<QBarSet*> barSets() const;
52 52 QBarCategories categories() const;
53 53
54 void setValuesVisible(bool visible = true);
54 void setLabelsVisible(bool visible = true);
55 55
56 56 bool setModel(QAbstractItemModel *model);
57 57 QAbstractItemModel *modelExt() { return m_model; }
@@ -56,8 +56,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
56 56 */
57 57
58 58 /*!
59 \fn void QBarSet::setValuesVisible(bool visible = true)
60 \brief Sets visibility of bar values. Values are visible, if parameter \a visible is true
59 \fn void QBarSet::setLabelssVisible(bool visible = true)
60 \brief Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets.
61 61 */
62 62
63 63 /*!
@@ -72,6 +72,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
72 72 QBarSet::QBarSet(QString name, QObject *parent)
73 73 : QObject(parent)
74 74 ,m_name(name)
75 ,m_labelsVisible(false)
75 76 {
76 77 }
77 78
@@ -183,27 +184,71 QBrush QBarSet::brush() const
183 184 }
184 185
185 186 /*!
186 Sets the pen for values that are drawn on top of this set
187 Sets pen of the values that are drawn on top of this barset
187 188 */
188 void QBarSet::setValuePen(const QPen &pen)
189 void QBarSet::setLabelPen(const QPen &pen)
189 190 {
190 m_valuePen = pen;
191 m_labelPen = pen;
192 emit valueChanged();
193 }
194
195 /*!
196 Returns pen of the values that are drawn on top of this barset
197 */
198 QPen QBarSet::labelPen() const
199 {
200 return m_labelPen;
201 }
202
203 /*!
204 Sets brush of the values that are drawn on top of this barset
205 */
206 void QBarSet::setLabelBrush(const QBrush &brush)
207 {
208 m_labelBrush = brush;
209 emit valueChanged();
210 }
211
212 /*!
213 Returns brush of the values that are drawn on top of this barset
214 */
215 QBrush QBarSet::labelBrush() const
216 {
217 return m_labelBrush;
218 }
219
220 /*!
221 Sets the pen for values that are drawn on top of this barset
222 */
223 void QBarSet::setLabelFont(const QFont &font)
224 {
225 m_labelFont = font;
226 emit valueChanged();
191 227 }
192 228
193 229 /*!
194 230 Returns the pen for values that are drawn on top of this set
195 231 */
196 QPen QBarSet::valuePen() const
232 QFont QBarSet::labelFont() const
197 233 {
198 return m_valuePen;
234 return m_labelFont;
199 235 }
200 236
201 237 /*!
202 238 Sets the visibility of barset values to \a visible
203 239 */
204 void QBarSet::setValuesVisible(bool visible)
240 void QBarSet::setLabelsVisible(bool visible)
241 {
242 m_labelsVisible = visible;
243 emit labelsVisibleChanged(visible);
244 }
245
246 /*!
247 Returns the visibility of values
248 */
249 bool QBarSet::labelsVisible() const
205 250 {
206 emit valuesVisibleChanged(visible);
251 return m_labelsVisible;
207 252 }
208 253
209 254 /*!
@@ -24,6 +24,7
24 24 #include <qchartglobal.h>
25 25 #include <QPen>
26 26 #include <QBrush>
27 #include <QFont>
27 28
28 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 30
@@ -55,10 +56,17 public:
55 56 void setBrush(const QBrush &brush);
56 57 QBrush brush() const;
57 58
58 void setValuePen(const QPen &pen);
59 QPen valuePen() const;
59 void setLabelPen(const QPen &pen);
60 QPen labelPen() const;
60 61
61 void setValuesVisible(bool visible = true);
62 void setLabelBrush(const QBrush &brush);
63 QBrush labelBrush() const;
64
65 void setLabelFont(const QFont &font);
66 QFont labelFont() const;
67
68 void setLabelsVisible(bool visible = true);
69 bool labelsVisible() const;
62 70
63 71 Q_SIGNALS:
64 72 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
@@ -69,7 +77,7 Q_SIGNALS:
69 77 void hoverEnter(QPoint pos);
70 78 void hoverLeave();
71 79 void showToolTip(QPoint pos, QString tip); // Private signal
72 void valuesVisibleChanged(bool visible);
80 void labelsVisibleChanged(bool visible);
73 81 // <--- TO PIMPL
74 82
75 83 public Q_SLOTS:
@@ -86,7 +94,10 private:
86 94 QMap<QString, qreal> m_mappedValues;
87 95 QPen m_pen;
88 96 QBrush m_brush;
89 QPen m_valuePen;
97 QPen m_labelPen;
98 QBrush m_labelBrush;
99 QFont m_labelFont;
100 bool m_labelsVisible;
90 101 };
91 102
92 103 QTCOMMERCIALCHART_END_NAMESPACE
@@ -20,7 +20,7
20 20
21 21 #include "stackedbarchartitem_p.h"
22 22 #include "bar_p.h"
23 #include "barvalue_p.h"
23 #include "barlabel_p.h"
24 24 #include "qbarset.h"
25 25 #include <QDebug>
26 26
@@ -60,25 +60,24 QVector<QRectF> StackedBarChartItem::calculateLayout()
60 60 for (int set=0; set < m_series->barsetCount(); set++) {
61 61 QBarSet* barSet = m_series->barsetAt(set);
62 62
63 qreal barHeight = barSet->valueAt(category) * scale; //m_series->valueAt(set, category) * scale;
63 qreal barHeight = barSet->valueAt(category) * scale;
64 64 Bar* bar = m_bars.at(itemIndex);
65 65 bar->setPen(barSet->pen());
66 66 bar->setBrush(barSet->brush());
67 67 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
68 68 layout.append(rect);
69 69
70 BarValue* value = m_values.at(itemIndex);
70 BarLabel* label = m_labels.at(itemIndex);
71 71
72 72 if (!qFuzzyIsNull(barSet->valueAt(category))) {
73 value->setText(QString::number(barSet->valueAt(category)));
73 label->setText(QString::number(barSet->valueAt(category)));
74 74 } else {
75 value->setText(QString(""));
75 label->setText(QString(""));
76 76 }
77 77
78 value->setPos(xPos + (rect.width()/2 - value->boundingRect().width()/2)
79 ,yPos - barHeight/2 - value->boundingRect().height()/2);
80 value->setPen(barSet->valuePen());
81
78 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
79 ,yPos - barHeight/2 - label->boundingRect().height()/2);
80 // value->setFont(barSet->valueFont());
82 81 itemIndex++;
83 82 yPos -= barHeight;
84 83 }
@@ -183,9 +183,9 void ChartTheme::decorate(QBarSeries* series, int index, bool force)
183 183 // Pick label color from the opposite end of the gradient.
184 184 // 0.3 as a boundary seems to work well.
185 185 if (takeAtPos < 0.3)
186 sets.at(i)->setValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
186 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
187 187 else
188 sets.at(i)->setValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
188 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
189 189
190 190 if (pen == sets.at(i)->pen() || force) {
191 191 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
General Comments 0
You need to be logged in to leave comments. Login now