0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 08:34:38 +03:00
LimeReport/limereport/items/charts/lrhorizontalbarchart.cpp

81 lines
2.8 KiB
C++
Raw Permalink Normal View History

2020-01-11 02:11:55 +03:00
#include "lrhorizontalbarchart.h"
namespace LimeReport {
2020-01-11 02:11:55 +03:00
void HorizontalBarChart::paintChart(QPainter* painter, QRectF chartRect)
2020-01-11 02:11:55 +03:00
{
2022-01-05 14:13:05 +03:00
updateMinAndMaxValues();
const qreal valuesVMargin = this->valuesVMargin(painter);
QRectF calcRect = verticalLabelsRect(
painter,
chartRect.adjusted(hPadding(chartRect), vPadding(chartRect) * 2, -(chartRect.width() * 0.9),
-(vPadding(chartRect) * 2 + valuesVMargin)));
2020-01-11 02:11:55 +03:00
qreal barsShift = calcRect.width();
paintHorizontalGrid(painter,
chartRect.adjusted(hPadding(chartRect) + barsShift, vPadding(chartRect),
-(hPadding(chartRect)), -vPadding(chartRect)));
2022-01-27 21:20:44 +03:00
paintHorizontalBars(painter,
chartRect.adjusted(hPadding(chartRect) + barsShift, vPadding(chartRect) * 2,
-(hPadding(chartRect)), -(vPadding(chartRect) * 2)));
2020-01-11 02:11:55 +03:00
paintVerticalLabels(painter, calcRect);
}
void HorizontalBarChart::paintHorizontalBars(QPainter* painter, QRectF barsRect)
2020-01-11 02:11:55 +03:00
{
if (seriesCount() == 0)
return;
2020-05-18 22:12:05 +03:00
2020-01-11 02:11:55 +03:00
painter->save();
painter->setRenderHint(QPainter::Antialiasing, false);
2022-01-27 21:20:44 +03:00
const AxisData& yAxisData = this->yAxisData();
const qreal delta = yAxisData.delta();
2020-01-11 02:11:55 +03:00
2022-01-27 21:20:44 +03:00
const qreal verticalOffset = painter->fontMetrics().height();
qreal vStep = (barsRect.height() - verticalOffset) / valuesCount() / seriesCount();
qreal hStep = (barsRect.width()
- painter->fontMetrics().boundingRect(QString::number(maxValue())).width())
/ delta;
2020-01-11 02:11:55 +03:00
if (!m_chartItem->series().isEmpty() && (m_chartItem->itemMode() != DesignMode)) {
2022-01-27 21:20:44 +03:00
qreal curVOffset = barsRect.top();
if (m_chartItem->horizontalAxisOnTop()) {
curVOffset += verticalOffset;
}
2020-01-11 02:11:55 +03:00
foreach (SeriesItem* series, m_chartItem->series()) {
painter->setBrush(series->color());
2022-01-27 21:20:44 +03:00
qreal y = curVOffset;
2020-01-11 02:11:55 +03:00
foreach (qreal value, series->data()->values()) {
painter->drawRect(
QRectF((-minValue() * hStep) + barsRect.left(), y, value * hStep, vStep));
y += vStep * seriesCount();
2020-01-11 02:11:55 +03:00
}
2022-01-27 21:20:44 +03:00
curVOffset += vStep;
2020-01-11 02:11:55 +03:00
}
} else {
qreal curVOffset = barsRect.top();
2022-01-27 21:20:44 +03:00
if (m_chartItem->horizontalAxisOnTop()) {
curVOffset += verticalOffset;
}
2020-01-11 02:11:55 +03:00
int curColor = 0;
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]);
painter->drawRect(
QRectF(barsRect.left(), curVOffset, designValues()[i] * hStep, vStep));
curVOffset += vStep;
2020-01-11 02:11:55 +03:00
curColor++;
}
}
painter->restore();
}
} // namespace LimeReport