63 QPainter painter(
this);
64 painter.setRenderHint(QPainter::Antialiasing);
66 const int w = width();
67 const int h = height();
68 const int marginLeft = 60;
69 const int marginRight = 20;
70 const int marginTop = 40;
71 const int marginBottom = 100;
72 const int plotW = w - marginLeft - marginRight;
73 const int plotH = h - marginTop - marginBottom;
75 if (plotW <= 0 || plotH <= 0)
79 if (!m_sTitle.isEmpty()) {
80 painter.setPen(Qt::black);
81 QFont titleFont = painter.font();
82 titleFont.setBold(
true);
83 titleFont.setPointSize(titleFont.pointSize() + 2);
84 painter.setFont(titleFont);
85 painter.drawText(QRect(0, 0, w, marginTop), Qt::AlignCenter, m_sTitle);
86 painter.setFont(QFont());
89 if (m_frequencies.isEmpty())
92 const int nBars = m_frequencies.size();
93 const int maxFreq = (m_iMaxFrequency > 0) ? m_iMaxFrequency : 1;
96 painter.setPen(QPen(Qt::black, 1));
97 painter.drawLine(marginLeft, marginTop, marginLeft, marginTop + plotH);
98 painter.drawLine(marginLeft, marginTop + plotH, marginLeft + plotW, marginTop + plotH);
101 const int nYTicks = 5;
102 for (
int i = 0; i <= nYTicks; ++i) {
103 int yVal = maxFreq * i / nYTicks;
104 int y = marginTop + plotH - (plotH * i / nYTicks);
105 painter.drawLine(marginLeft - 5, y, marginLeft, y);
106 painter.drawText(QRect(0, y - 10, marginLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter, QString::number(yVal));
110 double barWidth =
static_cast<double>(plotW) / nBars;
111 double barPadding = barWidth * 0.1;
113 for (
int i = 0; i < nBars; ++i) {
114 double barH = (
static_cast<double>(m_frequencies[i]) / maxFreq) * plotH;
115 double x = marginLeft + i * barWidth + barPadding;
116 double bw = barWidth - 2.0 * barPadding;
117 double y = marginTop + plotH - barH;
119 painter.setBrush(QColor(70, 130, 180));
120 painter.setPen(QPen(QColor(50, 100, 150), 1));
121 painter.drawRect(QRectF(x, y, bw, barH));
125 painter.setPen(Qt::black);
126 QFont smallFont = painter.font();
127 smallFont.setPointSize(smallFont.pointSize() - 1);
128 painter.setFont(smallFont);
130 for (
int i = 0; i < nBars && i < m_categories.size(); ++i) {
131 double cx = marginLeft + (i + 0.5) * barWidth;
133 painter.translate(cx, marginTop + plotH + 5);
135 painter.drawText(0, 0, m_categories[i]);
140 if (!m_sLegend.isEmpty()) {
141 painter.setFont(QFont());
142 painter.setPen(Qt::black);
143 painter.drawText(QRect(0, h - 20, w, 20), Qt::AlignCenter, m_sLegend);