Reputation: 111
I have set of about 600 pairs of x and y value that should be plotted. They are read as doubles from a CSV file
void ChartView::getData4Series()
{
QFile file(QCoreApplication::applicationDirPath() + "/../saveData/" + m_videoPath + "/" + "SaveDisplace.csv");
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(0, "error", file.errorString());
}
QTextStream in(&file);
QString line;
QStringList fields;
QtCharts::QScatterSeries* series = new QtCharts::QScatterSeries;
m_seriesTy->clear();
m_seriesTz->clear();
bool checkforChar ;
while (!in.atEnd())
{
line = in.readLine();
fields = line.split(";");
fields[2].toDouble(&checkforChar);
if (checkforChar)
{
if (fields[2].toDouble() <= 100.0 && fields[2].toDouble() >= -5000.0)
{
if (fields[6].toDouble() > -100000.0)
{
m_seriesTy->append(fields[2].toDouble(), (fields[3].toDouble() - fields[6].toDouble()));
m_seriesTz->append(fields[2].toDouble(), (fields[4].toDouble() - fields[7].toDouble()));
}
}
}
}
file.close();
}
I set up the chart by setting the axis, given the limits and adding a further line (only 2 value pairs) as reference.
void ChartView::setZErrorCharts(QtCharts::QScatterSeries* series)
{
series->setMarkerSize(8);
QtCharts::QValueAxis *xAxis = new QtCharts::QValueAxis;
QtCharts::QValueAxis *yAxis = new QtCharts::QValueAxis;
xAxis->setRange(-5000, 0);
yAxis->setRange(-500, 500);
QtCharts::QLineSeries* lineSeries = new QtCharts::QLineSeries;
lineSeries->append(qreal(0), qreal(0));
lineSeries->append(qreal(-5000), qreal(0));
m_chartTzError = new QtCharts::QChart();
m_chartTzError->setAxisX(xAxis);
m_chartTzError->setAxisY(yAxis);
m_chartTzError->setTitle("tz error ");
m_chartTzError->legend()->hide();
m_chartTzError->addSeries(series);
m_chartTzError->addSeries(lineSeries);
}
What I can see is that the value of axis don't fit to the actual values of the plotted one. I also plotted the values as labels and they also differ from the x and y axis values. How is the scaling of the axis arranged. I tried to mess aroud with the order, setting the range of the axis before adding the data series and vise versa. The values marked by the red circle should be around 0 but are at approximatley -200. Any idea how to fix this. Qt reference doesn't seem to help.
Upvotes: 4
Views: 9614
Reputation: 59
I had the same problem and solved it this way:
You create the axes which you need first. For example:
QLogValueAxis *axisX = new QLogValueAxis();
axisX->setTitleText("Frequency [Hz]");
axisX->setRange(200, 8000);
axisX->setMinorGridLineVisible(true);
axisX->setMinorTickCount(10);
chart->addAxis(axisX, Qt::AlignBottom);
QValueAxis *axisY = new QValueAxis();
axisY->setRange(20, 150);
axisY->setTitleText("dB");
chart->addAxis(axisY, Qt::AlignLeft);
Then, you create your series':
QLineSeries *series = new QLineSeries(chart);
series->append(x,y);
Add the series to your chart:
chart->addSeries(series);
Finally, attach the right axis to the series:
series->attachAxis(axisX);
series->attachAxis(axisY);
When you plot it now:
ui->chart_view->setChart(chart);
Everything should be in the right scale.
I think the decisive step is to attach the right corresponding axis when you add the series to the chart.
Upvotes: 2