4 #include <QFontMetrics> 12 BarGraph::BarGraph(QWidget *parent) :
21 BarGraph::~BarGraph() {}
23 void BarGraph::setLogScale(
bool b) {
28 void BarGraph::setData(std::vector<double> val)
30 min_val = *std::min_element(val.begin(),val.end());
31 max_val = *std::max_element(val.begin(),val.end());
33 for (
size_t i = 0; i < val.size(); i++)
34 values.push_back(val[i]);
36 setMinimumSize( (
int)(margin*values.size()*2),200+margin*5);
39 void BarGraph::paintBargraph(QPainter &painter)
44 painter.setBrush(Qt::blue);
46 for(
size_t i=0, s = values.size(); i < s; ++i)
48 painter.drawRect(bars[i]);
50 painter.drawText(barwidth*(i+0.5)+margin*(i+1),height()- 20,QString::number(i));
53 painter.drawText(2*margin,height()- 5,
"Eigenvalues");
55 int num_steps = use_log_?(
static_cast<int>(log10(max_val)) -
56 static_cast<int>(log10(min_val))+1):5;
57 num_steps = std::max(1,num_steps);
58 int start =
static_cast<int>(use_log_?log10(min_val):0);
59 int separation = (height() - 45) / num_steps;
60 for (
int i = 0; i < num_steps; i++) {
63 ss <<
"_1e" << (start+i);
64 painter.drawText(width() - 45,
65 height() - 75 - separation * i,QString(ss.str().c_str()));
67 ss <<
"_" <<
static_cast<int>(start + (max_val / num_steps) * i);
68 painter.drawText(width() - 45,
69 height() - 45 - separation * i,QString(ss.str().c_str()));
74 void BarGraph::paintEvent(QPaintEvent *event)
76 QPainter painter(
this);
77 paintBargraph(painter);
81 void BarGraph::resizeEvent(QResizeEvent *event)
86 void BarGraph::recalcBasicValues()
88 int sz = std::max(1,static_cast<int>(values.size()));
89 barwidth = std::max(margin, (
int)((width()-(margin*values.size()))/sz));
90 int h = height() - 45;
91 if(bars.size() != values.size())
92 bars.resize(values.size());
94 for(
size_t i=0, s = values.size(); i < s; ++i)
96 double val = use_log_?(log10(values[i])-log10(min_val)):(values[i]-min_val);
97 double range = use_log_?(log10(max_val) - log10(min_val)):(max_val - min_val);
98 int barheight =
static_cast<int>(val * h / (range));
99 bars[i].setRect(x,5 + h - barheight,barwidth, barheight);
100 x += margin + barwidth;