attachments are my codes override qt-everywhere-src-5.15.12/qtcharts/src/charts/barchart/horizontal/stacked/ (1) myhorizontalstackedbarchartitem_p.h ``` #ifndef MYHORIZONTALSTACKEDBARCHARTITEM_P_H #define MYHORIZONTALSTACKEDBARCHARTITEM_P_H #include #include #include QT_CHARTS_BEGIN_NAMESPACE class Q_CHARTS_PRIVATE_EXPORT myHorizontalStackedBarChartItem : public AbstractBarChartItem { Q_OBJECT public: myHorizontalStackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); private: virtual QVector calculateLayout(); void initializeLayout(int set, int category, int layoutIndex, bool resetAnimation); QPointF topLeftPoint(int category, qreal barWidth, qreal value); QPointF bottomRightPoint(int category, qreal barWidth, qreal value); protected: QString generateLabelText (int set, int category, qreal value) override; }; QT_CHARTS_END_NAMESPACE #endif // MYHORIZONTALSTACKEDBARCHARTITEM_P_H ``` (2) myqhorizontalstackedbarseries.h ``` #ifndef MYQHORIZONTALSTACKEDBARSERIES_H #define MYQHORIZONTALSTACKEDBARSERIES_H #include QT_CHARTS_BEGIN_NAMESPACE class myQHorizontalStackedBarSeriesPrivate; class Q_CHARTS_EXPORT myQHorizontalStackedBarSeries : public QAbstractBarSeries { Q_OBJECT public: explicit myQHorizontalStackedBarSeries(QObject *parent = nullptr); ~myQHorizontalStackedBarSeries(); QAbstractSeries::SeriesType type() const; protected: friend class myHorizontalStackedBarChartItem; private: Q_DECLARE_PRIVATE(myQHorizontalStackedBarSeries) Q_DISABLE_COPY(myQHorizontalStackedBarSeries) }; QT_CHARTS_END_NAMESPACE #endif // MYQHORIZONTALSTACKEDBARSERIES_H ``` (3)myqhorizontalstackedbarseries_p.h ``` #ifndef MYQHORIZONTALSTACKEDBARSERIES_P_H #define MYQHORIZONTALSTACKEDBARSERIES_P_H #include #include #include QT_CHARTS_BEGIN_NAMESPACE class Q_CHARTS_PRIVATE_EXPORT myQHorizontalStackedBarSeriesPrivate : public QAbstractBarSeriesPrivate { public: myQHorizontalStackedBarSeriesPrivate(myQHorizontalStackedBarSeries *q); void initializeGraphics(QGraphicsItem* parent); void initializeDomain(); private: Q_DECLARE_PUBLIC(myQHorizontalStackedBarSeries) }; QT_CHARTS_END_NAMESPACE #endif // MYQHORIZONTALSTACKEDBARSERIES_P_H ``` (4) myhorizontalstackedbarchartitem.cpp ``` //#include #include "myhorizontalstackedbarchartitem_p.h" #include #include #include QT_CHARTS_BEGIN_NAMESPACE myHorizontalStackedBarChartItem::myHorizontalStackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :AbstractBarChartItem(series, item) { } QString myHorizontalStackedBarChartItem::generateLabelText (int set, int category, qreal value) { Q_UNUSED(set); Q_UNUSED(category); static const QString valueTag(QLatin1String("@value")); QString valueString = presenter()->numberToString(value, 'g', m_series->labelsPrecision()); QString valueLabel; if (m_series->labelsFormat().isEmpty()) { valueLabel = valueString; } else { valueLabel = m_series->labelsFormat(); valueLabel.replace(valueTag, valueString); //show set name static const QString setTag(QLatin1String("@set")); if (valueLabel.contains(setTag)) { valueLabel.replace(setTag, QString::number(set)); } } return valueLabel; }; void myHorizontalStackedBarChartItem::initializeLayout(int set, int category, int layoutIndex, bool resetAnimation) { Q_UNUSED(set) Q_UNUSED(resetAnimation) QRectF rect; if (set > 0) { const QBarSet *barSet = m_series->barSets().at(set); const qreal value = barSet->at(category); int checkIndex = set; bool found = false; // Negative values stack to negative side and positive values to positive side, so we need // to find the previous set that stacks to the same side while (checkIndex > 0 && !found) { checkIndex--; QBarSet *checkSet = m_series->barSets().at(checkIndex); const qreal checkValue = checkSet->at(category); if ((value < 0.0) == (checkValue < 0.0)) { Bar *checkBar = m_indexForBarMap.value(checkSet).value(category); rect = m_layout.at(checkBar->layoutIndex()); found = true; break; } } // If we didn't find a previous set to the same direction, just stack next to the first set if (!found) { QBarSet *firsSet = m_series->barSets().at(0); Bar *firstBar = m_indexForBarMap.value(firsSet).value(category); rect = m_layout.at(firstBar->layoutIndex()); } if (value < 0) rect.setRight(rect.left()); else rect.setLeft(rect.right()); } else { QPointF topLeft; QPointF bottomRight; const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) { topLeft = topLeftPoint(category, barWidth, domain()->minX()); bottomRight = bottomRightPoint(category, barWidth, domain()->minX()); } else { topLeft = topLeftPoint(category, barWidth, 0.0); bottomRight = bottomRightPoint(category, barWidth, 0.0); } if (m_validData) { rect.setTopLeft(topLeft); rect.setBottomRight(bottomRight); } } m_layout[layoutIndex] = rect.normalized(); } QPointF myHorizontalStackedBarChartItem::topLeftPoint(int category, qreal barWidth, qreal value) { return domain()->calculateGeometryPoint( QPointF(value, m_seriesPosAdjustment + category - (barWidth / 2)), m_validData); } QPointF myHorizontalStackedBarChartItem::bottomRightPoint(int category, qreal barWidth, qreal value) { return domain()->calculateGeometryPoint( QPointF(value, m_seriesPosAdjustment + category + (barWidth / 2)), m_validData); } QVector myHorizontalStackedBarChartItem::calculateLayout() { QVector layout; layout.resize(m_layout.size()); const int setCount = m_series->count(); const qreal barWidth = m_series->d_func()->barWidth() * m_seriesWidth; QVector positiveSums(m_categoryCount, 0.0); QVector negativeSums(m_categoryCount, 0.0); for (int set = 0; set < setCount; set++) { QBarSet *barSet = m_series->barSets().at(set); const QList bars = m_barMap.value(barSet); for (int i = 0; i < m_categoryCount; i++) { Bar *bar = bars.at(i); const int category = bar->index(); qreal &positiveSum = positiveSums[category - m_firstCategory]; qreal &negativeSum = negativeSums[category - m_firstCategory]; qreal value = barSet->at(category); QRectF rect; QPointF topLeft; QPointF bottomRight; if (value < 0) { bottomRight = bottomRightPoint(category, barWidth, value + negativeSum); if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) { topLeft = topLeftPoint(category, barWidth, set ? negativeSum : domain()->minX()); } else { topLeft = topLeftPoint(category, barWidth, set ? negativeSum : 0.0); } negativeSum += value; } else { bottomRight = bottomRightPoint(category, barWidth, value + positiveSum); if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) { topLeft = topLeftPoint(category, barWidth, set ? positiveSum : domain()->minX()); } else { topLeft = topLeftPoint(category, barWidth, set ? positiveSum : 0.0); } positiveSum += value; } rect.setTopLeft(topLeft); rect.setBottomRight(bottomRight); rect = rect.normalized(); layout[bar->layoutIndex()] = rect; // If animating, we need to reinitialize ~zero size bars with non-zero values // so the bar growth animation starts at correct spot. We shouldn't reset if rect // is already at correct position horizontally, so we check for that. if (m_animation && value != 0.0) { const QRectF &checkRect = m_layout.at(bar->layoutIndex()); if (checkRect.isEmpty() && ((value < 0.0 && !qFuzzyCompare(checkRect.right(), rect.right())) || (value > 0.0 && !qFuzzyCompare(checkRect.left(), rect.left())))) { initializeLayout(set, category, bar->layoutIndex(), true); } } } } return layout; } QT_CHARTS_END_NAMESPACE #include "moc_myhorizontalstackedbarchartitem_p.cpp" ``` (5) myqhorizontalstackedbarseries.cpp ``` //#include //#include //#include #include "myqhorizontalstackedbarseries.h" #include "myqhorizontalstackedbarseries_p.h" #include "myhorizontalstackedbarchartitem_p.h" #include #include QT_CHARTS_BEGIN_NAMESPACE myQHorizontalStackedBarSeries::myQHorizontalStackedBarSeries(QObject *parent) :QAbstractBarSeries(*new myQHorizontalStackedBarSeriesPrivate(this), parent) { } myQHorizontalStackedBarSeries::~myQHorizontalStackedBarSeries() { Q_D(myQHorizontalStackedBarSeries); if (d->m_chart) d->m_chart->removeSeries(this); } QAbstractSeries::SeriesType myQHorizontalStackedBarSeries::type() const { return QAbstractSeries::SeriesTypeHorizontalStackedBar; } myQHorizontalStackedBarSeriesPrivate::myQHorizontalStackedBarSeriesPrivate(myQHorizontalStackedBarSeries *q):QAbstractBarSeriesPrivate(q) { } void myQHorizontalStackedBarSeriesPrivate::initializeDomain() { qreal minX(domain()->minX()); qreal minY(domain()->minY()); qreal maxX(domain()->maxX()); qreal maxY(domain()->maxY()); qreal y = categoryCount(); minX = qMin(minX, bottom()); minY = qMin(minY, - (qreal)0.5); maxX = qMax(maxX, top()); maxY = qMax(maxY, y - (qreal)0.5); domain()->setRange(minX, maxX, minY, maxY); }; void myQHorizontalStackedBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(myQHorizontalStackedBarSeries); myHorizontalStackedBarChartItem *bar = new myHorizontalStackedBarChartItem(q, parent); m_item.reset(bar); QAbstractSeriesPrivate::initializeGraphics(parent); }; QT_CHARTS_END_NAMESPACE #include "moc_myqhorizontalstackedbarseries.cpp" ``` (6) main.cpp ``` #include "myhorizontalstackedbarchartitem_p.h" #include "myqhorizontalstackedbarseries.h" #include "myqhorizontalstackedbarseries_p.h" QtCharts::myQHorizontalStackedBarSeries *series = new QtCharts::myQHorizontalStackedBarSeries(); series->setLabelsFormat("set-index @set"); ```