1
/***************************************************************************
4
--------------------------------------------------------------------
5
Copyright : (C) 2006 by Ion Vasilief, Tilman Benkert
6
Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
7
Description : Bar curve
9
***************************************************************************/
11
/***************************************************************************
13
* This program is free software; you can redistribute it and/or modify *
14
* it under the terms of the GNU General Public License as published by *
15
* the Free Software Foundation; either version 2 of the License, or *
16
* (at your option) any later version. *
18
* This program is distributed in the hope that it will be useful, *
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21
* GNU General Public License for more details. *
23
* You should have received a copy of the GNU General Public License *
24
* along with this program; if not, write to the Free Software *
25
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26
* Boston, MA 02110-1301 USA *
28
***************************************************************************/
29
#include "QwtBarCurve.h"
32
QwtBarCurve::QwtBarCurve(BarStyle style, Table *t, const QString& xColName, const char *name, int startRow, int endRow):
33
DataCurve(t, xColName, name, startRow, endRow)
39
setPen(QPen(Qt::black, 1, Qt::SolidLine));
40
setBrush(QBrush(Qt::red));
42
if (bar_style == Vertical)
43
setType(Graph::VerticalBars);
45
setType(Graph::HorizontalBars);
48
void QwtBarCurve::copy(const QwtBarCurve *b)
51
bar_offset = b->bar_offset;
52
bar_style = b->bar_style;
55
void QwtBarCurve::draw(QPainter *painter,
56
const QwtScaleMap &xMap, const QwtScaleMap &yMap, int from, int to) const
58
if ( !painter || dataSize() <= 0 )
65
painter->setPen(QwtPlotCurve::pen());
66
painter->setBrush(QwtPlotCurve::brush());
71
if (bar_style == Vertical)
72
ref= yMap.transform(1e-100); //smalest positive value for log scales
74
ref= xMap.transform(1e-100);
76
if (bar_style == Vertical)
78
dx = abs(xMap.transform(x(from+1))-xMap.transform(x(from)));
79
for (int i=from+2; i<to; i++)
81
int min = abs(xMap.transform(x(i+1))-xMap.transform(x(i)));
85
bar_width = dx*(1-bar_gap*0.01);
89
dy = abs(yMap.transform(y(from+1))-yMap.transform(y(from)));
90
for (int i=from+2; i<to; i++)
92
int min = abs(yMap.transform(y(i+1))-yMap.transform(y(i)));
96
bar_width = dy*(1-bar_gap*0.01);
99
const int half_width = int((0.5-bar_offset*0.01)*bar_width);
100
int bw1 = int(bar_width) + 1;
101
for (int i=from; i<=to; i++)
103
const int px = xMap.transform(x(i));
104
const int py = yMap.transform(y(i));
106
if (bar_style == Vertical)
109
painter->drawRect(px-half_width, ref, bw1, (py-ref));
111
painter->drawRect(px-half_width, py, bw1, (ref-py+1));
116
painter->drawRect(px, py-half_width, (ref-px), bw1);
118
painter->drawRect(ref, py-half_width, (px-ref), bw1);
124
QwtDoubleRect QwtBarCurve::boundingRect() const
126
QwtDoubleRect rect = QwtPlotCurve::boundingRect();
127
double n= (double)dataSize();
129
if (bar_style == Vertical)
131
double dx=(rect.right()-rect.left())/n;
132
rect.setLeft(rect.left()-dx);
133
rect.setRight(rect.right()+dx);
137
double dy=(rect.bottom()-rect.top())/n;
138
rect.setTop(rect.top()-dy);
139
rect.setBottom(rect.bottom()+dy);
145
void QwtBarCurve::setGap (int gap)
153
void QwtBarCurve::setOffset(int offset)
155
if (bar_offset == offset)
161
double QwtBarCurve::dataOffset()
163
if (bar_style == Vertical)
165
const QwtScaleMap &xMap = plot()->canvasMap(xAxis());
166
int dx = abs(xMap.transform(x(1))-xMap.transform(x(0)));
167
double bar_width = dx*(1-bar_gap*0.01);
168
if (plot()->isVisible())
170
for (int i = 2; i<dataSize(); i++)
172
int min = abs(xMap.transform(x(i))-xMap.transform(x(i-1)));
176
int x1 = xMap.transform(minXValue()) + int(bar_offset*0.01*bar_width);
177
return xMap.invTransform(x1) - minXValue();
180
return 0.5*bar_offset*0.01*bar_width;
184
const QwtScaleMap &yMap = plot()->canvasMap(yAxis());
185
int dy = abs(yMap.transform(y(1))-yMap.transform(y(0)));
186
double bar_width = dy*(1-bar_gap*0.01);
187
if (plot()->isVisible())
189
for (int i=2; i<dataSize(); i++)
191
int min = abs(yMap.transform(y(i))-yMap.transform(y(i-1)));
195
int y1 = yMap.transform(minYValue()) + int(bar_offset*0.01*bar_width);
196
return yMap.invTransform(y1) - minYValue();
199
return 0.5*bar_offset*0.01*bar_width;