Skip to content

Commit

Permalink
export graph to PNG fature using the GCViewer GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Àngel Ollé Blázquez authored and chewiebug committed May 3, 2015
1 parent 97c9a25 commit 99ca2f2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tagtraum.perf.gcviewer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -82,7 +83,7 @@ private void exportType(GCModel model, String summaryFilePath, DataWriterType ty

private void renderChart(GCModel model, String chartFilePath) throws IOException {
SimpleChartRenderer renderer = new SimpleChartRenderer();
renderer.render(model, chartFilePath);
renderer.render(model, new FileOutputStream(new File(chartFilePath)));
}

private static void usage() {
Expand Down
69 changes: 36 additions & 33 deletions src/main/java/com/tagtraum/perf/gcviewer/SimpleChartRenderer.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.tagtraum.perf.gcviewer;

import com.tagtraum.perf.gcviewer.model.GCModel;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import com.tagtraum.perf.gcviewer.model.GCModel;

public class SimpleChartRenderer {
private static final Logger LOGGER = Logger.getLogger(SimpleChartRenderer.class.getName());

public void render(GCModel model, String chartFilePath) throws IOException {
public void render(GCModel model, FileOutputStream outputStream) throws IOException {
GCPreferences gcPreferences = new GCPreferences();
gcPreferences.load();
Dimension d = new Dimension(gcPreferences.getWindowWidth(), gcPreferences.getWindowHeight());
Expand All @@ -26,30 +28,31 @@ public void render(GCModel model, String chartFilePath) throws IOException {
graphics.setBackground(Color.WHITE);
graphics.clearRect(0, 0, image.getWidth(), image.getHeight());
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
ModelChartImpl pane = new ModelChartImpl();
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

pane.setModel(model, gcPreferences);
pane.setFootprint(model.getFootprint());
pane.setMaxPause(model.getPause().getMax());
pane.setRunningTime(model.getRunningTime());

pane.setSize(d);
pane.addNotify();
pane.validate();

pane.autoSetScaleFactor();
pane.paint(graphics);
}
});
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to paint graphic", e);
}


ImageIO.write(image, "png", new File(chartFilePath));
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
ModelChartImpl pane = new ModelChartImpl();
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

pane.setModel(model, gcPreferences);
pane.setFootprint(model.getFootprint());
pane.setMaxPause(model.getPause().getMax());
pane.setRunningTime(model.getRunningTime());

pane.setSize(d);
pane.addNotify();
pane.validate();

pane.autoSetScaleFactor();
pane.paint(graphics);
}
});
}
catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to paint graphic", e);
}

ImageIO.write(image, "png", outputStream);
outputStream.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Export(final GCViewerGui gcViewer) {
saveDialog.addChoosableFileFilter(new ExtensionFileFilter(".txt", LocalisationHelper.getString("fileexport_dialog_txt"), DataWriterType.PLAIN));
saveDialog.addChoosableFileFilter(new ExtensionFileFilter(".simple.log", LocalisationHelper.getString("fileexport_dialog_simplelog"), DataWriterType.SIMPLE));
saveDialog.addChoosableFileFilter(new ExtensionFileFilter(".csv", LocalisationHelper.getString("fileexport_dialog_summarylog"), DataWriterType.SUMMARY));
saveDialog.addChoosableFileFilter(new ExtensionFileFilter(".png", LocalisationHelper.getString("fileexport_dialog_png"), DataWriterType.PNG));
}

public void actionPerformed(final ActionEvent e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public enum DataWriterType {
CSV,
CSV_TS,
SIMPLE,
SUMMARY;
SUMMARY,
PNG;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static DataWriter getDataWriter(File file, DataWriterType type, Map<Strin
case CSV_TS : return new CSVTSDataWriter(outputStream);
case SIMPLE : return new SimpleGcWriter(outputStream);
case SUMMARY : return new SummaryDataWriter(outputStream, configuration);
case PNG : return new PNGDataWriter(outputStream);
default : throw new IOException(LocalisationHelper.getString("datawriterfactory_instantiation_failed") + " " + file);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.tagtraum.perf.gcviewer.exp.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.tagtraum.perf.gcviewer.SimpleChartRenderer;
import com.tagtraum.perf.gcviewer.exp.AbstractDataWriter;
import com.tagtraum.perf.gcviewer.model.GCModel;

/**
* PNG data writter
*
* @author Angel Olle Blazquez
*
*/
public class PNGDataWriter extends AbstractDataWriter {
private FileOutputStream out;

public PNGDataWriter(OutputStream outputStream) {
super(outputStream);
out = (FileOutputStream)outputStream;
}

@Override
public void write(GCModel model) throws IOException {
new SimpleChartRenderer().render(model, out);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ fileexport_dialog_csv_ts = Comma separated values with unix timestamp (*.csv)

fileexport_dialog_error_occured = An error occured.

fileexport_dialog_png = PNG Image (*.png)

fileexport_dialog_simplelog = Simple GC Log (GCHisto compatible, *.simple.log)

fileexport_dialog_summarylog = Summary GC Log (*.csv)
Expand Down

0 comments on commit 99ca2f2

Please sign in to comment.