Copyright 2005, J4L
Components (http://www.java4less.com)
Go bak to contents
We recommend you to create charts using the parameters however if you need it you can create charts using the api (see documentation in the api subdirectory of your installation).
The process is:
1. Import RChart assembly |
[C#] |
2. Create the title | [C#] Title title=new Title("Sales (thousands $)"); [VB.NET] dim mytitle as Title = new Title("Sales (thousands $)") |
3. Create the axis you need (depends on the chart) |
[C#] |
4. Create the legend | [C#] Legend l=new Legend(); l.addItem("Products",new FillStyle(Color.Blue)); l.addItem("Services",new FillStyle(Color.Green)); [VB.NET] dim l as Legend=new Legend() l.addItem("Products",new FillStyle(Color.Blue)) l.addItem("Services",new FillStyle(Color.Green)) |
5. Create the axis labels |
[C#] YLabel= new VAxisLabel("Brutto",Color.Black,new Font(new FontFamily("Arial"),14); dim YLabel as VAxisLabel= new VAxisLabel("Brutto",Color.Black,new Font(new FontFamily("Arial"),14))
|
6. create the plotter (or plotters if you combine lines and bars) | [C#] LinePlotter3D plot=new LinePlotter3D(); [VB.NET] dim plot as LinePlotter3D=new LinePlotter3D() |
7. create the chart and set properties (labels,legend) | [C#] Chart chart=new Chart(title,plot,XAxis,YAxis); chart.XLabel=XLabel; chart.YLabel=YLabel; chart.legend=l; [VB.NET] dim mychart as Chart=new Chart(mytitle,plot,XAxis,YAxis) mychart.XLabel=XLabel mychart.YLabel=YLabel mychart.legend=l |
8. create the data series | [C#] RValue[] d1={RValue(1),RValue(1),RValue(3),RValue(3.5),RValue(5),RValue(4),RValue(2)}; LineDataSerie data1= new LineDataSerie(d1,new LineStyle(1,Color.Blue,LineStyle.LINE_NORMAL)); [VB.NET] dim d1 as RValue()={RValue(1),RValue(1),RValue(3),RValue(3.5),RValue(5),RValue(4),RValue(2)} dim data1 as LineDataSerie= new LineDataSerie(d1,new LineStyle(1,Color.Blue,LineStyle.LINE_NORMAL)) |
9. add serie to chart | [C#] chart.addSerie(data1); [VB.NET] mychart.addSerie(data1) |
You can use the ChartLoader to create your charts using the parameters:
[c#]
// define variable
ChartLoader chartLoader= new ChartLoader();
// set chart parameters
chartLoader.setParameter("TITLECHART","Sales 2002");
chartLoader.setParameter("XLABEL","Month");
chartLoader.setParameter("YLABEL","Million $");
chartLoader.setParameter("XSCALE_MIN","0");
chartLoader.setParameter("XSCALE_MAX","5.5");
chartLoader.setParameter("YSCALE_MIN","-15");
chartLoader.setParameter("BIG_TICK_INTERVALX","1");
chartLoader.setParameter("BIG_TICK_INTERVALY","10"); chartLoader.setParameter("XAXIS_LABELS","June|July|Aug.|Sept.|Oct.|Nov.|Dec."); chartLoader.setParameter("CERO_XAXIS","LINE");
chartLoader.setParameter("YAXIS_INTEGER","TRUE");
chartLoader.setParameter("SERIE_1","Products");
chartLoader.setParameter("SERIE_2","Services");
chartLoader.setParameter("SERIE_TYPE_1","BAR");
chartLoader.setParameter("SERIE_TYPE_2","BAR");
chartLoader.setParameter("SERIE_FONT_1","Arial|PLAIN|8");
chartLoader.setParameter("SERIE_FONT_2","Arial|PLAIN|8");
chartLoader.setParameter("SERIE_DATA_1","12|43|50|45|30");
chartLoader.setParameter("SERIE_DATA_2","-10|41|48|39|36");
chartLoader.setParameter("SERIE_BORDER_TYPE_1","RAISED"); chartLoader.setParameter("SERIE_BORDER_TYPE_2","RAISED");
chartLoader.setParameter("SERIE_BAR_STYLE_1","ff00");
chartLoader.setParameter("SERIE_BAR_STYLE_2","ff");
chartLoader.setParameter("BARCHART_BARSPACE","1");
chartLoader.setParameter("LEFT_MARGIN","0.15");
chartLoader.setParameter("CHART_FILL","ffcc00");
chartLoader.setParameter("SERIE_NEGATIVE_STYLE_2","RED");
chartLoader.setParameter("YLABEL_VERTICAL","TRUE");
// create chart
Chart chart=chartLoader.buildChart();[VB.NET]
' define variable
dim chartLoader as ChartLoader = new ChartLoader()
' set chart parameters
chartLoader.setParameter("TITLECHART","Sales 2002")
chartLoader.setParameter("XLABEL","Month")
chartLoader.setParameter("YLABEL","Million $")
chartLoader.setParameter("XSCALE_MIN","0")
chartLoader.setParameter("XSCALE_MAX","5.5")
chartLoader.setParameter("YSCALE_MIN","-15")
chartLoader.setParameter("BIG_TICK_INTERVALX","1")
chartLoader.setParameter("BIG_TICK_INTERVALY","10") chartLoader.setParameter("XAXIS_LABELS","June|July|Aug.|Sept.|Oct.|Nov.|Dec.")
chartLoader.setParameter("CERO_XAXIS","LINE")
chartLoader.setParameter("YAXIS_INTEGER","TRUE")
chartLoader.setParameter("SERIE_1","Products")
chartLoader.setParameter("SERIE_2","Services")
chartLoader.setParameter("SERIE_TYPE_1","BAR")
chartLoader.setParameter("SERIE_TYPE_2","BAR")
chartLoader.setParameter("SERIE_FONT_1","Arial|PLAIN|8")
chartLoader.setParameter("SERIE_FONT_2","Arial|PLAIN|8")
chartLoader.setParameter("SERIE_DATA_1","12|43|50|45|30")
chartLoader.setParameter("SERIE_DATA_2","-10|41|48|39|36")
chartLoader.setParameter("SERIE_BORDER_TYPE_1","RAISED") chartLoader.setParameter("SERIE_BORDER_TYPE_2","RAISED")
chartLoader.setParameter("SERIE_BAR_STYLE_1","ff00")
chartLoader.setParameter("SERIE_BAR_STYLE_2","ff")
chartLoader.setParameter("BARCHART_BARSPACE","1")
chartLoader.setParameter("LEFT_MARGIN","0.15")
chartLoader.setParameter("CHART_FILL","ffcc00")
chartLoader.setParameter("SERIE_NEGATIVE_STYLE_2","RED")
chartLoader.setParameter("YLABEL_VERTICAL","TRUE")
' create chart
dim mychart as Chart mychart=chartLoader.buildChart()
You can also read the parameters directly from a file:
[c#]
// define variable
ChartLoader chartLoader= new ChartLoader();
// set chart parameters
chartLoader.loadFromFile("params.txt",true);
// create chart
Chart chart=chartLoader.buildChart();
In all cases the steps are:
RChart can create the following image files: JPG, PNG and GIF. This can be done by executing chart.saveToFile(filename,format), where format can be "JPEG", "PNG" of "GIF".
[c#]
// define variable
ChartLoader chartLoader= new ChartLoader();
// set chart parameters
chartLoader.loadFromFile("params.txt",true);
// create chart
Chart chart=chartLoader.buildChart();
chart.saveToFile("chart.gif","GIF");
If you want to receive an event when the user click on one of the bars or points you must catch the Chart.EVENT_POINT_CLICKED event in the ChartListener interface.
The EVENT_POINT_CLICKED allows you to implement actions when the user clicks on a part of the chart. This can for example be used for implementing drill down charts (see our example, ChartViewer.cs). The first parameter of the chartEvent() method is the chart the event is triggered for. The following chart properties are normally used in the event handler:
You must set the listener like this:
chart.addChartListener(yourListener);
Instead of implementing your own listener you can extend the ChartAdapter class and overrride the methods you need. For example:
[c#]
class MyChartAdapter:ChartAdapter {
public MyChartAdapter() { }
public override void chartEvent(Chart c,int type) {if (type==Chart.EVENT_POINT_CLICKED)
MessageBox.Show("You just clicked on series "+ c.selectedSerie.name.ToUpper() + " point "+ (c.selectedSeriePoint+1));}
}
The sample application ChartViewer.cs includes a realtime chart example which will be updated every 2 seconds. The following section explains the how to create such charts. The code below will enable the automatic chart update:
// 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
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.
In order to recompile RChart you can use the following command:
csc.exe /unsafe /reference:system.dll /reference:system.data.dll /reference:System.Drawing.dll /reference:System.Windows.Forms.dll /target:library /out:rchart11.dll /nologo /debug- ODBCSource.cs Axis.cs AxisLabel.cs AssemblyInfo.cs ChartMapWebControl.cs BarDataSerie.cs BarPlotter3D.cs Chart.cs ChartComponent.cs ChartListener.cs ChartLoader.cs CurvePlotter.cs Legend.cs LineDataSerie.cs LinePlotter3D.cs LinePlotter.cs LogScale.cs MaxMinDataSerie.cs PieDataSerie.cs Plotter.cs RadarPlotter.cs RMatrix.cs Scale.cs Title.cs Vector.cs FillStyle.cs DataSerie.cs LineStyle.cs BarPlotter.cs PiePlotter.cs HAxisLabel.cs VAxisLabel.cs Polygon.cs Util.cs RValue.cs ChartControl.cs ChartWebControl.cs EventListener.cs AxisTargetZone.cs TargetZone.cs IFloatingObject.cs IConstants.cs TagObject.cs TagParser.cs ChartLabel.cs ChartAdapter.cs
If you just want to recompile the example application:
csc.exe /unsafe /reference:system.dll /reference:system.data.dll /reference:System.Drawing.dll /reference:System.Windows.Forms.dll /reference:rchart11.dll /out:chartviewer11.exe /nologo /debug- Main.cs ChartViewer.cs