--
PedroRio - 26 Mar 2010
Java code Associated to a PieChart
The grafic XVW components have properties that are fed by a Bean and some properties represent method calls on the Bean side, which have to return Java objects that implement determined inerfaces. In the
PieChart case it is needed at least that the property
dataSet is filled, and optionaly the property
configOptions.
Assuming a XML definition of the Pie Chart component as the following:
<xvw:pieChart
label="Chart Label"
dataSet="#{viewBean.dataSet}"
width="500"
height="400"
type="IMG"
configOptions="#{viewBean.configOptions}"
>
</xvw:pieChart>
The Bean to use for a pieChart may be something like this (may extend any XEO Bean, it is not required to extend the
XEOEditBean):
public class PieBean extends WebXEO.XEOEditBean
{
private PieDataSet dataSet;
private IPieChartConfiguration configOptions;
public PieBean()
{
super();
this.dataSet = new PieDataSetImpl();
this.configOptions = new IPieChartConfigImpl();
}
public PieDataSet dataSet()
{
return this.dataSet;
}
public IPieChartConfiguration configOptions()
{
return this.configOptions;
}
}
The class
PieDataSetImpl is a implementation of the interface
PieDataSet for this example, so as the class
IPieChartConfigImpl is a implementation of the interface
IPieChartConfiguration.
The class
PieDataSetImpl definition is very simple once a
PieDataSet is essentialy a set of key/value pairs. The values may be saved in a
HashMap<String,Integer> and for example purposes we'll have a preference (in numbers) of people between seasons (Summer, Fall and Winter), where, respectively are 190, 200 and 20 people prefering.
public class PieDataSetImpl implements PieDataSet {
private HashMap<String,Integer> dataSet;
public PieDataSetImpl() //Create the Map and fill with values
{
this.dataSet = new HashMap<String, Integer>();
this.dataSet.put("Summer", 190);
this.dataSet.put("Fall", 200);
this.dataSet.put("Winter", 20);
}
@Override //Returns a ame to identify the categories
public String getCategoryLabel()
{
return "Seasons";
}
@Override
public Number getValue(Comparable<String> key) //Gets a value from the category
{
return this.dataSet.get(key);
}
@Override
public String getValueLabel() //Returns a name to identify the values for categories
{
return "Values";
}
@Override
public List<String> getCategories() //Returns the list of categories, Summer, Fall, Winter
{
return new Vector<String>(dataSet.keySet());
}
}
The
IPieChartConfigImpl class is a very simple implementation of the configuration interface only to show the using of the same.
public class IPieChartConfigImpl implements IPieChartConfiguration
{
public IPieChartConfigImpl()
{}
@Override //Defines the grafic background colour.
public Color getBackgroundColour()
{
return Color.gray;
}
@Override //Maps the color categories, so each slice has it's color
public HashMap<String, Color> getColours()
{
HashMap<String, Color> maps = new HashMap<String, Color>();
maps.put("Winter", Color.black);
maps.put("Fall", Color.blue);
maps.put("Summer", Color.magenta);
return maps;
}
@Override //If the grafic title should be shown or not
public boolean showChartTitle()
{
return true;
}
@Override //If the grafic labels are shown or not.
public boolean showLabels()
{
return true;
}
@Override //returns a expression which can be used to show the tooltips, may contain special variables
//in a way to show values(consult interface documentation)
public String getTooltipString()
{
return "$key has value $val which is $percent of total";
}
}