2020-01-11 02:11:55 +03:00
|
|
|
#include "lrverticalbarchart.h"
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
namespace LimeReport {
|
2020-01-11 02:11:55 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VerticalBarChart::paintChart(QPainter* painter, QRectF chartRect)
|
2020-01-11 02:11:55 +03:00
|
|
|
{
|
2022-01-05 14:13:05 +03:00
|
|
|
updateMinAndMaxValues();
|
|
|
|
|
|
|
|
const qreal valuesHMargin = this->valuesHMargin(painter);
|
|
|
|
const qreal valuesVMargin = this->valuesVMargin(painter);
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
QRectF calcRect = horizontalLabelsRect(
|
|
|
|
painter,
|
2024-09-04 17:31:16 +03:00
|
|
|
chartRect.adjusted(hPadding(chartRect) * 2 + valuesHMargin,
|
|
|
|
chartRect.height()
|
|
|
|
- (painter->fontMetrics().height() + vPadding(chartRect) * 2),
|
|
|
|
-(hPadding(chartRect) * 2), -vPadding(chartRect)));
|
2020-01-11 02:11:55 +03:00
|
|
|
qreal barsShift = calcRect.height();
|
2024-09-04 17:31:16 +03:00
|
|
|
paintVerticalGrid(painter,
|
|
|
|
chartRect.adjusted(hPadding(chartRect), vPadding(chartRect) + valuesVMargin,
|
|
|
|
-hPadding(chartRect), -(vPadding(chartRect) + barsShift)));
|
|
|
|
paintVerticalBars(painter,
|
|
|
|
chartRect.adjusted(hPadding(chartRect) * 2 + valuesHMargin,
|
|
|
|
vPadding(chartRect) + valuesVMargin,
|
|
|
|
-hPadding(chartRect) * 2,
|
|
|
|
-(vPadding(chartRect) + barsShift)));
|
|
|
|
paintSerialLines(painter,
|
|
|
|
chartRect.adjusted(hPadding(chartRect) * 2 + valuesHMargin,
|
|
|
|
vPadding(chartRect) + valuesVMargin,
|
|
|
|
-hPadding(chartRect) * 2,
|
|
|
|
-(vPadding(chartRect) + barsShift)));
|
2020-01-11 02:11:55 +03:00
|
|
|
paintHorizontalLabels(painter, calcRect);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VerticalBarChart::paintVerticalBars(QPainter* painter, QRectF barsRect)
|
2020-01-11 02:11:55 +03:00
|
|
|
{
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (valuesCount() == 0)
|
|
|
|
return;
|
2020-05-18 22:12:05 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
const AxisData& yAxisData = this->yAxisData();
|
2022-01-18 12:56:05 +03:00
|
|
|
const qreal delta = yAxisData.delta();
|
2020-01-11 02:11:55 +03:00
|
|
|
|
|
|
|
int barSeriesCount = 0;
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (SeriesItem* series, m_chartItem->series()) {
|
|
|
|
if (series->preferredType() == SeriesItem::Bar)
|
|
|
|
barSeriesCount++;
|
2020-01-11 02:11:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
barSeriesCount = (m_chartItem->itemMode() == DesignMode) ? seriesCount() : barSeriesCount;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (barSeriesCount < 1)
|
|
|
|
return;
|
2020-01-11 02:11:55 +03:00
|
|
|
painter->save();
|
2024-09-04 17:31:16 +03:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing, false);
|
2020-01-11 02:11:55 +03:00
|
|
|
|
|
|
|
qreal vStep = barsRect.height() / delta;
|
|
|
|
qreal hStep = (barsRect.width() / valuesCount()) / (barSeriesCount == 0 ? 1 : barSeriesCount);
|
|
|
|
qreal topShift = (delta - (maxValue() - minValue())) * vStep + barsRect.top();
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_chartItem->series().isEmpty() && (m_chartItem->itemMode() != DesignMode)) {
|
2020-01-11 02:11:55 +03:00
|
|
|
int curSeries = 0;
|
|
|
|
foreach (SeriesItem* series, m_chartItem->series()) {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (series->preferredType() == SeriesItem::Bar) {
|
2020-01-11 02:11:55 +03:00
|
|
|
qreal curHOffset = curSeries * hStep + barsRect.left();
|
|
|
|
painter->setBrush(series->color());
|
|
|
|
foreach (qreal value, series->data()->values()) {
|
2024-09-04 17:31:16 +03:00
|
|
|
painter->drawRect(
|
|
|
|
QRectF(curHOffset, maxValue() * vStep + topShift, hStep, -value * vStep));
|
2020-01-11 02:11:55 +03:00
|
|
|
curHOffset += hStep * barSeriesCount;
|
|
|
|
}
|
|
|
|
curSeries++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qreal curHOffset = barsRect.left();
|
|
|
|
int curColor = 0;
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < 9; ++i) {
|
|
|
|
if (curColor == 3)
|
|
|
|
curColor = 0;
|
2020-01-11 02:11:55 +03:00
|
|
|
painter->setBrush(color_map[curColor]);
|
2024-09-04 17:31:16 +03:00
|
|
|
painter->drawRect(QRectF(curHOffset, maxValue() * vStep + barsRect.top(), hStep,
|
|
|
|
-designValues()[i] * vStep));
|
2020-01-11 02:11:55 +03:00
|
|
|
curHOffset += hStep;
|
|
|
|
curColor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalBarChart::paintSerialLines(QPainter* painter, QRectF barsRect)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (valuesCount() == 0 || m_chartItem->series().isEmpty())
|
|
|
|
return;
|
2020-05-18 22:12:05 +03:00
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
painter->save();
|
2024-09-04 17:31:16 +03:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
2020-01-11 02:11:55 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
for (SeriesItem* series : m_chartItem->series()) {
|
|
|
|
if (series->preferredType() == SeriesItem::Line) {
|
2022-01-18 12:56:05 +03:00
|
|
|
paintSeries(painter, series, barsRect);
|
2020-01-11 02:11:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace LimeReport
|