예제 #1
0
  protected String buildGraph(String seriesDirectory, Method<?, ?> method, List<Double> datas) {
    XYSeries series = new XYSeries("XYGraph", false, false);

    double snapshot = 0;
    for (Double data : datas) {
      double seconds = TimeUnit.NANOSECONDS.toSeconds(data.intValue());
      series.add(snapshot++, seconds);
    }

    XYSeriesCollection seriesCollection = new XYSeriesCollection();
    seriesCollection.addSeries(series);
    JFreeChart chart =
        ChartFactory.createXYLineChart(
            null,
            "Snapshots",
            "Time",
            seriesCollection,
            PlotOrientation.VERTICAL,
            false,
            false,
            false);
    chart.setTitle(new TextTitle(method.getName(), new Font("Arial", Font.BOLD, 11)));

    XYPlot xyPlot = chart.getXYPlot();
    NumberAxis yAxis = (NumberAxis) xyPlot.getRangeAxis();
    yAxis.setAutoRange(true);
    yAxis.setAutoRangeIncludesZero(true);

    NumberAxis xAxis = (NumberAxis) xyPlot.getDomainAxis();
    xAxis.setAutoRange(true);
    xAxis.setAutoRangeIncludesZero(true);
    // xAxis.setTickUnit(new NumberTickUnit(1));

    StringBuilder builder = new StringBuilder(method.getClassName());
    builder.append(method.getName());
    builder.append(method.getDescription());

    String fileName = Long.toString(Toolkit.hash(builder.toString()));
    fileName += ".jpeg";

    File chartSeriesDirectory = new File(IConstants.chartDirectory, seriesDirectory);
    File chartFile = new File(chartSeriesDirectory, fileName);
    try {
      if (!IConstants.chartDirectory.exists()) {
        //noinspection ResultOfMethodCallIgnored
        IConstants.chartDirectory.mkdirs();
      }
      if (!chartSeriesDirectory.exists()) {
        //noinspection ResultOfMethodCallIgnored
        chartSeriesDirectory.mkdirs();
      }
      if (!chartFile.exists()) {
        //noinspection ResultOfMethodCallIgnored
        chartFile.createNewFile();
      }
      ChartUtilities.saveChartAsJPEG(chartFile, chart, 450, 150);
      builder = new StringBuilder(IConstants.CHARTS);
      builder.append(File.separatorChar);
      builder.append(seriesDirectory);
      builder.append(File.separatorChar);
      builder.append(fileName);

      return builder.toString();
    } catch (Exception e) {
      logger.error("Exception generating the graph", e);
    }
    return null;
  }