I'm learning QtCharts. I need to zoom a chart, and adjust the range of the axis-Y accordingly, in order to make the logical visible part of the line can be completely plotted in the real visible area of the ChartView.
For example:
auto chart = new QtCharts::QChart;
auto lines = new QtCharts::QLineSeries;
QDateTime dt = QDateTime::currentDateTime();
for( int i = 0; i < 100; ++i ) {
lines->append( dt.toMSecsSinceEpoch(), std::pow( i, 2 ) );
dt = dt.addMSecs( 500 );
}
chart->addSeries( lines );
auto axisX = new QtCharts::QDateTimeAxis( chart );
axisX->setTickCount( 13 );
axisX->setFormat( "ss.zzz" );
chart->addAxis( axisX, Qt::AlignBottom );
lines->attachAxis( axisX );
auto axisY = new QtCharts::QValueAxis( chart );
axisY->setLabelFormat( "%i" );
chart->addAxis( axisY, Qt::AlignLeft );
lines->attachAxis( axisY );
auto cv = new QtCharts::QChartView( chart );
setCentralWidget( cv );
resize( 800, 600 );
cv->show();
At beginning, my chart looks like this:
When zooming in, I call "zoomin" method of chart:
chart->zoomIn();
But the line would "goes out of the visible area of the View", like this:
So, I called the scroll method of chart:
chart->scroll( 0, -50 );
But it obviously cannot be applied within my product program, As:
- I don't wana "repaint" multi-times, as I belive that the chart would be repaint after any calls to axisY->setRange and chart->zoom, and chart->scroll, and so on...
- How should I finger out the arguments of the axisY->setRange to adjust it? I looked through the members of QLineSeries/QChart/QValue/QChartView, but I didn't found a way to calculate the new max/min value of the axis-Y.
I belive that there must be a method can resolve my problem, but I don't know. Thanks! Sorry for my poor English.