|
| 1 | +package com.microWorkflow.jsonScalaPerftest.output |
| 2 | + |
| 3 | +import scala.collection.immutable.HashMap |
| 4 | +import com.microWorkflow.jsonScalaPerftest.Measurement |
| 5 | +import org.jfree.data.category.{CategoryDataset, DefaultCategoryDataset} |
| 6 | +import org.jfree.ui.{RefineryUtilities, ApplicationFrame} |
| 7 | +import org.jfree.chart.{ChartFactory, JFreeChart, ChartPanel} |
| 8 | +import scala.Predef._ |
| 9 | +import org.jfree.chart.plot.PlotOrientation |
| 10 | +import org.jfree.chart.axis.{CategoryLabelPositions, NumberAxis} |
| 11 | +import org.jfree.chart.renderer.category.BarRenderer |
| 12 | +import java.awt.image.BufferedImage |
| 13 | +import java.io.FileOutputStream |
| 14 | +import java.awt.geom.Rectangle2D |
| 15 | +import com.sun.image.codec.jpeg.JPEGCodec |
| 16 | + |
| 17 | + |
| 18 | +class ChartReporter(title: String, results: Array[(String, HashMap[String, Measurement])]) extends ApplicationFrame(title) { |
| 19 | + |
| 20 | + import java.awt.Color |
| 21 | + import java.awt.Dimension |
| 22 | + |
| 23 | + val graphDataSet = createDataset() |
| 24 | + |
| 25 | + val chart = createChart(graphDataSet) |
| 26 | + val chartPanel = new ChartPanel(chart) |
| 27 | + chartPanel.setPreferredSize(new Dimension(500, 270)) |
| 28 | + setContentPane(chartPanel) |
| 29 | + |
| 30 | + def createDataset(): DefaultCategoryDataset = { |
| 31 | + val categoryDataSet = new DefaultCategoryDataset() |
| 32 | + |
| 33 | + for ((category, datasets) <- results) { |
| 34 | + for ((dataset, measurement) <- datasets) { |
| 35 | + categoryDataSet.addValue(measurement.value, measurement.name, dataset) |
| 36 | + } |
| 37 | + } |
| 38 | + categoryDataSet |
| 39 | + } |
| 40 | + |
| 41 | + def createChart(dataset: CategoryDataset): JFreeChart = { |
| 42 | + |
| 43 | + // create the chart... |
| 44 | + val chart = ChartFactory.createBarChart( |
| 45 | + title, // chart title |
| 46 | + "JSON Data set", // domain axis label |
| 47 | + "Time (ms)", // range axis label |
| 48 | + dataset, // data |
| 49 | + PlotOrientation.VERTICAL, // orientation |
| 50 | + true, // include legend |
| 51 | + true, // tooltips? |
| 52 | + false // URLs? |
| 53 | + ) |
| 54 | + |
| 55 | + // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... |
| 56 | + |
| 57 | + // set the background color for the chart... |
| 58 | + chart.setBackgroundPaint(Color.white) |
| 59 | + |
| 60 | + // get a reference to the plot for further customisation... |
| 61 | + val plot = chart.getCategoryPlot() |
| 62 | + plot.setBackgroundPaint(Color.lightGray) |
| 63 | + plot.setDomainGridlinePaint(Color.white) |
| 64 | + plot.setRangeGridlinePaint(Color.white) |
| 65 | + |
| 66 | + // set the range axis to display integers only... |
| 67 | + val rangeAxis = plot.getRangeAxis().asInstanceOf[NumberAxis] |
| 68 | + rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()) |
| 69 | + |
| 70 | + // disable bar outlines... |
| 71 | + val renderer = plot.getRenderer().asInstanceOf[BarRenderer] |
| 72 | + renderer.setDrawBarOutline(false) |
| 73 | + renderer.setShadowVisible(false) |
| 74 | + |
| 75 | + val domainAxis = plot.getDomainAxis() |
| 76 | + domainAxis.setCategoryLabelPositions( |
| 77 | + CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0) |
| 78 | + ) |
| 79 | + chart |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + def view { |
| 84 | + this.pack() |
| 85 | + RefineryUtilities.centerFrameOnScreen(this) |
| 86 | + this.setVisible(true) |
| 87 | + } |
| 88 | + |
| 89 | + def draw(chart: JFreeChart, width: Int, height: Int): BufferedImage = { |
| 90 | + val img = |
| 91 | + new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) |
| 92 | + val g2 = img.createGraphics() |
| 93 | + |
| 94 | + chart.draw(g2, new Rectangle2D.Double(0, 0, width, height)) |
| 95 | + |
| 96 | + g2.dispose() |
| 97 | + img |
| 98 | + } |
| 99 | + |
| 100 | + def saveToFile( |
| 101 | + aFileName: String, |
| 102 | + width: Int, |
| 103 | + height: Int, |
| 104 | + quality: Double) { |
| 105 | + val img = draw(chart, width, height) |
| 106 | + |
| 107 | + val fos = new FileOutputStream(aFileName) |
| 108 | + val encoder2 = |
| 109 | + JPEGCodec.createJPEGEncoder(fos) |
| 110 | + val param2 = encoder2.getDefaultJPEGEncodeParam(img) |
| 111 | + param2.setQuality(quality.toFloat, true) |
| 112 | + encoder2.encode(img, param2) |
| 113 | + fos.close() |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments