173 QPainter painter(
this);
174 painter.setRenderHint(QPainter::Antialiasing);
176 const int w = width();
177 const int h = height();
178 const int mLeft = 60;
179 const int mRight = 20;
181 const int mBottom = 40;
182 const int plotW = w - mLeft - mRight;
183 const int plotH = h - mTop - mBottom;
185 if (plotW <= 0 || plotH <= 0)
189 if (!m_sTitle.isEmpty()) {
190 QFont titleFont = painter.font();
191 titleFont.setBold(
true);
192 titleFont.setPointSize(titleFont.pointSize() + 2);
193 painter.setFont(titleFont);
194 painter.setPen(Qt::black);
195 painter.drawText(QRect(0, 0, w, mTop), Qt::AlignCenter, m_sTitle);
196 painter.setFont(QFont());
200 painter.setPen(QPen(Qt::black, 1));
201 painter.drawLine(mLeft, mTop, mLeft, mTop + plotH);
202 painter.drawLine(mLeft, mTop + plotH, mLeft + plotW, mTop + plotH);
205 if (!m_sYLabel.isEmpty()) {
207 painter.translate(12, mTop + plotH / 2);
209 painter.drawText(QRect(-plotH / 2, 0, plotH, 15), Qt::AlignCenter, m_sYLabel);
214 if (!m_sXLabel.isEmpty()) {
215 painter.drawText(QRect(mLeft, h - 15, plotW, 15), Qt::AlignCenter, m_sXLabel);
218 double rangeX = m_dMaxX - m_dMinX;
219 double rangeY = m_dMaxY - m_dMinY;
226 const int nYTicks = 5;
227 for (
int i = 0; i <= nYTicks; ++i) {
228 double yVal = m_dMinY + rangeY * i / nYTicks;
229 int y = mTop + plotH -
static_cast<int>(plotH *
static_cast<double>(i) / nYTicks);
230 painter.drawLine(mLeft - 5, y, mLeft, y);
231 painter.drawText(QRect(0, y - 10, mLeft - 8, 20), Qt::AlignRight | Qt::AlignVCenter,
232 QString::number(yVal,
'g', 4));
236 const int nXTicks = 5;
237 for (
int i = 0; i <= nXTicks; ++i) {
238 double xVal = m_dMinX + rangeX * i / nXTicks;
239 int x = mLeft +
static_cast<int>(plotW *
static_cast<double>(i) / nXTicks);
240 painter.drawLine(x, mTop + plotH, x, mTop + plotH + 5);
241 painter.drawText(QRect(x - 30, mTop + plotH + 6, 60, 20), Qt::AlignCenter,
242 QString::number(xVal,
'g', 4));
245 if (m_vecXData.isEmpty() || m_vecYData.isEmpty())
250 auto toPixelX = [&](
double dx) ->
double {
251 return mLeft + ((dx - m_dMinX) / rangeX) * plotW;
253 auto toPixelY = [&](
double dy) ->
double {
254 return mTop + plotH - ((dy - m_dMinY) / rangeY) * plotH;
257 int n = qMin(m_vecXData.size(), m_vecYData.size());
258 path.moveTo(toPixelX(m_vecXData[0]), toPixelY(m_vecYData[0]));
259 for (
int i = 1; i < n; ++i) {
260 path.lineTo(toPixelX(m_vecXData[i]), toPixelY(m_vecYData[i]));
263 painter.setPen(QPen(QColor(70, 130, 180), 2));
264 painter.setBrush(Qt::NoBrush);
265 painter.drawPath(path);