/** * 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 sample chart with the given dataset. * * @param dataset the dataset. * @return A sample chart. */ private JFreeChart createChart(final CategoryDataset dataset) { final JFreeChart chart = ChartFactory.createMultiplePieChart( "Multiple Pie Chart", // chart title dataset, // dataset TableOrder.BY_ROW, true, // include legend true, true); final MultiplePiePlot plot = (MultiplePiePlot) chart.getPlot(); final JFreeChart subchart = plot.getPieChart(); final PiePlot p = (PiePlot) subchart.getPlot(); p.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}")); p.setLabelFont(new Font("SansSerif", Font.PLAIN, 8)); p.setInteriorGap(0.30); return chart; }
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); }