LimeReport/limereport/items/charts/lrhorizontalbarchart.cpp

81 lines
3.0 KiB
C++
Raw Normal View History

2020-01-11 02:11:55 +03:00
#include "lrhorizontalbarchart.h"
namespace LimeReport{
void HorizontalBarChart::paintChart(QPainter *painter, QRectF chartRect)
{
2022-01-05 14:13:05 +03:00
updateMinAndMaxValues();
const qreal valuesVMargin = this->valuesVMargin(painter);
2020-01-11 02:11:55 +03:00
QRectF calcRect = verticalLabelsRect(painter, chartRect.adjusted(
hPadding(chartRect),
vPadding(chartRect) * 2,
-(chartRect.width() * 0.9),
2022-01-05 14:13:05 +03:00
-(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),
2022-01-27 21:20:44 +03:00
-(hPadding(chartRect)),
-vPadding(chartRect)));
2020-01-11 02:11:55 +03:00
paintHorizontalBars(painter, chartRect.adjusted(
hPadding(chartRect) + barsShift,
vPadding(chartRect) * 2,
-(hPadding(chartRect)),
-(vPadding(chartRect) * 2) ));
paintVerticalLabels(painter, calcRect);
}
void HorizontalBarChart::paintHorizontalBars(QPainter *painter, QRectF barsRect)
{
2020-05-18 22:12:05 +03:00
if (seriesCount() == 0) return;
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();
2021-08-23 08:07:08 +03:00
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()) {
2022-01-27 21:20:44 +03:00
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;
painter->setBrush(color_map[curColor]);
painter->drawRect(QRectF(barsRect.left(), curVOffset, designValues()[i]*hStep, vStep));
curVOffset+=vStep;
curColor++;
}
}
painter->restore();
}
}