2022-01-25 21:46:14 +03:00
|
|
|
#include "lrgridlineschart.h"
|
|
|
|
|
|
|
|
namespace LimeReport {
|
2024-09-04 17:31:16 +03:00
|
|
|
void GridLinesChart::paintChart(QPainter* painter, QRectF chartRect)
|
2022-01-25 21:46:14 +03:00
|
|
|
{
|
|
|
|
updateMinAndMaxValues();
|
|
|
|
|
|
|
|
const qreal hPadding = this->hPadding(chartRect);
|
|
|
|
const qreal vPadding = this->vPadding(chartRect);
|
|
|
|
|
2022-01-30 19:02:24 +03:00
|
|
|
const qreal valuesVMargin = this->valuesVMargin(painter);
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QRectF gridRect
|
|
|
|
= chartRect.adjusted(hPadding, vPadding + valuesVMargin * 2, -hPadding * 3, -vPadding * 3);
|
2022-01-30 19:02:24 +03:00
|
|
|
|
2022-01-30 19:46:04 +03:00
|
|
|
if (!m_chartItem->horizontalAxisOnTop()) {
|
|
|
|
// If horizontal axis is on the bottom, move grid a little up
|
2024-09-04 17:31:16 +03:00
|
|
|
gridRect.adjust(0, -valuesVMargin, 0, -valuesVMargin);
|
2022-01-30 19:46:04 +03:00
|
|
|
}
|
|
|
|
|
2022-01-30 19:02:24 +03:00
|
|
|
// Adapt font for horizontal axis
|
2024-09-04 17:31:16 +03:00
|
|
|
painter->setFont(adaptFont((gridRect.width() - this->valuesHMargin(painter))
|
|
|
|
/ xAxisData().segmentCount() * 0.8,
|
|
|
|
painter->font(), xAxisData()));
|
2022-01-30 19:02:24 +03:00
|
|
|
|
|
|
|
const qreal valuesHMargin = this->valuesHMargin(painter);
|
|
|
|
|
2022-01-30 19:46:04 +03:00
|
|
|
// Adjust vertical axis labels padding
|
|
|
|
gridRect.adjust(valuesHMargin * 0.2, 0, 0, 0);
|
2022-01-29 10:57:50 +03:00
|
|
|
|
2022-01-25 21:46:14 +03:00
|
|
|
paintGrid(painter, gridRect);
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
paintSerialLines(painter, gridRect.adjusted(hPadding + valuesHMargin, 0, 0, 0));
|
2022-01-25 21:46:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GridLinesChart::paintSerialLines(QPainter* painter, QRectF barsRect)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (valuesCount() == 0)
|
|
|
|
return;
|
2022-01-25 21:46:14 +03:00
|
|
|
|
|
|
|
painter->save();
|
2024-09-04 17:31:16 +03:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
2022-01-25 21:46:14 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
const AxisData& yAxisData = this->yAxisData();
|
2022-01-25 21:46:14 +03:00
|
|
|
const qreal delta = yAxisData.delta();
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_chartItem->itemMode() == DesignMode) {
|
2022-01-25 21:46:14 +03:00
|
|
|
const qreal hStep = barsRect.width() / valuesCount();
|
|
|
|
const qreal vStep = barsRect.height() / delta;
|
|
|
|
const qreal topShift = (delta - (maxValue() - minValue())) * vStep + barsRect.top();
|
|
|
|
drawDesignMode(painter, hStep, vStep, topShift, barsRect);
|
|
|
|
painter->restore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
const AxisData& xAxisData = this->xAxisData();
|
2022-01-25 21:46:14 +03:00
|
|
|
const qreal hStep = barsRect.width() / (xAxisData.rangeMax() - xAxisData.rangeMin());
|
|
|
|
|
|
|
|
qreal leftMargin = 0;
|
|
|
|
const qreal topMargin = barsRect.top();
|
|
|
|
|
|
|
|
for (SeriesItem* series : m_chartItem->series()) {
|
|
|
|
QPen pen(series->color());
|
|
|
|
pen.setWidth(m_chartItem->seriesLineWidth());
|
|
|
|
painter->setPen(pen);
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
const QList<qreal>& xAxisValues = series->data()->xAxisValues();
|
|
|
|
const QList<qreal>& values = series->data()->values();
|
2022-01-25 21:46:14 +03:00
|
|
|
const int xAxisValuesSize = xAxisValues.size();
|
|
|
|
qreal lastXPos = 0;
|
|
|
|
qreal lastYPos = 0;
|
|
|
|
if (!values.isEmpty()) {
|
|
|
|
// Calculate first point position on plot before loop
|
|
|
|
lastYPos = calculatePos(yAxisData, values.first(), barsRect.height());
|
|
|
|
}
|
|
|
|
if (xAxisValues.isEmpty()) {
|
|
|
|
leftMargin = barsRect.left();
|
|
|
|
} else {
|
|
|
|
leftMargin = barsRect.left();
|
|
|
|
lastXPos = calculatePos(xAxisData, xAxisValues.first(), barsRect.width());
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < values.count() - 1; ++i) {
|
2022-01-25 21:46:14 +03:00
|
|
|
const qreal startY = lastYPos;
|
2024-09-04 17:31:16 +03:00
|
|
|
const qreal endY = calculatePos(yAxisData, values.at(i + 1), barsRect.height());
|
2022-01-25 21:46:14 +03:00
|
|
|
// Record last used Y position to only calculate new one
|
|
|
|
lastYPos = endY;
|
|
|
|
|
|
|
|
qreal startX = lastXPos;
|
|
|
|
qreal endX = 0;
|
|
|
|
if (i + 1 < xAxisValuesSize) {
|
2024-09-04 17:31:16 +03:00
|
|
|
endX = calculatePos(xAxisData, xAxisValues.at(i + 1), barsRect.width());
|
2022-01-25 21:46:14 +03:00
|
|
|
} else {
|
|
|
|
endX = startX + hStep;
|
|
|
|
}
|
|
|
|
// Record last used X position to only calculate new one
|
|
|
|
lastXPos = endX;
|
|
|
|
|
|
|
|
QPoint startPoint = QPoint(startX + leftMargin, startY + topMargin);
|
|
|
|
QPoint endPoint = QPoint(endX + leftMargin, endY + topMargin);
|
|
|
|
drawSegment(painter, startPoint, endPoint, series->color());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
} // namespace LimeReport
|