如何在 QBarSet 中设置条形图的颜色?

How to Set the Color of a Bar in a QBarSet?

我有一个带有单个 QBarSet 的水平条形图,如下所示:

我想单独设置 QBarSet 的每个条形的颜色。有什么办法可以做到吗?

我知道我可以使用 3 个单独的 QBarSet 而不是一个,并分别设置每个 QBarSet 的颜色来实现我想要的。但是,我不想使用这种策略,因为条形图的标签会出现在图表顶部的图例中(我希望每个条形图的标签都出现在条形图的左侧,就像在快照中那样我提供了)。

我看到了一个解决方案 ,它解释了如何在悬停事件上执行此操作,但我可以找到一种方法来修改该解决方案,以便条形始终是彩色的。

谢谢!

以下方法在另一种配置中可能会失败,因为它高度依赖于图表上放置的内容,因此如果您想找到真正问题的答案,您必须提供 MRE。

综合以上,逻辑是通过filter获取item(矩形)并设置颜色:

#include <random>

#include <QApplication>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QChartView w;

    QBarSet *set0 = new QBarSet("bar1");

    *set0 << 1 << 4 << 3 << 7 << 2 << 5 << 1 << 3 << 3 << 2 << 1 << 6 << 7 << 5;

    QBarSeries *series = new QBarSeries;
    series->append(set0);

    QChart *chart= new QChart;
    w.setChart(chart);
    chart->addSeries(series);

    // filter items
    QList<QGraphicsRectItem *> rect_items;
    for(QGraphicsItem * it : w.items()){
        if(QGraphicsRectItem *rect = qgraphicsitem_cast<QGraphicsRectItem *>(it)){
            if(rect->parentItem() != chart && rect->parentItem()->parentItem() == chart){
                rect_items << rect;
            }
        }
    }

    // change color
    std::random_device dev;
    std::mt19937 rng(dev());
    std::uniform_int_distribution<std::mt19937::result_type> dist255(0, 255);

    for(QGraphicsRectItem * rect : rect_items){
        QColor color(dist255(rng), dist255(rng), dist255(rng));
        rect->setBrush(color);
    }

    w.show();

    return a.exec();
}

I know I could use 3 separate QBarSet instead of one and set the color of each QBarSet individually to achieve what I want. However, I don't want to use this strategy because the label of the bars would appear in a legend at the top of the chart (I want the label of each bar to appear at the left of the bar, like in the snapshot that I provided).

出于同样的目的,我自己刚刚应对了这个挑战,我最终使用了带有单独 QBarSet 实例的 QStackedBarSeries,根据图表主题,这些实例使用虚拟零值使每个绘制的条形图具有不同的颜色。显然,同样的技巧可以用于每个条形集的手动着色。

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QStringList names;
    names << "Parti Conservateur" << "Parti Liberal" << "Parti Vert";
    QList<qreal> counts;
    counts << 41 << 36.3 << 22.7;

    auto series = new QHorizontalStackedBarSeries();
    QList<qreal> dummyCount;
    for(int i = 0; i < names.count(); ++i) {
      auto barSet = new QBarSet(names.at(i));
      // to "trick" the bar chart into different colors, each new group
      // must be in a different value position so replace the previous
      // value with 0 and insert the next one
      if(i > 0) dummyCount.replace(i-1, 0);
      dummyCount << counts.at(i);
      barSet->append(dummyCount);
      series->append(barSet);
    }
    series->setLabelsVisible(true);
    series->setLabelsFormat(QStringLiteral("@value%"));
    series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);

    auto chart = new QChart();
    chart->addSeries(series);
    chart->legend()->hide();

    auto axisY = new QBarCategoryAxis();
    axisY->append(names);
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisY);
    auto axisX = new QValueAxis();
    chart->addAxis(axisX, Qt::AlignBottom);
    series->attachAxis(axisX);
    axisX->applyNiceNumbers();

    QChartView chartView;
    chartView.setChart(chart);
    chartView.show();

    return a.exec();
}

stacked bar chart with varying colors