169 QPainter painter(
this);
170 painter.setRenderHint(QPainter::Antialiasing);
172 const int w = width();
173 const int h = height();
174 const int mLeft = 60;
175 const int mRight = 20;
177 const int mBottom = 40;
178 const int plotW = w - mLeft - mRight;
179 const int plotH = h - mTop - mBottom;
181 if (plotW <= 0 || plotH <= 0)
185 if (!m_sTitle.isEmpty()) {
186 QFont titleFont = painter.font();
187 titleFont.setBold(
true);
188 titleFont.setPointSize(titleFont.pointSize() + 2);
189 painter.setFont(titleFont);
190 painter.setPen(Qt::black);
191 painter.drawText(QRect(0, 0, w, mTop), Qt::AlignCenter, m_sTitle);
192 painter.setFont(QFont());
196 painter.setPen(QPen(Qt::black, 1));
197 painter.drawLine(mLeft, mTop, mLeft, mTop + plotH);
198 painter.drawLine(mLeft, mTop + plotH, mLeft + plotW, mTop + plotH);
201 if (!m_sYLabel.isEmpty()) {
203 painter.translate(12, mTop + plotH / 2);
205 painter.drawText(QRect(-plotH / 2, 0, plotH, 15), Qt::AlignCenter, m_sYLabel);
210 if (!m_sXLabel.isEmpty()) {
211 painter.drawText(QRect(mLeft, h - 15, plotW, 15), Qt::AlignCenter, m_sXLabel);
214 double rangeX = m_dMaxX - m_dMinX;
215 double rangeY = m_dMaxY - m_dMinY;
216 if (rangeX == 0) rangeX = 1.0;
217 if (rangeY == 0) rangeY = 1.0;
220 const int nYTicks = 5;
221 for (
int i = 0; i <= nYTicks; ++i) {
222 double yVal = m_dMinY + rangeY * i / nYTicks;
223 int y = mTop + plotH -
static_cast<int>(plotH *
static_cast<double>(i) / nYTicks);
224 painter.drawLine(mLeft - 5, y, mLeft, y);
225 painter.drawText(QRect(0, y - 10, mLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter,
226 QString::number(yVal,
'g', 4));
230 const int nXTicks = 5;
231 for (
int i = 0; i <= nXTicks; ++i) {
232 double xVal = m_dMinX + rangeX * i / nXTicks;
233 int x = mLeft +
static_cast<int>(plotW *
static_cast<double>(i) / nXTicks);
234 painter.drawLine(x, mTop + plotH, x, mTop + plotH + 5);
235 painter.drawText(QRect(x - 30, mTop + plotH + 6, 60, 20), Qt::AlignCenter,
236 QString::number(xVal,
'g', 4));
239 if (m_vecXData.isEmpty() || m_vecYData.isEmpty())
244 auto toPixelX = [&](
double dx) ->
double {
245 return mLeft + ((dx - m_dMinX) / rangeX) * plotW;
247 auto toPixelY = [&](
double dy) ->
double {
248 return mTop + plotH - ((dy - m_dMinY) / rangeY) * plotH;
251 int n = qMin(m_vecXData.size(), m_vecYData.size());
252 path.moveTo(toPixelX(m_vecXData[0]), toPixelY(m_vecYData[0]));
253 for (
int i = 1; i < n; ++i) {
254 path.lineTo(toPixelX(m_vecXData[i]), toPixelY(m_vecYData[i]));
257 painter.setPen(QPen(QColor(70, 130, 180), 2));
258 painter.setBrush(Qt::NoBrush);
259 painter.drawPath(path);