Example usage for org.jfree.chart JFreeChart removeLegend

List of usage examples for org.jfree.chart JFreeChart removeLegend

Introduction

In this page you can find the example usage for org.jfree.chart JFreeChart removeLegend.

Prototype

public void removeLegend() 

Source Link

Document

Removes the first legend in the chart and sends a ChartChangeEvent to all registered listeners.

Usage

From source file:loci.slim.ui.DecayGraph.java

/**
 * Creates the chart/*from w ww  . ja  v  a2 s  . c  om*/
 *
 * @param bins number of bins
 * @param timeInc time increment per bin
 * @return the chart
 */
JFreeChart createCombinedChart(final int bins, final double timeInc) {

    // create empty chart data sets
    _decayDataset = new XYSeriesCollection();
    _residualDataset = new XYSeriesCollection();

    // make a common horizontal axis for both sub-plots
    final NumberAxis timeAxis = new NumberAxis(TIME_AXIS_LABEL);
    timeAxis.setLabel(UNITS_LABEL);
    timeAxis.setRange(0.0, (bins - 1) * timeInc);

    // make a vertically combined plot
    final CombinedDomainXYPlot parent = new CombinedDomainXYPlot(timeAxis);

    // create decay sub-plot
    NumberAxis photonAxis;
    if (_logarithmic) {
        photonAxis = new LogarithmicAxis(PHOTON_AXIS_LABEL);
    } else {
        photonAxis = new NumberAxis(PHOTON_AXIS_LABEL);
    }
    final XYSplineRenderer decayRenderer = new XYSplineRenderer();
    decayRenderer.setSeriesShapesVisible(0, false);
    decayRenderer.setSeriesShapesVisible(1, false);
    decayRenderer.setSeriesLinesVisible(2, false);
    decayRenderer.setSeriesShape(2, new Ellipse2D.Float(-1.0f, -1.0f, 2.0f, 2.0f));

    decayRenderer.setSeriesPaint(0, PROMPT_COLOR);
    decayRenderer.setSeriesPaint(1, FITTED_COLOR);
    decayRenderer.setSeriesPaint(2, DECAY_COLOR);

    _decaySubPlot = new XYPlot(_decayDataset, null, photonAxis, decayRenderer);
    _decaySubPlot.setDomainCrosshairVisible(true);
    _decaySubPlot.setRangeCrosshairVisible(true);

    // add decay sub-plot to parent
    parent.add(_decaySubPlot, DECAY_WEIGHT);

    // create residual sub-plot
    final NumberAxis residualAxis = new NumberAxis(RESIDUAL_AXIS_LABEL);
    final XYSplineRenderer residualRenderer = new XYSplineRenderer();
    residualRenderer.setSeriesPaint(0, RESIDUAL_COLOR);
    residualRenderer.setSeriesLinesVisible(0, false);
    residualRenderer.setSeriesShape(0, new Ellipse2D.Float(-1.0f, -1.0f, 2.0f, 2.0f));

    final XYPlot residualSubPlot = new XYPlot(_residualDataset, null, residualAxis, residualRenderer);
    residualSubPlot.setDomainCrosshairVisible(true);
    residualSubPlot.setRangeCrosshairVisible(true);
    residualSubPlot.setFixedLegendItems(null);

    // add residual sub-plot to parent
    parent.add(residualSubPlot, RESIDUAL_WEIGHT);

    // now make the top level JFreeChart
    final JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, parent, true);
    chart.removeLegend();

    return chart;
}

From source file:edu.ucla.stat.SOCR.chart.demo.PolarChartDemo1.java

/**
 * Creates a sample chart./* w  w w  .  j  ava  2s  . c  om*/
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
protected JFreeChart createChart(XYDataset dataset) {

    // create the chart...
    JFreeChart chart = ChartFactory.createPolarChart(chartTitle, dataset, true, false, false);

    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    PolarPlot plot = (PolarPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    if (isDemo) {
        plot.addCornerTextItem("Corner Item 1");
        plot.addCornerTextItem("Corner Item 2");
    }

    plot.setRenderer(new SOCRPolarItemRenderer());
    //PolarItemRenderer renderer = plot.getRenderer();
    //renderer.setLegendItemLabelGenerator(new SOCRXYSeriesLabelGenerator());

    // set the range axis to display integers only...
    NumberAxis rangeAxis = (NumberAxis) plot.getAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    setXSummary(dataset);
    if (legendPanelOn)
        chart.removeLegend();

    return chart;

}

From source file:roaded.MainGUI.java

public void loadGraphInfo() {
    if (busyStatus) {
        infoBox("Please load 311 data first on the data tab.", "Message");
        return;//from   w w w.  ja v a2s .co m
    }
    pieDataset.clear();

    // Store coordinates of each related ticket incident
    Coordinates coordinateData = new Coordinates();
    coords.clear();
    for (int i = 0; i < 12; i++) {
        int counter = 0;
        if (wardsSelected[i] == true) {
            for (TicketData entry : ticketObj.getData()) {
                if (i < 10) {
                    if (entry.getWard() != null && entry.getWard().compareTo("WARD 0" + (i + 1)) == 0) {
                        counter++;
                        coordinateData = new Coordinates();
                        coordinateData.setCoordinates(entry.getLoc_lat(), entry.getLoc_long());
                        coords.add(coordinateData);
                    }
                } else {
                    if (entry.getWard() != null && entry.getWard().compareTo("WARD 1" + (i - 9)) == 0) {
                        counter++;
                        coordinateData = new Coordinates();
                        coordinateData.setCoordinates(entry.getLoc_lat(), entry.getLoc_long());
                        coords.add(coordinateData);
                    }
                }
            }
            pieDataset.setValue("Ward " + (i + 1), counter);
            counter = 0;
        }

    }

    // Export to file so that pin mapper can retrieve it
    try {
        PrintStream out = new PrintStream(new FileOutputStream("plotData.txt"));
        for (Coordinates coord : coords) {
            out.println(coord);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Loads the pie chart for all wards selected
    JFreeChart chart = ChartFactory.createPieChart(wardNumber, pieDataset);
    chart.removeLegend();
    PieChart.setLayout(new java.awt.BorderLayout());
    ChartPanel CP = new ChartPanel(chart);
    CP.addChartMouseListener(CML);
    CP.setPreferredSize(new Dimension(785, 440)); //size according to my window
    CP.setMouseWheelEnabled(true);
    PieChart.add(CP);

    //bargraph
    dataset.clear(); //resets bargraph when new wards are elected
    CategoryDataset dataset1 = createDataset1(); //uses dataset to create the bargraph
    JFreeChart chart1 = ChartFactory.createBarChart(
            // bargraph labels and orientation
            "Ward Incidents", "Wards", "Incidents", dataset1, PlotOrientation.VERTICAL, true, true, false);
    //bargraph settings
    DualAxis.setLayout(new java.awt.BorderLayout());
    chart1.setBackgroundPaint(Color.white);

    final CategoryPlot plot = chart1.getCategoryPlot();
    plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

    final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
    renderer2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
    plot.setRenderer(1, renderer2);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);

    //bar graph chart panel creation 
    final ChartPanel chartPanel = new ChartPanel(chart1);
    chartPanel.setDomainZoomable(true);
    chartPanel.setRangeZoomable(true);
    chartPanel.setMouseZoomable(true);
    chartPanel.setMouseWheelEnabled(true);

    DualAxis.add(chartPanel);
}

From source file:it.eng.spagobi.engines.chart.bo.charttypes.linecharts.LineChart.java

public JFreeChart createChart() {

    logger.debug("IN");
    CategoryPlot plot = new CategoryPlot();

    NumberAxis rangeAxis = new NumberAxis("Kpi Values");
    rangeAxis.setLabelFont(new Font("Arial", Font.PLAIN, 12));
    Color colorLabel = Color.decode("#000000");
    rangeAxis.setLabelPaint(colorLabel);
    rangeAxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
    rangeAxis.setTickLabelPaint(colorLabel);
    plot.setRangeAxis(rangeAxis);//from  w  w w.j a va 2s . c  om

    CategoryAxis domainAxis = new CategoryAxis();
    domainAxis.setLabelFont(new Font("Arial", Font.PLAIN, 10));
    domainAxis.setLabelPaint(colorLabel);
    domainAxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
    domainAxis.setTickLabelPaint(colorLabel);
    plot.setDomainAxis(domainAxis);

    plot.setOrientation(PlotOrientation.VERTICAL);
    plot.setRangeGridlinesVisible(true);
    plot.setDomainGridlinesVisible(true);

    //I create a line renderer 
    MyStandardCategoryItemLabelGenerator generator = null;

    LineAndShapeRenderer lineRenderer = new LineAndShapeRenderer();
    lineRenderer.setShapesFilled(true);
    lineRenderer.setBaseItemLabelGenerator(generator);
    lineRenderer.setBaseItemLabelFont(new Font("Arial", Font.PLAIN, 12));
    lineRenderer.setBaseItemLabelPaint(colorLabel);
    lineRenderer.setBaseItemLabelsVisible(true);

    DefaultCategoryDataset datasetLine = (DefaultCategoryDataset) datasetMap.getDatasets().get("line");

    for (Iterator iterator = datasetLine.getRowKeys().iterator(); iterator.hasNext();) {
        String serName = (String) iterator.next();
        String labelName = "";
        int index = -1;
        index = datasetLine.getRowIndex(serName);

        Color color = Color.decode("#990200");
        lineRenderer.setSeriesPaint(index, color);
    }

    plot.setDataset(0, datasetLine);
    plot.setRenderer(0, lineRenderer);

    plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    JFreeChart chart = new JFreeChart(plot);
    logger.debug("Chart created");
    TextTitle title = new TextTitle(name, new Font("Arial", Font.BOLD, 16), Color.decode("#990200"),
            RectangleEdge.TOP, HorizontalAlignment.CENTER, VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS);
    chart.setTitle(title);
    TextTitle subTitle = new TextTitle(subName, new Font("Arial", Font.PLAIN, 12), Color.decode("#000000"),
            RectangleEdge.TOP, HorizontalAlignment.CENTER, VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS);
    chart.addSubtitle(subTitle);
    TextTitle subTitle2 = new TextTitle(subName, new Font("Arial", Font.PLAIN, 8), Color.decode("#FFFFFF"),
            RectangleEdge.TOP, HorizontalAlignment.CENTER, VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS);
    chart.addSubtitle(subTitle2);
    chart.removeLegend();

    chart.setBackgroundPaint(Color.white);
    logger.debug("OUT");
    return chart;
}

From source file:it.eng.spagobi.engines.kpi.bo.charttypes.trendcharts.LineChart.java

public JFreeChart createChart() {

    logger.debug("IN");
    CategoryPlot plot = new CategoryPlot();
    IMessageBuilder msgBuilder = MessageBuilderFactory.getMessageBuilder();
    String rangeAxisName = msgBuilder.getMessage("sbi.kpi.rangeAxisName");
    NumberAxis rangeAxis = new NumberAxis(rangeAxisName);
    rangeAxis.setLabelFont(new Font("Arial", Font.PLAIN, 12));
    Color colorLabel = Color.decode("#000000");
    rangeAxis.setLabelPaint(colorLabel);
    rangeAxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
    rangeAxis.setTickLabelPaint(colorLabel);
    plot.setRangeAxis(rangeAxis);/*w w w.j  a  va  2s.  co  m*/

    CategoryAxis domainAxis = new CategoryAxis();
    domainAxis.setLabelFont(new Font("Arial", Font.PLAIN, 10));
    domainAxis.setLabelPaint(colorLabel);
    domainAxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
    domainAxis.setTickLabelPaint(colorLabel);
    plot.setDomainAxis(domainAxis);

    plot.setOrientation(PlotOrientation.VERTICAL);
    plot.setRangeGridlinesVisible(true);
    plot.setDomainGridlinesVisible(true);

    //I create a line renderer 
    MyStandardCategoryItemLabelGenerator generator = null;

    LineAndShapeRenderer lineRenderer = new LineAndShapeRenderer();
    lineRenderer.setShapesFilled(true);
    lineRenderer.setBaseItemLabelGenerator(generator);
    lineRenderer.setBaseItemLabelFont(new Font("Arial", Font.PLAIN, 12));
    lineRenderer.setBaseItemLabelPaint(colorLabel);
    lineRenderer.setBaseItemLabelsVisible(true);

    DefaultCategoryDataset datasetLine = (DefaultCategoryDataset) datasetMap.getDatasets().get("line");

    for (Iterator iterator = datasetLine.getRowKeys().iterator(); iterator.hasNext();) {
        String serName = (String) iterator.next();
        String labelName = "";
        int index = -1;
        index = datasetLine.getRowIndex(serName);

        Color color = Color.decode("#990200");
        lineRenderer.setSeriesPaint(index, color);
    }

    plot.setDataset(0, datasetLine);
    plot.setRenderer(0, lineRenderer);

    plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    JFreeChart chart = new JFreeChart(plot);
    logger.debug("Chart created");
    TextTitle title = new TextTitle(name, new Font("Arial", Font.BOLD, 16), Color.decode("#990200"),
            RectangleEdge.TOP, HorizontalAlignment.CENTER, VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS);
    chart.setTitle(title);
    TextTitle subTitle = new TextTitle(subName, new Font("Arial", Font.PLAIN, 12), Color.decode("#000000"),
            RectangleEdge.TOP, HorizontalAlignment.CENTER, VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS);
    chart.addSubtitle(subTitle);
    TextTitle subTitle2 = new TextTitle(subName, new Font("Arial", Font.PLAIN, 8), Color.decode("#FFFFFF"),
            RectangleEdge.TOP, HorizontalAlignment.CENTER, VerticalAlignment.TOP, RectangleInsets.ZERO_INSETS);
    chart.addSubtitle(subTitle2);
    chart.removeLegend();

    chart.setBackgroundPaint(Color.white);
    logger.debug("OUT");
    return chart;
}

From source file:projects.wdlf47tuc.ProcessAllSwathcal.java

/**
 * Plots the CMD using the existing dataset. Used whenever chart annotations change, without the
 * underlying plot data changing. This method identifies all sources lying within the boxed region
 * and loads them into the secondary list {@link #boxedSources}.
 * /*from  ww  w .java2 s .  c  o m*/
 * @param allSources
 *    The {@link Source}s to plot
 * @return
 *    A JFreeChart presenting the colour-magnitude diagram for the current selection criteria and colours.
 */
private JFreeChart plotCmd() {

    XYSeries outside = new XYSeries("Outside");
    XYSeries inside = new XYSeries("Inside");

    // Use a Path2D.Double instance to determine polygon intersection
    Path2D.Double path = new Path2D.Double();
    boxedSources.clear();

    boolean performBoxSelection = (points.size() > 2);

    if (performBoxSelection) {
        // Initialise Path2D object
        path.moveTo(points.get(0)[0], points.get(0)[1]);
        for (double[] point : points) {
            path.lineTo(point[0], point[1]);
        }
    }

    for (Source source : selectedSources) {
        double magnitude = source.getMag(magFilter);
        double col1 = source.getMag(col1Filter);
        double col2 = source.getMag(col2Filter);

        double x = col1 - col2;
        double y = magnitude;

        if (performBoxSelection) {
            Point2D.Double point = new Point2D.Double(x, y);
            if (path.contains(point)) {
                inside.add(x, y);
                boxedSources.add(source);
            } else {
                outside.add(x, y);
            }
        } else {
            outside.add(x, y);
        }
    }

    final XYSeriesCollection data = new XYSeriesCollection();
    data.addSeries(outside);
    data.addSeries(inside);

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, false);
    renderer.setSeriesShapesVisible(0, true);
    renderer.setSeriesShape(0, new Ellipse2D.Float(-0.5f, -0.5f, 1, 1));
    renderer.setSeriesPaint(0, ChartColor.BLACK);

    renderer.setSeriesLinesVisible(1, false);
    renderer.setSeriesShapesVisible(1, true);
    renderer.setSeriesShape(1, new Ellipse2D.Float(-0.5f, -0.5f, 1, 1));
    renderer.setSeriesPaint(1, ChartColor.RED);

    NumberAxis xAxis = new NumberAxis(col1Filter.toString() + " - " + col2Filter.toString());
    xAxis.setRange(getXRange());

    NumberAxis yAxis = new NumberAxis(magFilter.toString());
    yAxis.setRange(getYRange());
    yAxis.setInverted(true);

    // Configure plot
    XYPlot xyplot = new XYPlot(data, xAxis, yAxis, renderer);
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinePaint(Color.white);

    // Specify selection box, if points have been specified
    if (!points.isEmpty()) {

        double[] coords = new double[points.size() * 2];

        for (int i = 0; i < points.size(); i++) {
            double[] point = points.get(i);
            coords[2 * i + 0] = point[0];
            coords[2 * i + 1] = point[1];
        }
        XYPolygonAnnotation box = new XYPolygonAnnotation(coords, new BasicStroke(2.0f), Color.BLUE);
        xyplot.addAnnotation(box);
    }

    // Configure chart
    JFreeChart chart = new JFreeChart("47 Tuc CMD", xyplot);
    chart.setBackgroundPaint(Color.white);
    chart.setTitle("47 Tuc colour-magnitude diagram");
    chart.removeLegend();

    return chart;
}

From source file:projects.wdlf47tuc.ProcessAllSwathcal.java

/**
 * Computes the luminosity function for the current boxed region and plots it in a JFrame.
 * Also prints out the coordinates of the selection box vertices and the luminosity function
 * quantities.//from   w ww.  j a v a2s  .co  m
 * 
 * @param sources
 *    The {@link Source}s to compute the luminosity function for.
 */
private void computeAndPlotLuminosityFunction(List<Source> sources) {

    // Print out coordinates of selection box corners
    System.out.println("# Coordinates of selection box corners:");
    System.out.println("# (" + col1Filter + "-" + col2Filter + ")\t" + magFilter);
    for (double[] point : points) {
        System.out.println("# " + point[0] + "\t" + point[1]);
    }
    System.out.println("# Luminosity function:");
    System.out.println("# Mag.\tN\tsigN");

    double magBinWidth = 0.5;

    // Get the range of the data
    double mMin = Double.MAX_VALUE;
    double mMax = -Double.MAX_VALUE;
    for (Source source : sources) {
        double mag = source.getMag(magFilter) - mu;
        mMin = Math.min(mMin, mag);
        mMax = Math.max(mMax, mag);
    }

    // Quantize this to a whole number
    mMin = Math.floor(mMin);
    mMax = Math.ceil(mMax);

    int nBins = (int) Math.rint((mMax - mMin) / magBinWidth);

    // Array to accumulate all objects in each bin
    int[] n = new int[nBins];

    for (Source source : sources) {
        double mag = source.getMag(magFilter) - mu;
        // Bin number
        int bin = (int) Math.floor((mag - mMin) / magBinWidth);
        n[bin]++;
    }

    YIntervalSeries luminosityFunction = new YIntervalSeries("Luminosity Function");

    for (int i = 0; i < nBins; i++) {
        // Bin centre
        double x = mMin + i * magBinWidth + 0.5 * magBinWidth;
        double y = n[i];
        double yErr = n[i] > 0 ? Math.sqrt(y) : 0;
        luminosityFunction.add(x, y, y - yErr, y + yErr);
        System.out.println(x + "\t" + y + "\t" + yErr);
    }

    final YIntervalSeriesCollection data = new YIntervalSeriesCollection();
    data.addSeries(luminosityFunction);

    XYErrorRenderer renderer = new XYErrorRenderer();
    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(0, true);
    renderer.setSeriesShape(0, new Ellipse2D.Float(-1f, -1f, 2, 2));
    renderer.setSeriesPaint(0, ChartColor.BLACK);

    NumberAxis xAxis = new NumberAxis("Absolute Magnitude (" + magFilter.toString() + ")");
    xAxis.setAutoRange(true);
    xAxis.setAutoRangeIncludesZero(false);

    NumberAxis yAxis = new NumberAxis("N");
    yAxis.setAutoRange(true);
    yAxis.setAutoRangeIncludesZero(true);

    // Configure plot
    XYPlot xyplot = new XYPlot(data, xAxis, yAxis, renderer);
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinePaint(Color.white);

    // Configure chart
    JFreeChart chart = new JFreeChart("Luminosity Function", xyplot);
    chart.setBackgroundPaint(Color.white);
    chart.setTitle("47 Tuc luminosity function");
    chart.removeLegend();

    final ChartPanel lfChartPanel = new ChartPanel(chart);

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame tester = new JFrame();
            tester.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            tester.setLayout(new BorderLayout());
            tester.add(lfChartPanel, BorderLayout.CENTER);
            tester.pack();
            tester.setVisible(true);
        }
    });

}

From source file:edu.ucla.stat.SOCR.chart.demo.DotChart.java

protected JFreeChart createChart2(BoxAndWhiskerCategoryDataset dataset) {
    //System.out.println("createChart2 called");
    CategoryAxis domainAxis = new CategoryAxis(null);
    // NumberAxis rangeAxis = new NumberAxis("X");

    //  System.out.println("using the common RangeAxis\n");
    common_rangeAxis.setAutoRange(false);
    // NumberAxis rangeAxis = common_rangeAxis;
    BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
    CategoryPlot plot = new CategoryPlot(dataset, domainAxis, common_rangeAxis, renderer);
    JFreeChart chart = new JFreeChart("", plot);

    chart.setBackgroundPaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.lightGray);
    plot.setOrientation(PlotOrientation.HORIZONTAL);

    //rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    renderer.setFillBox(false);/*www.  jav  a2  s . c  om*/
    // renderer.setLegendItemLabelGenerator(new SOCRCategoryCellLabelGenerator(dataset, values_storage,SERIES_COUNT, CATEGORY_COUNT));

    domainAxis.setLowerMargin(0.46);
    domainAxis.setUpperMargin(0.46);
    chart.removeLegend();
    return chart;

}

From source file:it.eng.spagobi.engines.chart.bo.ChartImpl.java

public void drawLegend(JFreeChart chart) {
    //remove ipotetical other legend
    chart.removeLegend();
    BlockContainer wrapper = new BlockContainer(new BorderArrangement());
    wrapper.setFrame(new BlockBorder(1.0, 1.0, 1.0, 1.0));

    /*LabelBlock titleBlock = new LabelBlock("Legend Items:",
    new Font("SansSerif", Font.BOLD, 12));
    titleBlock.setPadding(5, 5, 5, 5);//from  w w  w . j  a  va 2  s . c o  m
    wrapper.add(titleBlock, RectangleEdge.TOP);*/

    LegendTitle legend = new LegendTitle(chart.getPlot());
    BlockContainer items = legend.getItemContainer();
    if (styleLegend != null && styleLegend.getFont() != null) {
        legend.setItemFont(new Font(styleLegend.getFontName(), Font.BOLD, styleLegend.getSize()));
    }

    items.setPadding(2, 5, 5, 2);
    wrapper.add(items);
    legend.setWrapper(wrapper);

    if (legendPosition.equalsIgnoreCase("bottom"))
        legend.setPosition(RectangleEdge.BOTTOM);
    else if (legendPosition.equalsIgnoreCase("left"))
        legend.setPosition(RectangleEdge.LEFT);
    else if (legendPosition.equalsIgnoreCase("right"))
        legend.setPosition(RectangleEdge.RIGHT);
    else if (legendPosition.equalsIgnoreCase("top"))
        legend.setPosition(RectangleEdge.TOP);
    else
        legend.setPosition(RectangleEdge.BOTTOM);

    legend.setHorizontalAlignment(HorizontalAlignment.CENTER);
    chart.addSubtitle(legend);

}

From source file:edu.dlnu.liuwenpeng.ChartFactory.ChartFactory.java

/**    
* Creates a chart that displays multiple pie plots.  The chart object     
* returned by this method uses a {@link MultiplePiePlot} instance as the    
* plot.    //from  w w  w  . j a  va 2s.  c om
*    
* @param title  the chart title (<code>null</code> permitted).    
* @param dataset  the dataset (<code>null</code> permitted).    
* @param order  the order that the data is extracted (by row or by column)     
*               (<code>null</code> not permitted).    
* @param legend  include a legend?    
* @param tooltips  generate tooltips?    
* @param urls  generate URLs?    
*    
* @return A chart.    
*/
public static JFreeChart createMultiplePieChart3D(String title, CategoryDataset dataset, TableOrder order,
        boolean legend, boolean tooltips, boolean urls) {

    if (order == null) {
        throw new IllegalArgumentException("Null 'order' argument.");
    }
    MultiplePiePlot plot = new MultiplePiePlot(dataset);
    plot.setDataExtractOrder(order);
    plot.setBackgroundPaint(null);
    plot.setOutlineStroke(null);

    JFreeChart pieChart = new JFreeChart(new PiePlot3D(null));
    TextTitle seriesTitle = new TextTitle("Series Title", new Font("SansSerif", Font.BOLD, 12));
    seriesTitle.setPosition(RectangleEdge.BOTTOM);
    pieChart.setTitle(seriesTitle);
    pieChart.removeLegend();
    pieChart.setBackgroundPaint(null);
    plot.setPieChart(pieChart);

    if (tooltips) {
        PieToolTipGenerator tooltipGenerator = new StandardPieToolTipGenerator();
        PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
        pp.setToolTipGenerator(tooltipGenerator);
    }

    if (urls) {
        PieURLGenerator urlGenerator = new StandardPieURLGenerator();
        PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
        pp.setURLGenerator(urlGenerator);
    }

    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);

    return chart;

}