A sample SOCRChartAPITest class

From Socr

(Difference between revisions)
Jump to: navigation, search
 
(6 intermediate revisions not shown)
Line 1: Line 1:
 +
'''SOCRChartAPIDemo class'''
 +
<pre>
import javax.swing.JOptionPane;
import javax.swing.JOptionPane;
-
 
import javax.swing.JPanel;
import javax.swing.JPanel;
-
 
import javax.swing.JTable;
import javax.swing.JTable;
-
 
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartPanel;
-
 
import org.jfree.chart.JFreeChart;
import org.jfree.chart.JFreeChart;
-
 
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.RefineryUtilities;
-
 
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.ApplicationFrame;
-
 
import edu.ucla.stat.SOCR.chart.ChartGenerator_JTable;
import edu.ucla.stat.SOCR.chart.ChartGenerator_JTable;
-
 
public class SOCRChartAPIDemo extends ApplicationFrame {
public class SOCRChartAPIDemo extends ApplicationFrame {
-
 
-
    /**
 
-
 
-
    * Creates a new demo.
 
-
 
-
    */
 
    public SOCRChartAPIDemo(String title) {
    public SOCRChartAPIDemo(String title) {
-
 
+
        super(title);
-
    super(title);
+
-
 
+
    JTable dTable = createDataTable();
    JTable dTable = createDataTable();
-
 
        JPanel chartPanel = createDemoPanel(title, dTable);
        JPanel chartPanel = createDemoPanel(title, dTable);
-
 
        // chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        // chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
-
 
        setContentPane(chartPanel);
        setContentPane(chartPanel);
-
 
    }
    }
  private JPanel createDemoPanel(String title, JTable dTable) {
  private JPanel createDemoPanel(String title, JTable dTable) {
-
 
        JFreeChart chart = createChart(title, dTable);
        JFreeChart chart = createChart(title, dTable);
-
 
        return new ChartPanel(chart);
        return new ChartPanel(chart);
    }
    }
-
   
+
   
-
    private JFreeChart createChart(String title, JTable dataTable){
+
          private JFreeChart createChart(String title, JTable dataTable){
-
 
+
              JFreeChart chart;
-
        JFreeChart chart;
+
-
 
+
            ChartGenerator_JTable chartMaker = new ChartGenerator_JTable();
-
ChartGenerator_JTable chartMaker = new ChartGenerator_JTable();
+
 +
        int no_series = dataTable.getRowCount()-1;
 +
int no_category = dataTable.getColumnCount()-2;
-
int no_series = dataTable.getRowCount()-1;
+
                int[][] pairs = new int[no_category][2];
 +
for (int i=0; i<no_category; i++){
 +
  pairs[i][0] = i+1;   //column i store category i
 +
  pairs[i][1] = 0;    //column 0 stores series name
 +
}
-
int no_category = dataTable.getColumnCount()-2;
+
chart = chartMaker.getCategoryChart("Bar", title, "Category", "value", dataTable, no_category, pairs, "3D");
-
int[][] pairs = new int[no_category][2];
+
return chart;
 +
    }
-
for (int i=0; i<no_category; i++){
+
/*
-
 
+
      Series    | Category 1 | Category 2  | Category 3  | Category 4  | Category 5  |
-
pairs[i][0] = i+1;    //column i store category i
+
      --------------------------------------------------------------------------------------
-
 
+
      First      |  1.0       | 4.0         | 3.0        | 5.0      | 5.0        |
-
pairs[i][1] = 0;    //column 0 stores series name
+
      Second    |  5.0        | 7.0        | 6.0        | 8.0      | 4.0        |
-
}
+
      Third      |  4.0        | 3.0        | 2.0        | 3.0      | 6.0        |
-
 
+
-
chart = chartMaker.getCategoryChart("Bar", title, "Category", "value", dataTable, no_category, pairs, "3D");
+
-
+
-
return chart;
+
-
    }
+
 +
*/
    private JTable createDataTable(){
    private JTable createDataTable(){
    JTable dTable;
    JTable dTable;
-
 
    String[] columnNames = new String[6];
    String[] columnNames = new String[6];
-
 
+
    String[][] example = new String[3][6];
-
    String[][] example = new String[6][3];
+
   
   
    columnNames[0]="Series";
    columnNames[0]="Series";
-
 
    columnNames[1]= "Category 1";  
    columnNames[1]= "Category 1";  
-
+
columnNames[2]= "Category 2";  
-
    columnNames[2]= "Category 2";  
+
-
 
+
    columnNames[3]= "Category 3";
    columnNames[3]= "Category 3";
-
 
    columnNames[4]= "Category 4";  
    columnNames[4]= "Category 4";  
-
+
columnNames[5]= "Category 5";
-
    columnNames[5]= "Category 5";
+
   
   
-
   
+
        example[0][0]="First";example[0][1]="1.0";example[0][2]="4.0";
-
    example[0][0]="First";example[1][0]="1.0";example[2][0]="4.0";example[3][0]="3.0";example[4][0]="5.0";example[5][0]="5.0";
+
                example[0][3]="3.0";example[0][4]="5.0";example[0][5]="5.0";
-
    example[0][1]="Second";example[1][1]="5.0";example[2][1]="7.0";example[3][1]="6.0";example[4][1]="8.0";example[5][1]="4.0";
+
                example[1][0]="Second";example[1][1]="5.0";example[1][2]="7.0";
 +
                example[1][3]="6.0";example[1][4]="8.0";example[1][5]="4.0";
-
    example[0][1]="Third";example[1][2]="4.0";example[2][2]="3.0";example[3][2]="2.0";example[4][2]="3.0";example[5][2]="6.0";
+
                example[2][0]="Third";example[2][1]="4.0";example[2][2]="3.0";
 +
                example[2][3]="2.0";example[2][4]="3.0";example[2][5]="6.0";
   
   
    dTable = new JTable(example, columnNames);
    dTable = new JTable(example, columnNames);
-
 
+
                return dTable;
-
    return dTable;
+
    }
    }
-
    /**
+
-
 
+
            /**
-
    * Starting point for the demonstration application.
+
    * Starting point for the demonstration application
-
 
+
    *
    *
-
 
    * @param args  ignored.
    * @param args  ignored.
-
 
    */
    */
    public static void main(String[] args) {
    public static void main(String[] args) {
-
    final SOCRChartAPIDemo demo = new SOCRChartAPIDemo("3D Bar Chart Demo 1");
+
        final SOCRChartAPIDemo demo = new SOCRChartAPIDemo("3D Bar Chart Demo 1");
-
 
+
        demo.pack();
        demo.pack();
-
 
        RefineryUtilities.centerFrameOnScreen(demo);
        RefineryUtilities.centerFrameOnScreen(demo);
-
 
        demo.setVisible(true);
        demo.setVisible(true);
-
 
    }
    }
}
}
 +
</pre>
 +
 +
The  '''createChart''' method is the center part of the class. In this method, the ''pairs'' value is being set first, then a '''get*Chart''' method of the ''ChartGenerator_JTable'' class  is called to generate the chart figure.  The ''init[][] pairs'' provides the coulmn info about the JTable and is used by the ''ChartGenerator_JTable'' to generate dataset for creating the chart. 
 +
 +
See  [[list of charts |List of Charts]]  for more examples. The  '''createChart''' method's source code  for each example is listed for your reference.

Current revision as of 19:39, 27 June 2006

SOCRChartAPIDemo class

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.ApplicationFrame;
import edu.ucla.stat.SOCR.chart.ChartGenerator_JTable;


	public class SOCRChartAPIDemo extends ApplicationFrame {

	    public SOCRChartAPIDemo(String title) {
	        super(title);
	     	 JTable dTable = createDataTable();
	         JPanel chartPanel = createDemoPanel(title, dTable);
	        // chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
	         setContentPane(chartPanel);
	    }

	   private JPanel createDemoPanel(String title, JTable dTable) {
	        JFreeChart chart = createChart(title, dTable);
	        return new ChartPanel(chart);
	    }

    
          private JFreeChart createChart(String title, JTable dataTable){
              JFreeChart chart;
			
             ChartGenerator_JTable chartMaker = new ChartGenerator_JTable();

	        int no_series = dataTable.getRowCount()-1;
		int no_category = dataTable.getColumnCount()-2;
			
                int[][] pairs = new int[no_category][2];
		for (int i=0; i<no_category; i++){
		   pairs[i][0] = i+1;    //column i store category i
		   pairs[i][1] = 0;    //column 0 stores series name
		 }

		 chart = chartMaker.getCategoryChart("Bar", title, "Category", "value", dataTable, no_category, pairs, "3D");

		 return chart;
	    }

/*
       Series     | Category 1  | Category 2  | Category 3  | Category 4  | Category 5  |
      --------------------------------------------------------------------------------------
       First      |  1.0        | 4.0         | 3.0        | 5.0       | 5.0         |
       Second     |  5.0        | 7.0         | 6.0        | 8.0       | 4.0         |
       Third      |  4.0        | 3.0         | 2.0        | 3.0       | 6.0         |

*/
	    private JTable createDataTable(){

	    	JTable dTable;
	    	String[] columnNames = new String[6];
	    	String[][] example = new String[3][6];
	    	
	    	columnNames[0]="Series";
	    	columnNames[1]= "Category 1"; 
		columnNames[2]= "Category 2"; 
	    	columnNames[3]= "Category 3"; 	
	    	columnNames[4]= "Category 4"; 
		columnNames[5]= "Category 5";
	    	
	        example[0][0]="First";example[0][1]="1.0";example[0][2]="4.0";
                example[0][3]="3.0";example[0][4]="5.0";example[0][5]="5.0";

                example[1][0]="Second";example[1][1]="5.0";example[1][2]="7.0";
                example[1][3]="6.0";example[1][4]="8.0";example[1][5]="4.0";

                example[2][0]="Third";example[2][1]="4.0";example[2][2]="3.0";
                example[2][3]="2.0";example[2][4]="3.0";example[2][5]="6.0";
	    	
	    	dTable = new JTable(example, columnNames);
                return dTable;
	    }
	
            /**
	     * Starting point for the demonstration application
	     *
	     * @param args  ignored.
	     */

	    public static void main(String[] args) {

	        final SOCRChartAPIDemo demo = new SOCRChartAPIDemo("3D Bar Chart Demo 1");
	        demo.pack();
	        RefineryUtilities.centerFrameOnScreen(demo);
	        demo.setVisible(true);
	    }

	}

The createChart method is the center part of the class. In this method, the pairs value is being set first, then a get*Chart method of the ChartGenerator_JTable class is called to generate the chart figure. The init[][] pairs provides the coulmn info about the JTable and is used by the ChartGenerator_JTable to generate dataset for creating the chart.

See List of Charts for more examples. The createChart method's source code for each example is listed for your reference.

Personal tools