Skip to content

Commit

Permalink
Also dump a PNG chart for the tag report
Browse files Browse the repository at this point in the history
  • Loading branch information
Mats-SX committed Mar 24, 2016
1 parent a8a89a4 commit 36c71c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Expand Up @@ -35,30 +35,33 @@
import org.w3c.dom.Document;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;
import javax.imageio.ImageIO;

public class ChartWriter
{
private final OutputStream out;
private final File outDirectory;
private final String filename;

public ChartWriter( OutputStream out )
public ChartWriter( File outDirectory, String filename )
{
this.out = out;
this.outDirectory = outDirectory;
this.filename = filename;
}

public void dumpSVG( Map<String,Integer> data )
{
JFreeChart chart = createBarChart( data );

Document document = GenericDOMImplementation.getDOMImplementation().createDocument( null, "svg", null );
SVGGraphics2D svgGenerator = new SVGGraphics2D( document );
chart.draw( svgGenerator, new Rectangle( 1500, 500 ) );

// Write svg file
try ( OutputStreamWriter writer = new OutputStreamWriter( out ) )
createBarChart( data ).draw( svgGenerator, new Rectangle( 1500, 500 ) );

try ( OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream( new File( outDirectory, filename + ".svg" ) ) ) )
{
svgGenerator.stream( writer, true );
}
Expand All @@ -68,8 +71,26 @@ public void dumpSVG( Map<String,Integer> data )
}
}

public void dumpPNG( Map<String,Integer> data )
{
try ( FileOutputStream output = new FileOutputStream( new File( outDirectory, filename + ".png" ) ) )
{
ImageIO.write( createBarChart( data ).createBufferedImage( 1500, 500 ), "png", output );
}
catch ( IOException e )
{
throw new RuntimeException( "Unexpected error during PNG file creation", e );
}
}

private JFreeChart cached = null;

private JFreeChart createBarChart( Map<String,Integer> data )
{
if ( cached != null )
{
return cached;
}
JFreeChart chart = ChartFactory
.createBarChart( "TCK tag distribution", "Tags", "Occurrences in queries",
createCategoryDataset( data ) );
Expand All @@ -89,6 +110,7 @@ private JFreeChart createBarChart( Map<String,Integer> data )
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions( Math.PI / 6.0 ) );

cached = chart;
return chart;
}

Expand Down
Expand Up @@ -41,7 +41,7 @@ class CypherResultReporter(producer: OutputProducer, jsonWriter: PrintStream, ch
def this(reportDir: File) = {
this(producer = JsonProducer,
jsonWriter = CypherResultReporter.createPrintStream(reportDir, "compact.json"),
chartWriter = new ChartWriter(CypherResultReporter.createPrintStream(reportDir, "tag_distribution.svg")))
chartWriter = new ChartWriter(reportDir, "tags"))
}

private var query: String = null
Expand All @@ -53,6 +53,7 @@ class CypherResultReporter(producer: OutputProducer, jsonWriter: PrintStream, ch
override def done(): Unit = {
jsonWriter.println(producer.dump())
chartWriter.dumpSVG(producer.dumpTagStats())
chartWriter.dumpPNG(producer.dumpTagStats())
}

override def close(): Unit = {
Expand Down

0 comments on commit 36c71c6

Please sign in to comment.