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

Message ListMessage List     Post MessagePost Message

  Gettings X axis Date Coordinates
Posted by Alden on Jun-06-2011 21:34
Hi Peter,

I'm struggling to get the date value on my x axis from the following code (getting the Y
value is not a problem):

double xAxisMin = XYChart.xAxis().getMinValue();
double xAxisMax = XYChart.xAxis().getMaxValue();
int pixelAtXMin = XYChart.getXCoor(xAxisMin);
int pixelAtXMax = XYChart.getXCoor(xAxisMax);

double xValue = (x - pixelAtXMin) / (pixelAtXMax - pixelAtXMin) * (xAxisMax - xAxisMin)
+ xAxisMin;

The plotted values are passed into createChart() as DATE (not double), so I get a zero
value each time I print to the java console.

How do I get a bar as a session number/value to use for plotting, and then get the date
value from that session number?

Any suggestions?
Alden

  Re: Gettings X axis Date Coordinates
Posted by Peter Kwan on Jun-07-2011 01:31
Hi Alden,

The xValue you got is already the visible session number. It can be a fractional value if the coordinate is in between two sessions.

If you want to get the nearest timestamp, the code is:

java.util.Date myData = myTimeStamps[(int)Math.round(xValue) + extraPoints];

In the above, I use Math.round(xValue) to get the nearest visible session. Because some leading sessions may be invisible (in case you use the extraPoints parameter in FinanceChart.setData), we need to add them back to get the session in your myTimeStamps array. Then we can get the date from your myTimeStamps array.

For simplicity and ease of understanding, I have not added bounds check in the above code. For robustness, you should add bounds check (eg. to handle the index of our range cases).

Hope this can help.

Regards
Peter Kwan

  Re: Gettings X axis Date Coordinates
Posted by Alden on Jun-07-2011 02:22
Hi Peter,

It seems as though data is being used for the whole dataset instead of just what's visible
in the viewport. I tried the following but it returns one date no matter where on the x axis
the mouse is located:

int startIndex = (int)Math.round(viewer.getViewPortLeft() * (timeStamps.length - 1));
int endIndex = (int)Math.round((viewer.getViewPortLeft() + viewer.getViewPortWidth()) *
(timeStamps.length - 1));

int pixelAtXMin = mainChart.getXCoor(startIndex);
int pixelAtXMax = mainChart.getXCoor(endIndex);

int noOfPoints = endIndex - startIndex+1;

double xValue = (x - pixelAtXMin) / (pixelAtXMax - pixelAtXMin) * (endIndex - startIndex)
+ startIndex;

Date myDate = timeStamps[(int)Math.round(xValue) + noOfPoints];

What am I doing wrong?
Alden

  Re: Gettings X axis Date Coordinates
Posted by Alden on Jun-07-2011 21:01
... following on from the previous post:

In both the previous posts's code I get a static (and sometimes negative) number for the
xValue. Then if I zoom out/in the viewport I get a different but also static number for the
double xValue.

Alden

  Re: Gettings X axis Date Coordinates
Posted by Peter Kwan on Jun-07-2011 23:09
Hi Alden,

From your code structure, I assume your code is based on the "Zooming and Scrolling" sample code.

Because startIndex, endIndex, noOfPoints and timeStamps are never passed to ChartDirector (there is no ChartDirector API calls that uses these parameters in the code), so they should not be used to compute the xValue and myData.

Instead, please use the code as mentioned in my previous message:

double xAxisMin = mainChart.xAxis().getMinValue();
double xAxisMax = mainChart.xAxis().getMaxValue();
int pixelAtXMin = mainChart.getXCoor(xAxisMin);
int pixelAtXMax = mainChart.getXCoor(xAxisMax);

double xValue = (x - pixelAtXMin) / (pixelAtXMax - pixelAtXMin) * (xAxisMax - xAxisMin)
+ xAxisMin;

In the "Zooming and Scrolling" sample code, the actual data array passed to ChartDirector is viewPortTimeStamps, so the array to use should be viewPortTimeStamps:

Date myDate = viewPortTimeStamps[(int)Math.round(xValue) + extraPoints];

Note that all of the above code should only be used after the chart is drawn (after calling "setChart"). Also, the "x" should be the x-coordinate relative to the chart, and the extraPoints is the leading points in viewPortTimeStamps which are not visible (the 7th parameter in the FinanceChart.setData call).

Hope this can help.

Regards
Peter Kwan

  Re: Gettings X axis Date Coordinates
Posted by Alden on Jun-08-2011 00:07
Hi Peter,

Thanks for your patience and help.

Using the code you suggested, regardless of where the mouse moves to, it returns the
first visible date on the chart (viewportleft?).

It's still not returning the date at the x-coordinate the mouse is over?

Alden

  Re: Gettings X axis Date Coordinates
Posted by Alden on Jun-08-2011 01:13
further to my previous post, the following is returning is returning 0:

double xValue = (x - pixelAtXMin) / (pixelAtXMax - pixelAtXMin) * (xAxisMax - xAxisMin)
+ xAxisMin;

  Re: Gettings X axis Date Coordinates
Posted by Peter Kwan on Jun-08-2011 02:40
Hi Alden,

Are you declaring x as an integer?

Note that x should be declared a floating point number such as "double". (I hope it is clear in the code that it is using floating point arithmatic.) In integer arithmatic, the (x - pixelAtXMin) / (pixelAtXMax - pixelAtXMin) is always equal to 0 (eg. 100/124 is equal to 0 in integer arithmatic).

To be sure, just cast the x to double if it is not already in double:

double xValue = ((double)x - pixelAtXMin) / (pixelAtXMax - pixelAtXMin) * (xAxisMax - xAxisMin) + xAxisMin;

Hope this can help.

Regards
Peter Kwan

  Re: Gettings X axis Date Coordinates
Posted by Alden on Jun-08-2011 04:32
Thanks Peter,

Casting x to double solved the problem.

I'm trying to do the bounds check. Obviously the minimum bound is 0 ie xValue must be
>=0. But I'm unsure which value to use for the max boundary. I've tried using
xValue<=(double)viewPortTimeStamps.length-1 but I get an exception (out of bounds).

Is there a better way?
Alden

  Re: Gettings X axis Date Coordinates
Posted by Peter Kwan on Jun-08-2011 16:41
Hi Alden,

You should check if the array index is out of bounds, not the variables.

The array index you are uisng is "(int)Math.round(xValue) + extraPoints", so you should check this for bounds. For example:

if ((int)Math.round(xValue) + extraPoints >= viewPortTimeStamps.length) {
   //out of bounds - the mouse probably has moved outside the plot area
}

Hope this can help.

Regards
Peter Kwan

  Re: Gettings X axis Date Coordinates
Posted by Alden on Jun-09-2011 01:51
Ok, the following sorted out the problem:

if (xValue<=0 || ((int)Math.round(xValue) + extraPoints >= viewPortTimeStamps.length-
1)){

                return;

}

Thanks Peter!