2

QValueAxis我在 y1 和QLogValueAxisy2上有一个 2Y Axis plo 。

显示了线性图,未显示对数图。两者的数据相同。 在此处输入图像描述

我想知道如何设置日志轴的刻度和限制?

这是代码:

chart = new QChart();
chart->legend()->hide();
chart->setTitle("Histogramm");

axisX = new QValueAxis;
axisX->setLabelFormat("%g");
chart->addAxis(axisX, Qt::AlignBottom);

series = new QLineSeries;
chart->addSeries(series);

axisY = new QValueAxis;
axisY->setTitleText("linear scale");
axisY->setLinePenColor(series->pen().color());

chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisX);
series->attachAxis(axisY);

serieslog = new QLineSeries;
chart->addSeries(serieslog);


axisY3 = new QLogValueAxis();
axisY3->setTitleText("logarithmic scale");
axisY3->setBase(10.0);
axisY3->setLinePenColor(serieslog->pen().color());

chart->addAxis(axisY3, Qt::AlignRight);
serieslog->attachAxis(axisX);
serieslog->attachAxis(axisY3);

chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);

// Create a layout and add Chart
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(chartView);

void WidgetHistogramm::setData(const std::vector<int> data)
{
    if (data.size() <= 0)
    {
        LOG_DEBUG() << "Histogram Data empty";
        return;
    }

    auto max = *max_element(std::begin(data), std::end(data));

    QVector<QPointF> points(data.size());

    for(std::vector<int>::size_type i = 0; i != data.size(); ++i) {
      points[i] = QPointF(i, data[i]*100/max);
    }

    series->replace(points);
    serieslog->replace(points);

    chart->axisX(series)->setRange(0, data.size());
    chart->axisY(series)->setRange(0, 100);
    chart->axisX(serieslog)->setRange(0, data.size());
    chart->axisY(serieslog)->setRange(-1000, 1);
}
4

1 回答 1

1

该范围不是指缩放值,而是实际值,例如,在您的情况下,它应该是从 epsilon 到 100。另一方面,在对数刻度中显示的值必须是正的,在您的情况下,我看到有零,所以一个可能的解决方案是添加一个 epsilon:

void WidgetHistogramm::setData(const std::vector<int> data)
{
    if (data.size() <= 0){
        LOG_DEBUG() << "Histogram Data empty";
        return;
    }

    auto max = *max_element(std::begin(data), std::end(data));

    QVector<QPointF> points(data.size());

    for(std::vector<int>::size_type i = 0; i != data.size(); ++i) {
        points[i] = QPointF(i, data[i]*100.0/max + std::numeric_limits<qreal>::epsilon());
    }

    series->replace(points);
    serieslog->replace(points);

    chart->axisX(series)->setRange(0, points.size());
    chart->axisY(series)->setRange(0, 100);
    chart->axisX(serieslog)->setRange(0, points.size());
    chart->axisY(serieslog)->setRange( std::numeric_limits<qreal>::epsilon(), 100);
}

在此处输入图像描述

于 2018-09-19T15:40:05.763 回答