Saltar al contenido

JFreeChart – Ejemplos BarChart

Desde un Servlet realizamos la impresión en formato PNG.

servlets01-JFreeChart

21-07-2014 6-04-15

21-07-2014 6-07-50

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**/
response.setContentType("image/png");
OutputStream out = response.getOutputStream();

//Obtenemos el grafico
//JFreeChart grafico01 = this.dibujarGrafico01();
//JFreeChart grafico02 = this.dibujarGrafico02();
JFreeChart grafico03 = this.dibujarGrafico03();

ChartUtilities.writeChartAsPNG(out, grafico03, 800, 600);

out.close();
/**/
}

public JFreeChart dibujarGrafico02(){

// create a dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

//Saldos A
for (int i = 1; i <= 24; i++) { dataset.addValue(i+1, "A", "Mes " + i); } //Saldos B for (int i = 1; i <= 24; i++) { dataset.addValue(i+2, "B", "Mes " + i); } //Saldos C for (int i = 1; i <= 24; i++) { dataset.addValue(i+3, "C", "Mes " + i); } //Saldos D for (int i = 1; i <= 24; i++) { dataset.addValue(i+4, "D", "Mes " + i); } // create a chart... JFreeChart chart = ChartFactory.createBarChart( "Bar Chart Demo - AprendeJAVA", // chart title "Meses", // domain axis label "Valor", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? ); // chart Customisation chart.setBackgroundPaint(Color.white); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setRangeGridlinePaint(Color.white); //Indicar el label vertical final CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); // Color de las barras BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setSeriesPaint(0, Color.GRAY); //Saldos A renderer.setSeriesPaint(1, Color.ORANGE); //Saldos B renderer.setSeriesPaint(1, Color.BLUE); //Saldos B renderer.setSeriesPaint(1, Color.GREEN); //Saldos B renderer.setDrawBarOutline(true); //Borde a las barras // Bar Spacing Within Categories renderer.setItemMargin(-0.5); return chart; } public JFreeChart dibujarGrafico03(){ /*==============================================================*/ // create a dataset... DefaultCategoryDataset dataset1 = new DefaultCategoryDataset(); //Saldos A for (int i = 1; i <= 24; i++) { dataset1.addValue(i+5, "A", "Mes " + i); } //Saldos B for (int i = 1; i <= 24; i++) { dataset1.addValue(i+2, "B", "Mes " + i); } // create the first renderer... final CategoryItemRenderer renderer = new BarRenderer(); final CategoryPlot plot = new CategoryPlot(); plot.setDataset(dataset1); plot.setRenderer(renderer); plot.setDomainAxis(new CategoryAxis("Eje X")); plot.setRangeAxis(new NumberAxis("Eje Y")); plot.setOrientation(PlotOrientation.VERTICAL); plot.setRangeGridlinesVisible(true); plot.setDomainGridlinesVisible(true); /*==============================================================*/ // create dataset T1 DefaultCategoryDataset datasetT1 = new DefaultCategoryDataset(); //Tipo T1 for (int i = 1; i <= 24; i++) { datasetT1.addValue(i+4.12, "T1", "Mes " + i); } // create dataset T2 DefaultCategoryDataset datasetT2 = new DefaultCategoryDataset(); //Tipo T2 for (int i = 1; i <= 24; i++) { datasetT2.addValue(i+2.5, "T2", "Mes " + i); } DefaultCategoryDataset datasetT3 = new DefaultCategoryDataset(); //Tipo T3 for (int i = 1; i <= 24; i++) { datasetT3.addValue(i+1.8, "T3", "Mes " + i); } DefaultCategoryDataset datasetT4 = new DefaultCategoryDataset(); //Tipo T4 for (int i = 1; i <= 24; i++) { datasetT4.addValue(i+3.5, "T4", "Mes " + i); } // Seteamos DATA para el eje Y derecha plot.setDataset(1, datasetT1); plot.setDataset(2, datasetT2); plot.setDataset(3, datasetT3); plot.setDataset(4, datasetT4); final CategoryItemRenderer rendererT1 = new LineAndShapeRenderer(); plot.setRenderer(1, rendererT1); final CategoryItemRenderer rendererT2 = new LineAndShapeRenderer(); plot.setRenderer(2, rendererT2); final CategoryItemRenderer rendererT3 = new LineAndShapeRenderer(); plot.setRenderer(3, rendererT3); final CategoryItemRenderer rendererT4 = new LineAndShapeRenderer(); plot.setRenderer(4, rendererT4); //Creamos un eje a la derecha final ValueAxis rangeAxis2 = new NumberAxis("Eje M"); plot.setRangeAxis(1, rangeAxis2); plot.mapDatasetToRangeAxis(2, 1); plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90); // Color de las barras BarRenderer rendererABC = (BarRenderer) plot.getRenderer(); rendererABC.setSeriesPaint(0, Color.GRAY); //Saldos A rendererABC.setSeriesPaint(1, Color.ORANGE); //Saldos B rendererABC.setDrawBarOutline(true); //Borde a las barras rendererABC.setItemMargin(-1); final JFreeChart chart = new JFreeChart(plot); chart.setTitle("Overlaid Bar Chart - AprendeJAVA"); /*==============================================================*/ return chart; }

JSP


Jfreechart : Grafico XY

Si te ha interesado este artículo y deseas un apoyo o asesoría en algún requerimiento, envíame un mensaje a: (info@juliopari.com) o sino a través de Linkedin: https://www.linkedin.com/in/juliopari/