color individual points separately in QScatterSeries of Qt Charts
-
Hi, 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.
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_clusterseries[1]].
But on, clicking the Blue series points[ie, m_clusterseries[0]], it fails in the qgraphicsitem_cast<QGraphicsRectItem>(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.
I suspect, there maybe in any issue in z-value with the blue series, so the GraphicsItem* It is picking something else than the cluster points.
Any suggestion to color individual points of a qscatterseries is appreciated.QPointF p = m_chart->mapToPosition(point); QGraphicsItem *it = itemAt(mapFromScene(p)); qInfo()<<"Flags"<<it->flags()<<"\tparent item "<<it->parentItem() <<"\t childItems"<<it->childItems()<<"\t z value"<<it->zValue();
After some debugging I found, that the GraphicsItem retrived from the scene are different as below, so the graphicsitem_cast fails for QRectGraphicsItem.
Yellow Series ================ AnnotationViewerPlugins::RadarGraphicsView::onClickedClusterScatterObject QtCharts::QScatterSeries(0x1f454c3e1e0) 1 "Stationary" Flags ( ItemIsSelectable ) parent item QGraphicsItem(0x1f454c50f00, parent=0x1f454c50ea0, pos=0,0) childItems () z value 0 "#ff69b4" color set to rect item 0 Blue Series ============== AnnotationViewerPlugins::RadarGraphicsView::onClickedClusterScatterObject QtCharts::QScatterSeries(0x1f454c42230) 1 "Moving" Flags ( ItemClipsChildrenToShape ) parent item QtCharts::QChart(0x1f4434d1e10, pos=0,0, flags=(ItemUsesExtendedStyleOption|ItemSendsGeometryChanges)) childItems (QGraphicsItem(0x1f454c50f00, parent=0x1f454c50ea0, pos=0,0)) z value 4