7 Graph::Graph(QQuickItem
*parent
)
8 : QQuickPaintedItem(parent
)
9 , primary(QColor("#ff0000"))
10 , secondary(QColor("#00ff00"))
11 , highlight(QColor("#0000ff"))
29 void Graph::paint(QPainter
*painter
) {
40 QRectF size
= contentsBoundingRect();
42 bars
= model
.length();
43 labelygap
= size
.height() * 0.1l;
44 labelxgap
= size
.width() * 0.11;
45 axiswidth
= qMin(size
.width() * axisThickness
, size
.height() * axisThickness
);
46 barfullwidth
= ((size
.width() - labelxgap
- axiswidth
) / bars
);
47 barwidth
= barfullwidth
* (1.0 - gap
);
48 labels
= labelsy
.length() > 0 ? labelsy
.length() : 1 + (maxy
- miny
) / stepy
;
49 labelheight
= (size
.height() - labelygap
- axiswidth
) / (labels
- 0.5);
51 QBrush
axiscolour(primary
);
52 QBrush
barcolour(secondary
);
54 painter
->setBrush(axiscolour
);
55 painter
->setPen(Qt::NoPen
);
56 painter
->setRenderHint(QPainter::Antialiasing
);
57 painter
->setOpacity(1.0);
59 const QPointF points
[6] = {
60 QPointF(labelxgap
, (labelheight
/ 2.0)),
61 QPointF(labelxgap
, size
.height() - labelygap
),
62 QPointF(size
.width(), size
.height() - labelygap
),
63 QPointF(size
.width(), size
.height() - labelygap
- axiswidth
),
64 QPointF(labelxgap
+ axiswidth
, size
.height() - labelygap
- axiswidth
),
65 QPointF(labelxgap
+ axiswidth
, (labelheight
/ 2.0))
67 painter
->drawPolygon(points
, 6);
70 painter
->setBrush(barcolour
);
73 for (QList
<float>::const_iterator iter
= model
.constBegin(); (count
< bars
) && (iter
!= model
.constEnd()); iter
++) {
74 float barheight
= ((*iter
) - miny
) / (maxy
- miny
);
75 barheight
= qBound(0.0f
, barheight
, 1.0f
) * (size
.height() - labelygap
- axiswidth
- (labelheight
/ 2.0));
77 painter
->drawRect(labelxgap
+ axiswidth
+ (barfullwidth
* count
) + (barfullwidth
* gap
/ 2.0), size
.height() - labelygap
- barheight
- axiswidth
, barwidth
, barheight
);
83 QFont font
= painter
->font();
84 font
.setPixelSize(fontsize
);
85 painter
->setFont(font
);
86 painter
->setPen(primary
);
87 painter
->setBrush(Qt::NoBrush
);
89 if ((bars
> 0) && (labelsx
.length() == bars
)) {
90 for (count
= 0; count
< bars
; count
++) {
91 QRectF
rect(labelxgap
+ axiswidth
+ (barfullwidth
* count
), size
.height() - labelygap
+ 8.0, barfullwidth
, labelygap
- 8.0);
92 painter
->drawText(rect
, Qt::AlignHCenter
| Qt::NoClip
| Qt::TextSingleLine
, labelsx
[count
]);
93 //painter->drawRect(rect);
97 if (labelsy
.length() > 0) {
98 for (count
= 0; count
< labels
; count
++) {
99 QRectF
rect(0, size
.height() - (labelheight
* (count
+ 0.5)) - labelygap
, labelxgap
- 8.0, labelheight
);
100 painter
->drawText(rect
, Qt::AlignVCenter
| Qt::AlignRight
| Qt::NoClip
| Qt::TextSingleLine
, labelsy
[count
]);
101 //painter->drawRect(rect);
105 float labelvalue
= miny
;
106 for (count
= 0; count
< labels
; count
++) {
107 QString labeltext
= unitsy
== "%" ? locale
.toString(labelvalue
* 100, 'f', 0) + "%" : locale
.toString(labelvalue
, 'g', 2) + unitsy
;
108 QRectF
rect(0, size
.height() - (labelheight
* (count
+ 0.5)) - labelygap
, labelxgap
- 8.0, labelheight
);
109 painter
->drawText(rect
, Qt::AlignVCenter
| Qt::AlignRight
| Qt::NoClip
| Qt::TextSingleLine
, labeltext
);
110 //painter->drawRect(rect);
117 QList
<float> Graph::getModel() const {
121 void Graph::setModel(QList
<float> value
) {
125 if (model
.length() > 0) {
128 foreach (int y
, model
) {
139 QStringList
Graph::getLabelsx() const {
143 void Graph::setLabelsx(QStringList value
) {
145 emit
labelsxChanged();
148 QStringList
Graph::getLabelsy() const {
152 void Graph::setLabelsy(QStringList value
) {
154 emit
labelsyChanged();
157 QColor
Graph::getPrimary() const {
158 return this->primary
;
161 void Graph::setPrimary(QColor value
) {
162 if (primary
!= value
) {
164 emit
primaryChanged(value
);
168 QColor
Graph::getSecondary() const {
169 return this->secondary
;
172 void Graph::setSecondary(QColor value
) {
173 if (secondary
!= value
) {
175 emit
secondaryChanged(value
);
179 QColor
Graph::getHighlight() const {
180 return this->highlight
;
183 void Graph::setHighlight(QColor value
) {
184 if (highlight
!= value
) {
186 emit
highlightChanged(value
);
190 float Graph::getGap() const {
194 void Graph::setGap(float value
) {
197 emit
gapChanged(value
);
201 float Graph::getMiny() const {
205 void Graph::setMiny(float value
) {
208 emit
minyChanged(value
);
212 float Graph::getMaxy() const {
216 void Graph::setMaxy(float value
) {
219 emit
maxyChanged(value
);
223 float Graph::getStepy() const {
227 void Graph::setStepy(float value
) {
228 if (stepy
!= value
) {
230 emit
stepyChanged(value
);
234 QString
Graph::getUnitsy() const {
238 void Graph::setUnitsy(QString value
) {
239 if (unitsy
!= value
) {
241 emit
unitsyChanged();
245 float Graph::getFontsize() const {
249 void Graph::setFontsize(float value
) {
250 if (fontsize
!= value
) {
252 emit
fontsizeChanged(value
);
256 float Graph::getAnimate() const {
260 void Graph::setAnimate(float value
) {
261 if (animate
!= value
) {
263 emit
animateChanged(value
);