QChart log axis data not shown

1.2k views Asked by At

I have a 2Y Axis plo with a QValueAxison the y1 and QLogValueAxis on y2.

The linear plot is shown, the log plot is not shown. The data for both is the same. enter image description here

I wonder how to set up the ticks and limits for the log axis?

This is the code:

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

There are 1 answers

0
eyllanesc On

The range does not refer to the scaled values, but to the actual values, for example in your case it should be from epsilon to 100. On the other hand the values that are shown in the logarithmic scale must be positive, in your case I see that there is zeros so a possible solution is to add an 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