Search code examples
c++qtqgraphicsitemqtcharts

Is it possible to have an individual marker color for each point in a QScatterSeries?


This post shows that it is possible to have an individual marker size for each point in a QScatterSeries. What about an indiviual marker color for each point?


Solution

  • The circles are QGraphicsEllipseItem so you can cast them, and then use the setBrush method to set the color:

    for(int index= 0; index < series->count(); index++){
        QPointF p =  chart->mapToPosition(series->at(index) , series);
        QGraphicsItem *it = view.itemAt(view.mapFromScene(p));
        it->setTransformOriginPoint(it->boundingRect().center());
        it->setScale(data[index].s);
        if(QGraphicsEllipseItem *ellipse = qgraphicsitem_cast(it)){
            QColor color = QColor::fromRgb(QRandomGenerator::global()->generate());;
            ellipse->setBrush(color);
        }
    }

    enter image description here