Usage: Can implement in different chart, either horizontal/vertical/both zoom
Sample demo can view Order Manager System(OMS) link at http://vnetcode.org
Event: When user right-click it will restore previous zooming point
Testing senario: User can zoom n times...then everytime it right-click it will restore back to previous zooming point/rectangle/plotArea (no limitation in zooming count cause it is arrayList not object/array)
Algorithm(workaround):
1)firstly store six element(hLower,hUpper,vLower,vUpper,plotInfo,selectOrigin) in ArrayList
2)Based on total size of ArrayList, we knew how many times user had zoom in
3)Hence, every time user right-click, it will firstly return to original bounds
and then start zoom in n-1 times.
4)If we want chart to restore previous zoom Point by right-click,
then we called restoreZoomPoint() in 'mouseReleased'>'else if (e.isPopupTrigger()){' method
5)Delete last index of the zooming data everytime after restore back.
Overriding code in 'org.jfree.chart.ChartPanel' as follow:
(1 global variable,1 new method,1 existing method,1 call event)
global Variable:
Code: Select all
/**
* WZW override
* ArrayList to store zoom data(in percentage)
* alLowerX, alUpperX, alLowerY, alUpperY, alPlotInfo, alSelectOrigin
* X-domainAxis, Y-rangeAxis
*/
private ArrayList<Double> alLowerX = new ArrayList<Double>();
private ArrayList<Double> alUpperX = new ArrayList<Double>();
private ArrayList<Double> alLowerY = new ArrayList<Double>();
private ArrayList<Double> alUpperY = new ArrayList<Double>();
private ArrayList<PlotRenderingInfo> alPlotInfo = new ArrayList<PlotRenderingInfo>();
private ArrayList<Point2D> alSelectOrigin = new ArrayList<Point2D>();
New method:
Code: Select all
/**
* WZW override
* Restore zoomPoints based on storing of 6 element
* called by mouse Right-click
*/
private void restoreZoomPoint(){
int iLen = alLowerX.size();
int iLast = iLen - 1; //minus 1
//^Restore only if user had zoom before(store data in ArrayList)
//^Reset to original chart at initial stage before looping zoom
if(iLen>0){
restoreAutoBounds();
for(int i=0; i<iLast; i++){
//^Value restoring
double hLower = alLowerX.get(i);
double hUpper = alUpperX.get(i);
double vLower = alLowerY.get(i);
double vUpper = alUpperY.get(i);
PlotRenderingInfo plotInfo = alPlotInfo.get(i);
Point2D selectOrigin = alSelectOrigin.get(i);
Plot p = this.chart.getPlot();
if (p instanceof Zoomable) {
Zoomable z = (Zoomable) p;
if (z.getOrientation() == PlotOrientation.HORIZONTAL) {
z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin);
z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin);
}else {
z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin);
z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin);
}
}
}
//^Removing last(most recent) element
alLowerX.remove(iLast);
alUpperX.remove(iLast);
alLowerY.remove(iLast);
alUpperY.remove(iLast);
alPlotInfo.remove(iLast);
alSelectOrigin.remove(iLast);
}
}
start: //^WZW override start
end: //^WZW override end
Code: Select all
/**
* WZW override
* Zooms in on a selected region.
*
* @param selection the selected region.
*/
public void zoom(Rectangle2D selection) {
// get the origin of the zoom selection in the Java2D space used for
// drawing the chart (that is, before any scaling to fit the panel)
Point2D selectOrigin = translateScreenToJava2D(new Point(
(int) Math.ceil(selection.getX()),
(int) Math.ceil(selection.getY())));
PlotRenderingInfo plotInfo = this.info.getPlotInfo();
Rectangle2D scaledDataArea = getScreenDataArea(
(int) selection.getCenterX(), (int) selection.getCenterY());
if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) {
double hLower = (selection.getMinX() - scaledDataArea.getMinX())
/ scaledDataArea.getWidth();
double hUpper = (selection.getMaxX() - scaledDataArea.getMinX())
/ scaledDataArea.getWidth();
double vLower = (scaledDataArea.getMaxY() - selection.getMaxY())
/ scaledDataArea.getHeight();
double vUpper = (scaledDataArea.getMaxY() - selection.getMinY())
/ scaledDataArea.getHeight();
//^WZW override start
alLowerX.add(hLower);
alUpperX.add(hUpper);
alLowerY.add(vLower);
alUpperY.add(vUpper);
alPlotInfo.add(plotInfo);
alSelectOrigin.add(selectOrigin);
//^WZW override end
Plot p = this.chart.getPlot();
if (p instanceof Zoomable) {
Zoomable z = (Zoomable) p;
if (z.getOrientation() == PlotOrientation.HORIZONTAL) {
z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin);
z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin);
}
else {
z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin);
z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin);
}
}
}
}
Right-click to restore(replace popup menu) event...
In public void mouseReleased(MouseEvent e) {: method
Code: Select all
else if (e.isPopupTrigger()) {
if (this.popup != null) {
//#?displayPopupMenu(e.getX(), e.getY());
restoreZoomPoint();
}
}
Any mistake found, or query, can post here or email me to wzwei28 (->) yahoo.com
Another simple algo for realtime dynamic crosshair(mouse XY) + tooltip
Sample demo URL: http://vnetcode.org (OMS)
Jfreechart post: http://www.jfree.org/phpBB2/viewtopic.php?p=64888