Add type for axis data for corner cases

This commit is contained in:
Emil Sawicki 2022-03-13 22:49:39 +01:00
parent e7a301c8b6
commit c45aef0efd
4 changed files with 22 additions and 9 deletions

View File

@ -70,7 +70,7 @@ void LinesChart::drawDesignMode(QPainter* painter, qreal hStep, qreal vStep, qre
qreal LinesChart::calculatePos(const AxisData &data, qreal value, qreal rectSize) const qreal LinesChart::calculatePos(const AxisData &data, qreal value, qreal rectSize) const
{ {
if (data.reverseDirection() && data.rangeMin() >= 0) { if (data.type() == AxisData::XAxis || (data.reverseDirection() && data.rangeMin() >= 0)) {
// Not flipping for minimum less than 0 because lower number is at the bottom. // Not flipping for minimum less than 0 because lower number is at the bottom.
return (1 - (data.rangeMax() - value) / data.delta()) * rectSize; return (1 - (data.rangeMax() - value) / data.delta()) * rectSize;
} else { } else {

View File

@ -148,9 +148,9 @@ ChartItem::ChartItem(QObject *owner, QGraphicsItem *parent)
m_horizontalAxisOnTop(false), m_gridChartLines(AllLines), m_horizontalAxisOnTop(false), m_gridChartLines(AllLines),
m_legendStyle(LegendPoints) m_legendStyle(LegendPoints)
{ {
m_xAxisData = new AxisData(this); m_xAxisData = new AxisData(AxisData::XAxis, this);
m_xAxisData->setReverseDirection(true); m_xAxisData->setReverseDirection(true);
m_yAxisData = new AxisData(this); m_yAxisData = new AxisData(AxisData::YAxis, this);
m_labels<<"First"<<"Second"<<"Thrid"; m_labels<<"First"<<"Second"<<"Thrid";
m_chart = new PieChart(this); m_chart = new PieChart(this);
m_chart->setTitleFont(font()); m_chart->setTitleFont(font());
@ -1033,7 +1033,7 @@ void AbstractSeriesChart::paintGrid(QPainter *painter, QRectF gridRect)
painter->save(); painter->save();
const AxisData &yAxisData = this->yAxisData(); const AxisData &yAxisData = this->yAxisData();
const AxisData &xAxisData = this->xAxisData(); AxisData &xAxisData = this->xAxisData();
painter->setRenderHint(QPainter::Antialiasing,false); painter->setRenderHint(QPainter::Antialiasing,false);
@ -1091,7 +1091,6 @@ void AbstractSeriesChart::paintGrid(QPainter *painter, QRectF gridRect)
text); text);
} }
} }
painter->restore(); painter->restore();
} }
@ -1178,7 +1177,7 @@ QString AbstractSeriesChart::axisLabel(int i, const AxisData &axisData)
const qreal min = axisData.rangeMin(); const qreal min = axisData.rangeMin();
const qreal step = axisData.step(); const qreal step = axisData.step();
qreal value = 0; qreal value = 0;
if (axisData.reverseDirection() && min >= 0) { if (axisData.type() == AxisData::YAxis && axisData.reverseDirection() && min >= 0) {
value = min + (axisData.segmentCount() - i) * step; value = min + (axisData.segmentCount() - i) * step;
} else { } else {
value = min + i * step; value = min + i * step;

View File

@ -4,13 +4,14 @@
#include <QDebug> #include <QDebug>
namespace LimeReport { namespace LimeReport {
AxisData::AxisData(QObject *parent) AxisData::AxisData(AxisType type, QObject *parent)
: QObject(parent), m_rangeMin(0), m_rangeMax(0), : QObject(parent), m_rangeMin(0), m_rangeMax(0),
m_minValue(0), m_maxValue(0), m_step(0), m_minValue(0), m_maxValue(0), m_step(0),
m_delta(0), m_segmentCount(4), m_calculateAxisScale(false), m_delta(0), m_segmentCount(4), m_calculateAxisScale(false),
m_reverseDirection(false), m_manualMaximum(0), m_reverseDirection(false), m_manualMaximum(0),
m_manualMinimum(0), m_manualStep(0), m_isMaximumAutomatic(true), m_manualMinimum(0), m_manualStep(0), m_isMaximumAutomatic(true),
m_isMinimumAutomatic(true), m_isStepAutomatic(true) m_isMinimumAutomatic(true), m_isStepAutomatic(true),
m_type(type)
{ {
} }
@ -333,4 +334,9 @@ void AxisData::setIsStepAutomatic(bool newIsStepAutomatic)
m_isStepAutomatic = newIsStepAutomatic; m_isStepAutomatic = newIsStepAutomatic;
} }
AxisData::AxisType AxisData::type() const
{
return m_type;
}
} }

View File

@ -16,7 +16,12 @@ class AxisData : public QObject
Q_PROPERTY(qreal manualMinimum READ manualMinimum WRITE setManualMinimum) Q_PROPERTY(qreal manualMinimum READ manualMinimum WRITE setManualMinimum)
Q_PROPERTY(qreal manualMaximum READ manualMaximum WRITE setManualMaximum) Q_PROPERTY(qreal manualMaximum READ manualMaximum WRITE setManualMaximum)
public: public:
AxisData(QObject *parent = nullptr); enum AxisType {
YAxis = 0,
XAxis = 1
};
AxisData(AxisType type, QObject *parent = nullptr);
QString toString() const; QString toString() const;
void copy(AxisData *other); void copy(AxisData *other);
@ -58,6 +63,8 @@ public:
bool isStepAutomatic() const; bool isStepAutomatic() const;
void setIsStepAutomatic(bool newIsStepAutomatic); void setIsStepAutomatic(bool newIsStepAutomatic);
AxisType type() const;
private: private:
void calculateRoundedAxisScale(); void calculateRoundedAxisScale();
void calculateSimpleAxisScale(); void calculateSimpleAxisScale();
@ -79,6 +86,7 @@ private:
bool m_isMaximumAutomatic; bool m_isMaximumAutomatic;
bool m_isMinimumAutomatic; bool m_isMinimumAutomatic;
bool m_isStepAutomatic; bool m_isStepAutomatic;
const AxisType m_type;
}; };
}; };