Advertisement
Elandiro

Charts

Mar 14th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package gna;
  2.  
  3. import java.io.File;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6.  
  7. import org.jfree.chart.ChartFactory;
  8. import org.jfree.chart.ChartPanel;
  9. import org.jfree.chart.ChartUtilities;
  10. import org.jfree.chart.JFreeChart;
  11. import org.jfree.chart.plot.PlotOrientation;
  12. import org.jfree.chart.plot.XYPlot;
  13. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
  14. import org.jfree.data.xy.XYSeries;
  15. import org.jfree.data.xy.XYSeriesCollection;
  16. import org.jfree.ui.ApplicationFrame;
  17.  
  18. public class SortingAlgorithmsCharts {
  19.  
  20. public static void main(String[] args) {
  21. XYSeriesCollection dataset;
  22. SortingAlgorithms algorithms = new SortingAlgorithms();
  23. new SortingAlgorithmsCharts();
  24. dataset = new XYSeriesCollection();
  25. XYSeries selectionSort = new XYSeries("Selection Sort");
  26. XYSeries insertionSort = new XYSeries("Insertion Sort");
  27. XYSeries mergeSort = new XYSeries("Merge Sort");
  28. XYSeries quickSort = new XYSeries("Quick Sort");
  29.  
  30. for (int j = 0; j <= 99; j++) {
  31.  
  32. for (int k = 1; k <= 100; k++) {
  33. Integer[] array = new Integer[k];
  34. for (int n = 0; n < k; n++) {
  35. array[n] = n;
  36. }
  37. Collections.shuffle(Arrays.asList(array));
  38. Integer[] aux = array.clone();
  39. selectionSort.add(k, algorithms.selectionSort(array));
  40. array = aux.clone();
  41. insertionSort.add(k, algorithms.insertionSort(array));
  42. array = aux.clone();
  43. mergeSort.add(k, algorithms.mergeSort(array));
  44. array = aux.clone();
  45. quickSort.add(k, algorithms.quickSort(array));
  46. }
  47. }
  48. dataset.addSeries(selectionSort);
  49. dataset.addSeries(insertionSort);
  50. dataset.addSeries(mergeSort);
  51. dataset.addSeries(quickSort);
  52.  
  53. final JFreeChart chart = ChartFactory.createScatterPlot(
  54. "Shuffled array", // chart title
  55. "Number of elements", // x axis label
  56. "Number of comparisons", // y axis label
  57. dataset, // data
  58. PlotOrientation.VERTICAL, true, // include legend
  59. true, // tooltips
  60. false // urls
  61. );
  62. XYPlot plot = (XYPlot) chart.getPlot();
  63. XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
  64. renderer.setSeriesLinesVisible(0, true);
  65. plot.setRenderer(renderer);
  66. final ChartPanel chartPanel = new ChartPanel(chart);
  67. chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
  68. final ApplicationFrame frame = new ApplicationFrame("Title");
  69. frame.setContentPane(chartPanel);
  70. frame.pack();
  71. frame.setVisible(true);
  72.  
  73. try {
  74. ChartUtilities
  75. .saveChartAsJPEG(new File(
  76. "D:\\Schoolwerk\\G&A\\shuffledChart.jpg"), chart,
  77. 500, 300);
  78. System.out.println("Chart created succesfully.");
  79. } catch (Exception e) {
  80. System.out.println("Problem occurred creating chart.");
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement