0

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: AtBeginning

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:

Zoomed

I want it look like this: Scrolled up

So, I called the scroll method of chart:

chart->scroll( 0, -50 );

But it obviously cannot be applied within my product program, As:

  1. 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...
  2. 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.

Leon
  • 1,489
  • 1
  • 12
  • 31
  • What you want is confusing, for example in your third image there is an uncovered space on the left vertical axis. In addition there are cases that you can not cover the vertical part or that there are many sections that meet that condition, when you zoom you will see a deltay = (ymax - ymin) that you want to be covered but what section of the plot should be displayed? , Not only place an image of what you want but explain where in that image are the requirements you want. – eyllanesc Jan 17 '20 at 05:29
  • So that you understand me better, I am going to propose an extreme case: Let's say that the curve is a vertical line so large that it will always cover the vertical axis and that it passes through the midpoint of the horizontal axis, so according to that characteristic I understand that it should cover your requirements, now if we move the line a little the left would also be valid, so in what position should the line be placed? And let's go to the other extreme case: a horizontal line What should happen in that case? – eyllanesc Jan 17 '20 at 05:34
  • Now if we use the extreme cases we can deform those lines a bit and also those cases generate those same problems. – eyllanesc Jan 17 '20 at 05:37
  • IMHO I think you have not analyzed all the cases, according to what I analyze for a C curve there are points where what you want will never be fulfilled, and there are other points where there is more than one possibility. Which of these possibilities should be used? I think you need to limit your problem. – eyllanesc Jan 17 '20 at 05:39
  • Thanks for your reply! I thinks I know what you hinted: When a zooming performed, the range of axis-X would change(narrow for ZoomIn), but the range of axis-Y would not certainly change(maybe change,maybe not). So I need to find out the new Max-Y-Value and the new Min-Y-Value of the new range of X after zooming, and then invoke axisY->setRange(New-Min-Y-Value, New-Max-Y-Value), in order to make the part of a curve that falls in the new range of axis-X appear in the visible area. If I don't do this, I must scroll the chart so that it can appear in visible area. My question is how to auto do. – Leon Jan 17 '20 at 07:57
  • This is a very common requirement, so I guess that there must be a auto-way to do so such as by setting few of properties of some objs. If we have to make a call to scroll() of a chart obj(or setRange() of a axis obj) after zoomIn the chart, in fact the chart would be re-paint twice, but I think the first re-paint(triggered by zoomIn() ) is not required. – Leon Jan 17 '20 at 08:06
  • In your visual example, what is new Max-Y-Value and new Min-Y-Value? on the other hand if you use zoomIm both axes will be modified. Finally you say that it is a common requirement, you could point out the SWs that implement it – eyllanesc Jan 17 '20 at 19:27
  • Sorry, it is very hard for me to express actually/precisely in English(I'm a Chinese). I do NOT mean "Qt is bad" in the statement "It's a common requirement", but I think there must be someone encountered same/similar problem as mine. Just now I learnt a concept "Rubber Band", and maybe it can resolve my problem. The final job I have to do, is finger out "how to adjust axis-Y in rubberBandChanged() of QGraphicsView". Indeed with the words "new Max-Y-Value/Min-Y-Value", I was meaning the max-Y/min-Y witin the rubber band. Thank you for your help and patient! – Leon Jan 18 '20 at 03:25

0 Answers0