Java examples for 2D Graphics:Chart
Create bar chart
import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import java.io.File; import org.jfree.data.category.DefaultCategoryDataset; import javax.swing.JOptionPane; /**//ww w . j a v a 2s . c o m * @author Lewis Radcliffe, Laurence Tsang & Joshua Reynolds * @file BarChart.java * @date 28.02.13 * @version 2.1 * @see http://www.jfree.org/jfreechart/api/javadoc/index.html , Wicked * Cool Java By: Brian D. Eubanks, chapter 6 -creating jfreecharts * * \brief BarChart class creates the Bar Chart. */ public class BarChart extends Visualization { /** * Constructor that reads in a DataSet to initialize * * @param DataSet with information to be visualized */ public BarChart(DataSet inputdataset) { super.setDataset(inputdataset); } /** * Method that creates the bar chart * * @return the chart for display */ public JFreeChart MakeChart() { Object[] column1Data = super.getDataset().GetColumnData( super.getAttribute1()); Object[] column2Data = super.getDataset().GetColumnData( super.getAttribute2()); boolean addDataset = true; String errors = ""; int errorCounter = 0; DefaultCategoryDataset barDataset = new DefaultCategoryDataset(); for (int i = 0; i < super.getDataset().GetNoOfEntrys(); i++) { Double nextValue2 = null; boolean addThis = true; Comparable<Object> nextValue1; nextValue1 = (Comparable<Object>) column1Data[i]; try { int intNextValue2 = Integer.parseInt(column2Data[i] .toString()); nextValue2 = (Double) ((double) intNextValue2); } catch (NumberFormatException nfe) { try { double doubleNextValue2 = Double .parseDouble(column2Data[i].toString()); nextValue2 = (Double) doubleNextValue2; } catch (NumberFormatException nfe2) { String strNextValue2 = column2Data[i].toString(); if (strNextValue2.equalsIgnoreCase("True")) { nextValue2 = TRUE; } else if (strNextValue2.equalsIgnoreCase("False")) { nextValue2 = FALSE; } else { addThis = false; } } } catch (Exception e) { addThis = false; } if (addThis == true) { barDataset.addValue(nextValue2, nextValue1, nextValue1); } else { addDataset = false; if (errorCounter < MAX_ERROR_LENGTH) { errors = errors + "\n" + column2Data[i].toString(); errorCounter++; } } } if (addDataset == false) { barDataset = new DefaultCategoryDataset(); //Reset JOptionPane .showMessageDialog( null, "Your selected y-axis has data in the wrong format" + "\n" + "The following data needs to be a number in order to be" + " represented." + errors); } JFreeChart chart = ChartFactory.createBarChart(super.getHeader(), super.getxAxis(), super.getyAxis(), barDataset, PlotOrientation.VERTICAL, true, false, false); return chart; } private final Double TRUE = 1.0; private final Double FALSE = 0.0; private final int MAX_ERROR_LENGTH = 5; /** * Unit test used to test BarChart class. */ public static void main(String args[]) { DataSet testdataset = new DataSet(); File inputfile = new File("test.csv"); testdataset.BuildDataSet(inputfile); System.out.println("BarChart:: BarChart()"); BarChart testBarChart = new BarChart(testdataset); System.out.println("BarChart::BarChart()- Test Passed"); System.out.println("BarChart:: MakeChart()"); testBarChart.MakeChart(); System.out.println("BarChart::MakeChart()- Test Passed"); } } /* end BarChart class */
Visualization.java
import java.io.File; /**//from w w w . j av a 2 s. co m * @file Visualization.java * @author Joshua Reynolds * @date 20 Feb 2013 * @see * * \brief Visualization that stores selected attirbutes and data */ public class Visualization { /* * Method that gets the first attribute of the visualization * * @return the first attribute */ public int getAttribute1() { return m_attribute1; } /* * Method that gets the second attribute of the visualization * * @return the second attribute */ public int getAttribute2() { return m_attribute2; } /* * Method that gets the dataset of the visualization * * @return the dataset */ public DataSet getDataset() { return m_dataset; } /* * Method that gets the header of the visualization * * @return the header of the visualization */ public String getHeader() { return m_header; } /* * Method that gets the x axis of the visualization * * @eturn the x axis label */ public String getxAxis() { return m_xAxis; } /* * Method that gets the y axis of the visualization * * @return the y axis label */ public String getyAxis() { return m_yAxis; } /* * Method that allows you to set the first attribute of the visualization * * @param attribute1-the index of the attribute * * @return true if first attribute has been set */ public boolean setAttribute1(int attribute1) { m_attribute1 = attribute1; return true; } /* * Method that allows you to set the second attribute of the visualization * * @param attribute2-the index of the attribute * * @return true if second attribute has been set */ public boolean setAttribute2(int attribute2) { m_attribute2 = attribute2; return true; } /* * Method that allows you to set the dataset of the visualization * * @param dataset * * @return true if dataset has been set */ public boolean setDataset(DataSet dataset) { m_dataset = dataset; return true; } /* * Method that allows you to set the header of the visualization * * @param header-the header of the visualization * * @return true if the header has been set */ public boolean setHeader(String header) { m_header = header; return true; } /* * Method that allows you to set the x axis label of the visualization * * @param xAxis-the label for the x axis * * @return true if the x axis has been set */ public boolean setxAxis(String xAxis) { m_xAxis = xAxis; return true; } /* * Method that allows you to set the y axis label of the visualization * * @param yAxis-the label for the y axis * * @return true if the y axis has been set */ public boolean setyAxis(String yAxis) { m_yAxis = yAxis; return true; } private DataSet m_dataset; private int m_attribute1; private int m_attribute2; private String m_header; private String m_xAxis; private String m_yAxis; public static void main(String args[]) { final int TEST_DATA = 100; //test create visualization class System.out.println("Visualization:: Visualization()"); Visualization testVisualization = new Visualization(); System.out.println("Visualization:: Visualization() - Test Passed"); //test DataSet class DataSet testdataset = new DataSet(); File inputfile = new File("test.csv"); testdataset.BuildDataSet(inputfile); //test setDataset method System.out.println("Visualization:: setDataset()"); testVisualization.setDataset(testdataset); System.out.println("Visualization:: setDataset() - Test Passed"); //test setAttribute1 method System.out.println("Visualization:: setAttribute1()"); boolean testSetAttribute1 = testVisualization .setAttribute1(TEST_DATA); System.out.println("Visualization:: setAttribute1() - Test Passed"); //test setAttribute2 method System.out.println("Visualization:: setAttribute2()"); boolean testSetAttribute2 = testVisualization .setAttribute2(TEST_DATA); System.out.println("Visualization:: setAttribute2() - Test Passed"); //test setHeader method System.out.println("Visualization:: setHeader()"); boolean testSetHeader = testVisualization.setHeader("Visulization"); System.out.println("Visualization:: setHeader() - Test Passed"); //test setxAxis method System.out.println("Visualization:: setxAxis()"); boolean testSetxAxis = testVisualization.setxAxis("xAxis"); System.out.println("Visualization:: setxAxis() - Test Passed"); //test setyAxis method System.out.println("Visualization:: setyAxis()"); boolean testSetyAxis = testVisualization.setyAxis("yAxis"); System.out.println("Visualization:: setyAxis() - Test Passed"); //test getDataset method System.out.println("Visualization:: getDataset()"); testVisualization.getDataset(); System.out.println("Visualization:: getDataset() - Test Passed"); //test getAttribute1 method System.out.println("Visualization:: getAttribute1()"); int testGetAttribute1 = testVisualization.getAttribute1(); System.out.println("Visualization:: getAttribute1() - Test Passed"); //test getAttribute2 method System.out.println("Visualization:: getAttribute2()"); int testGetAttribute2 = testVisualization.getAttribute2(); System.out.println("Visualization:: getAttribute2() - Test Passed"); //test getHeader method System.out.println("Visualization:: getHeader()"); String testGetHeader = testVisualization.getHeader(); System.out.println("Visualization:: getHeader() - Test Passed"); //test getxAxis method System.out.println("Visualization:: getxAxis()"); String testGetxAxis = testVisualization.getxAxis(); System.out.println("Visualization:: getxAxis() - Test Passed"); //test getyAxis method System.out.println("Visualization:: getyAxis()"); String testGetyAxis = testVisualization.getyAxis(); System.out.println("Visualization:: getyAxis() - Test Passed"); } } /* End of Visualization Class */