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
) {
44 QRectF size
= contentsBoundingRect();
46 bars
= bardata
.length();
47 points
= linedata
.length();
48 labelsxcount
= labelsx
.length();
50 steps
= labelsx
.length();
55 labelygap
= size
.height() * 0.1l;
56 labelxgap
= size
.width() * 0.11;
57 axiswidth
= qMin(size
.width() * axisThickness
, size
.height() * axisThickness
);
58 barfullwidth
= ((size
.width() - labelxgap
- axiswidth
) / bars
);
59 barwidth
= barfullwidth
* (1.0 - gap
);
60 pointfullwidth
= ((size
.width() - labelxgap
- axiswidth
) / points
);
61 labelsycount
= labelsy
.length() > 0 ? labelsy
.length() : 1 + (maxy
- miny
) / stepy
;
62 labelheight
= (size
.height() - labelygap
- axiswidth
) / (labelsycount
- 0.5);
63 labelfullwidth
= ((size
.width() - labelxgap
- axiswidth
) / labelsxcount
);
65 QBrush
axiscolour(primary
);
66 QBrush
barcolour(secondary
);
67 QBrush
linecolour(highlight
);
69 painter
->setBrush(axiscolour
);
70 painter
->setPen(Qt::NoPen
);
71 painter
->setRenderHint(QPainter::Antialiasing
);
72 painter
->setOpacity(1.0);
74 const QPointF axis
[6] = {
75 QPointF(labelxgap
, (labelheight
/ 2.0)),
76 QPointF(labelxgap
, size
.height() - labelygap
),
77 QPointF(size
.width(), size
.height() - labelygap
),
78 QPointF(size
.width(), size
.height() - labelygap
- axiswidth
),
79 QPointF(labelxgap
+ axiswidth
, size
.height() - labelygap
- axiswidth
),
80 QPointF(labelxgap
+ axiswidth
, (labelheight
/ 2.0))
82 painter
->drawPolygon(axis
, 6);
85 painter
->setBrush(barcolour
);
88 for (QList
<float>::const_iterator iter
= bardata
.constBegin(); (count
< bars
) && (iter
!= bardata
.constEnd()); iter
++) {
89 float barheight
= ((*iter
) - miny
) / (maxy
- miny
);
90 barheight
= qBound(0.0f
, barheight
, 1.0f
) * (size
.height() - labelygap
- axiswidth
- (labelheight
/ 2.0));
92 painter
->drawRect(labelxgap
+ axiswidth
+ (barfullwidth
* count
) + (barfullwidth
* gap
/ 2.0), size
.height() - labelygap
- barheight
- axiswidth
, barwidth
, barheight
);
99 QPen pen
= QPen(linecolour
, 8.0, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
100 painter
->setPen(pen
);
101 painter
->setBrush(Qt::NoBrush
);
103 QPointF point
[points
];
105 for (QList
<float>::const_iterator iter
= linedata
.constBegin(); (count
< points
) && (iter
!= linedata
.constEnd()); iter
++) {
106 float lineheight
= ((*iter
) - miny
) / (maxy
- miny
);
107 lineheight
= qBound(0.0f
, lineheight
, 1.0f
) * (size
.height() - labelygap
- axiswidth
- (labelheight
/ 2.0));
108 point
[count
] = QPointF(labelxgap
+ axiswidth
+ (pointfullwidth
* count
) + (pointfullwidth
/ 2.0), size
.height() - labelygap
- lineheight
- axiswidth
);
111 painter
->setClipRect(0, 0, labelxgap
+ axiswidth
+ (pointfullwidth
/ 2.0) - 8.0 + (size
.width() - labelxgap
- axiswidth
- pointfullwidth
+ 16.0) * animate
, size
.height(), Qt::ReplaceClip
);
112 painter
->setClipping(true);
113 painter
->drawPolyline(point
, points
);
116 painter
->setClipping(false);
118 QFont font
= painter
->font();
119 font
.setPixelSize(fontsize
);
120 painter
->setFont(font
);
121 painter
->setPen(primary
);
122 painter
->setBrush(Qt::NoBrush
);
124 if (labelsxcount
> 0) {
125 for (count
= 0; count
< labelsxcount
; count
++) {
126 QRectF
rect(labelxgap
+ axiswidth
+ (labelfullwidth
* count
), size
.height() - labelygap
+ 8.0, labelfullwidth
, labelygap
- 8.0);
127 painter
->drawText(rect
, Qt::AlignHCenter
| Qt::NoClip
| Qt::TextSingleLine
, labelsx
[count
]);
128 //painter->drawRect(rect);
132 if (labelsy
.length() > 0) {
133 for (count
= 0; count
< labelsycount
; count
++) {
134 QRectF
rect(0, size
.height() - (labelheight
* (count
+ 0.5)) - labelygap
, labelxgap
- 8.0, labelheight
);
135 painter
->drawText(rect
, Qt::AlignVCenter
| Qt::AlignRight
| Qt::NoClip
| Qt::TextSingleLine
, labelsy
[count
]);
136 //painter->drawRect(rect);
140 float labelvalue
= miny
;
141 for (count
= 0; count
< labelsycount
; count
++) {
142 QString labeltext
= unitsy
== "%" ? locale
.toString(labelvalue
* 100, 'f', 0) + "%" : locale
.toString(labelvalue
, 'g', 2) + unitsy
;
143 QRectF
rect(0, size
.height() - (labelheight
* (count
+ 0.5)) - labelygap
, labelxgap
- 8.0, labelheight
);
144 painter
->drawText(rect
, Qt::AlignVCenter
| Qt::AlignRight
| Qt::NoClip
| Qt::TextSingleLine
, labeltext
);
145 //painter->drawRect(rect);
152 QList
<float> Graph::getBarData() const {
156 void Graph::setBarData(QList
<float> value
) {
158 emit
barDataChanged();
161 QList
<float> Graph::getLineData() const {
165 void Graph::setLineData(QList
<float> value
) {
167 emit
lineDataChanged();
170 QStringList
Graph::getLabelsx() const {
174 void Graph::setLabelsx(QStringList value
) {
176 emit
labelsxChanged();
179 QStringList
Graph::getLabelsy() const {
183 void Graph::setLabelsy(QStringList value
) {
185 emit
labelsyChanged();
188 QColor
Graph::getPrimary() const {
189 return this->primary
;
192 void Graph::setPrimary(QColor value
) {
193 if (primary
!= value
) {
195 emit
primaryChanged(value
);
199 QColor
Graph::getSecondary() const {
200 return this->secondary
;
203 void Graph::setSecondary(QColor value
) {
204 if (secondary
!= value
) {
206 emit
secondaryChanged(value
);
210 QColor
Graph::getHighlight() const {
211 return this->highlight
;
214 void Graph::setHighlight(QColor value
) {
215 if (highlight
!= value
) {
217 emit
highlightChanged(value
);
221 float Graph::getGap() const {
225 void Graph::setGap(float value
) {
228 emit
gapChanged(value
);
232 float Graph::getMiny() const {
236 void Graph::setMiny(float value
) {
239 emit
minyChanged(value
);
243 float Graph::getMaxy() const {
247 void Graph::setMaxy(float value
) {
250 emit
maxyChanged(value
);
254 float Graph::getStepy() const {
258 void Graph::setStepy(float value
) {
259 if (stepy
!= value
) {
261 emit
stepyChanged(value
);
265 QString
Graph::getUnitsy() const {
269 void Graph::setUnitsy(QString value
) {
270 if (unitsy
!= value
) {
272 emit
unitsyChanged();
276 float Graph::getFontsize() const {
280 void Graph::setFontsize(float value
) {
281 if (fontsize
!= value
) {
283 emit
fontsizeChanged(value
);
287 float Graph::getAnimate() const {
291 void Graph::setAnimate(float value
) {
292 if (animate
!= value
) {
294 emit
animateChanged(value
);