77 QPainter painter(
this);
78 painter.setRenderHint(QPainter::Antialiasing);
80 const int w = width();
81 const int h = height();
82 const int marginLeft = 60;
83 const int marginRight = 20;
84 const int marginTop = 40;
85 const int marginBottom = 100;
86 const int plotW = w - marginLeft - marginRight;
87 const int plotH = h - marginTop - marginBottom;
89 if (plotW <= 0 || plotH <= 0)
93 if (!m_sTitle.isEmpty()) {
94 painter.setPen(Qt::black);
95 QFont titleFont = painter.font();
96 titleFont.setBold(
true);
97 titleFont.setPointSize(titleFont.pointSize() + 2);
98 painter.setFont(titleFont);
99 painter.drawText(QRect(0, 0, w, marginTop), Qt::AlignCenter, m_sTitle);
100 painter.setFont(QFont());
103 if (m_frequencies.isEmpty())
106 const int nBars = m_frequencies.size();
107 const int maxFreq = (m_iMaxFrequency > 0) ? m_iMaxFrequency : 1;
110 painter.setPen(QPen(Qt::black, 1));
111 painter.drawLine(marginLeft, marginTop, marginLeft, marginTop + plotH);
112 painter.drawLine(marginLeft, marginTop + plotH, marginLeft + plotW, marginTop + plotH);
115 const int nYTicks = 5;
116 for (
int i = 0; i <= nYTicks; ++i) {
117 int yVal = maxFreq * i / nYTicks;
118 int y = marginTop + plotH - (plotH * i / nYTicks);
119 painter.drawLine(marginLeft - 5, y, marginLeft, y);
120 painter.drawText(QRect(0, y - 10, marginLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter, QString::number(yVal));
124 double barWidth =
static_cast<double>(plotW) / nBars;
125 double barPadding = barWidth * 0.1;
127 for (
int i = 0; i < nBars; ++i) {
128 double barH = (
static_cast<double>(m_frequencies[i]) / maxFreq) * plotH;
129 double x = marginLeft + i * barWidth + barPadding;
130 double bw = barWidth - 2.0 * barPadding;
131 double y = marginTop + plotH - barH;
133 painter.setBrush(QColor(70, 130, 180));
134 painter.setPen(QPen(QColor(50, 100, 150), 1));
135 painter.drawRect(QRectF(x, y, bw, barH));
139 painter.setPen(Qt::black);
140 QFont smallFont = painter.font();
141 smallFont.setPointSize(smallFont.pointSize() - 1);
142 painter.setFont(smallFont);
144 for (
int i = 0; i < nBars && i < m_categories.size(); ++i) {
145 double cx = marginLeft + (i + 0.5) * barWidth;
147 painter.translate(cx, marginTop + plotH + 5);
149 painter.drawText(0, 0, m_categories[i]);
154 if (!m_sLegend.isEmpty()) {
155 painter.setFont(QFont());
156 painter.setPen(Qt::black);
157 painter.drawText(QRect(0, h - 20, w, 20), Qt::AlignCenter, m_sLegend);