Scripting with Chart
To display custom colors for charts:
Applicable for : Area, Bar, Column, Pie
Code Snippet
importClass(Packages.com.elixirtech.chart2.custom.CustomDrawingSupplier);
//Diagram of Named Colours available in
//Elixir Report Designer User Manual, Figure 4.2. Name Colours
paints = ["DarkSeaGreen","Gold","LightCoral","CornflowerBlue","GreenYellow"];
cds = new CustomDrawingSupplier();
cds.setPaintNames(paints);
plot.drawingSupplier = cds;
To change the legend's font:
Code Snippet
getLegend().setItemFont(new java.awt.Font("Arial",0,20));
To place the y-axis on the right:
Code Snippet
plot.setRangeAxisLocation(Packages.org.jfree.chart.axis.AxisLocation.TOP_OR_RIGHT);
To put a background image on a chart you can use the following script:
Applicable for : Area, Bar, Column, Line, Meter, Pie, Polar, Stocks, Waterfall, XY
See
alignment options
Code Snippet
url = new java.net.URL("repository:/ElixirSamples/Resources/images/LogoElixir.gif");
im = Packages.javax.imageio.ImageIO.read(url);
plot.backgroundImage = im;
plot.setBackgroundImageAlignment(Packages.org.jfree.ui.Align.TOP_LEFT);
// Instead of plot area only, the image can be shown in the entire chart area with:
backgroundImage = im;
setBackgroundImageAlignment(Packages.org.jfree.ui.Align.TOP_LEFT);
To show colors of chart based on the values of the field:
Applicable for : Area, Bar, Column, Line, Pie
Code Snippet
//Importing the Chart class for usage
importClass(Packages.com.elixirtech.chart2.custom.CustomDrawingSupplier);
var ds = plot.getDataset();
var rows = ds.getRowCount();
var paints = Array();
for (i=0;i<rows;i++)
{
//ds.getValue() is the values as defined in 'Value' Tab
var value = ds.getValue(i,0);
if (value>500) paints[i] = "Red";
else paints[i] = "Gold";
}
cds = new CustomDrawingSupplier();
cds.setPaintNames(paints);
plot.drawingSupplier = cds;
To add markers for the Line Chart:
Code Snippet
importPackage(Packages.com.elixirtech.chart2.plot);
var lineandshaperenderer = plot.getRenderer();
lineandshaperenderer.setShapesVisible(true);
To display patterns instead of color for charts:
Applicable for : Area, Bar, Column, Pie
(Known issue for version 6.1, please do not use with version 6.1)
Code Snippet
importClass(Packages.com.elixirtech.chart2.custom.CustomDrawingSupplier);
paints = ["Diag","Horiz","Wave","RDiag"];
cds = new CustomDrawingSupplier();
cds.setPaintNames(paints);
plot.drawingSupplier = cds;
To remove the labels from the Pie Chart, you can use the following script:
Code Snippet
plot.setLabelGenerator(null);
To remove the individual legend from the Multiple Pie Chart, you can use the following script:
Code Snippet
var chart = plot.getPieChart();
chart.removeLegend();
To include value in Label for Pie Chart, you can include the field name in 'Key' tab:
Code Snippet
Fruit + " - " + _2000
//where Fruit and 2000 is the Field Name
To include the percentage value on the label for Multiple Pie Chart, you can include the following in 'Script' tab:
Code Snippet
importClass(Packages.org.jfree.chart.labels.StandardPieSectionLabelGenerator);
var chart = plot.getPieChart();
var p = chart.plot;
p.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {1} ({2})"));
To include the percentage value on the label for Pie Chart, you can include the following in 'Script' tab:
Code Snippet
importClass(Packages.org.jfree.chart.labels.StandardPieSectionLabelGenerator);
var gen = new StandardPieSectionLabelGenerator("{0} = {1} ({2})");
plot.setLabelGenerator(gen);
To include the percentage value on the label for Pie Chart with decimal format, you can include the following in 'Script' tab:
Code Snippet
importClass(java.text.DecimalFormat);
importClass(Packages.org.jfree.chart.labels.StandardPieSectionLabelGenerator);
var gen = new StandardPieSectionLabelGenerator("{0} = {1} ({2})", new DecimalFormat("0.00"), new DecimalFormat("0.00%"));
plot.setLabelGenerator(gen);
To change the font properties for Pie Chart:
Code Snippet
plot.labelFont = new java.awt.Font("SansSerif",0,18);
To change the font properties for Meter Chart:
Code Snippet
//Font for the Tick Value
plot.setTickLabelFont(new java.awt.Font("SansSerif",0,14));
//Font for the Meter Chart
plot.setValueFont(new java.awt.Font("SansSerif",0,20));
To change the font properties for XY Chart:
Code Snippet
importClass(Packages.com.elixirtech.ui.StandardColor);
var lineandshaperenderer = plot.getRenderer();
lineandshaperenderer.setItemLabelFont(new java.awt.Font("SansSerif",0,10));
lineandshaperenderer.setItemLabelPaint(StandardColor.lookup("Red"));
lineandshaperenderer.setItemLabelsVisible(true);
XY Chart, to display month abbreviation instead of integer on the x-axis:
(Work with Elixir Repertoire 6.2 and above)
Code Snippet
importPackage(Packages.com.elixirtech.chart2.custom);
var oldtu = plot.domainAxis.getTickUnit();
var newtu = TickUnitFactory.getShortMonths(true);
var tu = new CustomNumberTickUnit(oldtu,newtu);
plot.domainAxis.setTickUnit(tu);
To add markers for the XY Chart:
Code Snippet
importPackage(Packages.org.jfree.chart.renderer.xy);
//new XYLineAndShapeRenderer(boolean line, boolean shape)
plot.setRenderer(new XYLineAndShapeRenderer(true, true));
To change the text orientation for chart:
Applicable for : Area, Bar, Column, Line, Waterfall
Code Snippet
importClass(Packages.org.jfree.chart.axis.CategoryLabelPositions);
domainAxis = plot.getDomainAxis();
/**
* Math.PI / 1 = 180 degree
* Math.PI / 2 = 90 degree
* Math.PI / 4 = 45 degree
* Math.PI * 2 = 360 degree (Default horizontal layout)
*/
mydegree=4;
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI /mydegree)
);
Sample
For more information on the available method for Charts, please refer to the API available.
Reference : -
JFreeChart
--
ShihHor? - 31 May 2007