gpt4 book ai didi

c++ - QChart 日志轴数据未显示

转载 作者:行者123 更新时间:2023-11-30 04:56:38 24 4
gpt4 key购买 nike

我有一个 2Y 轴图,在 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);
}

最佳答案

范围不是指缩放值,而是实际值,例如在您的情况下它应该是从 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

关于c++ - QChart 日志轴数据未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52407267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com