How to make only one point label visible for QLineSeries/QXYSeries

╄→尐↘猪︶ㄣ 提交于 2019-12-08 09:45:52

问题


I am using the qtcharts module of qt.
I am using c++ but it does not matter if the solution comes for another language (I will translate it afterwards).

Problem: I plot a bunch of QLineSeries in a QChart and I want to display the point labels only when hovering them.

I planned to use the signal QXYSeries::hovered() to detect when the mouse moves over a point (the same when the mouse moves away the point).

I know that there exists a member function QXYSeries::setPointLabelsVisible() but it makes visible all the points of the series.

I want to be able to display only one point at a time because the series are relatively large and displaying all the labels would degrade the readability.

Question: Is it possible to display only one point label for a QLineSeries ? If yes, how ?
I could not find such a feature anywhere in the Qt documentation.


Here is a baseline code sample to start with (for convenience):

Declaration:

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:
        MainWindow();
};

Definition:

#include <QApplication>
#include <QLineSeries>
#include <QDateTimeAxis>
#include <QValueAxis>
#include <QChartView>
#include <QDateTime>

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);

    MainWindow w;
    w.show();

    return app.exec();
}

MainWindow::MainWindow()
{
    setWindowTitle("QtCharts baseline");
    resize(800, 500);

    QtCharts::QChart * chart = new QtCharts::QChart;
    chart->setTitle("Baseline sample");
    chart->legend()->setAlignment(Qt::AlignRight);

    QtCharts::QDateTimeAxis * time_axis = new QtCharts::QDateTimeAxis;
    time_axis->setFormat("hh:mm:ss");
    time_axis->setTitleText("Time");
    time_axis->setTickCount(5);

    QtCharts::QValueAxis * value_axis = new QtCharts::QValueAxis;
    value_axis->setTitleText("Value (unit)");
    value_axis->setTickCount(6);

    chart->addAxis(time_axis, Qt::AlignBottom);
    chart->addAxis(value_axis, Qt::AlignLeft);

    QtCharts::QLineSeries * ls = new QtCharts::QLineSeries;
    ls->setName("Test series");
    ls->setPointsVisible(true);
    //ls->setPointLabelsVisible(true);

    QDateTime dt = QDateTime::currentDateTime();
    ls->append(dt.toMSecsSinceEpoch(), -10);
    ls->append(dt.addSecs(1).toMSecsSinceEpoch(), 8);
    ls->append(dt.addSecs(2).toMSecsSinceEpoch(), 27);
    ls->append(dt.addSecs(3).toMSecsSinceEpoch(), 12);
    ls->append(dt.addSecs(4).toMSecsSinceEpoch(), 42);
    chart->addSeries(ls);
    ls->attachAxis(time_axis);
    ls->attachAxis(value_axis);

    QtCharts::QChartView * view = new QtCharts::QChartView;
    view->setChart(chart);
    this->setCentralWidget(view);
}

来源:https://stackoverflow.com/questions/58707516/how-to-make-only-one-point-label-visible-for-qlineseries-qxyseries

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