color individual points separately in QScatterSeries of Qt Charts

231 Views Asked by At

am trying to color each points of QScatterSeries in Qt Chart separately when we click on each points.

//adding scatter series
for(int i = 0; i < CLUSTERSERIESCOUNT; ++i)//3 times
   {
        m_clusterseries[i] = new QScatterSeries();
        if(m_clusterseries[i])
        {
            m_clusterseries[i]->clear();
            connect(m_clusterseries[i], &QScatterSeries::clicked, this, &RadarGraphicsView::onClickedClusterScatterObject);
            connect(m_clusterseries[i], &QScatterSeries::hovered, this, &RadarGraphicsView::onHoverClusterObject);
        }
    }

for(int seriesIndex = 0; seriesIndex < CLUSTERSERIESCOUNT; ++seriesIndex)
    {
        if(seriesIndex == 0)
        {
            m_clusterseries[0]->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
            m_clusterseries[0]->setPen(Qt::SolidLine);
            m_clusterseries[0]->setBorderColor(Qt::blue);
            m_clusterseries[0]->setColor(Qt::transparent);
            m_clusterseries[0]->setName(MOVING);
            m_clusterseries[0]->setMarkerSize(7.5);

            for(int i = 0; i < m_xValuesMCluster.count(); ++i)
            {
                m_clusterseries[0]->append(QPointF(m_xValuesMCluster.at(i), m_yValuesMCluster.at(i)));
            }
        }
        else if(seriesIndex == 1)
        {
            m_clusterseries[1]->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
            m_clusterseries[1]->setPen(Qt::SolidLine);
            m_clusterseries[1]->setBorderColor(Qt::yellow);
            m_clusterseries[1]->setColor(Qt::transparent);
            m_clusterseries[1]->setName(STATIONARY);
            m_clusterseries[1]->setMarkerSize(7.5);

            for(int i = 0; i < m_xValuesSCluster.count(); ++i)
            {
                m_clusterseries[1]->append(QPointF(m_xValuesSCluster.at(i), m_yValuesSCluster.at(i)));
            }
        }
        else  if(seriesIndex == 2)
        {
            m_clusterseries[2]->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
            m_clusterseries[2]->setPen(Qt::SolidLine);
            m_clusterseries[2]->setBorderColor(Qt::gray);
            m_clusterseries[2]->setColor(Qt::transparent);
            m_clusterseries[2]->setName(UNKNOWN);
            m_clusterseries[2]->setMarkerSize(7.5);

            for(int i = 0; i < m_xValuesUNCluster.count(); ++i)
            {
                m_clusterseries[2]->append(QPointF(m_xValuesUNCluster.at(i), m_yValuesUNCluster.at(i)));
            }
        }
    }

 //add series to the chart
    for(int i = 0; i < CLUSTERSERIESCOUNT; ++i)
    {
        m_chart->addSeries(m_clusterseries[i]);
        m_clusterseries[i]->attachAxis(m_axisX);
        m_clusterseries[i]->attachAxis(m_axisY);
    }

This is how am adding points and it looks as below. enter image description here

on Clicking each point, I want that to color with a selected color.

void MyView::onClickedClusterScatterObject(const QPointF& point)
{
  //color the selected cluster
  QPointF p =  m_chart->mapToPosition(point);
  QGraphicsItem *it = itemAt(mapFromScene(p));
  it->setTransformOriginPoint(it->boundingRect().center());
//the below colors the whole series
//QGraphicsColorizeEffect* effect = new QGraphicsColorizeEffect(this);
//effect->setColor(QColor(m_CRLTColor));
//it->setGraphicsEffect(effect);

 QScatterSeries* series = qobject_cast<QScatterSeries*>(sender());
 qInfo()<<__FUNCTION__<<sender()<<series->markerShape();
 if(QGraphicsRectItem *rect = qgraphicsitem_cast<QGraphicsRectItem*>(it))
{
    rect->setBrush(QColor(m_CRLTColor));
}
 m_mainScene->update();
}

With above code, am able to color the selected points, only the Yellow series[ie, m_clusterseries1].

enter image description here

But on, clicking the Blue series points[ie, m_clusterseries[0]], it fails in the qgraphicsitem_cast(it) line. and rect->setBrush(color) doesn't execute. Why so? what wrong am doing?

Another issue, after new data is received, I clear the old series and perform the same initialization steps again. After the the nothing works, ie, the graphicsitem_cast fails for the yellow series also.

QGraphicsColorizeEffect code, colors the whole series, and some unexpected things, like on clicking the blue points, the yellow series is getting colored.

enter image description here

Any suggestion to color individual points of a qscatterseries is appreciated.

0

There are 0 best solutions below