189 QPainter painter(
this);
190 painter.setRenderHint(QPainter::Antialiasing);
192 const int w = width();
193 const int h = height();
194 const int mLeft = 60;
195 const int mRight = 20;
197 const int mBottom = 40;
198 const int plotW = w - mLeft - mRight;
199 const int plotH = h - mTop - mBottom;
201 if (plotW <= 0 || plotH <= 0)
205 if (!m_sTitle.isEmpty()) {
206 QFont titleFont = painter.font();
207 titleFont.setBold(
true);
208 titleFont.setPointSize(titleFont.pointSize() + 2);
209 painter.setFont(titleFont);
210 painter.setPen(Qt::black);
211 painter.drawText(QRect(0, 0, w, mTop), Qt::AlignCenter, m_sTitle);
212 painter.setFont(QFont());
216 painter.setPen(QPen(Qt::black, 1));
217 painter.drawLine(mLeft, mTop, mLeft, mTop + plotH);
218 painter.drawLine(mLeft, mTop + plotH, mLeft + plotW, mTop + plotH);
221 if (!m_sYLabel.isEmpty()) {
223 painter.translate(12, mTop + plotH / 2);
225 painter.drawText(QRect(-plotH / 2, 0, plotH, 15), Qt::AlignCenter, m_sYLabel);
230 if (!m_sXLabel.isEmpty()) {
231 painter.drawText(QRect(mLeft, h - 15, plotW, 15), Qt::AlignCenter, m_sXLabel);
234 double rangeX = m_dMaxX - m_dMinX;
235 double rangeY = m_dMaxY - m_dMinY;
236 if (rangeX == 0) rangeX = 1.0;
237 if (rangeY == 0) rangeY = 1.0;
240 const int nYTicks = 5;
241 for (
int i = 0; i <= nYTicks; ++i) {
242 double yVal = m_dMinY + rangeY * i / nYTicks;
243 int y = mTop + plotH -
static_cast<int>(plotH *
static_cast<double>(i) / nYTicks);
244 painter.drawLine(mLeft - 5, y, mLeft, y);
245 painter.drawText(QRect(0, y - 10, mLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter,
246 QString::number(yVal,
'g', 4));
250 const int nXTicks = 5;
251 for (
int i = 0; i <= nXTicks; ++i) {
252 double xVal = m_dMinX + rangeX * i / nXTicks;
253 int x = mLeft +
static_cast<int>(plotW *
static_cast<double>(i) / nXTicks);
254 painter.drawLine(x, mTop + plotH, x, mTop + plotH + 5);
255 painter.drawText(QRect(x - 30, mTop + plotH + 6, 60, 20), Qt::AlignCenter,
256 QString::number(xVal,
'g', 4));
259 if (m_vecXData.isEmpty() || m_vecYData.isEmpty())
264 auto toPixelX = [&](
double dx) ->
double {
265 return mLeft + ((dx - m_dMinX) / rangeX) * plotW;
267 auto toPixelY = [&](
double dy) ->
double {
268 return mTop + plotH - ((dy - m_dMinY) / rangeY) * plotH;
271 int n = qMin(m_vecXData.size(), m_vecYData.size());
272 path.moveTo(toPixelX(m_vecXData[0]), toPixelY(m_vecYData[0]));
273 for (
int i = 1; i < n; ++i) {
274 path.lineTo(toPixelX(m_vecXData[i]), toPixelY(m_vecYData[i]));
277 painter.setPen(QPen(QColor(70, 130, 180), 2));
278 painter.setBrush(Qt::NoBrush);
279 painter.drawPath(path);