/** * Handles actions. * * @param e the action event. */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if ("BY_KEY".equals(cmd)) { if (!this.ascendingByKey) { this.dataset.sortByKeys(SortOrder.ASCENDING); this.ascendingByKey = true; } else { this.dataset.sortByKeys(SortOrder.DESCENDING); this.ascendingByKey = false; } } else if ("BY_VALUE".equals(cmd)) { if (!this.ascendingByValue) { this.dataset.sortByValues(SortOrder.ASCENDING); this.ascendingByValue = true; } else { this.dataset.sortByValues(SortOrder.DESCENDING); this.ascendingByValue = false; } } else if ("RANDOM".equals(cmd)) { // we create a new dataset here - that's a bit wasteful, // but the DefaultPieDataset will need new methods before we // can shuffle it 'in place'... List keys = new ArrayList(this.dataset.getKeys()); Collections.shuffle(keys); DefaultPieDataset pd = new DefaultPieDataset(); Iterator iterator = keys.iterator(); while (iterator.hasNext()) { Comparable key = (Comparable) iterator.next(); pd.setValue(key, this.dataset.getValue(key)); } PiePlot plot = (PiePlot) this.chart.getPlot(); plot.setDataset(pd); this.dataset = pd; } else if ("LABELS".equals(cmd)) { PiePlot plot = (PiePlot) this.chart.getPlot(); boolean simple = plot.getSimpleLabels(); if (simple) { plot.setInteriorGap(0.05); plot.setSimpleLabels(false); } else { plot.setInteriorGap(0.01); plot.setSimpleLabels(true); } } }
/** * Creates a chart. * * @param dataset the dataset. * @return A chart. */ private static JFreeChart createChart(PieDataset dataset) { JFreeChart chart = ChartFactory.createPieChart( "Pie Chart Demo 1", // chart title dataset, // data true, // include legend true, false); PiePlot plot = (PiePlot) chart.getPlot(); plot.setSectionOutlinesVisible(false); plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 10)); plot.setNoDataMessage("No data available"); plot.setSimpleLabels(true); return chart; }
private BufferedImage generarGraficoTorta( String titulo, double[] valores, String[] funciones, int anchoImagen, int altoImagen) { if (valores.length != funciones.length) { return null; } DefaultPieDataset pieDataset = new DefaultPieDataset(); for (int i = 0; i < valores.length; i++) { pieDataset.setValue(funciones[i], valores[i]); } JFreeChart chart = ChartFactory.createPieChart(titulo, pieDataset, true, true, false); chart.setBackgroundPaint(null); chart.getTitle().setPaint(LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL); chart.getTitle().setFont(LookAndFeelEntropy.FUENTE_TITULO_GRANDE); chart.setBorderVisible(false); chart.getLegend().setItemFont(LookAndFeelEntropy.FUENTE_REGULAR); chart.getLegend().setBackgroundPaint(LookAndFeelEntropy.COLOR_TABLA_PRIMARIO); PiePlot plot = (PiePlot) chart.getPlot(); plot.setBackgroundPaint(null); plot.setBackgroundImageAlpha(0.0f); plot.setSimpleLabels(true); plot.setOutlineVisible(false); for (int i = 0; i < valores.length; i++) { plot.setSectionPaint( funciones[i], generarColorAleatorio(Color.ORANGE)); // LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL)); plot.setExplodePercent(funciones[i], 0.10); } PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator( "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0%")); plot.setLabelGenerator(gen); this.lastChart = chart; // Generamos una imagen return chart.createBufferedImage(anchoImagen, altoImagen); }
protected void buildChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); chart = ChartFactory.createMultiplePieChart( "Untitled Chart", dataset, org.jfree.util.TableOrder.BY_COLUMN, false, true, false); chart.setAntiAlias(true); // chartPanel = new ScrollableChartPanel(chart, true); chartPanel = buildChartPanel(chart); // chartHolder.getViewport().setView(chartPanel); setChartPanel(chartPanel); JFreeChart baseChart = (JFreeChart) ((MultiplePiePlot) (chart.getPlot())).getPieChart(); PiePlot base = (PiePlot) (baseChart.getPlot()); base.setIgnoreZeroValues(true); base.setLabelOutlinePaint(java.awt.Color.WHITE); base.setLabelShadowPaint(java.awt.Color.WHITE); base.setMaximumLabelWidth( 0.25); // allow bigger labels by a bit (this will make the chart smaller) base.setInteriorGap(0.000); // allow stretch to compensate for the bigger label width base.setLabelBackgroundPaint(java.awt.Color.WHITE); base.setOutlinePaint(null); base.setBackgroundPaint(null); base.setShadowPaint(null); base.setSimpleLabels(false); // I think they're false anyway // change the look of the series title to be smaller StandardChartTheme theme = new StandardChartTheme("Hi"); TextTitle title = new TextTitle("Whatever", theme.getLargeFont()); title.setPaint(theme.getAxisLabelPaint()); title.setPosition(RectangleEdge.BOTTOM); baseChart.setTitle(title); // this must come last because the chart must exist for us to set its dataset setSeriesDataset(dataset); }