Example usage for org.jfree.chart JFreeChart addLegend

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

Introduction

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

Prototype

public void addLegend(LegendTitle legend) 

Source Link

Document

Adds a legend to the plot and sends a ChartChangeEvent to all registered listeners.

Usage

From source file:ec.nbdemetra.ui.chart3d.functions.Functions2DChart.java

private JFreeChart createChart() {
    XYPlot plot = new XYPlot();

    plot.setDataset(0, Charts.emptyXYDataset());
    plot.setRenderer(0, functionRenderer);
    plot.mapDatasetToDomainAxis(0, 0);// w  ww .  j a va2s . c  o m
    plot.mapDatasetToRangeAxis(0, 0);

    plot.setDataset(1, Charts.emptyXYDataset());
    plot.setRenderer(1, optimumRenderer);
    plot.mapDatasetToDomainAxis(1, 0);
    plot.mapDatasetToRangeAxis(1, 0);

    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    JFreeChart result = new JFreeChart("", TsCharts.CHART_TITLE_FONT, plot, false);

    LegendTitle legend = new LegendTitle(result.getPlot());
    legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
    legend.setFrame(new LineBorder());
    legend.setBackgroundPaint(Color.white);
    legend.setPosition(RectangleEdge.BOTTOM);
    result.addLegend(legend);

    result.setPadding(TsCharts.CHART_PADDING);
    return result;
}

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

protected JFreeChart createLegendChart(JFreeChart origchart) {

    JFreeChart legendChart = new JFreeChart("", null, new HiddenPlot(), false);

    legendChart.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) origchart.getPlot();

    LegendTitle legendTitle = new LegendTitle(plot,
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0),
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0));
    legendChart.addLegend(legendTitle);

    return legendChart;

}

From source file:edu.cudenver.bios.chartsvc.resource.ScatterPlotResource.java

/**
 * Create a JFreeChart object from the chart specification.  JFreechart provides 
 * functionality to render 2D scatter plots as jpeg images
 * //from   w ww  .  ja v a  2s.  co m
 * @param chart chart specification object
 * @return JFreeChart object
 * @throws ResourceException
 */
private JFreeChart buildScatterPlot(Chart chart) throws ResourceException {
    ArrayList<LineStyle> lineStyleList = chart.getLineStyleList();

    float dashedLength = 1.0f;
    float spaceLength = 1.0f;
    float thickness = 1.0f;

    // the first series is treated as the x values
    if (chart.getSeries() == null || chart.getSeries().size() <= 0)
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No data series specified");

    // create the jfree chart series
    XYSeriesCollection chartData = new XYSeriesCollection();
    // use a spline renderer to make the connecting lines smooth
    XYSplineRenderer rend = new XYSplineRenderer();

    int seriesIdx = 0;

    for (Series series : chart.getSeries()) {
        XYSeries xySeries = new XYSeries(series.getLabel());

        List<Double> xList = series.getXCoordinates();
        List<Double> yList = series.getYCoordinates();
        if (xList != null && yList != null && xList.size() == yList.size()) {
            for (int i = 0; i < xList.size(); i++) {
                xySeries.add(xList.get(i), yList.get(i));
            }
        }

        if (seriesIdx >= 0 && seriesIdx < lineStyleList.size()) {
            LineStyle lineStyle = lineStyleList.get(seriesIdx);
            dashedLength = (float) lineStyle.getDashLength();
            spaceLength = (float) lineStyle.getSpaceLength();
            thickness = (float) lineStyle.getWidth();
        } else {
            dashedLength = 1.0f;
            spaceLength = 1.0f;
            thickness = 1.0f;
        }

        // set the line style
        rend.setSeriesPaint(seriesIdx, Color.BLACK);

        if (seriesIdx >= 0) {
            /*rend.setSeriesStroke(seriesIdx, 
                  new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {(float) seriesIdx, (float) 2*seriesIdx}, 0.0f));*/
            rend.setSeriesStroke(seriesIdx, new BasicStroke(thickness, BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_ROUND, 1.0f, new float[] { dashedLength, spaceLength }, 0.0f));
        }
        // add the series to the data set
        chartData.addSeries(xySeries);
        seriesIdx++;
    }

    // turn off shapes displayed at each data point to make a smooth curve
    rend.setBaseShapesVisible(false);

    // Create the line chart
    NumberAxis xAxis = new NumberAxis();
    xAxis.setAutoRangeIncludesZero(false);
    if (chart.getXAxis() != null) {
        Axis xAxisSpec = chart.getXAxis();
        xAxis.setLabel(xAxisSpec.getLabel());
        if (!Double.isNaN(xAxisSpec.getRangeMin()) && !Double.isNaN(xAxisSpec.getRangeMax())) {
            xAxis.setRange(xAxisSpec.getRangeMin(), xAxisSpec.getRangeMax());
        }
    }
    NumberAxis yAxis = new NumberAxis();
    if (chart.getYAxis() != null) {
        Axis yAxisSpec = chart.getYAxis();
        yAxis.setLabel(chart.getYAxis().getLabel());
        if (!Double.isNaN(yAxisSpec.getRangeMin()) && !Double.isNaN(yAxisSpec.getRangeMax())) {
            xAxis.setRange(yAxisSpec.getRangeMin(), yAxisSpec.getRangeMax());
        }
    }
    XYPlot plot = new XYPlot((XYDataset) chartData, xAxis, yAxis, rend);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);

    JFreeChart renderedChart = new JFreeChart(chart.getTitle(), JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    renderedChart.setBackgroundPaint(Color.WHITE);
    if (chart.hasLegend()) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        legend.setPosition(RectangleEdge.BOTTOM);
        renderedChart.addLegend(legend);
    }

    return renderedChart;
}

From source file:com.graphhopper.jsprit.analysis.toolbox.Plotter.java

private void plot(VehicleRoutingProblem vrp, final Collection<VehicleRoute> routes, String pngFile,
        String title) {/*  ww w .  j  ava 2 s. c  o m*/
    log.info("plot to {}", pngFile);
    XYSeriesCollection problem;
    XYSeriesCollection solution = null;
    final XYSeriesCollection shipments;
    try {
        retrieveActivities(vrp);
        problem = new XYSeriesCollection(activities);
        shipments = makeShipmentSeries(vrp.getJobs().values());
        if (routes != null)
            solution = makeSolutionSeries(vrp, routes);
    } catch (NoLocationFoundException e) {
        log.warn("cannot plot vrp, since coord is missing");
        return;
    }
    final XYPlot plot = createPlot(problem, shipments, solution);
    JFreeChart chart = new JFreeChart(title, plot);

    LegendTitle legend = createLegend(routes, shipments, plot);
    chart.removeLegend();
    chart.addLegend(legend);

    save(chart, pngFile);

}

From source file:jspritTest.util.Plotter.java

private void plot(VehicleRoutingProblem vrp, final Collection<VehicleRoute> routes, String pngFile,
        String title) {/*from ww w  .  ja v a 2 s  .  c o  m*/
    log.info("plot to {}", pngFile);
    XYSeriesCollection problem;
    XYSeriesCollection solution = null;
    final XYSeriesCollection shipments;
    try {
        retrieveActivities(vrp); //this is where the activities are set

        if (activities == null)
            log.info("activities is null");

        problem = new XYSeriesCollection(activities);
        shipments = makeShipmentSeries(vrp.getJobs().values());
        if (routes != null)
            solution = makeSolutionSeries(vrp, routes);
    } catch (NoLocationFoundException e) {
        log.warn("cannot plot vrp, since coord is missing");
        return;
    }
    final XYPlot plot = createPlot(problem, shipments, solution);
    JFreeChart chart = new JFreeChart(title, plot);

    LegendTitle legend = createLegend(routes, shipments, plot);
    chart.removeLegend();
    chart.addLegend(legend);

    save(chart, pngFile);

}

From source file:edu.ucla.stat.SOCR.chart.SuperBoxAndWhiskerChart.java

protected JFreeChart createLegendChart(JFreeChart origchart) {

    JFreeChart legendChart = new JFreeChart("", null, new HiddenPlot(), false);

    legendChart.setBackgroundPaint(Color.white);
    CategoryPlot plot = origchart.getCategoryPlot();

    LegendTitle legendTitle = new LegendTitle(plot,
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0),
            new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0));
    legendChart.addLegend(legendTitle);

    return legendChart;

}

From source file:edu.ucla.stat.SOCR.chart.SuperPieChart.java

protected JFreeChart createLegendChart(JFreeChart origchart) {

    JFreeChart legendChart = new JFreeChart("", null, new HiddenPlot(), false);

    legendChart.setBackgroundPaint(Color.white);
    PiePlot plot = (PiePlot) origchart.getPlot();

    /* LegendTitle legendTitle = new LegendTitle(plot, 
           new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0), 
           new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0)); */

    LegendTitle legendTitle = new LegendTitle(plot);

    legendChart.addLegend(legendTitle);

    return legendChart;

}

From source file:com.AandR.beans.plotting.LinePlotPanel.LinePlotPanel.java

private ChartPanel createChartPanel() {
    JFreeChart xyChart = ChartFactory.createXYLineChart("f", "x", "y", plotSeries, PlotOrientation.VERTICAL,
            false, true, false);//from  w  w  w .ja va 2  s .c om
    RenderingHints hints = xyChart.getRenderingHints();
    hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    xyChart.setBackgroundPaint(Color.WHITE);

    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyChart.getXYPlot().getRenderer();
    renderer.setBaseLinesVisible(isLineVisible);
    renderer.setBaseShapesVisible(isSymbolVisible);

    LegendTitle legend = new LegendTitle(renderer);
    legend.setPosition(RectangleEdge.BOTTOM);
    xyChart.addLegend(legend);

    xyChart.getXYPlot().getRangeAxis().setStandardTickUnits(createTickUnits());
    //xyChart.getXYPlot().getRangeAxis().setStandardTickUnits(new StandardTickUnitSource());
    xyChart.getXYPlot().getRangeAxis().setAutoRangeMinimumSize(1.0e-45);

    chartPanel = new ChartPanel(xyChart);

    JPopupMenu popup = chartPanel.getPopupMenu();
    popup.remove(1); // removes separator
    popup.remove(1); // removes save as...
    popup.add(createLinePropMenu());
    popup.add(createAxesPropMenu());
    popup.addSeparator();
    popup.add(createExportMenu());
    return chartPanel;
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.JFreeChartPlotEngine.java

/**
 * @param chart/*from  w  w w .  ja v  a 2s. co m*/
 */
private void formatLegend(JFreeChart chart) {
    List<LegendTitle> legendTitles = createLegendTitles();

    while (chart.getLegend() != null) {
        chart.removeLegend();
    }
    for (LegendTitle legendTitle : legendTitles) {
        chart.addLegend(legendTitle);
    }

    // set legend font
    Font legendFont = plotInstance.getCurrentPlotConfigurationClone().getLegendConfiguration().getLegendFont();
    if (legendFont != null) {
        for (LegendTitle legendTitle : legendTitles) {
            legendTitle.setItemFont(legendFont);
        }
    }
}

From source file:com.ikon.servlet.admin.StatsGraphServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String action = WebUtils.getString(request, "action", "graph");
    String type = WebUtils.getString(request, "t");
    JFreeChart chart = null;
    updateSessionManager(request);// w ww.j a v a  2s . c o  m

    try {
        if ("refresh".equals(action)) {
            new RepositoryInfo().runAs(null);
            ServletContext sc = getServletContext();
            sc.getRequestDispatcher("/admin/stats.jsp").forward(request, response);
        } else {
            response.setContentType("image/png");
            OutputStream out = response.getOutputStream();

            if (DOCUMENTS.equals(type) || DOCUMENTS_SIZE.equals(type) || FOLDERS.equals(type)) {
                chart = repoStats(type);
            } else if (DISK.equals(type)) {
                chart = diskStats();
            } else if (JVM_MEMORY.equals(type)) {
                chart = jvmMemStats();
            } else if (OS_MEMORY.equals(type)) {
                chart = osMemStats();
            }

            if (chart != null) {
                // Customize title font
                chart.getTitle().setFont(new Font("Tahoma", Font.BOLD, 16));

                // Match body {   background-color:#F6F6EE; }
                chart.setBackgroundPaint(new Color(246, 246, 238));

                // Customize no data
                PiePlot plot = (PiePlot) chart.getPlot();
                plot.setNoDataMessage("No data to display");

                // Customize labels
                plot.setLabelGenerator(null);

                // Customize legend
                LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
                legend.setPosition(RectangleEdge.BOTTOM);
                legend.setFrame(BlockBorder.NONE);
                legend.setItemFont(new Font("Tahoma", Font.PLAIN, 12));
                chart.removeLegend();
                chart.addLegend(legend);

                if (DISK.equals(type) || JVM_MEMORY.equals(type) || OS_MEMORY.equals(type)) {
                    ChartUtilities.writeChartAsPNG(out, chart, 225, 225);
                } else {
                    ChartUtilities.writeChartAsPNG(out, chart, 250, 250);
                }
            }

            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}