jkriege2 / JKQtPlotter

an extensive Qt5 & Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
http://jkriege2.github.io/JKQtPlotter/index.html
GNU Lesser General Public License v2.1
898 stars 190 forks source link

How to control axis tick interval? #136

Open taibai123abc opened 1 month ago

taibai123abc commented 1 month ago

image As you can see in the picture, the tick labels will overlap when the tick labels are long. So how to control axis tick interval?

jkriege2 commented 1 month ago
  1. JKQTPlotter::getXAxis() (see https://jkriege2.github.io/JKQtPlotter/class_j_k_q_t_plotter.html#af0b876017115828f4d14581621808323) returns the x-axis-object
  2. There are a lot of properties in JKQTPCoordinateAxis (see https://jkriege2.github.io/JKQtPlotter/class_j_k_q_t_p_coordinate_axis.html) that control the ticks ... e.g. use setMinTicks (default=5, see https://jkriege2.github.io/JKQtPlotter/class_j_k_q_t_p_coordinate_axis.html#a0eca026ef834ac65e4f86316717382b7) to reduce the minimum number of ticks to place,
  3. or switch to manual labels all together, see addAxisTickLabels(), https://jkriege2.github.io/JKQtPlotter/class_j_k_q_t_p_coordinate_axis.html#a1b7a8dbe23830a47bfb06f4f2fdaf522 and https://jkriege2.github.io/JKQtPlotter/_j_k_q_t_plotter_barcharts.html
  4. You can also declutter by rotating the labels using setTickLabelAngle() (see https://jkriege2.github.io/JKQtPlotter/class_j_k_q_t_p_coordinate_axis.html#a0a155e789899833d69f32c75fd7e45f7)

Hope that helps ... but it seems an example for all this is missing ...

taibai123abc commented 1 month ago

Thanks for your response and it is useful for me. But when tick label is too long, a natural idea is to make it able to change lines, but when I use ui->plot->getXAxis()->setTickDateTimeFormat("yyyy/MM/dd\nHH:mm:ss");, it has no effect.

jkriege2 commented 1 month ago

All labels/text in JKQTPlotter use Latex-markup ... try using \ (I.e. in c-code "...\\..." instead of \n

jkriege2 commented 1 month ago

The documentation of JkQtMathText has a large list of available LaTeX : https://jkriege2.github.io/JKQtPlotter/group__jkqtmathtext__supportedlatex.html

taibai123abc commented 1 month ago

I hava tried "\", but it didn't work.

jkriege2 commented 1 month ago

Could you post a code-snippet?

taibai123abc commented 1 month ago

Of course i can. I use the following code to read data from a file:

    QFile file(fileName);
    if (!fileName.isEmpty())
    {
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug() << "Failed to open the file.";
            return;
        }
        QTextStream infile(&file);
        QString line;
        QVector<double> temp_points;
        int i = 0;
        QVector<double> tempX;
        QVector<double> tempY;
        while (!infile.atEnd())
        {
            QString line = infile.readLine();
            QStringList parts;
            if (line.contains(" "))  parts = line.split(" ");
            else if (line.contains(","))  parts = line.split(",");
            QString dataAndTimeString = parts[0] + " " + parts[1];               
            dateAndTime.push_back(QDateTime::fromString(dataAndTimeString, "yyyy/MM/dd HH:mm:ss").toUTC().toMSecsSinceEpoch());
            squeezeData.push_back(parts[2].toDouble());             
        }

The datas of the file like the picture: image Then i use the dateAndTime and squeezeData to draw a time-height graph.

JKQTPDatastore* ds = ui->plot->getDatastore();
QVector<double> partXData = dateAndTime.mid(0, ui->numSpinBox->value());
QVector<double> partYData = squeezeData.mid(0, ui->numSpinBox->value());
ui->plot->addGraph(graph);
size_t colDate = ds->addCopiedColumn(partXData, "date");
size_t colSq = ds->addCopiedColumn(partYData, "squeeze");
graph->setXColumn(colDate);
graph->setYColumn(colSq);

graph->setLineStyle(Qt::SolidLine);
graph->setSymbolType(static_cast<JKQTPGraphSymbols>(0));
graph->setTitle(QObject::tr("height"));

ui->plot->getXAxis()->setTickLabelType(JKQTPCALTdatetime);
ui->plot->getXAxis()->setAxisLabel("dataTime");
ui->plot->getXAxis()->setTickDateTimeFormat("yyyy/MM/dd HH:mm:ss");
ui->plot->getXAxis()->setTickLabelFontSize(5);
ui->plot->getXAxis()->setTickSpacing(2);
ui->plot->zoomToFit();
ui->plot->show();

but the "yyyy/MM/dd HH:mm:ss" is too long to make the tick label too sparse or overlaped. So i want to convert "yyyy/MM/dd HH:mm:ss" to two lines. I tried "yyyy/MM/dd\HH:mm:ss" or "yyyy/MM/dd\nHH:mm:ss", but it dosen't works. Maybe the JKQTPlotter dosen't have the function? I know the QCustomPlot has the function and it is useful. Maybe JKQTPlotter should have the function too?

jkriege2 commented 1 month ago

try using ui->plot->getXAxis()->setTickDateTimeFormat("yyyy/MM/dd\\HH:mm:ss");

This will end up as a string "yyyy/MM/dd\HH:mm:ss" after the compiler has resolved the c-escapes "\" -> '\' And the newline ist \ in LaTeX

taibai123abc commented 1 month ago

I am sorry, i spell error in last response.I have triedyyyy/MM/dd\\HH:mm:ssand yyyy/MM/dd\nHH:mm:ss, but it dosen't works.