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

非 Y 不嫁゛ 提交于 2021-02-07 20:26:47

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/50811328/is-it-possible-to-have-a-individual-marker-size-for-each-point-in-a-qscatterseri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!