/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.geoforge.jfreechart.xyplot;
import java.awt.Color;
import java.awt.Font;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import org.geoforge.java.awt.color.GfrColor;
import org.geoforge.jfreechart.data.xy.GfrXYSeriesDepVarAbs;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
import org.geoforge.lang.util.number.GfrDouble;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.annotations.XYAnnotation;
import org.jfree.chart.annotations.XYTitleAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
/**
*
* @author robert
*/
public class GfrXYPlot extends XYPlot implements
IGfrHandlerLifeCycleObject
{
static public int INT_TOP_RIGHT = 1;
static public int INT_TOP_LEFT = 2;
static public int INT_BOTTOM_RIGHT = 3;
static public int INT_BOTTOM_LEFT = 4;
private boolean _blnIsAnnotationAlreadyAdded_ = false;//tempo, done in a hurry!!!!
public GfrXYPlot(
XYDataset dataset,
String strDomainAxis,
String strRangeAxis,
XYItemRenderer renderer,
Color col)
{
this(
dataset,
new NumberAxis(strDomainAxis),
new NumberAxis(strRangeAxis),
renderer,
col);
}
public GfrXYPlot(
XYDataset dataset,
ValueAxis domainAxis,
ValueAxis rangeAxis,
XYItemRenderer renderer)
{
this(
dataset,
domainAxis,
rangeAxis,
renderer,
GfrColor.lightGray);
}
protected GfrXYPlot(
XYDataset dataset,
ValueAxis domainAxis,
ValueAxis rangeAxis,
XYItemRenderer renderer,
Color col)
{
super(
dataset,
domainAxis,
rangeAxis,
renderer);
super.setBackgroundPaint(col);
super.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
// tempo
/**
* trying to hide duplicated or unwanted labels
* ==> not working properly!
* lines and shapes of remaining legends are then random!!!
*/
/*LegendItemCollection licOri = super.getLegendItems();
LegendItemCollection licNew = new LegendItemCollection();
for (int i=0; i<licOri.getItemCount(); i++)
{
LegendItem lim = licOri.get(i);
String strLabel = lim.getLabel();
System.out.println(">> " + i + ", strLabel=" + strLabel);
boolean blnLineVisible = lim.isLineVisible();
boolean blnShapeVisible = lim.isShapeVisible();
System.out.println(">> " + i + ", blnLineVisible=" + blnLineVisible + ", blnShapeVisible=" + blnShapeVisible);
System.out.println();
if (strLabel.compareTo("Water level") == 0)
{
lim.setLineVisible(false);
lim.setShapeVisible(false);
}
else
licNew.add(lim);
}*/
//super.setFixedLegendItems(licOri);
}
@Override
public LegendItemCollection getLegendItems()
{
LegendItemCollection licUnsorted = super.getLegendItems();
if (licUnsorted == null || licUnsorted.getItemCount() < 2)
return licUnsorted;
// ---
LegendItem[] objs = new LegendItem[licUnsorted.getItemCount()];
for (int i = 0; i < objs.length; i++)
objs[i] = licUnsorted.get(i);
Arrays.sort(objs, new MyComparator()); // ATTN possible memory leaks
LegendItemCollection licSorted = new LegendItemCollection();
for (int i = 0; i < objs.length; i++)
licSorted.add(objs[i]);
return licSorted;
}
@Override
public boolean init()
{
// !!! never called !!!
return true;
}
@Override
public void destroy()
{
List<XYAnnotation> anns = super.getAnnotations();
for (XYAnnotation annCur : anns)
{
if (annCur instanceof IGfrHandlerLifeCycleObject)
{
((IGfrHandlerLifeCycleObject) annCur).destroy();
}
}
super.clearAnnotations();
// ---
XYDataset dst = super.getDataset();
if (dst == null)
return;
if (dst instanceof IGfrHandlerLifeCycleObject)
((IGfrHandlerLifeCycleObject) dst).destroy();
XYItemRenderer irr = super.getRenderer();
if (irr instanceof IGfrHandlerLifeCycleObject)
((IGfrHandlerLifeCycleObject) irr).destroy();
}
// ---
static private class MyComparator implements Comparator<LegendItem>
{
private MyComparator()
{
}
@Override
public int compare(LegendItem obj1, LegendItem obj2)
{
Comparable cpr1 = obj1.getSeriesKey();
Comparable cpr2 = obj2.getSeriesKey();
// ---
String strLabel1 = cpr1.toString();
String strLabel2 = cpr2.toString();
if (GfrXYSeriesDepVarAbs.s_isRenable(cpr1))
strLabel1 = GfrXYSeriesDepVarAbs.s_getLabel(cpr1);
if (GfrXYSeriesDepVarAbs.s_isRenable(cpr2))
strLabel2 = GfrXYSeriesDepVarAbs.s_getLabel(cpr2);
// ---
return strLabel1.compareToIgnoreCase(strLabel2);
}
}
final protected void _addLegendAnnottaionInChart(int mode)
{
if(this._blnIsAnnotationAlreadyAdded_)
throw new UnsupportedOperationException("!!!Annotation already added!!!!");
this._blnIsAnnotationAlreadyAdded_ = true;
RectangleAnchor rec = null;
double dblX = GfrDouble.DBL_NDV;
double dblY = GfrDouble.DBL_NDV;
if (mode == INT_TOP_RIGHT)
{
rec = RectangleAnchor.TOP_RIGHT;
dblX = 0.98;
dblY = 0.98;
}
else if (mode == INT_TOP_LEFT)
{
rec = RectangleAnchor.TOP_LEFT;
dblX = 0.02;
dblY = 0.98;
}
else if (mode == INT_BOTTOM_RIGHT)
{
rec = RectangleAnchor.BOTTOM_RIGHT;
dblX = 0.98;
dblY = 0.02;
}
else if (mode == INT_BOTTOM_LEFT)
{
rec = RectangleAnchor.BOTTOM_LEFT;
dblX = 0.02;
dblY = 0.02;
}
else
{
throw new RuntimeException("Unsupported mode :" + mode);
}
LegendTitle lt = new LegendTitle((GfrXYPlot) this);
lt.setItemFont(new Font("Dialog", Font.PLAIN, 12));
lt.setBackgroundPaint(new Color(200, 200, 255, 100));
lt.setFrame(new BlockBorder(Color.white));
lt.setPosition(RectangleEdge.BOTTOM);
XYTitleAnnotation ta = new XYTitleAnnotation(dblX, dblY, lt, rec);
ta.setMaxWidth(0.48);
super.addAnnotation(ta);
}
}