Example usage for org.jfree.chart JFreeChart createBufferedImage

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

Introduction

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

Prototype

public BufferedImage createBufferedImage(int width, int height) 

Source Link

Document

Creates and returns a buffered image into which the chart has been drawn.

Usage

From source file:Tester.java

public static void main(String[] args) throws Exception {

    final String filename = "fcl/generated.fcl";
    final String[] linguisticTermNames = { "muuuuuuypeque", "muypeque", "peque", "normal", "grande",
            "muygrande", "muuuuygrande" };
    final RegionDistributionInfo[] linguisticTerms = new RegionDistributionInfo[linguisticTermNames.length];
    for (int i = 0; i < linguisticTermNames.length; ++i)
        linguisticTerms[i] = new RegionDistributionInfo(linguisticTermNames[i],
                1.0 / (linguisticTerms.length - 1));

    final boolean database = true;

    System.out.println("Creating dataset " + System.currentTimeMillis());

    final MobileDevices mobileDevices = createDataset(database);
    System.out.println(mobileDevices.getMobileDevices().size());

    final Map<DeviceCapability, Variable> inputVariables = new HashMap<DeviceCapability, Variable>();

    final Variable realSizeVar = new Variable("real_size", Arrays.asList(linguisticTerms));
    final Variable resoSizeVar = new Variable("reso_size", Arrays.asList(linguisticTerms));

    inputVariables.put(DeviceCapability.real_size, realSizeVar);
    inputVariables.put(DeviceCapability.reso_size, resoSizeVar);

    final Map<String, Variable> outputVariables = new HashMap<String, Variable>();
    outputVariables.put("hey", new Variable("hey", Arrays.asList(new RegionDistributionInfo("ho", 1.0 / 2),
            new RegionDistributionInfo("lets", 1.0 / 2), new RegionDistributionInfo("go", 1.0 / 2))));
    final String rules = "// the rules \n";

    final FclCreator creator = new FclCreator();
    System.out.println("Creating rule file " + System.currentTimeMillis());

    final WarningStore warningStore = new WarningStore();
    final String fileContent = creator.createRuleFile("prueba", inputVariables,
            new HashMap<UserCapability, Variable>(), outputVariables, mobileDevices, rules, warningStore);
    warningStore.print();//www  . j  a  v  a  2s. c om
    final File file = new File(filename);
    file.createNewFile();
    System.out.println("Dumping the rule file " + System.currentTimeMillis());
    FileUtils.writeStringToFile(file, fileContent);

    System.out.println("Processing the file " + System.currentTimeMillis());
    final FIS fis = FIS.load(filename, true);
    net.sourceforge.jFuzzyLogic.rule.Variable realSize = fis.getVariable("real_size");

    JFreeChart theChart = realSize.chart(false);
    @SuppressWarnings("unused")
    BufferedImage img = theChart.createBufferedImage(1000, 1000);

    /*
    FileOutputStream fos = new FileOutputStream("imagen.png");
    ImageEncoder myEncoder = ImageEncoderFactory.newInstance("png");
    myEncoder.encode(img, fos);
    fos.flush();
    fos.close();
    */

    fis.chart();

}

From source file:org.martus.client.swingui.dialogs.UiChartPreviewDlg.java

public static JLabel createChartComponent(JFreeChart chart) {
    BufferedImage image = chart.createBufferedImage(800, 600);
    ImageIcon imageIcon = new ImageIcon(image);
    JLabel label = new JLabel(imageIcon);
    return label;
}

From source file:org.optaplanner.benchmark.impl.statistic.common.GraphSupport.java

public static void writeChartToImageFile(JFreeChart chart, File chartFile) {
    BufferedImage chartImage = chart.createBufferedImage(1024, 768);
    OutputStream out = null;// w ww . j  a v a  2s.c om
    try {
        out = new FileOutputStream(chartFile);
        ImageIO.write(chartImage, "png", out);
    } catch (IOException e) {
        throw new IllegalArgumentException("Problem writing chartFile: " + chartFile, e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.limy.common.graph.GraphUtils.java

/**
 * PNG??????/*from w ww.  ja va 2  s.c o m*/
 * @param chart ?
 * @param pngFile png
 * @param width width
 * @param height height
 * @throws IOException I/O
 */
public static void writeImagePng(JFreeChart chart, File pngFile, int width, int height) throws IOException {

    BufferedImage image = chart.createBufferedImage(width, height);
    FileOutputStream outStream = new FileOutputStream(pngFile);
    try {
        PngEncoder pngEncoder = new PngEncoder(image);
        pngEncoder.setCompressionLevel(9);
        outStream.write(pngEncoder.pngEncode());
    } finally {
        outStream.close();
    }
}

From source file:com.gallatinsystems.common.util.JFreechartChartUtil.java

/**
 * generates a pie chart with the set of labels and values passed in (the labels and values
 * arrays must both be non-null and contain the same number of elements). The chart is returned
 * as a byte array representing the image.
 * //from www. j  a v  a2 s  .  c  o m
 * @param labels
 * @param values
 * @param title
 * @param width
 * @param height
 * @return - byte array containing the image, null if there is an error.
 */
public static byte[] getPieChart(List<String> labels, List<String> values, String title, int width,
        int height) {
    DefaultPieDataset pieDataset = new DefaultPieDataset();

    for (int i = 0; i < labels.size(); i++) {
        pieDataset.setValue(labels.get(i) + " (" + values.get(i) + ")", Double.parseDouble(values.get(i)));
    }
    JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false);
    try {
        return ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height));
    } catch (Exception e) {
        e.printStackTrace(System.err);
        return null;
    }

}

From source file:org.talend.dataprofiler.chart.util.ChartUtils.java

public static ImageDescriptor convertToImage(JFreeChart chart, int maxWidth, int maxHeight) throws IOException {
    BufferedImage image = chart.createBufferedImage(maxWidth, maxHeight);
    ImageDescriptor descriptor = bufferedToDescriptorOptimized(image);
    return descriptor;
}

From source file:com.glaf.chart.util.ChartUtils.java

public static void createChart(String path, Chart chartModel, JFreeChart chart) {
    try {//from  w w w  .  jav  a 2s. c  o  m
        java.awt.image.BufferedImage bi = chart.createBufferedImage(chartModel.getChartWidth(),
                chartModel.getChartHeight());

        String name = chartModel.getChartName();
        if (StringUtils.isNotEmpty(chartModel.getMapping())) {
            name = chartModel.getMapping();
        }

        if ("png".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(),
                    new FileOutputStream(path + "/" + name + ".png"));
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsPNG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        } else if ("jpeg".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(),
                    new FileOutputStream(path + "/" + name + ".jpg"));
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsJPEG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}

From source file:inflor.core.utils.ChartUtils.java

public static byte[] renderChartToPNG(JFreeChart jfc, int x, int y) throws IOException {
    jfc.setBackgroundPaint(Color.WHITE);
    BufferedImage objBufferedImage = jfc.createBufferedImage(x, y);
    ByteArrayOutputStream bas = new ByteArrayOutputStream();
    ImageIO.write(objBufferedImage, "png", bas);
    return bas.toByteArray();
}

From source file:com.church.tools.ChartTools.java

/**
 * Generate pie chart./*  w w w  .  j  av  a 2 s  . c  o  m*/
 * 
 * @param title the title
 * @param values the values
 * @param captions the captions
 * @param width the width
 * @param height the height
 * @param color the color
 * 
 * @return the buffered image
 */
public static BufferedImage GeneratePieChart(String title, double[] values, String[] captions, int width,
        int height, Color color) {
    BufferedImage bufferedImage = null;
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    Hashtable<String, String> ht = new Hashtable<String, String>();
    for (int i = 0; i < values.length; i++)
        ht.put(captions[i], Double.toString(values[i]));

    Enumeration<String> enu = ht.keys();
    int i = 0;
    while (enu.hasMoreElements()) {
        String str = (String) enu.nextElement();
        pieDataset.setValue(str, new Double(Double.parseDouble((String) ht.get(str))));
        i++;
    }

    JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false);
    chart.setBackgroundPaint(color);
    bufferedImage = chart.createBufferedImage(width, height);
    return bufferedImage;
}

From source file:erpsystem.chart.Charts.java

/**Gafico criado para mostrar 
o faturamento considerando o // w  ww  . jav a  2s  .  c  o m
valor bruto das sadas das compras R$
e valor bruto das entradas das vendas R$
realizadas no perodo especificado
Tambm  de responsabilidade do grfico
monstrar graficamente o valor do lucro.
*/

public static BufferedImage create001(int w, int h, Calendar initialCalendar, Calendar finalCalendar) {
    DefaultCategoryDataset ds = new DefaultCategoryDataset();
    String COMPRA = "Compra R$";
    String VENDA = "Venda R$";
    String LUCRO = "Lucro R$";

    long initialDate = initialCalendar.getTime().getTime();
    long finalDate = finalCalendar.getTime().getTime();

    //toString para depurao.
    String s1 = toString(initialCalendar);
    String s2 = toString(finalCalendar);

    double compra = erpsystem.db.chart.Chart001.getValorCompraEm(initialDate, finalDate);
    double venda = erpsystem.db.chart.Chart001.getValorVendaEm(initialDate, finalDate);
    double lucro = erpsystem.db.chart.Chart001.getValorLucroEm(initialDate, finalDate);

    ds.addValue(compra, COMPRA, COMPRA);
    ds.addValue(venda, VENDA, VENDA);
    ds.addValue(lucro, LUCRO, LUCRO);

    JFreeChart chart = ChartFactory.createBarChart3D("Faturamento", "Faturamento", "Valor R$", ds);

    return chart.createBufferedImage(w, h);
}