Copyright 2005, J4L
Components (http://www.java4less.com)
Go bak to contents
We recommend you to create charts using the parameters provided by RChart Visual Builder, however if you need it you can create charts using the Java API (see the Java documentation in the javadoc subdirectory of your installation).
The process is:
1. Import RChart package | import com.java4less.rchart.*; import com.java4less.rchart.gc.*; |
2. Create the title | Title title=new Title("Sales (thousands $)") |
3. Create the axis you need (depends on the chart, for example piecharts do not have axis) | com.java4less.rchart.Axis XAxis=new Axis(Axis.HORIZONTAL,new Scale()); com.java4less.rchart.Axis YAxis=new Axis(Axis.VERTICAL,new Scale()); XAxis.scale.min=0; YAxis.scale.min=0; ..... |
4. Create the legend | Legend l=new Legend(); l.addItem("Products",new FillStyle( GraphicsProvider.getColor(ChartColor.BLUE)) )); l.addItem("Services",new FillStyle( GraphicsProvider.getColor(ChartColor.GREEN)) )); |
5. Create the axis labels |
XLabel= new HAxisLabel("", GraphicsProvider.getColor(ChartColor.BLUE)), GraphicsProvider.getFont("Arial",ChartFont.BOLD,14)); YLabel= new VAxisLabel("Brutto", GraphicsProvider.getColor(ChartColor.BLACK)),new GraphicsProvider.getFont("Arial",ChartFont.BOLD,14)); |
6. create the plotter (or plotters if you combine lines and bars) | LinePlotter3D plot=new LinePlotter3D(); |
7. create the chart and set properties (labels,legend) | com.java4less.rchart.Chart chart=new Chart(title,plot,XAxis,YAxis); chart.XLabel=XLabel; chart.YLabel=YLabel; chart.legend=l; |
8. create the data series | double[] d1={1,1,3,3.5,5,4,2}; LineDataSerie data1= new LineDataSerie(d1,new LineStyle(0.2f, GraphicsProvider.getColor(ChartColor.BLUE)) ,LineStyle.LINE_NORMAL)); |
9. add serie to chart | chart.addSerie(data1); |
You can use the chart loader to create the charts using the parameters created by RChart Visual Builder in this way:
import com.java4less.rchart.*;
public class test{
public test() {
}
public static void main(String[] args) {
try {ChartLoader loader=new ChartLoader();
// Set parameters
loader.setParameter("TITLECHART","Sales 1999");
loader.setParameter("LEGEND_FILL","WHITE");
loader.setParameter("LEGEND_VERTICAL","FALSE");
loader.setParameter("LEGEND_BORDER","0.2|0x0|NORMAL");
loader.setParameter("SERIE_1","Pie");
loader.setParameter("SERIE_TYPE_1","PIE");
loader.setParameter("SERIE_FONT_1","ARIAL|BOLD|12");
loader.setParameter("SERIE_DATA_1","94|48|28");
loader.setParameter("PIE_NAME_1","Products");
loader.setParameter("PIE_NAME_2","Services");
loader.setParameter("PIE_NAME_3","Other");
loader.setParameter("PIE_STYLE_1","RED");
loader.setParameter("PIE_STYLE_2","BLUE");
loader.setParameter("PIE_STYLE_3","GREEN");
loader.setParameter("PIECHART_3D","true");
loader.setParameter("PIE_LABEL_FORMAT","#VALUE# (#PERCENTAGE#)");
loader.setParameter("SERIE_LABELS_1","Products|Services|Other");
loader.setParameter("SERIE_TOGETHER_1","true|false|true");
loader.setParameter("LEGEND_POSITION","TOP");
loader.setParameter("LEGEND_MARGIN","0.3");
loader.setParameter("CHART_BORDER","0.2|0x0|NORMAL");
loader.setParameter("CHART_FILL","0x99cccc");/* process parameters and create chart*/
Chart chart=loader.buildChart(false,false);
chart.setSize(300,300);
/* create png file */
chart.saveToFile("chart.png","PNG");
}catch(Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
You can also read the parameters directly from a file:
import com.java4less.rchart.*;
public class test{
public test() {
}
public static void main(String[] args) {
try {ChartLoader loader=new ChartLoader();
// Set parameters
loader.setDataFile("examples\\piechart3D.txt"); // this can also be a url like http://www.myserver.com or file:///..../* process parameters and create chart*/
Chart chart=loader.buildChart(false,true); // true means read data file
chart.setSize(300,300);/* create png file */
chart.saveToFile("chart.png","PNG");}
catch(Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
In all cases the steps are:
}
}
public class SimpleAwtExample
extends Frame {
ChartViewer chartViewer= new ChartViewer();
public static void main(String[] args) {
SimpleAwtExample demo=new SimpleAwtExample();
demo.show();
}
public SimpleAwtExample() {
this.setTitle("RChart for AWT example");
this.setResizable(false);
this.setLayout(new java.awt.BorderLayout());
this.setSize(new Dimension(500,500));
this.add("Center", chartViewer);
this.doLayout();
// create chart
ChartLoader loader=new ChartLoader();
loader.loadFromFile("examples/lineChart.txt",true);
chartViewer.setChart(loader.build(false,false));
}
}
RChart can create the following image files: JPG, PNG, BMP and GIF. This can be done by executing chart.saveToFile(filename,format), where format can be "GIF", "PNG", "BMP" of "JPG".
AWT
If you are using AWT or Swing you can use the GIF, JPG and PNG format. The
JPG format is supported by Sun's java virtual machine 1.2 or later. The other
two formats require you to install a third party package:
You must download one of
those packages and include them in your classpath.
You can also use other encoders, In that case the source code would look like
this:
// create image
java.awt.image.BufferedImage image = new java.awt.image.BufferedImage( chart.getWidth(),chart.getHeight(),java.awt.image.BufferedImage.TYPE_INT_RGB );
java.awt.Graphics imgGraphics = image.createGraphics();// paint chart on image
ChartGraphics chartGraphics=GraphicsProvider.getGraphics(imgGraphics)
chart.paint( chartGraphics );
chartGraphics.dispose();// open file
java.io.File f=new java.io.File(filename);
f.delete();
java.io.FileOutputStream outb=new java.io.FileOutputStream(f);// encode image using your encoder
com.company.Enconder encoder= new com.company.Enconder(image ,outb); // encode the image to the output stream
encoder.encodeImage();
SWT
If you are using SWT, you can use the JPG, GIF and BMP formats. No external packages are required.
There is a way to force RChart to rebuild itself every X seconds. In this way you can create charts that will get updated with realtime data. For example you can let RChart read new data from a given URL or file every 5 seconds. Look at the same application provided with the product to see a complete working example.
The following example shows how to let RChart be updated every 5 seconds and load new data from a given URL.
chartLoader.loadFromFile("file:///c:\\examples\\areaChart.txt",false);
Chart chart=cloader.build(false,false);
ChartViewer chartViewer=new ChartViewer(shell,SWT.NONE);
chartViewer.setBounds(0,0,400,400);
chartViewer.setChart(chart);
shell.open();// set up realtime update
chart.msecs=5000; // update every 5 seconds
chart.reloadFrom="http://www.myserver.com/newdata.html"; // get new data from this url
chart.startUpdater(); // start update thread
while(!shell.isDisposed())
if(!display.readAndDispatch())
display.sleep();
display.dispose();
chart.dispose();
Each time the chart gets updated by the updater thread, RChart will trigger 2 events which can be catched by ChartListeners. These events are:
There are 2 ways for updating the values in your chart (normally inside the EVENT_BEFORE_UPDATE event handler):
// rebuild chart
// this is not required inside the EVENT_BEFORE_UPDATE event handler
myChart.loader.build(myChart,false,false); // since we pass the chart as
first parameter, the loader will not create a new Chart object but it will
rebuild the existing one
Note: if you are setting the new parameters inside the EVENT_BEFORE_UPDATE
event, you should not rebuild the chart yourself, instead you must set:
myChart.autoRebuild=true;
and the chart will be rebuilt automatically.
If you want to compile from the command line you must do it like this: