The Column Chart Applet example.
This example produces a chart of fictitious Sales of three products for the 4 quarters of the year. This example demonstrates many of the features of the Column Chart Bean including, multiple series, automatic legend generation, bespoke chart notes, the 3D effects and bespoke images.
The full source code for this example can be found in the ColumnechartApplet directory,
./ColumnchartApplet/ColumnchartApplet.java
To see the applet working simply open the file ./ColumnchartApplet/columnchart.htm in a browser or the appletviewer.
You should now see the following,
Step 1 - Create the Basic Applet Framework.
The first thing to do is to create a basic applet framework which imports all the necessary classes and create a Window and Frame.
import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { public ColumnchartApplet() { } public void init() { setLayout(null); setBackground(Color.white); } //---------------------------------------------------------------------------------- } // End Applet class
Step 2 - Add in and initialise the Columnchart Bean. In this step we declare the Columnchart object ( columnchart ) and create and instance of the Columnchart Bean. Following this the columnchart canvas is resized and then added to the applet frame. import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); setLayout(null); setBackground(Color.white); add(columnchart); } //---------------------------------------------------------------------------------- } // End Applet class Step 3 - Create the Text data object and set this property of the Column Chart Bean. Pay particular attention to the set_textdata method. This is where the three elements of text are created and added to the text data object. Finally in this method the Textdata property of the Columnchart bean is set with the column, columnchart.setTextdata(textdata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //---------------------------------------------------------------------------------- } // End Applet class Step 4 - Create the Title data object and set this property of the Column Chart Bean. Pay particular attention to the set_titledata method. This is where the three elements representing the titles are created and added to the title data object. Finally in this method the Titledata property of the Columnchart bean is set with the column, columnchart.setTitledata(textdata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //---------------------------------------------------------------------------------- } // End Applet class Step 5 - Create the X axis Labels data object and set this property of the Column Chart Bean. Pay particular attention to the set_xlabelsdata method. Here the twelve labels representing the months of the year are created. Finally in this method the Xlabelsdata property of the Columnchart bean is set with the column, columnchart.setXlabelsdata(xlabelsdata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) private Vector xlabelsdata; // X labels Objects public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); set_xlabelsdata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //----------------------------------------------------------------------------- // Create the xlabels object // private void set_xlabelsdata() { xlabelsdata = new Vector(); xlabelsdata.addElement(new Xlabelsdata("Quarter 1",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 2",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 3",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 4",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); columnchart.setXlabelsdata(xlabelsdata); } //---------------------------------------------------------------------------------- } // End Applet class Step 6 - Create the Y axis Labels data object and set this property of the Column Chart Bean. Pay particular attention to the set_ylabelsdata method. Only one element is added to this object and the bean will only ever use the first element. Finally in this method the Ylabelsdata property of the Columnchart bean is set with the column, columnchart.setYlabelsdata(ylabelsdata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) private Vector xlabelsdata; // X labels Objects private Vector ylabelsdata; // Y labels Objects public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); set_xlabelsdata(); set_ylabelsdata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //----------------------------------------------------------------------------- // Create the xlabels object // private void set_xlabelsdata() { xlabelsdata = new Vector(); xlabelsdata.addElement(new Xlabelsdata("Quarter 1",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 2",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 3",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 4",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); columnchart.setXlabelsdata(xlabelsdata); } //----------------------------------------------------------------------------- // Create the ylabels data object // private void set_ylabelsdata() { ylabelsdata = new Vector(); ylabelsdata.addElement(new Ylabelsdata(new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10))); columnchart.setYlabelsdata(ylabelsdata); } //---------------------------------------------------------------------------------- } // End Applet class Step 7 - Create the Column and Series data objects and set this property of the Column Chart Bean. Here we create the Column and Series data objects, notice how the Column data object becomes part of the series data object. Finally in this method the Seriesdata property of the Columnchart bean is set with the column, columnchart.setSeriesdata(seriesdata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) private Vector xlabelsdata; // X labels Objects private Vector ylabelsdata; // Y labels Objects private Vector columndata; // Column data Objects private Vector seriesdata; // Series data Objects public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); set_xlabelsdata(); set_ylabelsdata(); set_seriescolumndata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //----------------------------------------------------------------------------- // Create the xlabels object // private void set_xlabelsdata() { xlabelsdata = new Vector(); xlabelsdata.addElement(new Xlabelsdata("Quarter 1",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 2",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 3",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 4",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); columnchart.setXlabelsdata(xlabelsdata); } //----------------------------------------------------------------------------- // Create the ylabels data object // private void set_ylabelsdata() { ylabelsdata = new Vector(); ylabelsdata.addElement(new Ylabelsdata(new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10))); columnchart.setYlabelsdata(ylabelsdata); } //----------------------------------------------------------------------------- // Create the series and column objects // private void set_seriescolumndata() { seriesdata = new Vector(); // Series 1 columndata = new Vector(); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7900, false)); columndata.addElement(new Columndata(10350, false)); columndata.addElement(new Columndata(13800, false)); seriesdata.addElement(new Seriesdata("Product X", new Color(200,0,0), columndata)); // Series 2 columndata = new Vector(); columndata.addElement(new Columndata(6300, false)); columndata.addElement(new Columndata(6400, false)); columndata.addElement(new Columndata(8700, false)); columndata.addElement(new Columndata(9200, false)); seriesdata.addElement(new Seriesdata("Product Y", new Color(200,0,200), columndata)); // Series 3 columndata = new Vector(); columndata.addElement(new Columndata(6000, false)); columndata.addElement(new Columndata(6200, false)); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7750, false)); seriesdata.addElement(new Seriesdata("Product Z", new Color(0,200,0), columndata)); columnchart.setSeriesdata(seriesdata); } //---------------------------------------------------------------------------------- } // End Applet class Step 8 - Create the Legend data object and set this property of the Column Chart Bean. Here we create the Legend data object with the method set_legenddata(). Finally in this method the Seriesdata property of the Columnchart bean is set with the column, columnchart.setLegenddata(legenddata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) private Vector xlabelsdata; // X labels Objects private Vector ylabelsdata; // Y labels Objects private Vector columndata; // Column data Objects private Vector seriesdata; // Series data Objects private Vector legenddata; // Legend Properties public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); set_xlabelsdata(); set_ylabelsdata(); set_seriescolumndata(); set_legenddata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //----------------------------------------------------------------------------- // Create the xlabels object // private void set_xlabelsdata() { xlabelsdata = new Vector(); xlabelsdata.addElement(new Xlabelsdata("Quarter 1",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 2",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 3",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 4",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); columnchart.setXlabelsdata(xlabelsdata); } //----------------------------------------------------------------------------- // Create the ylabels data object // private void set_ylabelsdata() { ylabelsdata = new Vector(); ylabelsdata.addElement(new Ylabelsdata(new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10))); columnchart.setYlabelsdata(ylabelsdata); } //----------------------------------------------------------------------------- // Create the series and column objects // private void set_seriescolumndata() { seriesdata = new Vector(); // Series 1 columndata = new Vector(); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7900, false)); columndata.addElement(new Columndata(10350, false)); columndata.addElement(new Columndata(13800, false)); seriesdata.addElement(new Seriesdata("Product X", new Color(200,0,0), columndata)); // Series 2 columndata = new Vector(); columndata.addElement(new Columndata(6300, false)); columndata.addElement(new Columndata(6400, false)); columndata.addElement(new Columndata(8700, false)); columndata.addElement(new Columndata(9200, false)); seriesdata.addElement(new Seriesdata("Product Y", new Color(200,0,200), columndata)); // Series 3 columndata = new Vector(); columndata.addElement(new Columndata(6000, false)); columndata.addElement(new Columndata(6200, false)); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7750, false)); seriesdata.addElement(new Seriesdata("Product Z", new Color(0,200,0), columndata)); columnchart.setSeriesdata(seriesdata); } //----------------------------------------------------------------------------- // Create the legenddata object // private void set_legenddata() { legenddata = new Vector(); legenddata.addElement(new Legenddata("Products", // Legend title new Color(0,0,0), // Legend text color new Font("Courier",Font.PLAIN,12), // Legend text font new Point(400,100) // Legend top-left position (x,y) )); columnchart.setLegenddata(legenddata); } //---------------------------------------------------------------------------------- } // End Applet class Step 9 - Create the Chart data object and set this property of the Column Chart Bean. Here we create Chart data object with the method set_chartdata(). Finally in this method the Seriesdata property of the Columnchart bean is set with the column, columnchart.setChartdata(chartdata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) private Vector xlabelsdata; // X labels Objects private Vector ylabelsdata; // Y labels Objects private Vector columndata; // Point data Objects private Vector seriesdata; // Series data Objects private Vector legenddata; // Legend Properties private Vector chartdata; // Chart Properties Object public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); set_xlabelsdata(); set_ylabelsdata(); set_seriescolumndata(); set_legenddata(); set_chartdata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //----------------------------------------------------------------------------- // Create the xlabels object // private void set_xlabelsdata() { xlabelsdata = new Vector(); xlabelsdata.addElement(new Xlabelsdata("Quarter 1",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 2",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 3",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 4",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); columnchart.setXlabelsdata(xlabelsdata); } //----------------------------------------------------------------------------- // Create the ylabels data object // private void set_ylabelsdata() { ylabelsdata = new Vector(); ylabelsdata.addElement(new Ylabelsdata(new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10))); columnchart.setYlabelsdata(ylabelsdata); } //----------------------------------------------------------------------------- // Create the series and column objects // private void set_seriescolumndata() { seriesdata = new Vector(); // Series 1 columndata = new Vector(); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7900, false)); columndata.addElement(new Columndata(10350, false)); columndata.addElement(new Columndata(13800, false)); seriesdata.addElement(new Seriesdata("Product X", new Color(200,0,0), columndata)); // Series 2 columndata = new Vector(); columndata.addElement(new Columndata(6300, false)); columndata.addElement(new Columndata(6400, false)); columndata.addElement(new Columndata(8700, false)); columndata.addElement(new Columndata(9200, false)); seriesdata.addElement(new Seriesdata("Product Y", new Color(200,0,200), columndata)); // Series 3 columndata = new Vector(); columndata.addElement(new Columndata(6000, false)); columndata.addElement(new Columndata(6200, false)); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7750, false)); seriesdata.addElement(new Seriesdata("Product Z", new Color(0,200,0), columndata)); columnchart.setSeriesdata(seriesdata); } //----------------------------------------------------------------------------- // Create the legenddata object // private void set_legenddata() { legenddata = new Vector(); legenddata.addElement(new Legenddata("Products", // Legend title new Color(0,0,0), // Legend text color new Font("Courier",Font.PLAIN,12), // Legend text font new Point(400,100) // Legend top-left position (x,y) )); columnchart.setLegenddata(legenddata); } //----------------------------------------------------------------------------- // Create the chartdata object // private void set_chartdata() { chartdata = new Vector(); chartdata.addElement(new Chartdata(true, // threeD true, // grid true, // axis true, // ylabels true, // outline true, // legend 500, // width 420, // height 4, // number of columns, 7, // number of nrows, 30, // vertical spacing, 3, // number of series, 80, // horizontal spacing, 75, // gridxpos, 350, // gridypos, 15, // depth3D, 0, // ndecplaces, 20, // Column width 1000, // chartScale, 6000, // chartStartY, new Color(100,100,100), // gridColor, new Color(0,0,0), // axisColor, new Color(100,100,100), // floorColor, new Color(0,0,0), // labelColor, new Color(255,255,255), // backgroundColor new Color(0,0,0) // Outline Color )); columnchart.setChartdata(chartdata); } //---------------------------------------------------------------------------------- } // End Applet class Step 10 - Create the Image data object and set this property of the Column Chart Bean. Here we create Image data object with the method set_imagedata(). We create three elements for this object which represent the three product images dispayed on the chart canvas. This method uses the DownloadImage method to load the images from file. Finally in this method the Imagedata property of the Columnchart bean is set with the column, columnchart.seImagedata(imagedata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) private Vector xlabelsdata; // X labels Objects private Vector ylabelsdata; // Y labels Objects private Vector columndata; // Point data Objects private Vector seriesdata; // Series data Objects private Vector legenddata; // Legend Properties private Vector chartdata; // Chart Properties Object private Vector imagedata; // Image Objects public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); set_xlabelsdata(); set_ylabelsdata(); set_seriescolumndata(); set_legenddata(); set_chartdata(); set_imagedata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //----------------------------------------------------------------------------- // Create the xlabels object // private void set_xlabelsdata() { xlabelsdata = new Vector(); xlabelsdata.addElement(new Xlabelsdata("Quarter 1",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 2",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 3",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 4",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); columnchart.setXlabelsdata(xlabelsdata); } //----------------------------------------------------------------------------- // Create the ylabels data object // private void set_ylabelsdata() { ylabelsdata = new Vector(); ylabelsdata.addElement(new Ylabelsdata(new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10))); columnchart.setYlabelsdata(ylabelsdata); } //----------------------------------------------------------------------------- // Create the series and column objects // private void set_seriescolumndata() { seriesdata = new Vector(); // Series 1 columndata = new Vector(); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7900, false)); columndata.addElement(new Columndata(10350, false)); columndata.addElement(new Columndata(13800, false)); seriesdata.addElement(new Seriesdata("Product X", new Color(200,0,0), columndata)); // Series 2 columndata = new Vector(); columndata.addElement(new Columndata(6300, false)); columndata.addElement(new Columndata(6400, false)); columndata.addElement(new Columndata(8700, false)); columndata.addElement(new Columndata(9200, false)); seriesdata.addElement(new Seriesdata("Product Y", new Color(200,0,200), columndata)); // Series 3 columndata = new Vector(); columndata.addElement(new Columndata(6000, false)); columndata.addElement(new Columndata(6200, false)); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7750, false)); seriesdata.addElement(new Seriesdata("Product Z", new Color(0,200,0), columndata)); columnchart.setSeriesdata(seriesdata); } //----------------------------------------------------------------------------- // Create the legenddata object // private void set_legenddata() { legenddata = new Vector(); legenddata.addElement(new Legenddata("Products", // Legend title new Color(0,0,0), // Legend text color new Font("Courier",Font.PLAIN,12), // Legend text font new Point(400,100) // Legend top-left position (x,y) )); columnchart.setLegenddata(legenddata); } //----------------------------------------------------------------------------- // Create the chartdata object // private void set_chartdata() { chartdata = new Vector(); chartdata.addElement(new Chartdata(true, // threeD true, // grid true, // axis true, // ylabels true, // outline true, // legend 500, // width 420, // height 4, // number of columns, 7, // number of nrows, 30, // vertical spacing, 3, // number of series, 80, // horizontal spacing, 75, // gridxpos, 350, // gridypos, 15, // depth3D, 0, // ndecplaces, 20, // Column width 1000, // chartScale, 6000, // chartStartY, new Color(100,100,100), // gridColor, new Color(0,0,0), // axisColor, new Color(100,100,100), // floorColor, new Color(0,0,0), // labelColor, new Color(255,255,255), // backgroundColor new Color(0,0,0) // Outline Color )); columnchart.setChartdata(chartdata); } //----------------------------------------------------------------------------- // Create the Images data object // private void set_imagedata() { imagedata = new Vector(); imagedata.addElement(new Imagedata("meter1.gif", downloadImage("meter1.gif"), new Point(570,100))); imagedata.addElement(new Imagedata("tools1.gif", downloadImage("tools1.gif"), new Point(570,118))); imagedata.addElement(new Imagedata("drill.gif", downloadImage("drill1.gif"), new Point(570,136))); columnchart.setImagedata(imagedata); } //DownloadImage-------------------------------------------------------------------------------- public Image downloadImage(String img) { int pixs[]; MediaTracker tracker; tracker = new MediaTracker(this); Image di = getImage(getCodeBase(), img); tracker.addImage(di, 0); try { showStatus("Loading Image..."); tracker.waitForID(0); showStatus(""); } catch(InterruptedException e) { return createImage(size().width, size().height); } //MakeVMThinkTheImageSourceIsMemory______________________________________ int w = di.getWidth(this); int h = di.getHeight(this); pixs = new int[w * h]; PixelGrabber pg = new PixelGrabber(di, 0, 0, w, h, pixs, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); showStatus("Image loading error"); return createImage(size().width, size().height); } if((pg.status() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); showStatus("Image loading error"); return createImage(size().width, size().height); } di = createImage(new MemoryImageSource(w, h, pixs, 0, w)); int index = 0; return di; } // End download image method //---------------------------------------------------------------------------------- } // End Applet class Step 11 - Create the MinMax data object and set this property of the Column Chart Bean. Here we create MinMax data object with the method set_minmaxdata(). We create two elements for this object which represent the Break Even and Target columns on our our Sale chart. Finally in this method the data property of the Columnchart bean is set with the column, columnchart.setMinmaxdata(minmaxdata); import Sirius.columnchart.*; import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.lang.*; public class ColumnchartApplet extends Applet { // Bean Object private Columnchart columnchart; // Data Objects private Vector textdata; // Text Objects private Vector titledata; // Title Objects (Main Title, X Title, Y Title) private Vector xlabelsdata; // X labels Objects private Vector ylabelsdata; // Y labels Objects private Vector columndata; // Point data Objects private Vector seriesdata; // Series data Objects private Vector legenddata; // Legend Properties private Vector chartdata; // Chart Properties Object private Vector imagedata; // Image Objects private Vector minmaxdata; // Min Max Data columns public ColumnchartApplet() { } public void init() { columnchart = new Columnchart(); columnchart.resize(500,420); set_textdata(); set_titledata(); set_xlabelsdata(); set_ylabelsdata(); set_seriescolumndata(); set_legenddata(); set_chartdata(); set_imagedata(); set_minmaxdata(); setLayout(null); setBackground(Color.white); add(columnchart); } //----------------------------------------------------------------------------- // Create the text object // private void set_textdata() { textdata = new Vector(); textdata.addElement(new Textdata("Note:", // Text line 1 new Color(0,0,255), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,60) // text position (x,y) )); textdata.addElement(new Textdata("New product range", // Text line 2 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,80) // text position (x,y) )); textdata.addElement(new Textdata("launched during", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,100) // text position (x,y) )); textdata.addElement(new Textdata("quarter 2.", // Text line 3 new Color(0,0,0), // text color new Font("TimesRoman",Font.PLAIN,12), // text font new Point(80,120) // text position (x,y) )); columnchart.setTextdata(textdata); } //----------------------------------------------------------------------------- // Create the titledata object // private void set_titledata() { titledata = new Vector(); titledata.addElement(new Titledata("Sales by Quarter",new Color(100,100,100),new Font("TimesRoman",Font.BOLD + Font.ITALIC,18),new Point(50,20))); titledata.addElement(new Titledata("Year 2001",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(200,400))); titledata.addElement(new Titledata("Value $",new Color(100,100,100),new Font("TimesRoman",Font.BOLD,16),new Point(10,300))); columnchart.setTitledata(titledata); } //----------------------------------------------------------------------------- // Create the xlabels object // private void set_xlabelsdata() { xlabelsdata = new Vector(); xlabelsdata.addElement(new Xlabelsdata("Quarter 1",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 2",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 3",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); xlabelsdata.addElement(new Xlabelsdata("Quarter 4",new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10), 375, 0)); columnchart.setXlabelsdata(xlabelsdata); } //----------------------------------------------------------------------------- // Create the ylabels data object // private void set_ylabelsdata() { ylabelsdata = new Vector(); ylabelsdata.addElement(new Ylabelsdata(new Color(0,0,0),new Font("TimesRoman",Font.PLAIN,10))); columnchart.setYlabelsdata(ylabelsdata); } //----------------------------------------------------------------------------- // Create the series and column objects // private void set_seriescolumndata() { seriesdata = new Vector(); // Series 1 columndata = new Vector(); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7900, false)); columndata.addElement(new Columndata(10350, false)); columndata.addElement(new Columndata(13800, false)); seriesdata.addElement(new Seriesdata("Product X", new Color(200,0,0), columndata)); // Series 2 columndata = new Vector(); columndata.addElement(new Columndata(6300, false)); columndata.addElement(new Columndata(6400, false)); columndata.addElement(new Columndata(8700, false)); columndata.addElement(new Columndata(9200, false)); seriesdata.addElement(new Seriesdata("Product Y", new Color(200,0,200), columndata)); // Series 3 columndata = new Vector(); columndata.addElement(new Columndata(6000, false)); columndata.addElement(new Columndata(6200, false)); columndata.addElement(new Columndata(7000, false)); columndata.addElement(new Columndata(7750, false)); seriesdata.addElement(new Seriesdata("Product Z", new Color(0,200,0), columndata)); columnchart.setSeriesdata(seriesdata); } //----------------------------------------------------------------------------- // Create the legenddata object // private void set_legenddata() { legenddata = new Vector(); legenddata.addElement(new Legenddata("Products", // Legend title new Color(0,0,0), // Legend text color new Font("Courier",Font.PLAIN,12), // Legend text font new Point(400,100) // Legend top-left position (x,y) )); columnchart.setLegenddata(legenddata); } //----------------------------------------------------------------------------- // Create the chartdata object // private void set_chartdata() { chartdata = new Vector(); chartdata.addElement(new Chartdata(true, // threeD true, // grid true, // axis true, // ylabels true, // outline true, // legend 500, // width 420, // height 4, // number of columns, 7, // number of nrows, 30, // vertical spacing, 3, // number of series, 80, // horizontal spacing, 75, // gridxpos, 350, // gridypos, 15, // depth3D, 0, // ndecplaces, 20, // Column width 1000, // chartScale, 6000, // chartStartY, new Color(100,100,100), // gridColor, new Color(0,0,0), // axisColor, new Color(100,100,100), // floorColor, new Color(0,0,0), // labelColor, new Color(255,255,255), // backgroundColor new Color(0,0,0) // Outline Color )); columnchart.setChartdata(chartdata); } //----------------------------------------------------------------------------- // Create the Images data object // private void set_imagedata() { imagedata = new Vector(); imagedata.addElement(new Imagedata("meter1.gif", downloadImage("meter1.gif"), new Point(570,100))); imagedata.addElement(new Imagedata("tools1.gif", downloadImage("tools1.gif"), new Point(570,118))); imagedata.addElement(new Imagedata("drill.gif", downloadImage("drill1.gif"), new Point(570,136))); columnchart.setImagedata(imagedata); } //DownloadImage-------------------------------------------------------------------------------- public Image downloadImage(String img) { int pixs[]; MediaTracker tracker; tracker = new MediaTracker(this); Image di = getImage(getCodeBase(), img); tracker.addImage(di, 0); try { showStatus("Loading Image..."); tracker.waitForID(0); showStatus(""); } catch(InterruptedException e) { return createImage(size().width, size().height); } //MakeVMThinkTheImageSourceIsMemory______________________________________ int w = di.getWidth(this); int h = di.getHeight(this); pixs = new int[w * h]; PixelGrabber pg = new PixelGrabber(di, 0, 0, w, h, pixs, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); showStatus("Image loading error"); return createImage(size().width, size().height); } if((pg.status() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); showStatus("Image loading error"); return createImage(size().width, size().height); } di = createImage(new MemoryImageSource(w, h, pixs, 0, w)); int index = 0; return di; } // End download image method //----------------------------------------------------------------------------- // Create the Min Max Column data object // private void set_minmaxdata() { minmaxdata = new Vector(); minmaxdata.addElement(new MinMaxdata(" Target",new Color(0,100,0), new Font("Arial",Font.BOLD,12),8700,1)); columnchart.setMinmaxdata(minmaxdata); } //---------------------------------------------------------------------------------- } // End Applet class
Summary
In order to generate the column chart within an applet a simple applet framework
was created, an instance of the columnchart bean was created and finally the data
objects were created and the corresponding bean properties set.