private BufferedImage generarGraficoLineal( String titulo, String ejeHorizontal, String ejeVertical, double[] valores, String[] funciones, boolean porcentaje, boolean leyenda, int anchoImagen, int altoImagen) { XYSeries frecObservadas = new XYSeries("Cantidades de resoluciones por calificación"); // Calculamos las frecuencias por intervalo for (int i = 0; i < valores.length; i++) { frecObservadas.add(i, valores[i]); } XYDataset dataset = new XYSeriesCollection(frecObservadas); // Creamos el gráfico JFreeChart chart = ChartFactory.createXYLineChart( titulo, ejeHorizontal, ejeVertical, null, PlotOrientation.VERTICAL, leyenda, false, true); // Seteamos color de título chart.setBackgroundPaint(null); chart.setBorderVisible(false); chart.getTitle().setPaint(LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL); chart.getTitle().setFont(LookAndFeelEntropy.FUENTE_TITULO_GRANDE); if (leyenda) { chart.getLegend().setItemFont(LookAndFeelEntropy.FUENTE_REGULAR); chart.getLegend().setBackgroundPaint(LookAndFeelEntropy.COLOR_TABLA_PRIMARIO); } // Seteamos datasets XYPlot xyplot = chart.getXYPlot(); xyplot.setDataset(1, dataset); xyplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); // Seteamos características de la función lineal XYItemRenderer xyLineRendererObs = new XYLineAndShapeRenderer(); xyLineRendererObs.setSeriesShape(0, new Line2D.Double(0.0, 0.0, 0.0, 0.0)); xyLineRendererObs.setSeriesStroke(0, new BasicStroke(2.5f)); xyLineRendererObs.setBasePaint(generarColorAleatorio(Color.orange)); xyplot.setRenderer(1, xyLineRendererObs); // Seteamos color de labels de ejes ValueAxis axisX = new SymbolAxis(ejeHorizontal, funciones); axisX.setVerticalTickLabels(true); xyplot.setDomainAxis(axisX); xyplot.getDomainAxis().setTickLabelPaint(Color.BLACK); xyplot.getDomainAxis().setLabelPaint(Color.BLACK); xyplot.getDomainAxis().setTickLabelFont(LookAndFeelEntropy.FUENTE_REGULAR); xyplot.getRangeAxis().setTickLabelPaint(Color.BLACK); xyplot.getRangeAxis().setLabelPaint(Color.BLACK); xyplot.getRangeAxis().setTickLabelFont(LookAndFeelEntropy.FUENTE_REGULAR); // Seteamos porcentaje final NumberAxis rangeAxis = (NumberAxis) xyplot.getRangeAxis(); if (porcentaje) { rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance()); } else { rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } // Seteamos transparencia de fondo Plot plot = chart.getPlot(); plot.setBackgroundPaint(null); plot.setBackgroundImageAlpha(0.0f); plot.setOutlineVisible(false); // Generamos una imagen this.lastChart = chart; return chart.createBufferedImage(anchoImagen, altoImagen); }
private JFreeChart createChart() { // create the chart... JFreeChart chart = ChartFactory.createXYLineChart( null, // chart title null, // x axis label null, // y axis label null, // data PlotOrientation.VERTICAL, false, // include legend true, // tooltips false // urls ); chart.setBackgroundPaint(Color.white); // get a reference to the plot for further customization... XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.WHITE); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setDomainGridlinePaint(Color.LIGHT_GRAY); plot.setRangeGridlinePaint(Color.LIGHT_GRAY); // domain axis if ((indexAxis >= 0) && (!dataTable.isNominal(indexAxis))) { if ((dataTable.isDate(indexAxis)) || (dataTable.isDateTime(indexAxis))) { DateAxis domainAxis = new DateAxis(dataTable.getColumnName(indexAxis)); domainAxis.setTimeZone(Tools.getPreferredTimeZone()); chart.getXYPlot().setDomainAxis(domainAxis); } } else { plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits(Locale.US)); ((NumberAxis) plot.getDomainAxis()).setAutoRangeStickyZero(false); ((NumberAxis) plot.getDomainAxis()).setAutoRangeIncludesZero(false); } ValueAxis xAxis = plot.getDomainAxis(); if (indexAxis > -1) xAxis.setLabel(getDataTable().getColumnName(indexAxis)); else xAxis.setLabel(SERIESINDEX_LABEL); xAxis.setAutoRange(true); xAxis.setLabelFont(LABEL_FONT_BOLD); xAxis.setTickLabelFont(LABEL_FONT); xAxis.setVerticalTickLabels(isLabelRotating()); if (indexAxis > 0) { if (getRangeForDimension(indexAxis) != null) { xAxis.setRange(getRangeForDimension(indexAxis)); } } else { if (getRangeForName(SERIESINDEX_LABEL) != null) { xAxis.setRange(getRangeForName(SERIESINDEX_LABEL)); } } // renderer and range axis synchronized (dataTable) { int numberOfSelectedColumns = 0; for (int c = 0; c < dataTable.getNumberOfColumns(); c++) { if (getPlotColumn(c)) { if (dataTable.isNumerical(c)) { numberOfSelectedColumns++; } } } int columnCount = 0; for (int c = 0; c < dataTable.getNumberOfColumns(); c++) { if (getPlotColumn(c)) { if (dataTable.isNumerical(c)) { // YIntervalSeries series = new YIntervalSeries(this.dataTable.getColumnName(c)); XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries series = new XYSeries(dataTable.getColumnName(c)); Iterator<DataTableRow> i = dataTable.iterator(); int index = 1; while (i.hasNext()) { DataTableRow row = i.next(); double value = row.getValue(c); if ((indexAxis >= 0) && (!dataTable.isNominal(indexAxis))) { double indexValue = row.getValue(indexAxis); series.add(indexValue, value); } else { series.add(index++, value); } } dataset.addSeries(series); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); Color color = getColorProvider().getPointColor(1.0d); if (numberOfSelectedColumns > 1) { color = getColorProvider() .getPointColor(columnCount / (double) (numberOfSelectedColumns - 1)); } renderer.setSeriesPaint(0, color); renderer.setSeriesStroke( 0, new BasicStroke(1.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); renderer.setSeriesShapesVisible(0, false); NumberAxis yAxis = new NumberAxis(dataTable.getColumnName(c)); if (getRangeForDimension(c) != null) { yAxis.setRange(getRangeForDimension(c)); } else { yAxis.setAutoRange(true); yAxis.setAutoRangeStickyZero(false); yAxis.setAutoRangeIncludesZero(false); } yAxis.setLabelFont(LABEL_FONT_BOLD); yAxis.setTickLabelFont(LABEL_FONT); if (numberOfSelectedColumns > 1) { yAxis.setAxisLinePaint(color); yAxis.setTickMarkPaint(color); yAxis.setLabelPaint(color); yAxis.setTickLabelPaint(color); } plot.setRangeAxis(columnCount, yAxis); plot.setRangeAxisLocation(columnCount, AxisLocation.TOP_OR_LEFT); plot.setDataset(columnCount, dataset); plot.setRenderer(columnCount, renderer); plot.mapDatasetToRangeAxis(columnCount, columnCount); columnCount++; } } } } chart.setBackgroundPaint(Color.white); return chart; }