Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to control axis tick interval? #136

Open
taibai123abc opened this issue Oct 8, 2024 · 9 comments
Open

How to control axis tick interval? #136

taibai123abc opened this issue Oct 8, 2024 · 9 comments

Comments

@taibai123abc
Copy link

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
Copy link
Owner

jkriege2 commented Oct 8, 2024

  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
Copy link
Author

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
Copy link
Owner

jkriege2 commented Oct 9, 2024

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

@jkriege2
Copy link
Owner

jkriege2 commented Oct 9, 2024

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

@taibai123abc
Copy link
Author

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

@jkriege2
Copy link
Owner

Could you post a code-snippet?

@taibai123abc
Copy link
Author

taibai123abc commented Oct 16, 2024

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
Copy link
Owner

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
Copy link
Author

taibai123abc commented Oct 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants