QChart日志轴数据未显示

时间:2018-09-19 13:39:20

标签: c++ qt qt5 qtchart

我有一个2Y轴plo,在y1上有QValueAxis,在y2上有QLogValueAxis

显示线性图,未显示对数图。两者的数据相同。 enter image description here

我想知道如何为对数轴设置刻度和限制吗?

这是代码:

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);
}

1 个答案:

答案 0 :(得分: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);
}

enter image description here