QCustomPlot:如何在 y 轴上设置百分比?如何更改刻度标签对齐方式?
QCustomPlot: How to set percent on the y-axis? How to change tick label alignment?
我正在研究 Qt 的 QCustomPlot 库。我成功地创建了一些地块。但我还有问题:
1:如何将 y 轴范围设置为 0% 到 100%?
2:我的刻度标签在刻度下方居中。我怎样才能将其更改为左对齐?
感谢您的帮助。
彼得
// generate some data:
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = (i*960); // x goes from -1 to 1
y[i] = x[i]/96000.0; // let's plot a quadratic function
}
// create graph and assign data to it:
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x, y);
// set axes ranges, so we see all data:
ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
ui->customPlot->xAxis->setDateTimeSpec(Qt::UTC);
ui->customPlot->xAxis->setDateTimeFormat("hh:mm");
ui->customPlot->xAxis->setAutoTickStep(false);
ui->customPlot->xAxis->setTickStep(3600);
ui->customPlot->xAxis->setRange(0, 86399);
ui->customPlot->yAxis->setRange(0, 1);
ui->customPlot->replot();
ui->customPlot->xAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickLabelColor(Qt::white);
ui->customPlot->yAxis->setTickLabelColor(Qt::white);
ui->customPlot->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->grid()->setSubGridVisible(true);
ui->customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
ui->customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
你应该用
解决这两个问题
ui->customPlot->yAxis->setRange(0, 100, Qt::AlignLeft);
编辑:要显示刻度的自定义文本,您应该添加此代码:
QVector<double> TickValues;
QVector<QString> TickLabels;
// you can safely change the values according to the output
TickValues << 0 << 20 << 40 << 60 << 80 << 100;
TickLabels << "0" << "20%" << "40%" << "60%" << "80%" << "100%";
// disable default ticks and their labels
ui->customPlot->yAxis->setAutoTicks(false);
ui->customPlot->yAxis->setAutoTickLabels(false);
// add your custom values and labels
ui->customPlot->yAxis->setTickVector(TicksValues);
ui->customPlot->yAxis->setTickVectorLabels(TickLabels);
我正在研究 Qt 的 QCustomPlot 库。我成功地创建了一些地块。但我还有问题:
1:如何将 y 轴范围设置为 0% 到 100%?
2:我的刻度标签在刻度下方居中。我怎样才能将其更改为左对齐?
感谢您的帮助。
彼得
// generate some data:
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = (i*960); // x goes from -1 to 1
y[i] = x[i]/96000.0; // let's plot a quadratic function
}
// create graph and assign data to it:
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setData(x, y);
// set axes ranges, so we see all data:
ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
ui->customPlot->xAxis->setDateTimeSpec(Qt::UTC);
ui->customPlot->xAxis->setDateTimeFormat("hh:mm");
ui->customPlot->xAxis->setAutoTickStep(false);
ui->customPlot->xAxis->setTickStep(3600);
ui->customPlot->xAxis->setRange(0, 86399);
ui->customPlot->yAxis->setRange(0, 1);
ui->customPlot->replot();
ui->customPlot->xAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickLabelColor(Qt::white);
ui->customPlot->yAxis->setTickLabelColor(Qt::white);
ui->customPlot->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->grid()->setSubGridVisible(true);
ui->customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
ui->customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
你应该用
解决这两个问题ui->customPlot->yAxis->setRange(0, 100, Qt::AlignLeft);
编辑:要显示刻度的自定义文本,您应该添加此代码:
QVector<double> TickValues;
QVector<QString> TickLabels;
// you can safely change the values according to the output
TickValues << 0 << 20 << 40 << 60 << 80 << 100;
TickLabels << "0" << "20%" << "40%" << "60%" << "80%" << "100%";
// disable default ticks and their labels
ui->customPlot->yAxis->setAutoTicks(false);
ui->customPlot->yAxis->setAutoTickLabels(false);
// add your custom values and labels
ui->customPlot->yAxis->setTickVector(TicksValues);
ui->customPlot->yAxis->setTickVectorLabels(TickLabels);