ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  How to Prevent Zooming in Too Close (FinanceChart)
Posted by MarkZB on Jun-07-2018 22:54
Attachments:
I get an error if I zoom in too close on a FinanceChart.

I've tried intercepting  ViewPortWidth but this isnt bullet proof.
if (viewer.ViewPortWidth < 0.01) viewer.ViewPortWidth = 0.01;

*Plus are there methods to perform zoom in/zoom out?
ZoomError.png

  Re: How to Prevent Zooming in Too Close (FinanceChart)
Posted by Peter Kwan on Jun-08-2018 00:17
Hi MarkZB,

If the chart is completely empty, it is possible for getNearestXValue to return Chart.NoValue, as there is no "nearest x value" for an empty chart. If you think it is possible for your chart, your code would need to check for this condition and handle it properly (such as immediately return without doing anything, as there is nothing to draw).

I am not sure how your code implements the zoom. There are a number of post in this forum regarding zooming in a FinanceChart, with some code fragments:

http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&thread=1467686966#N1467786759

http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&thread=1377713024#N1378380007

http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&thread=1459929537#N1459969772


In brief, the code is something like below. It ensures there is at least 10 points in the chart, and also allows for the "extra leading points" required for technical indicators.


.... during initialization ....
// Set the full x range to be the duration of the data
viewer.setFullRange("x", 0, timeStamps.Length - 1);

// Initialize the view port to show the latest 100 points
viewer.ViewPortWidth = 100.0 / timeStamps.Length;
viewer.ViewPortLeft = 1 - viewer.ViewPortWidth;

// Set the maximum zoom to 10 points
viewer.ZoomInWidthLimit = 10.0 / timeStamps.Length;


.... in draw chart ....
// Get the start date and end date that are visible on the chart.
int startIndex = (int)Math.Floor(viewer.getValueAtViewPort("x", viewer.ViewPortLeft));
int endIndex = (int)Math.Ceiling(viewer.getValueAtViewPort("x", viewer.ViewPortLeft + viewer.ViewPortWidth));

// To compute moving averages starting from the first day, we need to get extra data points before the first day
int extraDays = Math.Min(30, startIndex);
startIndex -= extraDays;
int noOfPoints = endIndex - startIndex + 1;

// Extract the part of the data array that are visible.
DateTime[] viewPortTimeStamps = (DateTime[])Chart.arraySlice(timeStamps, startIndex, noOfPoints);
double[] viewPortHighData = (double[])Chart.arraySlice(highData, startIndex, noOfPoints);
double[] viewPortLowData = (double[])Chart.arraySlice(lowData, startIndex, noOfPoints);
double[] viewPortOpenData = (double[])Chart.arraySlice(openData, startIndex, noOfPoints);
double[] viewPortCloseData = (double[])Chart.arraySlice(closeData, startIndex, noOfPoints);
double[] viewPortVolData = (double[])Chart.arraySlice(volData, startIndex, noOfPoints);


Hope this can help.

Regards
Peter Kwan

  Re: How to Prevent Zooming in Too Close (FinanceChart)
Posted by MarkZB on Jun-08-2018 03:48
Hi Peter

The problem is not with Zoom but with the display of the vertical track cursor.

If I disable...

trackFinance((MultiChart)viewer.Chart, viewer.PlotAreaMouseX);

the crash problem problem goes away.

Any ideas on how to solve?

  Re: How to Prevent Zooming in Too Close (FinanceChart)
Posted by Peter Kwan on Jun-08-2018 13:51
Hi MarkZB,

I think the cause of the problem is because the chart is completely empty (may be the  zooming code you are currently using can create an empty chart when zooming in too much).

The current trackFinance code does not handle a completely empty chart, in which case no track line can be drawn. My previous message suggests some zooming code that always ensures the chart is not empty. This can solve the problem if it is caused by an empty chart.

If you want to allow an empty, please modify the trackFinance code to check for this case and abort the drawing immediately.

For the xValue, please modify the code to:

double xValueF = ((XYChart)m.getChart(0)).getNearestXValue(mouseX);
if (xValueF == Chart.NoValue)
   return;   //possibly an empty chart so no track line can be drawn
int xValue = (int)xValueF;

Hope this can help.

Regards
Peter Kwan

  Re: How to Prevent Zooming in Too Close (FinanceChart)
Posted by MarkZB on Jun-08-2018 15:10
Attachments:
Hi Peter

Ok that appears to have done the trick.

How do I prevent "Zooming to Empty"?

BTW on the Zoombar - I added a check for <1 check else it crashes

var zoomBarVal = (int)Math.Round(Math.Min(viewer.ViewPortWidth, viewer.ViewPortHeight) *zoomBar.Maximum);
if (zoomBarVal < 1) zoomBarVal = 1;
zoomBar.Value = zoomBarVal;
ZOOM_EMPTY.gif

  Re: How to Prevent Zooming in Too Close (FinanceChart)
Posted by Peter Kwan on Jun-09-2018 04:43
Hi MarkZB,

In the image in your post, it seems to suggest the zoomBar can cause the viewport to change. However, in your attached code, it is the reverse, that is, the viewport cause the zoomBar to change.

Anyay, in the response in my earlier post, the code is already an example of how to avoid the empty chart. It ensures there is at least 1 data point. The code is repeated as follows:

// Get the start date and end date that are visible on the chart.
int startIndex = (int)Math.Floor(viewer.getValueAtViewPort("x", viewer.ViewPortLeft));
int endIndex = (int)Math.Ceiling(viewer.getValueAtViewPort("x", viewer.ViewPortLeft + viewer.ViewPortWidth));

// To compute moving averages starting from the first day, we need to get extra data points before the first day
int extraDays = Math.Min(30, startIndex);
startIndex -= extraDays;
int noOfPoints = endIndex - startIndex + 1;

// Extract the part of the data array that are visible.
DateTime[] viewPortTimeStamps = (DateTime[])Chart.arraySlice(timeStamps, startIndex, noOfPoints);
double[] viewPortHighData = (double[])Chart.arraySlice(highData, startIndex, noOfPoints);
double[] viewPortLowData = (double[])Chart.arraySlice(lowData, startIndex, noOfPoints);
double[] viewPortOpenData = (double[])Chart.arraySlice(openData, startIndex, noOfPoints);
double[] viewPortCloseData = (double[])Chart.arraySlice(closeData, startIndex, noOfPoints);
double[] viewPortVolData = (double[])Chart.arraySlice(volData, startIndex, noOfPoints);


I am not sure how your zoomBar can change the viewport. You can simply implement your code so that the zoomBar cannot change the viewport to become too small. For example, you can set the zoomBar to be from 10 to 100, which may refer to the viewport width of 0.1 to 1. Instead of fixing the lower bound to 10, you may dynamically compute it so that it contains at least one data point. (By the way, in your code that sets the zoomBar based on the viewport, it seems to assume the minimum value of the zoomBar to be 0, which may not be what you want.)


Regards
Peter Kwan

  Re: How to Prevent Zooming in Too Close (FinanceChart)
Posted by MarkZB on Jun-09-2018 13:45
This is still a work in progress for me.  Will revert back.