1 4 package com.tctest.performance.results; 5 6 import org.jfree.chart.ChartFactory; 7 import org.jfree.chart.ChartPanel; 8 import org.jfree.chart.ChartUtilities; 9 import org.jfree.chart.JFreeChart; 10 import org.jfree.chart.axis.NumberAxis; 11 import org.jfree.chart.plot.PlotOrientation; 12 import org.jfree.chart.plot.XYPlot; 13 import org.jfree.chart.renderer.xy.XYItemRenderer; 14 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 15 import org.jfree.data.xy.XYDataset; 16 import org.jfree.data.xy.XYSeries; 17 import org.jfree.data.xy.XYSeriesCollection; 18 import org.jfree.ui.ApplicationFrame; 19 import org.jfree.ui.RectangleInsets; 20 import org.jfree.ui.RefineryUtilities; 21 22 import com.tctest.performance.generate.load.Measurement; 23 24 import java.awt.Color ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.List ; 28 29 import javax.swing.JPanel ; 30 31 36 public class ResultsGraph extends ApplicationFrame { 37 38 private static final Color [] colors = new Color [] { Color.red, Color.blue, Color.green, Color.orange, Color.cyan, 39 Color.magenta, Color.yellow, Color.pink }; 40 41 public ResultsGraph(PerformanceMeasurementMarshaller.Statistics stats, boolean save) { 42 super(stats.header.title + " - duration: " + stats.header.duration + " secs."); 43 ChartPanel chartPanel = (ChartPanel) createDemoPanel(stats, save); 44 chartPanel.setPreferredSize(new java.awt.Dimension (700, 500)); 45 chartPanel.setMouseZoomable(true, false); 46 setContentPane(chartPanel); 47 } 48 49 private static JFreeChart createChart(XYDataset dataset, PerformanceMeasurementMarshaller.Header header) { 50 JFreeChart chart = ChartFactory.createXYLineChart(header.title, header.xLabel, header.yLabel, dataset, 51 PlotOrientation.VERTICAL, true, true, false); 52 53 chart.setAntiAlias(true); 54 chart.setBackgroundPaint(Color.white); 55 chart.setPadding(new RectangleInsets(20.0, 20.0, 20.0, 20.0)); 56 57 XYPlot plot = (XYPlot) chart.getPlot(); 58 XYLineAndShapeRenderer xyColors = (XYLineAndShapeRenderer) plot.getRenderer(); 59 int xyCount = plot.getSeriesCount(); 60 for (int i = 0; i < xyCount; i++) { 61 if (i == xyCount - 1) { 62 xyColors.setSeriesPaint(i, Color.LIGHT_GRAY); break; 64 } 65 xyColors.setSeriesPaint(i, colors[i]); } 67 68 plot.setBackgroundPaint(new Color (240, 240, 240)); 69 plot.setDomainGridlinePaint(Color.white); 70 plot.setRangeGridlinePaint(Color.white); 71 plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); 72 plot.setDomainAxis(new NumberAxis()); 73 plot.setDomainCrosshairVisible(true); 74 plot.setRangeCrosshairVisible(true); 75 76 XYItemRenderer r = plot.getRenderer(); 77 if (r instanceof XYLineAndShapeRenderer) { 78 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; 79 renderer.setBaseShapesVisible(false); 80 renderer.setBaseShapesFilled(true); 81 } 82 return chart; 83 } 84 85 private static XYDataset createDataset(List measurementList, String [] titles) { 86 long max = 0; long current; 88 int size = measurementList.size(); 89 if (size > colors.length) throw new RuntimeException ("Unable to draw more than " + colors.length + " lines."); 90 XYSeriesCollection dataset = new XYSeriesCollection(); 91 XYSeries xySeries; 92 Measurement[] measurements; 93 94 for (int i = size - 1; i >= 0; i--) { 95 measurements = (Measurement[]) measurementList.get(i); 96 xySeries = new XYSeries(titles[i]); 97 98 for (int j = 0; j < measurements.length; j++) { 99 current = measurements[j].y; 100 if (i != 0) { 101 if (current > max) max = current; 102 } else if (System.getProperty("clip", "false").equals("true")) { 103 if (current > max * 4) break; 104 } 105 xySeries.add(measurements[j].x, current); 106 } 107 108 dataset.addSeries(xySeries); 109 } 110 return dataset; 111 } 112 113 public static JPanel createDemoPanel(PerformanceMeasurementMarshaller.Statistics stats, boolean save) { 114 PerformanceMeasurementMarshaller.Header header = stats.header; 115 JFreeChart chart = createChart(createDataset(stats.measurements, stats.lineDescriptions), header); 116 if (save) { 117 try { 118 ChartUtilities.saveChartAsPNG(new File ("../results-graph.png"), chart, 700, 500); 119 } catch (IOException e) { 120 System.out.println("Unable to save graph image to disk"); 121 } 122 } 123 return new ChartPanel(chart); 124 } 125 126 public static void main(String [] args) throws Exception { 127 File datafile = new File (args[0]); 128 boolean save = (args.length == 2 && args[1] != null && args[1].equals("save")); 129 PerformanceMeasurementMarshaller.Statistics stats = PerformanceMeasurementMarshaller.deMarshall(datafile); 130 ResultsGraph graph = new ResultsGraph(stats, save); 131 if (save) System.exit(0); 132 graph.pack(); 133 RefineryUtilities.centerFrameOnScreen(graph); 134 graph.setVisible(true); 135 } 136 } | Popular Tags |