I started playing with the various signals available, and I managed to get my sample program working as desired. I'm not sure whether this is the approved method, or if it scales well, but it works for me.
One thing that wasn't especially clear to me is that the scroll bars on the QChartView
don't scroll the right thing. They scroll the whole QChart
, axes and all. To scroll the zoomed data in the chart, you need to add a separate scroll bar, and then call the QChart
's scroll()
function in response to the valueChanged
signal. (I didn't test this, but it might work to call the QAxis
's setRange( <i>min</i>, <i>max</i> )
instead.)
Here is code that displays two sine-wave cycles and allows rubber-band zooming. When zoomed, you can scroll back and forth along the wave.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, scrolling( false )
{
ui->setupUi(this);
QLineSeries *s = new QLineSeries;
QChart *c = new QChart;
for (double theta = 0.0; theta < 4.0 * M_PI; theta += M_PI / 50.0)
s->append( theta / M_PI, sin( theta ) );
c->addSeries( s );
c->createDefaultAxes( );
c->legend( )->hide( );
ui->chartView->setChart( c );
ui->chartView->setRubberBand( QChartView::HorizontalRubberBand );
// PlotAreaChanged seems like it might be useful, but not for this purpose
// rangeChanged (on the axis) is the one you need.
connect( c->axisX( ), SIGNAL(rangeChanged(qreal, qreal)), this, SLOT(on_rangeChanged(qreal,qreal)) );
}
void
MainWindow::on_chartScroll_valueChanged( int v )
{
if (!scrolling) {
scrolling = true;
// cerr << "scroll " << v << endl;
ui->chartView->chart( )->scroll( v - sv, 0 );
sv = v;
scrolling = false;
}
}
void
MainWindow::on_rangeChanged( qreal min, qreal max )
{
if (scrolling) return; // Scrolling causes range changes, but we don't have to do anything.
QChart *c = ui->chartView->chart( );
QAbstractAxis *x = c->axisX( );
qreal avg = (min + max) / 2.0;
bool range_fixed = false;
/*
* Make sure the new range is sane; fix if not.
*/
if ((max - min) < 0.1) { // Avoid overzooming
min = avg - 0.05;
max = avg + 0.05;
range_fixed = true;
}
if (min < 0.0) { min = 0.0; range_fixed = true; }
if (max > 4.0) { max = 4.0; range_fixed = true; }
if (range_fixed) {
x->setRange( min, max ); // will re-signal with the fixed range
return;
}
qreal vis_width = c->plotArea( ).width( );
qreal all_width = vis_width * (4.0 - 0.0) / (max - min);
// cerr << "range " << min << " ... " << max << " in " << vis_width << " pixels" << endl;
// cerr << "full width requires " << all_width << " pixels" << endl;;
if (max - min < 4.0) {
// cerr << "set scroll parameters" << endl;
scrolling = true;
ui->chartScroll->setMaximum( all_width - vis_width );
sv = min / (4.0 - 0.0) * all_width;
ui->chartScroll->setValue( sv );
scrolling = false;
} else {
// cerr << "disable scroll bar" << endl;
scrolling = true;
ui->chartScroll->setMaximum( 0 );
ui->chartScroll->setValue( sv );
scrolling = false;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
(Apologies for the hard-coded 4.0
[from 4π
]. The Right Thing to do is look at the full range of the data you're scrolling through.)