Example usage for org.jfree.chart JFreeChart setBackgroundImageAlpha

List of usage examples for org.jfree.chart JFreeChart setBackgroundImageAlpha

Introduction

In this page you can find the example usage for org.jfree.chart JFreeChart setBackgroundImageAlpha.

Prototype

public void setBackgroundImageAlpha(float alpha) 

Source Link

Document

Sets the alpha-transparency for the chart's background image.

Usage

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * This method is used to save all image formats. it uses the specific methods for each file
 * format//from ww w.  j av  a 2 s  . com
 * 
 * @param chart
 * @param sett
 * @param chartRenderingInfo
 */
private static void writeChartToImage(JFreeChart chart, GraphicsExportParameters sett, ChartRenderingInfo info)
        throws Exception {
    // Background color
    Paint saved = chart.getBackgroundPaint();
    chart.setBackgroundPaint(sett.getColorWithAlpha());
    chart.setBackgroundImageAlpha((float) sett.getTransparency());
    if (chart.getLegend() != null)
        chart.getLegend().setBackgroundPaint(sett.getColorWithAlpha());
    // legends and stuff
    for (int i = 0; i < chart.getSubtitleCount(); i++)
        if (PaintScaleLegend.class.isAssignableFrom(chart.getSubtitle(i).getClass()))
            ((PaintScaleLegend) chart.getSubtitle(i)).setBackgroundPaint(sett.getColorWithAlpha());

    // apply bg
    chart.getPlot().setBackgroundPaint(sett.getColorWithAlpha());

    // create folder
    File f = sett.getFullpath();
    if (!f.exists()) {
        if (f.getParentFile() != null)
            f.getParentFile().mkdirs();
        // f.createNewFile();
    }

    Dimension size = sett.getPixelSize();
    // Format
    switch (sett.getFormat()) {
    case "PDF":
        writeChartToPDF(chart, size.width, size.height, f);
        break;
    case "PNG":
        writeChartToPNG(chart, info, size.width, size.height, f, (int) sett.getDPI());
        break;
    case "JPG":
        writeChartToJPEG(chart, info, size.width, size.height, f, (int) sett.getDPI());
        break;
    case "EPS":
        writeChartToEPS(chart, size.width, size.height, f);
        break;
    case "SVG":
        writeChartToSVG(chart, size.width, size.height, f);
        break;
    case "EMF":
        writeChartToEMF(chart, size.width, size.height, f);
        break;
    }
    //
    chart.setBackgroundPaint(saved);
    chart.setBackgroundImageAlpha(255);
    if (chart.getLegend() != null)
        chart.getLegend().setBackgroundPaint(saved);
    // legends and stuff
    for (int i = 0; i < chart.getSubtitleCount(); i++)
        if (PaintScaleLegend.class.isAssignableFrom(chart.getSubtitle(i).getClass()))
            ((PaintScaleLegend) chart.getSubtitle(i)).setBackgroundPaint(saved);

    // apply bg
    chart.getPlot().setBackgroundPaint(saved);
}

From source file:org.gbif.portal.web.util.ChartUtils.java

/**
 * Writes out the image using the supplied file name.
 * // w w  w . j a v  a 2s .  com
 * @param legend
 * @param fileName
 * @return
 */
public static String writePieChartImageToTempFile(Map<String, Double> legend, String fileName) {

    String filePath = System.getProperty(tmpDirSystemProperty) + File.separator + fileName + defaultExtension;
    File fileToCheck = new File(filePath);
    if (fileToCheck.exists()) {
        return fileName + defaultExtension;
    }

    final DefaultPieDataset data = new DefaultPieDataset();
    Set<String> keys = legend.keySet();
    for (String key : keys) {
        logger.info("Adding key : " + key);
        data.setValue(key, legend.get(key));
    }

    // create a pie chart...
    final boolean withLegend = true;
    final JFreeChart chart = ChartFactory.createPieChart(null, data, withLegend, false, false);

    PiePlot piePlot = (PiePlot) chart.getPlot();
    piePlot.setLabelFont(new Font("Arial", Font.PLAIN, 10));
    piePlot.setLabelBackgroundPaint(Color.WHITE);

    LegendTitle lt = chart.getLegend();
    lt.setBackgroundPaint(Color.WHITE);
    lt.setWidth(300);
    lt.setBorder(0, 0, 0, 0);
    lt.setItemFont(new Font("Arial", Font.PLAIN, 11));

    chart.setPadding(new RectangleInsets(0, 0, 0, 0));
    chart.setBorderVisible(false);
    chart.setBackgroundImageAlpha(0);
    chart.setBackgroundPaint(Color.WHITE);
    chart.setBorderPaint(Color.LIGHT_GRAY);

    final BufferedImage image = new BufferedImage(300, 250, BufferedImage.TYPE_INT_RGB);
    KeypointPNGEncoderAdapter adapter = new KeypointPNGEncoderAdapter();
    adapter.setQuality(1);
    try {
        adapter.encode(image);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    final Graphics2D g2 = image.createGraphics();
    g2.setFont(new Font("Arial", Font.PLAIN, 11));
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 300, 250);

    // draw
    chart.draw(g2, chartArea, null, null);

    try {
        FileOutputStream fOut = new FileOutputStream(fileToCheck);
        ChartUtilities.writeChartAsPNG(fOut, chart, 300, 250);
        return fileToCheck.getName();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

public static void writeChartToJPEG(JFreeChart chart, ChartRenderingInfo info, int width, int height,
        File fileName, int resolution) throws IOException {
    // Background color
    Paint saved = chart.getBackgroundPaint();
    if (((Color) saved).getAlpha() == 0) {
        chart.setBackgroundPaint(Color.WHITE);
        chart.setBackgroundImageAlpha(255);
        if (chart.getLegend() != null)
            chart.getLegend().setBackgroundPaint(Color.WHITE);
        // legends and stuff
        for (int i = 0; i < chart.getSubtitleCount(); i++)
            if (PaintScaleLegend.class.isAssignableFrom(chart.getSubtitle(i).getClass()))
                ((PaintScaleLegend) chart.getSubtitle(i)).setBackgroundPaint(Color.WHITE);

        // apply bg
        chart.getPlot().setBackgroundPaint(Color.WHITE);
    }/*from ww w  .  j  a v a  2  s  .c om*/
    //
    if (resolution == 72)
        writeChartToJPEG(chart, width, height, fileName);
    else {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
        try {
            BufferedImage image = paintScaledChartToBufferedImage(chart, info, out, width, height, resolution,
                    BufferedImage.TYPE_INT_RGB);
            EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, 1.f);
        } finally {
            out.close();
        }
    }
}

From source file:org.sonar.server.charts.deprecated.BaseChart.java

protected void configureChart(JFreeChart chart, RectangleEdge legendPosition) {
    chart.setBackgroundPaint(new Color(255, 255, 255, 0));
    chart.setBackgroundImageAlpha(0.0f);
    chart.setBorderVisible(false);//from w  w w .ja v  a 2s  . co m
    chart.setAntiAlias(true);
    chart.setTextAntiAlias(true);

    chart.removeLegend();
    if (legendPosition != null) {
        LegendTitle legend = new LegendTitle(chart.getPlot());
        legend.setPosition(legendPosition);
        legend.setItemPaint(BASE_COLOR);
        chart.addSubtitle(legend);
    }
}

From source file:de.perdian.apps.dashboard.support.chart.ChartCreator.java

public JFreeChart createChart(XYDataset dataset) {

    JFreeChart chart = ChartFactory.createXYLineChart(this.getTitle(), null, null, dataset,
            PlotOrientation.VERTICAL, false, false, false);
    chart.setBackgroundPaint(null);// w w w  . j a v  a  2 s .  c om
    chart.setBackgroundImageAlpha(0.0f);
    if (chart.getTitle() != null) {
        chart.getTitle().setFont(new Font(this.getFontName(), Font.BOLD, 14));
    }

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(null);
    plot.setBackgroundAlpha(0.0f);
    plot.setOutlinePaint(this.getColor());
    plot.getDomainAxis().setVisible(false);
    plot.getDomainAxis().setAxisLineVisible(false);
    plot.getDomainAxis().setMinorTickCount(10);
    plot.getDomainAxis().setTickMarksVisible(false);
    if (this.getDomainTickUnits() != null) {
        plot.getDomainAxis().setStandardTickUnits(this.getDomainTickUnits());
    }
    plot.getRangeAxis().setVisible(true);
    plot.getRangeAxis().setAxisLineVisible(false);
    plot.getRangeAxis().setTickLabelFont(new Font(this.getFontName(), Font.PLAIN, 10));
    plot.getRangeAxis().setTickLabelPaint(this.getColor());
    plot.getRangeAxis().setTickMarksVisible(false);
    if (this.getRangeTickUnits() != null) {
        plot.getRangeAxis().setStandardTickUnits(this.getRangeTickUnits());
    }
    plot.getRangeAxis().setAttributedLabel(this.getRangeAxisLabel());

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
    for (int i = 0; i < this.getStrokeDefinitions().size(); i++) {
        ChartStrokeDefinition strokeDefinition = this.getStrokeDefinitions().get(i);
        strokeDefinition.applyTo(renderer, i);
    }

    return chart;

}

From source file:de.suse.swamp.modules.scheduledjobs.Statistics.java

/**
 * Generating the graphs that show the amount of running finished wfs over the time.
 *//*from ww w  .ja v a  2s .  c o m*/
protected void generateWorkflowGraph(String templateName, Date startDate, Date endDate) throws Exception {
    List stats = StatisticStorage.loadStats(templateName, startDate, endDate);
    // only generate if we have stats: 
    if (stats != null && stats.size() > 0) {
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        TimeSeriesCollection avgdataset = new TimeSeriesCollection();
        TimeSeries serie = new TimeSeries("running workflows", Day.class);
        TimeSeries avgserie = new TimeSeries("average age", Day.class);
        for (Iterator datait = stats.iterator(); datait.hasNext();) {
            Dbstatistics statisticItem = (Dbstatistics) datait.next();
            serie.addOrUpdate(new Day(statisticItem.getDate()), statisticItem.getRunningcount());
            avgserie.addOrUpdate(new Day(statisticItem.getDate()), statisticItem.getAvgage() / (3600 * 24));
        }
        dataset.addSeries(serie);
        avgdataset.addSeries(avgserie);

        JFreeChart chart = ChartFactory.createTimeSeriesChart("Running " + templateName + " workflows", "Date",
                "running workflows", dataset, false, false, false);

        // modify chart appearance
        chart.setBackgroundImageAlpha(0.5f);
        XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.getRangeAxis().setLabelPaint(Color.blue);

        // add the second line: 
        final NumberAxis axis2 = new NumberAxis("Avg. age in days");
        axis2.setLabelPaint(Color.red);
        plot.setRangeAxis(1, axis2);
        plot.setDataset(1, avgdataset);
        plot.mapDatasetToRangeAxis(1, 1);

        final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
        renderer2.setDrawOutlines(false);
        renderer2.setDrawSeriesLineAsPath(true);
        renderer2.setBaseShapesVisible(false);
        plot.setRenderer(1, renderer2);

        File image = new File(statPath + fs + templateName + ".png");
        if (image.exists())
            image.delete();
        try {
            ChartUtilities.saveChartAsPNG(image, chart, 750, 200);
        } catch (Exception e) {
            Logger.ERROR("Error generating graph for " + templateName + ", e: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
}

From source file:com.rapidminer.gui.viewer.metadata.model.NumericalAttributeStatisticsModel.java

/**
 * Creates the histogram chart.//www.ja  va  2s  .  c  o  m
 *
 * @param exampleSet
 * @return
 */
private JFreeChart createHistogramChart(ExampleSet exampleSet) {
    JFreeChart chart = ChartFactory.createHistogram(null, null, null, createHistogramDataset(exampleSet),
            PlotOrientation.VERTICAL, false, false, false);
    AbstractAttributeStatisticsModel.setDefaultChartFonts(chart);
    chart.setBackgroundPaint(null);
    chart.setBackgroundImageAlpha(0.0f);

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setRangeGridlinesVisible(false);
    plot.setDomainGridlinesVisible(false);
    plot.setOutlineVisible(false);
    plot.setRangeZeroBaselineVisible(false);
    plot.setDomainZeroBaselineVisible(false);
    plot.setBackgroundPaint(COLOR_INVISIBLE);
    plot.setBackgroundImageAlpha(0.0f);

    XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
    renderer.setSeriesPaint(0, AttributeGuiTools.getColorForValueType(Ontology.NUMERICAL));
    renderer.setBarPainter(new StandardXYBarPainter());
    renderer.setDrawBarOutline(true);
    renderer.setShadowVisible(false);

    return chart;
}

From source file:com.rapidminer.gui.viewer.metadata.model.NominalAttributeStatisticsModel.java

/**
 * Creates the histogram chart./* w  w  w.  j a va 2s.  co  m*/
 * 
 * @return
 */
private JFreeChart createBarChart() {
    JFreeChart chart = ChartFactory.createBarChart(null, null, null, createBarDataset(),
            PlotOrientation.VERTICAL, false, false, false);
    AbstractAttributeStatisticsModel.setDefaultChartFonts(chart);
    chart.setBackgroundPaint(null);
    chart.setBackgroundImageAlpha(0.0f);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setRangeGridlinesVisible(false);
    plot.setDomainGridlinesVisible(false);
    plot.setOutlineVisible(false);
    plot.setRangeZeroBaselineVisible(false);
    plot.setDomainGridlinesVisible(false);
    plot.setBackgroundPaint(COLOR_INVISIBLE);
    plot.setBackgroundImageAlpha(0.0f);

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setSeriesPaint(0, AttributeGuiTools.getColorForValueType(Ontology.NOMINAL));
    renderer.setBarPainter(new StandardBarPainter());
    renderer.setDrawBarOutline(true);
    renderer.setShadowVisible(false);

    return chart;
}

From source file:tdunnick.jphineas.console.queue.Charts.java

private BufferedImage getJFreeImage(JFreeChart image, int width, int height, ChartRenderingInfo info) {
    image.getTitle().setFont(new Font("Times New Roman", Font.BOLD, 16));
    image.setBackgroundPaint(new Color(255, 255, 255, 0));
    image.setBackgroundImageAlpha(0.0f);
    return (image.createBufferedImage(width, height, info));
}

From source file:com.rapidminer.template.gui.RoleRequirementSelector.java

private JFreeChart makeChart(ExampleSet exampleSet, Attribute att) {
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    if (att.isNominal()) {
        for (String val : att.getMapping().getValues()) {
            pieDataset.setValue(val, exampleSet.getStatistics(att, Statistics.COUNT, val));
        }//www .ja v  a 2s  .  c om
    }
    JFreeChart chart = ChartFactory.createPieChart(null, pieDataset, true, false, false);
    chart.setBackgroundPaint(Color.WHITE);
    chart.getLegend().setFrame(BlockBorder.NONE);
    chart.setBackgroundImageAlpha(0.0f);
    chart.setBorderVisible(false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelGenerator(null);
    plot.setShadowPaint(null);
    plot.setOutlineVisible(false);
    plot.setBackgroundPaint(new Color(255, 255, 255, 0));
    plot.setBackgroundImageAlpha(0.0f);
    plot.setCircular(true);
    return chart;
}