Add property to draw lines in grid

This commit is contained in:
Emil Sawicki 2022-01-27 19:51:40 +01:00
parent d0830a7a37
commit 0369137800
2 changed files with 48 additions and 7 deletions

View File

@ -144,7 +144,7 @@ ChartItem::ChartItem(QObject *owner, QGraphicsItem *parent)
m_legendAlign(LegendAlignCenter), m_titleAlign(TitleAlignCenter), m_legendAlign(LegendAlignCenter), m_titleAlign(TitleAlignCenter),
m_chartType(Pie), m_labelsField(""), m_isEmpty(true), m_chartType(Pie), m_labelsField(""), m_isEmpty(true),
m_showLegend(true), m_drawPoints(true), m_seriesLineWidth(4), m_showLegend(true), m_drawPoints(true), m_seriesLineWidth(4),
m_horizontalAxisOnTop(false) m_horizontalAxisOnTop(false), m_gridChartLines(AllLines)
{ {
m_labels<<"First"<<"Second"<<"Thrid"; m_labels<<"First"<<"Second"<<"Thrid";
m_chart = new PieChart(this); m_chart = new PieChart(this);
@ -508,6 +508,25 @@ void ChartItem::setHorizontalAxisOnTop(bool horizontalAxisOnTop)
m_horizontalAxisOnTop = horizontalAxisOnTop; m_horizontalAxisOnTop = horizontalAxisOnTop;
} }
ChartItem::GridChartLines ChartItem::gridChartLines() const
{
return m_gridChartLines;
}
void ChartItem::setGridChartLines(GridChartLines flags)
{
if (m_gridChartLines == flags) {
return;
}
GridChartLines oldValue = m_gridChartLines;
m_gridChartLines = flags;
if (isLoading()) {
return;
}
update(rect());
notify("gridChartLines",QVariant(oldValue),QVariant(flags));
}
AbstractChart::AbstractChart(ChartItem *chartItem) AbstractChart::AbstractChart(ChartItem *chartItem)
:m_chartItem(chartItem) :m_chartItem(chartItem)
{ {
@ -832,18 +851,25 @@ void AbstractSeriesChart::paintGrid(QPainter *painter, QRectF gridRect)
const QTextOption verticalTextOption(Qt::AlignRight); const QTextOption verticalTextOption(Qt::AlignRight);
for (int i = 0 ; i < yAxisLineCount ; i++ ) { for (int i = 0 ; i < yAxisLineCount ; i++ ) {
const qreal y = vStep * i; const qreal y = vStep * i;
const bool drawFullLine = m_chartItem->gridChartLines() & ChartItem::HorizontalLine
|| i == 0 || i == xAxisSegmentCount;
painter->drawText(QRectF(gridRect.bottomLeft()-QPointF(halfFontHeight, y + halfFontHeight), painter->drawText(QRectF(gridRect.bottomLeft()-QPointF(halfFontHeight, y + halfFontHeight),
QSizeF(valuesHMargin,fontHeight)), QSizeF(valuesHMargin,fontHeight)),
axisLabel(i, yAxisData), axisLabel(i, yAxisData),
verticalTextOption); verticalTextOption);
painter->drawLine(gridRect.bottomLeft()-QPointF(-valuesHMargin, y),
gridRect.bottomRight()-QPointF(0, y)); QPointF lineEndPos = gridRect.bottomRight() - QPointF(0, y);
if (!drawFullLine) {
lineEndPos.setX(gridRect.left() + valuesHMargin + gridOffset);
}
painter->drawLine(gridRect.bottomLeft() - QPointF(-valuesHMargin, y), lineEndPos);
} }
// Horizontal axis lines // Horizontal axis lines
for (int i = 0 ; i < xAxisLineCount ; i++) { for (int i = 0 ; i < xAxisLineCount ; i++) {
const qreal x = gridRect.left() + hStep * i + valuesHMargin + gridOffset; const qreal x = gridRect.left() + hStep * i + valuesHMargin + gridOffset;
const bool drawFullLine = i == 0 || i == xAxisSegmentCount; const bool drawFullLine = m_chartItem->gridChartLines() & ChartItem::VerticalLine
|| i == 0 || i == xAxisSegmentCount;
const QString text = axisLabel(i, xAxisData); const QString text = axisLabel(i, xAxisData);
if (m_chartItem->horizontalAxisOnTop()) { if (m_chartItem->horizontalAxisOnTop()) {
@ -1035,4 +1061,5 @@ QRectF AbstractBarChart::horizontalLabelsRect(QPainter *painter, QRectF labelsRe
else else
return labelsRect.adjusted(0, (labelsRect.height() - maxWidth), 0, 0); return labelsRect.adjusted(0, (labelsRect.height() - maxWidth), 0, 0);
} }
} // namespace LimeReport } // namespace LimeReport

View File

@ -144,10 +144,11 @@ class ChartItem : public LimeReport::ItemDesignIntf
Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints) Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints)
Q_PROPERTY(int seriesLineWidth READ seriesLineWidth WRITE setSeriesLineWidth) Q_PROPERTY(int seriesLineWidth READ seriesLineWidth WRITE setSeriesLineWidth)
Q_PROPERTY(bool horizontalAxisOnTop READ horizontalAxisOnTop WRITE setHorizontalAxisOnTop) Q_PROPERTY(bool horizontalAxisOnTop READ horizontalAxisOnTop WRITE setHorizontalAxisOnTop)
Q_PROPERTY(QString xAxisField READ xAxisField WRITE setXAxisField)
//girdChart //gridChart
// TODO_ES add grid property for showing lines inside Q_FLAGS(GridChartLines)
Q_PROPERTY(QString xAxisField READ xAxisField WRITE setXAxisField)
Q_PROPERTY(GridChartLines gridChartLines READ gridChartLines WRITE setGridChartLines)
friend class AbstractChart; friend class AbstractChart;
public: public:
@ -155,15 +156,24 @@ public:
enum LegendStyle{LegendPoints, LegendLines}; enum LegendStyle{LegendPoints, LegendLines};
enum TitleAlign{TitleAlignLeft, TitleAlignCenter, TitleAlignRight}; enum TitleAlign{TitleAlignLeft, TitleAlignCenter, TitleAlignRight};
enum ChartType{Pie, VerticalBar, HorizontalBar, Lines, GridLines}; enum ChartType{Pie, VerticalBar, HorizontalBar, Lines, GridLines};
enum LineType {
NoLine = 0,
HorizontalLine = 1,
VerticalLine = 2,
AllLines = 3
};
#if QT_VERSION >= 0x050500 #if QT_VERSION >= 0x050500
Q_ENUM(LegendAlign) Q_ENUM(LegendAlign)
Q_ENUM(TitleAlign) Q_ENUM(TitleAlign)
Q_ENUM(ChartType) Q_ENUM(ChartType)
Q_ENUM(LineType)
#else #else
Q_ENUMS(LegendAlign) Q_ENUMS(LegendAlign)
Q_ENUMS(TitleAlign) Q_ENUMS(TitleAlign)
Q_ENUMS(ChartType) Q_ENUMS(ChartType)
Q_ENUMS(LineType)
#endif #endif
Q_DECLARE_FLAGS(GridChartLines, LineType)
ChartItem(QObject* owner, QGraphicsItem* parent); ChartItem(QObject* owner, QGraphicsItem* parent);
~ChartItem(); ~ChartItem();
@ -213,6 +223,9 @@ public:
bool horizontalAxisOnTop() const; bool horizontalAxisOnTop() const;
void setHorizontalAxisOnTop(bool horizontalAxisOnTop); void setHorizontalAxisOnTop(bool horizontalAxisOnTop);
GridChartLines gridChartLines() const;
void setGridChartLines(GridChartLines flags);
protected: protected:
void paintChartTitle(QPainter* painter, QRectF titleRect); void paintChartTitle(QPainter* painter, QRectF titleRect);
virtual BaseDesignIntf* createSameTypeItem(QObject *owner, QGraphicsItem *parent); virtual BaseDesignIntf* createSameTypeItem(QObject *owner, QGraphicsItem *parent);
@ -243,6 +256,7 @@ private:
int m_seriesLineWidth; int m_seriesLineWidth;
QString m_xAxisField; QString m_xAxisField;
bool m_horizontalAxisOnTop; bool m_horizontalAxisOnTop;
GridChartLines m_gridChartLines;
}; };
} //namespace LimeReport } //namespace LimeReport
#endif // LRCHARTITEM_H #endif // LRCHARTITEM_H