问题
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