lunes, 30 de mayo de 2011

Ejemplo 3: Colocando una imagen de fondo a nuestro Chart



Después de haber instalado las librerías en el proyecto(Ejemplo 1).
Creamos un objeto image para almacenar una imagen
BufferedImage img = ImageIO.read(new File("src/img/camaleon.jpg"));

agregamos a nuestro chart usando setBackgroundImage que nos pide como parámetro un objeto image
chart.setBackgroundImage(img);
y el resultado es este...



Ahora no se vé muy bien, pero también podemos colocar una imagen al plot de la misma forma que lo hicimos con el chart
plot.setBackgroundImage(img);

si queremos difuminar las barras del histograma para que se vea el ojo del camaleón


Ahora sólo nos queda ver el vídeo.


package jfree;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Histograma extends ApplicationFrame {
public Histograma(String title) throws IOException {
super(title);
JPanel chartPanel = crearPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 475));
setContentPane(chartPanel);
}
private static IntervalXYDataset crearDataset() {
HistogramDataset dataset = new HistogramDataset();
//vecto almacena los ingresos quincenales de 45 personas
double vector[] = {63, 89, 36, 49, 56, 64, 59, 35, 78,
43, 53, 70, 57, 62, 43, 68, 62, 26,
64, 72, 52, 51, 62, 60, 71, 61, 55,
59, 60, 67, 57, 67, 61, 67, 51, 81,
53, 64, 76, 44, 73, 56, 62, 63, 60};
//En el ejercicio nos piden construir una distribución de frecuencias de 8 intervalos
//Por eso ponemos 8 en el tercer parámetro del addSeries
dataset.addSeries("Frecuencias de los ingresos", vector, 8);
return dataset;
}
private static JFreeChart crearChart(IntervalXYDataset dataset) throws IOException {
JFreeChart chart = ChartFactory.createHistogram(
"Histograma",
null,
null,
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
XYPlot plot = (XYPlot) chart.getPlot();
XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);

BufferedImage img = ImageIO.read(new File("src/img/camaleon.jpg"));
chart.setBackgroundImage(img);
plot.setBackgroundImage(img);
plot.setForegroundAlpha(0.5f);

try{
ChartUtilities.saveChartAsJPEG(new File("C:\\histograma.jpg"), chart, 500, 475);
}
catch(IOException e){
System.out.println("Error al abrir el archivo");
}
return chart;
}
public static JPanel crearPanel() throws IOException {
JFreeChart chart = crearChart(crearDataset());
return new ChartPanel(chart);
}
public static void main(String[] args) throws IOException {
Histograma histo = new Histograma("Histograma");
histo.pack();
RefineryUtilities.centerFrameOnScreen(histo);
histo.setVisible(true);
}
}








Espero te haya ayudado este tutorial, visita mi nuevo proyecto Test de Velocidad en el Teclado http://clubtyping.com/es

No hay comentarios:

Publicar un comentario