Introduction · A Basic Plot Chart · Adding Formatting Information
Plotting an Inverse Chart · Changing to a Bar Chart · Inverting Bar Orientation
End-User Interaction · Proceeding From Here
The data to be graphed is displayed in the following table:
|
Q1 |
Q2 |
Q3 |
Q4 |
---|---|---|---|---|
Expenses | 150.0 | 175.0 | 160.0 | 170.0 |
Revenue | 125.0 | 100.0 | 225.0 | 300.0 |
1 package jclass.chart.examples; 2 3 import java.awt.GridLayout; 4 import jclass.chart.JCChart; 5 import jclass.chart.ChartDataView; 6 import jclass.chart.FileDataSource; 7 8 public class plot1 extends java.applet.Applet { 9 10 public void init() { 11 setLayout(new GridLayout(1,1)); 12 13 try { 14 JCChart c = new JCChart(); 15 c.getDataView(0).setDataSource(new FileDataSource("plot1.dat")); 16 add(c); 17 } 18 catch (Exception e) { 19 e.printStackTrace(System.out); 20 } 21 } 22 23 public static void main(String args[]) { 24 ExampleFrame f = new ExampleFrame("plot1"); 25 plot1 p = new plot1(); 26 p.init(); 27 f.setLayout(new GridLayout(1,1)); 28 f.add(p); 29 f.pack(); 30 f.resize(400, 300); 31 f.show(); 32 } 33 34 }
Most of the code in plot1.java should be familiar to Java programmers. The first few lines (3-6) import the classes necessary to run plot1.java. In addition to importing the standard java.awt.GridLayout class, it also imports three JClass Chart classes: jclass.chart.JCChart, jclass.chart.ChartDataView and jclass.chart.FileDataSource.
The Layout property on line 11 (derived from the layout manager GridLayout) lays out a simple grid structure to display the components it holds. A new Chart is then initiated in line 14, and the data for this program is read from the file plot1.dat through the JClass Chart component FileDataSource referenced in line 15.
The data to be plotted by the plot1.java program is shown below:
ARRAY 2 4 # X-values 1.0 2.0 3.0 4.0 # Y-values 150.0 175.0 160.0 170.0 # Y-values set 2 125.0 100.0 225.0 300.0
Any data elements that are invalid are caught by the code in lines 18-19 before they are used within the program.
A simple plot diagram is then made from the data obtained from the plot1.dat file by the code in line 23-31. They create a window for the chart, and then make the window visible.
When plot1.java is compiled and run, the window shown in the following illustration is displayed:
JClass Chart will always try to display a reasonable chart display, even if very few methods or properties have been specified. JClass Chart will use intelligent defaults for all unspecified properties.
All properties for a particular graph may be specified when the graph component is created. Properties may also be changed at any time during the execution of the program a property with its set method. A programmer may ask for the current value of any property by specifying the get method of particular property.
c.getDataView(0).setDataSource(new FileDataSource("plot2.dat")); c.getHeader().setIsShowing(true); c.getHeader().getLabel().setText("Michelle's Microchips", false); c.getFooter().setIsShowing(true); c.getFooter().getLabel().setText("1963 [COLOR=BLUE]Quarterly[DEFAULT_COLOR] Results", true); c.getLegend().setIsShowing(true);
The IsShowing method tells the program to display the text contained with the header and footers strings defined by Text ("Michelle's Microchips" and "1963 [COLOR=BLUE]Quarterly[DEFAULT_COLOR] Results"). The information contained within the square brackets ("[COLOR=BLUE]" and "[DEFAULT_COLOR]") are JCString values, which enable programmers to conveniently add or modify such things as image files, URLs or text. In this case it simply tells the program to display the text it contains ("Quarterly") in a blue font. More information on JCStrings can be found in Appendix B: JCString Properties.
c.getLegend().setIsShowing(true);
The AnnotationMethod property specifies the axis labelling method. AnnotationMethod is a property of XAxis, and XAxis is an indexed property of ChartArea. JCChartArea controls the charting region in the Chart instance, and handles rendering of the charts by using JCAxis and ChartDataView object instances to determine how data is plotted. A single JCChartArea can plot several ChartDataViews against a number of axes.
The following line of code shows how to set the AnnotationMethod:
c.getChartArea().getXAxis(0).setAnnotationMethod(JCAxis.POINT_LABELS);
The value (JCAxis.POINT_LABELS) is an enum that tells the program to display the information as a point label. A point label is a label applied to an axis at a particular data point. Typically, point labels are part of the data. There are many enums contained within the JClass Chart components, all designed to ease the task of modifying an individual component's display characteristics and behavior.
When these elements are added to a program, the following code listing (plot2.java) is the result:
package jclass.chart.examples; import java.awt.GridLayout; import jclass.chart.JCChart; import jclass.chart.ChartDataView; import jclass.chart.FileDataSource; import jclass.chart.JCAxis; public class plot2 extends java.applet.Applet { public void init() { setLayout(new GridLayout(1,1)); try { JCChart c = new JCChart(); c.getDataView(0).setDataSource(new FileDataSource("plot2.dat")); c.getHeader().setIsShowing(true); c.getHeader().getLabel().setText("Michelle's Microchips", false); c.getFooter().setIsShowing(true); c.getFooter().getLabel().setText("1963 [COLOR=BLUE]Quarterly[DEFAULT_COLOR] Results", true); c.getLegend().setIsShowing(true); c.getChartArea().getXAxis(0).setAnnotationMethod(JCAxis.POINT_LABELS); add(c); } catch (Exception e) { e.printStackTrace(System.out); } } public static void main(String args[]) { ExampleFrame f = new ExampleFrame("plot2"); plot2 p = new plot2(); p.init(); f.setLayout(new GridLayout(1,1)); f.add(p); f.pack(); f.resize(400, 300); f.show(); } }
This program can be fed information as a external data file (plot2.dat), which data can take the following form:
ARRAY '' 2 4 'Q1' 'Q2' 'Q3' 'Q4' '' 1.0 2.0 3.0 4.0 'Expenses' 150.0 175.0 160.0 170.0 'Revenue' 125.0 100.0 225.0 300.0
Note that the contents of this file are different that the contents of plot1.dat. It contains point label information ("Q1", "Q2", etc.), and it also contains data series labels ("Expenses", "Revenue", etc.).
When compiled and run, the results of plot2.java are displayed, as in the following illustration:
The IsInverted property is used to switch the values of the data displayed by plot2.java. All that is necessary is to insert a single line of code into the listing for plot2.java, which reads as follows:
c.getDataView(0).setIsInverted(true);
The DataView property is an indexed property that contains all the data to be displayed in Chart.
When this line of code is added to the plot2.java program and compiled the following is displayed:
The full code for this program (plot2inv.java) can be found in with the example files included with JClass Chart in the jclass/chart/examples/ directory.
The following line has been added to the plot2.java to have the data displayed as a bar chart instead:
JCChart c = new JCChart(JCChart.BAR);
When bar2.java is compiled and run, the program in the following illustration is displayed.
The full code for this program (bar2.java) can be found in with the example files included with JClass Chart.
In addition to the plot and bar charts shown here, there are three other chart types which can be specified using JClass Chart components: stacking bar, scatter plot and pie chart types. The methods and properties necessary to create and modify the properties of all chart types can be found in the JClass Chart API.
In a plot, inverting causes the Y-values to be plotted against the horizontal axis, and the X-values to be plotted against the vertical. In a bar chart, it causes the bars to be displayed horizontally instead of vertically.
To invert, set the IsInverted method to true. By default, it is false.
The following illustration shows the effects of IsInverted set to false as incorporated within the bar2.java program:
As you can see, the same method used to invert the plot values originally displayed in plot2inv.java also apply here. This sharing of common methods makes it easy to program different types of chart types easily and quickly.
The full code for this program (bar2inv.java) can be found in with the example files included with JClass Chart.
1 Although there are interdependencies between some properties, most properties are completely orthogonal.