| 1 | package de.uka.ipd.sdq.pcmsolver.visualisation; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Color; |
| 5 | import java.awt.Toolkit; |
| 6 | import java.io.BufferedWriter; |
| 7 | import java.io.FileWriter; |
| 8 | import java.io.IOException; |
| 9 | import java.util.List; |
| 10 | |
| 11 | import javax.swing.JFrame; |
| 12 | |
| 13 | import org.jfree.chart.ChartFactory; |
| 14 | import org.jfree.chart.ChartPanel; |
| 15 | import org.jfree.chart.JFreeChart; |
| 16 | import org.jfree.chart.plot.PlotOrientation; |
| 17 | import org.jfree.chart.plot.XYPlot; |
| 18 | import org.jfree.data.xy.DefaultTableXYDataset; |
| 19 | import org.jfree.data.xy.XYSeries; |
| 20 | |
| 21 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
| 22 | import de.uka.ipd.sdq.probfunction.math.ISample; |
| 23 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
| 24 | import de.uka.ipd.sdq.probfunction.math.ManagedPMF; |
| 25 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilityFunctionException; |
| 26 | |
| 27 | public class JFVisualisation { |
| 28 | |
| 29 | protected JFrame graphFrame; |
| 30 | |
| 31 | protected JFreeChart myChart; |
| 32 | |
| 33 | protected ChartPanel chartPanel; |
| 34 | |
| 35 | protected DefaultTableXYDataset dataset; |
| 36 | |
| 37 | protected double distance; |
| 38 | |
| 39 | public JFVisualisation(double distance) { |
| 40 | dataset = new DefaultTableXYDataset(); |
| 41 | graphFrame = new JFrame("Execution Time"); |
| 42 | this.distance = distance; |
| 43 | } |
| 44 | |
| 45 | public void addSamplePDF(ISamplePDF samplePDF, String name) |
| 46 | throws ProbabilityFunctionException { |
| 47 | //ISamplePDF samplePDF = pdf.getSamplePdfTimeDomain(); |
| 48 | |
| 49 | |
| 50 | XYSeries series = new XYSeries(name, true, false); |
| 51 | |
| 52 | samplePDF = samplePDF.getFunctionWithNewDistance(distance); |
| 53 | List<Double> points = samplePDF.getValuesAsDouble(); |
| 54 | |
| 55 | |
| 56 | |
| 57 | try { |
| 58 | BufferedWriter out = new BufferedWriter(new FileWriter("test.csv")); |
| 59 | |
| 60 | int i = 0; |
| 61 | for (Double point : points) { |
| 62 | series.add(i, point); |
| 63 | out.write(i/10.0+";"+point); |
| 64 | out.newLine(); |
| 65 | i++; |
| 66 | } |
| 67 | |
| 68 | out.close(); |
| 69 | |
| 70 | } catch (IOException e) { |
| 71 | // TODO Auto-generated catch block |
| 72 | e.printStackTrace(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | dataset.addSeries(series); |
| 78 | } |
| 79 | |
| 80 | public void visualize() { |
| 81 | dataset.setIntervalWidth(distance); |
| 82 | myChart = ChartFactory.createHistogram( |
| 83 | "Measured Execution Time Histogram", "Time ["+distance+" s]", |
| 84 | "Probability", dataset, PlotOrientation.VERTICAL, true, true, |
| 85 | true); |
| 86 | XYPlot plot = (XYPlot) myChart.getPlot(); |
| 87 | plot.getRangeAxis().setRange(0, 1.1d); |
| 88 | |
| 89 | graphFrame.setSize(800, 600); |
| 90 | graphFrame |
| 91 | .setLocation( |
| 92 | (Toolkit.getDefaultToolkit().getScreenSize().width - graphFrame |
| 93 | .getSize().width) / 2, |
| 94 | (Toolkit.getDefaultToolkit().getScreenSize().height - graphFrame |
| 95 | .getSize().height) / 2); |
| 96 | graphFrame.getContentPane().setLayout(new BorderLayout()); |
| 97 | chartPanel = new ChartPanel(myChart); |
| 98 | graphFrame.getContentPane().add(chartPanel, BorderLayout.CENTER); |
| 99 | graphFrame.setVisible(true); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | public void visualizeOverlay() { |
| 104 | dataset.setIntervalWidth(1); |
| 105 | myChart = ChartFactory.createHistogram("", "Time ["+distance+" ms]", // x Axis label |
| 106 | "Probability", // y Axis label |
| 107 | dataset, PlotOrientation.VERTICAL, true, // legend |
| 108 | true, // tooltips |
| 109 | true); // url |
| 110 | |
| 111 | XYPlot plot = (XYPlot) myChart.getPlot(); |
| 112 | |
| 113 | plot.getRenderer().setSeriesPaint(0, Color.BLUE.brighter()); |
| 114 | //plot.getRenderer().setSeriesPaint(0, Color.RED); // Foreground Series |
| 115 | plot.getRenderer().setSeriesPaint(1, Color.BLUE.brighter()); // Background Series |
| 116 | plot.setForegroundAlpha(0.8f); // for transparency |
| 117 | //plot.getRangeAxis().setRange(0, 0.5d); |
| 118 | plot.getRangeAxis().setAutoRange(true); |
| 119 | plot.setBackgroundPaint(Color.white); |
| 120 | |
| 121 | graphFrame.setSize(800, 600); |
| 122 | graphFrame.setLocation( |
| 123 | (Toolkit.getDefaultToolkit().getScreenSize().width - graphFrame |
| 124 | .getSize().width) / 2, |
| 125 | (Toolkit.getDefaultToolkit().getScreenSize().height - graphFrame |
| 126 | .getSize().height) / 2); |
| 127 | graphFrame.getContentPane().setLayout(new BorderLayout()); |
| 128 | chartPanel = new ChartPanel(myChart); |
| 129 | graphFrame.getContentPane().add(chartPanel, BorderLayout.CENTER); |
| 130 | //graphFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 131 | graphFrame.setVisible(true); |
| 132 | |
| 133 | |
| 134 | // File testFile = new File("examplePMF2.png"); |
| 135 | // try { |
| 136 | // ChartUtilities.saveChartAsPNG(testFile, myChart, 600, 400); |
| 137 | // } catch (IOException e) { |
| 138 | // e.printStackTrace(); |
| 139 | // } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | public void addPMF(ManagedPMF managedPMF, String name) { |
| 144 | IProbabilityMassFunction pmf = managedPMF.getPmfTimeDomain(); |
| 145 | |
| 146 | XYSeries series = new XYSeries(name, true, false); |
| 147 | |
| 148 | List<ISample> points = pmf.getSamples(); |
| 149 | |
| 150 | for (ISample sample : points) { |
| 151 | double value = (Integer) sample.getValue(); |
| 152 | series.add(value, sample.getProbability()); |
| 153 | } |
| 154 | dataset.addSeries(series); |
| 155 | } |
| 156 | } |