|
Increment/Decrement viewport |
Posted by Alden on May-20-2011 02:24 |
|
Hi Peter,
I'm trying to move the viewport backwards/forwards by ONE bar using the left/right arrow
keys. I'm trying not to do it by date so that weekends/holidays are not an issue.
I've done this so far for the right arrow press:
double width = viewer.getViewPortWidth();
double left = viewer.getViewPortLeft();
double days=Double.valueOf(durationCombo.getSelectedItem().toString()).doubleValue();
double change = width/days;
viewer.setViewPortLeft(left+change);
viewer.updateViewPort(true, true);
The viewport does move forwards but it's not smooth and it tends to skip days or not
move for 2 or 3 presses.
Is there an easier/better way to do this to smooth out the movement?
Thanks!
Alden |
Re: Increment/Decrement viewport |
Posted by Peter Kwan on May-21-2011 03:08 |
|
Hi Alden,
In a standard financial chart, the x-axis should be treated as trading session number, not date/time.
The timestamps in a financial chart are for human reading only, and can be considered as "names". They are not used to position the bars. (The bars are always evenly spaced irrespective of what are the timestamps.) Also, the timestamps are arbitrary and unpredictable (they miss Sat, Sun, national holidays, or non-trading days due to hurrican or typhoons, etc).
The code you mentioned in your message seems correct. I think the part you need to modify is the code that actually plots the chart. For example, the durationCombo should be interpreted as the number of trading sessions (not the number of calendar dates).
For example, in "createChart", you may use the following code to compute the startIndex and endIndex. (The code assumes the x-axis is from 0 to (timeStamps.length - 1), which are the trading session numbers.)
int startIndex = (int)Math.round(viewer.getViewPortLeft() * (timeStamps.length - 1));
int endIndex = (int)Math.round((viewer.getViewPortLeft() + viewer.getViewPortWidth()) * (timeStamps.length - 1));
In "chartViewer1_ViewPortChanged", you may update the code to reflect how the duration and start date user interface should be updated when the view port changed:
int startIndex = (int)Math.round(viewer.getViewPortLeft() * (timeStamps.length - 1));
int endIndex = (int)Math.round((viewer.getViewPortLeft() + viewer.getViewPortWidth()) * (timeStamps.length - 1));
setStartDate(timeStamps[startIndex]);
duration.setSelectedItem("" + (endIndex - startIndex + 1));
You may also need to modify the code in "duration_ValueChanged" and "startDate_ValueChanged" to perform the reverse (to modify the view port should the user changes the duration or start date).
Hope this can help.
Regards
Peter Kwan |
Re: Increment/Decrement viewport |
Posted by Alden on May-23-2011 01:15 |
|
That did it!
Thanks Peter. |
|