Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

charts

I want to plot a series of points (distance r, angle a, strength s) in a QPolarChart

QPolarChart *chart = new QPolarChart();
ScatterSeries *series1 = new QScatterSeries();
series1->setMarkerSize(s);

for(int i = 0; i < count; i++)
{  
    series1->setMarkerSize(s); // ->  of course changes the marker size for the complete series
    series1->append(r, a);
}

chart->addSeries(series1);

Now I want to make the marker size individual for each point, basically the size should represent the "strength" of each point.

I could use an own QScatterSeries for each point, but I'm looking for a nicer implementation.

like image 637
schtz Avatar asked Feb 01 '26 23:02

schtz


1 Answers

There is no direct way to change the size of the marker, markers are custom items that we can not set the size but can scale. To obtain the items, use the itemAt() method of QChartView that inherits from QGraphicsView as shown below:

#include <QApplication>
#include <QtCharts>

QT_CHARTS_USE_NAMESPACE

struct Data{
    qreal r;
    qreal a;
    qreal s;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QChartView view;
    QPolarChart *chart = new QPolarChart;

    std::vector<Data> data;

    for(int i=0; i <= 360; i+=30){
        data.push_back(Data{i*1.0, i*1.0, 2*qAbs(sin(i*3.14159265358979323846/360))});
    }

    view.setChart(chart);
    QScatterSeries *series = new QScatterSeries;
    for(const Data & d: data){
        *series << QPointF(d.a, d.r);
    }

    chart->addSeries(series);
    chart->createDefaultAxes();
    view.show();

    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);
    }

    return a.exec();
}

enter image description here

like image 123
eyllanesc Avatar answered Feb 04 '26 14:02

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!